6fd220074c84827469b2d1260e6a339460108618
[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.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient;
34
35
36 /**
37  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/5/18
38  */
39 class DMaaPReactiveWebClientFactoryTest {
40
41     private static final String KEY_STORE = "keyStore";
42     private static final String KEY_STORE_PASS = "keyStorePass";
43     private static final String TRUST_STORE = "trustStore";
44     private static final String TRUST_STORE_PASS = "trustStorePass";
45     private SslFactory sslFactory = mock(SslFactory.class);
46     private SslContext dummySslContext = mock(SslContext.class);
47     private DMaaPReactiveWebClientFactory webClientFactory = new DMaaPReactiveWebClientFactory(sslFactory);
48
49     @Test
50     void builder_shouldBuildDMaaPReactiveWebClientwithInsecureSslContext() throws Exception {
51         //given
52         DmaapConsumerConfiguration dmaapConsumerConfiguration = givenDmaapConfigurationWithSslDisabled();
53
54         //when
55         CloudHttpClient dmaapReactiveWebClient = webClientFactory.build(dmaapConsumerConfiguration);
56
57         //then
58         Assertions.assertNotNull(dmaapReactiveWebClient);
59         verify(sslFactory).createInsecureContext();
60     }
61
62     @Test
63     void builder_shouldBuildDMaaPReactiveWebClientwithSecureSslContext() throws Exception {
64         //given
65         DmaapConsumerConfiguration dmaapConsumerConfiguration = givenDmaapConfigurationWithSslEnabled();
66
67         //when
68         CloudHttpClient dmaapReactiveWebClient = webClientFactory.build(dmaapConsumerConfiguration);
69
70         //then
71         Assertions.assertNotNull(dmaapReactiveWebClient);
72         verify(sslFactory).createSecureContext(KEY_STORE, KEY_STORE_PASS, TRUST_STORE, TRUST_STORE_PASS);
73     }
74
75     private DmaapConsumerConfiguration givenDmaapConfigurationWithSslDisabled() throws SSLException {
76         DmaapConsumerConfiguration dmaapConsumerConfiguration = mock(DmaapConsumerConfiguration.class);
77         when(dmaapConsumerConfiguration.enableDmaapCertAuth()).thenReturn(false);
78         when(sslFactory.createInsecureContext()).thenReturn(dummySslContext);
79         return dmaapConsumerConfiguration;
80     }
81
82     private DmaapConsumerConfiguration givenDmaapConfigurationWithSslEnabled() throws SSLException {
83         DmaapConsumerConfiguration dmaapConsumerConfiguration = mock(DmaapConsumerConfiguration.class);
84         when(dmaapConsumerConfiguration.enableDmaapCertAuth()).thenReturn(true);
85         when(dmaapConsumerConfiguration.keyStorePath()).thenReturn(KEY_STORE);
86         when(dmaapConsumerConfiguration.keyStorePasswordPath()).thenReturn(KEY_STORE_PASS);
87         when(dmaapConsumerConfiguration.trustStorePath()).thenReturn(TRUST_STORE);
88         when(dmaapConsumerConfiguration.trustStorePasswordPath()).thenReturn(TRUST_STORE_PASS);
89         when(sslFactory.createSecureContext(KEY_STORE, KEY_STORE_PASS, TRUST_STORE, TRUST_STORE_PASS))
90                 .thenReturn(dummySslContext);
91         return dmaapConsumerConfiguration;
92     }
93 }