72e28987a24be0c0f947c4bcc49bb2fa90a2fc80
[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.ImmutableReRegistrationConsumerDmaapModel;
43 import org.onap.bbs.event.processor.model.ReRegistrationConsumerDmaapModel;
44 import org.onap.bbs.event.processor.utilities.ReRegistrationDmaapConsumerJsonParser;
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 DmaapReRegistrationConsumerTaskImplTest {
54
55     private static final String RE_REGISTRATION_EVENT_TEMPLATE = "{\"event\": {"
56             + "\"commonEventHeader\": { \"sourceName\":\"%s\"},"
57             + "\"additionalFields\": {"
58             + " \"attachment-point\": \"%s\","
59             + " \"remote-id\": \"%s\","
60             + " \"cvlan\": \"%s\","
61             + " \"svlan\": \"%s\""
62             + "}}}";
63
64     private static DmaapReRegistrationConsumerTaskImpl dmaapConsumerTask;
65     private static ReRegistrationConsumerDmaapModel reRegistrationConsumerDmaapModel;
66     private static DMaaPConsumerReactiveHttpClient dMaaPConsumerReactiveHttpClient;
67     private static String eventsArray;
68     private static Gson gson = new Gson();
69
70     @BeforeAll
71     static void setUp() throws SSLException {
72
73         final String sourceName = "PNF-CorrelationId";
74         final String attachmentPoint = "olt2/2/2";
75         final String remoteId = "remoteId";
76         final String cvlan = "1005";
77         final String svlan = "100";
78
79         // Mock Re-registration configuration
80         DmaapConsumerConfiguration dmaapConsumerConfiguration = testVersionOfDmaapConsumerConfiguration();
81         ApplicationConfiguration configuration = mock(ApplicationConfiguration.class);
82         when(configuration.getDmaapReRegistrationConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration);
83
84         // Mock reactive DMaaP client
85         ConsumerReactiveHttpClientFactory httpClientFactory = mock(ConsumerReactiveHttpClientFactory.class);
86         dMaaPConsumerReactiveHttpClient = mock(DMaaPConsumerReactiveHttpClient.class);
87         doReturn(dMaaPConsumerReactiveHttpClient).when(httpClientFactory).create(dmaapConsumerConfiguration);
88
89         dmaapConsumerTask = new DmaapReRegistrationConsumerTaskImpl(configuration,
90                 new ReRegistrationDmaapConsumerJsonParser(), httpClientFactory);
91
92         reRegistrationConsumerDmaapModel = ImmutableReRegistrationConsumerDmaapModel.builder()
93                 .correlationId(sourceName)
94                 .attachmentPoint(attachmentPoint)
95                 .remoteId(remoteId)
96                 .cVlan(cvlan)
97                 .sVlan(svlan)
98                 .build();
99
100         String event = String.format(RE_REGISTRATION_EVENT_TEMPLATE, sourceName, attachmentPoint, remoteId,
101                 cvlan, svlan);
102
103         eventsArray = "[" + event + "]";
104     }
105
106     @AfterEach
107     void resetMock() {
108         reset(dMaaPConsumerReactiveHttpClient);
109     }
110
111     @Test
112     void passingEmptyMessage_NothingHappens() {
113         JsonElement empty = gson.toJsonTree("");
114         when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse(Optional.empty())).thenReturn(Mono.just(empty));
115
116         StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
117                 .expectSubscription()
118                 .expectError(EmptyDmaapResponseException.class);
119         verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse(Optional.empty());
120     }
121
122     @Test
123     void passingNormalMessage_ResponseSucceeds() {
124         JsonElement normalEventsArray = gson.toJsonTree(eventsArray);
125         when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse(Optional.empty()))
126                 .thenReturn(Mono.just(normalEventsArray));
127
128         StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
129                 .expectSubscription()
130                 .consumeNextWith(e -> Assert.assertEquals(e, reRegistrationConsumerDmaapModel));
131         verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse(Optional.empty());
132     }
133
134     private static DmaapConsumerConfiguration testVersionOfDmaapConsumerConfiguration() {
135         return new ImmutableDmaapConsumerConfiguration.Builder()
136                 .consumerGroup("OpenDCAE-c12")
137                 .consumerId("c12")
138                 .dmaapContentType("application/json")
139                 .dmaapHostName("message-router.onap.svc.cluster.local")
140                 .dmaapPortNumber(3904)
141                 .dmaapProtocol("http")
142                 .dmaapUserName("admin")
143                 .dmaapUserPassword("admin")
144                 .trustStorePath("/opt/app/bbs/local/org.onap.bbs.trust.jks")
145                 .trustStorePasswordPath("change_it")
146                 .keyStorePath("/opt/app/bbs/local/org.onap.bbs.p12")
147                 .keyStorePasswordPath("change_it")
148                 .enableDmaapCertAuth(false)
149                 .dmaapTopicName("/events/unauthenticated.PNF_REREGISTRATION")
150                 .timeoutMs(-1)
151                 .messageLimit(-1)
152                 .build();
153     }
154 }