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 CHANGE_TYPE = "FileReady";
49 private static final String NOTIFICATION_FIELDS_VERSION = "1.0";
52 void whenPassingCorrectJson_validationNotThrowingAnException() throws DmaapNotFoundException {
53 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME).location(LOCATION)
54 .compression(GZIP_COMPRESSION).fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION)
56 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
57 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
58 .addAdditionalField(additionalField).build();
60 FileData expectedFileData = ImmutableFileData.builder().changeIdentifier(CHANGE_IDENTIFIER)
61 .changeType(CHANGE_TYPE).name(PM_FILE_NAME).location(LOCATION).compression(GZIP_COMPRESSION)
62 .fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
64 String messageString = message.toString();
65 String parsedString = message.getParsed();
66 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
67 JsonElement jsonElement = new JsonParser().parse(parsedString);
68 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
69 .getJsonObjectFromAnArray(jsonElement);
71 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
72 .expectNext(expectedFileData).verifyComplete();
76 void whenPassingCorrectJsonWihoutName_noFileData() {
77 AdditionalField additionalField =
78 new JsonMessage.AdditionalFieldBuilder().location(LOCATION).compression(GZIP_COMPRESSION)
79 .fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
80 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
81 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
82 .addAdditionalField(additionalField).build();
84 String messageString = message.toString();
85 String parsedString = message.getParsed();
86 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
87 JsonElement jsonElement = new JsonParser().parse(parsedString);
88 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
89 .getJsonObjectFromAnArray(jsonElement);
91 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
92 .expectNextCount(0).verifyComplete();
96 void whenPassingCorrectJsonWihoutLocation_noFileData() {
97 AdditionalField additionalField =
98 new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME).compression(GZIP_COMPRESSION)
99 .fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
100 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
101 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
102 .addAdditionalField(additionalField).build();
104 String messageString = message.toString();
105 String parsedString = message.getParsed();
106 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
107 JsonElement jsonElement = new JsonParser().parse(parsedString);
108 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
109 .getJsonObjectFromAnArray(jsonElement);
111 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
112 .expectNextCount(0).verifyComplete();
116 void whenPassingCorrectJsonWihoutCompression_noFileData() {
117 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME).location(LOCATION)
118 .fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
119 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
120 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
121 .addAdditionalField(additionalField).build();
123 String messageString = message.toString();
124 String parsedString = message.getParsed();
125 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
126 JsonElement jsonElement = new JsonParser().parse(parsedString);
127 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
128 .getJsonObjectFromAnArray(jsonElement);
130 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
131 .expectNextCount(0).verifyComplete();
135 void whenPassingCorrectJsonWihoutFileFormatType_noFileData() {
136 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME).location(LOCATION)
137 .compression(GZIP_COMPRESSION).fileFormatVersion(FILE_FORMAT_VERSION).build();
138 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
139 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
140 .addAdditionalField(additionalField).build();
142 String messageString = message.toString();
143 String parsedString = message.getParsed();
144 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
145 JsonElement jsonElement = new JsonParser().parse(parsedString);
146 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
147 .getJsonObjectFromAnArray(jsonElement);
149 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
150 .expectNextCount(0).verifyComplete();
154 void whenPassingOneCorrectJsonWihoutFileFormatVersionAndOneCorrect_oneFileData() {
155 AdditionalField additionalFaultyField = new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME)
156 .location(LOCATION).compression(GZIP_COMPRESSION).fileFormatType(FILE_FORMAT_TYPE).build();
157 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder().name(PM_FILE_NAME).location(LOCATION)
158 .compression(GZIP_COMPRESSION).fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION)
160 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
161 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
162 .addAdditionalField(additionalFaultyField).addAdditionalField(additionalField).build();
164 FileData expectedFileData = ImmutableFileData.builder().changeIdentifier(CHANGE_IDENTIFIER)
165 .changeType(CHANGE_TYPE).name(PM_FILE_NAME).location(LOCATION).compression(GZIP_COMPRESSION)
166 .fileFormatType(FILE_FORMAT_TYPE).fileFormatVersion(FILE_FORMAT_VERSION).build();
168 String messageString = message.toString();
169 String parsedString = message.getParsed();
170 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
171 JsonElement jsonElement = new JsonParser().parse(parsedString);
172 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
173 .getJsonObjectFromAnArray(jsonElement);
175 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
176 .expectNext(expectedFileData).verifyComplete();
180 void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() {
181 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier("PM_MEAS_FILES_INVALID")
182 .changeType("FileReady_INVALID").notificationFieldsVersion("1.0_INVALID").build();
184 String incorrectMessageString = message.toString();
185 String parsedString = message.getParsed();
186 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
187 JsonElement jsonElement = new JsonParser().parse(parsedString);
188 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
189 .getJsonObjectFromAnArray(jsonElement);
191 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
192 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
196 void whenPassingJsonWithNullJsonElement_validationThrowingAnException() {
197 JsonMessage message = new JsonMessage.JsonMessageBuilder().build();
199 String incorrectMessageString = message.toString();
200 String parsedString = message.getParsed();
201 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
202 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(incorrectMessageString)))
208 .expectSubscription().expectError(DmaapNotFoundException.class).verify();