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