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