Add of config for copier 29/112229/5
authorkjaniak <kornel.janiak@nokia.com>
Mon, 7 Sep 2020 12:13:33 +0000 (14:13 +0200)
committerkjaniak <kornel.janiak@nokia.com>
Tue, 8 Sep 2020 13:04:04 +0000 (15:04 +0200)
Add Source and Destination paths to merger.

Issue-ID: DCAEGEN2-2253
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
Change-Id: Id0ec28d8d7f90c5ce61d5393b6e1d8363dbfd24d

19 files changed:
trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/TrustStoreMerger.java
trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/ExitStatus.java
trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreController.java
trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/ConfigurationEnvs.java [moved from trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/api/ConfigurationEnvs.java with 78% similarity]
trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProvider.java
trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/model/MergerConfiguration.java
trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReader.java [moved from trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProvider.java with 51% similarity]
trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactory.java [new file with mode: 0644]
trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/PathValidator.java [deleted file]
trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactory.java [deleted file]
trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/env/EnvProvider.java [moved from trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/EnvProvider.java with 90% similarity]
trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctions.java [new file with mode: 0644]
trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java
trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java
trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationProviderTest.java
trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderFactoryTest.java [moved from trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactoryTest.java with 64% similarity]
trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/DelimitedPathsReaderTest.java [moved from trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderTest.java with 59% similarity]
trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/PathValidatorTest.java [deleted file]
trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/path/validation/ValidationFunctionsTest.java [new file with mode: 0644]

index e2e0c62..babd32e 100644 (file)
 
 package org.onap.oom.truststoremerger;
 
+import java.util.List;
 import org.onap.oom.truststoremerger.api.ExitStatus;
 import org.onap.oom.truststoremerger.api.ExitableException;
-import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias;
+import org.onap.oom.truststoremerger.certification.file.TruststoreFileFactory;
+import org.onap.oom.truststoremerger.certification.file.TruststoreFilesListProvider;
 import org.onap.oom.truststoremerger.certification.file.model.Truststore;
 import org.onap.oom.truststoremerger.certification.file.provider.FileManager;
 import org.onap.oom.truststoremerger.certification.file.provider.PasswordReader;
-import org.onap.oom.truststoremerger.certification.file.TruststoreFileFactory;
-import org.onap.oom.truststoremerger.certification.file.TruststoreFilesListProvider;
-import org.onap.oom.truststoremerger.configuration.path.TruststoresPathsProviderFactory;
-import org.onap.oom.truststoremerger.configuration.path.TruststoresPathsProvider;
-import org.onap.oom.truststoremerger.configuration.model.MergerConfiguration;
+import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias;
 import org.onap.oom.truststoremerger.configuration.MergerConfigurationProvider;
-
-import java.util.List;
+import org.onap.oom.truststoremerger.configuration.model.MergerConfiguration;
+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.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -55,12 +55,15 @@ class TrustStoreMerger {
         } catch (ExitableException e) {
             LOGGER.error("Truststore Merger fails in execution: ", e);
             appExitHandler.exit(e.applicationExitStatus());
+        } catch (Exception e) {
+            LOGGER.error("Truststore Merger fails in execution: ", e);
+            appExitHandler.exit(ExitStatus.UNEXPECTED_EXCEPTION);
         }
     }
 
     private void mergeTruststores() throws ExitableException {
         MergerConfiguration configuration = loadConfiguration();
-        List<Truststore> truststoreFilesList = getTruststoreFilesList(configuration);
+        List<Truststore> truststoreFilesList = getTruststoreFiles(configuration);
 
         Truststore baseFile = truststoreFilesList.get(FIRST_TRUSTSTORE_INDEX);
         baseFile.createBackup();
@@ -74,12 +77,17 @@ class TrustStoreMerger {
     }
 
     private MergerConfiguration loadConfiguration() throws ExitableException {
-        TruststoresPathsProvider truststoresPathsProvider = TruststoresPathsProviderFactory.create();
-        MergerConfigurationProvider factory = new MergerConfigurationProvider(truststoresPathsProvider);
+        DelimitedPathsReaderFactory readerFactory = new DelimitedPathsReaderFactory(new EnvProvider());
+        DelimitedPathsReader certificatesPathsReader = readerFactory.createCertificatePathsReader();
+        DelimitedPathsReader passwordsPathsReader = readerFactory.createPasswordPathsReader();
+        DelimitedPathsReader copierPathsReader = readerFactory.createKeystoreCopierPathsReader();
+        MergerConfigurationProvider factory = new MergerConfigurationProvider(certificatesPathsReader,
+            passwordsPathsReader,
+            copierPathsReader);
         return factory.createConfiguration();
     }
 
-    private List<Truststore> getTruststoreFilesList(MergerConfiguration configuration) throws ExitableException {
+    private List<Truststore> getTruststoreFiles(MergerConfiguration configuration) throws ExitableException {
         TruststoreFileFactory truststoreFileFactory = new TruststoreFileFactory(new FileManager(),
             new PasswordReader());
         TruststoreFilesListProvider truststoreFilesListProvider = new TruststoreFilesListProvider(
index b9111bf..d3e246a 100644 (file)
@@ -32,7 +32,8 @@ public enum ExitStatus {
     TRUSTSTORE_DATA_OPERATION_EXCEPTION(8, "Cannot operate on truststore data"),
     MISSING_TRUSTSTORE_EXCEPTION(9, "Missing truststore certificates in provided file"),
     ALIAS_CONFLICT_EXCEPTION(10, "Alias conflict detected"),
-    WRITE_TRUSTSTORE_FILE_EXCEPTION(11, "Cannot save truststore file");
+    WRITE_TRUSTSTORE_FILE_EXCEPTION(11, "Cannot save truststore file"),
+    UNEXPECTED_EXCEPTION(99, "Application exited abnormally");
 
 
     private final int value;
index 1c20fd3..a4b129c 100644 (file)
@@ -28,13 +28,13 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import org.onap.oom.truststoremerger.api.ExitableException;
-import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias;
-import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAliasFactory;
 import org.onap.oom.truststoremerger.certification.file.exception.AliasConflictException;
-import org.onap.oom.truststoremerger.certification.file.exception.TruststoreDataOperationException;
 import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException;
 import org.onap.oom.truststoremerger.certification.file.exception.MissingTruststoreException;
+import org.onap.oom.truststoremerger.certification.file.exception.TruststoreDataOperationException;
 import org.onap.oom.truststoremerger.certification.file.exception.WriteTruststoreFileException;
+import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias;
+import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAliasFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -55,7 +55,7 @@ public class JavaCertificateStoreController implements CertificateController {
     }
 
     public List<CertificateWithAlias> getNotEmptyCertificateList() throws ExitableException {
-        List<String> aliases = getTruststoreAliasesList();
+        List<String> aliases = getTruststoreAliases();
         if (aliases.isEmpty()) {
             throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath());
         }
@@ -64,7 +64,7 @@ public class JavaCertificateStoreController implements CertificateController {
 
     public void addCertificates(List<CertificateWithAlias> certificatesWithAliases)
         throws ExitableException {
-        if (getTruststoreAliasesList().isEmpty()){
+        if (getTruststoreAliases().isEmpty()) {
             throw new MissingTruststoreException("Missing certificate aliases in file: " + storeFile.getPath());
         }
         for (CertificateWithAlias certificate : certificatesWithAliases) {
@@ -133,7 +133,7 @@ public class JavaCertificateStoreController implements CertificateController {
         }
     }
 
-    private List<String> getTruststoreAliasesList() throws TruststoreDataOperationException {
+    private List<String> getTruststoreAliases() throws TruststoreDataOperationException {
         try {
             List<String> aliases = Collections.list(keyStore.aliases());
             return getFilteredAlias(aliases);
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.oom.truststoremerger.api;
+package org.onap.oom.truststoremerger.configuration;
 
-public class ConfigurationEnvs {
+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";
+
 }
index 5a40a64..5cbf8b2 100644 (file)
 
 package org.onap.oom.truststoremerger.configuration;
 
-import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV;
-import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV;
+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 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 org.onap.oom.truststoremerger.configuration.path.DelimitedPathsReader;
 
 public class MergerConfigurationProvider {
 
-    private final TruststoresPathsProvider pathsProvider;
+    private final DelimitedPathsReader truststoresPathsReader;
+    private final DelimitedPathsReader truststoresPasswordsPathsReader;
+    private final DelimitedPathsReader copierPathsReader;
 
-    public MergerConfigurationProvider(TruststoresPathsProvider pathsProvider) {
-        this.pathsProvider = pathsProvider;
+    public MergerConfigurationProvider(DelimitedPathsReader truststoresPathsReader,
+        DelimitedPathsReader truststoresPasswordsPathsReader, DelimitedPathsReader copierPathsReader) {
+        this.truststoresPathsReader = truststoresPathsReader;
+        this.truststoresPasswordsPathsReader = truststoresPasswordsPathsReader;
+        this.copierPathsReader = copierPathsReader;
     }
 
     public MergerConfiguration createConfiguration()
         throws MergerConfigurationException, TruststoresPathsProviderException {
-        List<String> truststores = pathsProvider.getTruststores();
-        List<String> truststoresPasswords = pathsProvider.getTruststoresPasswords();
+        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);
 
-        if (truststores.size() != truststoresPasswords.size()) {
+        ensureSameSize(truststoresPaths, truststoresPasswordsPaths, TRUSTSTORES_PATHS_ENV,
+            TRUSTSTORES_PASSWORDS_PATHS_ENV);
+        ensureSameSize(sourceKeystorePaths, destinationKeystorePaths, KEYSTORE_SOURCE_PATHS_ENV,
+            KEYSTORE_DESTINATION_PATHS_ENV);
+
+        return new MergerConfiguration(truststoresPaths, truststoresPasswordsPaths, sourceKeystorePaths,
+            destinationKeystorePaths);
+    }
+
+    private void ensureSameSize(List<String> firstList, List<String> secondList, String firstListEnvName,
+        String secondListEnvName)
+        throws MergerConfigurationException {
+        if (firstList.size() != secondList.size()) {
             throw new MergerConfigurationException(
-                "Size of " + TRUSTSTORES_PATHS_ENV
-                    + " does not match size of " + TRUSTSTORES_PASSWORDS_PATHS_ENV + " environment variables");
+                "Size of " + firstListEnvName
+                    + " does not match size of " + secondListEnvName + " environment variables");
         }
-
-        return new MergerConfiguration(truststores, truststoresPasswords);
     }
 }
index 3b807c3..0809ab5 100644 (file)
@@ -25,20 +25,32 @@ import java.util.List;
 public final class MergerConfiguration {
     private final List<String> truststoreFilePaths;
     private final List<String> truststoreFilePasswordPaths;
+    private final List<String> sourceKeystorePaths;
+    private final List<String> destinationKeystorePaths;
 
     public MergerConfiguration(List<String> truststoreFilePaths,
-                               List<String> truststoreFilePasswordPaths) {
+        List<String> truststoreFilePasswordPaths, List<String> sourceKeystorePaths,
+        List<String> destinationKeystorePaths) {
         this.truststoreFilePaths = List.copyOf(truststoreFilePaths);
         this.truststoreFilePasswordPaths = List.copyOf(truststoreFilePasswordPaths);
+        this.sourceKeystorePaths = List.copyOf(sourceKeystorePaths);
+        this.destinationKeystorePaths = List.copyOf(destinationKeystorePaths);
     }
 
     public List<String> getTruststoreFilePaths() {
         return Collections.unmodifiableList(truststoreFilePaths);
     }
 
-
     public List<String> getTruststoreFilePasswordPaths() {
         return Collections.unmodifiableList(truststoreFilePasswordPaths);
     }
 
+
+    public List<String> getDestinationKeystorePaths() {
+        return Collections.unmodifiableList(destinationKeystorePaths);
+    }
+
+    public List<String> getSourceKeystorePaths() {
+        return Collections.unmodifiableList(sourceKeystorePaths);
+    }
 }
 
 package org.onap.oom.truststoremerger.configuration.path;
 
-import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PATHS_ENV;
-import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_PATHS_ENV;
-
 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;
 
-public class TruststoresPathsProvider {
+public class DelimitedPathsReader {
 
     private static final String DELIMITER = ":";
     private static final int NEGATIVE_SPLIT_LIMIT = -1;
 
     private final EnvProvider envProvider;
-    private final PathValidator pathValidator;
+    private final Predicate<List<String>> pathsValidator;
 
-    TruststoresPathsProvider(EnvProvider envProvider, PathValidator pathValidator) {
+    DelimitedPathsReader(EnvProvider envProvider, Predicate<List<String>> pathsValidator) {
         this.envProvider = envProvider;
-        this.pathValidator = pathValidator;
+        this.pathsValidator = pathsValidator;
     }
 
-    public List<String> getTruststores() throws TruststoresPathsProviderException {
-        return envProvider.getEnv(TRUSTSTORES_PATHS_ENV)
-            .filter(Predicate.not(String::isEmpty))
+    public List<String> get(String envName) throws TruststoresPathsProviderException {
+        return envProvider.getEnv(envName)
+            .filter(this::hasValue)
             .map(this::splitToList)
-            .filter(this::validateTruststores)
+            .filter(pathsValidator)
             .orElseThrow(() -> new TruststoresPathsProviderException(
-                TRUSTSTORES_PATHS_ENV + " environment variable does not contain valid truststores paths"));
-    }
-
-    public List<String> getTruststoresPasswords() throws TruststoresPathsProviderException {
-        return envProvider.getEnv(TRUSTSTORES_PASSWORDS_PATHS_ENV)
-            .map(this::splitToList)
-            .filter(this::validateTruststoresPasswords)
-            .orElseThrow(() -> new TruststoresPathsProviderException(
-                TRUSTSTORES_PASSWORDS_PATHS_ENV + " environment variable does not contain valid passwords paths"));
-    }
-
-    private boolean validateTruststores(List<String> truststores) {
-        return truststores.stream()
-            .allMatch(pathValidator::isTruststorePathValid);
+                envName + " environment variable does not contain valid paths"));
     }
 
-    private boolean validateTruststoresPasswords(List<String> truststoresPasswords) {
-        return truststoresPasswords.stream()
-            .allMatch(pathValidator::isTruststorePasswordPathValid);
+    private boolean hasValue(String envValue) {
+        return !envValue.isEmpty();
     }
 
     private List<String> splitToList(String stringToSplit) {
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/path/DelimitedPathsReaderFactory.java
new file mode 100644 (file)
index 0000000..b2a64f9
--- /dev/null
@@ -0,0 +1,46 @@
+/*============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.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;
+
+public final class DelimitedPathsReaderFactory {
+    private final EnvProvider envProvider;
+
+    public DelimitedPathsReaderFactory(EnvProvider envProvider) {
+        this.envProvider = envProvider;
+    }
+
+    public DelimitedPathsReader createPasswordPathsReader() {
+        return new DelimitedPathsReader(envProvider, doesItContainValidPasswordPaths());
+    }
+
+    public DelimitedPathsReader createCertificatePathsReader() {
+        return new DelimitedPathsReader(envProvider, doesItContainValidCertificatesPaths());
+    }
+
+    public DelimitedPathsReader createKeystoreCopierPathsReader() {
+        return new DelimitedPathsReader(envProvider, doesItContainValidPathsToCopy());
+    }
+}
diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/PathValidator.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/PathValidator.java
deleted file mode 100644 (file)
index 4240228..0000000
+++ /dev/null
@@ -1,38 +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;
-
-class PathValidator {
-
-    private static final String TRUSTSTORE_PATH_REGEX = "^(/[a-zA-Z0-9_-]+)+\\.(pem|jks|p12)";
-    private static final String TRUSTSTORE_PASSWORD_PATH_REGEX = "^(/[a-zA-Z0-9_-]+)+\\.pass";
-
-    boolean isTruststorePathValid(String truststorePath) {
-        return isPathValid(truststorePath, TRUSTSTORE_PATH_REGEX);
-    }
-
-    boolean isTruststorePasswordPathValid(String truststorePasswordPath) {
-        return truststorePasswordPath.isEmpty() || isPathValid(truststorePasswordPath, TRUSTSTORE_PASSWORD_PATH_REGEX);
-    }
-
-    private boolean isPathValid(String path, String regex) {
-        return path.matches(regex);
-    }
-}
diff --git a/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactory.java b/trustStoreMerger/src/main/java/org/onap/oom/truststoremerger/configuration/path/TruststoresPathsProviderFactory.java
deleted file mode 100644 (file)
index 7a99910..0000000
+++ /dev/null
@@ -1,30 +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;
-
-public class TruststoresPathsProviderFactory {
-
-    private TruststoresPathsProviderFactory() {
-    }
-
-    public static TruststoresPathsProvider create() {
-        return new TruststoresPathsProvider(new EnvProvider(), new PathValidator());
-    }
-}
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.oom.truststoremerger.configuration.path;
+package org.onap.oom.truststoremerger.configuration.path.env;
 
 import java.util.Optional;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-class EnvProvider {
+public class EnvProvider {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(EnvProvider.class);
 
-    Optional<String> getEnv(String name) {
+    public Optional<String> getEnv(String name) {
         String value = System.getenv(name);
         LOGGER.info("Read variable: {} , value: {}", name, value);
         return Optional.ofNullable(System.getenv(name));
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
new file mode 100644 (file)
index 0000000..f4cfa60
--- /dev/null
@@ -0,0 +1,57 @@
+/*============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 java.util.List;
+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 ValidationFunctions() {
+    }
+
+    public static Predicate<List<String>> doesItContainValidPasswordPaths() {
+        return paths -> paths.stream().allMatch(ValidationFunctions::isCertificatePasswordPathValid);
+    }
+
+    public static Predicate<List<String>> doesItContainValidCertificatesPaths() {
+        return paths -> paths.stream().allMatch(ValidationFunctions::isCertificatePathValid);
+    }
+
+    public static Predicate<List<String>> doesItContainValidPathsToCopy() {
+        return paths -> paths.stream().allMatch(path ->
+            doesMatch(path, TRUSTSTORE_PASSWORD_PATH_REGEX) || isCertificatePathValid(path));
+    }
+
+    private static boolean isCertificatePathValid(String path) {
+        return doesMatch(path, CERTIFICATE_PATH_REGEX);
+    }
+
+    private static boolean isCertificatePasswordPathValid(String path) {
+        return path.isEmpty() || doesMatch(path, TRUSTSTORE_PASSWORD_PATH_REGEX);
+    }
+
+    private static boolean doesMatch(String path, String regex) {
+        return path.matches(regex);
+    }
+}
index c971ca1..b589323 100644 (file)
@@ -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
index 0897de2..f05cbc8 100644 (file)
@@ -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
index 854cde7..3df9bfd 100644 (file)
 
 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;
     }
+
 }
@@ -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);
     }
 
 }
@@ -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 (file)
index b048747..0000000
+++ /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 (file)
index 0000000..3c3760f
--- /dev/null
@@ -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();
+    }
+
+}