0dff2e24ddfe07eae4245de63e9ed9954eed084a
[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 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20 package org.onap.appc.dg.common.utils;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.fail;
24
25 import org.junit.Before;
26 import org.junit.Test;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.io.FileReader;
30 import java.io.FileNotFoundException;
31
32 import java.io.UncheckedIOException;
33
34
35 public class JSONUtilTest {
36
37     @Before
38     public void setUp() {
39     }
40
41     @Test
42     public void testFromJsonReader() {
43
44         try {
45             JSONUtilVnfTest jOut = JSONUtil.fromJson(new FileReader("src/test/resources/data/input.json"), JSONUtilVnfTest.class);
46             assertEquals("I1", jOut.getVnfId());
47             assertEquals("T1", jOut.getVnfType());
48         } catch (UncheckedIOException uioe) {
49             fail(uioe.getMessage() + " Unchecked IO exception encountered");
50         }
51         catch (FileNotFoundException fnfe) {
52             fail(fnfe.getMessage() + " File Not Found exception encountered");
53         }
54
55     }
56
57     @Test
58     public void testFromJsonException() {
59         JSONUtilVnfTest jOut = null;
60         try {
61             jOut = JSONUtil.fromJson("{\"vnfId\":\"I2\",\"vnfType\"\"T2\"}", JSONUtilVnfTest.class);
62         } catch (UncheckedIOException uioe) {
63             assertEquals(jOut, null);
64         }
65     }
66
67     @Test
68     public void testFromToJsonStr() {
69         JSONUtilVnfTest jRef = new JSONUtilVnfTest("I1", "T1");
70
71         try {
72             assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I1\",\"vnfType\":\"T1\"}");
73             jRef.setVnfId("I2");
74             jRef.setVnfType("T2");
75             assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I2\",\"vnfType\":\"T2\"}");
76             String refJson = JSONUtil.toJson(jRef);
77
78             JSONUtilVnfTest jOut = JSONUtil.fromJson(refJson, JSONUtilVnfTest.class);
79             assertEquals(jRef.getVnfId(), jOut.getVnfId());
80             assertEquals(jRef.getVnfType(), jOut.getVnfType());
81
82         } catch (UncheckedIOException uioe) {
83             fail(uioe.getMessage() + " Unchecked IO exception encountered");
84         }
85     }
86
87     @Test
88     public void testExttractValues() {
89         JSONUtilVnfTest jRef = new JSONUtilVnfTest("I2", "T2");
90
91         try {
92
93             String refJson = JSONUtil.toJson(jRef);
94
95
96             Map<String, String> map = JSONUtil.extractPlainValues(refJson, "vnfId", "vnfType");
97
98             HashMap<String, String> hashMap =
99                  (map instanceof HashMap)
100                   ? (HashMap) map
101                   : new HashMap<String, String>(map);
102             assertEquals(hashMap.get("vnfId"), "I2");
103             assertEquals(hashMap.get("vnfType"), "T2");
104
105
106         } catch (UncheckedIOException uioe) {
107             fail(uioe.getMessage() + " Unchecked IO exception encountered");
108         }
109     }
110
111 }