e67d4a33bc161c08dab6be26f12e7d5cf766adf6
[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 static org.assertj.core.api.AssertionsForClassTypes.assertThat;
24 import static org.onap.pnfsimulator.simulator.KeywordsValueProvider.DEFAULT_STRING_LENGTH;
25
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;
33
34 class KeywordsHandlerTest {
35
36     private static final String TEMPLATE_JSON = "{\n" +
37         "  \"event\": {\n" +
38         "    \"commonEventHeader\": {\n" +
39         "      \"domain\": \"#RandomString\"\n" +
40         "    },\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" +
48         "        }\n" +
49         "      }\n" +
50         "    }\n" +
51         "  }\n" +
52         "}";
53
54     private static final String TEMPLATE_JSON_WITH_MANY_KEYWORDS_INSIDE_SINGLE_VALUE = "{\n" +
55         "  \"event\": {\n" +
56         "    \"commonEventHeader\": {\n" +
57         "      \"domain1\": \"#RandomString(1) #RandomString(2) #RandomString(3)\",\n" +
58         "      \"domain2\": \"1 #RandomString(1) 2\"\n" +
59         "    },\n" +
60         "    \"measurementsForVfScalingFields\": {\n" +
61         "      \"measurementsForVfSclaingFieldsVersion\": 2.0,\n" +
62         "      \"additionalMeasurements\": {\n" +
63         "        \"name\": \"licenseUsage\",\n" +
64         "        \"extraFields\": {\n" +
65         "          \"value\": \"1\"\n" +
66         "        }\n" +
67         "      }\n" +
68         "    }\n" +
69         "  }\n" +
70         "}";
71
72     private static final String TEMPLATE_JSON_WITH_ARRAY = "{\n"
73         + "    \"event\": {\n"
74         + "        \"commonEventHeader\": {\n"
75         + "            \"domain\": \"#RandomString(1)\",\n"
76         + "            \"version\": 2.0\n"
77         + "        },\n"
78         + "        \"measurementsForVfScalingFields\": {\n"
79         + "            \"additionalMeasurements\": [\n"
80         + "                {\n"
81         + "                    \"name\": \"licenseUsage\",\n"
82         + "                    \"arrayOfFields\": [\n"
83         + "                        {\n"
84         + "                            \"name\": \"G711AudioPort\",\n"
85         + "                            \"value\": \"1\"\n"
86         + "                        },\n"
87         + "                        {\n"
88         + "                            \"name\": [\"1\",\"2\"],\n"
89         + "                            \"value\": \"#RandomString(2)\"\n"
90         + "                        },\n"
91         + "                        {\n"
92         + "                            \"name\": \"G722AudioPort\",\n"
93         + "                            \"value\": \"1\"\n"
94         + "                        }\n"
95         + "                    ]\n"
96         + "                }\n"
97         + "            ]\n"
98         + "        }\n"
99         + "    }\n"
100         + "}";
101
102     private static final String TEMPLATE_ONE_INCREMENT_JSON = "{\n" +
103         "  \"event\": {\n" +
104         "    \"commonEventHeader\": {\n" +
105         "      \"domain\": \"#RandomString\"\n" +
106         "    },\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" +
114         "        }\n" +
115         "      }\n" +
116         "    }\n" +
117         "  }\n" +
118         "}";
119
120     private static final String TEMPLATE_WITH_SIMPLE_VALUE= "\"#RandomString(4)\"";
121
122     private static final String TEMPLATE_WITH_ARRAY_OF_PRIMITIVES = "[ 1, \"#RandomString(5)\", 3]";
123
124     private static final String TEMPLATE_TWO_INCREMENT_JSON = "{\n" +
125         "  \"event\": {\n" +
126         "    \"commonEventHeader\": {\n" +
127         "      \"domain\": \"#RandomString\"\n" +
128         "    },\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" +
137         "        }\n" +
138         "      }\n" +
139         "    }\n" +
140         "  }\n" +
141         "}";
142
143     private Gson gson = new Gson();
144
145     @Test
146     void shouldReplaceRandomStringKeyword() {
147         // given
148         JsonObject templateJson = gson.fromJson(TEMPLATE_JSON, JsonObject.class);
149         KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), (id) -> 1);
150
151         // when
152         JsonObject resultJson = keywordsHandler.substituteKeywords(templateJson, "").getAsJsonObject();
153
154         // then
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();
165
166         assertThat(extraFields.length()).isEqualTo(4);
167         assertThat(newDomain.length()).isEqualTo(DEFAULT_STRING_LENGTH);
168     }
169
170     @Test
171     void shouldReplaceRandomStringKeywordsInsideSingleValue() {
172         // given
173         JsonObject templateJson = gson.fromJson(TEMPLATE_JSON_WITH_MANY_KEYWORDS_INSIDE_SINGLE_VALUE, JsonObject.class);
174         KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), (id) -> 1);
175
176         // when
177         JsonObject resultJson = keywordsHandler.substituteKeywords(templateJson, "").getAsJsonObject();
178
179         // then
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();
188
189         assertThat(newDomain1.length()).isEqualTo(1+1+2+1+3);
190         assertThat(newDomain2.length()).isEqualTo(1+1+1+1+1);
191     }
192
193     @Test
194     void shouldReplaceRandomStringKeywordInTeplateAsArrayWithPrimitves() {
195         // given
196         JsonElement templateJson = gson.fromJson(TEMPLATE_WITH_ARRAY_OF_PRIMITIVES, JsonElement.class);
197         KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), (id) -> 1);
198
199         // when
200         JsonElement resultJson = keywordsHandler.substituteKeywords(templateJson, "");
201         assertThat(resultJson.getAsJsonArray().get(1).getAsString().length()).isEqualTo(5);
202     }
203
204     @Test
205     void shouldReplaceRandomStringKeywordInTeplateAsSimpleValue() {
206         // given
207         JsonElement templateJson = gson.fromJson(TEMPLATE_WITH_SIMPLE_VALUE, JsonElement.class);
208         KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), (id) -> 1);
209
210         // when
211         JsonElement resultJson = keywordsHandler.substituteKeywords(templateJson, "");
212
213         // then
214         assertThat(resultJson.getAsString().length()).isEqualTo(4);
215     }
216
217     @Test
218     void shouldReplaceRandomStringKeywordInTeplateWithJsonArray() {
219         // given
220         JsonElement templateJson = gson.fromJson(TEMPLATE_JSON_WITH_ARRAY, JsonElement.class);
221         KeywordsHandler keywordsHandler = new KeywordsHandler(new KeywordsExtractor(), (id) -> 1);
222
223         // when
224         JsonObject resultJson = keywordsHandler.substituteKeywords(templateJson, "").getAsJsonObject();
225
226         // then
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();
239
240         assertThat(otherActualValue.length()).isEqualTo(1);
241         assertThat(actualValue.length()).isEqualTo(2);
242     }
243
244     @Test
245     void shouldReplaceOneIncrementKeyword() {
246         // given
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);
250
251         // when
252         JsonObject resultJson = keywordsHandler.substituteKeywords(templateJson, "some random id").getAsJsonObject();
253
254         // then
255         String actualValue = resultJson
256             .get("event").getAsJsonObject()
257             .get("measurementsForVfScalingFields").getAsJsonObject()
258             .get("additionalMeasurements").getAsJsonObject()
259             .get("extraFields").getAsJsonObject()
260             .get("value").getAsString();
261
262         assertThat(actualValue).isEqualTo(newIncrementedValue.toString());
263     }
264
265     @Test
266     void shouldReplaceTwoIncrementKeyword() {
267         // given
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));
274
275             @Override
276             public int getAndIncrement(String id) {
277                 return sequenceOfValues.poll();
278             }
279         });
280
281         // when
282         JsonObject resultJson = keywordsHandler.substituteKeywords(templateJson, "some random id").getAsJsonObject();
283         resultJson = keywordsHandler.substituteKeywords(templateJson, "some random id").getAsJsonObject();
284
285         // then
286         String actualValue = resultJson
287             .get("event").getAsJsonObject()
288             .get("measurementsForVfScalingFields").getAsJsonObject()
289             .get("additionalMeasurements").getAsJsonObject()
290             .get("extraFields").getAsJsonObject()
291             .get("value").getAsString();
292
293         String actualOtherValue = resultJson
294             .get("event").getAsJsonObject()
295             .get("measurementsForVfScalingFields").getAsJsonObject()
296             .get("additionalMeasurements").getAsJsonObject()
297             .get("extraFields").getAsJsonObject()
298             .get("otherValue").getAsString();
299
300         assertThat(actualValue).isEqualTo(secondIncrementValue.toString());
301         assertThat(actualOtherValue).isEqualTo(secondIncrementValue.toString());
302
303     }
304 }