40bcb65dd8c87395d50a8d5fc44a0c8d366b7b2d
[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.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;
28
29 import com.google.gson.Gson;
30 import com.google.gson.JsonElement;
31
32 import java.util.Optional;
33
34 import javax.net.ssl.SSLException;
35
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;
49
50 import reactor.core.publisher.Mono;
51 import reactor.test.StepVerifier;
52
53 class DmaapCpeAuthenticationConsumerTaskImplTest {
54
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\""
64             + "}}}}";
65
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();
71
72     @BeforeAll
73     static void setUp() throws SSLException {
74
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";
81
82         // Mock Re-registration configuration
83         DmaapConsumerConfiguration dmaapConsumerConfiguration = testVersionOfDmaapConsumerConfiguration();
84         ApplicationConfiguration configuration = mock(ApplicationConfiguration.class);
85         when(configuration.getDmaapCpeAuthenticationConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration);
86
87         // Mock reactive DMaaP client
88         ConsumerReactiveHttpClientFactory httpClientFactory = mock(ConsumerReactiveHttpClientFactory.class);
89         dMaaPConsumerReactiveHttpClient = mock(DMaaPConsumerReactiveHttpClient.class);
90         doReturn(dMaaPConsumerReactiveHttpClient).when(httpClientFactory).create(dmaapConsumerConfiguration);
91
92         dmaapConsumerTask = new DmaapCpeAuthenticationConsumerTaskImpl(configuration,
93                 new CpeAuthenticationDmaapConsumerJsonParser(), httpClientFactory);
94
95         cpeAuthenticationConsumerDmaapModel = ImmutableCpeAuthenticationConsumerDmaapModel.builder()
96                 .correlationId(sourceName)
97                 .oldAuthenticationState(oldAuthenticationState)
98                 .newAuthenticationState(newAuthenticationState)
99                 .stateInterface(stateInterface)
100                 .rgwMacAddress(rgwMacAddress)
101                 .swVersion(swVersion)
102                 .build();
103
104         String event = String.format(CPE_AUTHENTICATION_EVENT_TEMPLATE, sourceName, oldAuthenticationState,
105                 newAuthenticationState, stateInterface, rgwMacAddress, swVersion);
106
107         eventsArray = "[" + event + "]";
108     }
109
110     @AfterEach
111     void resetMock() {
112         reset(dMaaPConsumerReactiveHttpClient);
113     }
114
115     @Test
116     void passingEmptyMessage_NothingHappens() throws Exception {
117         JsonElement empty = gson.toJsonTree("");
118         when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse(Optional.empty())).thenReturn(Mono.just(empty));
119
120         StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
121                 .expectSubscription()
122                 .expectError(EmptyDmaapResponseException.class);
123         verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse(Optional.empty());
124     }
125
126     @Test
127     void passingNormalMessage_ResponseSucceeds() throws Exception {
128         JsonElement normalEventsArray = gson.toJsonTree(eventsArray);
129         when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse(Optional.empty()))
130                 .thenReturn(Mono.just(normalEventsArray));
131
132         StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
133                 .expectSubscription()
134                 .consumeNextWith(e -> Assert.assertEquals(e, cpeAuthenticationConsumerDmaapModel));
135         verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse(Optional.empty());
136     }
137
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")
154                 .timeoutMs(-1)
155                 .messageLimit(-1)
156                 .build();
157     }
158 }