b5457b82a5a292ca7212345bedebcd091da7cef4
[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 CHANGE_TYPE = "FileReady";
49     private static final String NOTIFICATION_FIELDS_VERSION = "1.0";
50
51     @Test
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)
55                 .build();
56         JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
57                 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
58                 .addAdditionalField(additionalField).build();
59
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();
63
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);
70
71         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
72                 .expectNext(expectedFileData).verifyComplete();
73     }
74
75     @Test
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();
83
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);
90
91         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
92                 .expectNextCount(0).verifyComplete();
93     }
94
95     @Test
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();
103
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);
110
111         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
112                 .expectNextCount(0).verifyComplete();
113     }
114
115     @Test
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();
122
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);
129
130         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
131                 .expectNextCount(0).verifyComplete();
132     }
133
134     @Test
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();
141
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);
148
149         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
150                 .expectNextCount(0).verifyComplete();
151     }
152
153     @Test
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)
159                 .build();
160         JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier(CHANGE_IDENTIFIER)
161                 .changeType(CHANGE_TYPE).notificationFieldsVersion(NOTIFICATION_FIELDS_VERSION)
162                 .addAdditionalField(additionalFaultyField).addAdditionalField(additionalField).build();
163
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();
167
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);
174
175         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(messageString))).expectSubscription()
176                 .expectNext(expectedFileData).verifyComplete();
177     }
178
179     @Test
180     void whenPassingJsonWithoutMandatoryHeaderInformation_validationThrowingAnException() {
181         JsonMessage message = new JsonMessage.JsonMessageBuilder().changeIdentifier("PM_MEAS_FILES_INVALID")
182                 .changeType("FileReady_INVALID").notificationFieldsVersion("1.0_INVALID").build();
183
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);
190
191         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
192                 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
193     }
194
195     @Test
196     void whenPassingJsonWithNullJsonElement_validationThrowingAnException() {
197         JsonMessage message = new JsonMessage.JsonMessageBuilder().build();
198
199         String incorrectMessageString = message.toString();
200         String parsedString = message.getParsed();
201         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
202         JsonElement jsonElement = new JsonParser().parse(parsedString);
203
204         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject())).when(dmaapConsumerJsonParser)
205                 .getJsonObjectFromAnArray(jsonElement);
206
207         StepVerifier.create(dmaapConsumerJsonParser.getJsonObject(Mono.just(incorrectMessageString)))
208                 .expectSubscription().expectError(DmaapNotFoundException.class).verify();
209     }
210 }