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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.pnfsimulator.simulator;
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;
30 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
32 class TemplatePatcherTest {
34 private static final String TEMPLATE_JSON = "{\n" +
36 " \"commonEventHeader\": {\n" +
37 " \"domain\": \"measurementsForVfScaling\"\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" +
52 private TemplatePatcher templatePatcher;
53 private Gson gson = new Gson();
54 private JsonObject templateJson;
58 templatePatcher = new TemplatePatcher();
59 templateJson = gson.fromJson(TEMPLATE_JSON, JsonObject.class);
63 void shouldReplaceJsonElementsInTemplate() {
65 String patchJsonString = "{\n"
67 + " \"commonEventHeader\": {\n"
68 + " \"domain\": \"newDomain\"\n"
72 JsonObject patchJson = gson.fromJson(patchJsonString, JsonObject.class);
75 JsonObject requestJson = templatePatcher.mergeTemplateWithPatch(templateJson, patchJson);
78 String newDomain = requestJson
79 .get("event").getAsJsonObject()
80 .get("commonEventHeader").getAsJsonObject()
81 .get("domain").getAsString();
82 assertThat(newDomain).isEqualTo("newDomain");
86 void shouldAddWholeJsonObjectToTemplateWhenItFinished() {
88 String patchJsonString =
91 + " \"commonEventHeader\": {\n"
93 + " \"extraFields\": {\n"
94 + " \"name\": \"G711AudioPort\",\n"
95 + " \"value\": \"1\"\n"
101 JsonObject patchJson = gson.fromJson(patchJsonString, JsonObject.class);
104 JsonObject requestJson = templatePatcher.mergeTemplateWithPatch(templateJson, patchJson);
107 JsonElement newDomain = requestJson
108 .get("event").getAsJsonObject()
109 .get("commonEventHeader").getAsJsonObject()
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");
119 void shouldReplaceJsonObjectWithJsonElementFromPatch() {
121 String patchJsonString = "{ \"event\": \"test\" }";
122 JsonObject patchJson = gson.fromJson(patchJsonString, JsonObject.class);
125 JsonObject requestJson = templatePatcher.mergeTemplateWithPatch(templateJson, patchJson);
128 assertThat(requestJson.get("event").isJsonObject()).isFalse();
129 assertThat(requestJson.get("event").getAsString()).isEqualTo("test");
133 void shouldAddNewKeyIfPatchHasItAndTempleteDoesnt() {
135 String patchJsonString = "{ \"newTestKey\": { \"newTestKeyChild\":\"newTestValue\" }}";
136 JsonObject patchJson = gson.fromJson(patchJsonString, JsonObject.class);
139 JsonObject requestJson = templatePatcher.mergeTemplateWithPatch(templateJson, patchJson);
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");
152 void shouldNotChangeInputTemplateParam() {
154 String patchJsonString = "{ \"newTestKey\": { \"newTestKeyChild\":\"newTestValue\" }}";
155 JsonObject patchJson = gson.fromJson(patchJsonString, JsonObject.class);
158 templatePatcher.mergeTemplateWithPatch(templateJson, patchJson);
161 assertThat(templateJson).isEqualTo(gson.fromJson(TEMPLATE_JSON, JsonObject.class));