More license header updates to appc-common files
[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-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.util;
25
26 import com.fasterxml.jackson.annotation.JsonProperty;
27 import com.fasterxml.jackson.core.JsonParseException;
28 import java.io.FileNotFoundException;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32
33 import java.io.IOException;
34 import java.util.HashMap;
35 import java.util.Map;
36
37 import static org.junit.Assert.*;
38
39
40 public class JsonUtilTest {
41
42     @Test(expected = NullPointerException.class)
43     public void should_throw_exception_when_json_string_is_null() throws IOException {
44
45         JsonUtil.convertJsonStringToFlatMap(null);
46     }
47
48     @Test(expected = JsonParseException.class)
49     public void should_throw_exception_when_invalid_json_string() throws IOException {
50
51         JsonUtil.convertJsonStringToFlatMap("{key: value}");
52     }
53
54     @Test
55     public void should_convert_json_string_to_flat_map() throws IOException {
56
57         String jsonString = "{\"A\":\"A-value\",\"B\":{\"C\":\"B.C-value\",\"D\":\"B.D-value\"}}";
58         Map<String, String> flatMap = JsonUtil.convertJsonStringToFlatMap(jsonString);
59
60         Map<String, String> expectedMap = new HashMap<>();
61         expectedMap.put("A", "A-value");
62         expectedMap.put("B.C", "B.C-value");
63         expectedMap.put("B.D", "B.D-value");
64
65         assertEquals(expectedMap, flatMap);
66         assertNotNull(flatMap);
67     }
68
69     @Test
70     public void should_convert_json_string_to_flat_map_with_nested_json() throws IOException {
71
72         String jsonString = "{\"A\":\"A-value\",\"B\":\"{\\\"C\\\":\\\"C-value\\\",\\\"D\\\":\\\"D-value\\\"}\"}";
73         Map<String, String> flatMap = JsonUtil.convertJsonStringToFlatMap(jsonString);
74
75         Map<String, String> expectedMap = new HashMap<>();
76         expectedMap.put("A", "A-value");
77         expectedMap.put("B", "{\"C\":\"C-value\",\"D\":\"D-value\"}");
78
79         assertEquals(expectedMap, flatMap);
80         assertNotNull(flatMap);
81     }
82
83     @Test(expected = FileNotFoundException.class)
84     public void should_throw_exception_when_not_found_json_file() throws IOException {
85         JsonUtil.readInputJson("not-existing.json", DummyClass.class);
86     }
87
88
89     @Test(expected = JsonParseException.class)
90     public void should_throw_exception_when_invalid_json_file() throws IOException {
91         JsonUtil.readInputJson("/invalid.json", DummyClass.class);
92     }
93
94     @Test
95     public void should_parse_valid_json_file () throws IOException {
96         DummyClass dummyClass = JsonUtil.readInputJson("/valid.json", DummyClass.class);
97
98         assertEquals("dummy name", dummyClass.getName());
99         assertEquals(99, dummyClass.getValue());
100     }
101
102     private static class DummyClass {
103
104         private String name;
105         private int value;
106
107         public DummyClass(@JsonProperty("name") String name, @JsonProperty("value") int someValue) {
108             this.name = name;
109             this.value = someValue;
110         }
111
112         public String getName() {
113             return name;
114         }
115
116         public int getValue() {
117             return value;
118         }
119     }
120 }
121
122