d9989d1dbe53b993f2fde1868dd344ec28f4d9f3
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2018 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.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer;
22
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26
27 import io.netty.handler.ssl.SslContext;
28 import javax.net.ssl.SSLException;
29 import org.junit.jupiter.api.Assertions;
30 import org.junit.jupiter.api.Test;
31 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
32 import org.onap.dcaegen2.services.sdk.rest.services.ssl.SslFactory;
33 import org.springframework.web.reactive.function.client.WebClient;
34
35 /**
36  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/5/18
37  */
38 class DMaaPReactiveWebClientFactoryTest {
39
40     private static final String KEY_STORE = "keyStore";
41     private static final String KEY_STORE_PASS = "keyStorePass";
42     private static final String TRUST_STORE = "trustStore";
43     private static final String TRUST_STORE_PASS = "trustStorePass";
44     private SslFactory sslFactory = mock(SslFactory.class);
45     private SslContext dummySslContext = mock(SslContext.class);
46     private DMaaPReactiveWebClientFactory webClientFactory = new DMaaPReactiveWebClientFactory(sslFactory);
47
48     @Test
49     void builder_shouldBuildDMaaPReactiveWebClientwithInsecureSslContext() throws Exception {
50         //given
51         DmaapConsumerConfiguration dmaapConsumerConfiguration = givenDmaapConfigurationWithSslDisabled();
52
53         //when
54         WebClient dmaapReactiveWebClient = webClientFactory.build(dmaapConsumerConfiguration);
55
56         //then
57         Assertions.assertNotNull(dmaapReactiveWebClient);
58         verify(sslFactory).createInsecureContext();
59     }
60
61     @Test
62     void builder_shouldBuildDMaaPReactiveWebClientwithSecureSslContext() throws Exception {
63         //given
64         DmaapConsumerConfiguration dmaapConsumerConfiguration = givenDmaapConfigurationWithSslEnabled();
65
66         //when
67         WebClient dmaapReactiveWebClient = webClientFactory.build(dmaapConsumerConfiguration);
68
69         //then
70         Assertions.assertNotNull(dmaapReactiveWebClient);
71         verify(sslFactory).createSecureContext(KEY_STORE, KEY_STORE_PASS, TRUST_STORE, TRUST_STORE_PASS);
72     }
73
74     private DmaapConsumerConfiguration givenDmaapConfigurationWithSslDisabled() throws SSLException {
75         DmaapConsumerConfiguration dmaapConsumerConfiguration = mock(DmaapConsumerConfiguration.class);
76         when(dmaapConsumerConfiguration.enableDmaapCertAuth()).thenReturn(false);
77         when(sslFactory.createInsecureContext()).thenReturn(dummySslContext);
78         return dmaapConsumerConfiguration;
79     }
80
81     private DmaapConsumerConfiguration givenDmaapConfigurationWithSslEnabled() throws SSLException {
82         DmaapConsumerConfiguration dmaapConsumerConfiguration = mock(DmaapConsumerConfiguration.class);
83         when(dmaapConsumerConfiguration.enableDmaapCertAuth()).thenReturn(true);
84         when(dmaapConsumerConfiguration.keyStorePath()).thenReturn(KEY_STORE);
85         when(dmaapConsumerConfiguration.keyStorePasswordPath()).thenReturn(KEY_STORE_PASS);
86         when(dmaapConsumerConfiguration.trustStorePath()).thenReturn(TRUST_STORE);
87         when(dmaapConsumerConfiguration.trustStorePasswordPath()).thenReturn(TRUST_STORE_PASS);
88         when(sslFactory.createSecureContext(KEY_STORE, KEY_STORE_PASS, TRUST_STORE, TRUST_STORE_PASS))
89                 .thenReturn(dummySslContext);
90         return dmaapConsumerConfiguration;
91     }
92 }