1 package org.onap.oom.truststoremerger.certification.file;
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;
9 import java.util.Optional;
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;
17 @ExtendWith(MockitoExtension.class)
18 class TruststoresPathsProviderTest {
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";
26 private EnvProvider envProvider;
27 private TruststoresPathsProvider truststoresPathsProvider;
31 truststoresPathsProvider = new TruststoresPathsProvider(envProvider, new PathValidator());
35 void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException {
36 mockTruststoresEnv(VALID_TRUSTSTORES);
38 assertThat(truststoresPathsProvider.getTruststores())
39 .contains("/opt/app/certificates/truststore.jks",
40 "/opt/app/certificates/truststore.pem");
44 void shouldReturnCorrectListWhenTruststoresPasswordsValid() throws TruststoresPathsProviderException {
45 mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS);
47 assertThat(truststoresPathsProvider.getTruststoresPasswords())
48 .contains("/opt/app/certificates/truststore.pass",
53 void shouldThrowExceptionWhenTruststoresEmpty() {
54 mockTruststoresEnv("");
56 assertThatExceptionOfType(TruststoresPathsProviderException.class)
57 .isThrownBy(truststoresPathsProvider::getTruststores);
61 void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() {
62 mockTruststoresEnv(INVALID_TRUSTSTORES);
64 assertThatExceptionOfType(TruststoresPathsProviderException.class)
65 .isThrownBy(truststoresPathsProvider::getTruststores);
69 void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() {
70 mockTruststoresPasswordsEnv(INVALID_TRUSTSTORES_PASSWORDS);
72 assertThatExceptionOfType(TruststoresPathsProviderException.class)
73 .isThrownBy(truststoresPathsProvider::getTruststoresPasswords);
76 private void mockTruststoresEnv(String truststores) {
77 mockEnv(truststores, TRUSTSTORES_ENV);
80 private void mockTruststoresPasswordsEnv(String truststoresPasswords) {
81 mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_ENV);
84 private void mockEnv(String envValue, String envName) {
85 when(envProvider.getEnv(envName))
86 .thenReturn(Optional.of(envValue));