JsonUtil unit tests
[appc.git] / appc-common / src / test / java / org / onap / appc / util / JsonUtilTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.util;
26
27 import com.fasterxml.jackson.annotation.JsonProperty;
28 import com.fasterxml.jackson.core.JsonParseException;
29 import java.io.FileNotFoundException;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33
34 import java.io.IOException;
35 import java.util.HashMap;
36 import java.util.Map;
37
38 import static org.junit.Assert.*;
39
40
41 public class JsonUtilTest {
42
43     @Test(expected = NullPointerException.class)
44     public void should_throw_exception_when_json_string_is_null() throws IOException {
45
46         JsonUtil.convertJsonStringToFlatMap(null);
47     }
48
49     @Test(expected = JsonParseException.class)
50     public void should_throw_exception_when_invalid_json_string() throws IOException {
51
52         JsonUtil.convertJsonStringToFlatMap("{key: value}");
53     }
54
55     @Test
56     public void should_convert_json_string_to_flat_map() throws IOException {
57
58         String jsonString = "{\"A\":\"A-value\",\"B\":{\"C\":\"B.C-value\",\"D\":\"B.D-value\"}}";
59         Map<String, String> flatMap = JsonUtil.convertJsonStringToFlatMap(jsonString);
60
61         Map<String, String> expectedMap = new HashMap<>();
62         expectedMap.put("A", "A-value");
63         expectedMap.put("B.C", "B.C-value");
64         expectedMap.put("B.D", "B.D-value");
65
66         assertEquals(expectedMap, flatMap);
67         assertNotNull(flatMap);
68     }
69
70     @Test
71     public void should_convert_json_string_to_flat_map_with_nested_json() throws IOException {
72
73         String jsonString = "{\"A\":\"A-value\",\"B\":\"{\\\"C\\\":\\\"C-value\\\",\\\"D\\\":\\\"D-value\\\"}\"}";
74         Map<String, String> flatMap = JsonUtil.convertJsonStringToFlatMap(jsonString);
75
76         Map<String, String> expectedMap = new HashMap<>();
77         expectedMap.put("A", "A-value");
78         expectedMap.put("B", "{\"C\":\"C-value\",\"D\":\"D-value\"}");
79
80         assertEquals(expectedMap, flatMap);
81         assertNotNull(flatMap);
82     }
83
84     @Test(expected = FileNotFoundException.class)
85     public void should_throw_exception_when_not_found_json_file() throws IOException {
86         JsonUtil.readInputJson("not-existing.json", DummyClass.class);
87     }
88
89
90     @Test(expected = JsonParseException.class)
91     public void should_throw_exception_when_invalid_json_file() throws IOException {
92         JsonUtil.readInputJson("/invalid.json", DummyClass.class);
93     }
94
95     @Test
96     public void should_parse_valid_json_file () throws IOException {
97         DummyClass dummyClass = JsonUtil.readInputJson("/valid.json", DummyClass.class);
98
99         assertEquals("dummy name", dummyClass.getName());
100         assertEquals(99, dummyClass.getValue());
101     }
102
103     private static class DummyClass {
104
105         private String name;
106         private int value;
107
108         public DummyClass(@JsonProperty("name") String name, @JsonProperty("value") int someValue) {
109             this.name = name;
110             this.value = someValue;
111         }
112
113         public String getName() {
114             return name;
115         }
116
117         public int getValue() {
118             return value;
119         }
120     }
121 }
122
123