9d670c65c7f984d15b89732d012cb8d44c7037dd
[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.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import io.netty.handler.ssl.SslContext;
31 import org.junit.jupiter.api.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
34 import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.CloudHttpClient;
35 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.utlis.SecurityKeysUtil;
36 import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys;
37 import org.onap.dcaegen2.services.sdk.security.ssl.SslFactory;
38
39
40 /**
41  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/5/18
42  */
43 class DMaaPReactiveWebClientFactoryTest {
44
45     private static final String KEY_STORE_RESOURCE_PATH = "/org.onap.dcae.jks";
46     private static final String KEY_STORE_PASS_RESOURCE_PATH = "/keystore.password";
47     private static final String TRUST_STORE_RESOURCE_PATH = "/org.onap.dcae.trust.jks";
48     private static final String TRUST_STORE_PASS_RESOURCE_PATH = "/truststore.password";
49     private SslFactory sslFactory = mock(SslFactory.class);
50     private SslContext dummySslContext = mock(SslContext.class);
51     private DMaaPReactiveWebClientFactory webClientFactory = new DMaaPReactiveWebClientFactory(sslFactory);
52     private ArgumentCaptor<SecurityKeys> securityKeysArgumentCaptor = ArgumentCaptor
53             .forClass(SecurityKeys.class);
54
55     @Test
56     void builder_shouldBuildDMaaPReactiveWebClientwithInsecureSslContext(){
57         //given
58         DmaapConsumerConfiguration dmaapConsumerConfiguration = givenDmaapConfigurationWithSslDisabled();
59
60         //when
61         CloudHttpClient dmaapReactiveWebClient = webClientFactory.build(dmaapConsumerConfiguration);
62
63         //then
64         assertNotNull(dmaapReactiveWebClient);
65         verify(sslFactory).createInsecureClientContext();
66     }
67
68     @Test
69     void builder_shouldBuildDMaaPReactiveWebClientwithSecureSslContext(){
70         //given
71         DmaapConsumerConfiguration dmaapConsumerConfiguration = givenDmaapConfigurationWithSslEnabled();
72         SecurityKeys givenKeys = SecurityKeysUtil.fromDmappCustomConfig(dmaapConsumerConfiguration);
73
74         //when
75         CloudHttpClient dmaapReactiveWebClient = webClientFactory.build(dmaapConsumerConfiguration);
76
77         //then
78         assertNotNull(dmaapReactiveWebClient);
79
80         verify(sslFactory).createSecureClientContext(securityKeysArgumentCaptor.capture());
81
82         SecurityKeys capturedKeys = securityKeysArgumentCaptor.getValue();
83
84         assertEquals(capturedKeys.keyStore().path(), givenKeys.keyStore().path());
85         assertEquals(capturedKeys.keyStorePassword().toString(), givenKeys.keyStorePassword().toString());
86         assertEquals(capturedKeys.trustStore().path(), givenKeys.trustStore().path());
87         assertEquals(capturedKeys.trustStorePassword().toString(), givenKeys.trustStorePassword().toString());
88     }
89
90     private DmaapConsumerConfiguration givenDmaapConfigurationWithSslDisabled(){
91         DmaapConsumerConfiguration dmaapConsumerConfiguration = mock(DmaapConsumerConfiguration.class);
92         when(dmaapConsumerConfiguration.enableDmaapCertAuth()).thenReturn(false);
93         when(sslFactory.createInsecureClientContext()).thenReturn(dummySslContext);
94         return dmaapConsumerConfiguration;
95     }
96
97     private DmaapConsumerConfiguration givenDmaapConfigurationWithSslEnabled(){
98         DmaapConsumerConfiguration dmaapConsumerConfiguration = mock(DmaapConsumerConfiguration.class);
99
100         when(dmaapConsumerConfiguration.enableDmaapCertAuth()).thenReturn(true);
101         when(dmaapConsumerConfiguration.keyStorePath()).thenReturn(KEY_STORE_RESOURCE_PATH);
102         when(dmaapConsumerConfiguration.keyStorePasswordPath()).thenReturn(KEY_STORE_PASS_RESOURCE_PATH);
103         when(dmaapConsumerConfiguration.trustStorePath()).thenReturn(TRUST_STORE_RESOURCE_PATH);
104         when(dmaapConsumerConfiguration.trustStorePasswordPath()).thenReturn(TRUST_STORE_PASS_RESOURCE_PATH);
105
106         when(sslFactory.createSecureClientContext(any(SecurityKeys.class))).thenReturn(dummySslContext);
107
108         return dmaapConsumerConfiguration;
109     }
110 }