Fix checkstyle warnings
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / aaf / certservice / client / configuration / factory / SslContextFactoryTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * aaf-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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aaf.certservice.client.configuration.factory;
22
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.aaf.certservice.client.configuration.EnvsForTls;
28 import org.onap.aaf.certservice.client.configuration.exception.TlsConfigurationException;
29
30 import javax.net.ssl.SSLContext;
31 import java.util.Optional;
32
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;
37
38
39 @ExtendWith(MockitoExtension.class)
40 public class SslContextFactoryTest {
41
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";
50     @Mock
51     private EnvsForTls envsForTls;
52
53     @Test
54     public void shouldThrowExceptionWhenKeystorePathEnvIsMissing() {
55         // Given
56         when(envsForTls.getKeystorePath()).thenReturn(Optional.empty());
57         SslContextFactory sslContextFactory = new SslContextFactory(envsForTls);
58
59         // When, Then
60         Exception exception = assertThrows(
61                 TlsConfigurationException.class, sslContextFactory::create
62         );
63         assertThat(exception.getMessage()).contains("KEYSTORE_PATH");
64     }
65
66     @Test
67     public void shouldThrowExceptionWhenKeystorePasswordEnvIsMissing() {
68         // Given
69         when(envsForTls.getKeystorePath()).thenReturn(Optional.of("keystore"));
70         when(envsForTls.getKeystorePassword()).thenReturn(Optional.empty());
71         SslContextFactory sslContextFactory = new SslContextFactory(envsForTls);
72
73         // When, Then
74         Exception exception = assertThrows(
75                 TlsConfigurationException.class, sslContextFactory::create
76         );
77         assertThat(exception.getMessage()).contains("KEYSTORE_PASSWORD");
78     }
79
80     @Test
81     public void shouldThrowExceptionWhenTruststorePathEnvIsMissing() {
82         // Given
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);
87
88         // When, Then
89         Exception exception = assertThrows(
90                 TlsConfigurationException.class, sslContextFactory::create
91         );
92         assertThat(exception.getMessage()).contains("TRUSTSTORE_PATH");
93     }
94
95     @Test
96     public void shouldThrowExceptionWhenTruststorePasswordEnvIsMissing() {
97         // Given
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);
103
104         // When, Then
105         Exception exception = assertThrows(
106                 TlsConfigurationException.class, sslContextFactory::create
107         );
108         assertThat(exception.getMessage()).contains("TRUSTSTORE_PASSWORD");
109     }
110
111     @Test
112     public void shouldThrowExceptionWhenKeystoreIsMissing() {
113         // Given
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);
119
120         // When, Then
121         assertThrows(
122                 TlsConfigurationException.class, sslContextFactory::create
123         );
124     }
125
126     @Test
127     public void shouldThrowExceptionWhenKeystorePasswordIsWrong() {
128         // Given
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);
135
136         // When, Then
137         assertThrows(
138                 TlsConfigurationException.class, sslContextFactory::create
139         );
140     }
141
142     @Test
143     public void shouldThrowExceptionWhenTruststoreIsMissing() {
144         // Given
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);
151
152         // When, Then
153         assertThrows(
154                 TlsConfigurationException.class, sslContextFactory::create
155         );
156     }
157
158     @Test
159     public void shouldThrowExceptionWhenTruststorePasswordIsWrong() {
160         // Given
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);
168
169         // When, Then
170         assertThrows(
171                 TlsConfigurationException.class, sslContextFactory::create
172         );
173     }
174
175     @Test
176     public void shouldReturnSslContext() throws TlsConfigurationException {
177         // Given
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);
185
186         // When
187         SSLContext sslContext = sslContextFactory.create();
188
189         // Then
190         assertNotNull(sslContext);
191     }
192
193     private String getResourcePath(String resource) {
194         return getClass().getClassLoader().getResource(resource).getFile();
195     }
196 }
197