2 * ============LICENSE_START=======================================================
3 * BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2019 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.bbs.event.processor.utilities;
23 import static org.mockito.Mockito.spy;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParser;
27 import com.google.gson.stream.JsonReader;
29 import java.io.StringReader;
30 import java.util.Optional;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34 import org.mockito.Mockito;
35 import org.onap.bbs.event.processor.model.ImmutableReRegistrationConsumerDmaapModel;
36 import org.onap.bbs.event.processor.model.ReRegistrationConsumerDmaapModel;
38 import reactor.core.publisher.Mono;
39 import reactor.test.StepVerifier;
41 class ReRegistrationDmaapConsumerJsonParserTest {
43 private static JsonParser jsonParser;
45 private static final String RE_REGISTRATION_EVENT_TEMPLATE = "{"
46 + "\"correlationId\":\"%s\","
47 + "\"additionalFields\": {"
48 + " \"attachment-point\": \"%s\","
49 + " \"remote-id\": \"%s\","
50 + " \"cvlan\": \"%s\","
51 + " \"svlan\": \"%s\""
54 private static final String RE_REGISTRATION_EVENT_TEMPLATE_MISSING_ATTACHMENT_POINT = "{"
55 + "\"correlationId\":\"%s\","
56 + "\"additionalFields\": {"
57 + " \"remote-id\": \"%s\","
58 + " \"cvlan\": \"%s\","
59 + " \"svlan\": \"%s\""
62 private static final String RE_REGISTRATION_EVENT_TEMPLATE_MISSING_CORRELATION_ID = "{"
63 + "\"additionalFields\": {"
64 + " \"attachment-point\": \"%s\","
65 + " \"remote-id\": \"%s\","
66 + " \"cvlan\": \"%s\","
67 + " \"svlan\": \"%s\""
70 private static final String RE_REGISTRATION_EVENT_TEMPLATE_MISSING_CORRELATION_ID_VALUE = "{"
71 + "\"correlationId\":\"\","
72 + "\"additionalFields\": {"
73 + " \"attachment-point\": \"%s\","
74 + " \"remote-id\": \"%s\","
75 + " \"cvlan\": \"%s\","
76 + " \"svlan\": \"%s\""
79 private static final String RE_REGISTRATION_EVENT_TEMPLATE_MISSING_ADDITIONAL_FIELDS = "{"
80 + "\"correlationId\":\"%s\","
81 + "\"somethingElse\": {"
82 + " \"attachment-point\": \"%s\","
83 + " \"remote-id\": \"%s\","
84 + " \"cvlan\": \"%s\","
85 + " \"svlan\": \"%s\""
90 jsonParser = new JsonParser();
94 void passingNonJson_getIllegalStateException() {
96 ReRegistrationDmaapConsumerJsonParser consumerJsonParser = new ReRegistrationDmaapConsumerJsonParser();
97 JsonReader jsonReader = new JsonReader(new StringReader("not JSON"));
98 jsonReader.setLenient(true);
99 JsonElement notJson = jsonParser.parse(jsonReader).getAsJsonPrimitive();
100 StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(notJson)))
101 .expectSubscription()
102 .verifyError(IllegalStateException.class);
106 void passingNoEvents_EmptyFluxIsReturned() {
108 ReRegistrationDmaapConsumerJsonParser consumerJsonParser = new ReRegistrationDmaapConsumerJsonParser();
109 StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse("[]"))))
110 .expectSubscription()
115 void passingOneCorrectEvent_validationSucceeds() {
117 String correlationId = "PNF-CorrelationId";
118 String attachmentPoint = "olt1/1/1";
119 String remoteId = "remoteId";
120 String cvlan = "1005";
121 String svlan = "100";
123 String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE, correlationId, attachmentPoint,
124 remoteId, cvlan, svlan);
126 ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
127 JsonElement jsonElement = jsonParser.parse(event);
128 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
129 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
131 String eventsArray = "[" + event + "]";
133 ReRegistrationConsumerDmaapModel expectedEventObject = ImmutableReRegistrationConsumerDmaapModel.builder()
134 .correlationId(correlationId)
135 .attachmentPoint(attachmentPoint)
141 StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse(eventsArray))))
142 .expectSubscription()
143 .expectNext(expectedEventObject);
147 void passingTwoCorrectEvents_validationSucceeds() {
149 String correlationId1 = "PNF-CorrelationId1";
150 String correlationId2 = "PNF-CorrelationId2";
151 String attachmentPoint1 = "olt1/1/1";
152 String attachmentPoint2 = "olt2/2/2";
153 String remoteId1 = "remoteId1";
154 String remoteId2 = "remoteId2";
155 String cvlan = "1005";
156 String svlan = "100";
158 String firstEvent = String.format(RE_REGISTRATION_EVENT_TEMPLATE, correlationId1, attachmentPoint1,
159 remoteId1, cvlan, svlan);
160 String secondEvent = String.format(RE_REGISTRATION_EVENT_TEMPLATE, correlationId1, attachmentPoint1,
161 remoteId1, cvlan, svlan);
163 ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
164 JsonElement jsonElement1 = jsonParser.parse(firstEvent);
165 Mockito.doReturn(Optional.of(jsonElement1.getAsJsonObject()))
166 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement1);
167 JsonElement jsonElement2 = jsonParser.parse(secondEvent);
168 Mockito.doReturn(Optional.of(jsonElement2.getAsJsonObject()))
169 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement2);
171 String eventsArray = "[" + firstEvent + "," + secondEvent + "]";
173 ReRegistrationConsumerDmaapModel expectedFirstEventObject = ImmutableReRegistrationConsumerDmaapModel.builder()
174 .correlationId(correlationId1)
175 .attachmentPoint(attachmentPoint1)
180 ReRegistrationConsumerDmaapModel expectedSecondEventObject = ImmutableReRegistrationConsumerDmaapModel.builder()
181 .correlationId(correlationId2)
182 .attachmentPoint(attachmentPoint2)
188 StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse(eventsArray))))
189 .expectSubscription()
190 .expectNext(expectedFirstEventObject)
191 .expectNext(expectedSecondEventObject);
195 void passingJsonWithMissingAttachmentPoint_validationFails() {
197 String correlationId = "PNF-CorrelationId";
198 String remoteId = "remoteId";
199 String cvlan = "1005";
200 String svlan = "100";
202 String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_ATTACHMENT_POINT,
208 ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
209 JsonElement jsonElement = jsonParser.parse(event);
210 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
211 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
213 String eventsArray = "[" + event + "]";
215 StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse(eventsArray))))
216 .expectSubscription()
221 void passingJsonWithMissingCorrelationId_validationFails() {
223 String attachmentPoint = "olt1/1/1";
224 String remoteId = "remoteId";
225 String cvlan = "1005";
226 String svlan = "100";
228 String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_CORRELATION_ID,
234 ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
235 JsonElement jsonElement = jsonParser.parse(event);
236 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
237 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
239 String eventsArray = "[" + event + "]";
241 StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse(eventsArray))))
242 .expectSubscription()
247 void passingJsonWithMissingCorrelationIdValue_validationFails() {
249 String attachmentPoint = "olt1/1/1";
250 String remoteId = "remoteId";
251 String cvlan = "1005";
252 String svlan = "100";
254 String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_CORRELATION_ID_VALUE,
260 ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
261 JsonElement jsonElement = jsonParser.parse(event);
262 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
263 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
265 String eventsArray = "[" + event + "]";
267 StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse(eventsArray))))
268 .expectSubscription()
273 void passingJsonWithMissingAdditionalFields_validationFails() {
275 String correlationId = "PNF-CorrelationId";
276 String attachmentPoint = "olt1/1/1";
277 String remoteId = "remoteId";
278 String cvlan = "1005";
279 String svlan = "100";
281 String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_ADDITIONAL_FIELDS,
288 ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
289 JsonElement jsonElement = jsonParser.parse(event);
290 Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
291 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
293 String eventsArray = "[" + event + "]";
295 StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse(eventsArray))))
296 .expectSubscription()