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;
 
  28 import java.util.Optional;
 
  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;
 
  36 import reactor.core.publisher.Mono;
 
  37 import reactor.test.StepVerifier;
 
  39 class ReRegistrationDmaapConsumerJsonParserTest {
 
  41     private static JsonParser jsonParser;
 
  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\""
 
  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\""
 
  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\""
 
  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\""
 
  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\""
 
  88         jsonParser = new JsonParser();
 
  92     void passingNonJson_EmptyFluxIsReturned() {
 
  94         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = new ReRegistrationDmaapConsumerJsonParser();
 
  96         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just("not JSON")))
 
 102     void passingNoEvents_EmptyFluxIsReturned() {
 
 104         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = new ReRegistrationDmaapConsumerJsonParser();
 
 106         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just("[]")))
 
 107                 .expectSubscription()
 
 112     void passingOneCorrectEvent_validationSucceeds() {
 
 114         String correlationId = "PNF-CorrelationId";
 
 115         String attachmentPoint = "olt1/1/1";
 
 116         String remoteId = "remoteId";
 
 117         String cvlan = "1005";
 
 118         String svlan = "100";
 
 120         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE, correlationId, attachmentPoint,
 
 121                 remoteId, cvlan, svlan);
 
 123         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
 
 124         JsonElement jsonElement = jsonParser.parse(event);
 
 125         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
 
 126                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
 
 128         String eventsArray = "[" + event + "]";
 
 130         ReRegistrationConsumerDmaapModel expectedEventObject = ImmutableReRegistrationConsumerDmaapModel.builder()
 
 131                 .correlationId(correlationId)
 
 132                 .attachmentPoint(attachmentPoint)
 
 138         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(eventsArray)))
 
 139                 .expectSubscription()
 
 140                 .expectNext(expectedEventObject);
 
 144     void passingTwoCorrectEvents_validationSucceeds() {
 
 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";
 
 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);
 
 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);
 
 168         String eventsArray = "[" + firstEvent + secondEvent + "]";
 
 170         ReRegistrationConsumerDmaapModel expectedFirstEventObject = ImmutableReRegistrationConsumerDmaapModel.builder()
 
 171                 .correlationId(correlationId1)
 
 172                 .attachmentPoint(attachmentPoint1)
 
 177         ReRegistrationConsumerDmaapModel expectedSecondEventObject = ImmutableReRegistrationConsumerDmaapModel.builder()
 
 178                 .correlationId(correlationId2)
 
 179                 .attachmentPoint(attachmentPoint2)
 
 185         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(eventsArray)))
 
 186                 .expectSubscription()
 
 187                 .expectNext(expectedFirstEventObject)
 
 188                 .expectNext(expectedSecondEventObject);
 
 192     void passingJsonWithMissingAttachmentPoint_validationFails() {
 
 194         String correlationId = "PNF-CorrelationId";
 
 195         String remoteId = "remoteId";
 
 196         String cvlan = "1005";
 
 197         String svlan = "100";
 
 199         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_ATTACHMENT_POINT,
 
 205         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
 
 206         JsonElement jsonElement = jsonParser.parse(event);
 
 207         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
 
 208                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
 
 210         String eventsArray = "[" + event + "]";
 
 212         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(eventsArray)))
 
 213                 .expectSubscription()
 
 218     void passingJsonWithMissingCorrelationId_validationFails() {
 
 220         String attachmentPoint = "olt1/1/1";
 
 221         String remoteId = "remoteId";
 
 222         String cvlan = "1005";
 
 223         String svlan = "100";
 
 225         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_CORRELATION_ID,
 
 231         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
 
 232         JsonElement jsonElement = jsonParser.parse(event);
 
 233         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
 
 234                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
 
 236         String eventsArray = "[" + event + "]";
 
 238         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(eventsArray)))
 
 239                 .expectSubscription()
 
 244     void passingJsonWithMissingCorrelationIdValue_validationFails() {
 
 246         String attachmentPoint = "olt1/1/1";
 
 247         String remoteId = "remoteId";
 
 248         String cvlan = "1005";
 
 249         String svlan = "100";
 
 251         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_CORRELATION_ID_VALUE,
 
 257         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
 
 258         JsonElement jsonElement = jsonParser.parse(event);
 
 259         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
 
 260                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
 
 262         String eventsArray = "[" + event + "]";
 
 264         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(eventsArray)))
 
 265                 .expectSubscription()
 
 270     void passingJsonWithMissingAdditionalFields_validationFails() {
 
 272         String correlationId = "PNF-CorrelationId";
 
 273         String attachmentPoint = "olt1/1/1";
 
 274         String remoteId = "remoteId";
 
 275         String cvlan = "1005";
 
 276         String svlan = "100";
 
 278         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE_MISSING_ADDITIONAL_FIELDS,
 
 285         ReRegistrationDmaapConsumerJsonParser consumerJsonParser = spy(new ReRegistrationDmaapConsumerJsonParser());
 
 286         JsonElement jsonElement = jsonParser.parse(event);
 
 287         Mockito.doReturn(Optional.of(jsonElement.getAsJsonObject()))
 
 288                 .when(consumerJsonParser).getJsonObjectFromAnArray(jsonElement);
 
 290         String eventsArray = "[" + event + "]";
 
 292         StepVerifier.create(consumerJsonParser.extractModelFromDmaap(Mono.just(eventsArray)))
 
 293                 .expectSubscription()