X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=trustStoreMerger%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Foom%2Ftruststoremerger%2Fconfiguration%2FMergerConfigurationProviderTest.java;h=3df9bfdb8b38ca9de60e3b9544616bd0c0aa808f;hb=0d7863b1fdbdb7d25610ec33460c2442a60fef45;hp=854cde79e600c1e296824e33d67eb13048b79cc9;hpb=9518352d2bdc2db7b33c5ad305faeca77eba9656;p=oom%2Fplatform%2Fcert-service.git diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProviderTest.java index 854cde79..3df9bfdb 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProviderTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProviderTest.java @@ -19,82 +19,154 @@ package org.onap.oom.truststoremerger.configuration; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.Mockito.when; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.KEYSTORE_DESTINATION_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.KEYSTORE_SOURCE_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV; + +import java.util.ArrayList; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - -import java.util.ArrayList; -import java.util.List; import org.onap.oom.truststoremerger.configuration.exception.MergerConfigurationException; import org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException; import org.onap.oom.truststoremerger.configuration.model.MergerConfiguration; -import org.onap.oom.truststoremerger.configuration.path.TruststoresPathsProvider; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.mockito.Mockito.when; +import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReader; @ExtendWith(MockitoExtension.class) class MergerConfigurationProviderTest { private static final String BASE_TRUSTSTORE_PATH = "/opt/app/truststore_"; - private static final String TRUSTSTORE_EXTENSION = ".jks"; - private static final String PASSWORD_EXTENSION = ".pass"; + private static final String KEYSTORE_PATH = "/opt/app/keystore_"; + private static final String ANOTHER_KEYSTORE_PATH = "/opt/app/external/keystore_"; + private static final String JKS_EXTENSION = ".jks"; + private static final String PEM_EXTENSION = ".pem"; + private static final String PASS_EXTENSION = ".pass"; @Mock - private TruststoresPathsProvider pathsProvider; + private DelimitedPathsReader certificatesPathsProvider; + @Mock + private DelimitedPathsReader passwordsPathsProvider; + @Mock + private DelimitedPathsReader copierPathsReader; private MergerConfigurationProvider factory; @BeforeEach void setUp() { - factory = new MergerConfigurationProvider(pathsProvider); + factory = new MergerConfigurationProvider(certificatesPathsProvider, passwordsPathsProvider, copierPathsReader); } @Test - void shouldReturnConfigurationWithCorrectPaths() throws TruststoresPathsProviderException, MergerConfigurationException { + void shouldReturnConfigurationWithCorrectPaths() + throws TruststoresPathsProviderException, MergerConfigurationException { int numberOfPaths = 5; - List truststoresPaths = createListOfPathsWithExtension(numberOfPaths, TRUSTSTORE_EXTENSION); - List truststorePasswordPaths = createListOfPathsWithExtension(numberOfPaths, PASSWORD_EXTENSION); - mockPaths(truststoresPaths, truststorePasswordPaths); + List truststoresPaths = createListOfPathsWithExtension(numberOfPaths, JKS_EXTENSION); + List truststorePasswordPaths = createListOfPathsWithExtension(numberOfPaths, PASS_EXTENSION); + mockTruststorePaths(truststoresPaths, truststorePasswordPaths); + + List sourceKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(KEYSTORE_PATH, + numberOfPaths, PEM_EXTENSION); + List destKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(ANOTHER_KEYSTORE_PATH, + numberOfPaths, PEM_EXTENSION); + mockKeystorePaths(sourceKeystoresPairPaths, destKeystoresPairPaths); MergerConfiguration configuration = factory.createConfiguration(); assertThat(configuration.getTruststoreFilePaths()).containsAll(truststoresPaths); assertThat(configuration.getTruststoreFilePasswordPaths()).containsAll(truststorePasswordPaths); + assertThat(configuration.getSourceKeystorePaths()).containsAll(sourceKeystoresPairPaths); + assertThat(configuration.getDestinationKeystorePaths()).containsAll(destKeystoresPairPaths); } @Test - void shouldThrowExceptionWhenTruststoresLenghtDifferentThanTruststoresPasswordsLength() throws TruststoresPathsProviderException { - int numberOfTruststores = 5; + void shouldThrowExceptionWhenTruststoresLengthDifferentThanTruststoresPasswordsLength() + throws TruststoresPathsProviderException { + int numberOfCertificates = 5; int numberOfTruststoresPasswords = 4; - List truststoresPaths = createListOfPathsWithExtension(numberOfTruststores, TRUSTSTORE_EXTENSION); - List truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASSWORD_EXTENSION); - mockPaths(truststoresPaths, truststorePasswordPaths); + List truststoresPaths = createListOfPathsWithExtension(numberOfCertificates, JKS_EXTENSION); + List truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASS_EXTENSION); + mockTruststorePaths(truststoresPaths, truststorePasswordPaths); + + List sourceKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(KEYSTORE_PATH, + numberOfCertificates, PEM_EXTENSION); + List destKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(ANOTHER_KEYSTORE_PATH, + numberOfCertificates, PEM_EXTENSION); + mockKeystorePaths(sourceKeystoresPairPaths, destKeystoresPairPaths); assertThatExceptionOfType(MergerConfigurationException.class) - .isThrownBy(factory::createConfiguration); + .isThrownBy(factory::createConfiguration); } - private void mockPaths(List truststores, List truststoresPasswords) throws TruststoresPathsProviderException { + @Test + void shouldThrowExceptionWhenSourceLengthDifferentThanDestinationLength() + throws TruststoresPathsProviderException { + int numberOfCertificates = 5; + int anotherNumberOfCertificates = 1; + List truststoresPaths = createListOfPathsWithExtension(numberOfCertificates, JKS_EXTENSION); + List truststorePasswordPaths = createListOfPathsWithExtension(numberOfCertificates, PASS_EXTENSION); + mockTruststorePaths(truststoresPaths, truststorePasswordPaths); + + List sourceKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(KEYSTORE_PATH, + numberOfCertificates, PEM_EXTENSION); + List destKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(ANOTHER_KEYSTORE_PATH, + anotherNumberOfCertificates, PEM_EXTENSION); + mockKeystorePaths(sourceKeystoresPairPaths, destKeystoresPairPaths); + + assertThatExceptionOfType(MergerConfigurationException.class) + .isThrownBy(factory::createConfiguration); + } + + private void mockTruststorePaths(List truststores, List truststoresPasswords) + throws TruststoresPathsProviderException { mockTruststores(truststores); mockTruststoresPasswords(truststoresPasswords); } + private void mockKeystorePaths(List sourceKeystoresPairPaths, List destKeystoresPairPaths) + throws TruststoresPathsProviderException { + mockKeystoreCopierSourcePaths(sourceKeystoresPairPaths); + mockKeystoreCopierDestinationPaths(destKeystoresPairPaths); + } + private void mockTruststores(List truststores) throws TruststoresPathsProviderException { - when(pathsProvider.getTruststores()).thenReturn(truststores); + when(certificatesPathsProvider.get(TRUSTSTORES_PATHS_ENV)).thenReturn(truststores); } private void mockTruststoresPasswords(List truststoresPasswords) throws TruststoresPathsProviderException { - when(pathsProvider.getTruststoresPasswords()).thenReturn(truststoresPasswords); + when(passwordsPathsProvider.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)).thenReturn(truststoresPasswords); + } + + private void mockKeystoreCopierSourcePaths(List paths) throws TruststoresPathsProviderException { + when(copierPathsReader.get(KEYSTORE_SOURCE_PATHS_ENV)).thenReturn(paths); } - private List createListOfPathsWithExtension(int numberOfPaths, String password_extension) { + private void mockKeystoreCopierDestinationPaths(List paths) throws TruststoresPathsProviderException { + when(copierPathsReader.get(KEYSTORE_DESTINATION_PATHS_ENV)).thenReturn(paths); + } + + private List createListOfPathsWithExtension(int numberOfPaths, String passwordExtension) { + List paths = new ArrayList<>(); + while (numberOfPaths-- > 0) { + paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + passwordExtension); + } + return paths; + } + + private List createListOfKeystorePairsPathsWithExtension(String path, int numberOfPaths, + String certExtension) { List paths = new ArrayList<>(); + String passExtension = certExtension.equalsIgnoreCase(".pem") ? certExtension : ".pass"; while (numberOfPaths-- > 0) { - paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + password_extension); + paths.add(path + numberOfPaths + certExtension); + paths.add(ANOTHER_KEYSTORE_PATH + numberOfPaths + passExtension); } return paths; } + }