ca448dd0d4539edd3d89736fbb4cda00b4d58e96
[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
28 import java.util.Optional;
29
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
32 import org.mockito.Mockito;
33 import org.onap.bbs.event.processor.model.ImmutableReRegistrationConsumerDmaapModel;
34 import org.onap.bbs.event.processor.model.ReRegistrationConsumerDmaapModel;
35
36 import reactor.core.publisher.Mono;
37 import reactor.test.StepVerifier;
38
39 class ReRegistrationDmaapConsumerJsonParserTest {
40
41     private static JsonParser jsonParser;
42
43     private static final String RE_REGISTRATION_EVENT_TEMPLATE = "{"
44             + "\"correlationId\":\"%s\","
45             + "\"additionalFields\": {"
46             + " \"attachment-point\": \"%s\","
47             + " \"remote-id\": \"%s\","
48             + " \"cvlan\": \"%s\","
49             + " \"svlan\": \"%s\""
50             + "}}";
51
52     private static final String RE_REGISTRATION_EVENT_TEMPLATE_MISSING_ATTACHMENT_POINT = "{"
53             + "\"correlationId\":\"%s\","
54             + "\"additionalFields\": {"
55             + " \"remote-id\": \"%s\","
56             + " \"cvlan\": \"%s\","
57             + " \"svlan\": \"%s\""
58             + "}}";
59
60     private static final String RE_REGISTRATION_EVENT_TEMPLATE_MISSING_CORRELATION_ID = "{"
61             + "\"additionalFields\": {"
62             + " \"attachment-point\": \"%s\","
63             + " \"remote-id\": \"%s\","
64             + " \"cvlan\": \"%s\","
65             + " \"svlan\": \"%s\""
66             + "}}";
67
68     private static final String RE_REGISTRATION_EVENT_TEMPLATE_MISSING_CORRELATION_ID_VALUE = "{"
69             + "\"correlationId\":\"\","
70             + "\"additionalFields\": {"
71             + " \"attachment-point\": \"%s\","
72             + " \"remote-id\": \"%s\","
73             + " \"cvlan\": \"%s\","
74             + " \"svlan\": \"%s\""
75             + "}}";
76
77     private static final String RE_REGISTRATION_EVENT_TEMPLATE_MISSING_ADDITIONAL_FIELDS = "{"
78             + "\"correlationId\":\"%s\","
79             + "\"somethingElse\": {"
80             + " \"attachment-point\": \"%s\","
81             + " \"remote-id\": \"%s\","
82             + " \"cvlan\": \"%s\","
83             + " \"svlan\": \"%s\""
84             + "}}";
85
86     @BeforeAll
87     static void init() {
88         jsonParser = new JsonParser();
89     }
90
91     @Test
92     void passingNonJson_EmptyFluxIsReturned() {
93
94         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = new ReRegistrationDmaapConsumerJsonParser();
95
96         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just("not JSON")))
97                 .expectSubscription()
98                 .verifyComplete();
99     }
100
101     @Test
102     void passingNoEvents_EmptyFluxIsReturned() {
103
104         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = new ReRegistrationDmaapConsumerJsonParser();
105
106         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just("[]")))
107                 .expectSubscription()
108                 .verifyComplete();
109     }
110
111     @Test
112     void passingOneCorrectEvent_validationSucceeds() {
113
114         String correlationId = "PNF-CorrelationId";
115         String attachmentPoint = "olt1/1/1";
116         String remoteId = "remoteId";
117         String cvlan = "1005";
118         String svlan = "100";
119
120         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE, correlationId, attachmentPoint,
121                 remoteId, cvlan, svlan);
122
123         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
124         JsonElement jsonElement = jsonParser.parse(event);
125         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
126                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
127
128         String eventsArray = "[" + event + "]";
129
130         ReRegistrationConsumerDmaapModel expectedEventObject = ImmutableReRegistrationConsumerDmaapModel.builder()
131                 .correlationId(correlationId)
132                 .attachmentPoint(attachmentPoint)
133                 .remoteId(remoteId)
134                 .cVlan(cvlan)
135                 .sVlan(svlan)
136                 .build();
137
138         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(eventsArray)))
139                 .expectSubscription()
140                 .expectNext(expectedEventObject);
141     }
142
143     @Test
144     void passingTwoCorrectEvents_validationSucceeds() {
145
146         String correlationId1 = "PNF-CorrelationId1";
147         String correlationId2 = "PNF-CorrelationId2";
148         String attachmentPoint1 = "olt1/1/1";
149         String attachmentPoint2 = "olt2/2/2";
150         String remoteId1 = "remoteId1";
151         String remoteId2 = "remoteId2";
152         String cvlan = "1005";
153         String svlan = "100";
154
155         String firstEvent = String.format(RE_REGISTRATION_EVENT_TEMPLATE, correlationId1, attachmentPoint1,
156                 remoteId1, cvlan, svlan);
157         String secondEvent = String.format(RE_REGISTRATION_EVENT_TEMPLATE, correlationId1, attachmentPoint1,
158                 remoteId1, cvlan, svlan);
159
160         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
161         JsonElement jsonElement1 = jsonParser.parse(firstEvent);
162         Mockito.doReturn(Optional.of(jsonElement1.getAsJsonObject()))
163                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement1);
164         JsonElement jsonElement2 = jsonParser.parse(secondEvent);
165         Mockito.doReturn(Optional.of(jsonElement2.getAsJsonObject()))
166                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement2);
167
168         String eventsArray = "[" + firstEvent + secondEvent + "]";
169
170         ReRegistrationConsumerDmaapModel expectedFirstEventObject = ImmutableReRegistrationConsumerDmaapModel.builder()
171                 .correlationId(correlationId1)
172                 .attachmentPoint(attachmentPoint1)
173                 .remoteId(remoteId1)
174                 .cVlan(cvlan)
175                 .sVlan(svlan)
176                 .build();
177         ReRegistrationConsumerDmaapModel expectedSecondEventObject = ImmutableReRegistrationConsumerDmaapModel.builder()
178                 .correlationId(correlationId2)
179                 .attachmentPoint(attachmentPoint2)
180                 .remoteId(remoteId2)
181                 .cVlan(cvlan)
182                 .sVlan(svlan)
183                 .build();
184
185         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(eventsArray)))
186                 .expectSubscription()
187                 .expectNext(expectedFirstEventObject)
188                 .expectNext(expectedSecondEventObject);
189     }
190
191     @Test
192     void passingJsonWithMissingAttachmentPoint_validationFails() {
193
194         String correlationId = "PNF-CorrelationId";
195         String remoteId = "remoteId";
196         String cvlan = "1005";
197         String svlan = "100";
198
199         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_ATTACHMENT_POINT,
200                 correlationId,
201                 remoteId,
202                 cvlan,
203                 svlan);
204
205         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
206         JsonElement jsonElement = jsonParser.parse(event);
207         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
208                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
209
210         String eventsArray = "[" + event + "]";
211
212         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(eventsArray)))
213                 .expectSubscription()
214                 .verifyComplete();
215     }
216
217     @Test
218     void passingJsonWithMissingCorrelationId_validationFails() {
219
220         String attachmentPoint = "olt1/1/1";
221         String remoteId = "remoteId";
222         String cvlan = "1005";
223         String svlan = "100";
224
225         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_CORRELATION_ID,
226                 attachmentPoint,
227                 remoteId,
228                 cvlan,
229                 svlan);
230
231         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
232         JsonElement jsonElement = jsonParser.parse(event);
233         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
234                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
235
236         String eventsArray = "[" + event + "]";
237
238         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(eventsArray)))
239                 .expectSubscription()
240                 .verifyComplete();
241     }
242
243     @Test
244     void passingJsonWithMissingCorrelationIdValue_validationFails() {
245
246         String attachmentPoint = "olt1/1/1";
247         String remoteId = "remoteId";
248         String cvlan = "1005";
249         String svlan = "100";
250
251         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_CORRELATION_ID_VALUE,
252                 attachmentPoint,
253                 remoteId,
254                 cvlan,
255                 svlan);
256
257         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
258         JsonElement jsonElement = jsonParser.parse(event);
259         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
260                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
261
262         String eventsArray = "[" + event + "]";
263
264         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(eventsArray)))
265                 .expectSubscription()
266                 .verifyComplete();
267     }
268
269     @Test
270     void passingJsonWithMissingAdditionalFields_validationFails() {
271
272         String correlationId = "PNF-CorrelationId";
273         String attachmentPoint = "olt1/1/1";
274         String remoteId = "remoteId";
275         String cvlan = "1005";
276         String svlan = "100";
277
278         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_ADDITIONAL_FIELDS,
279                 correlationId,
280                 attachmentPoint,
281                 remoteId,
282                 cvlan,
283                 svlan);
284
285         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
286         JsonElement jsonElement = jsonParser.parse(event);
287         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
288                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
289
290         String eventsArray = "[" + event + "]";
291
292         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(eventsArray)))
293                 .expectSubscription()
294                 .verifyComplete();
295     }
296
297 }