Merge from ECOMP's repository
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / util / HttpsAuthClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.aai.util;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.vid.aai.exceptions.HttpClientBuilderException;
30 import org.onap.vid.exceptions.GenericUncheckedException;
31 import org.togglz.core.manager.FeatureManager;
32
33 import javax.net.ssl.SSLContext;
34 import java.util.Optional;
35
36 import static org.mockito.ArgumentMatchers.anyString;
37 import static org.mockito.Mockito.*;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class HttpsAuthClientTest {
41     @Mock
42     private SystemPropertyHelper systemPropertyHelper;
43     @Mock
44     private SSLContextProvider sslContextProvider;
45     @Mock
46     private SSLContext sslContext;
47
48     public static final String CERT_FILE_PATH = "any_path";
49
50     private HttpsAuthClient createTestSubject() {
51         return new HttpsAuthClient(CERT_FILE_PATH, systemPropertyHelper, sslContextProvider, mock(FeatureManager.class));
52     }
53
54     @Before
55     public void setUp() throws Exception {
56         when(systemPropertyHelper.getAAITruststoreFilename()).thenReturn(Optional.of("filename"));
57         when(systemPropertyHelper.getDecryptedKeystorePassword()).thenReturn("password");
58         when(systemPropertyHelper.getDecryptedTruststorePassword()).thenReturn("password");
59     }
60
61     @Test(expected = HttpClientBuilderException.class)
62     public void testHttpClientBuilderExceptionOnGetClient() throws Exception {
63         //when
64         when(systemPropertyHelper.isClientCertEnabled()).thenReturn(true);
65         when(sslContextProvider.getSslContext(anyString(), anyString(), any())).thenThrow(new HttpClientBuilderException(new GenericUncheckedException("msg")));
66         createTestSubject().getClient(HttpClientMode.WITH_KEYSTORE);
67     }
68
69     @Test
70     public void testGetSecuredClient() throws Exception {
71         // when
72         when(systemPropertyHelper.isClientCertEnabled()).thenReturn(true);
73         when(sslContextProvider.getSslContext(anyString(), anyString(), any())).thenReturn(sslContext);
74         createTestSubject().getClient(HttpClientMode.WITH_KEYSTORE);
75
76         //then
77         verify(sslContextProvider).getSslContext(anyString(), anyString(), any());
78     }
79
80     @Test
81     public void testGetUnsecuredClient() throws Exception {
82         // when
83         when(systemPropertyHelper.isClientCertEnabled()).thenReturn(false);
84         createTestSubject().getClient(HttpClientMode.WITH_KEYSTORE);
85
86         //then
87         verify(sslContextProvider, never()).getSslContext(anyString(), anyString(), any());
88     }
89 }