2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END============================================
22 * Modifications copyright (c) 2019 Nokia
23 * ===================================================================
27 package org.onap.clamp.clds.util;
29 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
30 import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.assertNotNull;
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonObject;
37 import java.io.IOException;
38 import java.util.List;
40 import org.junit.Test;
42 public class JsonUtilsTest {
44 public static class TestClass extends TestObject {
49 TestClass(String value1, String value2) {
54 void setObject2(TestObject2 object2) {
55 this.object2 = object2;
59 private static final JsonObject DEPLOY_PARAMETERS = JsonUtils.GSON.fromJson(
61 + " \"aaiEnrichmentHost\": \"aai.onap.svc.cluster.local\",\n"
62 + " \"aaiEnrichmentPort\": \"8443\",\n"
63 + " \"enableAAIEnrichment\": true,\n"
64 + " \"dmaap_host\": \"message-router\",\n"
65 + " \"dmaap_port\": \"3904\",\n"
66 + " \"enableRedisCaching\": false,\n"
67 + " \"redisHosts\": \"dcae-redis:6379\",\n"
68 + " \"tag_version\": \"nexus3.onap.org:10001/onap/org.onap.dcaegen2."
69 + "deployments.tca-cdap-container:1.1.0\",\n"
70 + " \"consul_host\": \"consul-server\",\n"
71 + " \"consul_port\": \"8500\",\n"
72 + " \"cbs_host\": \"config-binding-service\",\n"
73 + " \"cbs_port\": \"10000\",\n"
74 + " \"external_port\": \"32010\",\n"
75 + " \"policy_id\": \"AUTO_GENERATED_POLICY_ID_AT_SUBMIT\"\n"
76 + " }", JsonObject.class);
80 public void testGetObjectMapperInstance() {
81 assertNotNull(JsonUtils.GSON);
85 * This method test that the security hole in GSON is not enabled in the default
89 public void testCreateBeanDeserializer() {
90 TestClass test = new TestClass("value1", "value2");
91 test.setObject2(new TestObject2("test3"));
92 Object testObject = JsonUtils.GSON.fromJson("[\"org.onap.clamp.clds.util.JsonUtilsTest$TestClass\""
93 + ",{\"test\":\"value1\",\"test2\":\"value2\",\"object2\":[\"org.onap.clamp.clds.util.TestObject2\","
94 + "{\"test3\":\"test3\"}]}]", Object.class);
95 assertNotNull(testObject);
96 assertFalse(testObject instanceof TestObject);
100 public void shouldReturnJsonValueByName() throws IOException {
102 String modelProperties = ResourceFileUtil
103 .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json");
104 JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global");
107 String locationName = JsonUtils.getStringValueByName(globalElement, "location");
108 String timeoutValue = JsonUtils.getStringValueByName(globalElement, "timeout");
111 assertThat(locationName).isEqualTo("SNDGCA64");
112 assertThat(timeoutValue).isEqualTo("500");
116 public void shouldReturnJsonObjectByPropertyName() throws IOException {
118 String modelProperties = ResourceFileUtil
119 .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json");
120 JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global");
123 JsonObject deployParameters = JsonUtils.getJsonObjectByName(globalElement, "deployParameters");
126 assertThat(deployParameters).isEqualToComparingFieldByField(DEPLOY_PARAMETERS);
130 public void shouldReturnJsonValuesByPropertyName() throws IOException {
132 String modelProperties = ResourceFileUtil
133 .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json");
134 JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global");
137 List<String> vfs = JsonUtils.getStringValuesByName(globalElement, "vf");
140 assertThat(vfs).containsExactly(
141 "6c7aaec2-59eb-41d9-8681-b7f976ab668d",
142 "8sadsad0-a98s-6a7s-fd12-sadji9sa8d12",
143 "8sfd71ad-a90d-asd9-as87-8a7sd81adsaa"
148 public void shouldReturnJsonValueAsInteger() throws IOException {
150 String modelProperties = ResourceFileUtil
151 .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json");
152 JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global");
155 Integer timeoutValue = JsonUtils.getIntValueByName(globalElement, "timeout");
158 assertThat(timeoutValue).isEqualTo(500);
161 @Test(expected = IllegalArgumentException.class)
162 public void shouldThrowExceptionFileNotExists() throws IOException {
163 ResourceFileUtil.getResourceAsString("example/notExist.json");