2 * ============LICENSE_START=======================================================
3 * oom-certservice-client
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.oom.certservice.client.configuration.factory;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.jupiter.MockitoExtension;
27 import org.onap.oom.certservice.client.configuration.EnvsForTls;
28 import org.onap.oom.certservice.client.configuration.exception.TlsConfigurationException;
30 import javax.net.ssl.SSLContext;
31 import java.util.Optional;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.junit.jupiter.api.Assertions.assertNotNull;
35 import static org.junit.jupiter.api.Assertions.assertThrows;
36 import static org.mockito.Mockito.when;
39 @ExtendWith(MockitoExtension.class)
40 public class SslContextFactoryTest {
42 public static final String INVALID_KEYSTORE_PATH = "nonexistent/keystore";
43 public static final String VALID_KEYSTORE_NAME = "keystore.jks";
44 public static final String VALID_KEYSTORE_PASSWORD = "secret";
45 public static final String INVALID_KEYSTORE_PASSWORD = "wrong_secret";
46 public static final String INVALID_TRUSTSTORE_PATH = "nonexistent/truststore";
47 public static final String VALID_TRUSTSTORE_PASSWORD = "secret";
48 public static final String INVALID_TRUSTSTORE_PASSWORD = "wrong_secret";
49 public static final String VALID_TRUSTSTORE_NAME = "truststore.jks";
51 private EnvsForTls envsForTls;
54 public void shouldThrowExceptionWhenKeystorePathEnvIsMissing() {
56 when(envsForTls.getKeystorePath()).thenReturn(Optional.empty());
57 SslContextFactory sslContextFactory = new SslContextFactory(envsForTls);
60 Exception exception = assertThrows(
61 TlsConfigurationException.class, sslContextFactory::create
63 assertThat(exception.getMessage()).contains("KEYSTORE_PATH");
67 public void shouldThrowExceptionWhenKeystorePasswordEnvIsMissing() {
69 when(envsForTls.getKeystorePath()).thenReturn(Optional.of("keystore"));
70 when(envsForTls.getKeystorePassword()).thenReturn(Optional.empty());
71 SslContextFactory sslContextFactory = new SslContextFactory(envsForTls);
74 Exception exception = assertThrows(
75 TlsConfigurationException.class, sslContextFactory::create
77 assertThat(exception.getMessage()).contains("KEYSTORE_PASSWORD");
81 public void shouldThrowExceptionWhenTruststorePathEnvIsMissing() {
83 when(envsForTls.getKeystorePath()).thenReturn(Optional.of("keystore"));
84 when(envsForTls.getKeystorePassword()).thenReturn(Optional.of("password"));
85 when(envsForTls.getTruststorePath()).thenReturn(Optional.empty());
86 SslContextFactory sslContextFactory = new SslContextFactory(envsForTls);
89 Exception exception = assertThrows(
90 TlsConfigurationException.class, sslContextFactory::create
92 assertThat(exception.getMessage()).contains("TRUSTSTORE_PATH");
96 public void shouldThrowExceptionWhenTruststorePasswordEnvIsMissing() {
98 when(envsForTls.getKeystorePath()).thenReturn(Optional.of("keystore"));
99 when(envsForTls.getKeystorePassword()).thenReturn(Optional.of("password"));
100 when(envsForTls.getTruststorePath()).thenReturn(Optional.of("truststore"));
101 when(envsForTls.getTruststorePassword()).thenReturn(Optional.empty());
102 SslContextFactory sslContextFactory = new SslContextFactory(envsForTls);
105 Exception exception = assertThrows(
106 TlsConfigurationException.class, sslContextFactory::create
108 assertThat(exception.getMessage()).contains("TRUSTSTORE_PASSWORD");
112 public void shouldThrowExceptionWhenKeystoreIsMissing() {
114 when(envsForTls.getKeystorePath()).thenReturn(Optional.of(INVALID_KEYSTORE_PATH));
115 when(envsForTls.getKeystorePassword()).thenReturn(Optional.of("secret"));
116 when(envsForTls.getTruststorePath()).thenReturn(Optional.of("truststore.jks"));
117 when(envsForTls.getTruststorePassword()).thenReturn(Optional.of("secret"));
118 SslContextFactory sslContextFactory = new SslContextFactory(envsForTls);
122 TlsConfigurationException.class, sslContextFactory::create
127 public void shouldThrowExceptionWhenKeystorePasswordIsWrong() {
129 String keystorePath = getResourcePath(VALID_KEYSTORE_NAME);
130 when(envsForTls.getKeystorePath()).thenReturn(Optional.of(keystorePath));
131 when(envsForTls.getKeystorePassword()).thenReturn(Optional.of(INVALID_KEYSTORE_PASSWORD));
132 when(envsForTls.getTruststorePath()).thenReturn(Optional.of(VALID_TRUSTSTORE_NAME));
133 when(envsForTls.getTruststorePassword()).thenReturn(Optional.of(VALID_TRUSTSTORE_PASSWORD));
134 SslContextFactory sslContextFactory = new SslContextFactory(envsForTls);
138 TlsConfigurationException.class, sslContextFactory::create
143 public void shouldThrowExceptionWhenTruststoreIsMissing() {
145 String keystorePath = getResourcePath(VALID_KEYSTORE_NAME);
146 when(envsForTls.getKeystorePath()).thenReturn(Optional.of(keystorePath));
147 when(envsForTls.getKeystorePassword()).thenReturn(Optional.of(VALID_KEYSTORE_PASSWORD));
148 when(envsForTls.getTruststorePath()).thenReturn(Optional.of(INVALID_TRUSTSTORE_PATH));
149 when(envsForTls.getTruststorePassword()).thenReturn(Optional.of(VALID_TRUSTSTORE_PASSWORD));
150 SslContextFactory sslContextFactory = new SslContextFactory(envsForTls);
154 TlsConfigurationException.class, sslContextFactory::create
159 public void shouldThrowExceptionWhenTruststorePasswordIsWrong() {
161 String keystorePath = getResourcePath(VALID_KEYSTORE_NAME);
162 String truststorePath = getResourcePath(VALID_TRUSTSTORE_NAME);
163 when(envsForTls.getKeystorePath()).thenReturn(Optional.of(keystorePath));
164 when(envsForTls.getKeystorePassword()).thenReturn(Optional.of(VALID_KEYSTORE_PASSWORD));
165 when(envsForTls.getTruststorePath()).thenReturn(Optional.of(truststorePath));
166 when(envsForTls.getTruststorePassword()).thenReturn(Optional.of(INVALID_TRUSTSTORE_PASSWORD));
167 SslContextFactory sslContextFactory = new SslContextFactory(envsForTls);
171 TlsConfigurationException.class, sslContextFactory::create
176 public void shouldReturnSslContext() throws TlsConfigurationException {
178 String keystorePath = getResourcePath(VALID_KEYSTORE_NAME);
179 String truststorePath = getResourcePath(VALID_TRUSTSTORE_NAME);
180 when(envsForTls.getKeystorePath()).thenReturn(Optional.of(keystorePath));
181 when(envsForTls.getKeystorePassword()).thenReturn(Optional.of(VALID_KEYSTORE_PASSWORD));
182 when(envsForTls.getTruststorePath()).thenReturn(Optional.of(truststorePath));
183 when(envsForTls.getTruststorePassword()).thenReturn(Optional.of(VALID_TRUSTSTORE_PASSWORD));
184 SslContextFactory sslContextFactory = new SslContextFactory(envsForTls);
187 SSLContext sslContext = sslContextFactory.create();
190 assertNotNull(sslContext);
193 private String getResourcePath(String resource) {
194 return getClass().getClassLoader().getResource(resource).getFile();