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