8cf7bcec8c8b1bff184a0a5acece338a625d9114
[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
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\""
59                 + "}}]";
60
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\""
74                 + "}}";
75
76         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234")
77             .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
78             .pnfName("NOKQTFCOC540002E").build();
79         //when
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();
86         //then
87         Assertions.assertNotNull(consumerDmaapModel);
88         Assertions.assertEquals(expectedObject, consumerDmaapModel);
89     }
90
91     @Test
92     void whenPassingCorrectJsonWithoutIpv4_validationNotThrowingAnException() {
93         //given
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\""
104                 +  "}}]";
105
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\""
118                 +  "}}";
119
120         //when
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)))
127             .block();
128         //then
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);
134     }
135
136     @Test
137     void whenPassingCorrectJsonWithoutIpv6_validationNotThrowingAnException() {
138         //given
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\""
151                         +  "}}]";
152
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\""
165                         +  "}}";
166
167         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234").ipv6("")
168             .pnfName("NOKQTFCOC540002E").build();
169         //when
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)))
175             .block();
176         //then
177         Assertions.assertNotNull(consumerDmaapModel);
178         Assertions.assertEquals(expectedObject, consumerDmaapModel);
179     }
180
181     @Test
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\""
194                         + "}}]";
195
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\""
207                         + "}}";
208
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();
215
216     }
217
218     @Test
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\""
230                 + "}}";
231
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\""
246                 +  "}}]";
247         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessage)))
248             .expectSubscription().expectError(DmaapNotFoundException.class).verify();
249     }
250
251     @Test
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\""
264                         + "}}";
265
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\""
282                         + "}}]";
283         StepVerifier
284             .create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutVendorAndSerialNumber)))
285             .expectSubscription().expectError(DmaapNotFoundException.class).verify();
286     }
287
288     @Test
289     void whenPassingJsonWithoutIpInformation_validationThrowingAnException() {
290         String parsed =
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\""
302                         + "}}";
303
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\""
320                         + "}}]";
321         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutIpInformation)))
322             .expectSubscription().expectError(DmaapNotFoundException.class).verify();
323     }
324 }