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.tasks;
 
  23 import static org.mockito.Mockito.doReturn;
 
  24 import static org.mockito.Mockito.mock;
 
  25 import static org.mockito.Mockito.reset;
 
  26 import static org.mockito.Mockito.verify;
 
  27 import static org.mockito.Mockito.when;
 
  29 import com.google.gson.Gson;
 
  30 import com.google.gson.JsonElement;
 
  32 import java.util.Optional;
 
  34 import javax.net.ssl.SSLException;
 
  36 import org.junit.Assert;
 
  37 import org.junit.jupiter.api.AfterEach;
 
  38 import org.junit.jupiter.api.BeforeAll;
 
  39 import org.junit.jupiter.api.Test;
 
  40 import org.onap.bbs.event.processor.config.ApplicationConfiguration;
 
  41 import org.onap.bbs.event.processor.exceptions.EmptyDmaapResponseException;
 
  42 import org.onap.bbs.event.processor.model.ImmutableReRegistrationConsumerDmaapModel;
 
  43 import org.onap.bbs.event.processor.model.ReRegistrationConsumerDmaapModel;
 
  44 import org.onap.bbs.event.processor.utilities.ReRegistrationDmaapConsumerJsonParser;
 
  45 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
 
  46 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapConsumerConfiguration;
 
  47 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.ConsumerReactiveHttpClientFactory;
 
  48 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.DMaaPConsumerReactiveHttpClient;
 
  50 import reactor.core.publisher.Mono;
 
  51 import reactor.test.StepVerifier;
 
  53 class DmaapReRegistrationConsumerTaskImplTest {
 
  55     private static final String RE_REGISTRATION_EVENT_TEMPLATE = "{\"event\": {"
 
  56             + "\"commonEventHeader\": { \"sourceName\":\"%s\"},"
 
  57             + "\"additionalFields\": {"
 
  58             + " \"attachment-point\": \"%s\","
 
  59             + " \"remote-id\": \"%s\","
 
  60             + " \"cvlan\": \"%s\","
 
  61             + " \"svlan\": \"%s\""
 
  64     private static DmaapReRegistrationConsumerTaskImpl dmaapConsumerTask;
 
  65     private static ReRegistrationConsumerDmaapModel reRegistrationConsumerDmaapModel;
 
  66     private static DMaaPConsumerReactiveHttpClient dMaaPConsumerReactiveHttpClient;
 
  67     private static String eventsArray;
 
  68     private static Gson gson = new Gson();
 
  71     static void setUp() throws SSLException {
 
  73         final String sourceName = "PNF-CorrelationId";
 
  74         final String attachmentPoint = "olt2/2/2";
 
  75         final String remoteId = "remoteId";
 
  76         final String cvlan = "1005";
 
  77         final String svlan = "100";
 
  79         // Mock Re-registration configuration
 
  80         DmaapConsumerConfiguration dmaapConsumerConfiguration = testVersionOfDmaapConsumerConfiguration();
 
  81         ApplicationConfiguration configuration = mock(ApplicationConfiguration.class);
 
  82         when(configuration.getDmaapReRegistrationConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration);
 
  84         // Mock reactive DMaaP client
 
  85         ConsumerReactiveHttpClientFactory httpClientFactory = mock(ConsumerReactiveHttpClientFactory.class);
 
  86         dMaaPConsumerReactiveHttpClient = mock(DMaaPConsumerReactiveHttpClient.class);
 
  87         doReturn(dMaaPConsumerReactiveHttpClient).when(httpClientFactory).create(dmaapConsumerConfiguration);
 
  89         dmaapConsumerTask = new DmaapReRegistrationConsumerTaskImpl(configuration,
 
  90                 new ReRegistrationDmaapConsumerJsonParser(), httpClientFactory);
 
  92         reRegistrationConsumerDmaapModel = ImmutableReRegistrationConsumerDmaapModel.builder()
 
  93                 .correlationId(sourceName)
 
  94                 .attachmentPoint(attachmentPoint)
 
 100         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE, sourceName, attachmentPoint, remoteId,
 
 103         eventsArray = "[" + event + "]";
 
 108         reset(dMaaPConsumerReactiveHttpClient);
 
 112     void passingEmptyMessage_NothingHappens() {
 
 113         JsonElement empty = gson.toJsonTree("");
 
 114         when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse(Optional.empty())).thenReturn(Mono.just(empty));
 
 116         StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
 
 117                 .expectSubscription()
 
 118                 .expectError(EmptyDmaapResponseException.class);
 
 119         verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse(Optional.empty());
 
 123     void passingNormalMessage_ResponseSucceeds() {
 
 124         JsonElement normalEventsArray = gson.toJsonTree(eventsArray);
 
 125         when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse(Optional.empty()))
 
 126                 .thenReturn(Mono.just(normalEventsArray));
 
 128         StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
 
 129                 .expectSubscription()
 
 130                 .consumeNextWith(e -> Assert.assertEquals(e, reRegistrationConsumerDmaapModel));
 
 131         verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse(Optional.empty());
 
 134     private static DmaapConsumerConfiguration testVersionOfDmaapConsumerConfiguration() {
 
 135         return new ImmutableDmaapConsumerConfiguration.Builder()
 
 136                 .consumerGroup("OpenDCAE-c12")
 
 138                 .dmaapContentType("application/json")
 
 139                 .dmaapHostName("message-router.onap.svc.cluster.local")
 
 140                 .dmaapPortNumber(3904)
 
 141                 .dmaapProtocol("http")
 
 142                 .dmaapUserName("admin")
 
 143                 .dmaapUserPassword("admin")
 
 144                 .trustStorePath("/opt/app/bbs/local/org.onap.bbs.trust.jks")
 
 145                 .trustStorePasswordPath("change_it")
 
 146                 .keyStorePath("/opt/app/bbs/local/org.onap.bbs.p12")
 
 147                 .keyStorePasswordPath("change_it")
 
 148                 .enableDmaapCertAuth(false)
 
 149                 .dmaapTopicName("/events/unauthenticated.PNF_REREGISTRATION")