drogue_client/registry/v1/data/app/
downstream.rs

1use crate::{dialect, Section};
2use serde::{Deserialize, Serialize};
3
4dialect!(DownstreamSpec [Section::Spec => "downstream"]);
5
6/// The application downstream specification.
7#[derive(Clone, Default, Debug, Eq, PartialEq, Deserialize, Serialize)]
8pub struct DownstreamSpec {
9    #[serde(default, skip_serializing_if = "Option::is_none")]
10    pub password: Option<String>,
11}
12
13#[cfg(test)]
14mod test {
15    use crate::{
16        registry::v1::{Application, DownstreamSpec},
17        Translator,
18    };
19    use serde_json::json;
20
21    #[test]
22    fn test_json_default() {
23        let mut app = Application::default();
24        app.set_section(DownstreamSpec::default()).unwrap();
25        let json = serde_json::to_value(&app).unwrap();
26        assert_eq!(json!({"downstream": {}}), json["spec"]);
27    }
28
29    #[test]
30    fn test_deserialize_none() {
31        let app: Application = serde_json::from_value(json!({
32            "metadata": {
33                "name": "foo",
34            }
35        }))
36        .unwrap();
37
38        let spec = app.section::<DownstreamSpec>();
39        assert!(spec.is_none());
40        assert_eq!(
41            spec.transpose().unwrap().unwrap_or_default(),
42            Default::default()
43        );
44    }
45
46    #[test]
47    fn test_deserialize_empty() {
48        let app: Application = serde_json::from_value(json!({
49            "metadata": {
50                "name": "foo",
51            },
52            "spec": {
53                "downstream": {},
54            }
55        }))
56        .unwrap();
57
58        let spec = app.section::<DownstreamSpec>();
59        assert_eq!(spec.transpose().unwrap(), Some(Default::default()));
60    }
61
62    #[test]
63    fn test_deserialize_password() {
64        let app: Application = serde_json::from_value(json!({
65            "metadata": {
66                "name": "foo",
67            },
68            "spec": {
69                "downstream": {
70                    "password": "foobar",
71                },
72            }
73        }))
74        .unwrap();
75
76        let spec = app.section::<DownstreamSpec>();
77        assert_eq!(
78            spec.transpose().unwrap(),
79            Some(DownstreamSpec {
80                password: Some("foobar".to_string())
81            })
82        );
83    }
84}