summaryrefslogtreecommitdiffstats
path: root/crates/core/c8y_translator/src/json.rs
blob: 41d8ae357903c3b6ebcdd42b5cd59bb9eef98a23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! A library to translate the ThinEdgeJson into C8yJson
//! Takes thin_edge_json bytes and returns c8y json bytes
//!
//! # Examples
//!
//! ```
//! use c8y_translator::json::from_thin_edge_json;
//! let single_value_thin_edge_json = r#"{
//!        "time": "2020-06-22T17:03:14.000+02:00",
//!        "temperature": 23,
//!        "pressure": 220
//!     }"#;
//! let output = from_thin_edge_json(single_value_thin_edge_json);
//! ```

use crate::serializer;
use clock::{Clock, WallClock};
use thin_edge_json::parser::*;
use time::{self, OffsetDateTime};

#[derive(thiserror::Error, Debug)]
pub enum CumulocityJsonError {
    #[error(transparent)]
    C8yJsonSerializationError(#[from] serializer::C8yJsonSerializationError),

    #[error(transparent)]
    ThinEdgeJsonParserError(#[from] ThinEdgeJsonParserError),
}

/// Converts from thin-edge measurement JSON to C8Y measurement JSON
pub fn from_thin_edge_json(input: &str) -> Result<String, CumulocityJsonError> {
    let timestamp = WallClock.now();
    let c8y_vec = from_thin_edge_json_with_timestamp(input, timestamp, None)?;
    Ok(c8y_vec)
}

/// Converts from thin-edge Json to c8y_json with child id information
pub fn from_thin_edge_json_with_child(
    input: &str,
    child_id: &str,
) -> Result<String, CumulocityJsonError> {
    let timestamp = WallClock.now();
    let c8y_vec = from_thin_edge_json_with_timestamp(input, timestamp, Some(child_id))?;
    Ok(c8y_vec)
}

fn from_thin_edge_json_with_timestamp(
    input: &str,
    timestamp: OffsetDateTime,
    maybe_child_id: Option<&str>,
) -> Result<String, CumulocityJsonError> {
    let mut serializer = serializer::C8yJsonSerializer::new(timestamp, maybe_child_id);
    parse_str(input, &mut serializer)?;
    Ok(serializer.into_string()?)
}

#[cfg(test)]
mod tests {
    use super::*;
    use assert_json_diff::*;
    use proptest::prelude::*;
    use serde_json::{json, Value};
    use test_case::test_case;
    use time::{format_description, macros::datetime};

    #[test]
    fn check_single_value_translation() {
        let single_value_thin_edge_json = r#"{
                  "temperature": 23.0,
                  "pressure": 220.0
               }"#;

        let timestamp = datetime!(2021-04-08 0:00:0 +05:00);

        let output =
            from_thin_edge_json_with_timestamp(single_value_thin_edge_json, timestamp, None);

        let expected_output = json!({
            "type": "ThinEdgeMeasurement",
            "time": timestamp
                .format(&format_description::well_known::Rfc3339)
                .unwrap()
                .as_str(),
            "temperature": {
                "temperature": {
                    "value": 23.0
                }
            },
            "pressure": {
                "pressure": {
                    "value": 220.0
                }
            }
        });

        assert_json_eq!(
            serde_json::from_str::<serde_json::Value>(output.unwrap().as_str()).unwrap(),
            expected_output
        );
    }

    #[test]
    fn check_thin_edge_translation_with_timestamp() {
        let single_value_thin_edge_json = r#"{
                  "time" : "2013-06-22T17:03:14.123+02:00",
                  "temperature": 23.0,
                  "pressure": 220.0
               }"#;

        let expected_output = r#"{
                     "type": "ThinEdgeMeasurement",
                     "time": "2013-06-22T17:03:14.123+02:00",
                     "temperature": {
                         "temperature": {
                               "value": 23.0
                         }
                    },
                    "pressure" : {
                       "pressure": {
                          "value" : 220.0
                          }
                       }
                  }"#;

        let output = from_thin_edge_json(single_value_thin_edge_json);

        assert_eq!(
            expected_output.split_whitespace().collect::<String>(),
            output.unwrap().split_whitespace().collect::<String>()
        );
    }

    #[test]
    fn check_multi_value_translation() {
        let multi_value_thin_edge_json = r#"{
            "temperature": 25.0 ,
            "location": {
                  "latitude": 32.54,
                  "longitude": -117.67,
                  "altitude": 98.6
              },
            "pressure": 98.0
        }"#;

        let timestamp = datetime!(2021-04-08 0:00:0 +05:00);

        let output =
            from_thin_edge_json_with_timestamp(multi_value_thin_edge_json, timestamp, None);

        let expected_output = json!({
            "type": "ThinEdgeMeasurement",
            "time": timestamp
                .format(&format_description::well_known::Rfc3339)
                .unwrap()
                .as_str(),
            "temperature": {
                "temperature": {
                    "value": 25.0
                 }
            },
           "location": {
                "latitude": {
                   "value": 32.54
                 },
                "longitude": {
                  "value": -117.67
                },
                "altitude": {
                  "value": 98.6
               }
          },
         "pressure": {
            "pressure": {
                 "value": 98.0
            }
          }
        });

        assert_json_eq!(
            serde_json::from_str::<serde_json::Value>(output.unwrap().as_str()).unwrap(),
            expected_output
        );
    }

    #[test]
    fn thin_edge_json_round_tiny_number() {
        let input = r#"{
           "time" : "2013-06-22T17:03:14.000+02:00",
           "temperature": 10e-9999999999
          }"#;

        let expected_output = r#"{
             "type": "ThinEdgeMeasurement",
             "time": "2013-06-22T17:03:14+02:00",
             "temperature": {
                 "temperature": {
                    "value": 0.0
                 }
            }
        }"#;

        let output = from_thin_edge_json(input);

        let actual_output = output.unwrap().split_whitespace().collect::<String>();

        assert_eq!(
            expected_output.split_whitespace().collect::<String>(),
            actual_output
        );
    }

    proptest! {

        #[test]
        fn it_works_for_any_measurement(measurement in r#"[a-z]{3,6}"#) {
            if measurement == "time" || measurement == "type" {
                // Skip this test case, since the random measurement name happens to be a reserved key.
                return Ok(());
            }
            let input = format!(r#"{{"time": "2013-06-22T17:03:14.453+02:00",
                        "{}": 123.0
                      }}"#, measurement);
            let time = "2013-06-22T17:03:14.453+02:00";
            let expected_output = format!(r#"{{
                  "type": "ThinEdgeMeasurement",
                  "time": "{}",
                  "{}": {{
                  "{}": {{
                       "value": 123.0
                      }}
                   }}
                }}"#, time, measurement, measurement);

        let output = from_thin_edge_json(input.as_str()).unwrap();
        assert_eq!(
            expected_output.split_whitespace().collect::<String>(),
            output
                .split_whitespace()
                .collect::<String>()
        );
        }
    }

    #[test_case(
    "child1",
    r#"{"temperature": 23.0}"#,
    json!({
        "type": "ThinEdgeMeasurement",
        "externalSource": {"externalId": "child1","type": "c8y_Serial",},
        "time": "2021-04-08T00:00:00+05:00",
        "temperature": {"temperature": {"value": 23.0}}
    })
    ;"child device single value thin-edge json translation")]
    #[test_case(
    "child2",
    r#"{"temperature": 23.0, "pressure": 220.0}"#,
    json!({
        "type": "ThinEdgeMeasurement",
        "externalSource": {"externalId": "child2","type": "c8y_Serial",},
        "time": "2021-04-08T00:00:00+05:00",
        "temperature": {"temperature": {"value": 23.0}},
        "pressure": {"pressure": {"value": 220.0}}
    })
    ;"child device multiple values thin-edge json translation")]
    #[test_case(
    "child3",
    r#"{"temperature": 23.0, "time": "2021-04-23T19:00:00+05:00"}"#,
    json!({
        "type": "ThinEdgeMeasurement",
        "externalSource": {"externalId": "child3","type": "c8y_Serial",},
        "time": "2021-04-23T19:00:00+05:00",
        "temperature": {"temperature": {"value": 23.0}},
    })
    ;"child device single value with timestamp thin-edge json translation")]
    fn check_value_translation_for_child_device(
        child_id: &str,
        thin_edge_json: &str,
        expected_output: Value,
    ) {
        let timestamp = datetime!(2021-04-08 0:00:0 +05:00);
        let output = from_thin_edge_json_with_timestamp(thin_edge_json, timestamp, Some(child_id));
        assert_json_eq!(
            serde_json::from_str::<serde_json::Value>(output.unwrap().as_str()).unwrap(),
            expected_output
        );
    }
}