diff options
author | 2020-09-10 22:03:53 +0200 | |
---|---|---|
committer | 2020-09-14 10:57:59 +0200 | |
commit | 9f597ecbf18bbda75317926c35066b9571736054 (patch) | |
tree | c524613ee646d5d8bf482df6172a83b021cbdd46 | |
parent | 4ad0fafc796a7b65b9d653863d0f57a62eca5fa9 (diff) |
[OOM-CPMv2] Allow optional input parameters
Bug fix in merger to allow KEYSTORE paths to be optional.
Issue-ID: DCAEGEN2-2253
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
Change-Id: I9176d7fdb0e714d849a4ea617ccc4f8eb6a233e1
19 files changed, 417 insertions, 478 deletions
diff --git a/trustStoreMerger/README.md b/trustStoreMerger/README.md index dff8a145..46cce19f 100644 --- a/trustStoreMerger/README.md +++ b/trustStoreMerger/README.md @@ -75,3 +75,6 @@ docker logs oom-merger 9 Missing truststore certificates in provided file 10 Alias conflict detected 11 Cannot save truststore file +12 Cannot copy keystore file +13 Keystore file does not exist +99 Application exited abnormally diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/ExitStatus.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/ExitStatus.java index 01f440cc..2c3945c5 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/ExitStatus.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/ExitStatus.java @@ -22,8 +22,8 @@ package org.onap.oom.truststoremerger.api; public enum ExitStatus { SUCCESS(0, "Success"), - TRUSTSTORES_PATHS_PROVIDER_EXCEPTION(1, "Invalid paths in environment variables"), - MERGER_CONFIGURATION_EXCEPTION(2, "Invalid merger configuration"), + CERTIFICATES_PATHS_VALIDATION_EXCEPTION(1, "Invalid paths in environment variables"), + CONFIGURATION_EXCEPTION(2, "Invalid merger configuration"), TRUSTSTORE_FILE_FACTORY_EXCEPTION(3, "Invalid truststore file-password pair"), PASSWORD_READER_EXCEPTION(4, "Cannot read password from file"), CREATE_BACKUP_EXCEPTION(5, "Cannot create backup file"), diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/AppConfigurationLoader.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/AppConfigurationLoader.java index 7660ff7b..0f538e1d 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/AppConfigurationLoader.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/AppConfigurationLoader.java @@ -21,22 +21,14 @@ package org.onap.oom.truststoremerger.configuration; import org.onap.oom.truststoremerger.api.ExitableException; import org.onap.oom.truststoremerger.configuration.model.AppConfiguration; -import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReader; -import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReaderFactory; -import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider; +import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsSplitter; +import org.onap.oom.truststoremerger.configuration.path.env.EnvReader; public class AppConfigurationLoader { public AppConfiguration loadConfiguration() throws ExitableException { - DelimitedPathsReaderFactory readerFactory = new DelimitedPathsReaderFactory(new EnvProvider()); - DelimitedPathsReader certificatesPathsReader = readerFactory.createCertificatePathsReader(); - DelimitedPathsReader passwordsPathsReader = readerFactory.createPasswordPathsReader(); - DelimitedPathsReader copierPathsReader = readerFactory.createKeystoreCopierPathsReader(); - AppConfigurationProvider factory = new AppConfigurationProvider(certificatesPathsReader, - passwordsPathsReader, - copierPathsReader); + DelimitedPathsSplitter pathsSplitter = new DelimitedPathsSplitter(); + AppConfigurationProvider factory = new AppConfigurationProvider(pathsSplitter, new EnvReader()); return factory.createConfiguration(); } - - } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/AppConfigurationProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/AppConfigurationProvider.java index e1bdbfa3..33ea8e4b 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/AppConfigurationProvider.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/AppConfigurationProvider.java @@ -19,51 +19,65 @@ package org.onap.oom.truststoremerger.configuration; -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_PATHS_ENV; -import static org.onap.oom.truststoremerger.configuration.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.KEYSTORE_DESTINATION_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.KEYSTORE_SOURCE_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PASSWORDS_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PATHS_ENV; + +import java.util.Collections; import java.util.List; -import org.onap.oom.truststoremerger.configuration.exception.MergerConfigurationException; -import org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException; +import java.util.Optional; +import org.onap.oom.truststoremerger.configuration.exception.CertificatesPathsValidationException; +import org.onap.oom.truststoremerger.configuration.exception.ConfigurationException; import org.onap.oom.truststoremerger.configuration.model.AppConfiguration; -import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReader; +import org.onap.oom.truststoremerger.configuration.model.EnvVariable; +import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsSplitter; +import org.onap.oom.truststoremerger.configuration.path.env.EnvReader; public class AppConfigurationProvider { - private final DelimitedPathsReader truststoresPathsReader; - private final DelimitedPathsReader truststoresPasswordsPathsReader; - private final DelimitedPathsReader copierPathsReader; + private final EnvReader envReader; + private final DelimitedPathsSplitter pathsSplitter; - public AppConfigurationProvider(DelimitedPathsReader truststoresPathsReader, - DelimitedPathsReader truststoresPasswordsPathsReader, DelimitedPathsReader copierPathsReader) { - this.truststoresPathsReader = truststoresPathsReader; - this.truststoresPasswordsPathsReader = truststoresPasswordsPathsReader; - this.copierPathsReader = copierPathsReader; + public AppConfigurationProvider(DelimitedPathsSplitter pathsSplitter, EnvReader envReader) { + this.envReader = envReader; + this.pathsSplitter = pathsSplitter; } public AppConfiguration createConfiguration() - throws MergerConfigurationException, TruststoresPathsProviderException { - List<String> truststoresPaths = truststoresPathsReader.get(TRUSTSTORES_PATHS_ENV); - List<String> truststoresPasswordsPaths = truststoresPasswordsPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV); - List<String> sourceKeystorePaths = copierPathsReader.get(KEYSTORE_SOURCE_PATHS_ENV); - List<String> destinationKeystorePaths = copierPathsReader.get(KEYSTORE_DESTINATION_PATHS_ENV); + throws ConfigurationException, CertificatesPathsValidationException { + + List<String> truststoresPaths = getPaths(TRUSTSTORES_PATHS_ENV); + List<String> truststoresPasswordsPaths = getPaths(TRUSTSTORES_PASSWORDS_PATHS_ENV); + List<String> sourceKeystorePaths = getPaths(KEYSTORE_SOURCE_PATHS_ENV); + List<String> destinationKeystorePaths = getPaths(KEYSTORE_DESTINATION_PATHS_ENV); - ensureSameSize(truststoresPaths, truststoresPasswordsPaths, TRUSTSTORES_PATHS_ENV, - TRUSTSTORES_PASSWORDS_PATHS_ENV); - ensureSameSize(sourceKeystorePaths, destinationKeystorePaths, KEYSTORE_SOURCE_PATHS_ENV, - KEYSTORE_DESTINATION_PATHS_ENV); + ensureSameSize(truststoresPaths, truststoresPasswordsPaths, TRUSTSTORES_PATHS_ENV.name(), + TRUSTSTORES_PASSWORDS_PATHS_ENV.name()); + ensureSameSize(sourceKeystorePaths, destinationKeystorePaths, KEYSTORE_SOURCE_PATHS_ENV.name(), + KEYSTORE_DESTINATION_PATHS_ENV.name()); return new AppConfiguration(truststoresPaths, truststoresPasswordsPaths, sourceKeystorePaths, destinationKeystorePaths); } + private List<String> getPaths(EnvVariable envVariable) throws ConfigurationException { + Optional<String> envValue = envReader.getEnv(envVariable.name()); + isMandatoryEnvPresent(envVariable, envValue); + return envValue.isPresent() ? pathsSplitter.getValidatedPaths(envVariable, envValue) : Collections.emptyList(); + } + + private void isMandatoryEnvPresent(EnvVariable envVariable, Optional<String> envValue) { + if (envVariable.isMandatory() && envValue.isEmpty()) { + throw new ConfigurationException(envVariable + " mandatory environment variable is not defined"); + } + } + private void ensureSameSize(List<String> firstList, List<String> secondList, String firstListEnvName, - String secondListEnvName) - throws MergerConfigurationException { + String secondListEnvName) throws ConfigurationException { if (firstList.size() != secondList.size()) { - throw new MergerConfigurationException( + throw new ConfigurationException( "Size of " + firstListEnvName + " does not match size of " + secondListEnvName + " environment variables"); } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/ConfigurationEnvs.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/ConfigurationEnvs.java deleted file mode 100644 index fa72e4b0..00000000 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/ConfigurationEnvs.java +++ /dev/null @@ -1,33 +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; - -public final class ConfigurationEnvs { - - private ConfigurationEnvs() { - - } - - public static final String TRUSTSTORES_PATHS_ENV = "TRUSTSTORES_PATHS"; - public static final String TRUSTSTORES_PASSWORDS_PATHS_ENV = "TRUSTSTORES_PASSWORDS_PATHS"; - public static final String KEYSTORE_SOURCE_PATHS_ENV = "KEYSTORE_SOURCE_PATHS"; - public static final String KEYSTORE_DESTINATION_PATHS_ENV = "KEYSTORE_DESTINATION_PATHS"; - -} diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/TruststoresPathsProviderException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/CertificatesPathsValidationException.java index 6089d314..1069a0fe 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/TruststoresPathsProviderException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/CertificatesPathsValidationException.java @@ -22,9 +22,9 @@ package org.onap.oom.truststoremerger.configuration.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; -public class TruststoresPathsProviderException extends ExitableException { +public class CertificatesPathsValidationException extends ExitableException { - public TruststoresPathsProviderException(String errorMessage) { - super(errorMessage, ExitStatus.TRUSTSTORES_PATHS_PROVIDER_EXCEPTION); + public CertificatesPathsValidationException(String errorMessage) { + super(errorMessage, ExitStatus.CERTIFICATES_PATHS_VALIDATION_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/MergerConfigurationException.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/ConfigurationException.java index 4bdfd9f4..ffb99dae 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/MergerConfigurationException.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/exception/ConfigurationException.java @@ -22,9 +22,9 @@ package org.onap.oom.truststoremerger.configuration.exception; import org.onap.oom.truststoremerger.api.ExitStatus; import org.onap.oom.truststoremerger.api.ExitableException; -public class MergerConfigurationException extends ExitableException { +public class ConfigurationException extends ExitableException { - public MergerConfigurationException(String errorMessage) { - super(errorMessage, ExitStatus.MERGER_CONFIGURATION_EXCEPTION); + public ConfigurationException(String errorMessage) { + super(errorMessage, ExitStatus.CONFIGURATION_EXCEPTION); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/model/EnvVariable.java index b2a64f97..456c58f8 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactory.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/model/EnvVariable.java @@ -17,30 +17,35 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.configuration.path; +package org.onap.oom.truststoremerger.configuration.model; import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidCertificatesPaths; import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidPasswordPaths; import static org.onap.oom.truststoremerger.configuration.path.validation.ValidationFunctions.doesItContainValidPathsToCopy; -import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider; +import java.util.List; +import java.util.function.Predicate; -public final class DelimitedPathsReaderFactory { - private final EnvProvider envProvider; +public enum EnvVariable { + TRUSTSTORES_PATHS_ENV(true, doesItContainValidCertificatesPaths()), + TRUSTSTORES_PASSWORDS_PATHS_ENV(true, doesItContainValidPasswordPaths()), + KEYSTORE_SOURCE_PATHS_ENV(false, doesItContainValidPathsToCopy()), + KEYSTORE_DESTINATION_PATHS_ENV(false, doesItContainValidPathsToCopy()); - public DelimitedPathsReaderFactory(EnvProvider envProvider) { - this.envProvider = envProvider; - } + boolean isMandatory; + + Predicate<List<String>> validationFunction; - public DelimitedPathsReader createPasswordPathsReader() { - return new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths()); + EnvVariable(boolean isMandatory, Predicate<List<String>> validationFunction) { + this.isMandatory = isMandatory; + this.validationFunction = validationFunction; } - public DelimitedPathsReader createCertificatePathsReader() { - return new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths()); + public boolean isMandatory() { + return isMandatory; } - public DelimitedPathsReader createKeystoreCopierPathsReader() { - return new DelimitedPathsReader(envProvider, doesItContainValidPathsToCopy()); + public Predicate<List<String>> getValidationFunction() { + return validationFunction; } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReader.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsSplitter.java index e5de4d08..b7a4f4c5 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReader.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsSplitter.java @@ -21,30 +21,22 @@ package org.onap.oom.truststoremerger.configuration.path; import java.util.Arrays; import java.util.List; -import java.util.function.Predicate; -import org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException; -import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider; +import java.util.Optional; +import org.onap.oom.truststoremerger.configuration.exception.CertificatesPathsValidationException; +import org.onap.oom.truststoremerger.configuration.model.EnvVariable; -public class DelimitedPathsReader { +public class DelimitedPathsSplitter { private static final String DELIMITER = ":"; private static final int NEGATIVE_SPLIT_LIMIT = -1; - private final EnvProvider envProvider; - private final Predicate<List<String>> pathsValidator; - - DelimitedPathsReader(EnvProvider envProvider, Predicate<List<String>> pathsValidator) { - this.envProvider = envProvider; - this.pathsValidator = pathsValidator; - } - - public List<String> get(String envName) throws TruststoresPathsProviderException { - return envProvider.getEnv(envName) - .filter(this::hasValue) + public List<String> getValidatedPaths(EnvVariable envVariable, Optional<String> envValue) + throws CertificatesPathsValidationException { + return envValue.filter(this::hasValue) .map(this::splitToList) - .filter(pathsValidator) - .orElseThrow(() -> new TruststoresPathsProviderException( - envName + " environment variable does not contain valid paths")); + .filter(envVariable.getValidationFunction()) + .orElseThrow(() -> new CertificatesPathsValidationException( + envVariable + " environment variable does not contain valid paths")); } private boolean hasValue(String envValue) { diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/env/EnvProvider.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/env/EnvReader.java index 1dd127d0..cb6099c1 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/env/EnvProvider.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/env/EnvReader.java @@ -20,17 +20,20 @@ package org.onap.oom.truststoremerger.configuration.path.env; import java.util.Optional; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class EnvProvider { +public class EnvReader { - private static final Logger LOGGER = LoggerFactory.getLogger(EnvProvider.class); + private static final Logger LOGGER = LoggerFactory.getLogger(EnvReader.class); public Optional<String> getEnv(String name) { + return getSystemEnv(name); + } + + Optional<String> getSystemEnv(String name) { String value = System.getenv(name); LOGGER.info("Read variable: {} , value: {}", name, value); - return Optional.ofNullable(System.getenv(name)); + return Optional.ofNullable(value); } } diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctions.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctions.java index f4cfa60c..535d2e6e 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctions.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctions.java @@ -25,7 +25,7 @@ import java.util.function.Predicate; public final class ValidationFunctions { private static final String CERTIFICATE_PATH_REGEX = "^(/[a-zA-Z0-9_-]+)+\\.(pem|jks|p12)"; - private static final String TRUSTSTORE_PASSWORD_PATH_REGEX = "^(/[a-zA-Z0-9_-]+)+\\.pass"; + private static final String CERTIFICATE_PASSWORD_PATH_REGEX = "^(/[a-zA-Z0-9_-]+)+\\.pass"; private ValidationFunctions() { } @@ -40,7 +40,7 @@ public final class ValidationFunctions { public static Predicate<List<String>> doesItContainValidPathsToCopy() { return paths -> paths.stream().allMatch(path -> - doesMatch(path, TRUSTSTORE_PASSWORD_PATH_REGEX) || isCertificatePathValid(path)); + doesMatch(path, CERTIFICATE_PASSWORD_PATH_REGEX) || isCertificatePathValid(path)); } private static boolean isCertificatePathValid(String path) { @@ -48,7 +48,7 @@ public final class ValidationFunctions { } private static boolean isCertificatePasswordPathValid(String path) { - return path.isEmpty() || doesMatch(path, TRUSTSTORE_PASSWORD_PATH_REGEX); + return path.isEmpty() || doesMatch(path, CERTIFICATE_PASSWORD_PATH_REGEX); } private static boolean doesMatch(String path, String regex) { diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/copier/KeystoreCopier.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/copier/KeystoreCopier.java index 822979c1..7678397a 100644 --- a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/copier/KeystoreCopier.java +++ b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/copier/KeystoreCopier.java @@ -26,20 +26,23 @@ import org.onap.oom.truststoremerger.common.FileTools; import org.onap.oom.truststoremerger.configuration.model.AppConfiguration; import org.onap.oom.truststoremerger.copier.exception.KeystoreFileCopyException; import org.onap.oom.truststoremerger.copier.exception.KeystoreNotExistException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class KeystoreCopier { - final private FileTools fileTools; + private static final Logger LOGGER = LoggerFactory.getLogger(KeystoreCopier.class); + private final FileTools fileTools; public KeystoreCopier(FileTools fileTools) { this.fileTools = fileTools; } public void copyKeystores(AppConfiguration configuration) { + final List<String> sources = configuration.getSourceKeystorePaths(); + final List<String> destinations = configuration.getDestinationKeystorePaths(); + containsPaths(sources); try { - final List<String> sources = configuration.getSourceKeystorePaths(); - final List<String> destinations = configuration.getDestinationKeystorePaths(); - for (int i = 0; i < sources.size(); i++) { copy(sources.get(i), destinations.get(i)); } @@ -48,6 +51,12 @@ public class KeystoreCopier { } } + private void containsPaths(List<String> sources) { + if (sources.size() == 0) { + LOGGER.info("No Keystore files to copy"); + } + } + private void copy(String sourcePath, String destinationPath) throws IOException { final File source = new File(sourcePath); final File destination = new File(destinationPath); diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/ExtensionResolverTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/ExtensionResolverTest.java index e59a7671..0ed17d0c 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/ExtensionResolverTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/ExtensionResolverTest.java @@ -17,7 +17,6 @@ * ============LICENSE_END========================================================= */ - package org.onap.oom.truststoremerger.common; import org.junit.jupiter.params.ParameterizedTest; diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/AppConfigurationProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/AppConfigurationProviderTest.java new file mode 100644 index 00000000..a8da405d --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/AppConfigurationProviderTest.java @@ -0,0 +1,141 @@ +/*============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; + +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.model.EnvVariable.KEYSTORE_DESTINATION_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.KEYSTORE_SOURCE_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PASSWORDS_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PATHS_ENV; + +import java.util.ArrayList; +import java.util.List; +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.CertificatesPathsValidationException; +import org.onap.oom.truststoremerger.configuration.exception.ConfigurationException; +import org.onap.oom.truststoremerger.configuration.model.AppConfiguration; +import org.onap.oom.truststoremerger.configuration.path.DelimitedPathsSplitter; +import org.onap.oom.truststoremerger.configuration.path.env.EnvReader; + +@ExtendWith(MockitoExtension.class) +class AppConfigurationProviderTest { + + private static final String BASE_TRUSTSTORE_PATH = "/opt/app/truststore_"; + private static final String JKS_EXTENSION = ".jks"; + private static final String PASS_EXTENSION = ".pass"; + private static final String TRUSTSTORES_PATHS = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.pem"; + private static final String TRUSTSTORES_PASSWORDS_PATHS = "/opt/app/certificates/truststore.pass:/trust.pass"; + + @Mock + private DelimitedPathsSplitter pathsSplitter; + @Mock + private EnvReader envReader; + private AppConfigurationProvider provider; + + @BeforeEach + void setUp() { + provider = new AppConfigurationProvider(pathsSplitter, envReader); + } + + @Test + void shouldThrowExceptionWhenMandatoryEnvNotPresent() { + // given + when(envReader.getEnv(TRUSTSTORES_PATHS_ENV.name())).thenReturn(Optional.empty()); + // when, then + assertThatExceptionOfType(ConfigurationException.class).isThrownBy(() -> provider.createConfiguration()) + .withMessageContaining(TRUSTSTORES_PATHS_ENV + " mandatory environment variable is not defined"); + } + + @Test + void shouldThrowExceptionWhenTrustorePathsSizesDoNotMatch() { + // given + List<String> truststores = createListOfPathsWithExtension(2, JKS_EXTENSION); + List<String> truststoresPasswords = createListOfPathsWithExtension(1, PASS_EXTENSION); + + mockTruststorePaths(truststores, truststoresPasswords); + // when + assertThatExceptionOfType(ConfigurationException.class) + .isThrownBy(() -> provider.createConfiguration()) + .withMessageContaining("Size of " + TRUSTSTORES_PATHS_ENV + + " does not match size of " + TRUSTSTORES_PASSWORDS_PATHS_ENV + " environment variables"); + } + + @Test + void shouldReturnEmptyListWhenOptionalEnvNotPresent() { + // given + List<String> truststores = createListOfPathsWithExtension(2, JKS_EXTENSION); + List<String> truststoresPasswords = createListOfPathsWithExtension(2, PASS_EXTENSION); + mockTruststorePaths(truststores, truststoresPasswords); + mockKeystorePaths(Optional.empty(), Optional.empty()); + // when + AppConfiguration paths = provider.createConfiguration(); + // then + assertThat(paths.getDestinationKeystorePaths()).isEmpty(); + assertThat(paths.getSourceKeystorePaths()).isEmpty(); + } + + private void mockTruststorePaths(List<String> truststores, List<String> truststoresPasswords) { + mockTruststores(truststores); + mockTruststoresPasswords(truststoresPasswords); + } + + private void mockKeystorePaths(Optional<String> sourceKeystoresPairPaths, Optional<String> destKeystoresPairPaths) { + mockKeystoreCopierSourcePaths(sourceKeystoresPairPaths); + mockKeystoreCopierDestinationPaths(destKeystoresPairPaths); + } + + private void mockTruststores(List<String> truststores) throws CertificatesPathsValidationException { + when(envReader.getEnv(TRUSTSTORES_PATHS_ENV.name())).thenReturn(Optional.of(TRUSTSTORES_PATHS)); + when(pathsSplitter.getValidatedPaths(TRUSTSTORES_PATHS_ENV, Optional.of(TRUSTSTORES_PATHS))) + .thenReturn(truststores); + } + + private void mockTruststoresPasswords(List<String> truststoresPasswords) + throws CertificatesPathsValidationException { + Optional<String> passwordsPaths = Optional.of(TRUSTSTORES_PASSWORDS_PATHS); + when(envReader.getEnv(TRUSTSTORES_PASSWORDS_PATHS_ENV.name())).thenReturn(passwordsPaths); + when(pathsSplitter.getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS_ENV, passwordsPaths)) + .thenReturn(truststoresPasswords); + } + + private void mockKeystoreCopierSourcePaths(Optional<String> paths) { + when(envReader.getEnv(KEYSTORE_SOURCE_PATHS_ENV.name())).thenReturn(paths); + } + + private void mockKeystoreCopierDestinationPaths(Optional<String> paths) { + when(envReader.getEnv(KEYSTORE_DESTINATION_PATHS_ENV.name())).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; + } + +} 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 deleted file mode 100644 index 026199f4..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProviderTest.java +++ /dev/null @@ -1,172 +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; - -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 org.onap.oom.truststoremerger.configuration.exception.MergerConfigurationException; -import org.onap.oom.truststoremerger.configuration.exception.TruststoresPathsProviderException; -import org.onap.oom.truststoremerger.configuration.model.AppConfiguration; -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 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 DelimitedPathsReader certificatesPathsProvider; - @Mock - private DelimitedPathsReader passwordsPathsProvider; - @Mock - private DelimitedPathsReader copierPathsReader; - private AppConfigurationProvider factory; - - @BeforeEach - void setUp() { - factory = new AppConfigurationProvider(certificatesPathsProvider, passwordsPathsProvider, copierPathsReader); - } - - @Test - void shouldReturnConfigurationWithCorrectPaths() - throws TruststoresPathsProviderException, MergerConfigurationException { - int numberOfPaths = 5; - 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); - - AppConfiguration 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 shouldThrowExceptionWhenTruststoresLengthDifferentThanTruststoresPasswordsLength() - throws TruststoresPathsProviderException { - int numberOfCertificates = 5; - int numberOfTruststoresPasswords = 4; - 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); - } - - @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(certificatesPathsProvider.get(TRUSTSTORES_PATHS_ENV)).thenReturn(truststores); - } - - private void mockTruststoresPasswords(List<String> truststoresPasswords) throws TruststoresPathsProviderException { - 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 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(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/DelimitedPathsReaderFactoryTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactoryTest.java deleted file mode 100644 index 5a7e9cd0..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactoryTest.java +++ /dev/null @@ -1,39 +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.api.Test; -import org.onap.oom.truststoremerger.configuration.path.env.EnvProvider; - -class DelimitedPathsReaderFactoryTest { - - @Test - 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/DelimitedPathsReaderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderTest.java deleted file mode 100644 index 408a7d6d..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderTest.java +++ /dev/null @@ -1,144 +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 static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.mockito.Mockito.when; -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.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 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:"; - private static final String VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE = "/opt/app/certificates/truststore.pass::/etc/truststore.pass"; - private static final String INVALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.invalid"; - private static final String INVALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:/.pass"; - - @Mock - private EnvProvider envProvider; - private DelimitedPathsReader delimitedPathsReader; - - @Test - void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException { - // given - delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths()); - mockTruststoresEnv(VALID_TRUSTSTORES); - - // 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); - - // 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); - - // when, then - assertThat(delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)).containsSequence( - "/opt/app/certificates/truststore.pass", - "", - "/etc/truststore.pass" - ); - } - - @Test - void shouldThrowExceptionWhenTruststoresPasswordsPathEnvIsEmpty() { - // given - delimitedPathsReader = new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths()); - mockTruststoresPasswordsEnv(""); - - // when, then - assertThatExceptionOfType(TruststoresPathsProviderException.class) - .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(() -> delimitedPathsReader.get(TRUSTSTORES_PASSWORDS_PATHS_ENV)); - } - - private void mockTruststoresEnv(String truststores) { - mockEnv(TRUSTSTORES_PATHS_ENV, truststores); - } - - private void mockTruststoresPasswordsEnv(String truststoresPasswords) { - mockEnv(TRUSTSTORES_PASSWORDS_PATHS_ENV, truststoresPasswords); - } - - 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/DelimitedPathsSplitterTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsSplitterTest.java new file mode 100644 index 00000000..22b5a7fc --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsSplitterTest.java @@ -0,0 +1,107 @@ +/*============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 static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PASSWORDS_PATHS_ENV; +import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PATHS_ENV; + +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.junit.jupiter.MockitoExtension; +import org.onap.oom.truststoremerger.configuration.exception.CertificatesPathsValidationException; + +@ExtendWith(MockitoExtension.class) +class DelimitedPathsSplitterTest { + + 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:"; + private static final String VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE = "/opt/app/certificates/truststore.pass::/etc/truststore.pass"; + private static final String INVALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.invalid"; + private static final String INVALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:/.pass"; + + private DelimitedPathsSplitter delimitedPathsSplitter; + + @BeforeEach + void setUp() { + delimitedPathsSplitter = new DelimitedPathsSplitter(); + } + + @Test + void shouldReturnCorrectListWhenTruststoresValid() { + // when, then + assertThat(delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PATHS_ENV, Optional.of(VALID_TRUSTSTORES))) + .containsSequence("/opt/app/certificates/truststore.jks", + "/opt/app/certificates/truststore.pem"); + } + + @Test + void shouldThrowExceptionWhenTruststoresPathsEnvIsEmpty() { + // when, then + assertThatExceptionOfType(CertificatesPathsValidationException.class) + .isThrownBy(() -> delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PATHS_ENV, Optional.of(""))); + } + + @Test + void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() { + // when, then + assertThatExceptionOfType(CertificatesPathsValidationException.class) + .isThrownBy(() -> delimitedPathsSplitter + .getValidatedPaths(TRUSTSTORES_PATHS_ENV, Optional.of(INVALID_TRUSTSTORES))); + } + + @Test + void shouldReturnCorrectListWhenTruststoresPasswordsValid() { + // when, then + assertThat(delimitedPathsSplitter + .getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS_ENV, Optional.of(VALID_TRUSTSTORES_PASSWORDS))) + .containsSequence("/opt/app/certificates/truststore.pass", ""); + } + + @Test + void shouldReturnCorrectListWhenTruststoresPasswordsContainsEmptyPathsInTheMiddle() { + // when, then + assertThat(delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS_ENV, + Optional.of(VALID_TRUSTSTORES_PASSWORDS_WITH_EMPTY_IN_THE_MIDDLE))).containsSequence( + "/opt/app/certificates/truststore.pass", + "", + "/etc/truststore.pass" + ); + } + + @Test + void shouldThrowExceptionWhenTruststoresPasswordsPathEnvIsEmpty() { + // when, then + assertThatExceptionOfType(CertificatesPathsValidationException.class) + .isThrownBy( + () -> delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS_ENV, Optional.of(""))); + } + + @Test + void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() { + // when, then + assertThatExceptionOfType(CertificatesPathsValidationException.class) + .isThrownBy(() -> delimitedPathsSplitter + .getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS_ENV, Optional.of(INVALID_TRUSTSTORES_PASSWORDS))); + } +} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/env/EnvReaderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/env/EnvReaderTest.java new file mode 100644 index 00000000..0a654fe8 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/env/EnvReaderTest.java @@ -0,0 +1,62 @@ +/*============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.env; + + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; +import static org.onap.oom.truststoremerger.configuration.model.EnvVariable.TRUSTSTORES_PASSWORDS_PATHS_ENV; + +import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +class EnvReaderTest { + + private static final String SAMPLE_PASS_PATH = "/sample/path/trust.pass"; + EnvReader provider; + + @BeforeEach + void setUp() { + provider = Mockito.spy(EnvReader.class); + } + + @Test + void shouldReturnOptionalWithEnv() { + // given + String envName = TRUSTSTORES_PASSWORDS_PATHS_ENV.name(); + when(provider.getSystemEnv(envName)).thenReturn(Optional.of(SAMPLE_PASS_PATH)); + // when + Optional<String> result = provider.getEnv(envName); + // then + assertThat(result).isEqualTo(Optional.of(SAMPLE_PASS_PATH)); + } + + @Test + void shouldReturnEmptyOptional() { + // given + String envName = TRUSTSTORES_PASSWORDS_PATHS_ENV.name(); + // when + Optional<String> result = provider.getEnv(envName); + // then + assertThat(result).isEmpty(); + } +} |