0036961f8eb922f5bed718e8121f88e657e854b0
[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.JsonObject;
27 import com.google.gson.JsonParser;
28 import java.util.Optional;
29 import org.junit.jupiter.api.Assertions;
30 import org.junit.jupiter.api.Test;
31 import org.mockito.Mockito;
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\": { "
47                 + " \"sourceName\":\"NOKQTFCOC540002E\","
48                 + " \"nfNamingCode\":\"gNB\" "
49                 + "},"
50                 + "\"pnfRegistrationFields\": {"
51                 + " \"vendorName\": \"nokia\","
52                 + " \"serialNumber\": \"QTFCOC540002E\","
53                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
54                 + " \"modelNumber\": \"3310\","
55                 + " \"unitType\": \"type\",\n"
56                 + " \"unitFamily\": \"BBU\","
57                 + " \"softwareVersion\": \"v4.5.0.1\","
58                 + " \"additionalFields\": {\"attachmentPoint\":\"bla-bla-30-3\",\"cvlan\":\"678\",\"svlan\":\"1005\"}"
59                 + "}}}]";
60
61         String parsed = "{\"event\": {"
62                 + "\"commonEventHeader\": { "
63                 + " \"sourceName\":\"NOKQTFCOC540002E\","
64                 + " \"nfNamingCode\":\"gNB\" "
65                 + "},"
66                 + "\"pnfRegistrationFields\": {"
67                 + " \"vendorName\": \"nokia\","
68                 + " \"serialNumber\": \"QTFCOC540002E\","
69                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
70                 + " \"modelNumber\": \"3310\","
71                 + " \"unitType\": \"type\",\n"
72                 + " \"unitFamily\": \"BBU\","
73                 + " \"softwareVersion\": \"v4.5.0.1\","
74                 + " \"additionalFields\": {\"attachmentPoint\":\"bla-bla-30-3\",\"cvlan\":\"678\",\"svlan\":\"1005\"}"
75                 + "}}}";
76
77         JsonObject jsonObject = new JsonParser().parse("{\n"
78             + "        \"attachmentPoint\": \"bla-bla-30-3\",\n"
79             + "        \"cvlan\": \"678\",\n"
80             + "        \"svlan\": \"1005\"\n"
81             + "      }").getAsJsonObject();
82
83         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
84                 .correlationId("NOKQTFCOC540002E")
85                 .serialNumber("QTFCOC540002E")
86                 .equipVendor("nokia")
87                 .equipModel("3310")
88                 .equipType("type")
89                 .nfRole("gNB")
90                 .swVersion("v4.5.0.1")
91                 .additionalFields(jsonObject)
92                 .build();
93         //when
94         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
95         JsonElement jsonElement = new JsonParser().parse(parsed);
96         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
97             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
98         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser
99             .getJsonObject(Mono.just((message))).blockFirst();
100         //then
101         Assertions.assertNotNull(consumerDmaapModel);
102         Assertions.assertEquals(expectedObject, consumerDmaapModel);
103     }
104
105     @Test
106     void whenPassingJsonWithoutAdditionalFields_validationNotThrowingAnException() {
107         //given
108         String message = "[{\"event\": {"
109             + "\"commonEventHeader\": { "
110             + " \"sourceName\":\"NOKQTFCOC540002E\","
111             + " \"nfNamingCode\":\"gNB\" "
112             + "},"
113             + "\"pnfRegistrationFields\": {"
114             + " \"vendorName\": \"nokia\","
115             + " \"serialNumber\": \"QTFCOC540002E\","
116             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
117             + " \"modelNumber\": \"3310\","
118             + " \"unitType\": \"type\",\n"
119             + " \"unitFamily\": \"BBU\","
120             + " \"softwareVersion\": \"v4.5.0.1\""
121             + "}}}]";
122
123         String parsed = "{\"event\": {"
124             + "\"commonEventHeader\": { "
125             + " \"sourceName\":\"NOKQTFCOC540002E\","
126             + " \"nfNamingCode\":\"gNB\" "
127             + "},"
128             + "\"pnfRegistrationFields\": {"
129             + " \"vendorName\": \"nokia\","
130             + " \"serialNumber\": \"QTFCOC540002E\","
131             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
132             + " \"modelNumber\": \"3310\","
133             + " \"unitType\": \"type\",\n"
134             + " \"unitFamily\": \"BBU\","
135             + " \"softwareVersion\": \"v4.5.0.1\""
136             + "}}}";
137
138         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
139             .correlationId("NOKQTFCOC540002E")
140             .serialNumber("QTFCOC540002E")
141             .equipVendor("nokia")
142             .equipModel("3310")
143             .equipType("type")
144             .nfRole("gNB")
145             .swVersion("v4.5.0.1")
146             .additionalFields(new JsonObject())
147             .build();
148         //when
149         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
150         JsonElement jsonElement = new JsonParser().parse(parsed);
151         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
152             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
153         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser
154             .getJsonObject(Mono.just((message))).blockFirst();
155         //then
156         Assertions.assertNotNull(consumerDmaapModel);
157         Assertions.assertEquals(expectedObject, consumerDmaapModel);
158     }
159
160     @Test
161     void whenPassingJsonWithEmptyAdditionalFields_validationNotThrowingAnException() {
162         //given
163         String message = "[{\"event\": {"
164             + "\"commonEventHeader\": { "
165             + " \"sourceName\":\"NOKQTFCOC540002E\","
166             + " \"nfNamingCode\":\"gNB\" "
167             + "},"
168             + "\"pnfRegistrationFields\": {"
169             + " \"vendorName\": \"nokia\","
170             + " \"serialNumber\": \"QTFCOC540002E\","
171             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
172             + " \"modelNumber\": \"3310\","
173             + " \"unitType\": \"type\",\n"
174             + " \"unitFamily\": \"BBU\","
175             + " \"softwareVersion\": \"v4.5.0.1\","
176             + " \"additionalFields\": {}"
177             + "}}}]";
178
179         String parsed = "{\"event\": {"
180             + "\"commonEventHeader\": { "
181             + " \"sourceName\":\"NOKQTFCOC540002E\","
182             + " \"nfNamingCode\":\"gNB\" "
183             + "},"
184             + "\"pnfRegistrationFields\": {"
185             + " \"vendorName\": \"nokia\","
186             + " \"serialNumber\": \"QTFCOC540002E\","
187             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
188             + " \"modelNumber\": \"3310\","
189             + " \"unitType\": \"type\",\n"
190             + " \"unitFamily\": \"BBU\","
191             + " \"softwareVersion\": \"v4.5.0.1\","
192             + " \"additionalFields\": {}"
193             + "}}}";
194
195         ConsumerDmaapModel expectedObject = ImmutableConsumerDmaapModel.builder()
196             .correlationId("NOKQTFCOC540002E")
197             .serialNumber("QTFCOC540002E")
198             .equipVendor("nokia")
199             .equipModel("3310")
200             .equipType("type")
201             .nfRole("gNB")
202             .swVersion("v4.5.0.1")
203             .additionalFields(new JsonObject())
204             .build();
205         //when
206         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
207         JsonElement jsonElement = new JsonParser().parse(parsed);
208         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
209             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
210         ConsumerDmaapModel consumerDmaapModel = dmaapConsumerJsonParser
211             .getJsonObject(Mono.just((message))).blockFirst();
212         //then
213         Assertions.assertNotNull(consumerDmaapModel);
214         Assertions.assertEquals(expectedObject, consumerDmaapModel);
215     }
216
217     @Test
218     void whenPassingJsonWithoutMandatoryHeaderInformation_validationAddingAnException() {
219         String parsed = "{\"event\": {"
220             + "\"commonEventHeader\": {},"
221             + "\"pnfRegistrationFields\": {"
222             + " \"unitType\": \"AirScale\","
223             + " \"serialNumber\": \"QTFCOC540002E\","
224             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
225             + " \"manufactureDate\": \"1535014037024\","
226             + " \"modelNumber\": \"7BEA\",\n"
227             + " \"lastServiceDate\": \"1535014037024\","
228             + " \"unitFamily\": \"BBU\","
229             + " \"vendorName\": \"Nokia\","
230             + " \"softwareVersion\": \"v4.5.0.1\","
231             + " \"additionalFields\": {}"
232             + "}}}";
233
234         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
235         JsonElement jsonElement = new JsonParser().parse(parsed);
236         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
237             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
238         String incorrectMessage = "[{\"event\": {"
239             + "\"commonEventHeader\": {},"
240             + "\"pnfRegistrationFields\": {"
241             + " \"unitType\": \"AirScale\","
242             + " \"serialNumber\": \"QTFCOC540002E\","
243             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
244             + " \"manufactureDate\": \"1535014037024\","
245             + " \"modelNumber\": \"7BEA\",\n"
246             + " \"lastServiceDate\": \"1535014037024\","
247             + " \"unitFamily\": \"BBU\","
248             + " \"vendorName\": \"Nokia\","
249             + " \"softwareVersion\": \"v4.5.0.1\","
250             + " \"additionalFields\": {}"
251             + "}}}]";
252         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessage)))
253             .expectSubscription().thenRequest(1).verifyComplete();
254     }
255
256     @Test
257     void whenPassingJsonWithoutSourceName_validationAddingAnException() {
258         String parsed = "{\"event\": {"
259             + "\"commonEventHeader\": {},"
260             + "\"pnfRegistrationFields\": {"
261             + " \"unitType\": \"AirScale\","
262             + " \"serialNumber\": \"QTFCOC540002E\","
263             + " \"pnfRegistrationFieldsVersion\": \"2.0\","
264             + " \"manufactureDate\": \"1535014037024\","
265             + " \"modelNumber\": \"7BEA\",\n"
266             + " \"lastServiceDate\": \"1535014037024\","
267             + " \"unitFamily\": \"BBU\","
268             + " \"vendorName\": \"Nokia\","
269             + " \"softwareVersion\": \"v4.5.0.1\","
270             + " \"additionalFields\": {}"
271             + "}}}";
272
273         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
274         JsonElement jsonElement = new JsonParser().parse(parsed);
275         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
276             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
277         String jsonWithoutSourceName =
278             "[{\"event\": {"
279                 + "\"commonEventHeader\": {},"
280                 + "\"pnfRegistrationFields\": {"
281                 + " \"unitType\": \"AirScale\","
282                 + " \"serialNumber\": \"QTFCOC540002E\","
283                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
284                 + " \"manufactureDate\": \"1535014037024\","
285                 + " \"modelNumber\": \"7BEA\",\n"
286                 + " \"lastServiceDate\": \"1535014037024\","
287                 + " \"unitFamily\": \"BBU\","
288                 + " \"vendorName\": \"Nokia\","
289                 + " \"softwareVersion\": \"v4.5.0.1\","
290                 + " \"additionalFields\": {}"
291                 + "}}}]";
292         StepVerifier
293             .create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutSourceName)))
294             .expectSubscription().thenRequest(1)
295             .verifyComplete();
296     }
297
298     @Test
299     void whenPassingJsonWithoutSourceNameValue_validationAddingAnException() {
300         String parsed =
301             "{\"event\": {"
302                 + "\"commonEventHeader\": {\"sourceName\": \"\"},"
303                 + "\"pnfRegistrationFields\": {"
304                 + " \"unitType\": \"AirScale\","
305                 + " \"serialNumber\": \"QTFCOC540002E\","
306                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
307                 + " \"manufactureDate\": \"1535014037024\","
308                 + " \"modelNumber\": \"7BEA\",\n"
309                 + " \"lastServiceDate\": \"1535014037024\","
310                 + " \"unitFamily\": \"BBU\","
311                 + " \"vendorName\": \"Nokia\","
312                 + " \"softwareVersion\": \"v4.5.0.1\","
313                 + " \"additionalFields\": {}"
314                 + "}}}";
315
316         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
317         JsonElement jsonElement = new JsonParser().parse(parsed);
318         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
319             .when(dmaapConsumerJsonParser).getJsonObjectFromAnArray(jsonElement);
320         String jsonWithoutIpInformation =
321             "[{\"event\": {"
322                 + "\"commonEventHeader\": {\"sourceName\": \"\"},"
323                 + "\"pnfRegistrationFields\": {"
324                 + " \"unitType\": \"AirScale\","
325                 + " \"serialNumber\": \"QTFCOC540002E\","
326                 + " \"pnfRegistrationFieldsVersion\": \"2.0\","
327                 + " \"manufactureDate\": \"1535014037024\","
328                 + " \"modelNumber\": \"7BEA\",\n"
329                 + " \"lastServiceDate\": \"1535014037024\","
330                 + " \"unitFamily\": \"BBU\","
331                 + " \"vendorName\": \"Nokia\","
332                 + " \"softwareVersion\": \"v4.5.0.1\","
333                 + " \"additionalFields\": {}"
334                 + "}}}]";
335         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(jsonWithoutIpInformation)))
336             .expectSubscription().thenRequest(1).verifyComplete();
337     }
338 }