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.ImmutableFileData;
31 import org.onap.dcaegen2.collectors.datafile.utils.JsonMessage;
32 import org.onap.dcaegen2.collectors.datafile.utils.JsonMessage.AdditionalField;
34 import reactor.core.publisher.Mono;
35 import reactor.test.StepVerifier;
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>
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";
60 void whenPassingCorrectJson_validationNotThrowingAnException() throws DmaapNotFoundException {
62 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
65 .compression(GZIP_COMPRESSION)
66 .fileFormatType(FILE_FORMAT_TYPE)
67 .fileFormatVersion(FILE_FORMAT_VERSION)
69 JsonMessage message = new JsonMessage.JsonMessageBuilder()
70 .changeIdentifier(CHANGE_IDENTIFIER)
71 .changeType(CHANGE_TYPE)
72 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
73 .addAdditionalField(additionalField)
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)
87 .compression(GZIP_COMPRESSION)
88 .fileFormatType(FILE_FORMAT_TYPE)
89 .fileFormatVersion(FILE_FORMAT_VERSION)
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);
99 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
100 .expectNext(expectedFileData).verifyComplete();
104 void whenPassingCorrectJsonWithoutName_noFileData() {
106 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
108 .compression(GZIP_COMPRESSION)
109 .fileFormatType(FILE_FORMAT_TYPE)
110 .fileFormatVersion(FILE_FORMAT_VERSION)
112 JsonMessage message = new JsonMessage.JsonMessageBuilder()
113 .changeIdentifier(CHANGE_IDENTIFIER)
114 .changeType(CHANGE_TYPE)
115 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
116 .addAdditionalField(additionalField)
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);
126 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
127 .expectNextCount(0).verifyComplete();
131 void whenPassingCorrectJsonWithoutLocation_noFileData() {
133 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
135 .compression(GZIP_COMPRESSION)
136 .fileFormatType(FILE_FORMAT_TYPE)
137 .fileFormatVersion(FILE_FORMAT_VERSION)
139 JsonMessage message = new JsonMessage.JsonMessageBuilder()
140 .changeIdentifier(CHANGE_IDENTIFIER)
141 .changeType(CHANGE_TYPE)
142 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
143 .addAdditionalField(additionalField)
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);
153 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
154 .expectNextCount(0).verifyComplete();
158 void whenPassingCorrectJsonWithoutCompression_noFileData() {
160 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
163 .fileFormatType(FILE_FORMAT_TYPE)
164 .fileFormatVersion(FILE_FORMAT_VERSION)
166 JsonMessage message = new JsonMessage.JsonMessageBuilder()
167 .changeIdentifier(CHANGE_IDENTIFIER)
168 .changeType(CHANGE_TYPE)
169 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
170 .addAdditionalField(additionalField)
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);
180 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
181 .expectNextCount(0).verifyComplete();
185 void whenPassingCorrectJsonWithoutFileFormatType_noFileData() {
187 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
190 .compression(GZIP_COMPRESSION)
191 .fileFormatVersion(FILE_FORMAT_VERSION)
193 JsonMessage message = new JsonMessage.JsonMessageBuilder()
194 .changeIdentifier(CHANGE_IDENTIFIER)
195 .changeType(CHANGE_TYPE)
196 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
197 .addAdditionalField(additionalField)
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);
207 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
208 .expectNextCount(0).verifyComplete();
212 void whenPassingOneCorrectJsonWithoutFileFormatVersionAndOneCorrect_oneFileData() {
214 AdditionalField additionalFaultyField = new JsonMessage.AdditionalFieldBuilder()
217 .compression(GZIP_COMPRESSION)
218 .fileFormatType(FILE_FORMAT_TYPE)
220 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
223 .compression(GZIP_COMPRESSION)
224 .fileFormatType(FILE_FORMAT_TYPE)
225 .fileFormatVersion(FILE_FORMAT_VERSION)
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)
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)
246 .compression(GZIP_COMPRESSION)
247 .fileFormatType(FILE_FORMAT_TYPE)
248 .fileFormatVersion(FILE_FORMAT_VERSION)
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);
258 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
259 .expectNext(expectedFileData).verifyComplete();
263 void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() {
265 JsonMessage message = new JsonMessage.JsonMessageBuilder()
266 .changeIdentifier("PM_MEAS_FILES_INVALID")
267 .changeType("FileReady_INVALID")
268 .notificationFieldsVersion("1.0_INVALID")
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);
278 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
279 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
283 void whenPassingJsonWithNullJsonElement_validationThrowingAnException() {
284 JsonMessage message = new JsonMessage.JsonMessageBuilder().build();
286 String incorrectMessageString = message.toString();
287 String parsedString = message.getParsed();
288 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
289 JsonElement jsonElement = new JsonParser().parse(parsedString);
291 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
292 .getJsonObjectFromAnArray(jsonElement);
294 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
295 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
299 void whenPassingCorrectJsonWithIncorrectChangeType_validationThrowingAnException() {
301 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
304 .compression(GZIP_COMPRESSION)
305 .fileFormatVersion(FILE_FORMAT_VERSION)
307 JsonMessage message = new JsonMessage.JsonMessageBuilder()
308 .changeIdentifier(CHANGE_IDENTIFIER)
309 .changeType(INCORRECT_CHANGE_TYPE)
310 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
311 .addAdditionalField(additionalField)
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);
321 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
322 .expectNextCount(0).expectError(DmaapNotFoundException.class).verify();
326 void whenPassingCorrectJsonWithIncorrectChangeIdentifier_validationThrowingAnException() {
328 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
331 .compression(GZIP_COMPRESSION)
332 .fileFormatVersion(FILE_FORMAT_VERSION)
334 JsonMessage message = new JsonMessage.JsonMessageBuilder()
335 .changeIdentifier(INCORRECT_CHANGE_IDENTIFIER)
336 .changeType(CHANGE_TYPE)
337 .notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
338 .addAdditionalField(additionalField)
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);
348 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
349 .expectNextCount(0).expectError(DmaapNotFoundException.class).verify();