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 PM_FILE_NAME = "A20161224.1030-1045.bin.gz";
43 private static final String LOCATION = "ftpes://192.168.0.101:22/ftp/rop/" + PM_FILE_NAME;
44 private static final String GZIP_COMPRESSION = "gzip";
45 private static final String FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec";
46 private static final String FILE_FORMAT_VERSION = "V10";
47 private static final String CHANGE_IDENTIFIER = "PM_MEAS_FILES";
48 private static final String INCORRECT_CHANGE_IDENTIFIER = "INCORRECT_PM_MEAS_FILES";
49 private static final String CHANGE_TYPE = "FileReady";
50 private static final String INCORRECT_CHANGE_TYPE = "IncorrectFileReady";
51 private static final String NOTIFICATION_FIELDS_VERSION = "1.0";
54 void whenPassingCorrectJson_validationNotThrowingAnException() throws DmaapNotFoundException {
55 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME).location(LOCATION)
56 .compression(GZIP_COMPRESSION).fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION)
58 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
59 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
60 .addAdditionalField(additionalField).build();
62 FileData expectedFileData = ImmutableFileData.builder().changeIdentifier(CHANGE_IDENTIFIER)
63 .changeType(CHANGE_TYPE).name(PM_FILE_NAME).location(LOCATION).compression(GZIP_COMPRESSION)
64 .fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
66 String messageString = message.toString();
67 String parsedString = message.getParsed();
68 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
69 JsonElement jsonElement = new JsonParser().parse(parsedString);
70 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
71 .getJsonObjectFromAnArray(jsonElement);
73 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
74 .expectNext(expectedFileData).verifyComplete();
78 void whenPassingCorrectJsonWithoutName_noFileData() {
79 AdditionalField additionalField =
80 new JsonMessage.AdditionalFieldBuilder().location(LOCATION).compression(GZIP_COMPRESSION)
81 .fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
82 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
83 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
84 .addAdditionalField(additionalField).build();
86 String messageString = message.toString();
87 String parsedString = message.getParsed();
88 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
89 JsonElement jsonElement = new JsonParser().parse(parsedString);
90 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
91 .getJsonObjectFromAnArray(jsonElement);
93 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
94 .expectNextCount(0).verifyComplete();
98 void whenPassingCorrectJsonWithoutLocation_noFileData() {
99 AdditionalField additionalField =
100 new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME).compression(GZIP_COMPRESSION)
101 .fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
102 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
103 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
104 .addAdditionalField(additionalField).build();
106 String messageString = message.toString();
107 String parsedString = message.getParsed();
108 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
109 JsonElement jsonElement = new JsonParser().parse(parsedString);
110 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
111 .getJsonObjectFromAnArray(jsonElement);
113 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
114 .expectNextCount(0).verifyComplete();
118 void whenPassingCorrectJsonWithoutCompression_noFileData() {
119 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME).location(LOCATION)
120 .fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
121 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
122 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
123 .addAdditionalField(additionalField).build();
125 String messageString = message.toString();
126 String parsedString = message.getParsed();
127 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
128 JsonElement jsonElement = new JsonParser().parse(parsedString);
129 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
130 .getJsonObjectFromAnArray(jsonElement);
132 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
133 .expectNextCount(0).verifyComplete();
137 void whenPassingCorrectJsonWithoutFileFormatType_noFileData() {
138 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME).location(LOCATION)
139 .compression(GZIP_COMPRESSION).fileFormatVersion(FILE_FORMAT_VERSION).build();
140 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
141 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
142 .addAdditionalField(additionalField).build();
144 String messageString = message.toString();
145 String parsedString = message.getParsed();
146 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
147 JsonElement jsonElement = new JsonParser().parse(parsedString);
148 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
149 .getJsonObjectFromAnArray(jsonElement);
151 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
152 .expectNextCount(0).verifyComplete();
156 void whenPassingOneCorrectJsonWithoutFileFormatVersionAndOneCorrect_oneFileData() {
157 AdditionalField additionalFaultyField = new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME)
158 .location(LOCATION).compression(GZIP_COMPRESSION).fileFormatType(FILE_FORMAT_TYPE).build();
159 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME).location(LOCATION)
160 .compression(GZIP_COMPRESSION).fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION)
162 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
163 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
164 .addAdditionalField(additionalFaultyField).addAdditionalField(additionalField).build();
166 FileData expectedFileData = ImmutableFileData.builder().changeIdentifier(CHANGE_IDENTIFIER)
167 .changeType(CHANGE_TYPE).name(PM_FILE_NAME).location(LOCATION).compression(GZIP_COMPRESSION)
168 .fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
170 String messageString = message.toString();
171 String parsedString = message.getParsed();
172 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
173 JsonElement jsonElement = new JsonParser().parse(parsedString);
174 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
175 .getJsonObjectFromAnArray(jsonElement);
177 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
178 .expectNext(expectedFileData).verifyComplete();
182 void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() {
183 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier("PM_MEAS_FILES_INVALID")
184 .changeType("FileReady_INVALID").notificationFieldsVersion("1.0_INVALID").build();
186 String incorrectMessageString = message.toString();
187 String parsedString = message.getParsed();
188 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
189 JsonElement jsonElement = new JsonParser().parse(parsedString);
190 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
191 .getJsonObjectFromAnArray(jsonElement);
193 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
194 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
198 void whenPassingJsonWithNullJsonElement_validationThrowingAnException() {
199 JsonMessage message = new JsonMessage.JsonMessageBuilder().build();
201 String incorrectMessageString = message.toString();
202 String parsedString = message.getParsed();
203 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
204 JsonElement jsonElement = new JsonParser().parse(parsedString);
206 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
207 .getJsonObjectFromAnArray(jsonElement);
209 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
210 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
214 void whenPassingCorrectJsonWithIncorrectChangeType_validationThrowingAnException() {
215 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME).location(LOCATION)
216 .compression(GZIP_COMPRESSION).fileFormatVersion(FILE_FORMAT_VERSION).build();
217 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
218 .changeType(INCORRECT_CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
219 .addAdditionalField(additionalField).build();
221 String messageString = message.toString();
222 String parsedString = message.getParsed();
223 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
224 JsonElement jsonElement = new JsonParser().parse(parsedString);
225 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
226 .getJsonObjectFromAnArray(jsonElement);
228 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
229 .expectNextCount(0).expectError(DmaapNotFoundException.class).verify();
233 void whenPassingCorrectJsonWithIncorrectChangeIdentifier_validationThrowingAnException() {
234 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME).location(LOCATION)
235 .compression(GZIP_COMPRESSION).fileFormatVersion(FILE_FORMAT_VERSION).build();
236 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(INCORRECT_CHANGE_IDENTIFIER)
237 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
238 .addAdditionalField(additionalField).build();
240 String messageString = message.toString();
241 String parsedString = message.getParsed();
242 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
243 JsonElement jsonElement = new JsonParser().parse(parsedString);
244 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
245 .getJsonObjectFromAnArray(jsonElement);
247 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
248 .expectNextCount(0).expectError(DmaapNotFoundException.class).verify();