cd238e2d488b6abc456ee4332b760569c64ca2ad
[dcaegen2/services.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.bbs.event.processor.utilities;
22
23 import static org.mockito.Mockito.spy;
24
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParser;
27 import com.google.gson.stream.JsonReader;
28
29 import java.io.StringReader;
30 import java.util.Optional;
31
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;
37
38 import reactor.core.publisher.Mono;
39 import reactor.test.StepVerifier;
40
41 class ReRegistrationDmaapConsumerJsonParserTest {
42
43     private static JsonParser jsonParser;
44
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\""
52             + "}}";
53
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\""
60             + "}}";
61
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\""
68             + "}}";
69
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\""
77             + "}}";
78
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\""
86             + "}}";
87
88     @BeforeAll
89     static void init() {
90         jsonParser = new JsonParser();
91     }
92
93     @Test
94     void passingNonJson_getIllegalStateException() {
95
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);
103     }
104
105     @Test
106     void passingNoEvents_EmptyFluxIsReturned() {
107
108         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = new ReRegistrationDmaapConsumerJsonParser();
109         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse("[]"))))
110                 .expectSubscription()
111                 .verifyComplete();
112     }
113
114     @Test
115     void passingOneCorrectEvent_validationSucceeds() {
116
117         String correlationId = "PNF-CorrelationId";
118         String attachmentPoint = "olt1/1/1";
119         String remoteId = "remoteId";
120         String cvlan = "1005";
121         String svlan = "100";
122
123         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE, correlationId, attachmentPoint,
124                 remoteId, cvlan, svlan);
125
126         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
127         JsonElement jsonElement = jsonParser.parse(event);
128         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
129                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
130
131         String eventsArray = "[" + event + "]";
132
133         ReRegistrationConsumerDmaapModel expectedEventObject = ImmutableReRegistrationConsumerDmaapModel.builder()
134                 .correlationId(correlationId)
135                 .attachmentPoint(attachmentPoint)
136                 .remoteId(remoteId)
137                 .cVlan(cvlan)
138                 .sVlan(svlan)
139                 .build();
140
141         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse(eventsArray))))
142                 .expectSubscription()
143                 .expectNext(expectedEventObject);
144     }
145
146     @Test
147     void passingTwoCorrectEvents_validationSucceeds() {
148
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";
157
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);
162
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);
170
171         String eventsArray = "[" + firstEvent + "," + secondEvent + "]";
172
173         ReRegistrationConsumerDmaapModel expectedFirstEventObject = ImmutableReRegistrationConsumerDmaapModel.builder()
174                 .correlationId(correlationId1)
175                 .attachmentPoint(attachmentPoint1)
176                 .remoteId(remoteId1)
177                 .cVlan(cvlan)
178                 .sVlan(svlan)
179                 .build();
180         ReRegistrationConsumerDmaapModel expectedSecondEventObject = ImmutableReRegistrationConsumerDmaapModel.builder()
181                 .correlationId(correlationId2)
182                 .attachmentPoint(attachmentPoint2)
183                 .remoteId(remoteId2)
184                 .cVlan(cvlan)
185                 .sVlan(svlan)
186                 .build();
187
188         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse(eventsArray))))
189                 .expectSubscription()
190                 .expectNext(expectedFirstEventObject)
191                 .expectNext(expectedSecondEventObject);
192     }
193
194     @Test
195     void passingJsonWithMissingAttachmentPoint_validationFails() {
196
197         String correlationId = "PNF-CorrelationId";
198         String remoteId = "remoteId";
199         String cvlan = "1005";
200         String svlan = "100";
201
202         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_ATTACHMENT_POINT,
203                 correlationId,
204                 remoteId,
205                 cvlan,
206                 svlan);
207
208         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
209         JsonElement jsonElement = jsonParser.parse(event);
210         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
211                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
212
213         String eventsArray = "[" + event + "]";
214
215         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse(eventsArray))))
216                 .expectSubscription()
217                 .verifyComplete();
218     }
219
220     @Test
221     void passingJsonWithMissingCorrelationId_validationFails() {
222
223         String attachmentPoint = "olt1/1/1";
224         String remoteId = "remoteId";
225         String cvlan = "1005";
226         String svlan = "100";
227
228         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_CORRELATION_ID,
229                 attachmentPoint,
230                 remoteId,
231                 cvlan,
232                 svlan);
233
234         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
235         JsonElement jsonElement = jsonParser.parse(event);
236         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
237                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
238
239         String eventsArray = "[" + event + "]";
240
241         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse(eventsArray))))
242                 .expectSubscription()
243                 .verifyComplete();
244     }
245
246     @Test
247     void passingJsonWithMissingCorrelationIdValue_validationFails() {
248
249         String attachmentPoint = "olt1/1/1";
250         String remoteId = "remoteId";
251         String cvlan = "1005";
252         String svlan = "100";
253
254         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_CORRELATION_ID_VALUE,
255                 attachmentPoint,
256                 remoteId,
257                 cvlan,
258                 svlan);
259
260         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
261         JsonElement jsonElement = jsonParser.parse(event);
262         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
263                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
264
265         String eventsArray = "[" + event + "]";
266
267         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse(eventsArray))))
268                 .expectSubscription()
269                 .verifyComplete();
270     }
271
272     @Test
273     void passingJsonWithMissingAdditionalFields_validationFails() {
274
275         String correlationId = "PNF-CorrelationId";
276         String attachmentPoint = "olt1/1/1";
277         String remoteId = "remoteId";
278         String cvlan = "1005";
279         String svlan = "100";
280
281         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_ADDITIONAL_FIELDS,
282                 correlationId,
283                 attachmentPoint,
284                 remoteId,
285                 cvlan,
286                 svlan);
287
288         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
289         JsonElement jsonElement = jsonParser.parse(event);
290         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
291                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
292
293         String eventsArray = "[" + event + "]";
294
295         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(jsonParser.parse(eventsArray))))
296                 .expectSubscription()
297                 .verifyComplete();
298     }
299
300 }