225d46ee5e18adaa120da43bc6415f233abeb361
[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.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.exceptions.DmaapNotFoundException;
32 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
33 import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel;
34 import reactor.core.publisher.Mono;
35 import reactor.test.StepVerifier;
36
37 /**
38  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/8/18
39  */
40 class DmaapConsumerJsonParserTest {
41
42     @Test
43     void whenPassingCorrectJson_validationNotThrowingAnException() {
44         //given
45         String message = "[{\"event\": {"
46                 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
47                 + "\"pnfRegistrationFields\": {"
48                 + " \"unitType\": \"AirScale\","
49                 + " \"serialNumber\": \"QTFCOC540002E\","
50                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
51                 + " \"manufactureDate\": \"1535014037024\","
52                 + " \"modelNumber\": \"7BEA\",\n"
53                 + " \"lastServiceDate\": \"1535014037024\","
54                 + " \"unitFamily\": \"BBU\","
55                 + " \"vendorName\": \"Nokia\","
56                 + " \"oamV4IpAddress\": \"10.16.123.234\","
57                 + " \"softwareVersion\": \"v4.5.0.1\","
58                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
59                 + "}}}]";
60
61         String parsed = "{\"event\": {"
62                 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
63                 + "\"pnfRegistrationFields\": {"
64                 + " \"unitType\": \"AirScale\","
65                 + " \"serialNumber\": \"QTFCOC540002E\","
66                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
67                 + " \"manufactureDate\": \"1535014037024\","
68                 + " \"modelNumber\": \"7BEA\",\n"
69                 + " \"lastServiceDate\": \"1535014037024\","
70                 + " \"unitFamily\": \"BBU\","
71                 + " \"vendorName\": \"Nokia\","
72                 + " \"oamV4IpAddress\": \"10.16.123.234\","
73                 + " \"softwareVersion\": \"v4.5.0.1\","
74                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
75                 + "}}}";
76
77         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234")
78             .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
79             .correlationId("NOKQTFCOC540002E").build();
80         //when
81         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
82         JsonElement jsonElement = new JsonParser().parse(parsed);
83         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
84             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
85         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser
86             .getJsonObject(Mono.just((message))).block();
87         //then
88         Assertions.assertNotNull(consumerDmaapModel);
89         Assertions.assertEquals(expectedObject, consumerDmaapModel);
90     }
91
92     @Test
93     void whenPassingCorrectJsonWithoutIpv4_validationNotThrowingAnException() {
94         //given
95         String message = "[{\"event\": {"
96                 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
97                 + "\"pnfRegistrationFields\": {"
98                 + " \"unitType\": \"AirScale\","
99                 + " \"serialNumber\": \"QTFCOC540002E\","
100                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
101                 + " \"manufactureDate\": \"1535014037024\","
102                 + " \"modelNumber\": \"7BEA\",\n"
103                 + " \"lastServiceDate\": \"1535014037024\","
104                 + " \"unitFamily\": \"BBU\","
105                 + " \"vendorName\": \"Nokia\","
106                 + " \"softwareVersion\": \"v4.5.0.1\","
107                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
108                 + "}}}]";
109
110         String parsed = "{\"event\": {"
111                 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
112                 + "\"pnfRegistrationFields\": {"
113                 + " \"unitType\": \"AirScale\","
114                 + " \"serialNumber\": \"QTFCOC540002E\","
115                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
116                 + " \"manufactureDate\": \"1535014037024\","
117                 + " \"modelNumber\": \"7BEA\",\n"
118                 + " \"lastServiceDate\": \"1535014037024\","
119                 + " \"unitFamily\": \"BBU\","
120                 + " \"vendorName\": \"Nokia\","
121                 + " \"softwareVersion\": \"v4.5.0.1\","
122                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
123                 + "}}}";
124
125         //when
126         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
127         JsonElement jsonElement = new JsonParser().parse(parsed);
128         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
129             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
130         dmaapConsumerJsonParser.getJsonObject(Mono.just((message)));
131         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser.getJsonObject(Mono.just((message)))
132             .block();
133         //then
134         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("")
135             .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
136             .correlationId("NOKQTFCOC540002E").build();
137         Assertions.assertNotNull(consumerDmaapModel);
138         Assertions.assertEquals(expectedObject, consumerDmaapModel);
139     }
140
141     @Test
142     void whenPassingCorrectJsonWithoutIpv6_validationNotThrowingAnException() {
143         //given
144         String message = "[{\"event\": {"
145                 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
146                 + "\"pnfRegistrationFields\": {"
147                 + " \"unitType\": \"AirScale\","
148                 + " \"serialNumber\": \"QTFCOC540002E\","
149                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
150                 + " \"manufactureDate\": \"1535014037024\","
151                 + " \"modelNumber\": \"7BEA\",\n"
152                 + " \"lastServiceDate\": \"1535014037024\","
153                 + " \"unitFamily\": \"BBU\","
154                 + " \"vendorName\": \"Nokia\","
155                 + " \"oamV4IpAddress\": \"10.16.123.234\","
156                 + " \"softwareVersion\": \"v4.5.0.1\""
157                 + "}}}]";
158
159         String parsed = "{\"event\": {"
160                 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
161                 + "\"pnfRegistrationFields\": {"
162                 + " \"unitType\": \"AirScale\","
163                 + " \"serialNumber\": \"QTFCOC540002E\","
164                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
165                 + " \"manufactureDate\": \"1535014037024\","
166                 + " \"modelNumber\": \"7BEA\",\n"
167                 + " \"lastServiceDate\": \"1535014037024\","
168                 + " \"unitFamily\": \"BBU\","
169                 + " \"vendorName\": \"Nokia\","
170                 + " \"oamV4IpAddress\": \"10.16.123.234\","
171                 + " \"softwareVersion\": \"v4.5.0.1\""
172                 + "}}}";
173
174         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234").ipv6("")
175             .correlationId("NOKQTFCOC540002E").build();
176         //when
177         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
178         JsonElement jsonElement = new JsonParser().parse(parsed);
179         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
180             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
181         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser.getJsonObject(Mono.just((message)))
182             .block();
183         //then
184         Assertions.assertNotNull(consumerDmaapModel);
185         Assertions.assertEquals(expectedObject, consumerDmaapModel);
186     }
187
188     @Test
189     void whenPassingCorrectJsonWithoutIpv4andIpv6_validationThrowingAnException() {
190         //given
191         String message = "[{\"event\": {"
192                 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
193                 + "\"pnfRegistrationFields\": {"
194                 + " \"unitType\": \"AirScale\","
195                 + " \"serialNumber\": \"QTFCOC540002E\","
196                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
197                 + " \"manufactureDate\": \"1535014037024\","
198                 + " \"modelNumber\": \"7BEA\",\n"
199                 + " \"lastServiceDate\": \"1535014037024\","
200                 + " \"unitFamily\": \"BBU\","
201                 + " \"vendorName\": \"Nokia\","
202                 + " \"softwareVersion\": \"v4.5.0.1\""
203                 + "}}}]";
204
205         String parsed = "{\"event\": {"
206                 + "\"commonEventHeader\": { \"sourceName\":\"NOKQTFCOC540002E\"},"
207                 + "\"pnfRegistrationFields\": {"
208                 + " \"unitType\": \"AirScale\","
209                 + " \"serialNumber\": \"QTFCOC540002E\","
210                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
211                 + " \"manufactureDate\": \"1535014037024\","
212                 + " \"modelNumber\": \"7BEA\",\n"
213                 + " \"lastServiceDate\": \"1535014037024\","
214                 + " \"unitFamily\": \"BBU\","
215                 + " \"vendorName\": \"Nokia\","
216                 + " \"softwareVersion\": \"v4.5.0.1\""
217                 + "}}}";
218
219         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
220         JsonElement jsonElement = new JsonParser().parse(parsed);
221         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
222             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
223         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(message)))
224             .expectSubscription().expectError(DmaapNotFoundException.class).verify();
225
226     }
227
228     @Test
229     void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() {
230         String parsed = "{\"event\": {"
231                 + "\"commonEventHeader\": {},"
232                 + "\"pnfRegistrationFields\": {"
233                 + " \"unitType\": \"AirScale\","
234                 + " \"serialNumber\": \"QTFCOC540002E\","
235                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
236                 + " \"manufactureDate\": \"1535014037024\","
237                 + " \"modelNumber\": \"7BEA\",\n"
238                 + " \"lastServiceDate\": \"1535014037024\","
239                 + " \"unitFamily\": \"BBU\","
240                 + " \"vendorName\": \"Nokia\","
241                 + " \"softwareVersion\": \"v4.5.0.1\""
242                 + "}}}";
243
244         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
245         JsonElement jsonElement = new JsonParser().parse(parsed);
246         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
247             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
248         String incorrectMessage = "[{\"event\": {"
249                 + "\"commonEventHeader\": {},"
250                 + "\"pnfRegistrationFields\": {"
251                 + " \"unitType\": \"AirScale\","
252                 + " \"serialNumber\": \"QTFCOC540002E\","
253                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
254                 + " \"manufactureDate\": \"1535014037024\","
255                 + " \"modelNumber\": \"7BEA\",\n"
256                 + " \"lastServiceDate\": \"1535014037024\","
257                 + " \"unitFamily\": \"BBU\","
258                 + " \"vendorName\": \"Nokia\","
259                 + " \"softwareVersion\": \"v4.5.0.1\""
260                 + "}}}]";
261         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessage)))
262             .expectSubscription().expectError(DmaapNotFoundException.class).verify();
263     }
264
265     @Test
266     void whenPassingJsonWithoutSourceName_validationThrowingAnException() {
267         String parsed = "{\"event\": {"
268                 + "\"commonEventHeader\": {},"
269                 + "\"pnfRegistrationFields\": {"
270                 + " \"unitType\": \"AirScale\","
271                 + " \"serialNumber\": \"QTFCOC540002E\","
272                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
273                 + " \"manufactureDate\": \"1535014037024\","
274                 + " \"modelNumber\": \"7BEA\",\n"
275                 + " \"lastServiceDate\": \"1535014037024\","
276                 + " \"unitFamily\": \"BBU\","
277                 + " \"vendorName\": \"Nokia\","
278                 + " \"softwareVersion\": \"v4.5.0.1\","
279                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
280                 + "}}}";
281
282         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
283         JsonElement jsonElement = new JsonParser().parse(parsed);
284         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
285             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
286         String jsonWithoutSourceName =
287                 "[{\"event\": {"
288                         + "\"commonEventHeader\": {},"
289                         + "\"pnfRegistrationFields\": {"
290                         + " \"unitType\": \"AirScale\","
291                         + " \"serialNumber\": \"QTFCOC540002E\","
292                         + " \"pnfRegistrationFieldsVersion\": \"2.0\","
293                         + " \"manufactureDate\": \"1535014037024\","
294                         + " \"modelNumber\": \"7BEA\",\n"
295                         + " \"lastServiceDate\": \"1535014037024\","
296                         + " \"unitFamily\": \"BBU\","
297                         + " \"vendorName\": \"Nokia\","
298                         + " \"softwareVersion\": \"v4.5.0.1\","
299                         + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
300                         + "}}}]";
301         StepVerifier
302             .create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutSourceName)))
303             .expectSubscription().expectError(DmaapNotFoundException.class).verify();
304     }
305
306     @Test
307     void whenPassingJsonWithoutIpInformation_validationThrowingAnException() {
308         String parsed =
309                 "{\"event\": {"
310                         + "\"commonEventHeader\": {\"sourceName\": \"NOKQTFCOC540002E\"},"
311                         + "\"pnfRegistrationFields\": {"
312                         + " \"unitType\": \"AirScale\","
313                         + " \"serialNumber\": \"QTFCOC540002E\","
314                         + " \"pnfRegistrationFieldsVersion\": \"2.0\","
315                         + " \"manufactureDate\": \"1535014037024\","
316                         + " \"modelNumber\": \"7BEA\",\n"
317                         + " \"lastServiceDate\": \"1535014037024\","
318                         + " \"unitFamily\": \"BBU\","
319                         + " \"vendorName\": \"Nokia\","
320                         + " \"softwareVersion\": \"v4.5.0.1\","
321                         + " \"oamV4IpAddress\": \"\","
322                         + " \"oamV6IpAddress\": \"\""
323                         + "}}}";
324
325         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
326         JsonElement jsonElement = new JsonParser().parse(parsed);
327         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
328             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
329         String jsonWithoutIpInformation =
330                 "[{\"event\": {"
331                         + "\"commonEventHeader\": {\"sourceName\": \"NOKQTFCOC540002E\"},"
332                         + "\"pnfRegistrationFields\": {"
333                         + " \"unitType\": \"AirScale\","
334                         + " \"serialNumber\": \"QTFCOC540002E\","
335                         + " \"pnfRegistrationFieldsVersion\": \"2.0\","
336                         + " \"manufactureDate\": \"1535014037024\","
337                         + " \"modelNumber\": \"7BEA\",\n"
338                         + " \"lastServiceDate\": \"1535014037024\","
339                         + " \"unitFamily\": \"BBU\","
340                         + " \"vendorName\": \"Nokia\","
341                         + " \"softwareVersion\": \"v4.5.0.1\","
342                         + " \"oamV4IpAddress\": \"\","
343                         + " \"oamV6IpAddress\": \"\""
344                         + "}}}]";
345         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutIpInformation)))
346             .expectSubscription().expectError(DmaapNotFoundException.class).verify();
347     }
348
349     @Test
350     void whenPassingJsonWithoutSourceNameValue_validationThrowingAnException() {
351         String parsed =
352                 "{\"event\": {"
353                         + "\"commonEventHeader\": {\"sourceName\": \"\"},"
354                         + "\"pnfRegistrationFields\": {"
355                         + " \"unitType\": \"AirScale\","
356                         + " \"serialNumber\": \"QTFCOC540002E\","
357                         + " \"pnfRegistrationFieldsVersion\": \"2.0\","
358                         + " \"manufactureDate\": \"1535014037024\","
359                         + " \"modelNumber\": \"7BEA\",\n"
360                         + " \"lastServiceDate\": \"1535014037024\","
361                         + " \"unitFamily\": \"BBU\","
362                         + " \"vendorName\": \"Nokia\","
363                         + " \"softwareVersion\": \"v4.5.0.1\","
364                         + " \"oamV4IpAddress\": \"10.16.123.234\","
365                         + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
366                         + "}}}";
367
368         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
369         JsonElement jsonElement = new JsonParser().parse(parsed);
370         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
371                 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
372         String jsonWithoutIpInformation =
373                 "[{\"event\": {"
374                         + "\"commonEventHeader\": {\"sourceName\": \"\"},"
375                         + "\"pnfRegistrationFields\": {"
376                         + " \"unitType\": \"AirScale\","
377                         + " \"serialNumber\": \"QTFCOC540002E\","
378                         + " \"pnfRegistrationFieldsVersion\": \"2.0\","
379                         + " \"manufactureDate\": \"1535014037024\","
380                         + " \"modelNumber\": \"7BEA\",\n"
381                         + " \"lastServiceDate\": \"1535014037024\","
382                         + " \"unitFamily\": \"BBU\","
383                         + " \"vendorName\": \"Nokia\","
384                         + " \"softwareVersion\": \"v4.5.0.1\","
385                         + " \"oamV4IpAddress\": \"10.16.123.234\","
386                         + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
387                         + "}}}]";
388         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutIpInformation)))
389                 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
390     }
391 }