167ff03af90ac03611d622797510c767a48ab255
[dcaegen2/collectors/datafile.git] /
1 /*
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.service;
18
19 import static org.mockito.Mockito.spy;
20
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonParser;
23
24 import java.util.Optional;
25
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;
33
34 import reactor.core.publisher.Mono;
35 import reactor.test.StepVerifier;
36
37 /**
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>
40  */
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";
52
53     @Test
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)
57                 .build();
58         JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
59                 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
60                 .addAdditionalField(additionalField).build();
61
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();
65
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);
72
73         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
74                 .expectNext(expectedFileData).verifyComplete();
75     }
76
77     @Test
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();
85
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);
92
93         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
94                 .expectNextCount(0).verifyComplete();
95     }
96
97     @Test
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();
105
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);
112
113         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
114                 .expectNextCount(0).verifyComplete();
115     }
116
117     @Test
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();
124
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);
131
132         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
133                 .expectNextCount(0).verifyComplete();
134     }
135
136     @Test
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();
143
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);
150
151         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
152                 .expectNextCount(0).verifyComplete();
153     }
154
155     @Test
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)
161                 .build();
162         JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
163                 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
164                 .addAdditionalField(additionalFaultyField).addAdditionalField(additionalField).build();
165
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();
169
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);
176
177         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
178                 .expectNext(expectedFileData).verifyComplete();
179     }
180
181     @Test
182     void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() {
183         JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier("PM_MEAS_FILES_INVALID")
184                 .changeType("FileReady_INVALID").notificationFieldsVersion("1.0_INVALID").build();
185
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);
192
193         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
194                 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
195     }
196
197     @Test
198     void whenPassingJsonWithNullJsonElement_validationThrowingAnException() {
199         JsonMessage message = new JsonMessage.JsonMessageBuilder().build();
200
201         String incorrectMessageString = message.toString();
202         String parsedString = message.getParsed();
203         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
204         JsonElement jsonElement = new JsonParser().parse(parsedString);
205
206         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
207                 .getJsonObjectFromAnArray(jsonElement);
208
209         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
210                 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
211     }
212
213     @Test
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();
220
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);
227
228         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
229                 .expectNextCount(0).expectError(DmaapNotFoundException.class).verify();
230     }
231
232     @Test
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();
239
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);
246
247         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
248                 .expectNextCount(0).expectError(DmaapNotFoundException.class).verify();
249     }
250 }