2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * Modifications copyright (c) 2019 Nokia
21 * ===================================================================
25 package org.onap.clamp.clds.util;
27 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
28 import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonObject;
35 import java.io.IOException;
36 import java.util.List;
38 import org.junit.Test;
40 public class JsonUtilsTest {
42 public static class TestClass extends TestObject {
47 TestClass(String value1, String value2) {
52 void setObject2(TestObject2 object2) {
53 this.object2 = object2;
57 private static final JsonObject DEPLOY_PARAMETERS = JsonUtils.GSON.fromJson(
59 + " \"aaiEnrichmentHost\": \"aai.onap.svc.cluster.local\",\n"
60 + " \"aaiEnrichmentPort\": \"8443\",\n"
61 + " \"enableAAIEnrichment\": true,\n"
62 + " \"dmaap_host\": \"message-router\",\n"
63 + " \"dmaap_port\": \"3904\",\n"
64 + " \"enableRedisCaching\": false,\n"
65 + " \"redisHosts\": \"dcae-redis:6379\",\n"
66 + " \"tag_version\": \"nexus3.onap.org:10001/onap/org.onap.dcaegen2."
67 + "deployments.tca-cdap-container:1.1.0\",\n"
68 + " \"consul_host\": \"consul-server\",\n"
69 + " \"consul_port\": \"8500\",\n"
70 + " \"cbs_host\": \"config-binding-service\",\n"
71 + " \"cbs_port\": \"10000\",\n"
72 + " \"external_port\": \"32010\",\n"
73 + " \"policy_id\": \"AUTO_GENERATED_POLICY_ID_AT_SUBMIT\"\n"
74 + " }", JsonObject.class);
78 public void testGetObjectMapperInstance() {
79 assertNotNull(JsonUtils.GSON);
83 * This method test that the security hole in GSON is not enabled in the default
87 public void testCreateBeanDeserializer() {
88 TestClass test = new TestClass("value1", "value2");
89 test.setObject2(new TestObject2("test3"));
90 Object testObject = JsonUtils.GSON.fromJson("[\"org.onap.clamp.clds.util.JsonUtilsTest$TestClass\""
91 + ",{\"test\":\"value1\",\"test2\":\"value2\",\"object2\":[\"org.onap.clamp.clds.util.TestObject2\","
92 + "{\"test3\":\"test3\"}]}]", Object.class);
93 assertNotNull(testObject);
94 assertFalse(testObject instanceof TestObject);
98 public void shouldReturnJsonValueByName() throws IOException {
100 String modelProperties = ResourceFileUtil
101 .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json");
102 JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global");
105 String locationName = JsonUtils.getStringValueByName(globalElement, "location");
106 String timeoutValue = JsonUtils.getStringValueByName(globalElement, "timeout");
109 assertThat(locationName).isEqualTo("SNDGCA64");
110 assertThat(timeoutValue).isEqualTo("500");
114 public void shouldReturnJsonObjectByPropertyName() throws IOException {
116 String modelProperties = ResourceFileUtil
117 .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json");
118 JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global");
121 JsonObject deployParameters = JsonUtils.getJsonObjectByName(globalElement, "deployParameters");
124 assertThat(deployParameters).isEqualToComparingFieldByField(DEPLOY_PARAMETERS);
128 public void shouldReturnJsonValuesByPropertyName() throws IOException {
130 String modelProperties = ResourceFileUtil
131 .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json");
132 JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global");
135 List<String> vfs = JsonUtils.getStringValuesByName(globalElement, "vf");
138 assertThat(vfs).containsExactly(
139 "6c7aaec2-59eb-41d9-8681-b7f976ab668d",
140 "8sadsad0-a98s-6a7s-fd12-sadji9sa8d12",
141 "8sfd71ad-a90d-asd9-as87-8a7sd81adsaa"
146 public void shouldReturnJsonValueAsInteger() throws IOException {
148 String modelProperties = ResourceFileUtil
149 .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json");
150 JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global");
153 Integer timeoutValue = JsonUtils.getIntValueByName(globalElement, "timeout");
156 assertThat(timeoutValue).isEqualTo(500);