d5491f5efd1289c812894909b67b62bd25d1d64c
[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.ImmutableFileData;
31 import org.onap.dcaegen2.collectors.datafile.utils.JsonMessage;
32 import org.onap.dcaegen2.collectors.datafile.utils.JsonMessage.AdditionalField;
33
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  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
40  */
41 class DmaapConsumerJsonParserTest {
42     private static final String PRODUCT_NAME = "NrRadio";
43     private static final String VENDOR_NAME = "Ericsson";
44     private static final String LAST_EPOCH_MICROSEC = "1519837825682";
45     private static final String SOURCE_NAME = "5GRAN_DU";
46     private static final String START_EPOCH_MICROSEC = "1519837825682";
47     private static final String TIME_ZONE_OFFSET = "UTC+05:00";
48     private static final String PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
49     private static final String LOCATION = "ftpes://192.168.0.101:22/ftp/rop/" + PM_FILE_NAME;
50     private static final String GZIP_COMPRESSION = "gzip";
51     private static final String FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
52     private static final String FILE_FORMAT_VERSION = "V10";
53     private static final String CHANGE_IDENTIFIER = "PM_MEAS_FILES";
54     private static final String INCORRECT_CHANGE_IDENTIFIER = "INCORRECT_PM_MEAS_FILES";
55     private static final String CHANGE_TYPE = "FileReady";
56     private static final String INCORRECT_CHANGE_TYPE = "IncorrectFileReady";
57     private static final String NOTIFICATION_FIELDS_VERSION = "1.0";
58
59     @Test
60     void whenPassingCorrectJson_validationNotThrowingAnException() throws DmaapNotFoundException {
61         // @formatter:off
62         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
63                 .name(PM_FILE_NAME)
64                 .location(LOCATION)
65                 .compression(GZIP_COMPRESSION)
66                 .fileFormatType(FILE_FORMAT_TYPE)
67                 .fileFormatVersion(FILE_FORMAT_VERSION)
68                 .build();
69         JsonMessage message = new JsonMessage.JsonMessageBuilder()
70                 .changeIdentifier(CHANGE_IDENTIFIER)
71                 .changeType(CHANGE_TYPE)
72                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
73                 .addAdditionalField(additionalField)
74                 .build();
75
76         FileData expectedFileData = ImmutableFileData.builder()
77                 .productName(PRODUCT_NAME)
78                 .vendorName(VENDOR_NAME)
79                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
80                 .sourceName(SOURCE_NAME)
81                 .startEpochMicrosec(START_EPOCH_MICROSEC)
82                 .timeZoneOffset(TIME_ZONE_OFFSET)
83                 .changeIdentifier(CHANGE_IDENTIFIER)
84                 .changeType(CHANGE_TYPE)
85                 .name(PM_FILE_NAME)
86                 .location(LOCATION)
87                 .compression(GZIP_COMPRESSION)
88                 .fileFormatType(FILE_FORMAT_TYPE)
89                 .fileFormatVersion(FILE_FORMAT_VERSION)
90                 .build();
91         // @formatter:on
92         String messageString = message.toString();
93         String parsedString = message.getParsed();
94         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
95         JsonElement jsonElement = new JsonParser().parse(parsedString);
96         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
97                 .getJsonObjectFromAnArray(jsonElement);
98
99         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
100                 .expectNext(expectedFileData).verifyComplete();
101     }
102
103     @Test
104     void whenPassingCorrectJsonWithoutName_noFileData() {
105         // @formatter:off
106         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
107                 .location(LOCATION)
108                 .compression(GZIP_COMPRESSION)
109                 .fileFormatType(FILE_FORMAT_TYPE)
110                 .fileFormatVersion(FILE_FORMAT_VERSION)
111                 .build();
112         JsonMessage message = new JsonMessage.JsonMessageBuilder()
113                 .changeIdentifier(CHANGE_IDENTIFIER)
114                 .changeType(CHANGE_TYPE)
115                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
116                 .addAdditionalField(additionalField)
117                 .build();
118         // @formatter:on
119         String messageString = message.toString();
120         String parsedString = message.getParsed();
121         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
122         JsonElement jsonElement = new JsonParser().parse(parsedString);
123         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
124                 .getJsonObjectFromAnArray(jsonElement);
125
126         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
127                 .expectNextCount(0).verifyComplete();
128     }
129
130     @Test
131     void whenPassingCorrectJsonWithoutLocation_noFileData() {
132         // @formatter:off
133         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
134                 .name(PM_FILE_NAME)
135                 .compression(GZIP_COMPRESSION)
136                 .fileFormatType(FILE_FORMAT_TYPE)
137                 .fileFormatVersion(FILE_FORMAT_VERSION)
138                 .build();
139         JsonMessage message = new JsonMessage.JsonMessageBuilder()
140                 .changeIdentifier(CHANGE_IDENTIFIER)
141                 .changeType(CHANGE_TYPE)
142                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
143                 .addAdditionalField(additionalField)
144                 .build();
145         // @formatter:on
146         String messageString = message.toString();
147         String parsedString = message.getParsed();
148         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
149         JsonElement jsonElement = new JsonParser().parse(parsedString);
150         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
151                 .getJsonObjectFromAnArray(jsonElement);
152
153         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
154                 .expectNextCount(0).verifyComplete();
155     }
156
157     @Test
158     void whenPassingCorrectJsonWithoutCompression_noFileData() {
159         // @formatter:off
160         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
161                 .name(PM_FILE_NAME)
162                 .location(LOCATION)
163                 .fileFormatType(FILE_FORMAT_TYPE)
164                 .fileFormatVersion(FILE_FORMAT_VERSION)
165                 .build();
166         JsonMessage message = new JsonMessage.JsonMessageBuilder()
167                 .changeIdentifier(CHANGE_IDENTIFIER)
168                 .changeType(CHANGE_TYPE)
169                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
170                 .addAdditionalField(additionalField)
171                 .build();
172         // @formatter:on
173         String messageString = message.toString();
174         String parsedString = message.getParsed();
175         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
176         JsonElement jsonElement = new JsonParser().parse(parsedString);
177         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
178                 .getJsonObjectFromAnArray(jsonElement);
179
180         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
181                 .expectNextCount(0).verifyComplete();
182     }
183
184     @Test
185     void whenPassingCorrectJsonWithoutFileFormatType_noFileData() {
186         // @formatter:off
187         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
188                 .name(PM_FILE_NAME)
189                 .location(LOCATION)
190                 .compression(GZIP_COMPRESSION)
191                 .fileFormatVersion(FILE_FORMAT_VERSION)
192                 .build();
193         JsonMessage message = new JsonMessage.JsonMessageBuilder()
194                 .changeIdentifier(CHANGE_IDENTIFIER)
195                 .changeType(CHANGE_TYPE)
196                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
197                 .addAdditionalField(additionalField)
198                 .build();
199         // @formatter:on
200         String messageString = message.toString();
201         String parsedString = message.getParsed();
202         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
203         JsonElement jsonElement = new JsonParser().parse(parsedString);
204         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
205                 .getJsonObjectFromAnArray(jsonElement);
206
207         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
208                 .expectNextCount(0).verifyComplete();
209     }
210
211     @Test
212     void whenPassingOneCorrectJsonWithoutFileFormatVersionAndOneCorrect_oneFileData() {
213         // @formatter:off
214         AdditionalField additionalFaultyField = new JsonMessage.AdditionalFieldBuilder()
215                 .name(PM_FILE_NAME)
216                 .location(LOCATION)
217                 .compression(GZIP_COMPRESSION)
218                 .fileFormatType(FILE_FORMAT_TYPE)
219                 .build();
220         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
221                 .name(PM_FILE_NAME)
222                 .location(LOCATION)
223                 .compression(GZIP_COMPRESSION)
224                 .fileFormatType(FILE_FORMAT_TYPE)
225                 .fileFormatVersion(FILE_FORMAT_VERSION)
226                 .build();
227         JsonMessage message = new JsonMessage.JsonMessageBuilder()
228                 .changeIdentifier(CHANGE_IDENTIFIER)
229                 .changeType(CHANGE_TYPE)
230                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
231                 .addAdditionalField(additionalFaultyField)
232                 .addAdditionalField(additionalField)
233                 .build();
234
235         FileData expectedFileData = ImmutableFileData.builder()
236                 .productName(PRODUCT_NAME)
237                 .vendorName(VENDOR_NAME)
238                 .lastEpochMicrosec(LAST_EPOCH_MICROSEC)
239                 .sourceName(SOURCE_NAME)
240                 .startEpochMicrosec(START_EPOCH_MICROSEC)
241                 .timeZoneOffset(TIME_ZONE_OFFSET)
242                 .changeIdentifier(CHANGE_IDENTIFIER)
243                 .changeType(CHANGE_TYPE)
244                 .name(PM_FILE_NAME)
245                 .location(LOCATION)
246                 .compression(GZIP_COMPRESSION)
247                 .fileFormatType(FILE_FORMAT_TYPE)
248                 .fileFormatVersion(FILE_FORMAT_VERSION)
249                 .build();
250         // @formatter:on
251         String messageString = message.toString();
252         String parsedString = message.getParsed();
253         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
254         JsonElement jsonElement = new JsonParser().parse(parsedString);
255         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
256                 .getJsonObjectFromAnArray(jsonElement);
257
258         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
259                 .expectNext(expectedFileData).verifyComplete();
260     }
261
262     @Test
263     void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() {
264         // @formatter:off
265         JsonMessage message = new JsonMessage.JsonMessageBuilder()
266                 .changeIdentifier("PM_MEAS_FILES_INVALID")
267                 .changeType("FileReady_INVALID")
268                 .notificationFieldsVersion("1.0_INVALID")
269                 .build();
270         // @formatter:on
271         String incorrectMessageString = message.toString();
272         String parsedString = message.getParsed();
273         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
274         JsonElement jsonElement = new JsonParser().parse(parsedString);
275         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
276                 .getJsonObjectFromAnArray(jsonElement);
277
278         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
279                 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
280     }
281
282     @Test
283     void whenPassingJsonWithNullJsonElement_validationThrowingAnException() {
284         JsonMessage message = new JsonMessage.JsonMessageBuilder().build();
285
286         String incorrectMessageString = message.toString();
287         String parsedString = message.getParsed();
288         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
289         JsonElement jsonElement = new JsonParser().parse(parsedString);
290
291         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
292                 .getJsonObjectFromAnArray(jsonElement);
293
294         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
295                 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
296     }
297
298     @Test
299     void whenPassingCorrectJsonWithIncorrectChangeType_validationThrowingAnException() {
300         // @formatter:off
301         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
302                 .name(PM_FILE_NAME)
303                 .location(LOCATION)
304                 .compression(GZIP_COMPRESSION)
305                 .fileFormatVersion(FILE_FORMAT_VERSION)
306                 .build();
307         JsonMessage message = new JsonMessage.JsonMessageBuilder()
308                 .changeIdentifier(CHANGE_IDENTIFIER)
309                 .changeType(INCORRECT_CHANGE_TYPE)
310                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
311                 .addAdditionalField(additionalField)
312                 .build();
313         // @formatter:on
314         String messageString = message.toString();
315         String parsedString = message.getParsed();
316         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
317         JsonElement jsonElement = new JsonParser().parse(parsedString);
318         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
319                 .getJsonObjectFromAnArray(jsonElement);
320
321         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
322                 .expectNextCount(0).expectError(DmaapNotFoundException.class).verify();
323     }
324
325     @Test
326     void whenPassingCorrectJsonWithIncorrectChangeIdentifier_validationThrowingAnException() {
327         // @formatter:off
328         AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
329                 .name(PM_FILE_NAME)
330                 .location(LOCATION)
331                 .compression(GZIP_COMPRESSION)
332                 .fileFormatVersion(FILE_FORMAT_VERSION)
333                 .build();
334         JsonMessage message = new JsonMessage.JsonMessageBuilder()
335                 .changeIdentifier(INCORRECT_CHANGE_IDENTIFIER)
336                 .changeType(CHANGE_TYPE)
337                 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
338                 .addAdditionalField(additionalField)
339                 .build();
340         // @formatter:on
341         String messageString = message.toString();
342         String parsedString = message.getParsed();
343         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
344         JsonElement jsonElement = new JsonParser().parse(parsedString);
345         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
346                 .getJsonObjectFromAnArray(jsonElement);
347
348         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
349                 .expectNextCount(0).expectError(DmaapNotFoundException.class).verify();
350     }
351 }