2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.util;
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;
34 import org.junit.Test;
35 import com.fasterxml.jackson.annotation.JsonProperty;
36 import com.fasterxml.jackson.core.JsonParseException;
40 public class JsonUtilTest {
42 @Test(expected = NullPointerException.class)
43 public void should_throw_exception_when_json_string_is_null() throws IOException {
45 JsonUtil.convertJsonStringToFlatMap(null);
48 @Test(expected = JsonParseException.class)
49 public void should_throw_exception_when_invalid_json_string() throws IOException {
51 JsonUtil.convertJsonStringToFlatMap("{key: value}");
55 public void should_convert_json_string_to_flat_map() throws IOException {
57 String jsonString = "{\"A\":\"A-value\",\"B\":{\"C\":\"B.C-value\",\"D\":\"B.D-value\"}}";
58 Map<String, String> flatMap = JsonUtil.convertJsonStringToFlatMap(jsonString);
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");
65 assertEquals(expectedMap, flatMap);
66 assertNotNull(flatMap);
70 public void should_convert_json_string_to_flat_map_with_nested_json() throws IOException {
72 String jsonString = "{\"A\":\"A-value\",\"B\":\"{\\\"C\\\":\\\"C-value\\\",\\\"D\\\":\\\"D-value\\\"}\"}";
73 Map<String, String> flatMap = JsonUtil.convertJsonStringToFlatMap(jsonString);
75 Map<String, String> expectedMap = new HashMap<>();
76 expectedMap.put("A", "A-value");
77 expectedMap.put("B", "{\"C\":\"C-value\",\"D\":\"D-value\"}");
79 assertEquals(expectedMap, flatMap);
80 assertNotNull(flatMap);
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);
89 @Test(expected = JsonParseException.class)
90 public void should_throw_exception_when_invalid_json_file() throws IOException {
91 JsonUtil.readInputJson("/invalid.json", DummyClass.class);
95 public void should_parse_valid_json_file () throws IOException {
96 DummyClass dummyClass = JsonUtil.readInputJson("/valid.json", DummyClass.class);
98 assertEquals("dummy name", dummyClass.getName());
99 assertEquals(99, dummyClass.getValue());
102 private static class DummyClass {
107 public DummyClass(@JsonProperty("name") String name, @JsonProperty("value") int someValue) {
109 this.value = someValue;
112 public String getName() {
116 public int getValue() {