2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2018-2019 AT&T
6 *=================================================================================
7 * Modifications Copyright 2018 IBM.
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.
20 * ============LICENSE_END=========================================================
22 package org.onap.appc.dg.common.utils;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26 import java.io.FileNotFoundException;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.UncheckedIOException;
30 import java.util.HashMap;
32 import org.junit.Before;
33 import org.junit.Test;
36 public class JSONUtilTest {
43 public void testFromJsonReader() {
46 JSONUtilVnfTest jOut =
47 JSONUtil.fromJson(new FileReader("src/test/resources/data/input.json"), JSONUtilVnfTest.class);
48 assertEquals("I1", jOut.getVnfId());
49 assertEquals("T1", jOut.getVnfType());
50 } catch (UncheckedIOException uioe) {
51 fail(uioe.getMessage() + " Unchecked IO exception encountered");
53 catch (FileNotFoundException fnfe) {
54 fail(fnfe.getMessage() + " File Not Found exception encountered");
60 public void testFromJsonException() {
61 JSONUtilVnfTest jOut = null;
63 jOut = JSONUtil.fromJson("{\"vnfId\":\"I2\",\"vnfType\"\"T2\"}", JSONUtilVnfTest.class);
64 } catch (UncheckedIOException uioe) {
65 assertEquals(jOut, null);
70 public void testFromToJsonStr() {
71 JSONUtilVnfTest jRef = new JSONUtilVnfTest("I1", "T1");
74 assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I1\",\"vnfType\":\"T1\"}");
76 jRef.setVnfType("T2");
77 assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I2\",\"vnfType\":\"T2\"}");
78 String refJson = JSONUtil.toJson(jRef);
80 JSONUtilVnfTest jOut = JSONUtil.fromJson(refJson, JSONUtilVnfTest.class);
81 assertEquals(jRef.getVnfId(), jOut.getVnfId());
82 assertEquals(jRef.getVnfType(), jOut.getVnfType());
84 } catch (UncheckedIOException uioe) {
85 fail(uioe.getMessage() + " Unchecked IO exception encountered");
90 public void testExttractValues() {
91 JSONUtilVnfTest jRef = new JSONUtilVnfTest("I2", "T2");
95 String refJson = JSONUtil.toJson(jRef);
98 Map<String, String> map = JSONUtil.extractPlainValues(refJson, "vnfId", "vnfType");
100 HashMap<String, String> hashMap =
101 (map instanceof HashMap)
103 : new HashMap<String, String>(map);
104 assertEquals(hashMap.get("vnfId"), "I2");
105 assertEquals(hashMap.get("vnfType"), "T2");
108 } catch (UncheckedIOException uioe) {
109 fail(uioe.getMessage() + " Unchecked IO exception encountered");
113 @Test(expected=UncheckedIOException.class)
114 public void testCatchSectionOfToJson() throws IOException {
115 JSONUtil.toJson(new Object());