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 static org.assertj.core.api.AssertionsForClassTypes.assertThat;
24 import static org.onap.pnfsimulator.simulator.KeywordsValueProvider.DEFAULT_STRING_LENGTH;
26 import com.google.gson.Gson;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29 import java.util.Arrays;
30 import java.util.LinkedList;
31 import java.util.Queue;
32 import org.junit.jupiter.api.Test;
34 class KeywordsHandlerTest {
36 private static final String TEMPLATE_JSON = "{\n" +
38 " \"commonEventHeader\": {\n" +
39 " \"domain\": \"#RandomString\"\n" +
41 " \"measurementsForVfScalingFields\": {\n" +
42 " \"measurementsForVfSclaingFieldsVersion\": 2.0,\n" +
43 " \"additionalMeasurements\": {\n" +
44 " \"name\": \"licenseUsage\",\n" +
45 " \"extraFields\": {\n" +
46 " \"name\": \"#RandomString(4)\",\n" +
47 " \"value\": \"1\"\n" +
54 private static final String TEMPLATE_JSON_WITH_MANY_KEYWORDS_INSIDE_SINGLE_VALUE = "{\n" +
56 " \"commonEventHeader\": {\n" +
57 " \"domain1\": \"#RandomString(1) #RandomString(2) #RandomString(3)\",\n" +
58 " \"domain2\": \"1 #RandomString(1) 2\"\n" +
60 " \"measurementsForVfScalingFields\": {\n" +
61 " \"measurementsForVfSclaingFieldsVersion\": 2.0,\n" +
62 " \"additionalMeasurements\": {\n" +
63 " \"name\": \"licenseUsage\",\n" +
64 " \"extraFields\": {\n" +
65 " \"value\": \"1\"\n" +
72 private static final String TEMPLATE_JSON_WITH_ARRAY = "{\n"
74 + " \"commonEventHeader\": {\n"
75 + " \"domain\": \"#RandomString(1)\",\n"
76 + " \"version\": 2.0\n"
78 + " \"measurementsForVfScalingFields\": {\n"
79 + " \"additionalMeasurements\": [\n"
81 + " \"name\": \"licenseUsage\",\n"
82 + " \"arrayOfFields\": [\n"
84 + " \"name\": \"G711AudioPort\",\n"
85 + " \"value\": \"1\"\n"
88 + " \"name\": [\"1\",\"2\"],\n"
89 + " \"value\": \"#RandomString(2)\"\n"
92 + " \"name\": \"G722AudioPort\",\n"
93 + " \"value\": \"1\"\n"
102 private static final String TEMPLATE_ONE_INCREMENT_JSON = "{\n" +
104 " \"commonEventHeader\": {\n" +
105 " \"domain\": \"#RandomString\"\n" +
107 " \"measurementsForVfScalingFields\": {\n" +
108 " \"measurementsForVfSclaingFieldsVersion\": 2.0,\n" +
109 " \"additionalMeasurements\": {\n" +
110 " \"name\": \"licenseUsage\",\n" +
111 " \"extraFields\": {\n" +
112 " \"name\": \"#RandomString(4)\",\n" +
113 " \"value\": \"#Increment\"\n" +
120 private static final String TEMPLATE_WITH_SIMPLE_VALUE= "\"#RandomString(4)\"";
122 private static final String TEMPLATE_WITH_ARRAY_OF_PRIMITIVES = "[ 1, \"#RandomString(5)\", 3]";
124 private static final String TEMPLATE_TWO_INCREMENT_JSON = "{\n" +
126 " \"commonEventHeader\": {\n" +
127 " \"domain\": \"#RandomString\"\n" +
129 " \"measurementsForVfScalingFields\": {\n" +
130 " \"measurementsForVfSclaingFieldsVersion\": 2.0,\n" +
131 " \"additionalMeasurements\": {\n" +
132 " \"name\": \"licenseUsage\",\n" +
133 " \"extraFields\": {\n" +
134 " \"name\": \"#RandomString(4)\",\n" +
135 " \"value\": \"#Increment\",\n" +
136 " \"otherValue\": \"#Increment\"\n" +
143 private Gson gson = new Gson();
146 void shouldReplaceRandomStringKeyword() {
148 JsonObject templateJson = gson.fromJson(TEMPLATE_JSON, JsonObject.class);
149 KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), (id) -> 1);
152 JsonObject resultJson = keywordsHandler.substituteKeywords(templateJson, "").getAsJsonObject();
155 String extraFields = resultJson
156 .get("event").getAsJsonObject()
157 .get("measurementsForVfScalingFields").getAsJsonObject()
158 .get("additionalMeasurements").getAsJsonObject()
159 .get("extraFields").getAsJsonObject()
160 .get("name").getAsString();
161 String newDomain = resultJson
162 .get("event").getAsJsonObject()
163 .get("commonEventHeader").getAsJsonObject()
164 .get("domain").getAsString();
166 assertThat(extraFields.length()).isEqualTo(4);
167 assertThat(newDomain.length()).isEqualTo(DEFAULT_STRING_LENGTH);
171 void shouldReplaceRandomStringKeywordsInsideSingleValue() {
173 JsonObject templateJson = gson.fromJson(TEMPLATE_JSON_WITH_MANY_KEYWORDS_INSIDE_SINGLE_VALUE, JsonObject.class);
174 KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), (id) -> 1);
177 JsonObject resultJson = keywordsHandler.substituteKeywords(templateJson, "").getAsJsonObject();
180 String newDomain1 = resultJson
181 .get("event").getAsJsonObject()
182 .get("commonEventHeader").getAsJsonObject()
183 .get("domain1").getAsString();
184 String newDomain2 = resultJson
185 .get("event").getAsJsonObject()
186 .get("commonEventHeader").getAsJsonObject()
187 .get("domain2").getAsString();
189 assertThat(newDomain1.length()).isEqualTo(1+1+2+1+3);
190 assertThat(newDomain2.length()).isEqualTo(1+1+1+1+1);
194 void shouldReplaceRandomStringKeywordInTeplateAsArrayWithPrimitves() {
196 JsonElement templateJson = gson.fromJson(TEMPLATE_WITH_ARRAY_OF_PRIMITIVES, JsonElement.class);
197 KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), (id) -> 1);
200 JsonElement resultJson = keywordsHandler.substituteKeywords(templateJson, "");
201 assertThat(resultJson.getAsJsonArray().get(1).getAsString().length()).isEqualTo(5);
205 void shouldReplaceRandomStringKeywordInTeplateAsSimpleValue() {
207 JsonElement templateJson = gson.fromJson(TEMPLATE_WITH_SIMPLE_VALUE, JsonElement.class);
208 KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), (id) -> 1);
211 JsonElement resultJson = keywordsHandler.substituteKeywords(templateJson, "");
214 assertThat(resultJson.getAsString().length()).isEqualTo(4);
218 void shouldReplaceRandomStringKeywordInTeplateWithJsonArray() {
220 JsonElement templateJson = gson.fromJson(TEMPLATE_JSON_WITH_ARRAY, JsonElement.class);
221 KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), (id) -> 1);
224 JsonObject resultJson = keywordsHandler.substituteKeywords(templateJson, "").getAsJsonObject();
227 String actualValue = resultJson
228 .get("event").getAsJsonObject()
229 .get("measurementsForVfScalingFields").getAsJsonObject()
230 .get("additionalMeasurements").getAsJsonArray()
231 .get(0).getAsJsonObject()
232 .get("arrayOfFields").getAsJsonArray()
233 .get(1).getAsJsonObject()
234 .get("value").getAsString();
235 String otherActualValue = resultJson
236 .get("event").getAsJsonObject()
237 .get("commonEventHeader").getAsJsonObject()
238 .get("domain").getAsString();
240 assertThat(otherActualValue.length()).isEqualTo(1);
241 assertThat(actualValue.length()).isEqualTo(2);
245 void shouldReplaceOneIncrementKeyword() {
247 final Integer newIncrementedValue = 2;
248 JsonObject templateJson = gson.fromJson(TEMPLATE_ONE_INCREMENT_JSON, JsonObject.class);
249 KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), (id) -> newIncrementedValue);
252 JsonObject resultJson = keywordsHandler.substituteKeywords(templateJson, "some random id").getAsJsonObject();
255 String actualValue = resultJson
256 .get("event").getAsJsonObject()
257 .get("measurementsForVfScalingFields").getAsJsonObject()
258 .get("additionalMeasurements").getAsJsonObject()
259 .get("extraFields").getAsJsonObject()
260 .get("value").getAsString();
262 assertThat(actualValue).isEqualTo(newIncrementedValue.toString());
266 void shouldReplaceTwoIncrementKeyword() {
268 final Integer firstIncrementValue = 2;
269 final Integer secondIncrementValue = 3;
270 JsonObject templateJson = gson.fromJson(TEMPLATE_TWO_INCREMENT_JSON, JsonObject.class);
271 KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), new IncrementProvider() {
272 Queue<Integer> sequenceOfValues = new LinkedList<>(
273 Arrays.asList(firstIncrementValue, secondIncrementValue));
276 public int getAndIncrement(String id) {
277 return sequenceOfValues.poll();
282 JsonObject resultJson = keywordsHandler.substituteKeywords(templateJson, "some random id").getAsJsonObject();
283 resultJson = keywordsHandler.substituteKeywords(templateJson, "some random id").getAsJsonObject();
286 String actualValue = resultJson
287 .get("event").getAsJsonObject()
288 .get("measurementsForVfScalingFields").getAsJsonObject()
289 .get("additionalMeasurements").getAsJsonObject()
290 .get("extraFields").getAsJsonObject()
291 .get("value").getAsString();
293 String actualOtherValue = resultJson
294 .get("event").getAsJsonObject()
295 .get("measurementsForVfScalingFields").getAsJsonObject()
296 .get("additionalMeasurements").getAsJsonObject()
297 .get("extraFields").getAsJsonObject()
298 .get("otherValue").getAsString();
300 assertThat(actualValue).isEqualTo(secondIncrementValue.toString());
301 assertThat(actualOtherValue).isEqualTo(secondIncrementValue.toString());