2 * ============LICENSE_START=======================================================
4 * ================================================================================
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 = JSONUtil.fromJson(new FileReader("src/test/resources/data/input.json"), JSONUtilVnfTest.class);
47 assertEquals("I1", jOut.getVnfId());
48 assertEquals("T1", jOut.getVnfType());
49 } catch (UncheckedIOException uioe) {
50 fail(uioe.getMessage() + " Unchecked IO exception encountered");
52 catch (FileNotFoundException fnfe) {
53 fail(fnfe.getMessage() + " File Not Found exception encountered");
59 public void testFromJsonException() {
60 JSONUtilVnfTest jOut = null;
62 jOut = JSONUtil.fromJson("{\"vnfId\":\"I2\",\"vnfType\"\"T2\"}", JSONUtilVnfTest.class);
63 } catch (UncheckedIOException uioe) {
64 assertEquals(jOut, null);
69 public void testFromToJsonStr() {
70 JSONUtilVnfTest jRef = new JSONUtilVnfTest("I1", "T1");
73 assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I1\",\"vnfType\":\"T1\"}");
75 jRef.setVnfType("T2");
76 assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I2\",\"vnfType\":\"T2\"}");
77 String refJson = JSONUtil.toJson(jRef);
79 JSONUtilVnfTest jOut = JSONUtil.fromJson(refJson, JSONUtilVnfTest.class);
80 assertEquals(jRef.getVnfId(), jOut.getVnfId());
81 assertEquals(jRef.getVnfType(), jOut.getVnfType());
83 } catch (UncheckedIOException uioe) {
84 fail(uioe.getMessage() + " Unchecked IO exception encountered");
89 public void testExttractValues() {
90 JSONUtilVnfTest jRef = new JSONUtilVnfTest("I2", "T2");
94 String refJson = JSONUtil.toJson(jRef);
97 Map<String, String> map = JSONUtil.extractPlainValues(refJson, "vnfId", "vnfType");
99 HashMap<String, String> hashMap =
100 (map instanceof HashMap)
102 : new HashMap<String, String>(map);
103 assertEquals(hashMap.get("vnfId"), "I2");
104 assertEquals(hashMap.get("vnfType"), "T2");
107 } catch (UncheckedIOException uioe) {
108 fail(uioe.getMessage() + " Unchecked IO exception encountered");
112 @Test(expected=UncheckedIOException.class)
113 public void testCatchSectionOfToJson() throws IOException
115 JSONUtil.toJson(new Object());