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.List;
25 import java.util.Optional;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.Test;
29 import org.mockito.Mockito;
30 import org.onap.dcaegen2.collectors.datafile.exceptions.DmaapNotFoundException;
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 {
44 void whenPassingCorrectJson_validationNotThrowingAnException() throws DmaapNotFoundException {
46 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
47 .location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz").compression("gzip")
48 .fileFormatType("org.3GPP.32.435#measCollec").fileFormatVersion("V10").build();
49 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier("PM_MEAS_FILES")
50 .changeType("FileReady").notificationFieldsVersion("1.0").addAdditionalField(additionalField).build();
52 String messageString = message.toString();
54 String parsedString = message.getParsed();
56 FileData expectedFileData = ImmutableFileData.builder().changeIdentifier("PM_MEAS_FILES")
57 .changeType("FileReady").location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz")
58 .compression("gzip").fileFormatType("org.3GPP.32.435#measCollec").fileFormatVersion("V10").build();
60 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
61 JsonElement jsonElement = new JsonParser().parse(parsedString);
62 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
63 .getJsonObjectFromAnArray(jsonElement);
64 List<FileData> listOfFileData = dmaapConsumerJsonParser.getJsonObject(Mono.just((messageString))).block();
66 Assertions.assertNotNull(listOfFileData);
67 Assertions.assertEquals(expectedFileData, listOfFileData.get(0));
71 void whenPassingCorrectJsonWihoutLocation_validationThrowingAnException() {
72 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder().compression("gzip")
73 .fileFormatType("org.3GPP.32.435#measCollec").fileFormatVersion("V10").build();
74 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier("PM_MEAS_FILES")
75 .changeType("FileReady").notificationFieldsVersion("1.0").addAdditionalField(additionalField).build();
77 String messageString = message.toString();
79 String parsedString = message.getParsed();
81 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
82 JsonElement jsonElement = new JsonParser().parse(parsedString);
83 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
84 .getJsonObjectFromAnArray(jsonElement);
85 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
86 .expectError(DmaapNotFoundException.class).verify();
90 void whenPassingCorrectJsonWihoutCompression_validationThrowingAnException() {
91 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
92 .location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz")
93 .fileFormatType("org.3GPP.32.435#measCollec").fileFormatVersion("V10").build();
94 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier("PM_MEAS_FILES")
95 .changeType("FileReady").notificationFieldsVersion("1.0").addAdditionalField(additionalField).build();
97 String messageString = message.toString();
99 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);
105 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
106 .expectError(DmaapNotFoundException.class).verify();
110 void whenPassingCorrectJsonWihoutFileFormatType_validationThrowingAnException() {
111 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
112 .location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz").compression("gzip")
113 .fileFormatVersion("V10").build();
114 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier("PM_MEAS_FILES")
115 .changeType("FileReady").notificationFieldsVersion("1.0").addAdditionalField(additionalField).build();
117 String messageString = message.toString();
119 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 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
126 .expectError(DmaapNotFoundException.class).verify();
130 void whenPassingCorrectJsonWihoutFileFormatVersion_validationThrowingAnException() {
131 AdditionalField additionalField = new JsonMessage.AdditionalFieldBuilder()
132 .location("ftpes://192.168.0.101:22/ftp/rop/A20161224.1030-1045.bin.gz").compression("gzip")
133 .fileFormatType("org.3GPP.32.435#measCollec").build();
134 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier("PM_MEAS_FILES")
135 .changeType("FileReady").notificationFieldsVersion("1.0").addAdditionalField(additionalField).build();
137 String messageString = message.toString();
139 String parsedString = message.getParsed();
141 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
142 JsonElement jsonElement = new JsonParser().parse(parsedString);
143 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
144 .getJsonObjectFromAnArray(jsonElement);
145 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
146 .expectError(DmaapNotFoundException.class).verify();
151 void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() {
152 JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier("PM_MEAS_FILES_INVALID")
153 .changeType("FileReady_INVALID").notificationFieldsVersion("1.0_INVALID").build();
154 String incorrectMessageString = 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);
161 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
162 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
166 void whenPassingJsonWithNullJsonElement_validationThrowingAnException() {
167 JsonMessage message = new JsonMessage.JsonMessageBuilder().build();
168 String incorrectMessageString = message.toString();
170 String parsedString = message.getParsed();
172 DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
173 JsonElement jsonElement = new JsonParser().parse(parsedString);
175 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
176 .getJsonObjectFromAnArray(jsonElement);
177 StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
178 .expectSubscription().expectError(DmaapNotFoundException.class).verify();