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\": { "
46 + " \"sourceName\":\"NOKQTFCOC540002E\","
47 + " \"nfNamingCode\":\"gNB\" "
49 + "\"pnfRegistrationFields\": {"
50 + " \"vendorName\": \"nokia\","
51 + " \"serialNumber\": \"QTFCOC540002E\","
52 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
53 + " \"modelNumber\": \"3310\","
54 + " \"unitType\": \"type\",\n"
55 + " \"unitFamily\": \"BBU\","
56 + " \"oamV4IpAddress\": \"10.16.123.234\","
57 + " \"softwareVersion\": \"v4.5.0.1\","
58 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
61 String parsed = "{\"event\": {"
62 + "\"commonEventHeader\": { "
63 + " \"sourceName\":\"NOKQTFCOC540002E\","
64 + " \"nfNamingCode\":\"gNB\" "
66 + "\"pnfRegistrationFields\": {"
67 + " \"vendorName\": \"nokia\","
68 + " \"serialNumber\": \"QTFCOC540002E\","
69 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
70 + " \"modelNumber\": \"3310\","
71 + " \"unitType\": \"type\",\n"
72 + " \"unitFamily\": \"BBU\","
73 + " \"oamV4IpAddress\": \"10.16.123.234\","
74 + " \"softwareVersion\": \"v4.5.0.1\","
75 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
78 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
79 .ipv4("10.16.123.234")
80 .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
81 .correlationId("NOKQTFCOC540002E")
82 .serialNumber("QTFCOC540002E")
87 .swVersion("v4.5.0.1")
90 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
91 JsonElement jsonElement = new JsonParser().parse(parsed);
92 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
93 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
94 ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser
95 .getJsonObject(Mono.just((message))).blockFirst();
97 Assertions.assertNotNull(consumerDmaapModel);
98 Assertions.assertEquals(expectedObject, consumerDmaapModel);
102 void whenPassingCorrectJsonWithoutIpv4_validationNotThrowingAnException() {
104 String message = "[{\"event\": {"
105 + "\"commonEventHeader\": { "
106 + " \"sourceName\":\"NOKQTFCOC540002E\","
107 + " \"nfNamingCode\":\"gNB\" "
109 + "\"pnfRegistrationFields\": {"
110 + " \"vendorName\": \"nokia\","
111 + " \"serialNumber\": \"QTFCOC540002E\","
112 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
113 + " \"modelNumber\": \"3310\","
114 + " \"unitType\": \"type\",\n"
115 + " \"unitFamily\": \"BBU\","
116 + " \"softwareVersion\": \"v4.5.0.1\","
117 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
120 String parsed = "{\"event\": {"
121 + "\"commonEventHeader\": { "
122 + " \"sourceName\":\"NOKQTFCOC540002E\","
123 + " \"nfNamingCode\":\"gNB\" "
125 + "\"pnfRegistrationFields\": {"
126 + " \"vendorName\": \"nokia\","
127 + " \"serialNumber\": \"QTFCOC540002E\","
128 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
129 + " \"modelNumber\": \"3310\","
130 + " \"unitType\": \"type\",\n"
131 + " \"unitFamily\": \"BBU\","
132 + " \"softwareVersion\": \"v4.5.0.1\","
133 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
137 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
138 JsonElement jsonElement = new JsonParser().parse(parsed);
139 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
140 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
141 dmaapConsumerJsonParser.getJsonObject(Mono.just((message)));
142 ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser.getJsonObject(Mono.just((message)))
145 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
147 .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
148 .correlationId("NOKQTFCOC540002E")
149 .serialNumber("QTFCOC540002E")
150 .equipVendor("nokia")
154 .swVersion("v4.5.0.1")
156 Assertions.assertNotNull(consumerDmaapModel);
157 Assertions.assertEquals(expectedObject, consumerDmaapModel);
161 void whenPassingCorrectJsonWithoutIpv6_validationNotThrowingAnException() {
163 String message = "[{\"event\": {"
164 + "\"commonEventHeader\": { "
165 + " \"sourceName\":\"NOKQTFCOC540002E\","
166 + " \"nfNamingCode\":\"gNB\" "
168 + "\"pnfRegistrationFields\": {"
169 + " \"vendorName\": \"nokia\","
170 + " \"serialNumber\": \"QTFCOC540002E\","
171 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
172 + " \"modelNumber\": \"3310\","
173 + " \"unitType\": \"type\",\n"
174 + " \"unitFamily\": \"BBU\","
175 + " \"oamV4IpAddress\": \"10.16.123.234\","
176 + " \"softwareVersion\": \"v4.5.0.1\""
180 String parsed = "{\"event\": {"
181 + "\"commonEventHeader\": { "
182 + " \"sourceName\":\"NOKQTFCOC540002E\","
183 + " \"nfNamingCode\":\"gNB\" "
185 + "\"pnfRegistrationFields\": {"
186 + " \"vendorName\": \"nokia\","
187 + " \"serialNumber\": \"QTFCOC540002E\","
188 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
189 + " \"modelNumber\": \"3310\","
190 + " \"unitType\": \"type\",\n"
191 + " \"unitFamily\": \"BBU\","
192 + " \"oamV4IpAddress\": \"10.16.123.234\","
193 + " \"softwareVersion\": \"v4.5.0.1\""
197 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
198 .ipv4("10.16.123.234")
200 .correlationId("NOKQTFCOC540002E")
201 .serialNumber("QTFCOC540002E")
202 .equipVendor("nokia")
206 .swVersion("v4.5.0.1")
209 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
210 JsonElement jsonElement = new JsonParser().parse(parsed);
211 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
212 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
213 ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser.getJsonObject(Mono.just((message)))
216 Assertions.assertNotNull(consumerDmaapModel);
217 Assertions.assertEquals(expectedObject, consumerDmaapModel);
221 void whenPassingCorrectJsonWithoutIpv4andIpv6_validationAddingAnException() {
223 String message = "[{\"event\": {"
224 + "\"commonEventHeader\": { "
225 + " \"sourceName\":\"NOKQTFCOC540002E\","
226 + " \"nfNamingCode\":\"gNB\" "
228 + "\"pnfRegistrationFields\": {"
229 + " \"vendorName\": \"nokia\","
230 + " \"serialNumber\": \"QTFCOC540002E\","
231 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
232 + " \"modelNumber\": \"3310\","
233 + " \"unitType\": \"type\",\n"
234 + " \"unitFamily\": \"BBU\","
235 + " \"softwareVersion\": \"v4.5.0.1\""
238 String parsed = "{\"event\": {"
239 + "\"commonEventHeader\": { "
240 + " \"sourceName\":\"NOKQTFCOC540002E\","
241 + " \"nfNamingCode\":\"gNB\" "
243 + "\"pnfRegistrationFields\": {"
244 + " \"vendorName\": \"nokia\","
245 + " \"serialNumber\": \"QTFCOC540002E\","
246 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
247 + " \"modelNumber\": \"3310\","
248 + " \"unitType\": \"type\",\n"
249 + " \"unitFamily\": \"BBU\","
250 + " \"softwareVersion\": \"v4.5.0.1\""
253 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
254 JsonElement jsonElement = new JsonParser().parse(parsed);
255 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
256 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
257 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(message)))
258 .expectSubscription().thenRequest(1).verifyComplete();
263 void whenPassingJsonWithoutMandatoryHeaderInformation_validationAddingAnException() {
264 String parsed = "{\"event\": {"
265 + "\"commonEventHeader\": {},"
266 + "\"pnfRegistrationFields\": {"
267 + " \"unitType\": \"AirScale\","
268 + " \"serialNumber\": \"QTFCOC540002E\","
269 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
270 + " \"manufactureDate\": \"1535014037024\","
271 + " \"modelNumber\": \"7BEA\",\n"
272 + " \"lastServiceDate\": \"1535014037024\","
273 + " \"unitFamily\": \"BBU\","
274 + " \"vendorName\": \"Nokia\","
275 + " \"softwareVersion\": \"v4.5.0.1\""
278 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
279 JsonElement jsonElement = new JsonParser().parse(parsed);
280 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
281 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
282 String incorrectMessage = "[{\"event\": {"
283 + "\"commonEventHeader\": {},"
284 + "\"pnfRegistrationFields\": {"
285 + " \"unitType\": \"AirScale\","
286 + " \"serialNumber\": \"QTFCOC540002E\","
287 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
288 + " \"manufactureDate\": \"1535014037024\","
289 + " \"modelNumber\": \"7BEA\",\n"
290 + " \"lastServiceDate\": \"1535014037024\","
291 + " \"unitFamily\": \"BBU\","
292 + " \"vendorName\": \"Nokia\","
293 + " \"softwareVersion\": \"v4.5.0.1\""
295 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessage)))
296 .expectSubscription().thenRequest(1).verifyComplete();
300 void whenPassingJsonWithoutSourceName_validationAddingAnException() {
301 String parsed = "{\"event\": {"
302 + "\"commonEventHeader\": {},"
303 + "\"pnfRegistrationFields\": {"
304 + " \"unitType\": \"AirScale\","
305 + " \"serialNumber\": \"QTFCOC540002E\","
306 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
307 + " \"manufactureDate\": \"1535014037024\","
308 + " \"modelNumber\": \"7BEA\",\n"
309 + " \"lastServiceDate\": \"1535014037024\","
310 + " \"unitFamily\": \"BBU\","
311 + " \"vendorName\": \"Nokia\","
312 + " \"softwareVersion\": \"v4.5.0.1\","
313 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
316 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
317 JsonElement jsonElement = new JsonParser().parse(parsed);
318 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
319 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
320 String jsonWithoutSourceName =
322 + "\"commonEventHeader\": {},"
323 + "\"pnfRegistrationFields\": {"
324 + " \"unitType\": \"AirScale\","
325 + " \"serialNumber\": \"QTFCOC540002E\","
326 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
327 + " \"manufactureDate\": \"1535014037024\","
328 + " \"modelNumber\": \"7BEA\",\n"
329 + " \"lastServiceDate\": \"1535014037024\","
330 + " \"unitFamily\": \"BBU\","
331 + " \"vendorName\": \"Nokia\","
332 + " \"softwareVersion\": \"v4.5.0.1\","
333 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
336 .create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutSourceName)))
337 .expectSubscription().thenRequest(1)
342 void whenPassingJsonWithoutIpInformation_validationAddingAnException() {
345 + "\"commonEventHeader\": {\"sourceName\": \"NOKQTFCOC540002E\"},"
346 + "\"pnfRegistrationFields\": {"
347 + " \"unitType\": \"AirScale\","
348 + " \"serialNumber\": \"QTFCOC540002E\","
349 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
350 + " \"manufactureDate\": \"1535014037024\","
351 + " \"modelNumber\": \"7BEA\",\n"
352 + " \"lastServiceDate\": \"1535014037024\","
353 + " \"unitFamily\": \"BBU\","
354 + " \"vendorName\": \"Nokia\","
355 + " \"softwareVersion\": \"v4.5.0.1\","
356 + " \"oamV4IpAddress\": \"\","
357 + " \"oamV6IpAddress\": \"\""
360 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
361 JsonElement jsonElement = new JsonParser().parse(parsed);
362 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
363 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
364 String jsonWithoutIpInformation =
366 + "\"commonEventHeader\": {\"sourceName\": \"NOKQTFCOC540002E\"},"
367 + "\"pnfRegistrationFields\": {"
368 + " \"unitType\": \"AirScale\","
369 + " \"serialNumber\": \"QTFCOC540002E\","
370 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
371 + " \"manufactureDate\": \"1535014037024\","
372 + " \"modelNumber\": \"7BEA\",\n"
373 + " \"lastServiceDate\": \"1535014037024\","
374 + " \"unitFamily\": \"BBU\","
375 + " \"vendorName\": \"Nokia\","
376 + " \"softwareVersion\": \"v4.5.0.1\","
377 + " \"oamV4IpAddress\": \"\","
378 + " \"oamV6IpAddress\": \"\""
380 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutIpInformation)))
381 .expectSubscription().thenRequest(1).verifyComplete();
385 void whenPassingJsonWithoutSourceNameValue_validationAddingAnException() {
388 + "\"commonEventHeader\": {\"sourceName\": \"\"},"
389 + "\"pnfRegistrationFields\": {"
390 + " \"unitType\": \"AirScale\","
391 + " \"serialNumber\": \"QTFCOC540002E\","
392 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
393 + " \"manufactureDate\": \"1535014037024\","
394 + " \"modelNumber\": \"7BEA\",\n"
395 + " \"lastServiceDate\": \"1535014037024\","
396 + " \"unitFamily\": \"BBU\","
397 + " \"vendorName\": \"Nokia\","
398 + " \"softwareVersion\": \"v4.5.0.1\","
399 + " \"oamV4IpAddress\": \"10.16.123.234\","
400 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
403 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
404 JsonElement jsonElement = new JsonParser().parse(parsed);
405 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
406 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
407 String jsonWithoutIpInformation =
409 + "\"commonEventHeader\": {\"sourceName\": \"\"},"
410 + "\"pnfRegistrationFields\": {"
411 + " \"unitType\": \"AirScale\","
412 + " \"serialNumber\": \"QTFCOC540002E\","
413 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
414 + " \"manufactureDate\": \"1535014037024\","
415 + " \"modelNumber\": \"7BEA\",\n"
416 + " \"lastServiceDate\": \"1535014037024\","
417 + " \"unitFamily\": \"BBU\","
418 + " \"vendorName\": \"Nokia\","
419 + " \"softwareVersion\": \"v4.5.0.1\","
420 + " \"oamV4IpAddress\": \"10.16.123.234\","
421 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
423 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutIpInformation)))
424 .expectSubscription().thenRequest(1).verifyComplete();
428 void whenPassingCorrectJsoArrayWithoutIpv4_validationNotThrowingAnException() {
430 String message = "[{\"event\": {"
431 + "\"commonEventHeader\": { "
432 + " \"sourceName\":\"NOKQTFCOC540002E\","
433 + " \"nfNamingCode\":\"gNB\" "
435 + "\"pnfRegistrationFields\": {"
436 + " \"vendorName\": \"nokia\","
437 + " \"serialNumber\": \"QTFCOC540002E\","
438 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
439 + " \"modelNumber\": \"3310\","
440 + " \"unitType\": \"type\",\n"
441 + " \"unitFamily\": \"BBU\","
442 + " \"softwareVersion\": \"v4.5.0.1\","
443 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
446 + "\"commonEventHeader\": { "
447 + " \"sourceName\":\"NOKQTFCOC540002E\","
448 + " \"nfNamingCode\":\"gNB\" "
450 + "\"pnfRegistrationFields\": {"
451 + " \"vendorName\": \"nokia\","
452 + " \"serialNumber\": \"QTFCOC540002E\","
453 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
454 + " \"modelNumber\": \"3310\","
455 + " \"unitType\": \"type\",\n"
456 + " \"unitFamily\": \"BBU\","
457 + " \"softwareVersion\": \"v4.5.0.1\","
458 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
465 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
467 .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
468 .correlationId("NOKQTFCOC540002E")
469 .serialNumber("QTFCOC540002E")
470 .equipVendor("nokia")
474 .swVersion("v4.5.0.1")
477 DmaapConsumerJsonParser dmaapConsumerJsonParser = new DmaapConsumerJsonParser();
480 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(message)))
481 .expectSubscription().expectNext(expectedObject).expectNext(expectedObject).verifyComplete();