aboutsummaryrefslogtreecommitdiffstats
path: root/trustStoreMerger/src/test
diff options
context:
space:
mode:
authorRemigiusz Janeczek <remigiusz.janeczek@nokia.com>2020-08-05 11:36:06 +0200
committerRemigiusz Janeczek <remigiusz.janeczek@nokia.com>2020-08-05 13:53:07 +0200
commit4e61effd4ddd8e6869487914095f81a1566d0142 (patch)
treefeca1a785f14f118c5b406349faf79c7ff33f91f /trustStoreMerger/src/test
parent86ba43b6e0d298c469ee93ecfc08052d6e8ab8c9 (diff)
Add loading configuration from env to truststore merger
Issue-ID: DCAEGEN2-2253 Signed-off-by: Remigiusz Janeczek <remigiusz.janeczek@nokia.com> Change-Id: I06b3e3fcebbca96a4b9cbb4c693ae16eb29366eb
Diffstat (limited to 'trustStoreMerger/src/test')
-rw-r--r--trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/TrustStoreMergerTest.java4
-rw-r--r--trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/PathValidatorTest.java58
-rw-r--r--trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TruststoresPathsProviderTest.java88
-rw-r--r--trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactoryTest.java98
4 files changed, 246 insertions, 2 deletions
diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/TrustStoreMergerTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/TrustStoreMergerTest.java
index 11b18bf1..4787897a 100644
--- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/TrustStoreMergerTest.java
+++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/TrustStoreMergerTest.java
@@ -34,9 +34,9 @@ class TrustStoreMergerTest {
AppExitHandler appExitHandler;
@Test
- void shouldExitWithSuccess() {
+ void shouldExitWithMergeConfigurationExceptionDueToMissingEnvs() {
new TrustStoreMerger(appExitHandler).run();
- verify(appExitHandler).exit(ExitStatus.SUCCESS);
+ verify(appExitHandler).exit(ExitStatus.MERGER_CONFIGURATION_EXCEPTION);
}
}
diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/PathValidatorTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/PathValidatorTest.java
new file mode 100644
index 00000000..1c455764
--- /dev/null
+++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/PathValidatorTest.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.truststoremerger.certification.file;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+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/certification/file/TruststoresPathsProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TruststoresPathsProviderTest.java
new file mode 100644
index 00000000..d52d1899
--- /dev/null
+++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TruststoresPathsProviderTest.java
@@ -0,0 +1,88 @@
+package org.onap.oom.truststoremerger.certification.file;
+
+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.Optional;
+
+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.certification.file.TruststoresPathsProvider.TRUSTSTORES_ENV;
+import static org.onap.oom.truststoremerger.certification.file.TruststoresPathsProvider.TRUSTSTORES_PASSWORDS_ENV;
+
+@ExtendWith(MockitoExtension.class)
+class TruststoresPathsProviderTest {
+
+ 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 INVALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.invalid";
+ private static final String INVALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:/.pass";
+
+ @Mock
+ private EnvProvider envProvider;
+ private TruststoresPathsProvider truststoresPathsProvider;
+
+ @BeforeEach
+ void setUp() {
+ truststoresPathsProvider = new TruststoresPathsProvider(envProvider, new PathValidator());
+ }
+
+ @Test
+ void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException {
+ mockTruststoresEnv(VALID_TRUSTSTORES);
+
+ assertThat(truststoresPathsProvider.getTruststores())
+ .contains("/opt/app/certificates/truststore.jks",
+ "/opt/app/certificates/truststore.pem");
+ }
+
+ @Test
+ void shouldReturnCorrectListWhenTruststoresPasswordsValid() throws TruststoresPathsProviderException {
+ mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS);
+
+ assertThat(truststoresPathsProvider.getTruststoresPasswords())
+ .contains("/opt/app/certificates/truststore.pass",
+ "");
+ }
+
+ @Test
+ void shouldThrowExceptionWhenTruststoresEmpty() {
+ mockTruststoresEnv("");
+
+ assertThatExceptionOfType(TruststoresPathsProviderException.class)
+ .isThrownBy(truststoresPathsProvider::getTruststores);
+ }
+
+ @Test
+ void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() {
+ mockTruststoresEnv(INVALID_TRUSTSTORES);
+
+ assertThatExceptionOfType(TruststoresPathsProviderException.class)
+ .isThrownBy(truststoresPathsProvider::getTruststores);
+ }
+
+ @Test
+ void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() {
+ mockTruststoresPasswordsEnv(INVALID_TRUSTSTORES_PASSWORDS);
+
+ assertThatExceptionOfType(TruststoresPathsProviderException.class)
+ .isThrownBy(truststoresPathsProvider::getTruststoresPasswords);
+ }
+
+ private void mockTruststoresEnv(String truststores) {
+ mockEnv(truststores, TRUSTSTORES_ENV);
+ }
+
+ private void mockTruststoresPasswordsEnv(String truststoresPasswords) {
+ mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_ENV);
+ }
+
+ private void mockEnv(String envValue, String envName) {
+ when(envProvider.getEnv(envName))
+ .thenReturn(Optional.of(envValue));
+ }
+}
diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactoryTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactoryTest.java
new file mode 100644
index 00000000..336ba5da
--- /dev/null
+++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/configuration/MergerConfigurationFactoryTest.java
@@ -0,0 +1,98 @@
+/*============LICENSE_START=======================================================
+ * oom-truststore-merger
+ * ================================================================================
+ * Copyright (C) 2020 Nokia. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.oom.truststoremerger.configuration;
+
+import 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.certification.file.TruststoresPathsProvider;
+import org.onap.oom.truststoremerger.certification.file.TruststoresPathsProviderException;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class MergerConfigurationFactoryTest {
+
+ private static final String BASE_TRUSTSTORE_PATH = "/opt/app/truststore_";
+ private static final String TRUSTSTORE_EXTENSION = ".jks";
+ private static final String PASSWORD_EXTENSION = ".pass";
+
+ @Mock
+ private TruststoresPathsProvider pathsProvider;
+ private MergerConfigurationFactory factory;
+
+ @BeforeEach
+ void setUp() {
+ factory = new MergerConfigurationFactory(pathsProvider);
+ }
+
+ @Test
+ 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);
+
+ MergerConfiguration configuration = factory.createConfiguration();
+
+ assertThat(configuration.getTruststoreFilePaths()).containsAll(truststoresPaths);
+ assertThat(configuration.getTruststoreFilePasswordPaths()).containsAll(truststorePasswordPaths);
+ }
+
+ @Test
+ void shouldThrowExceptionWhenTruststoresLenghtDifferentThanTruststoresPasswordsLength() throws TruststoresPathsProviderException {
+ int numberOfTruststores = 5;
+ int numberOfTruststoresPasswords = 4;
+ List<String> truststoresPaths = createListOfPathsWithExtension(numberOfTruststores, TRUSTSTORE_EXTENSION);
+ List<String> truststorePasswordPaths = createListOfPathsWithExtension(numberOfTruststoresPasswords, PASSWORD_EXTENSION);
+ mockPaths(truststoresPaths, truststorePasswordPaths);
+
+ assertThatExceptionOfType(MergerConfigurationException.class)
+ .isThrownBy(factory::createConfiguration);
+ }
+
+ private void mockPaths(List<String> truststores, List<String> truststoresPasswords) throws TruststoresPathsProviderException {
+ mockTruststores(truststores);
+ mockTruststoresPasswords(truststoresPasswords);
+ }
+
+ private void mockTruststores(List<String> truststores) throws TruststoresPathsProviderException {
+ when(pathsProvider.getTruststores()).thenReturn(truststores);
+ }
+
+ private void mockTruststoresPasswords(List<String> truststoresPasswords) throws TruststoresPathsProviderException {
+ when(pathsProvider.getTruststoresPasswords()).thenReturn(truststoresPasswords);
+ }
+
+ private List<String> createListOfPathsWithExtension(int numberOfPaths, String password_extension) {
+ List<String> paths = new ArrayList<>();
+ while (numberOfPaths-- > 0) {
+ paths.add(BASE_TRUSTSTORE_PATH + numberOfPaths + password_extension);
+ }
+ return paths;
+ }
+}