summaryrefslogtreecommitdiffstats
path: root/certServicePostProcessor/src/test
diff options
context:
space:
mode:
authorJan Malkiewicz <jan.malkiewicz@nokia.com>2020-09-18 09:53:12 +0200
committerJan Malkiewicz <jan.malkiewicz@nokia.com>2020-09-18 10:01:49 +0200
commitf8ce5f7332bd79b584ddd45ed8062cfdb5a938f3 (patch)
tree378475eac7d70cbc4d478af39c82d809a1972b3f /certServicePostProcessor/src/test
parent900b8224a0401e126e9033c61602aceed68c4080 (diff)
[OOM-CMPv2] Rename module folder truststoreMerger -> certServicePostProcessor
Issue-ID: DCAEGEN2-2253 Signed-off-by: Jan Malkiewicz <jan.malkiewicz@nokia.com> Change-Id: I975f37507f7f79c238f1eac15f375fe64761c80c
Diffstat (limited to 'certServicePostProcessor/src/test')
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/AppExecutorTest.java76
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/ExtensionResolverTest.java44
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/FileToolsTest.java71
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/PasswordReaderTest.java43
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/AppConfigurationProviderTest.java141
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/DelimitedPathsSplitterTest.java107
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/env/EnvReaderTest.java62
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/validation/ValidationFunctionsTest.java135
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/copier/KeystoreCopierTest.java147
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/TruststoreFilesProviderTest.java63
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/JavaTruststoreTest.java56
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/PemTruststoreTest.java128
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TestCertificateProvider.java152
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TruststoreFactoryTest.java109
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TruststoreTest.java206
-rw-r--r--certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/certificate/PemAliasGeneratorTest.java58
-rw-r--r--certServicePostProcessor/src/test/resources/empty-truststore.pem1
-rw-r--r--certServicePostProcessor/src/test/resources/keystore.p12bin0 -> 2873 bytes
-rw-r--r--certServicePostProcessor/src/test/resources/keystore.pass1
-rw-r--r--certServicePostProcessor/src/test/resources/truststore-jks-uniq.jksbin0 -> 1530 bytes
-rw-r--r--certServicePostProcessor/src/test/resources/truststore-jks.jksbin0 -> 1285 bytes
-rw-r--r--certServicePostProcessor/src/test/resources/truststore-jks.pass1
-rw-r--r--certServicePostProcessor/src/test/resources/truststore-p12.p12bin0 -> 1530 bytes
-rw-r--r--certServicePostProcessor/src/test/resources/truststore-p12.pass1
-rw-r--r--certServicePostProcessor/src/test/resources/truststore-with-private-key.pem56
-rw-r--r--certServicePostProcessor/src/test/resources/truststore.pem28
26 files changed, 1686 insertions, 0 deletions
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/AppExecutorTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/AppExecutorTest.java
new file mode 100644
index 00000000..19833cb8
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/AppExecutorTest.java
@@ -0,0 +1,76 @@
+/*============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.certservice.postprocessor;
+
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.verify;
+import static org.onap.oom.certservice.postprocessor.api.ExitStatus.ALIAS_CONFLICT_EXCEPTION;
+import static org.onap.oom.certservice.postprocessor.api.ExitStatus.SUCCESS;
+import static org.onap.oom.certservice.postprocessor.api.ExitStatus.UNEXPECTED_EXCEPTION;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.onap.oom.certservice.postprocessor.merger.exception.AliasConflictException;
+
+@ExtendWith(MockitoExtension.class)
+class AppExecutorTest {
+
+ @Mock
+ Runnable logic;
+ @Mock
+ AppExitHandler exitHandler;
+ @InjectMocks
+ AppExecutor executor = new AppExecutor();
+
+ @Test
+ void shouldExitWithUnexpectedException() {
+ doThrow(new NullPointerException()).when(logic).run();
+ doNothing().when(exitHandler).exit(UNEXPECTED_EXCEPTION);
+
+ executor.execute(logic);
+
+ verify(exitHandler).exit(UNEXPECTED_EXCEPTION);
+ }
+
+ @Test
+ void shouldExitWithKnownException() {
+ doThrow(new AliasConflictException("")).when(logic).run();
+ doNothing().when(exitHandler).exit(ALIAS_CONFLICT_EXCEPTION);
+
+ executor.execute(logic);
+
+ verify(exitHandler).exit(ALIAS_CONFLICT_EXCEPTION);
+ }
+
+ @Test
+ void shouldExitWithSuccess() {
+ doNothing().when(logic).run();
+ doNothing().when(exitHandler).exit(SUCCESS);
+
+ executor.execute(logic);
+
+ verify(exitHandler).exit(SUCCESS);
+ }
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/ExtensionResolverTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/ExtensionResolverTest.java
new file mode 100644
index 00000000..8450ab6d
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/ExtensionResolverTest.java
@@ -0,0 +1,44 @@
+/*============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.certservice.postprocessor.common;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+import java.io.File;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class ExtensionResolverTest {
+
+ @ParameterizedTest
+ @CsvSource(value = {
+ "opt/app/truststore.jks:.jks",
+ "opt/app/truststore.p12:.p12",
+ "opt/app/truststore.pem:.pem",
+ "opt/app/truststore.PEM:.pem",
+ "opt/app/truststore:''",
+ }, delimiter = ':')
+ void shouldReturnCorrectExtension(String filePath, String expectedExtension) {
+ String extension = ExtensionResolver.get(new File(filePath));
+ assertThat(extension).isEqualTo(expectedExtension);
+ }
+
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/FileToolsTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/FileToolsTest.java
new file mode 100644
index 00000000..7b3b28ae
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/FileToolsTest.java
@@ -0,0 +1,71 @@
+/*============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.certservice.postprocessor.common;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import org.apache.commons.io.FileUtils;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+class FileToolsTest {
+
+ public static final String BAK_EXTENSION = ".bak";
+
+ @TempDir
+ File dir;
+
+ @Test
+ void shouldCreateBackupProvidedFile() throws Exception {
+ //given
+ File fileToBackup = createFile("truststore.pem", "arbitrary content");
+ String backupFilePath = fileToBackup.getPath() + BAK_EXTENSION;
+ //when
+ new FileTools().createBackup(fileToBackup);
+ //then
+ assertThat(fileToBackup).hasSameBinaryContentAs(new File(backupFilePath));
+ }
+
+ @Test
+ void shouldCopyFile() throws IOException {
+ //given
+ File sourceFile = createFile("source.p12", "any content");
+ File destinationFile = new File(dir.getAbsolutePath() + "destination.p12");
+ //when
+ new FileTools().copy(sourceFile, destinationFile);
+ //then
+ assertThat(sourceFile).hasSameBinaryContentAs(destinationFile);
+ }
+
+
+ private File createFile(String name, String content) throws IOException {
+ File file = new File(dir.getAbsolutePath() + File.pathSeparator + name);
+ if (file.createNewFile()) {
+ FileUtils.write(file, content, Charset.defaultCharset());
+ } else {
+ throw new IllegalStateException("File could not be created: " + file.getAbsolutePath());
+ }
+ return file;
+ }
+
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/PasswordReaderTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/PasswordReaderTest.java
new file mode 100644
index 00000000..697eaa83
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/common/PasswordReaderTest.java
@@ -0,0 +1,43 @@
+/*============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.certservice.postprocessor.common;
+
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import org.onap.oom.certservice.postprocessor.merger.exception.PasswordReaderException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+
+class PasswordReaderTest {
+
+ @Test
+ void shouldReturnCorrectPasswordFromFile() throws PasswordReaderException {
+ String fileData = PasswordReader.readPassword(new File("src/test/resources/truststore-jks.pass"));
+ assertThat(fileData).isEqualTo("EOyuFbuYDyq_EhpboM72RHua");
+ }
+
+ @Test
+ void shouldThrowExceptionForNonExistingFile() {
+ assertThatExceptionOfType(PasswordReaderException.class)
+ .isThrownBy(() -> PasswordReader.readPassword(new File("src/test/resources/non-esisting-file.pass")));
+ }
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/AppConfigurationProviderTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/AppConfigurationProviderTest.java
new file mode 100644
index 00000000..24e2dab8
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/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.certservice.postprocessor.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.certservice.postprocessor.configuration.model.EnvVariable.KEYSTORE_DESTINATION_PATHS;
+import static org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable.KEYSTORE_SOURCE_PATHS;
+import static org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable.TRUSTSTORES_PASSWORDS_PATHS;
+import static org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable.TRUSTSTORES_PATHS;
+
+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.certservice.postprocessor.configuration.exception.CertificatesPathsValidationException;
+import org.onap.oom.certservice.postprocessor.configuration.exception.ConfigurationException;
+import org.onap.oom.certservice.postprocessor.configuration.model.AppConfiguration;
+import org.onap.oom.certservice.postprocessor.configuration.path.DelimitedPathsSplitter;
+import org.onap.oom.certservice.postprocessor.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 SAMPLE_TRUSTSTORES_PATHS = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.pem";
+ private static final String SAMPLE_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.name())).thenReturn(Optional.empty());
+ // when, then
+ assertThatExceptionOfType(ConfigurationException.class).isThrownBy(() -> provider.createConfiguration())
+ .withMessageContaining(TRUSTSTORES_PATHS + " 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, then
+ assertThatExceptionOfType(ConfigurationException.class)
+ .isThrownBy(() -> provider.createConfiguration())
+ .withMessageContaining("Size of " + TRUSTSTORES_PATHS
+ + " does not match size of " + TRUSTSTORES_PASSWORDS_PATHS + " 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.name())).thenReturn(Optional.of(SAMPLE_TRUSTSTORES_PATHS));
+ when(pathsSplitter.getValidatedPaths(TRUSTSTORES_PATHS, Optional.of(SAMPLE_TRUSTSTORES_PATHS)))
+ .thenReturn(truststores);
+ }
+
+ private void mockTruststoresPasswords(List<String> truststoresPasswords)
+ throws CertificatesPathsValidationException {
+ Optional<String> passwordsPaths = Optional.of(SAMPLE_TRUSTSTORES_PASSWORDS_PATHS);
+ when(envReader.getEnv(TRUSTSTORES_PASSWORDS_PATHS.name())).thenReturn(passwordsPaths);
+ when(pathsSplitter.getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS, passwordsPaths))
+ .thenReturn(truststoresPasswords);
+ }
+
+ private void mockKeystoreCopierSourcePaths(Optional<String> paths) {
+ when(envReader.getEnv(KEYSTORE_SOURCE_PATHS.name())).thenReturn(paths);
+ }
+
+ private void mockKeystoreCopierDestinationPaths(Optional<String> paths) {
+ when(envReader.getEnv(KEYSTORE_DESTINATION_PATHS.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/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/DelimitedPathsSplitterTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/DelimitedPathsSplitterTest.java
new file mode 100644
index 00000000..be1bc394
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/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.certservice.postprocessor.configuration.path;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable.TRUSTSTORES_PASSWORDS_PATHS;
+import static org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable.TRUSTSTORES_PATHS;
+
+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.certservice.postprocessor.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, 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, Optional.of("")));
+ }
+
+ @Test
+ void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() {
+ // when, then
+ assertThatExceptionOfType(CertificatesPathsValidationException.class)
+ .isThrownBy(() -> delimitedPathsSplitter
+ .getValidatedPaths(TRUSTSTORES_PATHS, Optional.of(INVALID_TRUSTSTORES)));
+ }
+
+ @Test
+ void shouldReturnCorrectListWhenTruststoresPasswordsValid() {
+ // when, then
+ assertThat(delimitedPathsSplitter
+ .getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS, Optional.of(VALID_TRUSTSTORES_PASSWORDS)))
+ .containsSequence("/opt/app/certificates/truststore.pass", "");
+ }
+
+ @Test
+ void shouldReturnCorrectListWhenTruststoresPasswordsContainsEmptyPathsInTheMiddle() {
+ // when, then
+ assertThat(delimitedPathsSplitter.getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS,
+ 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, Optional.of("")));
+ }
+
+ @Test
+ void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() {
+ // when, then
+ assertThatExceptionOfType(CertificatesPathsValidationException.class)
+ .isThrownBy(() -> delimitedPathsSplitter
+ .getValidatedPaths(TRUSTSTORES_PASSWORDS_PATHS, Optional.of(INVALID_TRUSTSTORES_PASSWORDS)));
+ }
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/env/EnvReaderTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/env/EnvReaderTest.java
new file mode 100644
index 00000000..343e5655
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/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.certservice.postprocessor.configuration.path.env;
+
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.when;
+import static org.onap.oom.certservice.postprocessor.configuration.model.EnvVariable.TRUSTSTORES_PASSWORDS_PATHS;
+
+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.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.name();
+ // when
+ Optional<String> result = provider.getEnv(envName);
+ // then
+ assertThat(result).isEmpty();
+ }
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/validation/ValidationFunctionsTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/validation/ValidationFunctionsTest.java
new file mode 100644
index 00000000..f65147a9
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/configuration/path/validation/ValidationFunctionsTest.java
@@ -0,0 +1,135 @@
+/*============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.certservice.postprocessor.configuration.path.validation;
+
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.onap.oom.certservice.postprocessor.configuration.path.validation.ValidationFunctions.doesItContainValidCertificatesPaths;
+import static org.onap.oom.certservice.postprocessor.configuration.path.validation.ValidationFunctions.doesItContainValidPasswordPaths;
+import static org.onap.oom.certservice.postprocessor.configuration.path.validation.ValidationFunctions.doesItContainValidPathsToCopy;
+
+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 = 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 = 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 = doesItContainValidPathsToCopy().test(sourcePaths);
+ // then
+ assertThat(result).isFalse();
+ }
+
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/copier/KeystoreCopierTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/copier/KeystoreCopierTest.java
new file mode 100644
index 00000000..99193a8d
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/copier/KeystoreCopierTest.java
@@ -0,0 +1,147 @@
+/*============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.certservice.postprocessor.copier;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.Collections;
+import org.apache.commons.io.FileUtils;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.onap.oom.certservice.postprocessor.common.FileTools;
+import org.onap.oom.certservice.postprocessor.configuration.model.AppConfiguration;
+import org.onap.oom.certservice.postprocessor.copier.exception.KeystoreFileCopyException;
+import org.onap.oom.certservice.postprocessor.copier.exception.KeystoreNotExistException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+
+public class KeystoreCopierTest {
+
+ private static final String SOURCE_CONTENT = "source content";
+ private static final String DESTINATION_CONTENT = "destination content";
+
+ @TempDir
+ File dir;
+
+ private KeystoreCopier copier = new KeystoreCopier(new FileTools());
+
+ @Test
+ void shouldDoNothingForEmptySourceFileList() {
+ AppConfiguration configuration = createEmptyConfiguration();
+
+ copier.copyKeystores(configuration);
+
+ assertThat(dir.listFiles()).isEmpty();
+ }
+
+
+ @Test
+ void shouldCopyFileAndCreateBackup() throws IOException {
+ File source = createFile("source.p12", SOURCE_CONTENT);
+ File destination = createFile("destination.p12", DESTINATION_CONTENT);
+ File backup = declareFile("destination.p12.bak");
+ AppConfiguration configuration = createConfiguration(source, destination);
+
+ copier.copyKeystores(configuration);
+
+ assertThat(readFile(destination)).isEqualTo(readFile(source));
+ assertThat(backup.exists()).isTrue();
+ assertThat(readFile(backup)).isEqualTo(DESTINATION_CONTENT);
+ }
+
+ @Test
+ void shouldCopyFileWithoutCreatingBackup() throws IOException {
+ File source = createFile("source.p12", SOURCE_CONTENT);
+ File destination = declareFile("destination.p12");
+ File backup = declareFile("destination.p12.bak");
+ AppConfiguration configuration = createConfiguration(source, destination);
+
+ copier.copyKeystores(configuration);
+
+ assertThat(destination.exists()).isTrue();
+ assertThat(readFile(destination)).isEqualTo(readFile(source));
+ assertThat(backup.exists()).isFalse();
+ }
+
+ @Test
+ void shouldThrowKeystoreNotExistException() throws IOException {
+ File source = declareFile("source.p12");
+ File destination = declareFile("destination.p12");
+ File backup = declareFile("destination.p12.bak");
+ AppConfiguration configuration = createConfiguration(source, destination);
+
+ assertThatExceptionOfType(KeystoreNotExistException.class).isThrownBy( () ->
+ copier.copyKeystores(configuration)
+ );
+
+ assertThat(source.exists()).isFalse();
+ assertThat(destination.exists()).isFalse();
+ assertThat(backup.exists()).isFalse();
+ }
+
+ @Test
+ void shouldThrowKeystoreFileCopyException() throws IOException {
+ File source = createFile("source.p12", SOURCE_CONTENT);
+ source.setReadable(false);
+ File destination = declareFile("destination.p12");
+ File backup = declareFile("destination.p12.bak");
+ AppConfiguration configuration = createConfiguration(source, destination);
+
+ assertThatExceptionOfType(KeystoreFileCopyException.class).isThrownBy( () ->
+ copier.copyKeystores(configuration)
+ );
+
+ assertThat(source.exists()).isTrue();
+ assertThat(destination.exists()).isFalse();
+ assertThat(backup.exists()).isFalse();
+ }
+
+ private AppConfiguration createConfiguration(File source, File destination) {
+ return new AppConfiguration(Collections.emptyList(), Collections.emptyList(),
+ Collections.singletonList(source.getAbsolutePath()),
+ Collections.singletonList(destination.getAbsolutePath()));
+ }
+
+ private AppConfiguration createEmptyConfiguration() {
+ return new AppConfiguration(Collections.emptyList(), Collections.emptyList(),
+ Collections.emptyList(),
+ Collections.emptyList());
+ }
+
+ private String readFile(File file) throws IOException {
+ return FileUtils.readFileToString(file, Charset.defaultCharset());
+ }
+
+ private File declareFile(String name) {
+ return new File(dir.getAbsolutePath() + File.pathSeparator + name);
+ }
+
+ private File createFile(String name, String content) throws IOException {
+ File file = new File(dir.getAbsolutePath() + File.pathSeparator + name);
+ if (file.createNewFile()) {
+ FileUtils.write(file, content, Charset.defaultCharset());
+ } else {
+ throw new IllegalStateException("File could not be created: " + file.getAbsolutePath());
+ }
+ return file;
+ }
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/TruststoreFilesProviderTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/TruststoreFilesProviderTest.java
new file mode 100644
index 00000000..9040b53d
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/TruststoreFilesProviderTest.java
@@ -0,0 +1,63 @@
+/*============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.certservice.postprocessor.merger;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Arrays;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+import org.onap.oom.certservice.postprocessor.merger.exception.KeystoreInstanceException;
+import org.onap.oom.certservice.postprocessor.merger.exception.LoadTruststoreException;
+import org.onap.oom.certservice.postprocessor.merger.exception.PasswordReaderException;
+import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreFileFactoryException;
+import org.onap.oom.certservice.postprocessor.merger.model.Truststore;
+import org.onap.oom.certservice.postprocessor.merger.model.TruststoreFactory;
+
+class TruststoreFilesProviderTest {
+
+ private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks";
+ private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass";
+ private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12";
+ private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass";
+ private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem";
+ private static final String EMPTY_PASS_PATH = "";
+
+ @Test
+ void shouldReturnTruststoreFilesList()
+ throws TruststoreFileFactoryException, PasswordReaderException, LoadTruststoreException, KeystoreInstanceException {
+ //given
+ List<String> truststorePaths = Arrays.asList(TRUSTSTORE_JKS_PATH, TRUSTSTORE_P12_PATH, TRUSTSTORE_PEM_PATH);
+ List<String> truststorePasswordPaths = Arrays
+ .asList(TRUSTSTORE_JKS_PASS_PATH, TRUSTSTORE_P12_PASS_PATH, EMPTY_PASS_PATH);
+
+ //when
+ List<Truststore> truststoreFilesList = TruststoreFilesProvider
+ .getTruststoreFiles(truststorePaths, truststorePasswordPaths);
+
+ //then
+ assertThat(truststoreFilesList.size()).isEqualTo(3);
+ TruststoreFactory.create(TRUSTSTORE_JKS_PATH, TRUSTSTORE_JKS_PASS_PATH);
+ TruststoreFactory.create(TRUSTSTORE_P12_PATH, TRUSTSTORE_P12_PASS_PATH);
+ TruststoreFactory.create(TRUSTSTORE_PEM_PATH, EMPTY_PASS_PATH);
+ }
+
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/JavaTruststoreTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/JavaTruststoreTest.java
new file mode 100644
index 00000000..93a5359b
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/JavaTruststoreTest.java
@@ -0,0 +1,56 @@
+/*============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.certservice.postprocessor.merger.model;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+
+import java.util.List;
+import org.junit.jupiter.api.Test;
+import org.onap.oom.certservice.postprocessor.api.ExitableException;
+import org.onap.oom.certservice.postprocessor.merger.exception.AliasConflictException;
+import org.onap.oom.certservice.postprocessor.merger.exception.MissingTruststoreException;
+import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAlias;
+
+class JavaTruststoreTest {
+
+ @Test
+ void throwExceptionWhenAliasConflictDetected() throws Exception {
+ //given
+ Truststore p12Truststore = TestCertificateProvider.getSampleP12Truststore();
+
+ List<CertificateWithAlias> certificateFromJks = TestCertificateProvider
+ .getSampleJksTruststoreFile().getCertificates();
+
+ //when //then
+ assertThatExceptionOfType(AliasConflictException.class)
+ .isThrownBy(() -> p12Truststore.addCertificates(certificateFromJks));
+ }
+
+ @Test
+ void throwExceptionWhenFileNotContainsTruststoreEntry() throws ExitableException {
+ //given
+ Truststore p12Truststore = TestCertificateProvider.getSampleP12Keystore();
+
+ //when//then
+ assertThatExceptionOfType(MissingTruststoreException.class)
+ .isThrownBy(() -> p12Truststore.getCertificates());
+ }
+
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/PemTruststoreTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/PemTruststoreTest.java
new file mode 100644
index 00000000..af254896
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/PemTruststoreTest.java
@@ -0,0 +1,128 @@
+/*============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.certservice.postprocessor.merger.model;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.mockito.Mockito.mock;
+
+import java.io.File;
+import java.io.IOException;
+import java.security.KeyStore;
+import java.security.KeyStoreSpi;
+import java.security.cert.Certificate;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.junit.jupiter.api.Test;
+import org.onap.oom.certservice.postprocessor.api.ExitableException;
+import org.onap.oom.certservice.postprocessor.merger.exception.MissingTruststoreException;
+import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreDataOperationException;
+import org.onap.oom.certservice.postprocessor.merger.exception.WriteTruststoreFileException;
+import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAlias;
+
+class PemTruststoreTest {
+
+ private static final int EXPECTED_ONE = 1;
+
+ @Test
+ void getCertificatesShouldThrowExceptionWhenFileNotContainsCertificate() {
+ //given
+ File emptyPemFile = TestCertificateProvider.getEmptyPemFile();
+ PemTruststore pemCertificate = new PemTruststore(emptyPemFile);
+ //when//then
+ assertThatExceptionOfType(MissingTruststoreException.class)
+ .isThrownBy(pemCertificate::getCertificates);
+ }
+
+ @Test
+ void shouldThrowExceptionWhenCannotSaveFile() {
+ //given
+ KeyStoreSpi keyStoreSpi = mock(KeyStoreSpi.class);
+ KeyStore keyStore = new KeyStore(keyStoreSpi, null, "") {
+ };
+ File pemFile = TestCertificateProvider.getEmptyPemFile();
+ pemFile.setWritable(false);
+ PemTruststore pem = new PemTruststore(pemFile);
+
+ //when. then
+ assertThatExceptionOfType(WriteTruststoreFileException.class)
+ .isThrownBy(pem::saveFile);
+ }
+
+ @Test
+ void transformToStringInPemFormatShouldCorrectlyTransform() throws ExitableException, IOException {
+ //given
+ Truststore pemTruststore = TestCertificateProvider.getSamplePemTruststoreFile();
+
+ List<CertificateWithAlias> wrappedCertificates = pemTruststore.getCertificates();
+ List<Certificate> certificateList = unWrapCertificate(wrappedCertificates);
+ File notEmptyPemFile = TestCertificateProvider.getNotEmptyPemFile();
+ PemTruststore pemCertificate = new PemTruststore(notEmptyPemFile);
+
+ //when
+ String certificateTransformed = pemCertificate.transformToStringInPemFormat(certificateList);
+
+ //then
+ String expected = TestCertificateProvider.getExpectedPemCertificateAsString();
+ assertThat(certificateTransformed).isEqualTo(expected);
+ }
+
+ @Test
+ void fileNotContainsPemCertificateShouldReturnTrueIfFileNotContainsCertificate()
+ throws TruststoreDataOperationException {
+ //given
+ File emptyPemFile = TestCertificateProvider.getEmptyPemFile();
+ PemTruststore pemCertificate = new PemTruststore(emptyPemFile);
+ //when//then
+ assertThat(pemCertificate.isFileWithoutPemCertificate()).isTrue();
+ }
+
+ @Test
+ void fileNotContainsPemCertificateShouldReturnFalseIfFileContainsCertificate()
+ throws TruststoreDataOperationException {
+ //given
+ File notEmptyPemFile = TestCertificateProvider.getNotEmptyPemFile();
+ PemTruststore pemCertificate = new PemTruststore(notEmptyPemFile);
+
+ //when//then
+ assertThat(pemCertificate.isFileWithoutPemCertificate()).isFalse();
+ }
+
+ @Test
+ void privateKeyIsSkippedWhileReadingCertificates() throws ExitableException {
+ //given
+ File pemTruststoreFile = TestCertificateProvider.getPemWithPrivateKeyFile();
+ PemTruststore pemCertificate = new PemTruststore(pemTruststoreFile);
+
+ //when
+ List<CertificateWithAlias> certificate = pemCertificate.getCertificates();
+
+ //then
+ assertThat(certificate).hasSize(EXPECTED_ONE);
+ }
+
+ private List<Certificate> unWrapCertificate(List<CertificateWithAlias> certificateWithAliases) {
+ return certificateWithAliases
+ .stream()
+ .map(CertificateWithAlias::getCertificate)
+ .collect(Collectors.toList());
+ }
+
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TestCertificateProvider.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TestCertificateProvider.java
new file mode 100644
index 00000000..f32e09ce
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TestCertificateProvider.java
@@ -0,0 +1,152 @@
+/*============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.certservice.postprocessor.merger.model;
+
+import static org.onap.oom.certservice.postprocessor.api.CertificateConstants.JKS_TYPE;
+import static org.onap.oom.certservice.postprocessor.api.CertificateConstants.PKCS12_TYPE;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import org.onap.oom.certservice.postprocessor.merger.exception.KeystoreInstanceException;
+import org.onap.oom.certservice.postprocessor.merger.exception.LoadTruststoreException;
+import org.onap.oom.certservice.postprocessor.merger.exception.PasswordReaderException;
+
+public final class TestCertificateProvider {
+
+ public static final String SAMPLE_P12_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore-p12.p12";
+ public static final String SAMPLE_P12_TRUSTSTORE_PASSWORD_PATH = "src/test/resources/truststore-p12.pass";
+ public static final String TMP_P12_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore-p12.p12";
+
+ public static final String SAMPLE_P12_KEYSTORE_FILE_PATH = "src/test/resources/keystore.p12";
+ public static final String SAMPLE_P12_KEYSTORE_PASSWORD_PATH = "src/test/resources/keystore.pass";
+
+ public static final String SAMPLE_JKS_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore-jks.jks";
+ public static final String SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH = "src/test/resources/truststore-jks-uniq.jks";
+ public static final String SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH = "src/test/resources/truststore-jks.pass";
+ public static final String TMP_JKS_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore-jks.jks";
+
+ public static final String SAMPLE_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore.pem";
+ public static final String EMPTY_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/empty-truststore.pem";
+ public static final String TMP_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore.pem";
+ public static final String SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH = "src/test/resources/truststore-with-private-key.pem";
+
+ public static final String PEM_FILE_PATH = "src/test/resources/truststore.pem";
+ public static final String PEM_BACKUP_FILE_PATH = "src/test/resources/truststore.pem.bak";
+
+ private TestCertificateProvider() {
+ }
+
+ public static Truststore getSampleP12Truststore()
+ throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
+ return createJavaTruststore(SAMPLE_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD_PATH, PKCS12_TYPE);
+ }
+
+ public static Truststore getSampleP12Keystore()
+ throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
+ return createJavaTruststore(SAMPLE_P12_KEYSTORE_FILE_PATH, SAMPLE_P12_KEYSTORE_PASSWORD_PATH, PKCS12_TYPE);
+ }
+
+ public static Truststore createTmpP12TruststoreFile()
+ throws IOException, LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
+ copyFile(SAMPLE_P12_TRUSTSTORE_FILE_PATH, TMP_P12_TRUSTSTORE_FILE_PATH);
+ return createJavaTruststore(TMP_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD_PATH, PKCS12_TYPE);
+ }
+
+ public static Truststore getSamplePemTruststoreFile() {
+ return getPemTruststoreInstance(SAMPLE_PEM_TRUSTSTORE_FILE_PATH);
+ }
+
+ public static Truststore createEmptyTmpPemTruststoreFile()
+ throws IOException {
+ copyFile(EMPTY_PEM_TRUSTSTORE_FILE_PATH, TMP_PEM_TRUSTSTORE_FILE_PATH);
+ return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH);
+ }
+
+ public static Truststore createTmpPemTruststoreFile()
+ throws IOException {
+ copyFile(SAMPLE_PEM_TRUSTSTORE_FILE_PATH, TMP_PEM_TRUSTSTORE_FILE_PATH);
+ return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH);
+ }
+
+ public static String getExpectedPemCertificateAsString() throws IOException {
+ Path samplePemFilePath = Paths.get(SAMPLE_PEM_TRUSTSTORE_FILE_PATH);
+ return Files.readString(samplePemFilePath);
+ }
+
+ public static Truststore getSampleJksTruststoreFile()
+ throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
+ return createJavaTruststore(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH, JKS_TYPE);
+ }
+
+ public static Truststore getSampleJksTruststoreFileWithUniqueAlias()
+ throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
+ return createJavaTruststore(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH,
+ JKS_TYPE);
+ }
+
+ public static Truststore createTmpJksTruststoreFileWithUniqAlias()
+ throws IOException, LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
+ copyFile(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, TMP_JKS_TRUSTSTORE_FILE_PATH);
+ return createJavaTruststore(TMP_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH, JKS_TYPE);
+ }
+
+ public static File getEmptyPemFile() {
+ return getFile(EMPTY_PEM_TRUSTSTORE_FILE_PATH);
+ }
+
+ public static File getNotEmptyPemFile() {
+ return getFile(SAMPLE_PEM_TRUSTSTORE_FILE_PATH);
+ }
+
+ public static File getPemWithPrivateKeyFile() {
+ return getFile(SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH);
+ }
+
+ public static void removeTemporaryFiles() throws IOException {
+ Files.deleteIfExists(Paths.get(TMP_PEM_TRUSTSTORE_FILE_PATH));
+ Files.deleteIfExists(Paths.get(TMP_JKS_TRUSTSTORE_FILE_PATH));
+ Files.deleteIfExists(Paths.get(TMP_P12_TRUSTSTORE_FILE_PATH));
+ Files.deleteIfExists(Paths.get(PEM_BACKUP_FILE_PATH));
+ }
+
+ private static Truststore createJavaTruststore(String filePath, String password, String instanceType)
+ throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
+ File certFile = getFile(filePath);
+ return JavaTruststoreFactory.create(certFile, password, instanceType);
+ }
+
+ private static Truststore getPemTruststoreInstance(
+ String tmpPemTruststoreFilePath) {
+ File file = getFile(tmpPemTruststoreFilePath);
+ return new PemTruststore(file);
+ }
+
+ private static void copyFile(String sourcePath, String destPath) throws IOException {
+ Files.copy(Paths.get(sourcePath), Paths.get(destPath), StandardCopyOption.REPLACE_EXISTING);
+ }
+
+ private static File getFile(String path) {
+ return new File(path);
+ }
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TruststoreFactoryTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TruststoreFactoryTest.java
new file mode 100644
index 00000000..42bf533a
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TruststoreFactoryTest.java
@@ -0,0 +1,109 @@
+/*============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.certservice.postprocessor.merger.model;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.onap.oom.certservice.postprocessor.merger.exception.KeystoreInstanceException;
+import org.onap.oom.certservice.postprocessor.merger.exception.LoadTruststoreException;
+import org.onap.oom.certservice.postprocessor.merger.exception.PasswordReaderException;
+import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreFileFactoryException;
+
+@ExtendWith(MockitoExtension.class)
+class TruststoreFactoryTest {
+
+ private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks";
+ private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass";
+ private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12";
+ private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass";
+ private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem";
+ private static final String EMPTY_PASS_PATH = "";
+ private static final String TRUSTSTORE_UNKNOWN_EXTENSION_PATH = "src/test/resources/truststore-jks.unknown";
+ private static final String NON_EXISTING_TRUSTSTORE_PATH = "src/test/resources/non-existing-truststore.jks";
+
+ @Test
+ void shouldReturnCorrectJksTruststoreForJksFile()
+ throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException {
+ //given, when
+ Truststore truststore = TruststoreFactory
+ .create(TRUSTSTORE_JKS_PATH, TRUSTSTORE_JKS_PASS_PATH);
+
+ //then
+ assertThat(truststore).isInstanceOf(Truststore.class);
+ }
+
+ @Test
+ void shouldReturnCorrectP12TruststoreForP12File()
+ throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException {
+ //given, when
+ Truststore truststore = TruststoreFactory
+ .create(TRUSTSTORE_P12_PATH, TRUSTSTORE_P12_PASS_PATH);
+
+ //then
+ assertThat(truststore).isInstanceOf(Truststore.class);
+ }
+
+ @Test
+ void shouldReturnCorrectPemTruststoreForPemFile()
+ throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException {
+ //given, when
+ Truststore truststore = TruststoreFactory
+ .create(TRUSTSTORE_PEM_PATH,
+ EMPTY_PASS_PATH);
+
+ //then
+ assertThat(truststore).isInstanceOf(Truststore.class);
+ }
+
+ @Test
+ void shouldThrowExceptionForInvalidP12PassPath() {
+ assertThatExceptionOfType(PasswordReaderException.class).isThrownBy(
+ () -> TruststoreFactory.create(TRUSTSTORE_P12_PATH, EMPTY_PASS_PATH)
+ );
+ }
+
+ @Test
+ void shouldThrowExceptionForInvalidJksPassPath() {
+ assertThatExceptionOfType(PasswordReaderException.class).isThrownBy(
+ () -> TruststoreFactory.create(TRUSTSTORE_JKS_PATH, EMPTY_PASS_PATH)
+ );
+ }
+
+ @Test
+ void shouldThrowExceptionForUnknownTruststoreExtension() {
+ assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy(
+ () -> TruststoreFactory
+ .create(TRUSTSTORE_UNKNOWN_EXTENSION_PATH, TRUSTSTORE_JKS_PASS_PATH)
+ );
+ }
+
+ @Test
+ void shouldThrowExceptionForNonExistingTruststoreFile() {
+ assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy(
+ () -> TruststoreFactory.create(NON_EXISTING_TRUSTSTORE_PATH, TRUSTSTORE_JKS_PASS_PATH)
+ );
+ }
+
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TruststoreTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TruststoreTest.java
new file mode 100644
index 00000000..8ef148a8
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/TruststoreTest.java
@@ -0,0 +1,206 @@
+/*============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.certservice.postprocessor.merger.model;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.onap.oom.certservice.postprocessor.api.CertificateConstants.X_509_CERTIFICATE;
+import static org.onap.oom.certservice.postprocessor.merger.model.TestCertificateProvider.PEM_BACKUP_FILE_PATH;
+
+import java.io.File;
+import java.io.IOException;
+import java.security.cert.Certificate;
+import java.security.cert.CertificateEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Stream;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.onap.oom.certservice.postprocessor.api.CertificateConstants;
+import org.onap.oom.certservice.postprocessor.api.ExitableException;
+import org.onap.oom.certservice.postprocessor.merger.exception.CreateBackupException;
+import org.onap.oom.certservice.postprocessor.merger.exception.KeystoreInstanceException;
+import org.onap.oom.certservice.postprocessor.merger.exception.LoadTruststoreException;
+import org.onap.oom.certservice.postprocessor.merger.exception.MissingTruststoreException;
+import org.onap.oom.certservice.postprocessor.merger.exception.PasswordReaderException;
+import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreDataOperationException;
+import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAlias;
+import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAliasFactory;
+
+class TruststoreTest {
+
+ private static final String BACKUP_EXTENSION = ".bak";
+
+ private static final int EXPECTED_ONE = 1;
+ public static final int EXPECTED_THREE = 3;
+ public static final int FIRST_ELEMENT = 0;
+
+ private final CertificateWithAliasFactory factory = new CertificateWithAliasFactory();
+
+ @Test
+ void createBackupShouldCreateFileWithExtension() throws CreateBackupException {
+ //given
+ File pemFile = new File(TestCertificateProvider.PEM_FILE_PATH);
+ Truststore truststore = new PemTruststore(pemFile);
+ //when
+ truststore.createBackup();
+
+ //then
+ File backupFile = new File(PEM_BACKUP_FILE_PATH);
+ assertThat(backupFile.getName().endsWith(BACKUP_EXTENSION)).isTrue();
+ assertThat(backupFile.isFile()).isTrue();
+ }
+
+ @ParameterizedTest
+ @MethodSource("truststoreProvider")
+ void truststoreShouldReadCertificatesFromFile(Truststore truststore) throws ExitableException {
+ //when
+
+ List<CertificateWithAlias> certificates = truststore.getCertificates();
+ Certificate certificate = certificates.get(FIRST_ELEMENT).getCertificate();
+
+ //then
+ assertThat(certificates).hasSize(EXPECTED_ONE);
+ assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
+ }
+
+ @Test
+ void jksTruststoreShouldAddDifferentCertificates() throws Exception {
+ //given
+ Truststore jksTruststore = TestCertificateProvider.createTmpJksTruststoreFileWithUniqAlias();
+
+ List<CertificateWithAlias> certificateFromP12 = TestCertificateProvider.getSampleP12Truststore()
+ .getCertificates();
+
+ List<CertificateWithAlias> certificateFromPem = TestCertificateProvider
+ .getSamplePemTruststoreFile().getCertificates();
+
+ //when
+
+ jksTruststore.addCertificates(certificateFromP12);
+
+ jksTruststore.addCertificates(certificateFromPem);
+
+ jksTruststore.saveFile();
+
+ //then
+
+ assertThat(jksTruststore.getCertificates()).hasSize(EXPECTED_THREE);
+ }
+
+ @Test
+ void p12TruststoreShouldAddDifferentCertificates() throws Exception {
+ //given
+ Truststore p12Truststore = TestCertificateProvider.createTmpP12TruststoreFile();
+
+ List<CertificateWithAlias> certificateFromJks = TestCertificateProvider
+ .getSampleJksTruststoreFileWithUniqueAlias().getCertificates();
+
+ List<CertificateWithAlias> certificateFromPem = TestCertificateProvider
+ .getSamplePemTruststoreFile().getCertificates();
+
+ //when
+
+ p12Truststore.addCertificates(certificateFromJks);
+ p12Truststore.addCertificates(certificateFromPem);
+ p12Truststore.saveFile();
+
+ //then
+
+ assertThat(p12Truststore.getCertificates()).hasSize(EXPECTED_THREE);
+ }
+
+ @Test
+ void pemTruststoreShouldAddDifferentCertificates() throws IOException, ExitableException {
+ //given
+ Truststore pemTruststore = TestCertificateProvider
+ .createTmpPemTruststoreFile();
+
+ List<CertificateWithAlias> certificateFromJks = TestCertificateProvider
+ .getSampleJksTruststoreFileWithUniqueAlias().getCertificates();
+
+ List<CertificateWithAlias> certificateFromP12 = TestCertificateProvider.getSampleP12Truststore()
+ .getCertificates();
+
+ //when
+
+ pemTruststore.addCertificates(certificateFromJks);
+
+ pemTruststore.addCertificates(certificateFromP12);
+
+ pemTruststore.saveFile();
+
+ //then
+
+ List<CertificateWithAlias> addedCertificates = pemTruststore.getCertificates();
+ Certificate certificate = addedCertificates.get(FIRST_ELEMENT).getCertificate();
+
+ assertThat(pemTruststore.getCertificates()).hasSize(EXPECTED_THREE);
+ assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
+ }
+
+ @Test
+ void shouldThrowExceptionWhenFileNotContainsCertificate() throws IOException {
+ //given
+ Truststore tmpPemTruststoreFile = TestCertificateProvider
+ .createEmptyTmpPemTruststoreFile();
+ //when//then
+ assertThatExceptionOfType(MissingTruststoreException.class)
+ .isThrownBy(() -> tmpPemTruststoreFile.getCertificates());
+ }
+
+ @Test
+ void shouldThrowExceptionWhenCannotConvertCertificateToPem() throws Exception {
+ //given
+ Truststore pemTruststore = TestCertificateProvider.createTmpPemTruststoreFile();
+ Certificate certificate = mock(Certificate.class);
+
+ when(certificate.getEncoded()).thenThrow(new CertificateEncodingException());
+
+ List<CertificateWithAlias> certificateFromPem = new ArrayList<>();
+ certificateFromPem.add(factory.createPemCertificate(certificate));
+
+ pemTruststore.addCertificates(certificateFromPem);
+
+ //when //then
+ assertThatExceptionOfType(TruststoreDataOperationException.class)
+ .isThrownBy(() -> pemTruststore.saveFile());
+ }
+
+ @AfterEach
+ void removeTemporaryFiles() throws IOException {
+ TestCertificateProvider.removeTemporaryFiles();
+ }
+
+ private static Stream<Arguments> truststoreProvider()
+ throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException {
+ return Stream.of(
+ Arguments.of(TestCertificateProvider.getSampleJksTruststoreFile()),
+ Arguments.of(TestCertificateProvider.getSampleP12Truststore()),
+ Arguments.of(TestCertificateProvider.getSamplePemTruststoreFile())
+ );
+ }
+
+}
diff --git a/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/certificate/PemAliasGeneratorTest.java b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/certificate/PemAliasGeneratorTest.java
new file mode 100644
index 00000000..03989f6f
--- /dev/null
+++ b/certServicePostProcessor/src/test/java/org/onap/oom/certservice/postprocessor/merger/model/certificate/PemAliasGeneratorTest.java
@@ -0,0 +1,58 @@
+/*============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.certservice.postprocessor.merger.model.certificate;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.HashSet;
+import java.util.Set;
+import org.junit.jupiter.api.Test;
+
+class PemAliasGeneratorTest {
+
+ private static final String PREFIX_ALIAS_NAME = "pem-trusted-certificate-";
+ static final int GENERATED_ALIASES_NUMBER = 100;
+
+ @Test
+ void aliasHasPemPrefix() {
+ //given
+ PemAliasGenerator pemAliasGenerator = PemAliasGenerator.getInstance();
+ //when
+ String alias = pemAliasGenerator.getAlias();
+ //then
+ assertThat(alias).contains(PREFIX_ALIAS_NAME);
+ }
+
+ @Test
+ void generatedAliasesHaveUniqNames() {
+ //given
+ PemAliasGenerator pemAliasGenerator = PemAliasGenerator.getInstance();
+ Set<String> aliases = new HashSet<>();
+
+ //when
+ for (int i = 0; i < GENERATED_ALIASES_NUMBER; i++) {
+ aliases.add(pemAliasGenerator.getAlias());
+ }
+
+ //then
+ assertThat(aliases).hasSize(GENERATED_ALIASES_NUMBER);
+ }
+
+}
diff --git a/certServicePostProcessor/src/test/resources/empty-truststore.pem b/certServicePostProcessor/src/test/resources/empty-truststore.pem
new file mode 100644
index 00000000..8b137891
--- /dev/null
+++ b/certServicePostProcessor/src/test/resources/empty-truststore.pem
@@ -0,0 +1 @@
+
diff --git a/certServicePostProcessor/src/test/resources/keystore.p12 b/certServicePostProcessor/src/test/resources/keystore.p12
new file mode 100644
index 00000000..bc047a98
--- /dev/null
+++ b/certServicePostProcessor/src/test/resources/keystore.p12
Binary files differ
diff --git a/certServicePostProcessor/src/test/resources/keystore.pass b/certServicePostProcessor/src/test/resources/keystore.pass
new file mode 100644
index 00000000..665ff8e6
--- /dev/null
+++ b/certServicePostProcessor/src/test/resources/keystore.pass
@@ -0,0 +1 @@
+Foh49MJNYI7S_pEzE9gvUDSu \ No newline at end of file
diff --git a/certServicePostProcessor/src/test/resources/truststore-jks-uniq.jks b/certServicePostProcessor/src/test/resources/truststore-jks-uniq.jks
new file mode 100644
index 00000000..76ce8bc4
--- /dev/null
+++ b/certServicePostProcessor/src/test/resources/truststore-jks-uniq.jks
Binary files differ
diff --git a/certServicePostProcessor/src/test/resources/truststore-jks.jks b/certServicePostProcessor/src/test/resources/truststore-jks.jks
new file mode 100644
index 00000000..38229811
--- /dev/null
+++ b/certServicePostProcessor/src/test/resources/truststore-jks.jks
Binary files differ
diff --git a/certServicePostProcessor/src/test/resources/truststore-jks.pass b/certServicePostProcessor/src/test/resources/truststore-jks.pass
new file mode 100644
index 00000000..7426fd4d
--- /dev/null
+++ b/certServicePostProcessor/src/test/resources/truststore-jks.pass
@@ -0,0 +1 @@
+EOyuFbuYDyq_EhpboM72RHua \ No newline at end of file
diff --git a/certServicePostProcessor/src/test/resources/truststore-p12.p12 b/certServicePostProcessor/src/test/resources/truststore-p12.p12
new file mode 100644
index 00000000..0fa8aecc
--- /dev/null
+++ b/certServicePostProcessor/src/test/resources/truststore-p12.p12
Binary files differ
diff --git a/certServicePostProcessor/src/test/resources/truststore-p12.pass b/certServicePostProcessor/src/test/resources/truststore-p12.pass
new file mode 100644
index 00000000..86cc5aac
--- /dev/null
+++ b/certServicePostProcessor/src/test/resources/truststore-p12.pass
@@ -0,0 +1 @@
+88y9v5D8H3SG6bZWRVHDfOAo \ No newline at end of file
diff --git a/certServicePostProcessor/src/test/resources/truststore-with-private-key.pem b/certServicePostProcessor/src/test/resources/truststore-with-private-key.pem
new file mode 100644
index 00000000..95179062
--- /dev/null
+++ b/certServicePostProcessor/src/test/resources/truststore-with-private-key.pem
@@ -0,0 +1,56 @@
+-----BEGIN CERTIFICATE-----
+MIIEszCCAxugAwIBAgIUE+27eIlr12tQ+AMxkJTf2Y+ycOEwDQYJKoZIhvcNAQEL
+BQAwYTEjMCEGCgmSJomT8ixkAQEME2MtMDRjYmE2YjhhMDQ5ODEyNGQxFTATBgNV
+BAMMDE1hbmFnZW1lbnRDQTEjMCEGA1UECgwaRUpCQ0EgQ29udGFpbmVyIFF1aWNr
+c3RhcnQwHhcNMjAwNzA4MTIzODU4WhcNMzAwNzA4MTIzODU4WjBhMSMwIQYKCZIm
+iZPyLGQBAQwTYy0wNGNiYTZiOGEwNDk4MTI0ZDEVMBMGA1UEAwwMTWFuYWdlbWVu
+dENBMSMwIQYDVQQKDBpFSkJDQSBDb250YWluZXIgUXVpY2tzdGFydDCCAaIwDQYJ
+KoZIhvcNAQEBBQADggGPADCCAYoCggGBALTlx22Ld87VO5QgkD7OJvx81a8xLRWt
+b4cqmLSBRKw+jTjX4fHCtLh98hXNtYXJ9nxPa2t8MKR/I00Wf1razX1IYN9H/diV
+uICjyMxDyK6nwEMpqaWiQgOQx1N4TjNhr19ULTbyFLQMVfXy1OrTsfoWQ2omvRxN
+LIoVKwPHd92KG6iqJDZU14ErfA6UtypDV+4rOKQBh0JrfFI/KxKFKRH3e0oDxD8c
+PIOUpYVccVv/4Gbc0ZRs8KK0uPZN73LlQccYzPrSk/VAUeuZ52Wqk6dNrq5FHSCe
+EwPbx6aqgLwhTLlYAJqmYuDsGU9ZL09buCVKim1pjZiPaoaYAvv3KHdjEKAu9NxF
+dezd4JZ24hqYCA7EGnKgyjHxA0SiD/B8f+aBdRGDZbMlH1gKFKivjuHSfPwRv6Op
+p8ykEzk3yp0RcqSflVPg0mj+LPViYo/loLLOLybFFR7BetyFieN5QV7BKRyfc7Qi
+Se6Idh1nLIrYR9ek8BDkEE9u/JiTT0gP3QIDAQABo2MwYTAPBgNVHRMBAf8EBTAD
+AQH/MB8GA1UdIwQYMBaAFDYtHGSe9lYaC9+WnNT91wuiMlkjMB0GA1UdDgQWBBQ2
+LRxknvZWGgvflpzU/dcLojJZIzAOBgNVHQ8BAf8EBAMCAYYwDQYJKoZIhvcNAQEL
+BQADggGBAIcLj76GVhYSuVaWMMCVlVl8rHhYYufT9z2X7G/0D1G655/dAeAJLltL
+S4T7SZI44XKfVH4ztc4TO6OEMLZzslcfDzv/tUzL4EOsXtBTpsK9JgHP2lzCE+aj
+a7uxn5SGWlu0YmT/++2d+QYaVVAjqalal8NsppOYCh8GB84TXbQjOMWcR9YBozZf
+DSy3/vDNMuggZfdEOMMP57M10NoOKor+8eMGB42k4NR+G2npYHZ4uh1Ifk+eoTAh
+o5O0iz3+/8eMTkLavqpnfzBhWHfRTI8wUu6zgm+QI+tsqhPePRuwauD8r79JBnPW
+0gayZI5jIWTwvufpweKMgLyQbiGVUDtsr2c43kJ6XHoEf0ACUzbJKtGDD3Y7H/G1
+5Q7hBWbQwhUpiVeRnofS9jHQPWu0Ueq4/784hy+yPWotBIeIWEy4KzKTS+GaRDm0
+OSYtta/BdU0iZO/PzzTC5yIzwrsaq+5Idp16mub7mCAW0B36x0Phmr0DQWpZwxmX
+9envV9HcJw==
+-----END CERTIFICATE-----
+-----BEGIN RSA PRIVATE KEY-----
+MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCeCRM315FGH1Fa
+nlHRo/JfNwPi+xAor+cC4b/5qEIsGI9Zwgg9rzs/k9XOyUYoWMCNzn8c3nTYhd8h
+8KzPu7o5thiHsr1z6luVPf9zF6K4UJupR0Vba812n7Z8Ye/uyOBu0TeE6RGL7Vxv
+2cKDBLKHIRpexhic2+NkfhZGyfU5kB6IMQBs08LC4wmiPffCWhoWcLk9QbNlJL0d
+3g389oWZQ2NVD2zCvkKe4+LfaNE1Rzk+1Wb+fHhLbL/2tFi9bbM8GjzVewREZekw
+vS4fD8i/Sdx85m35QqzX33b1KbUPmZummmyC37l2oihfWrNKxcpC0alYvfwypHZp
+E26Xy2D/AgMBAAECggEABUJeDlyxK/k81twv8t8W4M5O5c3fIst/z5u9rMxJr3f9
+xUnsxki/mwULd39BQ3R4q/90QXyxvMbvvwxoY91mfCcwN8vd/C6Pb68JgkYGF0Yv
+d/m0OC+lPc5g31DPEE5FEcsKovSyrcpvahWAIKYWXuLeIstK5GV48s4zZZWxAIJa
+7IhLst4I3Y7B6vmPHCutOxL6VXPllhe1gAI/tRWH0Hpbk/KiN0jGirTWo9FrNgAZ
+aRLcrS+a4sZuVZBMe7/NR4qXs+NbafFcuWcgRdgZLoktnKyWk5/WuhRXVuTY6H5B
+pBgrffaab+qMDuziQ2SyHlm1eCnQGbl/++9UPAHZ6QKBgQDQIpVwqzfDsGxsBFo5
+y6wL5uWP8oiDoAkTjX41tgFy5G+ccHaIle/N8U+yrMkgW4tKKYFdREfA9eNxLbsB
+Dy6MkYlbE6cWbcf42QN7/Nn0jWBVrSNBssAPdQYtCJw+07/Qn1rWVIkTNHpqvEV7
+T9+JgLtSD9d3yMEeW/wWpF0PBQKBgQDCYQEQ2iwqyMtsd5GRZFXboBWAVdjfuEd9
+7sZ3SM6z3U1fkKXImdncnihlLN2Ll7tMftGLMF8yxT4OWHPC9Tn7qnatlc3oSVIm
+82Kj0S0j0dr0V4tjpxAhcfuDh1n02A+JQX1gK/rQN/H8JMqpc5FySTV3lBswTvAs
+Gdk7J2tHMwKBgDeX1TS39vglCoC7lOH1Heo77TtKu930hBgd5gUwrShkDc/KVk7b
+RadLek8uSbaD3Suc9HnWABhxVSPo5Bc/V96iDP8vu6SJBC3awUx/2DOzA3U+/rjQ
+pu46AsFKmHlLk+OEfP3crJRdowkZarGqPvn6UY50vse27qZOSYI+usCFAoGBAJhF
+fZxCDY+GtTVHhdWsEEZ45d8fYUIBDqBsyTTw6Fym5NIUcorvW2gkzehUeUm9l5CZ
+WHX9ctZHBhIe4LC9gqrQIyBg1mk95wl0aLWETCRfZXM8kYmDenN441tqUOIp0CHq
+F9mbGmS7LuojuE9+pVYuW6BNee8iJ6ukpDRe8P9ZAoGAHbXYDvWfNgHE4w15uCpE
+riR19yvlWk9tsswdefhyIb36/2qX7+4cQLZsD9b/nVF+GVwbXFgn/qjRQyds+YUD
+dpD/KciWewZRhlQvWChEH/hZrzauBnkE0qcMURW6Xf7NHn/7d+jembEc3bkyjnEI
+6yNDF7D4l5W6gvqgiN5VSM8=
+-----END RSA PRIVATE KEY-----
diff --git a/certServicePostProcessor/src/test/resources/truststore.pem b/certServicePostProcessor/src/test/resources/truststore.pem
new file mode 100644
index 00000000..3268e3a6
--- /dev/null
+++ b/certServicePostProcessor/src/test/resources/truststore.pem
@@ -0,0 +1,28 @@
+-----BEGIN CERTIFICATE-----
+MIIEszCCAxugAwIBAgIUE+27eIlr12tQ+AMxkJTf2Y+ycOEwDQYJKoZIhvcNAQEL
+BQAwYTEjMCEGCgmSJomT8ixkAQEME2MtMDRjYmE2YjhhMDQ5ODEyNGQxFTATBgNV
+BAMMDE1hbmFnZW1lbnRDQTEjMCEGA1UECgwaRUpCQ0EgQ29udGFpbmVyIFF1aWNr
+c3RhcnQwHhcNMjAwNzA4MTIzODU4WhcNMzAwNzA4MTIzODU4WjBhMSMwIQYKCZIm
+iZPyLGQBAQwTYy0wNGNiYTZiOGEwNDk4MTI0ZDEVMBMGA1UEAwwMTWFuYWdlbWVu
+dENBMSMwIQYDVQQKDBpFSkJDQSBDb250YWluZXIgUXVpY2tzdGFydDCCAaIwDQYJ
+KoZIhvcNAQEBBQADggGPADCCAYoCggGBALTlx22Ld87VO5QgkD7OJvx81a8xLRWt
+b4cqmLSBRKw+jTjX4fHCtLh98hXNtYXJ9nxPa2t8MKR/I00Wf1razX1IYN9H/diV
+uICjyMxDyK6nwEMpqaWiQgOQx1N4TjNhr19ULTbyFLQMVfXy1OrTsfoWQ2omvRxN
+LIoVKwPHd92KG6iqJDZU14ErfA6UtypDV+4rOKQBh0JrfFI/KxKFKRH3e0oDxD8c
+PIOUpYVccVv/4Gbc0ZRs8KK0uPZN73LlQccYzPrSk/VAUeuZ52Wqk6dNrq5FHSCe
+EwPbx6aqgLwhTLlYAJqmYuDsGU9ZL09buCVKim1pjZiPaoaYAvv3KHdjEKAu9NxF
+dezd4JZ24hqYCA7EGnKgyjHxA0SiD/B8f+aBdRGDZbMlH1gKFKivjuHSfPwRv6Op
+p8ykEzk3yp0RcqSflVPg0mj+LPViYo/loLLOLybFFR7BetyFieN5QV7BKRyfc7Qi
+Se6Idh1nLIrYR9ek8BDkEE9u/JiTT0gP3QIDAQABo2MwYTAPBgNVHRMBAf8EBTAD
+AQH/MB8GA1UdIwQYMBaAFDYtHGSe9lYaC9+WnNT91wuiMlkjMB0GA1UdDgQWBBQ2
+LRxknvZWGgvflpzU/dcLojJZIzAOBgNVHQ8BAf8EBAMCAYYwDQYJKoZIhvcNAQEL
+BQADggGBAIcLj76GVhYSuVaWMMCVlVl8rHhYYufT9z2X7G/0D1G655/dAeAJLltL
+S4T7SZI44XKfVH4ztc4TO6OEMLZzslcfDzv/tUzL4EOsXtBTpsK9JgHP2lzCE+aj
+a7uxn5SGWlu0YmT/++2d+QYaVVAjqalal8NsppOYCh8GB84TXbQjOMWcR9YBozZf
+DSy3/vDNMuggZfdEOMMP57M10NoOKor+8eMGB42k4NR+G2npYHZ4uh1Ifk+eoTAh
+o5O0iz3+/8eMTkLavqpnfzBhWHfRTI8wUu6zgm+QI+tsqhPePRuwauD8r79JBnPW
+0gayZI5jIWTwvufpweKMgLyQbiGVUDtsr2c43kJ6XHoEf0ACUzbJKtGDD3Y7H/G1
+5Q7hBWbQwhUpiVeRnofS9jHQPWu0Ueq4/784hy+yPWotBIeIWEy4KzKTS+GaRDm0
+OSYtta/BdU0iZO/PzzTC5yIzwrsaq+5Idp16mub7mCAW0B36x0Phmr0DQWpZwxmX
+9envV9HcJw==
+-----END CERTIFICATE-----