153189fd16e9552bb3d9d63c81b139981f0df233
[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 class AaiReactiveWebClientFactoryTest {
37
38     private static final String TRUST_STORE_PATH = "trust_store_path";
39     private static final String TRUST_STORE_PASS_PATH = "trust_store_pass_path";
40     private static final String KEY_STORE_PATH = "key_store_path";
41     private static final String KEY_STORE_PASS_PATH = "key_store_pass_path";
42     private SslFactory sslFactory = mock(SslFactory.class);
43     private AaiClientConfiguration aaiClientConfiguration = mock(AaiClientConfiguration.class);
44     private AaiReactiveWebClientFactory aaiReactiveWebClientFactory;
45
46     @Test
47     void shouldCreateWebClientWithSecureSslContext() throws SSLException {
48         givenEnabledAaiCertAuthConfiguration();
49         aaiReactiveWebClientFactory = new AaiReactiveWebClientFactory(sslFactory, aaiClientConfiguration);
50
51         Assertions.assertNotNull(aaiReactiveWebClientFactory.build());
52         verify(sslFactory).createSecureContext(KEY_STORE_PATH, KEY_STORE_PASS_PATH,
53                 TRUST_STORE_PATH, TRUST_STORE_PASS_PATH);
54     }
55
56     @Test
57     void shouldCreateWebClientWithInsecureSslContext() throws SSLException {
58         when(aaiClientConfiguration.enableAaiCertAuth()).thenReturn(false);
59         aaiReactiveWebClientFactory = new AaiReactiveWebClientFactory(sslFactory, aaiClientConfiguration);
60
61         Assertions.assertNotNull(aaiReactiveWebClientFactory.build());
62         verify(sslFactory).createInsecureContext();
63     }
64
65     private void givenEnabledAaiCertAuthConfiguration() {
66         when(aaiClientConfiguration.enableAaiCertAuth()).thenReturn(true);
67         when(aaiClientConfiguration.trustStorePath()).thenReturn(TRUST_STORE_PATH);
68         when(aaiClientConfiguration.trustStorePasswordPath()).thenReturn(TRUST_STORE_PASS_PATH);
69         when(aaiClientConfiguration.keyStorePath()).thenReturn(KEY_STORE_PATH);
70         when(aaiClientConfiguration.keyStorePasswordPath()).thenReturn(KEY_STORE_PASS_PATH);
71     }
72 }