added test case to JSONUtilTest.java
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / test / java / org / onap / appc / dg / common / utils / JSONUtilTest.java
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : APPC
4 * ================================================================================
5 * Copyright 2018 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
12 *
13 *     http://www.apache.org/licenses/LICENSE-2.0
14 *
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=========================================================
21 */
22 package org.onap.appc.dg.common.utils;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27 import org.junit.Before;
28 import org.junit.Test;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.io.FileNotFoundException;
34
35 import java.io.UncheckedIOException;
36
37
38 public class JSONUtilTest {
39
40     @Before
41     public void setUp() {
42     }
43
44     @Test
45     public void testFromJsonReader() {
46
47         try {
48             JSONUtilVnfTest jOut = JSONUtil.fromJson(new FileReader("src/test/resources/data/input.json"), JSONUtilVnfTest.class);
49             assertEquals("I1", jOut.getVnfId());
50             assertEquals("T1", jOut.getVnfType());
51         } catch (UncheckedIOException uioe) {
52             fail(uioe.getMessage() + " Unchecked IO exception encountered");
53         }
54         catch (FileNotFoundException fnfe) {
55             fail(fnfe.getMessage() + " File Not Found exception encountered");
56         }
57
58     }
59
60     @Test
61     public void testFromJsonException() {
62         JSONUtilVnfTest jOut = null;
63         try {
64             jOut = JSONUtil.fromJson("{\"vnfId\":\"I2\",\"vnfType\"\"T2\"}", JSONUtilVnfTest.class);
65         } catch (UncheckedIOException uioe) {
66             assertEquals(jOut, null);
67         }
68     }
69
70     @Test
71     public void testFromToJsonStr() {
72         JSONUtilVnfTest jRef = new JSONUtilVnfTest("I1", "T1");
73
74         try {
75             assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I1\",\"vnfType\":\"T1\"}");
76             jRef.setVnfId("I2");
77             jRef.setVnfType("T2");
78             assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I2\",\"vnfType\":\"T2\"}");
79             String refJson = JSONUtil.toJson(jRef);
80
81             JSONUtilVnfTest jOut = JSONUtil.fromJson(refJson, JSONUtilVnfTest.class);
82             assertEquals(jRef.getVnfId(), jOut.getVnfId());
83             assertEquals(jRef.getVnfType(), jOut.getVnfType());
84
85         } catch (UncheckedIOException uioe) {
86             fail(uioe.getMessage() + " Unchecked IO exception encountered");
87         }
88     }
89
90     @Test
91     public void testExttractValues() {
92         JSONUtilVnfTest jRef = new JSONUtilVnfTest("I2", "T2");
93
94         try {
95
96             String refJson = JSONUtil.toJson(jRef);
97
98
99             Map<String, String> map = JSONUtil.extractPlainValues(refJson, "vnfId", "vnfType");
100
101             HashMap<String, String> hashMap =
102                  (map instanceof HashMap)
103                   ? (HashMap) map
104                   : new HashMap<String, String>(map);
105             assertEquals(hashMap.get("vnfId"), "I2");
106             assertEquals(hashMap.get("vnfType"), "T2");
107
108
109         } catch (UncheckedIOException uioe) {
110             fail(uioe.getMessage() + " Unchecked IO exception encountered");
111         }
112     }
113     
114     @Test(expected=UncheckedIOException.class)
115     public void testCatchSectionOfToJson() throws IOException
116     {
117        JSONUtil.toJson(new Object());
118     }
119 }