e0eccc36940012010e9b29e119ffe3af7de7a533
[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\": {\"pnfRegistrationFields\": {"
46                 + " \"unitType\": \"AirScale\","
47                 + " \"serialNumber\": \"QTFCOC540002E\","
48                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
49                 + " \"manufactureDate\": \"1535014037024\","
50                 + " \"modelNumber\": \"7BEA\",\n"
51                 + " \"lastServiceDate\": \"1535014037024\","
52                 + " \"unitFamily\": \"BBU\","
53                 + " \"vendorName\": \"Nokia\","
54                 + " \"oamV4IpAddress\": \"10.16.123.234\","
55                 + " \"softwareVersion\": \"v4.5.0.1\","
56                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
57                 + "}}}]";
58
59         String parsed = "{\"event\": {\"pnfRegistrationFields\": {"
60                 + " \"unitType\": \"AirScale\","
61                 + " \"serialNumber\": \"QTFCOC540002E\","
62                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
63                 + " \"manufactureDate\": \"1535014037024\","
64                 + " \"modelNumber\": \"7BEA\",\n"
65                 + " \"lastServiceDate\": \"1535014037024\","
66                 + " \"unitFamily\": \"BBU\","
67                 + " \"vendorName\": \"Nokia\","
68                 + " \"oamV4IpAddress\": \"10.16.123.234\","
69                 + " \"softwareVersion\": \"v4.5.0.1\","
70                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
71                 + "}}}";
72
73         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234")
74             .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
75             .pnfName("NOKQTFCOC540002E").build();
76         //when
77         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
78         JsonElement jsonElement = new JsonParser().parse(parsed);
79         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
80             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
81         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser
82             .getJsonObject(Mono.just((message))).block();
83         //then
84         Assertions.assertNotNull(consumerDmaapModel);
85         Assertions.assertEquals(expectedObject, consumerDmaapModel);
86     }
87
88     @Test
89     void whenPassingCorrectJsonWithoutIpv4_validationNotThrowingAnException() {
90         //given
91         String message = "[{\"event\": {\"pnfRegistrationFields\": {"
92                 + " \"unitType\": \"AirScale\","
93                 + " \"serialNumber\": \"QTFCOC540002E\","
94                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
95                 + " \"manufactureDate\": \"1535014037024\","
96                 + " \"modelNumber\": \"7BEA\",\n"
97                 + " \"lastServiceDate\": \"1535014037024\","
98                 + " \"unitFamily\": \"BBU\","
99                 + " \"vendorName\": \"Nokia\","
100                 + " \"softwareVersion\": \"v4.5.0.1\","
101                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
102                 + "}}}]";
103
104         String parsed = "{\"event\": {\"pnfRegistrationFields\": {"
105                 + " \"unitType\": \"AirScale\","
106                 + " \"serialNumber\": \"QTFCOC540002E\","
107                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
108                 + " \"manufactureDate\": \"1535014037024\","
109                 + " \"modelNumber\": \"7BEA\",\n"
110                 + " \"lastServiceDate\": \"1535014037024\","
111                 + " \"unitFamily\": \"BBU\","
112                 + " \"vendorName\": \"Nokia\","
113                 + " \"softwareVersion\": \"v4.5.0.1\","
114                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
115                 + "}}}";
116
117         //when
118         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
119         JsonElement jsonElement = new JsonParser().parse(parsed);
120         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
121             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
122         dmaapConsumerJsonParser.getJsonObject(Mono.just((message)));
123         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser.getJsonObject(Mono.just((message)))
124             .block();
125         //then
126         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("")
127             .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
128             .pnfName("NOKQTFCOC540002E").build();
129         Assertions.assertNotNull(consumerDmaapModel);
130         Assertions.assertEquals(expectedObject, consumerDmaapModel);
131     }
132
133     @Test
134     void whenPassingCorrectJsonWithoutIpv6_validationNotThrowingAnException() {
135         //given
136         String message = "[{\"event\": {\"pnfRegistrationFields\": {"
137                 + " \"unitType\": \"AirScale\","
138                 + " \"serialNumber\": \"QTFCOC540002E\","
139                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
140                 + " \"manufactureDate\": \"1535014037024\","
141                 + " \"modelNumber\": \"7BEA\",\n"
142                 + " \"lastServiceDate\": \"1535014037024\","
143                 + " \"unitFamily\": \"BBU\","
144                 + " \"vendorName\": \"Nokia\","
145                 + " \"oamV4IpAddress\": \"10.16.123.234\","
146                 + " \"softwareVersion\": \"v4.5.0.1\""
147                 + "}}}]";
148
149         String parsed = "{\"event\": {\"pnfRegistrationFields\": {"
150                 + " \"unitType\": \"AirScale\","
151                 + " \"serialNumber\": \"QTFCOC540002E\","
152                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
153                 + " \"manufactureDate\": \"1535014037024\","
154                 + " \"modelNumber\": \"7BEA\",\n"
155                 + " \"lastServiceDate\": \"1535014037024\","
156                 + " \"unitFamily\": \"BBU\","
157                 + " \"vendorName\": \"Nokia\","
158                 + " \"oamV4IpAddress\": \"10.16.123.234\","
159                 + " \"softwareVersion\": \"v4.5.0.1\""
160                 + "}}}";
161
162         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234").ipv6("")
163             .pnfName("NOKQTFCOC540002E").build();
164         //when
165         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
166         JsonElement jsonElement = new JsonParser().parse(parsed);
167         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
168             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
169         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser.getJsonObject(Mono.just((message)))
170             .block();
171         //then
172         Assertions.assertNotNull(consumerDmaapModel);
173         Assertions.assertEquals(expectedObject, consumerDmaapModel);
174     }
175
176     @Test
177     void whenPassingCorrectJsonWithoutIpv4andIpv6_validationThrowingAnException() {
178         String message = "[{\"event\": {\"pnfRegistrationFields\": {"
179                 + " \"unitType\": \"AirScale\","
180                 + " \"serialNumber\": \"QTFCOC540002E\","
181                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
182                 + " \"manufactureDate\": \"1535014037024\","
183                 + " \"modelNumber\": \"7BEA\",\n"
184                 + " \"lastServiceDate\": \"1535014037024\","
185                 + " \"unitFamily\": \"BBU\","
186                 + " \"vendorName\": \"Nokia\","
187                 + " \"softwareVersion\": \"v4.5.0.1\""
188                 + "}}}]";
189
190         String parsed = "{\"event\": {\"pnfRegistrationFields\": {"
191                 + " \"unitType\": \"AirScale\","
192                 + " \"serialNumber\": \"QTFCOC540002E\","
193                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
194                 + " \"manufactureDate\": \"1535014037024\","
195                 + " \"modelNumber\": \"7BEA\",\n"
196                 + " \"lastServiceDate\": \"1535014037024\","
197                 + " \"unitFamily\": \"BBU\","
198                 + " \"vendorName\": \"Nokia\","
199                 + " \"softwareVersion\": \"v4.5.0.1\""
200                 + "}}}";
201
202         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
203         JsonElement jsonElement = new JsonParser().parse(parsed);
204         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
205             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
206         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(message)))
207             .expectSubscription().expectError(DmaapNotFoundException.class).verify();
208
209     }
210
211     @Test
212     void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() {
213         String parsed = "{\"event\": {\"pnfRegistrationFields\": {"
214                 + " \"unitType\": \"AirScale\","
215                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
216                 + " \"manufactureDate\": \"1535014037024\","
217                 + " \"modelNumber\": \"7BEA\",\n"
218                 + " \"lastServiceDate\": \"1535014037024\","
219                 + " \"unitFamily\": \"BBU\","
220                 + " \"softwareVersion\": \"v4.5.0.1\""
221                 + "}}}";
222
223         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
224         JsonElement jsonElement = new JsonParser().parse(parsed);
225         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
226             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
227         String incorrectMessage = "[{\"event\": {\"pnfRegistrationFields\": {"
228                 + " \"unitType\": \"AirScale\","
229                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
230                 + " \"manufactureDate\": \"1535014037024\","
231                 + " \"modelNumber\": \"7BEA\",\n"
232                 + " \"lastServiceDate\": \"1535014037024\","
233                 + " \"unitFamily\": \"BBU\","
234                 + " \"softwareVersion\": \"v4.5.0.1\""
235                 + "}}}]";
236         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessage)))
237             .expectSubscription().expectError(DmaapNotFoundException.class).verify();
238     }
239
240     @Test
241     void whenPassingJsonWithoutSerialNumberOrVendorName_validationThrowingAnException() {
242         String parsed = "{\"event\": {\"pnfRegistrationFields\": {"
243                 + " \"unitType\": \"AirScale\","
244                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
245                 + " \"manufactureDate\": \"1535014037024\","
246                 + " \"modelNumber\": \"7BEA\",\n"
247                 + " \"lastServiceDate\": \"1535014037024\","
248                 + " \"unitFamily\": \"BBU\","
249                 + " \"oamV4IpAddress\": \"10.16.123.234\","
250                 + " \"softwareVersion\": \"v4.5.0.1\","
251                 + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
252                 + "}}}";
253
254         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
255         JsonElement jsonElement = new JsonParser().parse(parsed);
256         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
257             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
258         String jsonWithoutVendorAndSerialNumber =
259                 "[{\"event\": {\"pnfRegistrationFields\": {"
260                         + " \"unitType\": \"AirScale\","
261                         + " \"pnfRegistrationFieldsVersion\": \"2.0\","
262                         + " \"manufactureDate\": \"1535014037024\","
263                         + " \"modelNumber\": \"7BEA\",\n"
264                         + " \"lastServiceDate\": \"1535014037024\","
265                         + " \"unitFamily\": \"BBU\","
266                         + " \"oamV4IpAddress\": \"10.16.123.234\","
267                         + " \"softwareVersion\": \"v4.5.0.1\","
268                         + " \"oamV6IpAddress\": \"0:0:0:0:0:FFFF:0A10:7BEA\""
269                         + "}}}]";
270         StepVerifier
271             .create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutVendorAndSerialNumber)))
272             .expectSubscription().expectError(DmaapNotFoundException.class).verify();
273     }
274
275     @Test
276     void whenPassingJsonWithoutIpInformation_validationThrowingAnException() {
277         String parsed =
278                 "{\"event\": {\"pnfRegistrationFields\": {"
279                         + " \"unitType\": \"AirScale\","
280                         + " \"serialNumber\": \"QTFCOC540002E\","
281                         + " \"pnfRegistrationFieldsVersion\": \"2.0\","
282                         + " \"manufactureDate\": \"1535014037024\","
283                         + " \"modelNumber\": \"7BEA\",\n"
284                         + " \"lastServiceDate\": \"1535014037024\","
285                         + " \"unitFamily\": \"BBU\","
286                         + " \"vendorName\": \"Nokia\","
287                         + " \"softwareVersion\": \"v4.5.0.1\""
288                         + "}}}";
289
290         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
291         JsonElement jsonElement = new JsonParser().parse(parsed);
292         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
293             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
294         String jsonWithoutIpInformation =
295                 "[{\"event\": {\"pnfRegistrationFields\": {"
296                         + " \"unitType\": \"AirScale\","
297                         + " \"serialNumber\": \"QTFCOC540002E\","
298                         + " \"pnfRegistrationFieldsVersion\": \"2.0\","
299                         + " \"manufactureDate\": \"1535014037024\","
300                         + " \"modelNumber\": \"7BEA\",\n"
301                         + " \"lastServiceDate\": \"1535014037024\","
302                         + " \"unitFamily\": \"BBU\","
303                         + " \"vendorName\": \"Nokia\","
304                         + " \"softwareVersion\": \"v4.5.0.1\""
305                         + "}}}]";
306         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutIpInformation)))
307             .expectSubscription().expectError(DmaapNotFoundException.class).verify();
308     }
309 }