46a57b69bcd89c468d5de327368ecfdccb31440b
[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 io.netty.handler.ssl.SslContext;
29 import org.junit.jupiter.api.Assertions;
30 import org.junit.jupiter.api.Test;
31 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration;
32 import org.onap.dcaegen2.services.sdk.rest.services.ssl.SslFactory;
33
34 import javax.net.ssl.SSLException;
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     private SslContext dummySslContext = mock(SslContext.class);
47
48     @Test
49     void shouldCreateWebClientWithSecureSslContext() throws SSLException {
50         givenEnabledAaiCertAuthConfiguration();
51         aaiReactiveWebClientFactory = new AaiReactiveWebClientFactory(sslFactory, aaiClientConfiguration);
52
53         Assertions.assertNotNull(aaiReactiveWebClientFactory.build());
54         verify(sslFactory).createSecureContext(KEY_STORE_PATH, KEY_STORE_PASS_PATH,
55                 TRUST_STORE_PATH, TRUST_STORE_PASS_PATH);
56     }
57
58     @Test
59     void shouldCreateWebClientWithInsecureSslContext() throws SSLException {
60         when(aaiClientConfiguration.enableAaiCertAuth()).thenReturn(false);
61         when(sslFactory.createInsecureContext()).thenReturn(dummySslContext);
62         aaiReactiveWebClientFactory = new AaiReactiveWebClientFactory(sslFactory, aaiClientConfiguration);
63
64         Assertions.assertNotNull(aaiReactiveWebClientFactory.build());
65         verify(sslFactory).createInsecureContext();
66     }
67
68     private void givenEnabledAaiCertAuthConfiguration() throws SSLException {
69         when(aaiClientConfiguration.enableAaiCertAuth()).thenReturn(true);
70         when(aaiClientConfiguration.trustStorePath()).thenReturn(TRUST_STORE_PATH);
71         when(aaiClientConfiguration.trustStorePasswordPath()).thenReturn(TRUST_STORE_PASS_PATH);
72         when(aaiClientConfiguration.keyStorePath()).thenReturn(KEY_STORE_PATH);
73         when(aaiClientConfiguration.keyStorePasswordPath()).thenReturn(KEY_STORE_PASS_PATH);
74         when(sslFactory.createSecureContext(KEY_STORE_PATH, KEY_STORE_PASS_PATH, TRUST_STORE_PATH, TRUST_STORE_PASS_PATH))
75                 .thenReturn(dummySslContext);
76     }
77 }