Change code in appc dispatcher for new LCMs in R6
[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-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
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 =
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");
52         }
53         catch (FileNotFoundException fnfe) {
54             fail(fnfe.getMessage() + " File Not Found exception encountered");
55         }
56
57     }
58
59     @Test
60     public void testFromJsonException() {
61         JSONUtilVnfTest jOut = null;
62         try {
63             jOut = JSONUtil.fromJson("{\"vnfId\":\"I2\",\"vnfType\"\"T2\"}", JSONUtilVnfTest.class);
64         } catch (UncheckedIOException uioe) {
65             assertEquals(jOut, null);
66         }
67     }
68
69     @Test
70     public void testFromToJsonStr() {
71         JSONUtilVnfTest jRef = new JSONUtilVnfTest("I1", "T1");
72
73         try {
74             assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I1\",\"vnfType\":\"T1\"}");
75             jRef.setVnfId("I2");
76             jRef.setVnfType("T2");
77             assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I2\",\"vnfType\":\"T2\"}");
78             String refJson = JSONUtil.toJson(jRef);
79
80             JSONUtilVnfTest jOut = JSONUtil.fromJson(refJson, JSONUtilVnfTest.class);
81             assertEquals(jRef.getVnfId(), jOut.getVnfId());
82             assertEquals(jRef.getVnfType(), jOut.getVnfType());
83
84         } catch (UncheckedIOException uioe) {
85             fail(uioe.getMessage() + " Unchecked IO exception encountered");
86         }
87     }
88
89     @Test
90     public void testExttractValues() {
91         JSONUtilVnfTest jRef = new JSONUtilVnfTest("I2", "T2");
92
93         try {
94
95             String refJson = JSONUtil.toJson(jRef);
96
97
98             Map<String, String> map = JSONUtil.extractPlainValues(refJson, "vnfId", "vnfType");
99
100             HashMap<String, String> hashMap =
101                     (map instanceof HashMap)
102                         ? (HashMap) map
103                         : new HashMap<String, String>(map);
104             assertEquals(hashMap.get("vnfId"), "I2");
105             assertEquals(hashMap.get("vnfType"), "T2");
106
107
108         } catch (UncheckedIOException uioe) {
109             fail(uioe.getMessage() + " Unchecked IO exception encountered");
110         }
111     }
112
113     @Test(expected=UncheckedIOException.class)
114     public void testCatchSectionOfToJson() throws IOException {
115         JSONUtil.toJson(new Object());
116     }
117 }