a9bc546f96e2c2603c52438f50a79c9676509eb2
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.service;
18
19 import static org.mockito.Mockito.spy;
20
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonParser;
23
24 import java.util.Optional;
25
26 import org.junit.jupiter.api.Test;
27 import org.mockito.Mockito;
28 import org.onap.dcaegen2.collectors.datafile.exceptions.DmaapNotFoundException;
29 import org.onap.dcaegen2.collectors.datafile.model.FileData;
30 import org.onap.dcaegen2.collectors.datafile.model.FileMetaData;
31 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData;
32 import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileMetaData;
33 import org.onap.dcaegen2.collectors.datafile.utils.JsonMessage;
34 import org.onap.dcaegen2.collectors.datafile.utils.JsonMessage.AdditionalField;
35
36 import reactor.core.publisher.Mono;
37 import reactor.test.StepVerifier;
38
39 /**
40  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/8/18
41  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
42  */
43 class DmaapConsumerJsonParserTest {
44     private static final String NR_RADIO_ERICSSON_EVENT_NAME = "Noti_NrRadio-Ericsson_FileReady";
45     private static final String PRODUCT_NAME = "NrRadio";
46     private static final String VENDOR_NAME = "Ericsson";
47     private static final String LAST_EPOCH_MICROSEC = "1519837825682";
48     private static final String SOURCE_NAME = "5GRAN_DU";
49     private static final String START_EPOCH_MICROSEC = "1519837825682";
50     private static final String TIME_ZONE_OFFSET = "UTC+05:00";
51     private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
52     private static final String LOCATION = "ftpes://192.168.0.101:22/ftp/rop/" + PM_FILE_NAME;
53     private static final String GZIP_COMPRESSION = "gzip";
54     private static final String FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
55     private static final String FILE_FORMAT_VERSION = "V10";
56     private static final String CHANGE_IDENTIFIER = "PM_MEAS_FILES";
57     private static final String INCORRECT_CHANGE_IDENTIFIER = "INCORRECT_PM_MEAS_FILES";
58     private static final String CHANGE_TYPE = "FileReady";
59     private static final String INCORRECT_CHANGE_TYPE = "IncorrectFileReady";
60     private static final String NOTIFICATION_FIELDS_VERSION = "1.0";
61
62     @Test
63     void whenPassingCorrectJson_validationNotThrowingAnException() throws DmaapNotFoundException {
64         // @formatter:off
65         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
66                 .name(PM_FILE_NAME)
67                 .location(LOCATION)
68                 .compression(GZIP_COMPRESSION)
69                 .fileFormatType(FILE_FORMAT_TYPE)
70                 .fileFormatVersion(FILE_FORMAT_VERSION)
71                 .build();
72         JsonMessage message = new JsonMessage.JsonMessageBuilder()
73                 .eventName(NR_RADIO_ERICSSON_EVENT_NAME)
74                 .changeIdentifier(CHANGE_IDENTIFIER)
75                 .changeType(CHANGE_TYPE)
76                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
77                 .addAdditionalField(additionalField)
78                 .build();
79
80         FileMetaData fileMetaData = ImmutableFileMetaData.builder()
81                 .productName(PRODUCT_NAME)
82                 .vendorName(VENDOR_NAME)
83                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
84                 .sourceName(SOURCE_NAME)
85                 .startEpochMicrosec(START_EPOCH_MICROSEC)
86                 .timeZoneOffset(TIME_ZONE_OFFSET)
87                 .changeIdentifier(CHANGE_IDENTIFIER)
88                 .changeType(CHANGE_TYPE)
89                 .build();
90         FileData expectedFileData = ImmutableFileData.builder()
91                 .fileMetaData(fileMetaData)
92                 .name(PM_FILE_NAME)
93                 .location(LOCATION)
94                 .compression(GZIP_COMPRESSION)
95                 .fileFormatType(FILE_FORMAT_TYPE)
96                 .fileFormatVersion(FILE_FORMAT_VERSION)
97                 .build();
98         // @formatter:on
99         String messageString = message.toString();
100         String parsedString = message.getParsed();
101         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
102         JsonElement jsonElement = new JsonParser().parse(parsedString);
103         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
104                 .getJsonObjectFromAnArray(jsonElement);
105
106         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
107                 .expectNext(expectedFileData).verifyComplete();
108     }
109
110     @Test
111     void whenPassingCorrectJsonWithFaultyEventName_validationThrowingAnException() {
112         // @formatter:off
113         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
114                 .location(LOCATION)
115                 .compression(GZIP_COMPRESSION)
116                 .fileFormatType(FILE_FORMAT_TYPE)
117                 .fileFormatVersion(FILE_FORMAT_VERSION)
118                 .build();
119         JsonMessage message = new JsonMessage.JsonMessageBuilder()
120                 .eventName("Faulty event name")
121                 .changeIdentifier(CHANGE_IDENTIFIER)
122                 .changeType(CHANGE_TYPE)
123                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
124                 .addAdditionalField(additionalField)
125                 .build();
126         // @formatter:on
127         String messageString = message.toString();
128         String parsedString = message.getParsed();
129         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
130         JsonElement jsonElement = new JsonParser().parse(parsedString);
131         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
132                 .getJsonObjectFromAnArray(jsonElement);
133
134         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
135                 .expectError(DmaapNotFoundException.class).verify();
136     }
137
138     @Test
139     void whenPassingCorrectJsonWithoutName_noFileData() {
140         // @formatter:off
141         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
142                 .location(LOCATION)
143                 .compression(GZIP_COMPRESSION)
144                 .fileFormatType(FILE_FORMAT_TYPE)
145                 .fileFormatVersion(FILE_FORMAT_VERSION)
146                 .build();
147         JsonMessage message = new JsonMessage.JsonMessageBuilder()
148                 .eventName(NR_RADIO_ERICSSON_EVENT_NAME)
149                 .changeIdentifier(CHANGE_IDENTIFIER)
150                 .changeType(CHANGE_TYPE)
151                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
152                 .addAdditionalField(additionalField)
153                 .build();
154         // @formatter:on
155         String messageString = message.toString();
156         String parsedString = message.getParsed();
157         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
158         JsonElement jsonElement = new JsonParser().parse(parsedString);
159         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
160                 .getJsonObjectFromAnArray(jsonElement);
161
162         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
163                 .expectNextCount(0).verifyComplete();
164     }
165
166     @Test
167     void whenPassingCorrectJsonWithoutLocation_noFileData() {
168         // @formatter:off
169         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
170                 .name(PM_FILE_NAME)
171                 .compression(GZIP_COMPRESSION)
172                 .fileFormatType(FILE_FORMAT_TYPE)
173                 .fileFormatVersion(FILE_FORMAT_VERSION)
174                 .build();
175         JsonMessage message = new JsonMessage.JsonMessageBuilder()
176                 .eventName(NR_RADIO_ERICSSON_EVENT_NAME)
177                 .changeIdentifier(CHANGE_IDENTIFIER)
178                 .changeType(CHANGE_TYPE)
179                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
180                 .addAdditionalField(additionalField)
181                 .build();
182         // @formatter:on
183         String messageString = message.toString();
184         String parsedString = message.getParsed();
185         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
186         JsonElement jsonElement = new JsonParser().parse(parsedString);
187         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
188                 .getJsonObjectFromAnArray(jsonElement);
189
190         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
191                 .expectNextCount(0).verifyComplete();
192     }
193
194     @Test
195     void whenPassingCorrectJsonWithoutCompression_noFileData() {
196         // @formatter:off
197         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
198                 .name(PM_FILE_NAME)
199                 .location(LOCATION)
200                 .fileFormatType(FILE_FORMAT_TYPE)
201                 .fileFormatVersion(FILE_FORMAT_VERSION)
202                 .build();
203         JsonMessage message = new JsonMessage.JsonMessageBuilder()
204                 .eventName(NR_RADIO_ERICSSON_EVENT_NAME)
205                 .changeIdentifier(CHANGE_IDENTIFIER)
206                 .changeType(CHANGE_TYPE)
207                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
208                 .addAdditionalField(additionalField)
209                 .build();
210         // @formatter:on
211         String messageString = message.toString();
212         String parsedString = message.getParsed();
213         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
214         JsonElement jsonElement = new JsonParser().parse(parsedString);
215         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
216                 .getJsonObjectFromAnArray(jsonElement);
217
218         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
219                 .expectNextCount(0).verifyComplete();
220     }
221
222     @Test
223     void whenPassingCorrectJsonWithoutFileFormatType_noFileData() {
224         // @formatter:off
225         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
226                 .name(PM_FILE_NAME)
227                 .location(LOCATION)
228                 .compression(GZIP_COMPRESSION)
229                 .fileFormatVersion(FILE_FORMAT_VERSION)
230                 .build();
231         JsonMessage message = new JsonMessage.JsonMessageBuilder()
232                 .eventName(NR_RADIO_ERICSSON_EVENT_NAME)
233                 .changeIdentifier(CHANGE_IDENTIFIER)
234                 .changeType(CHANGE_TYPE)
235                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
236                 .addAdditionalField(additionalField)
237                 .build();
238         // @formatter:on
239         String messageString = message.toString();
240         String parsedString = message.getParsed();
241         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
242         JsonElement jsonElement = new JsonParser().parse(parsedString);
243         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
244                 .getJsonObjectFromAnArray(jsonElement);
245
246         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
247                 .expectNextCount(0).verifyComplete();
248     }
249
250     @Test
251     void whenPassingOneCorrectJsonWithoutFileFormatVersionAndOneCorrect_oneFileData() {
252         // @formatter:off
253         AdditionalField additionalFaultyField = new JsonMessage.AdditionalFieldBuilder()
254                 .name(PM_FILE_NAME)
255                 .location(LOCATION)
256                 .compression(GZIP_COMPRESSION)
257                 .fileFormatType(FILE_FORMAT_TYPE)
258                 .build();
259         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
260                 .name(PM_FILE_NAME)
261                 .location(LOCATION)
262                 .compression(GZIP_COMPRESSION)
263                 .fileFormatType(FILE_FORMAT_TYPE)
264                 .fileFormatVersion(FILE_FORMAT_VERSION)
265                 .build();
266         JsonMessage message = new JsonMessage.JsonMessageBuilder()
267                 .eventName(NR_RADIO_ERICSSON_EVENT_NAME)
268                 .changeIdentifier(CHANGE_IDENTIFIER)
269                 .changeType(CHANGE_TYPE)
270                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
271                 .addAdditionalField(additionalFaultyField)
272                 .addAdditionalField(additionalField)
273                 .build();
274
275         FileMetaData fileMetaData = ImmutableFileMetaData.builder()
276                 .productName(PRODUCT_NAME)
277                 .vendorName(VENDOR_NAME)
278                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
279                 .sourceName(SOURCE_NAME)
280                 .startEpochMicrosec(START_EPOCH_MICROSEC)
281                 .timeZoneOffset(TIME_ZONE_OFFSET)
282                 .changeIdentifier(CHANGE_IDENTIFIER)
283                 .changeType(CHANGE_TYPE)
284                 .build();
285         FileData expectedFileData = ImmutableFileData.builder()
286                 .fileMetaData(fileMetaData)
287                 .name(PM_FILE_NAME)
288                 .location(LOCATION)
289                 .compression(GZIP_COMPRESSION)
290                 .fileFormatType(FILE_FORMAT_TYPE)
291                 .fileFormatVersion(FILE_FORMAT_VERSION)
292                 .build();
293         // @formatter:on
294         String messageString = message.toString();
295         String parsedString = message.getParsed();
296         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
297         JsonElement jsonElement = new JsonParser().parse(parsedString);
298         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
299                 .getJsonObjectFromAnArray(jsonElement);
300
301         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
302                 .expectNext(expectedFileData).verifyComplete();
303     }
304
305     @Test
306     void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() {
307         // @formatter:off
308         JsonMessage message = new JsonMessage.JsonMessageBuilder()
309                 .eventName(NR_RADIO_ERICSSON_EVENT_NAME)
310                 .changeIdentifier("PM_MEAS_FILES_INVALID")
311                 .changeType("FileReady_INVALID")
312                 .notificationFieldsVersion("1.0_INVALID")
313                 .build();
314         // @formatter:on
315         String incorrectMessageString = message.toString();
316         String parsedString = message.getParsed();
317         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
318         JsonElement jsonElement = new JsonParser().parse(parsedString);
319         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
320                 .getJsonObjectFromAnArray(jsonElement);
321
322         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
323                 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
324     }
325
326     @Test
327     void whenPassingJsonWithNullJsonElement_validationThrowingAnException() {
328         JsonMessage message = new JsonMessage.JsonMessageBuilder().build();
329
330         String incorrectMessageString = message.toString();
331         String parsedString = message.getParsed();
332         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
333         JsonElement jsonElement = new JsonParser().parse(parsedString);
334
335         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
336                 .getJsonObjectFromAnArray(jsonElement);
337
338         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
339                 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
340     }
341
342     @Test
343     void whenPassingCorrectJsonWithIncorrectChangeType_validationThrowingAnException() {
344         // @formatter:off
345         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
346                 .name(PM_FILE_NAME)
347                 .location(LOCATION)
348                 .compression(GZIP_COMPRESSION)
349                 .fileFormatVersion(FILE_FORMAT_VERSION)
350                 .build();
351         JsonMessage message = new JsonMessage.JsonMessageBuilder()
352                 .eventName(NR_RADIO_ERICSSON_EVENT_NAME)
353                 .changeIdentifier(CHANGE_IDENTIFIER)
354                 .changeType(INCORRECT_CHANGE_TYPE)
355                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
356                 .addAdditionalField(additionalField)
357                 .build();
358         // @formatter:on
359         String messageString = message.toString();
360         String parsedString = message.getParsed();
361         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
362         JsonElement jsonElement = new JsonParser().parse(parsedString);
363         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
364                 .getJsonObjectFromAnArray(jsonElement);
365
366         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
367                 .expectNextCount(0).expectError(DmaapNotFoundException.class).verify();
368     }
369
370     @Test
371     void whenPassingCorrectJsonWithIncorrectChangeIdentifier_validationThrowingAnException() {
372         // @formatter:off
373         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
374                 .name(PM_FILE_NAME)
375                 .location(LOCATION)
376                 .compression(GZIP_COMPRESSION)
377                 .fileFormatVersion(FILE_FORMAT_VERSION)
378                 .build();
379         JsonMessage message = new JsonMessage.JsonMessageBuilder()
380                 .eventName(NR_RADIO_ERICSSON_EVENT_NAME)
381                 .changeIdentifier(INCORRECT_CHANGE_IDENTIFIER)
382                 .changeType(CHANGE_TYPE)
383                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
384                 .addAdditionalField(additionalField)
385                 .build();
386         // @formatter:on
387         String messageString = message.toString();
388         String parsedString = message.getParsed();
389         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
390         JsonElement jsonElement = new JsonParser().parse(parsedString);
391         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
392                 .getJsonObjectFromAnArray(jsonElement);
393
394         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
395                 .expectNextCount(0).expectError(DmaapNotFoundException.class).verify();
396     }
397 }