2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. 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.dcaegen2.services.prh.service;
23 import static org.mockito.Mockito.spy;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParser;
27 import java.util.Optional;
28 import org.junit.jupiter.api.Assertions;
29 import org.junit.jupiter.api.Test;
30 import org.mockito.Mockito;
31 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
32 import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel;
33 import reactor.core.publisher.Mono;
34 import reactor.test.StepVerifier;
37 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/8/18
39 class DmaapConsumerJsonParserTest {
42 void whenPassingCorrectJson_validationNotThrowingAnException() {
44 String message = "[{\"event\": {"
45 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
46 + "\"pnfRegistrationFields\": {"
47 + " \"equip-vendor\": \"nokia\","
48 + " \"serial-number\": \"QTFCOC540002E\","
49 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
50 + " \"equip-model\": \"3310\","
51 + " \"equip-type\": \"type\",\n"
52 + " \"nf-role\": \"role\","
53 + " \"oamV4IpAddress\": \"10.16.123.234\","
54 + " \"sw-version\": \"v4.5.0.1\","
55 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
58 String parsed = "{\"event\": {"
59 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
60 + "\"pnfRegistrationFields\": {"
61 + " \"equip-vendor\": \"nokia\","
62 + " \"serial-number\": \"QTFCOC540002E\","
63 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
64 + " \"equip-model\": \"3310\","
65 + " \"equip-type\": \"type\",\n"
66 + " \"nf-role\": \"role\","
67 + " \"unitFamily\": \"BBU\","
68 + " \"oamV4IpAddress\": \"10.16.123.234\","
69 + " \"sw-version\": \"v4.5.0.1\","
70 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
73 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
74 .ipv4("10.16.123.234")
75 .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
76 .correlationId("NOKQTFCOC540002E")
77 .serialNumber("QTFCOC540002E")
82 .swVersion("v4.5.0.1")
85 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
86 JsonElement jsonElement = new JsonParser().parse(parsed);
87 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
88 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
89 ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser
90 .getJsonObject(Mono.just((message))).blockFirst();
92 Assertions.assertNotNull(consumerDmaapModel);
93 Assertions.assertEquals(expectedObject, consumerDmaapModel);
97 void whenPassingCorrectJsonWithoutIpv4_validationNotThrowingAnException() {
99 String message = "[{\"event\": {"
100 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
101 + "\"pnfRegistrationFields\": {"
102 + " \"equip-vendor\": \"nokia\","
103 + " \"serial-number\": \"QTFCOC540002E\","
104 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
105 + " \"equip-model\": \"3310\","
106 + " \"equip-type\": \"type\",\n"
107 + " \"nf-role\": \"role\","
108 + " \"unitFamily\": \"BBU\","
109 + " \"sw-version\": \"v4.5.0.1\","
110 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
113 String parsed = "{\"event\": {"
114 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
115 + "\"pnfRegistrationFields\": {"
116 + " \"equip-vendor\": \"nokia\","
117 + " \"serial-number\": \"QTFCOC540002E\","
118 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
119 + " \"equip-model\": \"3310\","
120 + " \"equip-type\": \"type\",\n"
121 + " \"nf-role\": \"role\","
122 + " \"unitFamily\": \"BBU\","
123 + " \"sw-version\": \"v4.5.0.1\","
124 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
128 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
129 JsonElement jsonElement = new JsonParser().parse(parsed);
130 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
131 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
132 dmaapConsumerJsonParser.getJsonObject(Mono.just((message)));
133 ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser.getJsonObject(Mono.just((message)))
136 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
138 .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
139 .correlationId("NOKQTFCOC540002E")
140 .serialNumber("QTFCOC540002E")
141 .equipVendor("nokia")
145 .swVersion("v4.5.0.1")
147 Assertions.assertNotNull(consumerDmaapModel);
148 Assertions.assertEquals(expectedObject, consumerDmaapModel);
152 void whenPassingCorrectJsonWithoutIpv6_validationNotThrowingAnException() {
154 String message = "[{\"event\": {"
155 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
156 + "\"pnfRegistrationFields\": {"
157 + " \"equip-vendor\": \"nokia\","
158 + " \"serial-number\": \"QTFCOC540002E\","
159 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
160 + " \"equip-model\": \"3310\","
161 + " \"equip-type\": \"type\",\n"
162 + " \"nf-role\": \"role\","
163 + " \"unitFamily\": \"BBU\","
164 + " \"oamV4IpAddress\": \"10.16.123.234\","
165 + " \"sw-version\": \"v4.5.0.1\""
168 String parsed = "{\"event\": {"
169 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
170 + "\"pnfRegistrationFields\": {"
171 + " \"equip-vendor\": \"nokia\","
172 + " \"serial-number\": \"QTFCOC540002E\","
173 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
174 + " \"equip-model\": \"3310\","
175 + " \"equip-type\": \"type\",\n"
176 + " \"nf-role\": \"role\","
177 + " \"unitFamily\": \"BBU\","
178 + " \"oamV4IpAddress\": \"10.16.123.234\","
179 + " \"sw-version\": \"v4.5.0.1\""
182 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
183 .ipv4("10.16.123.234")
185 .correlationId("NOKQTFCOC540002E")
186 .serialNumber("QTFCOC540002E")
187 .equipVendor("nokia")
191 .swVersion("v4.5.0.1")
194 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
195 JsonElement jsonElement = new JsonParser().parse(parsed);
196 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
197 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
198 ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser.getJsonObject(Mono.just((message)))
201 Assertions.assertNotNull(consumerDmaapModel);
202 Assertions.assertEquals(expectedObject, consumerDmaapModel);
206 void whenPassingCorrectJsonWithoutIpv4andIpv6_validationAddingAnException() {
208 String message = "[{\"event\": {"
209 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
210 + "\"pnfRegistrationFields\": {"
211 + " \"unitType\": \"AirScale\","
212 + " \"serialNumber\": \"QTFCOC540002E\","
213 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
214 + " \"manufactureDate\": \"1535014037024\","
215 + " \"modelNumber\": \"7BEA\",\n"
216 + " \"lastServiceDate\": \"1535014037024\","
217 + " \"unitFamily\": \"BBU\","
218 + " \"vendorName\": \"Nokia\","
219 + " \"softwareVersion\": \"v4.5.0.1\""
222 String parsed = "{\"event\": {"
223 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
224 + "\"pnfRegistrationFields\": {"
225 + " \"unitType\": \"AirScale\","
226 + " \"serialNumber\": \"QTFCOC540002E\","
227 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
228 + " \"manufactureDate\": \"1535014037024\","
229 + " \"modelNumber\": \"7BEA\",\n"
230 + " \"lastServiceDate\": \"1535014037024\","
231 + " \"unitFamily\": \"BBU\","
232 + " \"vendorName\": \"Nokia\","
233 + " \"softwareVersion\": \"v4.5.0.1\""
236 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
237 JsonElement jsonElement = new JsonParser().parse(parsed);
238 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
239 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
240 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(message)))
241 .expectSubscription().thenRequest(1).verifyComplete();
246 void whenPassingJsonWithoutMandatoryHeaderInformation_validationAddingAnException() {
247 String parsed = "{\"event\": {"
248 + "\"commonEventHeader\": {},"
249 + "\"pnfRegistrationFields\": {"
250 + " \"unitType\": \"AirScale\","
251 + " \"serialNumber\": \"QTFCOC540002E\","
252 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
253 + " \"manufactureDate\": \"1535014037024\","
254 + " \"modelNumber\": \"7BEA\",\n"
255 + " \"lastServiceDate\": \"1535014037024\","
256 + " \"unitFamily\": \"BBU\","
257 + " \"vendorName\": \"Nokia\","
258 + " \"softwareVersion\": \"v4.5.0.1\""
261 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
262 JsonElement jsonElement = new JsonParser().parse(parsed);
263 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
264 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
265 String incorrectMessage = "[{\"event\": {"
266 + "\"commonEventHeader\": {},"
267 + "\"pnfRegistrationFields\": {"
268 + " \"unitType\": \"AirScale\","
269 + " \"serialNumber\": \"QTFCOC540002E\","
270 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
271 + " \"manufactureDate\": \"1535014037024\","
272 + " \"modelNumber\": \"7BEA\",\n"
273 + " \"lastServiceDate\": \"1535014037024\","
274 + " \"unitFamily\": \"BBU\","
275 + " \"vendorName\": \"Nokia\","
276 + " \"softwareVersion\": \"v4.5.0.1\""
278 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessage)))
279 .expectSubscription().thenRequest(1).verifyComplete();
283 void whenPassingJsonWithoutSourceName_validationAddingAnException() {
284 String parsed = "{\"event\": {"
285 + "\"commonEventHeader\": {},"
286 + "\"pnfRegistrationFields\": {"
287 + " \"unitType\": \"AirScale\","
288 + " \"serialNumber\": \"QTFCOC540002E\","
289 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
290 + " \"manufactureDate\": \"1535014037024\","
291 + " \"modelNumber\": \"7BEA\",\n"
292 + " \"lastServiceDate\": \"1535014037024\","
293 + " \"unitFamily\": \"BBU\","
294 + " \"vendorName\": \"Nokia\","
295 + " \"softwareVersion\": \"v4.5.0.1\","
296 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
299 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
300 JsonElement jsonElement = new JsonParser().parse(parsed);
301 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
302 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
303 String jsonWithoutSourceName =
305 + "\"commonEventHeader\": {},"
306 + "\"pnfRegistrationFields\": {"
307 + " \"unitType\": \"AirScale\","
308 + " \"serialNumber\": \"QTFCOC540002E\","
309 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
310 + " \"manufactureDate\": \"1535014037024\","
311 + " \"modelNumber\": \"7BEA\",\n"
312 + " \"lastServiceDate\": \"1535014037024\","
313 + " \"unitFamily\": \"BBU\","
314 + " \"vendorName\": \"Nokia\","
315 + " \"softwareVersion\": \"v4.5.0.1\","
316 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
319 .create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutSourceName)))
320 .expectSubscription().thenRequest(1)
325 void whenPassingJsonWithoutIpInformation_validationAddingAnException() {
328 + "\"commonEventHeader\": {\"sourceName\": \"NOKQTFCOC540002E\"},"
329 + "\"pnfRegistrationFields\": {"
330 + " \"unitType\": \"AirScale\","
331 + " \"serialNumber\": \"QTFCOC540002E\","
332 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
333 + " \"manufactureDate\": \"1535014037024\","
334 + " \"modelNumber\": \"7BEA\",\n"
335 + " \"lastServiceDate\": \"1535014037024\","
336 + " \"unitFamily\": \"BBU\","
337 + " \"vendorName\": \"Nokia\","
338 + " \"softwareVersion\": \"v4.5.0.1\","
339 + " \"oamV4IpAddress\": \"\","
340 + " \"oamV6IpAddress\": \"\""
343 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
344 JsonElement jsonElement = new JsonParser().parse(parsed);
345 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
346 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
347 String jsonWithoutIpInformation =
349 + "\"commonEventHeader\": {\"sourceName\": \"NOKQTFCOC540002E\"},"
350 + "\"pnfRegistrationFields\": {"
351 + " \"unitType\": \"AirScale\","
352 + " \"serialNumber\": \"QTFCOC540002E\","
353 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
354 + " \"manufactureDate\": \"1535014037024\","
355 + " \"modelNumber\": \"7BEA\",\n"
356 + " \"lastServiceDate\": \"1535014037024\","
357 + " \"unitFamily\": \"BBU\","
358 + " \"vendorName\": \"Nokia\","
359 + " \"softwareVersion\": \"v4.5.0.1\","
360 + " \"oamV4IpAddress\": \"\","
361 + " \"oamV6IpAddress\": \"\""
363 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutIpInformation)))
364 .expectSubscription().thenRequest(1).verifyComplete();
368 void whenPassingJsonWithoutSourceNameValue_validationAddingAnException() {
371 + "\"commonEventHeader\": {\"sourceName\": \"\"},"
372 + "\"pnfRegistrationFields\": {"
373 + " \"unitType\": \"AirScale\","
374 + " \"serialNumber\": \"QTFCOC540002E\","
375 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
376 + " \"manufactureDate\": \"1535014037024\","
377 + " \"modelNumber\": \"7BEA\",\n"
378 + " \"lastServiceDate\": \"1535014037024\","
379 + " \"unitFamily\": \"BBU\","
380 + " \"vendorName\": \"Nokia\","
381 + " \"softwareVersion\": \"v4.5.0.1\","
382 + " \"oamV4IpAddress\": \"10.16.123.234\","
383 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
386 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
387 JsonElement jsonElement = new JsonParser().parse(parsed);
388 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
389 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
390 String jsonWithoutIpInformation =
392 + "\"commonEventHeader\": {\"sourceName\": \"\"},"
393 + "\"pnfRegistrationFields\": {"
394 + " \"unitType\": \"AirScale\","
395 + " \"serialNumber\": \"QTFCOC540002E\","
396 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
397 + " \"manufactureDate\": \"1535014037024\","
398 + " \"modelNumber\": \"7BEA\",\n"
399 + " \"lastServiceDate\": \"1535014037024\","
400 + " \"unitFamily\": \"BBU\","
401 + " \"vendorName\": \"Nokia\","
402 + " \"softwareVersion\": \"v4.5.0.1\","
403 + " \"oamV4IpAddress\": \"10.16.123.234\","
404 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
406 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutIpInformation)))
407 .expectSubscription().thenRequest(1).verifyComplete();
411 void whenPassingCorrectJsoArrayWithoutIpv4_validationNotThrowingAnException() {
415 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
416 + "\"pnfRegistrationFields\": {"
417 + " \"equip-vendor\": \"nokia\","
418 + " \"serial-number\": \"QTFCOC540002E\","
419 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
420 + " \"equip-model\": \"3310\","
421 + " \"equip-type\": \"type\",\n"
422 + " \"nf-role\": \"role\","
423 + " \"unitFamily\": \"BBU\","
424 + " \"sw-version\": \"v4.5.0.1\","
425 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
428 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
429 + "\"pnfRegistrationFields\": {"
430 + " \"equip-vendor\": \"nokia\","
431 + " \"serial-number\": \"QTFCOC540002E\","
432 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
433 + " \"equip-model\": \"3310\","
434 + " \"equip-type\": \"type\",\n"
435 + " \"nf-role\": \"role\","
436 + " \"unitFamily\": \"BBU\","
437 + " \"sw-version\": \"v4.5.0.1\","
438 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
441 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
443 .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
444 .correlationId("NOKQTFCOC540002E")
445 .serialNumber("QTFCOC540002E")
446 .equipVendor("nokia")
450 .swVersion("v4.5.0.1")
453 DmaapConsumerJsonParser dmaapConsumerJsonParser = new DmaapConsumerJsonParser();
456 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(message)))
457 .expectSubscription().expectNext(expectedObject).expectNext(expectedObject).verifyComplete();