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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer;
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;
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;
41 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/5/18
43 class DMaaPReactiveWebClientFactoryTest {
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);
56 void builder_shouldBuildDMaaPReactiveWebClientwithInsecureSslContext(){
58 DmaapConsumerConfiguration dmaapConsumerConfiguration = givenDmaapConfigurationWithSslDisabled();
61 CloudHttpClient dmaapReactiveWebClient = webClientFactory.build(dmaapConsumerConfiguration);
64 assertNotNull(dmaapReactiveWebClient);
65 verify(sslFactory).createInsecureClientContext();
69 void builder_shouldBuildDMaaPReactiveWebClientwithSecureSslContext(){
71 DmaapConsumerConfiguration dmaapConsumerConfiguration = givenDmaapConfigurationWithSslEnabled();
72 SecurityKeys givenKeys = SecurityKeysUtil.fromDmappCustomConfig(dmaapConsumerConfiguration);
75 CloudHttpClient dmaapReactiveWebClient = webClientFactory.build(dmaapConsumerConfiguration);
78 assertNotNull(dmaapReactiveWebClient);
80 verify(sslFactory).createSecureClientContext(securityKeysArgumentCaptor.capture());
82 SecurityKeys capturedKeys = securityKeysArgumentCaptor.getValue();
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());
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;
97 private DmaapConsumerConfiguration givenDmaapConfigurationWithSslEnabled(){
98 DmaapConsumerConfiguration dmaapConsumerConfiguration = mock(DmaapConsumerConfiguration.class);
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);
106 when(sslFactory.createSecureClientContext(any(SecurityKeys.class))).thenReturn(dummySslContext);
108 return dmaapConsumerConfiguration;