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.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;
38 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/8/18
40 class DmaapConsumerJsonParserTest {
43 void whenPassingCorrectJson_validationNotThrowingAnException() {
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\""
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\""
73 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234")
74 .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
75 .pnfName("NOKQTFCOC540002E").build();
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();
84 Assertions.assertNotNull(consumerDmaapModel);
85 Assertions.assertEquals(expectedObject, consumerDmaapModel);
89 void whenPassingCorrectJsonWithoutIpv4_validationNotThrowingAnException() {
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\""
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\""
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)))
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);
134 void whenPassingCorrectJsonWithoutIpv6_validationNotThrowingAnException() {
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\""
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\""
162 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234").ipv6("")
163 .pnfName("NOKQTFCOC540002E").build();
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)))
172 Assertions.assertNotNull(consumerDmaapModel);
173 Assertions.assertEquals(expectedObject, consumerDmaapModel);
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\""
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\""
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();
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\""
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\""
236 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessage)))
237 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
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\""
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\""
271 .create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutVendorAndSerialNumber)))
272 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
276 void whenPassingJsonWithoutIpInformation_validationThrowingAnException() {
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\""
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\""
306 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutIpInformation)))
307 .expectSubscription().expectError(DmaapNotFoundException.class).verify();