50429f680807b445cfe535c40003c20dae7696ab
[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 import java.io.FileNotFoundException;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.UncheckedIOException;
30 import java.util.HashMap;
31 import java.util.Map;
32 import org.junit.Before;
33 import org.junit.Test;
34
35
36 public class JSONUtilTest {
37
38     @Before
39     public void setUp() {
40     }
41
42     @Test
43     public void testFromJsonReader() {
44
45         try {
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");
51         }
52         catch (FileNotFoundException fnfe) {
53             fail(fnfe.getMessage() + " File Not Found exception encountered");
54         }
55
56     }
57
58     @Test
59     public void testFromJsonException() {
60         JSONUtilVnfTest jOut = null;
61         try {
62             jOut = JSONUtil.fromJson("{\"vnfId\":\"I2\",\"vnfType\"\"T2\"}", JSONUtilVnfTest.class);
63         } catch (UncheckedIOException uioe) {
64             assertEquals(jOut, null);
65         }
66     }
67
68     @Test
69     public void testFromToJsonStr() {
70         JSONUtilVnfTest jRef = new JSONUtilVnfTest("I1", "T1");
71
72         try {
73             assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I1\",\"vnfType\":\"T1\"}");
74             jRef.setVnfId("I2");
75             jRef.setVnfType("T2");
76             assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I2\",\"vnfType\":\"T2\"}");
77             String refJson = JSONUtil.toJson(jRef);
78
79             JSONUtilVnfTest jOut = JSONUtil.fromJson(refJson, JSONUtilVnfTest.class);
80             assertEquals(jRef.getVnfId(), jOut.getVnfId());
81             assertEquals(jRef.getVnfType(), jOut.getVnfType());
82
83         } catch (UncheckedIOException uioe) {
84             fail(uioe.getMessage() + " Unchecked IO exception encountered");
85         }
86     }
87
88     @Test
89     public void testExttractValues() {
90         JSONUtilVnfTest jRef = new JSONUtilVnfTest("I2", "T2");
91
92         try {
93
94             String refJson = JSONUtil.toJson(jRef);
95
96
97             Map<String, String> map = JSONUtil.extractPlainValues(refJson, "vnfId", "vnfType");
98
99             HashMap<String, String> hashMap =
100                  (map instanceof HashMap)
101                   ? (HashMap) map
102                   : new HashMap<String, String>(map);
103             assertEquals(hashMap.get("vnfId"), "I2");
104             assertEquals(hashMap.get("vnfType"), "T2");
105
106
107         } catch (UncheckedIOException uioe) {
108             fail(uioe.getMessage() + " Unchecked IO exception encountered");
109         }
110     }
111     
112     @Test(expected=UncheckedIOException.class)
113     public void testCatchSectionOfToJson() throws IOException
114     {
115        JSONUtil.toJson(new Object());
116     }
117 }