e065fc77b914fa7e5e73f80454e37d47b8aaa258
[dcaegen2/services/prh.git] /
1 /*
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
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.dcaegen2.services.prh.service;
22
23 import static org.mockito.Mockito.spy;
24
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParser;
29 import java.util.Optional;
30 import org.junit.jupiter.api.Assertions;
31 import org.junit.jupiter.api.Test;
32 import org.mockito.Mockito;
33 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
34 import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel;
35 import reactor.core.publisher.Mono;
36 import reactor.test.StepVerifier;
37
38 /**
39  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/8/18
40  */
41 class DmaapConsumerJsonParserTest {
42
43     private JsonParser jsonParser = new JsonParser();
44
45     @Test
46     void whenPassingCorrectJson_validationNotThrowingAnException() {
47         //given
48         String message = "[{\"event\": {"
49             + "\"commonEventHeader\": { "
50             + " \"sourceName\":\"NOKQTFCOC540002E\","
51             + " \"nfNamingCode\":\"gNB\" "
52             + "},"
53             + "\"pnfRegistrationFields\": {"
54             + " \"vendorName\": \"nokia\","
55             + " \"serialNumber\": \"QTFCOC540002E\","
56             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
57             + " \"modelNumber\": \"3310\","
58             + " \"unitType\": \"type\",\n"
59             + " \"unitFamily\": \"BBU\","
60             + " \"oamV4IpAddress\": \"10.16.123.234\","
61             + " \"softwareVersion\": \"v4.5.0.1\","
62             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
63             + " \"additionalFields\": {\"attachmentPoint\":\"bla-bla-30-3\",\"cvlan\":\"678\",\"svlan\":\"1005\"}"
64             + "}}}]";
65
66         String parsed = "{\"event\": {"
67             + "\"commonEventHeader\": { "
68             + " \"sourceName\":\"NOKQTFCOC540002E\","
69             + " \"nfNamingCode\":\"gNB\" "
70             + "},"
71             + "\"pnfRegistrationFields\": {"
72             + " \"vendorName\": \"nokia\","
73             + " \"serialNumber\": \"QTFCOC540002E\","
74             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
75             + " \"modelNumber\": \"3310\","
76             + " \"unitType\": \"type\",\n"
77             + " \"unitFamily\": \"BBU\","
78             + " \"oamV4IpAddress\": \"10.16.123.234\","
79             + " \"softwareVersion\": \"v4.5.0.1\","
80             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
81             + " \"additionalFields\": {\"attachmentPoint\":\"bla-bla-30-3\",\"cvlan\":\"678\",\"svlan\":\"1005\"}"
82             + "}}}";
83
84         JsonObject jsonObject = new JsonParser().parse("{\n"
85             + "        \"attachmentPoint\": \"bla-bla-30-3\",\n"
86             + "        \"cvlan\": \"678\",\n"
87             + "        \"svlan\": \"1005\"\n"
88             + "      }").getAsJsonObject();
89
90         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
91             .ipv4("10.16.123.234")
92             .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
93             .correlationId("NOKQTFCOC540002E")
94             .serialNumber("QTFCOC540002E")
95             .equipVendor("nokia")
96             .equipModel("3310")
97             .equipType("type")
98             .nfRole("gNB")
99             .swVersion("v4.5.0.1")
100             .additionalFields(jsonObject)
101             .build();
102
103         JsonArray messageAsJsonArray = (JsonArray) jsonParser.parse(message);
104
105         //when
106         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
107         JsonElement jsonElement = jsonParser.parse(parsed);
108         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
109             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
110
111         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser
112             .getJsonObject(Mono.just((messageAsJsonArray))).blockFirst();
113         //then
114         Assertions.assertNotNull(consumerDmaapModel);
115         Assertions.assertEquals(expectedObject, consumerDmaapModel);
116     }
117
118     @Test
119     void whenPassingJsonWithoutAdditionalFields_validationNotThrowingAnException() {
120         //given
121         String message = "[{\"event\": {"
122             + "\"commonEventHeader\": { "
123             + " \"sourceName\":\"NOKQTFCOC540002E\","
124             + " \"nfNamingCode\":\"gNB\" "
125             + "},"
126             + "\"pnfRegistrationFields\": {"
127             + " \"vendorName\": \"nokia\","
128             + " \"serialNumber\": \"QTFCOC540002E\","
129             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
130             + " \"modelNumber\": \"3310\","
131             + " \"unitType\": \"type\",\n"
132             + " \"unitFamily\": \"BBU\","
133             + " \"oamV4IpAddress\": \"10.16.123.234\","
134             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
135             + " \"softwareVersion\": \"v4.5.0.1\""
136             + "}}}]";
137
138         String parsed = "{\"event\": {"
139             + "\"commonEventHeader\": { "
140             + " \"sourceName\":\"NOKQTFCOC540002E\","
141             + " \"nfNamingCode\":\"gNB\" "
142             + "},"
143             + "\"pnfRegistrationFields\": {"
144             + " \"vendorName\": \"nokia\","
145             + " \"serialNumber\": \"QTFCOC540002E\","
146             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
147             + " \"modelNumber\": \"3310\","
148             + " \"unitType\": \"type\",\n"
149             + " \"unitFamily\": \"BBU\","
150             + " \"oamV4IpAddress\": \"10.16.123.234\","
151             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
152             + " \"softwareVersion\": \"v4.5.0.1\""
153             + "}}}";
154
155         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
156             .correlationId("NOKQTFCOC540002E")
157             .serialNumber("QTFCOC540002E")
158             .ipv4("10.16.123.234")
159             .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
160             .equipVendor("nokia")
161             .equipModel("3310")
162             .equipType("type")
163             .nfRole("gNB")
164             .swVersion("v4.5.0.1")
165             .build();
166         JsonArray mesageAsJsonArray = (JsonArray) jsonParser.parse(message);
167
168         //when
169         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
170         JsonElement jsonElement = new JsonParser().parse(parsed);
171         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
172             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
173         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser
174             .getJsonObject(Mono.just((mesageAsJsonArray))).blockFirst();
175         //then
176         Assertions.assertNotNull(consumerDmaapModel);
177         Assertions.assertEquals(expectedObject, consumerDmaapModel);
178     }
179
180     @Test
181     void whenPassingJsonWithEmptyAdditionalFields_validationNotThrowingAnException() {
182         //given
183         String message = "[{\"event\": {"
184             + "\"commonEventHeader\": { "
185             + " \"sourceName\":\"NOKQTFCOC540002E\","
186             + " \"nfNamingCode\":\"gNB\" "
187             + "},"
188             + "\"pnfRegistrationFields\": {"
189             + " \"vendorName\": \"nokia\","
190             + " \"serialNumber\": \"QTFCOC540002E\","
191             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
192             + " \"modelNumber\": \"3310\","
193             + " \"unitType\": \"type\",\n"
194             + " \"unitFamily\": \"BBU\","
195             + " \"softwareVersion\": \"v4.5.0.1\","
196             + " \"oamV4IpAddress\": \"10.16.123.234\","
197             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
198             + " \"additionalFields\": {}"
199             + "}}}]";
200
201         String parsed = "{\"event\": {"
202             + "\"commonEventHeader\": { "
203             + " \"sourceName\":\"NOKQTFCOC540002E\","
204             + " \"nfNamingCode\":\"gNB\" "
205             + "},"
206             + "\"pnfRegistrationFields\": {"
207             + " \"vendorName\": \"nokia\","
208             + " \"serialNumber\": \"QTFCOC540002E\","
209             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
210             + " \"modelNumber\": \"3310\","
211             + " \"unitType\": \"type\",\n"
212             + " \"unitFamily\": \"BBU\","
213             + " \"softwareVersion\": \"v4.5.0.1\","
214             + " \"oamV4IpAddress\": \"10.16.123.234\","
215             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
216             + " \"additionalFields\": {}"
217             + "}}}";
218
219         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
220             .correlationId("NOKQTFCOC540002E")
221             .serialNumber("QTFCOC540002E")
222             .ipv4("10.16.123.234")
223             .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
224             .equipVendor("nokia")
225             .equipModel("3310")
226             .equipType("type")
227             .nfRole("gNB")
228             .swVersion("v4.5.0.1")
229             .additionalFields(new JsonObject())
230             .build();
231
232         JsonArray messageAsJsonArray = (JsonArray) jsonParser.parse(message);
233
234         //when
235         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
236         JsonElement jsonElement = jsonParser.parse(parsed);
237         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
238             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
239
240         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser
241             .getJsonObject(Mono.just((messageAsJsonArray))).blockFirst();
242         //then
243         Assertions.assertNotNull(consumerDmaapModel);
244         Assertions.assertEquals(expectedObject, consumerDmaapModel);
245     }
246
247     @Test
248     void whenPassingJsonWithoutMandatoryHeaderInformation_validationAddingAnException() {
249         String parsed = "{\"event\": {"
250             + "\"commonEventHeader\": {},"
251             + "\"pnfRegistrationFields\": {"
252             + " \"unitType\": \"AirScale\","
253             + " \"serialNumber\": \"QTFCOC540002E\","
254             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
255             + " \"manufactureDate\": \"1535014037024\","
256             + " \"modelNumber\": \"7BEA\",\n"
257             + " \"lastServiceDate\": \"1535014037024\","
258             + " \"unitFamily\": \"BBU\","
259             + " \"vendorName\": \"Nokia\","
260             + " \"softwareVersion\": \"v4.5.0.1\","
261             + " \"oamV4IpAddress\": \"10.16.123.234\","
262             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
263             + " \"additionalFields\": {}"
264             + "}}}";
265
266         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
267         JsonElement jsonElement = jsonParser.parse(parsed);
268         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
269             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
270         String incorrectMessage = "[{\"event\": {"
271             + "\"commonEventHeader\": {},"
272             + "\"pnfRegistrationFields\": {"
273             + " \"unitType\": \"AirScale\","
274             + " \"serialNumber\": \"QTFCOC540002E\","
275             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
276             + " \"manufactureDate\": \"1535014037024\","
277             + " \"modelNumber\": \"7BEA\",\n"
278             + " \"lastServiceDate\": \"1535014037024\","
279             + " \"unitFamily\": \"BBU\","
280             + " \"vendorName\": \"Nokia\","
281             + " \"softwareVersion\": \"v4.5.0.1\","
282             + " \"oamV4IpAddress\": \"10.16.123.234\","
283             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
284             + " \"additionalFields\": {}"
285             + "}}}]";
286
287         JsonArray incorrectMessageAsJsonArray = (JsonArray) jsonParser.parse(incorrectMessage);
288
289         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageAsJsonArray)))
290             .expectSubscription().thenRequest(1).verifyComplete();
291     }
292
293     @Test
294     void whenPassingJsonWithoutSourceName_validationAddingAnException() {
295         String parsed = "{\"event\": {"
296             + "\"commonEventHeader\": {},"
297             + "\"pnfRegistrationFields\": {"
298             + " \"unitType\": \"AirScale\","
299             + " \"serialNumber\": \"QTFCOC540002E\","
300             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
301             + " \"manufactureDate\": \"1535014037024\","
302             + " \"modelNumber\": \"7BEA\",\n"
303             + " \"lastServiceDate\": \"1535014037024\","
304             + " \"unitFamily\": \"BBU\","
305             + " \"vendorName\": \"Nokia\","
306             + " \"softwareVersion\": \"v4.5.0.1\","
307             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
308             + " \"additionalFields\": {}"
309             + "}}}";
310
311         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
312         JsonElement jsonElement = jsonParser.parse(parsed);
313         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
314             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
315         String jsonWithoutSourceName =
316             "[{\"event\": {"
317                 + "\"commonEventHeader\": {},"
318                 + "\"pnfRegistrationFields\": {"
319                 + " \"unitType\": \"AirScale\","
320                 + " \"serialNumber\": \"QTFCOC540002E\","
321                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
322                 + " \"manufactureDate\": \"1535014037024\","
323                 + " \"modelNumber\": \"7BEA\",\n"
324                 + " \"lastServiceDate\": \"1535014037024\","
325                 + " \"unitFamily\": \"BBU\","
326                 + " \"vendorName\": \"Nokia\","
327                 + " \"softwareVersion\": \"v4.5.0.1\","
328                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
329                 + " \"additionalFields\": {}"
330                 + "}}}]";
331
332         JsonArray jsonWithoutSourceNameAsJsonArray = (JsonArray) jsonParser.parse(jsonWithoutSourceName);
333         StepVerifier
334             .create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutSourceNameAsJsonArray)))
335             .expectSubscription().thenRequest(1)
336             .verifyComplete();
337
338     }
339
340     @Test
341     void whenPassingJsonWithoutSourceNameValue_validationAddingAnException() {
342         String parsed =
343             "{\"event\": {"
344                 + "\"commonEventHeader\": {\"sourceName\": \"\"},"
345                 + "\"pnfRegistrationFields\": {"
346                 + " \"unitType\": \"AirScale\","
347                 + " \"serialNumber\": \"QTFCOC540002E\","
348                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
349                 + " \"manufactureDate\": \"1535014037024\","
350                 + " \"modelNumber\": \"7BEA\",\n"
351                 + " \"lastServiceDate\": \"1535014037024\","
352                 + " \"unitFamily\": \"BBU\","
353                 + " \"vendorName\": \"Nokia\","
354                 + " \"softwareVersion\": \"v4.5.0.1\","
355                 + " \"oamV4IpAddress\": \"10.16.123.234\","
356                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
357                 + " \"additionalFields\": {}"
358                 + "}}}";
359
360         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
361         JsonElement jsonElement = jsonParser.parse(parsed);
362         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
363             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
364         String jsonWithoutIpInformation =
365             "[{\"event\": {"
366                 + "\"commonEventHeader\": {\"sourceName\": \"\"},"
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\": \"10.16.123.234\","
378                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
379                 + " \"additionalFields\": {}"
380                 + "}}}]";
381
382         JsonArray jsonWithoutIpInformationAsJsonArray = (JsonArray) jsonParser.parse(jsonWithoutIpInformation);
383
384         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutIpInformationAsJsonArray)))
385             .expectSubscription().thenRequest(1).verifyComplete();
386     }
387
388     @Test
389     void whenPassingCorrectJsonWithoutIpv4_validationNotThrowingAnException() {
390         //given
391         String message = "[{\"event\": {"
392             + "\"commonEventHeader\": { "
393             + " \"sourceName\":\"NOKQTFCOC540002E\","
394             + " \"nfNamingCode\":\"gNB\" "
395             + "},"
396             + "\"pnfRegistrationFields\": {"
397             + " \"vendorName\": \"nokia\","
398             + " \"serialNumber\": \"QTFCOC540002E\","
399             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
400             + " \"modelNumber\": \"3310\","
401             + " \"unitType\": \"type\",\n"
402             + " \"unitFamily\": \"BBU\","
403             + " \"softwareVersion\": \"v4.5.0.1\","
404             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
405             + "}}}]";
406
407         String parsed = "{\"event\": {"
408             + "\"commonEventHeader\": { "
409             + " \"sourceName\":\"NOKQTFCOC540002E\","
410             + " \"nfNamingCode\":\"gNB\" "
411             + "},"
412             + "\"pnfRegistrationFields\": {"
413             + " \"vendorName\": \"nokia\","
414             + " \"serialNumber\": \"QTFCOC540002E\","
415             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
416             + " \"modelNumber\": \"3310\","
417             + " \"unitType\": \"type\",\n"
418             + " \"unitFamily\": \"BBU\","
419             + " \"softwareVersion\": \"v4.5.0.1\","
420             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
421             + "}}}";
422
423         JsonArray messageAsJsonArray = (JsonArray) jsonParser.parse(message);
424
425         //when
426         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
427         JsonElement jsonElement = jsonParser.parse(parsed);
428         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
429             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
430
431         dmaapConsumerJsonParser.getJsonObject(Mono.just((messageAsJsonArray)));
432         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser.getJsonObject(Mono.just((messageAsJsonArray)))
433             .blockFirst();
434         //then
435         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
436             .ipv4("")
437             .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
438             .correlationId("NOKQTFCOC540002E")
439             .serialNumber("QTFCOC540002E")
440             .equipVendor("nokia")
441             .equipModel("3310")
442             .equipType("type")
443             .nfRole("gNB")
444             .swVersion("v4.5.0.1")
445             .build();
446         Assertions.assertNotNull(consumerDmaapModel);
447         Assertions.assertEquals(expectedObject, consumerDmaapModel);
448
449     }
450
451     @Test
452     void whenPassingCorrectJsonWithoutIpv6_validationNotThrowingAnException() {
453         //given
454         String message = "[{\"event\": {"
455             + "\"commonEventHeader\": { "
456             + " \"sourceName\":\"NOKQTFCOC540002E\","
457             + " \"nfNamingCode\":\"gNB\" "
458             + "},"
459             + "\"pnfRegistrationFields\": {"
460             + " \"vendorName\": \"nokia\","
461             + " \"serialNumber\": \"QTFCOC540002E\","
462             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
463             + " \"modelNumber\": \"3310\","
464             + " \"unitType\": \"type\",\n"
465             + " \"unitFamily\": \"BBU\","
466             + " \"oamV4IpAddress\": \"10.16.123.234\","
467             + " \"softwareVersion\": \"v4.5.0.1\""
468             + "}}}]";
469
470         String parsed = "{\"event\": {"
471             + "\"commonEventHeader\": { "
472             + " \"sourceName\":\"NOKQTFCOC540002E\","
473             + " \"nfNamingCode\":\"gNB\" "
474             + "},"
475             + "\"pnfRegistrationFields\": {"
476             + " \"vendorName\": \"nokia\","
477             + " \"serialNumber\": \"QTFCOC540002E\","
478             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
479             + " \"modelNumber\": \"3310\","
480             + " \"unitType\": \"type\",\n"
481             + " \"unitFamily\": \"BBU\","
482             + " \"oamV4IpAddress\": \"10.16.123.234\","
483             + " \"softwareVersion\": \"v4.5.0.1\""
484             + "}}}";
485
486         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
487             .ipv4("10.16.123.234")
488             .ipv6("")
489             .correlationId("NOKQTFCOC540002E")
490             .serialNumber("QTFCOC540002E")
491             .equipVendor("nokia")
492             .equipModel("3310")
493             .equipType("type")
494             .nfRole("gNB")
495             .swVersion("v4.5.0.1")
496             .build();
497
498         JsonArray messageAsJsonArray = (JsonArray) jsonParser.parse(message);
499
500         //when
501         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
502         JsonElement jsonElement = jsonParser.parse(parsed);
503         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
504             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
505         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser.getJsonObject(Mono.just((messageAsJsonArray)))
506             .blockFirst();
507
508         //then
509         Assertions.assertNotNull(consumerDmaapModel);
510         Assertions.assertEquals(expectedObject, consumerDmaapModel);
511
512     }
513
514     @Test
515     void whenPassingCorrectJsonWithoutIpv4andIpv6_validationAddingAnException() {
516         //given
517         String message = "[{\"event\": {"
518             + "\"commonEventHeader\": { "
519             + " \"sourceName\":\"NOKQTFCOC540002E\","
520             + " \"nfNamingCode\":\"gNB\" "
521             + "},"
522             + "\"pnfRegistrationFields\": {"
523             + " \"vendorName\": \"nokia\","
524             + " \"serialNumber\": \"QTFCOC540002E\","
525             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
526             + " \"modelNumber\": \"3310\","
527             + " \"unitType\": \"type\",\n"
528             + " \"unitFamily\": \"BBU\","
529             + " \"softwareVersion\": \"v4.5.0.1\","
530             + " \"additionalFields\": {}"
531             + "}}}]";
532
533         String parsed = "{\"event\": {"
534             + "\"commonEventHeader\": { "
535             + " \"sourceName\":\"NOKQTFCOC540002E\","
536             + " \"nfNamingCode\":\"gNB\" "
537             + "},"
538             + "\"pnfRegistrationFields\": {"
539             + " \"vendorName\": \"nokia\","
540             + " \"serialNumber\": \"QTFCOC540002E\","
541             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
542             + " \"modelNumber\": \"3310\","
543             + " \"unitType\": \"type\",\n"
544             + " \"unitFamily\": \"BBU\","
545             + " \"softwareVersion\": \"v4.5.0.1\","
546             + " \"additionalFields\": {}"
547             + "}}}";
548
549         JsonArray messageAsJsonArray = (JsonArray) jsonParser.parse(message);
550
551         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
552         JsonElement jsonElement = jsonParser.parse(parsed);
553         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
554             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
555
556         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageAsJsonArray)))
557             .expectSubscription().thenRequest(1).verifyComplete();
558
559     }
560
561     @Test
562     void whenPassingJsonWithoutIpInformation_validationAddingAnException() {
563         String parsed =
564             "{\"event\": {"
565                 + "\"commonEventHeader\": {\"sourceName\": \"NOKQTFCOC540002E\"},"
566                 + "\"pnfRegistrationFields\": {"
567                 + " \"unitType\": \"AirScale\","
568                 + " \"serialNumber\": \"QTFCOC540002E\","
569                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
570                 + " \"manufactureDate\": \"1535014037024\","
571                 + " \"modelNumber\": \"7BEA\",\n"
572                 + " \"lastServiceDate\": \"1535014037024\","
573                 + " \"unitFamily\": \"BBU\","
574                 + " \"vendorName\": \"Nokia\","
575                 + " \"softwareVersion\": \"v4.5.0.1\","
576                 + " \"oamV4IpAddress\": \"\","
577                 + " \"oamV6IpAddress\": \"\","
578                 + " \"additionalFields\": {}"
579                 + "}}}";
580
581         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
582         JsonElement jsonElement = jsonParser.parse(parsed);
583         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
584             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
585         String jsonWithoutIpInformation =
586             "[{\"event\": {"
587                 + "\"commonEventHeader\": {\"sourceName\": \"NOKQTFCOC540002E\"},"
588                 + "\"pnfRegistrationFields\": {"
589                 + " \"unitType\": \"AirScale\","
590                 + " \"serialNumber\": \"QTFCOC540002E\","
591                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
592                 + " \"manufactureDate\": \"1535014037024\","
593                 + " \"modelNumber\": \"7BEA\",\n"
594                 + " \"lastServiceDate\": \"1535014037024\","
595                 + " \"unitFamily\": \"BBU\","
596                 + " \"vendorName\": \"Nokia\","
597                 + " \"softwareVersion\": \"v4.5.0.1\","
598                 + " \"oamV4IpAddress\": \"\","
599                 + " \"oamV6IpAddress\": \"\","
600                 + " \"additionalFields\": {}"
601                 + "}}}]";
602
603         JsonArray jsonWithoutOpInformationAsJsonArray = (JsonArray) jsonParser.parse(jsonWithoutIpInformation);
604
605         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutOpInformationAsJsonArray)))
606             .expectSubscription().thenRequest(1).verifyComplete();
607     }
608
609     @Test
610     void whenPassingCorrectJsoArrayWithoutIpv4_validationNotThrowingAnException() {
611         //given
612         String message = "[{\"event\": {"
613             + "\"commonEventHeader\": { "
614             + "  \"sourceName\":\"NOKQTFCOC540002E\","
615             + "  \"nfNamingCode\":\"gNB\" "
616             + "  },"
617             + "\"pnfRegistrationFields\": {"
618             + " \"vendorName\": \"nokia\","
619             + " \"serialNumber\": \"QTFCOC540002E\","
620             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
621             + " \"modelNumber\": \"3310\","
622             + " \"unitType\": \"type\",\n"
623             + " \"unitFamily\": \"BBU\","
624             + " \"softwareVersion\": \"v4.5.0.1\","
625             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
626             + " \"additionalFields\": {}"
627             + "}}},"
628             + " {\"event\": {"
629             + "\"commonEventHeader\": { "
630             + " \"sourceName\":\"NOKQTFCOC540002E\","
631             + " \"nfNamingCode\":\"gNB\" "
632             + "},"
633             + "\"pnfRegistrationFields\": {"
634             + " \"vendorName\": \"nokia\","
635             + " \"serialNumber\": \"QTFCOC540002E\","
636             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
637             + " \"modelNumber\": \"3310\","
638             + " \"unitType\": \"type\",\n"
639             + " \"unitFamily\": \"BBU\","
640             + " \"softwareVersion\": \"v4.5.0.1\","
641             + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\","
642             + " \"additionalFields\": {}"
643             + "}}}]";
644
645         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
646             .ipv4("")
647             .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
648             .correlationId("NOKQTFCOC540002E")
649             .serialNumber("QTFCOC540002E")
650             .equipVendor("nokia")
651             .equipModel("3310")
652             .equipType("type")
653             .nfRole("gNB")
654             .swVersion("v4.5.0.1")
655             .additionalFields(new JsonObject())
656             .build();
657
658         JsonArray messageAsJsonArray = (JsonArray) jsonParser.parse(message);
659
660         //when
661         DmaapConsumerJsonParser dmaapConsumerJsonParser = new DmaapConsumerJsonParser();
662
663         //then
664         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageAsJsonArray)))
665             .expectSubscription().expectNext(expectedObject).expectNext(expectedObject).verifyComplete();
666     }
667 }