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 javax.net.ssl.SSLException;
 
  31 import org.junit.Assert;
 
  32 import org.junit.jupiter.api.AfterEach;
 
  33 import org.junit.jupiter.api.BeforeAll;
 
  34 import org.junit.jupiter.api.Test;
 
  35 import org.onap.bbs.event.processor.config.ApplicationConfiguration;
 
  36 import org.onap.bbs.event.processor.exceptions.EmptyDmaapResponseException;
 
  37 import org.onap.bbs.event.processor.model.CpeAuthenticationConsumerDmaapModel;
 
  38 import org.onap.bbs.event.processor.model.ImmutableCpeAuthenticationConsumerDmaapModel;
 
  39 import org.onap.bbs.event.processor.utilities.CpeAuthenticationDmaapConsumerJsonParser;
 
  40 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
 
  41 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapConsumerConfiguration;
 
  42 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.ConsumerReactiveHttpClientFactory;
 
  43 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.DMaaPConsumerReactiveHttpClient;
 
  45 import reactor.core.publisher.Mono;
 
  46 import reactor.test.StepVerifier;
 
  48 class DmaapCpeAuthenticationConsumerTaskImplTest {
 
  50     private static final String CPE_AUTHENTICATION_EVENT_TEMPLATE = "{\"event\": {"
 
  51             + "\"commonEventHeader\": { \"sourceName\":\"%s\"},"
 
  52             + "\"stateChangeFields\": {"
 
  53             + " \"oldState\": \"%s\","
 
  54             + " \"newState\": \"%s\","
 
  55             + " \"stateInterface\": \"%s\","
 
  56             + " \"additionalFields\": {"
 
  57             + "   \"macAddress\": \"%s\","
 
  58             + "   \"swVersion\": \"%s\""
 
  61     private static DmaapCpeAuthenticationConsumerTask dmaapConsumerTask;
 
  62     private static CpeAuthenticationConsumerDmaapModel cpeAuthenticationConsumerDmaapModel;
 
  63     private static DMaaPConsumerReactiveHttpClient dMaaPConsumerReactiveHttpClient;
 
  64     private static String eventsArray;
 
  67     static void setUp() throws SSLException {
 
  69         final String sourceName = "PNF-CorrelationId";
 
  70         final String oldAuthenticationState = "outOfService";
 
  71         final String newAuthenticationState = "inService";
 
  72         final String stateInterface = "stateInterface";
 
  73         final String rgwMacAddress = "00:0a:95:8d:78:16";
 
  74         final String swVersion = "1.2";
 
  76         // Mock Re-registration configuration
 
  77         DmaapConsumerConfiguration dmaapConsumerConfiguration = testVersionOfDmaapConsumerConfiguration();
 
  78         ApplicationConfiguration configuration = mock(ApplicationConfiguration.class);
 
  79         when(configuration.getDmaapCpeAuthenticationConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration);
 
  81         // Mock reactive DMaaP client
 
  82         ConsumerReactiveHttpClientFactory httpClientFactory = mock(ConsumerReactiveHttpClientFactory.class);
 
  83         dMaaPConsumerReactiveHttpClient = mock(DMaaPConsumerReactiveHttpClient.class);
 
  84         doReturn(dMaaPConsumerReactiveHttpClient).when(httpClientFactory).create(dmaapConsumerConfiguration);
 
  86         dmaapConsumerTask = new DmaapCpeAuthenticationConsumerTaskImpl(configuration,
 
  87                 new CpeAuthenticationDmaapConsumerJsonParser(), httpClientFactory);
 
  89         cpeAuthenticationConsumerDmaapModel = ImmutableCpeAuthenticationConsumerDmaapModel.builder()
 
  90                 .correlationId(sourceName)
 
  91                 .oldAuthenticationState(oldAuthenticationState)
 
  92                 .newAuthenticationState(newAuthenticationState)
 
  93                 .stateInterface(stateInterface)
 
  94                 .rgwMacAddress(rgwMacAddress)
 
  98         String event = String.format(CPE_AUTHENTICATION_EVENT_TEMPLATE, sourceName, oldAuthenticationState,
 
  99                 newAuthenticationState, stateInterface, rgwMacAddress, swVersion);
 
 101         eventsArray = "[" + event + "]";
 
 106         reset(dMaaPConsumerReactiveHttpClient);
 
 110     void passingEmptyMessage_NothingHappens() throws Exception {
 
 111         when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse()).thenReturn(Mono.just(""));
 
 113         StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
 
 114                 .expectSubscription()
 
 115                 .expectError(EmptyDmaapResponseException.class);
 
 116         verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse();
 
 120     void passingNormalMessage_ResponseSucceeds() throws Exception {
 
 121         when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse()).thenReturn(Mono.just(eventsArray));
 
 123         StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
 
 124                 .expectSubscription()
 
 125                 .consumeNextWith(e -> Assert.assertEquals(e, cpeAuthenticationConsumerDmaapModel));
 
 126         verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse();
 
 129     private static DmaapConsumerConfiguration testVersionOfDmaapConsumerConfiguration() {
 
 130         return new ImmutableDmaapConsumerConfiguration.Builder()
 
 131                 .consumerGroup("consumer-group")
 
 132                 .consumerId("consumer-id")
 
 133                 .dmaapContentType("application/json")
 
 134                 .dmaapHostName("message-router.onap.svc.cluster.local")
 
 135                 .dmaapPortNumber(3904)
 
 136                 .dmaapProtocol("http")
 
 137                 .dmaapUserName("admin")
 
 138                 .dmaapUserPassword("admin")
 
 139                 .trustStorePath("change it")
 
 140                 .trustStorePasswordPath("change_it")
 
 141                 .keyStorePath("change it")
 
 142                 .keyStorePasswordPath("change_it")
 
 143                 .enableDmaapCertAuth(false)
 
 144                 .dmaapTopicName("/events/unauthenticated.CPE_AUTHENTICATION")