Fix intermittent time based test failures
[appc.git] / appc-core / appc-common-bundle / 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  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.util;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.util.HashMap;
33 import java.util.Map;
34 import org.junit.Test;
35 import com.fasterxml.jackson.annotation.JsonProperty;
36 import com.fasterxml.jackson.core.JsonParseException;
37
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