dc51343d547b638ff68a9ca743456989954bd26f
[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.List;
25 import java.util.Optional;
26
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;
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
43     @Test
44     void whenPassingCorrectJson_validationNotThrowingAnException() throws DmaapNotFoundException {
45         // given
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();
51
52         String messageString = message.toString();
53
54         String parsedString = message.getParsed();
55
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();
59         // when
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();
65         // then
66         Assertions.assertNotNull(listOfFileData);
67         Assertions.assertEquals(expectedFileData, listOfFileData.get(0));
68     }
69
70     @Test
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();
76
77         String messageString = message.toString();
78
79         String parsedString = message.getParsed();
80
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();
87     }
88
89     @Test
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();
96
97         String messageString = message.toString();
98
99         String parsedString = message.getParsed();
100
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();
107     }
108
109     @Test
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();
116
117         String messageString = message.toString();
118
119         String parsedString = message.getParsed();
120
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();
127     }
128
129     @Test
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();
136
137         String messageString = message.toString();
138
139         String parsedString = message.getParsed();
140
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();
147     }
148
149     // Fixed temprarily
150     @Test
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();
155
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();
163     }
164
165     @Test
166     void whenPassingJsonWithNullJsonElement_validationThrowingAnException() {
167         JsonMessage message = new JsonMessage.JsonMessageBuilder().build();
168         String incorrectMessageString = message.toString();
169
170         String parsedString = message.getParsed();
171
172         DmaapConsumerJsonParser dmaapConsumerJsonParser = spy(new DmaapConsumerJsonParser());
173         JsonElement jsonElement = new JsonParser().parse(parsedString);
174
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();
179     }
180 }