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