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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.service;
19 import static org.mockito.Mockito.spy;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonParser;
24 import java.util.Optional;
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;
36 import reactor.core.publisher.Mono;
37 import reactor.test.StepVerifier;
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>
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";
63 void whenPassingCorrectJson_validationNotThrowingAnException() throws DmaapNotFoundException {
65 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
68 .compression(GZIP_COMPRESSION)
69 .fileFormatType(FILE_FORMAT_TYPE)
70 .fileFormatVersion(FILE_FORMAT_VERSION)
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)
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)
90 FileData expectedFileData = ImmutableFileData.builder()
91 .fileMetaData(fileMetaData)
94 .compression(GZIP_COMPRESSION)
95 .fileFormatType(FILE_FORMAT_TYPE)
96 .fileFormatVersion(FILE_FORMAT_VERSION)
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);
106 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
107 .expectNext(expectedFileData).verifyComplete();
111 void whenPassingCorrectJsonWithFaultyEventName_validationThrowingAnException() {
113 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
115 .compression(GZIP_COMPRESSION)
116 .fileFormatType(FILE_FORMAT_TYPE)
117 .fileFormatVersion(FILE_FORMAT_VERSION)
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)
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);
134 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
135 .expectError(DmaapNotFoundException.class).verify();
139 void whenPassingCorrectJsonWithoutName_noFileData() {
141 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
143 .compression(GZIP_COMPRESSION)
144 .fileFormatType(FILE_FORMAT_TYPE)
145 .fileFormatVersion(FILE_FORMAT_VERSION)
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)
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);
162 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
163 .expectNextCount(0).verifyComplete();
167 void whenPassingCorrectJsonWithoutLocation_noFileData() {
169 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
171 .compression(GZIP_COMPRESSION)
172 .fileFormatType(FILE_FORMAT_TYPE)
173 .fileFormatVersion(FILE_FORMAT_VERSION)
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)
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);
190 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
191 .expectNextCount(0).verifyComplete();
195 void whenPassingCorrectJsonWithoutCompression_noFileData() {
197 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
200 .fileFormatType(FILE_FORMAT_TYPE)
201 .fileFormatVersion(FILE_FORMAT_VERSION)
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)
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);
218 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
219 .expectNextCount(0).verifyComplete();
223 void whenPassingCorrectJsonWithoutFileFormatType_noFileData() {
225 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
228 .compression(GZIP_COMPRESSION)
229 .fileFormatVersion(FILE_FORMAT_VERSION)
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)
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);
246 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
247 .expectNextCount(0).verifyComplete();
251 void whenPassingOneCorrectJsonWithoutFileFormatVersionAndOneCorrect_oneFileData() {
253 AdditionalField additionalFaultyField = new JsonMessage.AdditionalFieldBuilder()
256 .compression(GZIP_COMPRESSION)
257 .fileFormatType(FILE_FORMAT_TYPE)
259 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
262 .compression(GZIP_COMPRESSION)
263 .fileFormatType(FILE_FORMAT_TYPE)
264 .fileFormatVersion(FILE_FORMAT_VERSION)
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)
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)
285 FileData expectedFileData = ImmutableFileData.builder()
286 .fileMetaData(fileMetaData)
289 .compression(GZIP_COMPRESSION)
290 .fileFormatType(FILE_FORMAT_TYPE)
291 .fileFormatVersion(FILE_FORMAT_VERSION)
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);
301 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
302 .expectNext(expectedFileData).verifyComplete();
306 void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() {
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")
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);
322 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
323 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
327 void whenPassingJsonWithNullJsonElement_validationThrowingAnException() {
328 JsonMessage message = new JsonMessage.JsonMessageBuilder().build();
330 String incorrectMessageString = message.toString();
331 String parsedString = message.getParsed();
332 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
333 JsonElement jsonElement = new JsonParser().parse(parsedString);
335 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
336 .getJsonObjectFromAnArray(jsonElement);
338 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
339 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
343 void whenPassingCorrectJsonWithIncorrectChangeType_validationThrowingAnException() {
345 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
348 .compression(GZIP_COMPRESSION)
349 .fileFormatVersion(FILE_FORMAT_VERSION)
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)
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);
366 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
367 .expectNextCount(0).expectError(DmaapNotFoundException.class).verify();
371 void whenPassingCorrectJsonWithIncorrectChangeIdentifier_validationThrowingAnException() {
373 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
376 .compression(GZIP_COMPRESSION)
377 .fileFormatVersion(FILE_FORMAT_VERSION)
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)
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);
394 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
395 .expectNextCount(0).expectError(DmaapNotFoundException.class).verify();