436206d2306ab674d7451e02e719409c5e894fe2
[dcaegen2/services.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.bbs.event.processor.tasks;
22
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;
30
31 import java.util.LinkedHashMap;
32 import java.util.Map;
33 import java.util.Optional;
34
35 import javax.net.ssl.SSLException;
36
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;
51
52 import reactor.core.publisher.Mono;
53 import reactor.test.StepVerifier;
54
55 class DmaapPublisherTaskImplTest {
56
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;
62
63     @BeforeAll
64     static void setUp() {
65         dmaapPublisherConfiguration = testVersionOfDmaapPublisherConfiguration();
66         configuration = mock(ApplicationConfiguration.class);
67
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";
82
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");
87
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)
98                 .version(version)
99                 .target(target)
100                 .requestId(requestId)
101                 .originator(from)
102                 .build();
103
104         when(configuration.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration);
105     }
106
107     @Test
108     void passingNullMessage_ExceptionIsRaised() {
109
110         task = new DmaapPublisherTaskImpl(configuration);
111
112         Executable executableFunction = () -> task.execute(null);
113
114         Assertions.assertThrows(DmaapException.class, executableFunction, "Input message is invalid");
115     }
116
117     @Test
118     void passingNormalMessage_ReactiveClientProcessesIt() throws DmaapException, SSLException {
119         HttpResponse response = setupMocks(HttpStatus.OK.value());
120
121         StepVerifier.create(task.execute(controlLoopPublisherDmaapModel)).expectSubscription()
122                 .expectNext(response).verifyComplete();
123
124         verify(reactiveHttpClient, times(1))
125                 .getDMaaPProducerResponse(controlLoopPublisherDmaapModel, Optional.empty());
126         verifyNoMoreInteractions(reactiveHttpClient);
127     }
128
129     @Test
130     void passingNormalMessage_IncorrectResponseIsHandled() throws DmaapException, SSLException {
131         HttpResponse response = setupMocks(HttpStatus.UNAUTHORIZED.value());
132
133         StepVerifier.create(task.execute(controlLoopPublisherDmaapModel)).expectSubscription()
134                 .expectNext(response).verifyComplete();
135
136         verify(reactiveHttpClient, times(1))
137                 .getDMaaPProducerResponse(controlLoopPublisherDmaapModel, Optional.empty());
138         verifyNoMoreInteractions(reactiveHttpClient);
139     }
140
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 {
144
145         HttpResponse response = mock(HttpResponse.class);
146         when(response.statusCode()).thenReturn(httpResponseCode);
147
148         reactiveHttpClient = mock(DMaaPPublisherReactiveHttpClient.class);
149         when(reactiveHttpClient.getDMaaPProducerResponse(any(), any(Optional.class)))
150                 .thenReturn(Mono.just(response));
151
152         PublisherReactiveHttpClientFactory httpClientFactory = mock(PublisherReactiveHttpClientFactory.class);
153         doReturn(reactiveHttpClient).when(httpClientFactory).create(dmaapPublisherConfiguration);
154
155         task = new DmaapPublisherTaskImpl(configuration, httpClientFactory);
156
157         return response;
158     }
159
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")
174                 .build();
175     }
176 }