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.ArgumentMatchers.any;
 
  24 import static org.mockito.Mockito.doReturn;
 
  25 import static org.mockito.Mockito.mock;
 
  26 import static org.mockito.Mockito.times;
 
  27 import static org.mockito.Mockito.verify;
 
  28 import static org.mockito.Mockito.verifyNoMoreInteractions;
 
  29 import static org.mockito.Mockito.when;
 
  31 import java.util.LinkedHashMap;
 
  33 import java.util.Optional;
 
  35 import javax.net.ssl.SSLException;
 
  37 import org.junit.jupiter.api.Assertions;
 
  38 import org.junit.jupiter.api.BeforeAll;
 
  39 import org.junit.jupiter.api.Test;
 
  40 import org.junit.jupiter.api.function.Executable;
 
  41 import org.onap.bbs.event.processor.config.ApplicationConfiguration;
 
  42 import org.onap.bbs.event.processor.exceptions.DmaapException;
 
  43 import org.onap.bbs.event.processor.model.ControlLoopPublisherDmaapModel;
 
  44 import org.onap.bbs.event.processor.model.ImmutableControlLoopPublisherDmaapModel;
 
  45 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse;
 
  46 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
 
  47 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapPublisherConfiguration;
 
  48 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer.DMaaPPublisherReactiveHttpClient;
 
  49 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.producer.PublisherReactiveHttpClientFactory;
 
  50 import org.springframework.http.HttpStatus;
 
  52 import reactor.core.publisher.Mono;
 
  53 import reactor.test.StepVerifier;
 
  55 class DmaapPublisherTaskImplTest {
 
  57     private static ControlLoopPublisherDmaapModel controlLoopPublisherDmaapModel;
 
  58     private static DmaapPublisherTaskImpl task;
 
  59     private static DMaaPPublisherReactiveHttpClient reactiveHttpClient;
 
  60     private static ApplicationConfiguration configuration;
 
  61     private static DmaapPublisherConfiguration dmaapPublisherConfiguration;
 
  65         dmaapPublisherConfiguration = testVersionOfDmaapPublisherConfiguration();
 
  66         configuration = mock(ApplicationConfiguration.class);
 
  68         final String closedLoopEventClient = "DCAE.BBS_mSInstance";
 
  69         final String policyVersion = "1.0.0.5";
 
  70         final String policyName = "CPE_Authentication";
 
  71         final String policyScope =
 
  72                 "service=HSIAService,type=SampleType,"
 
  73                         + "closedLoopControlName=CL-CPE_A-d925ed73-8231-4d02-9545-db4e101f88f8";
 
  74         final String targetType = "VM";
 
  75         final long closedLoopAlarmStart = 1484677482204798L;
 
  76         final String closedLoopEventStatus = "ONSET";
 
  77         final String closedLoopControlName = "ControlLoop-CPE_A-2179b738-fd36-4843-a71a-a8c24c70c88b";
 
  78         final String version = "1.0.2";
 
  79         final String target = "vserver.vserver-name";
 
  80         final String requestId = "97964e10-686e-4790-8c45-bdfa61df770f";
 
  81         final String from = "DCAE";
 
  83         final Map<String, String> aaiEnrichmentData = new LinkedHashMap<>();
 
  84         aaiEnrichmentData.put("service-information.service-instance-id", "service-instance-id-example");
 
  85         aaiEnrichmentData.put("cvlan-id", "example cvlan-id");
 
  86         aaiEnrichmentData.put("svlan-id", "example svlan-id");
 
  88         controlLoopPublisherDmaapModel = ImmutableControlLoopPublisherDmaapModel.builder()
 
  89                 .closedLoopEventClient(closedLoopEventClient)
 
  90                 .policyVersion(policyVersion)
 
  91                 .policyName(policyName)
 
  92                 .policyScope(policyScope)
 
  93                 .targetType(targetType)
 
  94                 .aaiEnrichmentData(aaiEnrichmentData)
 
  95                 .closedLoopAlarmStart(closedLoopAlarmStart)
 
  96                 .closedLoopEventStatus(closedLoopEventStatus)
 
  97                 .closedLoopControlName(closedLoopControlName)
 
 100                 .requestId(requestId)
 
 104         when(configuration.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration);
 
 108     void passingNullMessage_ExceptionIsRaised() {
 
 110         task = new DmaapPublisherTaskImpl(configuration);
 
 112         Executable executableFunction = () -> task.execute(null);
 
 114         Assertions.assertThrows(DmaapException.class, executableFunction, "Input message is invalid");
 
 118     void passingNormalMessage_ReactiveClientProcessesIt() throws DmaapException, SSLException {
 
 119         HttpResponse response = setupMocks(HttpStatus.OK.value());
 
 121         StepVerifier.create(task.execute(controlLoopPublisherDmaapModel)).expectSubscription()
 
 122                 .expectNext(response).verifyComplete();
 
 124         verify(reactiveHttpClient, times(1))
 
 125                 .getDMaaPProducerResponse(controlLoopPublisherDmaapModel, Optional.empty());
 
 126         verifyNoMoreInteractions(reactiveHttpClient);
 
 130     void passingNormalMessage_IncorrectResponseIsHandled() throws DmaapException, SSLException {
 
 131         HttpResponse response = setupMocks(HttpStatus.UNAUTHORIZED.value());
 
 133         StepVerifier.create(task.execute(controlLoopPublisherDmaapModel)).expectSubscription()
 
 134                 .expectNext(response).verifyComplete();
 
 136         verify(reactiveHttpClient, times(1))
 
 137                 .getDMaaPProducerResponse(controlLoopPublisherDmaapModel, Optional.empty());
 
 138         verifyNoMoreInteractions(reactiveHttpClient);
 
 141     // We can safely suppress unchecked assignment warning here since it is a mock class
 
 142     @SuppressWarnings("unchecked")
 
 143     private HttpResponse setupMocks(Integer httpResponseCode) throws SSLException {
 
 145         HttpResponse response = mock(HttpResponse.class);
 
 146         when(response.statusCode()).thenReturn(httpResponseCode);
 
 148         reactiveHttpClient = mock(DMaaPPublisherReactiveHttpClient.class);
 
 149         when(reactiveHttpClient.getDMaaPProducerResponse(any(), any(Optional.class)))
 
 150                 .thenReturn(Mono.just(response));
 
 152         PublisherReactiveHttpClientFactory httpClientFactory = mock(PublisherReactiveHttpClientFactory.class);
 
 153         doReturn(reactiveHttpClient).when(httpClientFactory).create(dmaapPublisherConfiguration);
 
 155         task = new DmaapPublisherTaskImpl(configuration, httpClientFactory);
 
 160     private static DmaapPublisherConfiguration testVersionOfDmaapPublisherConfiguration() {
 
 161         return new ImmutableDmaapPublisherConfiguration.Builder()
 
 162                 .dmaapContentType("application/json")
 
 163                 .dmaapHostName("message-router.onap.svc.cluster.local")
 
 164                 .dmaapPortNumber(3904)
 
 165                 .dmaapProtocol("http")
 
 166                 .dmaapUserName("admin")
 
 167                 .dmaapUserPassword("admin")
 
 168                 .trustStorePath("/opt/app/bbs/local/org.onap.bbs.trust.jks")
 
 169                 .trustStorePasswordPath("change_it")
 
 170                 .keyStorePath("/opt/app/bbs/local/org.onap.bbs.p12")
 
 171                 .keyStorePasswordPath("change_it")
 
 172                 .enableDmaapCertAuth(false)
 
 173                 .dmaapTopicName("/events/unauthenticated.DCAE_CL_OUTPUT")