2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.vid.aai.util;
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;
33 import javax.net.ssl.SSLContext;
34 import java.util.Optional;
36 import static org.mockito.ArgumentMatchers.anyString;
37 import static org.mockito.Mockito.*;
39 @RunWith(MockitoJUnitRunner.class)
40 public class HttpsAuthClientTest {
42 private SystemPropertyHelper systemPropertyHelper;
44 private SSLContextProvider sslContextProvider;
46 private SSLContext sslContext;
48 public static final String CERT_FILE_PATH = "any_path";
50 private HttpsAuthClient createTestSubject() {
51 return new HttpsAuthClient(CERT_FILE_PATH, systemPropertyHelper, sslContextProvider, mock(FeatureManager.class));
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");
61 @Test(expected = HttpClientBuilderException.class)
62 public void testHttpClientBuilderExceptionOnGetClient() throws Exception {
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);
70 public void testGetSecuredClient() throws Exception {
72 when(systemPropertyHelper.isClientCertEnabled()).thenReturn(true);
73 when(sslContextProvider.getSslContext(anyString(), anyString(), any())).thenReturn(sslContext);
74 createTestSubject().getClient(HttpClientMode.WITH_KEYSTORE);
77 verify(sslContextProvider).getSslContext(anyString(), anyString(), any());
81 public void testGetUnsecuredClient() throws Exception {
83 when(systemPropertyHelper.isClientCertEnabled()).thenReturn(false);
84 createTestSubject().getClient(HttpClientMode.WITH_KEYSTORE);
87 verify(sslContextProvider, never()).getSslContext(anyString(), anyString(), any());