diff options
author | 2020-09-07 14:13:33 +0200 | |
---|---|---|
committer | 2020-09-08 15:04:04 +0200 | |
commit | 0d7863b1fdbdb7d25610ec33460c2442a60fef45 (patch) | |
tree | 4913205aed685bf25f98de7528c9ae0919a23918 /trustStoreMerger/src/test | |
parent | 9518352d2bdc2db7b33c5ad305faeca77eba9656 (diff) |
Add of config for copier
Add Source and Destination paths to merger.
Issue-ID: DCAEGEN2-2253
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
Change-Id: Id0ec28d8d7f90c5ce61d5393b6e1d8363dbfd24d
Diffstat (limited to 'trustStoreMerger/src/test')
7 files changed, 302 insertions, 124 deletions
diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java index c971ca17..b5893230 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java @@ -113,19 +113,19 @@ public class TestCertificateProvider { public static JavaTruststore getSampleJksTruststoreFile() throws LoadTruststoreException, KeystoreInstanceException { - return createJKSTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); + return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); } public static JavaTruststore getSampleJksTruststoreFileWithUniqueAlias() throws LoadTruststoreException, KeystoreInstanceException { - return createJKSTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, + return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); } public static JavaTruststore createTmpJksTruststoreFileWithUniqAlias() throws IOException, LoadTruststoreException, KeystoreInstanceException { copyFile(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, TMP_JKS_TRUSTSTORE_FILE_PATH); - return createJKSTruststoreInstance(TMP_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); + return createJksTruststoreInstance(TMP_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); } public static void removeTemporaryFiles() throws IOException { @@ -134,7 +134,7 @@ public class TestCertificateProvider { Files.deleteIfExists(Paths.get(TMP_P12_TRUSTSTORE_FILE_PATH)); } - private static JavaTruststore createJKSTruststoreInstance(String filePath, String password) + private static JavaTruststore createJksTruststoreInstance(String filePath, String password) throws LoadTruststoreException, KeystoreInstanceException { File certFile = getFile(filePath); JavaCertificateStoreController storeController = certificateStoreControllerFactory diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java index 0897de29..f05cbc86 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java @@ -27,7 +27,7 @@ import org.junit.jupiter.api.Test; class PemAliasGeneratorTest { - private final static String PREFIX_ALIAS_NAME = "pem-trusted-certificate-"; + private static final String PREFIX_ALIAS_NAME = "pem-trusted-certificate-"; static final int GENERATED_ALIASES_NUMBER = 100; @Test 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<String> truststoresPaths = createListOfPathsWithExtension(numberOfPaths, TRUSTSTORE_EXTENSION); - List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfPaths, PASSWORD_EXTENSION); - mockPaths(truststoresPaths, truststorePasswordPaths); + List<String> truststoresPaths = createListOfPathsWithExtension(numberOfPaths, JKS_EXTENSION); + List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfPaths, PASS_EXTENSION); + mockTruststorePaths(truststoresPaths, truststorePasswordPaths); + + List<String> sourceKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(KEYSTORE_PATH, + numberOfPaths, PEM_EXTENSION); + List<String> 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<String> truststoresPaths = createListOfPathsWithExtension(numberOfTruststores, TRUSTSTORE_EXTENSION); - List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASSWORD_EXTENSION); - mockPaths(truststoresPaths, truststorePasswordPaths); + List<String> truststoresPaths = createListOfPathsWithExtension(numberOfCertificates, JKS_EXTENSION); + List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASS_EXTENSION); + mockTruststorePaths(truststoresPaths, truststorePasswordPaths); + + List<String> sourceKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(KEYSTORE_PATH, + numberOfCertificates, PEM_EXTENSION); + List<String> destKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(ANOTHER_KEYSTORE_PATH, + numberOfCertificates, PEM_EXTENSION); + mockKeystorePaths(sourceKeystoresPairPaths, destKeystoresPairPaths); assertThatExceptionOfType(MergerConfigurationException.class) - .isThrownBy(factory::createConfiguration); + .isThrownBy(factory::createConfiguration); } - private void mockPaths(List<String> truststores, List<String> truststoresPasswords) throws TruststoresPathsProviderException { + @Test + void shouldThrowExceptionWhenSourceLengthDifferentThanDestinationLength() + throws TruststoresPathsProviderException { + int numberOfCertificates = 5; + int anotherNumberOfCertificates = 1; + List<String> truststoresPaths = createListOfPathsWithExtension(numberOfCertificates, JKS_EXTENSION); + List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfCertificates, PASS_EXTENSION); + mockTruststorePaths(truststoresPaths, truststorePasswordPaths); + + List<String> sourceKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(KEYSTORE_PATH, + numberOfCertificates, PEM_EXTENSION); + List<String> destKeystoresPairPaths = createListOfKeystorePairsPathsWithExtension(ANOTHER_KEYSTORE_PATH, + anotherNumberOfCertificates, PEM_EXTENSION); + mockKeystorePaths(sourceKeystoresPairPaths, destKeystoresPairPaths); + + assertThatExceptionOfType(MergerConfigurationException.class) + .isThrownBy(factory::createConfiguration); + } + + private void mockTruststorePaths(List<String> truststores, List<String> truststoresPasswords) + throws TruststoresPathsProviderException { mockTruststores(truststores); mockTruststoresPasswords(truststoresPasswords); } + private void mockKeystorePaths(List<String> sourceKeystoresPairPaths, List<String> destKeystoresPairPaths) + throws TruststoresPathsProviderException { + mockKeystoreCopierSourcePaths(sourceKeystoresPairPaths); + mockKeystoreCopierDestinationPaths(destKeystoresPairPaths); + } + private void mockTruststores(List<String> truststores) throws TruststoresPathsProviderException { - when(pathsProvider.getTruststores()).thenReturn(truststores); + when(certificatesPathsProvider.get(TRUSTSTORES_PATHS_ENV)).thenReturn(truststores); } private void mockTruststoresPasswords(List<String> truststoresPasswords) throws TruststoresPathsProviderException { - when(pathsProvider.getTruststoresPasswords()).thenReturn(truststoresPasswords); + when(passwordsPathsProvider.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)).thenReturn(truststoresPasswords); + } + + private void mockKeystoreCopierSourcePaths(List<String> paths) throws TruststoresPathsProviderException { + when(copierPathsReader.get(KEYSTORE_SOURCE_PATHS_ENV)).thenReturn(paths); } - private List<String> createListOfPathsWithExtension(int numberOfPaths, String password_extension) { + private void mockKeystoreCopierDestinationPaths(List<String> paths) throws TruststoresPathsProviderException { + when(copierPathsReader.get(KEYSTORE_DESTINATION_PATHS_ENV)).thenReturn(paths); + } + + private List<String> createListOfPathsWithExtension(int numberOfPaths, String passwordExtension) { + List<String> paths = new ArrayList<>(); + while (numberOfPaths-- > 0) { + paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + passwordExtension); + } + return paths; + } + + private List<String> createListOfKeystorePairsPathsWithExtension(String path, int numberOfPaths, + String certExtension) { List<String> 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; } + } diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactoryTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactoryTest.java index efaef65f..5a7e9cd0 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactoryTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactoryTest.java @@ -22,13 +22,18 @@ package org.onap.oom.truststoremerger.configuration.path; import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; +import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider; -class TruststoresPathsProviderFactoryTest { +class DelimitedPathsReaderFactoryTest { @Test - void shouldReturnCorrectObject() { - // given, when, then - assertThat(TruststoresPathsProviderFactory.create()).isInstanceOf(TruststoresPathsProvider.class); + void shouldReturnObjectOfDelimitedPathsReaderType() { + // given + DelimitedPathsReaderFactory readerFactory = new DelimitedPathsReaderFactory(new EnvProvider()); + //when, then + assertThat(readerFactory.createPasswordPathsReader()).isInstanceOf(DelimitedPathsReader.class); + assertThat(readerFactory.createCertificatePathsReader()).isInstanceOf(DelimitedPathsReader.class); + assertThat(readerFactory.createKeystoreCopierPathsReader()).isInstanceOf(DelimitedPathsReader.class); } } diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderTest.java index d2e9cdef..408a7d6d 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderTest.java @@ -22,20 +22,21 @@ package org.onap.oom.truststoremerger.configuration.path; 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.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV; -import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_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 static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidCertificatesPaths; +import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidPasswordPaths; import java.util.Optional; -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 org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException; - +import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider; @ExtendWith(MockitoExtension.class) -class TruststoresPathsProviderTest { +class DelimitedPathsReaderTest { private static final String VALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.pem"; private static final String VALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:"; @@ -45,36 +46,62 @@ class TruststoresPathsProviderTest { @Mock private EnvProvider envProvider; - private TruststoresPathsProvider truststoresPathsProvider; - - @BeforeEach - void setUp() { - truststoresPathsProvider = new TruststoresPathsProvider(envProvider, new PathValidator()); - } + private DelimitedPathsReader delimitedPathsReader; @Test void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths()); mockTruststoresEnv(VALID_TRUSTSTORES); - assertThat(truststoresPathsProvider.getTruststores()) + // when, then + assertThat(delimitedPathsReader.get(TRUSTSTORES_PATHS_ENV)) .containsSequence("/opt/app/certificates/truststore.jks", "/opt/app/certificates/truststore.pem"); } @Test + void shouldThrowExceptionWhenTruststoresPathsEnvIsEmpty() { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths()); + mockTruststoresEnv(""); + + // when, then + assertThatExceptionOfType(TruststoresPathsProviderException.class) + .isThrownBy(() -> delimitedPathsReader.get(TRUSTSTORES_PATHS_ENV)); + } + + @Test + void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths()); + mockTruststoresEnv(INVALID_TRUSTSTORES); + + // when, then + assertThatExceptionOfType(TruststoresPathsProviderException.class) + .isThrownBy(() -> delimitedPathsReader.get(TRUSTSTORES_PATHS_ENV)); + } + + @Test void shouldReturnCorrectListWhenTruststoresPasswordsValid() throws TruststoresPathsProviderException { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths()); mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS); - assertThat(truststoresPathsProvider.getTruststoresPasswords()) + // when, then + assertThat(delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)) .containsSequence("/opt/app/certificates/truststore.pass", ""); } @Test void shouldReturnCorrectListWhenTruststoresPasswordsContainsEmptyPathsInTheMiddle() throws TruststoresPathsProviderException { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths()); mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE); - assertThat(truststoresPathsProvider.getTruststoresPasswords()).containsSequence( + // when, then + assertThat(delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)).containsSequence( "/opt/app/certificates/truststore.pass", "", "/etc/truststore.pass" @@ -82,38 +109,36 @@ class TruststoresPathsProviderTest { } @Test - void shouldThrowExceptionWhenTruststoresEmpty() { - mockTruststoresEnv(""); - - assertThatExceptionOfType(TruststoresPathsProviderException.class) - .isThrownBy(truststoresPathsProvider::getTruststores); - } - - @Test - void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() { - mockTruststoresEnv(INVALID_TRUSTSTORES); + void shouldThrowExceptionWhenTruststoresPasswordsPathEnvIsEmpty() { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths()); + mockTruststoresPasswordsEnv(""); + // when, then assertThatExceptionOfType(TruststoresPathsProviderException.class) - .isThrownBy(truststoresPathsProvider::getTruststores); + .isThrownBy(() -> delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)); } @Test void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() { + // given + delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths()); mockTruststoresPasswordsEnv(INVALID_TRUSTSTORES_PASSWORDS); + // when, then assertThatExceptionOfType(TruststoresPathsProviderException.class) - .isThrownBy(truststoresPathsProvider::getTruststoresPasswords); + .isThrownBy(() -> delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)); } private void mockTruststoresEnv(String truststores) { - mockEnv(truststores, TRUSTSTORES_PATHS_ENV); + mockEnv(TRUSTSTORES_PATHS_ENV, truststores); } private void mockTruststoresPasswordsEnv(String truststoresPasswords) { - mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_PATHS_ENV); + mockEnv(TRUSTSTORES_PASSWORDS_PATHS_ENV, truststoresPasswords); } - private void mockEnv(String envValue, String envName) { - when(envProvider.getEnv(envName)).thenReturn(Optional.of(envValue)); + private void mockEnv(String envName, String truststores) { + when(envProvider.getEnv(envName)).thenReturn(Optional.of(truststores)); } } diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/PathValidatorTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/PathValidatorTest.java deleted file mode 100644 index b0487478..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/PathValidatorTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.oom.truststoremerger.configuration.path; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -class PathValidatorTest { - - private final PathValidator validator = new PathValidator(); - - @ParameterizedTest() - @ValueSource(strings = {"/opt/app/truststore.pem", "/opt/app/truststore.jks", - "/opt/app/truststore.p12", "/truststore.pem"}) - void shouldAcceptValidTruststorePaths(String path) { - assertThat(validator.isTruststorePathValid(path)).isTrue(); - } - - @ParameterizedTest() - @ValueSource(strings = {"/opt/app/truststore.pass", "/opt/app/truststore.invalid", "/", - "truststore", "opt/app/truststore.p12", "/?.pem", "/.pem"}) - void shouldRejectInvalidTruststorePaths(String path) { - assertThat(validator.isTruststorePathValid(path)).isFalse(); - } - - @ParameterizedTest() - @ValueSource(strings = {"", "/opt/app/truststore.pass", "/truststore.pass"}) - void shouldAcceptValidTruststorePasswordPaths(String path) { - assertThat(validator.isTruststorePasswordPathValid(path)).isTrue(); - } - - @ParameterizedTest() - @ValueSource(strings = {"/opt/app/truststore.pem", "/opt/app/truststore.jks", - "/opt/app/truststore.p12", "/", "truststore", "opt/app/truststore.p12", "/?.pass", "/.pass"}) - void shouldRejectInvalidTruststorePasswordPaths(String path) { - assertThat(validator.isTruststorePasswordPathValid(path)).isFalse(); - } - -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctionsTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctionsTest.java new file mode 100644 index 00000000..3c3760f2 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctionsTest.java @@ -0,0 +1,134 @@ +/*============LICENSE_START======================================================= + * oom-truststore-merger + * ================================================================================ + * Copyright (C) 2020 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.oom.truststoremerger.configuration.path.validation; + + +import static org.assertj.core.api.Assertions.assertThat; +import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidCertificatesPaths; +import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidPasswordPaths; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; + +class ValidationFunctionsTest { + + @Test + void shouldValidateWithSuccessCorrectCertificatesPaths() { + // given + List<String> certPaths = Arrays.asList("/opt/app/certificates/truststore.p12"); + // when + boolean result = doesItContainValidCertificatesPaths().test(certPaths); + // then + assertThat(result).isTrue(); + } + + @Test + void shouldValidateWithFailureCertificatesPathsWithOneEmptyPath() { + // given + List<String> certPaths = Arrays.asList("/opt/app/certificates/truststore.p12", ""); + // when + boolean result = doesItContainValidCertificatesPaths().test(certPaths); + // then + assertThat(result).isFalse(); + } + + @Test + void shouldValidateWithFailureCertificatesPathsWithOnePathWhichHasIncorrectExtension() { + // given + List<String> certPaths = Arrays.asList("/opt/app/certificates/truststore.txt", "/opt/cert.p12"); + // when + boolean result = doesItContainValidCertificatesPaths().test(certPaths); + // then + assertThat(result).isFalse(); + } + + @Test + void shouldValidateWithSuccessCertificatesPasswordPaths() { + // given + List<String> passwordPaths = Arrays.asList("/opt/app/certificates/truststore.pass", ""); + // when + boolean result = doesItContainValidPasswordPaths().test(passwordPaths); + // then + assertThat(result).isTrue(); + } + + @Test + void shouldValidateWithSuccessCertificatePasswordsPathsWhichContainsEmptyPathsInTheMiddle() { + // given + List<String> passwordPaths = Arrays.asList("/opt/app/certificates/truststore.pass", "", "/etc/truststore.pass"); + // when + boolean result = doesItContainValidPasswordPaths().test(passwordPaths); + // then + assertThat(result).isTrue(); + } + + @Test + void shouldValidateWithFailureCertificatesPasswordsPathsWithIncorrectExtension() { + // given + List<String> passwordPaths = Arrays.asList("/pass.txt"); + // when + boolean result = doesItContainValidPasswordPaths().test(passwordPaths); + // then + assertThat(result).isFalse(); + } + + @Test + void shouldValidateWithFailureCertificatesPasswordPathsWithMissingPrecedingSlash() { + // given + List<String> passwordPaths = Arrays.asList("jks.pass"); + // when + boolean result = doesItContainValidPasswordPaths().test(passwordPaths); + // then + assertThat(result).isFalse(); + } + + @Test + void shouldValidateWithSuccessSourcePathsToCopyFiles() { + // given + List<String> sourcePaths = Arrays.asList("/opt/dcae/cacert/external/keystore.p12", + "/opt/dcae/cacert/external/keystore.pass"); + // when + boolean result = ValidationFunctions.doesItContainValidPathsToCopy().test(sourcePaths); + // then + assertThat(result).isTrue(); + } + + @Test + void shouldValidateWithSuccessDestinationPathsToCopyFiles() { + // given + List<String> sourcePaths = Arrays.asList("/opt/dcae/cacert/cert.p12","/opt/dcae/cacert/p12.pass"); + // when + boolean result = ValidationFunctions.doesItContainValidPathsToCopy().test(sourcePaths); + // then + assertThat(result).isTrue(); + } + + @Test + void shouldValidateWithFailureDestinationPathsWithIncorrectExtension() { + // given + List<String> sourcePaths = Arrays.asList("/opt/dcae/cacert/cert.txt","/opt/dcae/cacert/p12.other"); + // when + boolean result = ValidationFunctions.doesItContainValidPathsToCopy().test(sourcePaths); + // then + assertThat(result).isFalse(); + } + +} |