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.Mockito;
29 import org.mockito.runners.MockitoJUnitRunner;
30 import org.onap.vid.aai.exceptions.HttpClientBuilderException;
32 import javax.net.ssl.SSLContext;
33 import java.util.Optional;
35 import static org.mockito.Matchers.anyString;
36 import static org.mockito.Mockito.never;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.when;
40 @RunWith(MockitoJUnitRunner.class)
41 public class HttpsAuthClientTest {
43 private SystemPropertyHelper systemPropertyHelper;
45 private SSLContextProvider sslContextProvider;
47 private SSLContext sslContext;
49 public static final String CERT_FILE_PATH = "any_path";
54 private HttpsAuthClient createTestSubject() {
55 return new HttpsAuthClient(systemPropertyHelper, sslContextProvider);
59 public void setUp() throws Exception {
60 when(systemPropertyHelper.getAAITruststoreFilename()).thenReturn(Optional.of("filename"));
61 when(systemPropertyHelper.getEncodedTruststorePassword()).thenReturn("password");
64 @Test(expected = HttpClientBuilderException.class)
65 public void testHttpClientBuilderExceptionOnGetClient() throws HttpClientBuilderException {
67 when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("true"));
68 when(sslContextProvider.getSslContext(anyString(), anyString())).thenThrow(new HttpClientBuilderException());
69 createTestSubject().getClient("nonExistingFile");
73 public void testGetSecuredClient() throws Exception {
75 when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("true"));
76 when(sslContextProvider.getSslContext(anyString(), anyString())).thenReturn(sslContext);
77 createTestSubject().getClient(CERT_FILE_PATH);
80 verify(sslContextProvider).getSslContext(anyString(), anyString());
84 public void testGetUnsecuredClient() throws Exception {
86 when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("false"));
87 when(sslContextProvider.getSslContext(anyString(), anyString())).thenReturn(sslContext);
88 createTestSubject().getClient(CERT_FILE_PATH);
91 verify(sslContextProvider, never()).getSslContext(anyString(), anyString());