2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.util;
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;
34 import java.io.IOException;
35 import java.util.HashMap;
38 import static org.junit.Assert.*;
41 public class JsonUtilTest {
43 @Test(expected = NullPointerException.class)
44 public void should_throw_exception_when_json_string_is_null() throws IOException {
46 JsonUtil.convertJsonStringToFlatMap(null);
49 @Test(expected = JsonParseException.class)
50 public void should_throw_exception_when_invalid_json_string() throws IOException {
52 JsonUtil.convertJsonStringToFlatMap("{key: value}");
56 public void should_convert_json_string_to_flat_map() throws IOException {
58 String jsonString = "{\"A\":\"A-value\",\"B\":{\"C\":\"B.C-value\",\"D\":\"B.D-value\"}}";
59 Map<String, String> flatMap = JsonUtil.convertJsonStringToFlatMap(jsonString);
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");
66 assertEquals(expectedMap, flatMap);
67 assertNotNull(flatMap);
71 public void should_convert_json_string_to_flat_map_with_nested_json() throws IOException {
73 String jsonString = "{\"A\":\"A-value\",\"B\":\"{\\\"C\\\":\\\"C-value\\\",\\\"D\\\":\\\"D-value\\\"}\"}";
74 Map<String, String> flatMap = JsonUtil.convertJsonStringToFlatMap(jsonString);
76 Map<String, String> expectedMap = new HashMap<>();
77 expectedMap.put("A", "A-value");
78 expectedMap.put("B", "{\"C\":\"C-value\",\"D\":\"D-value\"}");
80 assertEquals(expectedMap, flatMap);
81 assertNotNull(flatMap);
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);
90 @Test(expected = JsonParseException.class)
91 public void should_throw_exception_when_invalid_json_file() throws IOException {
92 JsonUtil.readInputJson("/invalid.json", DummyClass.class);
96 public void should_parse_valid_json_file () throws IOException {
97 DummyClass dummyClass = JsonUtil.readInputJson("/valid.json", DummyClass.class);
99 assertEquals("dummy name", dummyClass.getName());
100 assertEquals(99, dummyClass.getValue());
103 private static class DummyClass {
108 public DummyClass(@JsonProperty("name") String name, @JsonProperty("value") int someValue) {
110 this.value = someValue;
113 public String getName() {
117 public int getValue() {