d52d18995717b10a284bbba794409a9c352622ee
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / certification / file / TruststoresPathsProviderTest.java
1 package org.onap.oom.truststoremerger.certification.file;
2
3 import org.junit.jupiter.api.BeforeEach;
4 import org.junit.jupiter.api.Test;
5 import org.junit.jupiter.api.extension.ExtendWith;
6 import org.mockito.Mock;
7 import org.mockito.junit.jupiter.MockitoExtension;
8
9 import java.util.Optional;
10
11 import static org.assertj.core.api.Assertions.assertThat;
12 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
13 import static org.mockito.Mockito.when;
14 import static org.onap.oom.truststoremerger.certification.file.TruststoresPathsProvider.TRUSTSTORES_ENV;
15 import static org.onap.oom.truststoremerger.certification.file.TruststoresPathsProvider.TRUSTSTORES_PASSWORDS_ENV;
16
17 @ExtendWith(MockitoExtension.class)
18 class TruststoresPathsProviderTest {
19
20     private static final String VALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.pem";
21     private static final String VALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:";
22     private static final String INVALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.invalid";
23     private static final String INVALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:/.pass";
24
25     @Mock
26     private EnvProvider envProvider;
27     private TruststoresPathsProvider truststoresPathsProvider;
28
29     @BeforeEach
30     void setUp() {
31         truststoresPathsProvider = new TruststoresPathsProvider(envProvider, new PathValidator());
32     }
33
34     @Test
35     void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException {
36         mockTruststoresEnv(VALID_TRUSTSTORES);
37
38         assertThat(truststoresPathsProvider.getTruststores())
39                 .contains("/opt/app/certificates/truststore.jks",
40                         "/opt/app/certificates/truststore.pem");
41     }
42
43     @Test
44     void shouldReturnCorrectListWhenTruststoresPasswordsValid() throws TruststoresPathsProviderException {
45         mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS);
46
47         assertThat(truststoresPathsProvider.getTruststoresPasswords())
48                 .contains("/opt/app/certificates/truststore.pass",
49                         "");
50     }
51
52     @Test
53     void shouldThrowExceptionWhenTruststoresEmpty() {
54         mockTruststoresEnv("");
55
56         assertThatExceptionOfType(TruststoresPathsProviderException.class)
57                 .isThrownBy(truststoresPathsProvider::getTruststores);
58     }
59
60     @Test
61     void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() {
62         mockTruststoresEnv(INVALID_TRUSTSTORES);
63
64         assertThatExceptionOfType(TruststoresPathsProviderException.class)
65                 .isThrownBy(truststoresPathsProvider::getTruststores);
66     }
67
68     @Test
69     void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() {
70         mockTruststoresPasswordsEnv(INVALID_TRUSTSTORES_PASSWORDS);
71
72         assertThatExceptionOfType(TruststoresPathsProviderException.class)
73                 .isThrownBy(truststoresPathsProvider::getTruststoresPasswords);
74     }
75
76     private void mockTruststoresEnv(String truststores) {
77         mockEnv(truststores, TRUSTSTORES_ENV);
78     }
79
80     private void mockTruststoresPasswordsEnv(String truststoresPasswords) {
81         mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_ENV);
82     }
83
84     private void mockEnv(String envValue, String envName) {
85         when(envProvider.getEnv(envName))
86                 .thenReturn(Optional.of(envValue));
87     }
88 }