52e0d6ae60c86fa167ead32b9f1c4b6e3e03c429
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.pnfsimulator.simulator;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import org.assertj.core.api.AssertionsForInterfaceTypes;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29
30 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
31
32 class TemplatePatcherTest {
33
34     private static final String TEMPLATE_JSON = "{\n" +
35             "  \"event\": {\n" +
36             "    \"commonEventHeader\": {\n" +
37             "      \"domain\": \"measurementsForVfScaling\"\n" +
38             "    },\n" +
39             "    \"measurementsForVfScalingFields\": {\n" +
40             "      \"measurementsForVfSclaingFieldsVersion\": 2.0,\n" +
41             "      \"additionalMeasurements\": {\n" +
42             "        \"name\": \"licenseUsage\",\n" +
43             "        \"extraFields\": {\n" +
44             "          \"name\": \"G711AudioPort\",\n" +
45             "          \"value\": \"1\"\n" +
46             "        }\n" +
47             "      }\n" +
48             "    }\n" +
49             "  }\n" +
50             "}";
51
52     private TemplatePatcher templatePatcher;
53     private Gson gson = new Gson();
54     private JsonObject templateJson;
55
56     @BeforeEach
57     void setUp() {
58         templatePatcher = new TemplatePatcher();
59         templateJson = gson.fromJson(TEMPLATE_JSON, JsonObject.class);
60     }
61
62     @Test
63     void shouldReplaceJsonElementsInTemplate() {
64         //given
65         String patchJsonString = "{\n"
66                 + "  \"event\": {\n"
67                 + "    \"commonEventHeader\": {\n"
68                 + "      \"domain\": \"newDomain\"\n"
69                 + "    }\n"
70                 + "  }\n"
71                 + "}";
72         JsonObject patchJson = gson.fromJson(patchJsonString, JsonObject.class);
73
74         //when
75         JsonObject requestJson = templatePatcher.mergeTemplateWithPatch(templateJson, patchJson);
76
77         //then
78         String newDomain = requestJson
79                 .get("event").getAsJsonObject()
80                 .get("commonEventHeader").getAsJsonObject()
81                 .get("domain").getAsString();
82         assertThat(newDomain).isEqualTo("newDomain");
83     }
84
85     @Test
86     void shouldAddWholeJsonObjectToTemplateWhenItFinished() {
87         //given
88         String patchJsonString =
89                 "{\n"
90                         + " \"event\": {\n"
91                         + "  \"commonEventHeader\": {\n"
92                         + "   \"domain\": {\n"
93                         + "    \"extraFields\": {\n"
94                         + "     \"name\": \"G711AudioPort\",\n"
95                         + "     \"value\": \"1\"\n"
96                         + "    }\n"
97                         + "   }\n"
98                         + "  }\n"
99                         + " }\n"
100                         + "}";
101         JsonObject patchJson = gson.fromJson(patchJsonString, JsonObject.class);
102
103         //when
104         JsonObject requestJson = templatePatcher.mergeTemplateWithPatch(templateJson, patchJson);
105
106         //then
107         JsonElement newDomain = requestJson
108                 .get("event").getAsJsonObject()
109                 .get("commonEventHeader").getAsJsonObject()
110                 .get("domain");
111         assertThat(newDomain.isJsonObject()).isTrue();
112         JsonObject newDomainJO = newDomain.getAsJsonObject();
113         AssertionsForInterfaceTypes.assertThat(newDomainJO.keySet()).containsExactly("extraFields");
114         JsonObject newDomainExtraFields = newDomainJO.get("extraFields").getAsJsonObject();
115         AssertionsForInterfaceTypes.assertThat(newDomainExtraFields.keySet()).containsExactly("name", "value");
116     }
117
118     @Test
119     void shouldReplaceJsonObjectWithJsonElementFromPatch() {
120         //given
121         String patchJsonString = "{ \"event\": \"test\" }";
122         JsonObject patchJson = gson.fromJson(patchJsonString, JsonObject.class);
123
124         //when
125         JsonObject requestJson = templatePatcher.mergeTemplateWithPatch(templateJson, patchJson);
126
127         //then
128         assertThat(requestJson.get("event").isJsonObject()).isFalse();
129         assertThat(requestJson.get("event").getAsString()).isEqualTo("test");
130     }
131
132     @Test
133     void shouldAddNewKeyIfPatchHasItAndTempleteDoesnt() {
134         //given
135         String patchJsonString = "{  \"newTestKey\": { \"newTestKeyChild\":\"newTestValue\"  }}";
136         JsonObject patchJson = gson.fromJson(patchJsonString, JsonObject.class);
137
138         //when
139         JsonObject requestJson = templatePatcher.mergeTemplateWithPatch(templateJson, patchJson);
140
141         //then
142         assertThat(requestJson.get("event").isJsonObject()).isTrue();
143         assertThat(requestJson.get("newTestKey").isJsonObject()).isTrue();
144         JsonObject newTestKey = requestJson.get("newTestKey").getAsJsonObject();
145         AssertionsForInterfaceTypes.assertThat(newTestKey.keySet()).containsExactly("newTestKeyChild");
146         assertThat(newTestKey.get("newTestKeyChild").getAsString()).isEqualTo("newTestValue");
147
148     }
149
150
151     @Test
152     void shouldNotChangeInputTemplateParam() {
153         //given
154         String patchJsonString = "{  \"newTestKey\": { \"newTestKeyChild\":\"newTestValue\"  }}";
155         JsonObject patchJson = gson.fromJson(patchJsonString, JsonObject.class);
156
157         //when
158         templatePatcher.mergeTemplateWithPatch(templateJson, patchJson);
159
160         //then
161         assertThat(templateJson).isEqualTo(gson.fromJson(TEMPLATE_JSON, JsonObject.class));
162
163     }
164 }