2 * ============LICENSE_START=======================================================
4 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.appc.dg.common.utils;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.fail;
25 import org.junit.Before;
26 import org.junit.Test;
27 import java.util.HashMap;
29 import java.io.FileReader;
30 import java.io.FileNotFoundException;
32 import java.io.UncheckedIOException;
35 public class JSONUtilTest {
42 public void testFromJsonReader() {
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");
51 catch (FileNotFoundException fnfe) {
52 fail(fnfe.getMessage() + " File Not Found exception encountered");
58 public void testFromJsonException() {
59 JSONUtilVnfTest jOut = null;
61 jOut = JSONUtil.fromJson("{\"vnfId\":\"I2\",\"vnfType\"\"T2\"}", JSONUtilVnfTest.class);
62 } catch (UncheckedIOException uioe) {
63 assertEquals(jOut, null);
68 public void testFromToJsonStr() {
69 JSONUtilVnfTest jRef = new JSONUtilVnfTest("I1", "T1");
72 assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I1\",\"vnfType\":\"T1\"}");
74 jRef.setVnfType("T2");
75 assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I2\",\"vnfType\":\"T2\"}");
76 String refJson = JSONUtil.toJson(jRef);
78 JSONUtilVnfTest jOut = JSONUtil.fromJson(refJson, JSONUtilVnfTest.class);
79 assertEquals(jRef.getVnfId(), jOut.getVnfId());
80 assertEquals(jRef.getVnfType(), jOut.getVnfType());
82 } catch (UncheckedIOException uioe) {
83 fail(uioe.getMessage() + " Unchecked IO exception encountered");
88 public void testExttractValues() {
89 JSONUtilVnfTest jRef = new JSONUtilVnfTest("I2", "T2");
93 String refJson = JSONUtil.toJson(jRef);
96 Map<String, String> map = JSONUtil.extractPlainValues(refJson, "vnfId", "vnfType");
98 HashMap<String, String> hashMap =
99 (map instanceof HashMap)
101 : new HashMap<String, String>(map);
102 assertEquals(hashMap.get("vnfId"), "I2");
103 assertEquals(hashMap.get("vnfType"), "T2");
106 } catch (UncheckedIOException uioe) {
107 fail(uioe.getMessage() + " Unchecked IO exception encountered");