c7323160ef21d9d69321e3ff49eeeed9586a49a3
[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
22 package org.onap.dcaegen2.services.sdk.rest.services.aai.client.service;
23
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import org.junit.jupiter.api.Assertions;
29 import org.junit.jupiter.api.Test;
30 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration;
31 import org.onap.dcaegen2.services.sdk.rest.services.ssl.SslFactory;
32
33 import javax.net.ssl.SSLException;
34
35
36
37 class AaiReactiveWebClientFactoryTest {
38
39     private static final String TRUST_STORE_PATH = "trust_store_path";
40     private static final String TRUST_STORE_PASS_PATH = "trust_store_pass_path";
41     private static final String KEY_STORE_PATH = "key_store_path";
42     private static final String KEY_STORE_PASS_PATH = "key_store_pass_path";
43     private SslFactory sslFactory = mock(SslFactory.class);
44     private AaiClientConfiguration aaiClientConfiguration = mock(AaiClientConfiguration.class);
45     private AaiReactiveWebClientFactory aaiReactiveWebClientFactory;
46
47     @Test
48     void shouldCreateWebClientWithSecureSslContext() throws SSLException {
49         givenEnabledAaiCertAuthConfiguration();
50         aaiReactiveWebClientFactory = new AaiReactiveWebClientFactory(sslFactory, aaiClientConfiguration);
51
52         Assertions.assertNotNull(aaiReactiveWebClientFactory.build());
53         verify(sslFactory).createSecureContext(KEY_STORE_PATH, KEY_STORE_PASS_PATH,
54                 TRUST_STORE_PATH, TRUST_STORE_PASS_PATH);
55     }
56
57     @Test
58     void shouldCreateWebClientWithInsecureSslContext() throws SSLException {
59         when(aaiClientConfiguration.enableAaiCertAuth()).thenReturn(false);
60         aaiReactiveWebClientFactory = new AaiReactiveWebClientFactory(sslFactory, aaiClientConfiguration);
61
62         Assertions.assertNotNull(aaiReactiveWebClientFactory.build());
63         verify(sslFactory).createInsecureContext();
64     }
65
66     private void givenEnabledAaiCertAuthConfiguration() {
67         when(aaiClientConfiguration.enableAaiCertAuth()).thenReturn(true);
68         when(aaiClientConfiguration.trustStorePath()).thenReturn(TRUST_STORE_PATH);
69         when(aaiClientConfiguration.trustStorePasswordPath()).thenReturn(TRUST_STORE_PASS_PATH);
70         when(aaiClientConfiguration.keyStorePath()).thenReturn(KEY_STORE_PATH);
71         when(aaiClientConfiguration.keyStorePasswordPath()).thenReturn(KEY_STORE_PASS_PATH);
72     }
73 }