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.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
28 import javax.net.ssl.SSLContext;
30 @RunWith(MockitoJUnitRunner.class)
31 public class HttpsAuthClientTest {
33 private SystemPropertyHelper systemPropertyHelper;
35 private SSLContextProvider sslContextProvider;
37 private SSLContext sslContext;
39 public static final String CERT_FILE_PATH = "any_path";
44 private HttpsAuthClient createTestSubject() {
45 return new HttpsAuthClient(systemPropertyHelper, sslContextProvider);
49 public void setUp() throws Exception {
50 when(systemPropertyHelper.getAAITruststoreFilename()).thenReturn(Optional.of("filename"));
51 when(systemPropertyHelper.getEncodedTruststorePassword()).thenReturn("password");
54 @Test(expected = HttpClientBuilderException.class)
55 public void testHttpClientBuilderExceptionOnGetClient() throws HttpClientBuilderException {
57 when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("true"));
58 when(sslContextProvider.getSslContext(anyString(), anyString())).thenThrow(new HttpClientBuilderException());
59 createTestSubject().getClient("nonExistingFile");
63 public void testGetSecuredClient() throws Exception {
65 when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("true"));
66 when(sslContextProvider.getSslContext(anyString(), anyString())).thenReturn(sslContext);
67 createTestSubject().getClient(CERT_FILE_PATH);
70 verify(sslContextProvider).getSslContext(anyString(), anyString());
74 public void testGetUnsecuredClient() throws Exception {
76 when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("false"));
77 when(sslContextProvider.getSslContext(anyString(), anyString())).thenReturn(sslContext);
78 createTestSubject().getClient(CERT_FILE_PATH);
81 verify(sslContextProvider, never()).getSslContext(anyString(), anyString());