Remove remaining Jackson
[clamp.git] / src / test / java / org / onap / clamp / clds / util / JsonUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.clds.util;
26
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;
31
32 import com.google.gson.JsonElement;
33 import com.google.gson.JsonObject;
34
35 import java.io.IOException;
36 import java.util.List;
37
38 import org.junit.Test;
39
40 public class JsonUtilsTest {
41
42     public static class TestClass extends TestObject {
43
44         String test2;
45         TestObject2 object2;
46
47         TestClass(String value1, String value2) {
48             super(value1);
49             test2 = value2;
50         }
51
52         void setObject2(TestObject2 object2) {
53             this.object2 = object2;
54         }
55     }
56
57     private static final JsonObject DEPLOY_PARAMETERS = JsonUtils.GSON.fromJson(
58         "{\n"
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);
75
76
77     @Test
78     public void testGetObjectMapperInstance() {
79         assertNotNull(JsonUtils.GSON);
80     }
81
82     /**
83      * This method test that the security hole in GSON is not enabled in the default
84      * ObjectMapper.
85      */
86     @Test
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);
95     }
96
97     @Test
98     public void shouldReturnJsonValueByName() throws IOException {
99         // given
100         String modelProperties = ResourceFileUtil
101             .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json");
102         JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global");
103
104         // when
105         String locationName = JsonUtils.getStringValueByName(globalElement, "location");
106         String timeoutValue = JsonUtils.getStringValueByName(globalElement, "timeout");
107
108         // then
109         assertThat(locationName).isEqualTo("SNDGCA64");
110         assertThat(timeoutValue).isEqualTo("500");
111     }
112
113     @Test
114     public void shouldReturnJsonObjectByPropertyName() throws IOException {
115         // given
116         String modelProperties = ResourceFileUtil
117             .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json");
118         JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global");
119
120         // when
121         JsonObject deployParameters = JsonUtils.getJsonObjectByName(globalElement, "deployParameters");
122
123         // then
124         assertThat(deployParameters).isEqualToComparingFieldByField(DEPLOY_PARAMETERS);
125     }
126
127     @Test
128     public void shouldReturnJsonValuesByPropertyName() throws IOException {
129         // given
130         String modelProperties = ResourceFileUtil
131             .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json");
132         JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global");
133
134         // when
135         List<String> vfs = JsonUtils.getStringValuesByName(globalElement, "vf");
136
137         // then
138         assertThat(vfs).containsExactly(
139             "6c7aaec2-59eb-41d9-8681-b7f976ab668d",
140             "8sadsad0-a98s-6a7s-fd12-sadji9sa8d12",
141             "8sfd71ad-a90d-asd9-as87-8a7sd81adsaa"
142         );
143     }
144
145     @Test
146     public void shouldReturnJsonValueAsInteger() throws IOException {
147         // given
148         String modelProperties = ResourceFileUtil
149             .getResourceAsString("example/model-properties/custom/modelBpmnPropertiesMultiVF.json");
150         JsonElement globalElement = JsonUtils.GSON.fromJson(modelProperties, JsonObject.class).get("global");
151
152         // when
153         Integer timeoutValue = JsonUtils.getIntValueByName(globalElement, "timeout");
154
155         // then
156         assertThat(timeoutValue).isEqualTo(500);
157     }
158 }