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.CpeAuthenticationConsumerDmaapModel;
 
  43 import org.onap.bbs.event.processor.model.ImmutableCpeAuthenticationConsumerDmaapModel;
 
  44 import org.onap.bbs.event.processor.utilities.CpeAuthenticationDmaapConsumerJsonParser;
 
  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 DmaapCpeAuthenticationConsumerTaskImplTest {
 
  55     private static final String CPE_AUTHENTICATION_EVENT_TEMPLATE = "{\"event\": {"
 
  56             + "\"commonEventHeader\": { \"sourceName\":\"%s\"},"
 
  57             + "\"stateChangeFields\": {"
 
  58             + " \"oldState\": \"%s\","
 
  59             + " \"newState\": \"%s\","
 
  60             + " \"stateInterface\": \"%s\","
 
  61             + " \"additionalFields\": {"
 
  62             + "   \"macAddress\": \"%s\","
 
  63             + "   \"swVersion\": \"%s\""
 
  66     private static DmaapCpeAuthenticationConsumerTask dmaapConsumerTask;
 
  67     private static CpeAuthenticationConsumerDmaapModel cpeAuthenticationConsumerDmaapModel;
 
  68     private static DMaaPConsumerReactiveHttpClient dMaaPConsumerReactiveHttpClient;
 
  69     private static String eventsArray;
 
  70     private static Gson gson = new Gson();
 
  73     static void setUp() throws SSLException {
 
  75         final String sourceName = "PNF-CorrelationId";
 
  76         final String oldAuthenticationState = "outOfService";
 
  77         final String newAuthenticationState = "inService";
 
  78         final String stateInterface = "stateInterface";
 
  79         final String rgwMacAddress = "00:0a:95:8d:78:16";
 
  80         final String swVersion = "1.2";
 
  82         // Mock Re-registration configuration
 
  83         DmaapConsumerConfiguration dmaapConsumerConfiguration = testVersionOfDmaapConsumerConfiguration();
 
  84         ApplicationConfiguration configuration = mock(ApplicationConfiguration.class);
 
  85         when(configuration.getDmaapCpeAuthenticationConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration);
 
  87         // Mock reactive DMaaP client
 
  88         ConsumerReactiveHttpClientFactory httpClientFactory = mock(ConsumerReactiveHttpClientFactory.class);
 
  89         dMaaPConsumerReactiveHttpClient = mock(DMaaPConsumerReactiveHttpClient.class);
 
  90         doReturn(dMaaPConsumerReactiveHttpClient).when(httpClientFactory).create(dmaapConsumerConfiguration);
 
  92         dmaapConsumerTask = new DmaapCpeAuthenticationConsumerTaskImpl(configuration,
 
  93                 new CpeAuthenticationDmaapConsumerJsonParser(), httpClientFactory);
 
  95         cpeAuthenticationConsumerDmaapModel = ImmutableCpeAuthenticationConsumerDmaapModel.builder()
 
  96                 .correlationId(sourceName)
 
  97                 .oldAuthenticationState(oldAuthenticationState)
 
  98                 .newAuthenticationState(newAuthenticationState)
 
  99                 .stateInterface(stateInterface)
 
 100                 .rgwMacAddress(rgwMacAddress)
 
 101                 .swVersion(swVersion)
 
 104         String event = String.format(CPE_AUTHENTICATION_EVENT_TEMPLATE, sourceName, oldAuthenticationState,
 
 105                 newAuthenticationState, stateInterface, rgwMacAddress, swVersion);
 
 107         eventsArray = "[" + event + "]";
 
 112         reset(dMaaPConsumerReactiveHttpClient);
 
 116     void passingEmptyMessage_NothingHappens() throws Exception {
 
 117         JsonElement empty = gson.toJsonTree("");
 
 118         when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse(Optional.empty())).thenReturn(Mono.just(empty));
 
 120         StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
 
 121                 .expectSubscription()
 
 122                 .expectError(EmptyDmaapResponseException.class);
 
 123         verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse(Optional.empty());
 
 127     void passingNormalMessage_ResponseSucceeds() throws Exception {
 
 128         JsonElement normalEventsArray = gson.toJsonTree(eventsArray);
 
 129         when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse(Optional.empty()))
 
 130                 .thenReturn(Mono.just(normalEventsArray));
 
 132         StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
 
 133                 .expectSubscription()
 
 134                 .consumeNextWith(e -> Assert.assertEquals(e, cpeAuthenticationConsumerDmaapModel));
 
 135         verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse(Optional.empty());
 
 138     private static DmaapConsumerConfiguration testVersionOfDmaapConsumerConfiguration() {
 
 139         return new ImmutableDmaapConsumerConfiguration.Builder()
 
 140                 .consumerGroup("consumer-group")
 
 141                 .consumerId("consumer-id")
 
 142                 .dmaapContentType("application/json")
 
 143                 .dmaapHostName("message-router.onap.svc.cluster.local")
 
 144                 .dmaapPortNumber(3904)
 
 145                 .dmaapProtocol("http")
 
 146                 .dmaapUserName("admin")
 
 147                 .dmaapUserPassword("admin")
 
 148                 .trustStorePath("change it")
 
 149                 .trustStorePasswordPath("change_it")
 
 150                 .keyStorePath("change it")
 
 151                 .keyStorePasswordPath("change_it")
 
 152                 .enableDmaapCertAuth(false)
 
 153                 .dmaapTopicName("/events/unauthenticated.CPE_AUTHENTICATION")