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() {
46 String message = "[{\"pnfRegistrationFields\":{"
47 + "\"lastServiceDate\":\"1517206400\","
48 + "\"macAddress\":\"FFFF:0A10:7BEA\","
49 + "\"manufactureDate\":\"1517206400\","
50 + "\"modelNumber\":\"7BEA\","
51 + "\"oamV4IpAddress\":\"10.16.123.234\","
52 + "\"oamV6IpAddress\":\"0:0:0:0:0:FFFF:0A10:7BEA\","
53 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
54 + "\"serialNumber\":\"QTFCOC540002E\","
55 + "\"softwareVersion\":\"v4.5.0.1\","
56 + "\"unitFamily\":\"BBU\","
57 + "\"unitType\":\"AirScale\","
58 + "\"vendorName\":\"Nokia\""
61 String parsed = "{\"pnfRegistrationFields\":{"
62 + "\"lastServiceDate\":\"1517206400\","
63 + "\"macAddress\":\"FFFF:0A10:7BEA\","
64 + "\"manufactureDate\":\"1517206400\","
65 + "\"modelNumber\":\"7BEA\","
66 + "\"oamV4IpAddress\":\"10.16.123.234\","
67 + "\"oamV6IpAddress\":\"0:0:0:0:0:FFFF:0A10:7BEA\","
68 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
69 + "\"serialNumber\":\"QTFCOC540002E\","
70 + "\"softwareVersion\":\"v4.5.0.1\","
71 + "\"unitFamily\":\"BBU\","
72 + "\"unitType\":\"AirScale\","
73 + "\"vendorName\":\"Nokia\""
76 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234")
77 .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
78 .pnfName("NOKQTFCOC540002E").build();
80 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
81 JsonElement jsonElement = new JsonParser().parse(parsed);
82 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
83 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
84 ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser
85 .getJsonObject(Mono.just((message))).block();
87 Assertions.assertNotNull(consumerDmaapModel);
88 Assertions.assertEquals(expectedObject, consumerDmaapModel);
92 void whenPassingCorrectJsonWithoutIpv4_validationNotThrowingAnException() {
94 String message = "[{\"pnfRegistrationFields\":{"
95 + "\"lastServiceDate\":\"1517206400\","
96 + "\"macAddress\":\"FFFF:0A10:7BEA\","
97 + "\"manufactureDate\":\"1517206400\",\"modelNumber\":\"7BEA\","
98 + "\"oamV6IpAddress\":\"0:0:0:0:0:FFFF:0A10:7BEA\","
99 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
100 + "\"serialNumber\":\"QTFCOC540002E\","
101 + "\"softwareVersion\":\"1517206400\","
102 + "\"unitFamily\":\"BBU\","
103 + "\"unitType\":\"AirScale\",\"vendorName\":\"Nokia\""
106 String parsed = "{\"pnfRegistrationFields\":{"
107 + "\"lastServiceDate\":\"1517206400\","
108 + "\"macAddress\":\"FFFF:0A10:7BEA\","
109 + "\"manufactureDate\":\"1517206400\","
110 + "\"modelNumber\":\"7BEA\","
111 + "\"oamV6IpAddress\":\"0:0:0:0:0:FFFF:0A10:7BEA\","
112 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
113 + "\"serialNumber\":\"QTFCOC540002E\","
114 + "\"softwareVersion\":\"1517206400\","
115 + "\"unitFamily\":\"BBU\","
116 + "\"unitType\":\"AirScale\","
117 + "\"vendorName\":\"Nokia\""
121 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
122 JsonElement jsonElement = new JsonParser().parse(parsed);
123 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
124 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
125 dmaapConsumerJsonParser.getJsonObject(Mono.just((message)));
126 ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser.getJsonObject(Mono.just((message)))
129 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("")
130 .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
131 .pnfName("NOKQTFCOC540002E").build();
132 Assertions.assertNotNull(consumerDmaapModel);
133 Assertions.assertEquals(expectedObject, consumerDmaapModel);
137 void whenPassingCorrectJsonWithoutIpv6_validationNotThrowingAnException() {
139 String message = "[{\"pnfRegistrationFields\":{"
140 + "\"lastServiceDate\":\"1517206400\","
141 + "\"macAddress\":\"FFFF:0A10:7BEA\","
142 + "\"manufactureDate\":\"1517206400\","
143 + "\"modelNumber\":\"7BEA\","
144 + "\"oamV4IpAddress\":\"10.16.123.234\","
145 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
146 + "\"serialNumber\":\"QTFCOC540002E\","
147 + "\"softwareVersion\":\"1517206400\","
148 + "\"unitFamily\":\"BBU\","
149 + "\"unitType\":\"AirScale\","
150 + "\"vendorName\":\"Nokia\""
153 String parsed = "{\"pnfRegistrationFields\":{"
154 + "\"lastServiceDate\":\"1517206400\","
155 + "\"macAddress\":\"FFFF:0A10:7BEA\","
156 + "\"manufactureDate\":\"1517206400\","
157 + "\"modelNumber\":\"7BEA\","
158 + "\"oamV4IpAddress\":\"10.16.123.234\","
159 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
160 + "\"serialNumber\":\"QTFCOC540002E\","
161 + "\"softwareVersion\":\"1517206400\","
162 + "\"unitFamily\":\"BBU\","
163 + "\"unitType\":\"AirScale\","
164 + "\"vendorName\":\"Nokia\""
167 ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234").ipv6("")
168 .pnfName("NOKQTFCOC540002E").build();
170 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
171 JsonElement jsonElement = new JsonParser().parse(parsed);
172 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
173 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
174 ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser.getJsonObject(Mono.just((message)))
177 Assertions.assertNotNull(consumerDmaapModel);
178 Assertions.assertEquals(expectedObject, consumerDmaapModel);
182 void whenPassingCorrectJsonWithoutIpv4andIpv6_validationThrowingAnException() {
183 String message = "[{\"pnfRegistrationFields\":{"
184 + "\"lastServiceDate\":\"1517206400\","
185 + "\"macAddress\":\"FFFF:0A10:7BEA\","
186 + "\"manufactureDate\":\"1517206400\","
187 + "\"modelNumber\":\"7BEA\","
188 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
189 + "\"serialNumber\":\"QTFCOC540002E\","
190 + "\"softwareVersion\":\"1517206400\","
191 + "\"unitFamily\":\"BBU\","
192 + "\"unitType\":\"AirScale\","
193 + "\"vendorName\":\"Nokia\""
196 String parsed = "{\"pnfRegistrationFields\":{"
197 + "\"lastServiceDate\":\"1517206400\","
198 + "\"macAddress\":\"FFFF:0A10:7BEA\","
199 + "\"manufactureDate\":\"1517206400\","
200 + "\"modelNumber\":\"7BEA\","
201 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
202 + "\"serialNumber\":\"QTFCOC540002E\","
203 + "\"softwareVersion\":\"1517206400\","
204 + "\"unitFamily\":\"BBU\","
205 + "\"unitType\":\"AirScale\","
206 + "\"vendorName\":\"Nokia\""
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 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(message)))
214 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
219 void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() {
220 String parsed = "{\"pnfRegistrationFields\":{"
221 + "\"lastServiceDate\":\"1517206400\","
222 + "\"macAddress\":\"FFFF:0A10:7BEA\","
223 + "\"manufactureDate\":\"1517206400\","
224 + "\"modelNumber\":\"7BEA\","
225 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
226 + "\"serialNumber\":\"QTFCOC540002E\","
227 + "\"softwareVersion\":\"1517206400\","
228 + "\"unitFamily\":\"BBU\","
229 + "\"unitType\":\"AirScale\""
232 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
233 JsonElement jsonElement = new JsonParser().parse(parsed);
234 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
235 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
236 String incorrectMessage = "[{\"pnfRegistrationFields\":{"
237 + "\"lastServiceDate\":\"1517206400\","
238 + "\"macAddress\":\"FFFF:0A10:7BEA\","
239 + "\"manufactureDate\":\"1517206400\","
240 + "\"modelNumber\":\"7BEA\","
241 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
242 + "\"serialNumber\":\"QTFCOC540002E\","
243 + "\"softwareVersion\":\"1517206400\","
244 + "\"unitFamily\":\"BBU\","
245 + "\"unitType\":\"AirScale\""
247 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessage)))
248 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
252 void whenPassingJsonWithoutSerialNumberOrVendorName_validationThrowingAnException() {
253 String parsed = "{\"pnfRegistrationFields\":{"
254 + "\"lastServiceDate\":\"1517206400\","
255 + "\"macAddress\":\"FFFF:0A10:7BEA\","
256 + "\"manufactureDate\":\"1517206400\","
257 + "\"modelNumber\":\"7BEA\","
258 + "\"oamV4IpAddress\":\"10.16.123.234\","
259 + "\"oamV6IpAddress\":\"0:0:0:0:0:FFFF:0A10:7BEA\","
260 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
261 + "\"softwareVersion\":\"v4.5.0.1\","
262 + "\"unitFamily\":\"BBU\","
263 + "\"unitType\":\"AirScale\""
266 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
267 JsonElement jsonElement = new JsonParser().parse(parsed);
268 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
269 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
270 String jsonWithoutVendorAndSerialNumber =
271 "[{\"pnfRegistrationFields\":{"
272 + "\"lastServiceDate\":\"1517206400\","
273 + "\"macAddress\":\"FFFF:0A10:7BEA\","
274 + "\"manufactureDate\":\"1517206400\","
275 + "\"modelNumber\":\"7BEA\","
276 + "\"oamV4IpAddress\":\"10.16.123.234\","
277 + "\"oamV6IpAddress\":\"0:0:0:0:0:FFFF:0A10:7BEA\","
278 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
279 + "\"softwareVersion\":\"v4.5.0.1\","
280 + "\"unitFamily\":\"BBU\","
281 + "\"unitType\":\"AirScale\""
284 .create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutVendorAndSerialNumber)))
285 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
289 void whenPassingJsonWithoutIpInformation_validationThrowingAnException() {
291 "{\"pnfRegistrationFields\":{"
292 + "\"lastServiceDate\":\"1517206400\","
293 + "\"macAddress\":\"FFFF:0A10:7BEA\","
294 + "\"manufactureDate\":\"1517206400\","
295 + "\"modelNumber\":\"7BEA\","
296 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
297 + "\"serialNumber\":\"QTFCOC540002E\","
298 + "\"softwareVersion\":\"v4.5.0.1\","
299 + "\"unitFamily\":\"BBU\","
300 + "\"unitType\":\"AirScale\","
301 + "\"vendorName\":\"Nokia\""
304 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
305 JsonElement jsonElement = new JsonParser().parse(parsed);
306 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
307 .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
308 String jsonWithoutIpInformation =
309 "[{\"pnfRegistrationFields\":{"
310 + "\"lastServiceDate\":\"1517206400\","
311 + "\"macAddress\":\"FFFF:0A10:7BEA\","
312 + "\"manufactureDate\":\"1517206400\","
313 + "\"modelNumber\":\"7BEA\","
314 + "\"pnfRegistrationFieldsVersion\":\"1517206400\","
315 + "\"serialNumber\":\"QTFCOC540002E\","
316 + "\"softwareVersion\":\"v4.5.0.1\","
317 + "\"unitFamily\":\"BBU\","
318 + "\"unitType\":\"AirScale\","
319 + "\"vendorName\":\"Nokia\""
321 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutIpInformation)))
322 .expectSubscription().expectError(DmaapNotFoundException.class).verify();