2 * ============LICENSE_START=======================================================
4 * ================================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.appc.dg.common.utils;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
27 import org.junit.Before;
28 import org.junit.Test;
29 import java.util.HashMap;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.io.FileNotFoundException;
35 import java.io.UncheckedIOException;
38 public class JSONUtilTest {
45 public void testFromJsonReader() {
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");
54 catch (FileNotFoundException fnfe) {
55 fail(fnfe.getMessage() + " File Not Found exception encountered");
61 public void testFromJsonException() {
62 JSONUtilVnfTest jOut = null;
64 jOut = JSONUtil.fromJson("{\"vnfId\":\"I2\",\"vnfType\"\"T2\"}", JSONUtilVnfTest.class);
65 } catch (UncheckedIOException uioe) {
66 assertEquals(jOut, null);
71 public void testFromToJsonStr() {
72 JSONUtilVnfTest jRef = new JSONUtilVnfTest("I1", "T1");
75 assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I1\",\"vnfType\":\"T1\"}");
77 jRef.setVnfType("T2");
78 assertEquals(JSONUtil.toJson(jRef), "{\"vnfId\":\"I2\",\"vnfType\":\"T2\"}");
79 String refJson = JSONUtil.toJson(jRef);
81 JSONUtilVnfTest jOut = JSONUtil.fromJson(refJson, JSONUtilVnfTest.class);
82 assertEquals(jRef.getVnfId(), jOut.getVnfId());
83 assertEquals(jRef.getVnfType(), jOut.getVnfType());
85 } catch (UncheckedIOException uioe) {
86 fail(uioe.getMessage() + " Unchecked IO exception encountered");
91 public void testExttractValues() {
92 JSONUtilVnfTest jRef = new JSONUtilVnfTest("I2", "T2");
96 String refJson = JSONUtil.toJson(jRef);
99 Map<String, String> map = JSONUtil.extractPlainValues(refJson, "vnfId", "vnfType");
101 HashMap<String, String> hashMap =
102 (map instanceof HashMap)
104 : new HashMap<String, String>(map);
105 assertEquals(hashMap.get("vnfId"), "I2");
106 assertEquals(hashMap.get("vnfType"), "T2");
109 } catch (UncheckedIOException uioe) {
110 fail(uioe.getMessage() + " Unchecked IO exception encountered");
114 @Test(expected=UncheckedIOException.class)
115 public void testCatchSectionOfToJson() throws IOException
117 JSONUtil.toJson(new Object());