diff options
author | 2020-09-02 14:35:32 +0200 | |
---|---|---|
committer | 2020-09-09 14:03:01 +0200 | |
commit | a9fd6d0a175e647ac36932ce77b91f9e54e97084 (patch) | |
tree | 2c9c150c07162770be7629b0bd665185cff7f9b0 /trustStoreMerger/src/test | |
parent | 4014c7482b233bba9e344b9b3fbe6b7641ebdcfd (diff) |
Refactor truststore merger logic
- Merge PemTruststore and JavaTruststore into Truststore
- Rename controller classes to be consistent
- Remove duplicated methods
Issue-ID: DCAEGEN2-2253
Signed-off-by: Piotr Marcinkiewicz <piotr.marcinkiewicz@nokia.com>
Change-Id: I3270cd9811e5eaf360ceea0d0ca99be1155eaf00
Diffstat (limited to 'trustStoreMerger/src/test')
14 files changed, 449 insertions, 528 deletions
diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststoreTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststoreTest.java deleted file mode 100644 index eccf36bc..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/JavaTruststoreTest.java +++ /dev/null @@ -1,116 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.oom.truststoremerger.certification.file.model; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.onap.oom.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE; - -import java.io.IOException; -import java.security.cert.Certificate; -import java.util.List; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Test; -import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.TestCertificateProvider; - - -class JavaTruststoreTest { - public static final int FIRST_ELEMENT = 0; - private static final int EXPECTED_ONE = 1; - public static final int EXPECTED_THREE = 3; - - @Test - void jksTruststoreShouldReadCertificatesFromFile() throws ExitableException { - - //given - JavaTruststore jksTruststoreFile = TestCertificateProvider.getSampleJksTruststoreFile(); - - //when - List<CertificateWithAlias> certificates = jksTruststoreFile.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 - JavaTruststore jksTruststore = TestCertificateProvider.createTmpJksTruststoreFileWithUniqAlias(); - List<CertificateWithAlias> p12certificates = TestCertificateProvider.getSampleP12Truststore() - .getCertificates(); - List<CertificateWithAlias> pemCertificates = TestCertificateProvider.getSamplePemTruststoreFile() - .getCertificates(); - - //when - jksTruststore.addCertificate(p12certificates); - jksTruststore.addCertificate(pemCertificates); - - //then - assertThat(jksTruststore.getCertificates()).hasSize(EXPECTED_THREE); - - } - - @Test - void p12TruststoreShouldReadCertificatesFromFile() throws ExitableException { - //given - JavaTruststore p12Truststore = TestCertificateProvider.getSampleP12Truststore(); - - //when - List<CertificateWithAlias> certificatesWithAliases = p12Truststore.getCertificates(); - Certificate certificate = certificatesWithAliases.get(FIRST_ELEMENT).getCertificate(); - - //then - assertThat(certificatesWithAliases).hasSize(EXPECTED_ONE); - assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE); - } - - - @Test - void p12TruststoreShouldAddDifferentCertificates() throws Exception { - //given - JavaTruststore p12Truststore = TestCertificateProvider.createTmpP12TruststoreFile(); - List<CertificateWithAlias> jksTruststoreCertificates = TestCertificateProvider - .getSampleJksTruststoreFileWithUniqueAlias() - .getCertificates(); - List<CertificateWithAlias> pemTruststoreCertificates = TestCertificateProvider.getSamplePemTruststoreFile() - .getCertificates(); - - //when - p12Truststore.addCertificate(jksTruststoreCertificates); - p12Truststore.addCertificate(pemTruststoreCertificates); - p12Truststore.saveFile(); - - - //then - JavaTruststore p12TruststoreSaved = TestCertificateProvider.getTmpP12TruststoreFile(); - assertThat(p12TruststoreSaved.getCertificates()).hasSize(EXPECTED_THREE); - } - - - - @AfterAll - static void removeTemporaryFiles() throws IOException { - TestCertificateProvider.removeTemporaryFiles(); - } -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststoreTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststoreTest.java deleted file mode 100644 index e7ffa093..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/PemTruststoreTest.java +++ /dev/null @@ -1,149 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.oom.truststoremerger.certification.file.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.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE; - -import java.io.IOException; -import java.security.cert.Certificate; -import java.security.cert.CertificateEncodingException; -import java.util.ArrayList; -import java.util.List; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Test; -import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAliasFactory; -import org.onap.oom.truststoremerger.certification.file.TestCertificateProvider; -import org.onap.oom.truststoremerger.certification.file.exception.MissingTruststoreException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreDataOperationException; -import org.onap.oom.truststoremerger.certification.file.exception.WriteTruststoreFileException; - -class PemTruststoreTest { - - public 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 pemTruststoreShouldReadCertificatesFromFile() throws ExitableException { - - //given - PemTruststore pemTruststore = TestCertificateProvider.getSamplePemTruststoreFile(); - - //when - List<CertificateWithAlias> certificates = pemTruststore.getCertificates(); - Certificate certificate = certificates.get(FIRST_ELEMENT).getCertificate(); - //then - - assertThat(certificates).hasSize(EXPECTED_ONE); - assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE); - } - - @Test - void pemTruststoreShouldAddDifferentCertificates() throws IOException, ExitableException { - - //given - PemTruststore tmpPemTruststoreFile = TestCertificateProvider.createTmpPemTruststoreFile(); - List<CertificateWithAlias> jksTruststoreCertificates = TestCertificateProvider - .getSampleJksTruststoreFileWithUniqueAlias().getCertificates(); - List<CertificateWithAlias> p12TruststoreCertificates = TestCertificateProvider.getSampleP12Truststore() - .getCertificates(); - - //when - tmpPemTruststoreFile.addCertificate(jksTruststoreCertificates); - tmpPemTruststoreFile.addCertificate(p12TruststoreCertificates); - tmpPemTruststoreFile.saveFile(); - - PemTruststore tmpPemTruststoreSaved = TestCertificateProvider.getTmpPemTruststoreFile(); - List<CertificateWithAlias> addedCertificates = tmpPemTruststoreSaved.getCertificates(); - Certificate certificate = addedCertificates.get(FIRST_ELEMENT).getCertificate(); - - //then - assertThat(addedCertificates).hasSize(EXPECTED_THREE); - assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE); - - } - - @Test - void privateKeyIsSkippedWhileReadingCertificates() throws ExitableException { - //given - PemTruststore pemTruststore = TestCertificateProvider.getPemWithPrivateKeyTruststoreFile(); - - //when - List<CertificateWithAlias> certificate = pemTruststore.getCertificates(); - //then - - assertThat(certificate).hasSize(EXPECTED_ONE); - } - - @Test - void shouldThrowExceptionWhenCannotSaveFile() throws IOException, ExitableException { - //given - PemTruststore tmpPemTruststoreFile = TestCertificateProvider.createTmpPemTruststoreFile(); - List<CertificateWithAlias> pemTruststoreCertificates = - TestCertificateProvider.getSamplePemTruststoreFile().getCertificates(); - //when - tmpPemTruststoreFile.addCertificate(pemTruststoreCertificates); - tmpPemTruststoreFile.getFile().setWritable(false); - //then - assertThatExceptionOfType(WriteTruststoreFileException.class) - .isThrownBy(tmpPemTruststoreFile::saveFile); - - } - - @Test - void shouldThrowExceptionWhenFileNotContainsCertificate() throws IOException { - //given - PemTruststore tmpPemTruststoreFile = TestCertificateProvider.createEmptyTmpPemTruststoreFile(); - //when//then - assertThatExceptionOfType(MissingTruststoreException.class) - .isThrownBy(tmpPemTruststoreFile::getCertificates); - } - - @Test - void shouldThrowExceptionWhenCannotConvertCertificateToPem() throws Exception { - //given - PemTruststore pemTruststore = TestCertificateProvider.createTmpPemTruststoreFile(); - Certificate certificate = mock(Certificate.class); - - when(certificate.getEncoded()).thenThrow(new CertificateEncodingException()); - - List<CertificateWithAlias> certificatesWithAliases = new ArrayList<>(); - certificatesWithAliases.add(factory.createPemCertificate(certificate)); - pemTruststore.addCertificate(certificatesWithAliases); - - //when //then - assertThatExceptionOfType(TruststoreDataOperationException.class) - .isThrownBy(pemTruststore::saveFile); - } - - @AfterAll - static void removeTemporaryFiles() throws IOException { - TestCertificateProvider.removeTemporaryFiles(); - } - -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProviderTest.java deleted file mode 100644 index 0dadcfef..00000000 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFilesListProviderTest.java +++ /dev/null @@ -1,87 +0,0 @@ -/*============LICENSE_START======================================================= - * oom-truststore-merger - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - - -package org.onap.oom.truststoremerger.certification.file.provider; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.onap.oom.truststoremerger.certification.file.TruststoreFileFactory; -import org.onap.oom.truststoremerger.certification.file.TruststoreFilesListProvider; -import org.onap.oom.truststoremerger.certification.file.model.JavaTruststore; -import org.onap.oom.truststoremerger.certification.file.model.PemTruststore; -import org.onap.oom.truststoremerger.certification.file.model.Truststore; -import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; - -import java.io.File; -import java.util.Arrays; -import java.util.List; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreFileFactoryException; - -import static org.assertj.core.api.Assertions.assertThat; - -class TruststoreFilesListProviderTest { - - 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 TruststoreFilesListProvider truststoreFilesListProvider; - - @BeforeEach - void setUp() { - TruststoreFileFactory truststoreFileFactory = new TruststoreFileFactory(new FileManager(), new PasswordReader()); - truststoreFilesListProvider = new TruststoreFilesListProvider(truststoreFileFactory); - } - - @Test - void shouldReturnTruststoreFilesList() - throws TruststoreFileFactoryException, PasswordReaderException, LoadTruststoreException, KeystoreInstanceException { - 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); - List<Truststore> truststoreFilesList = truststoreFilesListProvider.getTruststoreFilesList(truststorePaths, truststorePasswordPaths); - assertThat(truststoreFilesList.size()).isEqualTo(3); - assertCorrectJksTruststore(truststoreFilesList.get(0), TRUSTSTORE_JKS_PATH); - assertCorrectP12Truststore(truststoreFilesList.get(1), TRUSTSTORE_P12_PATH); - assertCorrectPemTruststore(truststoreFilesList.get(2), TRUSTSTORE_PEM_PATH); - } - - private void assertCorrectJksTruststore(Truststore truststore, String truststorePath) { - assertCorrectTypeAndTruststorePath(truststore, truststorePath, JavaTruststore.class); - } - - private void assertCorrectP12Truststore(Truststore truststore, String truststorePath) { - assertCorrectTypeAndTruststorePath(truststore, truststorePath, JavaTruststore.class); - } - - private void assertCorrectPemTruststore(Truststore truststore, String truststorePath) { - assertCorrectTypeAndTruststorePath(truststore, truststorePath, PemTruststore.class); - } - - private void assertCorrectTypeAndTruststorePath(Truststore truststore, String truststorePath, Class<?> truststoreType) { - assertThat(truststore).isInstanceOf(truststoreType); - assertThat(truststore.getFile()).isEqualTo(new File(truststorePath)); - } - -} diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/TruststoreTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/BackupCreatorTest.java index eea1f9c7..b81eb36e 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/model/TruststoreTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/BackupCreatorTest.java @@ -17,44 +17,35 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.model; +package org.onap.oom.truststoremerger.common; + +import static org.assertj.core.api.Assertions.assertThat; import java.io.File; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -import org.onap.oom.truststoremerger.certification.file.exception.CreateBackupException; -import org.onap.oom.truststoremerger.certification.file.provider.PemCertificateController; - -import static org.assertj.core.api.Assertions.assertThat; +import org.onap.oom.truststoremerger.merger.exception.CreateBackupException; +import org.onap.oom.truststoremerger.merger.model.TestCertificateProvider; -class TruststoreTest { - - private static final String PEM_FILE_PATH = "src/test/resources/truststore.pem"; - private static final String PEM_BACKUP_FILE_PATH = "src/test/resources/truststore.pem.bak"; - private static final String BACKUP_EXTENSION = ".bak"; +public class BackupCreatorTest { + public static final String BAK_EXTENSION = ".bak"; @Test - void createBackupShouldCreateFileWithExtension() throws CreateBackupException { + void shouldCreateBackupProvidedFile() throws CreateBackupException { //given - File pemFile = new File(PEM_FILE_PATH); - Truststore truststore = new PemTruststore(pemFile, new PemCertificateController(pemFile)); + File fileToBackup = new File(TestCertificateProvider.PEM_FILE_PATH); + String backupFilePath = fileToBackup.getPath() + BAK_EXTENSION; //when - truststore.createBackup(); - + BackupCreator.createBackup(fileToBackup); //then - File backupFile = new File(PEM_BACKUP_FILE_PATH); - assertThat(backupFile.getName().endsWith(BACKUP_EXTENSION)).isTrue(); - assertThat(backupFile.isFile()).isTrue(); + assertThat(fileToBackup.equals(new File(backupFilePath))); } - - @AfterAll - static void removeBackupFile() throws IOException { - Files.deleteIfExists(Paths.get(PEM_BACKUP_FILE_PATH)); + @AfterEach + void removeTemporaryFiles() throws IOException { + TestCertificateProvider.removeTemporaryFiles(); } } diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/FileManagerTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/ExtensionResolverTest.java index c649ba68..e59a7671 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/FileManagerTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/ExtensionResolverTest.java @@ -18,7 +18,7 @@ */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.common; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -27,9 +27,7 @@ import java.io.File; import static org.assertj.core.api.Assertions.assertThat; -class FileManagerTest { - - private FileManager fileManager = new FileManager(); +class ExtensionResolverTest { @ParameterizedTest @CsvSource(value = { @@ -40,7 +38,7 @@ class FileManagerTest { "opt/app/truststore:''", }, delimiter = ':') void shouldReturnCorrectExtension(String filePath, String expectedExtension) { - String extension = fileManager.getExtension(new File(filePath)); + String extension = ExtensionResolver.get(new File(filePath)); assertThat(extension).isEqualTo(expectedExtension); } diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/PasswordReaderTest.java index 40eda4dd..a43951bd 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PasswordReaderTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/common/PasswordReaderTest.java @@ -17,12 +17,12 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.common; import org.junit.jupiter.api.Test; import java.io.File; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -31,15 +31,13 @@ class PasswordReaderTest { @Test void shouldReturnCorrectPasswordFromFile() throws PasswordReaderException { - PasswordReader passwordReader = new PasswordReader(); - String fileData = passwordReader.readPassword(new File("src/test/resources/truststore-jks.pass")); + String fileData = PasswordReader.readPassword(new File("src/test/resources/truststore-jks.pass")); assertThat(fileData).isEqualTo("EOyuFbuYDyq_EhpboM72RHua"); } @Test void shouldThrowExceptionForNonExistingFile() { - PasswordReader passwordReader = new PasswordReader(); assertThatExceptionOfType(PasswordReaderException.class) - .isThrownBy(() -> passwordReader.readPassword(new File("src/test/resources/non-esisting-file.pass"))); + .isThrownBy(() -> PasswordReader.readPassword(new File("src/test/resources/non-esisting-file.pass"))); } } diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProviderTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/TruststoreFilesProviderTest.java new file mode 100644 index 00000000..9fc00a42 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/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.truststoremerger.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.truststoremerger.merger.exception.KeystoreInstanceException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreFileFactoryException; +import org.onap.oom.truststoremerger.merger.model.Truststore; +import org.onap.oom.truststoremerger.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/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreControllerTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreTest.java index 8ee77ef2..99b8e623 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/JavaCertificateStoreControllerTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/JavaTruststoreTest.java @@ -17,43 +17,40 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.merger.model; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import static org.onap.oom.truststoremerger.certification.file.TestCertificateProvider.getSampleJksTruststoreFile; import java.util.List; import org.junit.jupiter.api.Test; import org.onap.oom.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.TestCertificateProvider; -import org.onap.oom.truststoremerger.certification.file.exception.AliasConflictException; -import org.onap.oom.truststoremerger.certification.file.exception.MissingTruststoreException; -import org.onap.oom.truststoremerger.certification.file.model.JavaTruststore; - -class JavaCertificateStoreControllerTest { +import org.onap.oom.truststoremerger.merger.exception.AliasConflictException; +import org.onap.oom.truststoremerger.merger.exception.MissingTruststoreException; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; +class JavaTruststoreTest { @Test void throwExceptionWhenAliasConflictDetected() throws Exception { //given - JavaTruststore p12Truststore = TestCertificateProvider.getSampleP12Truststore(); - List<CertificateWithAlias> jksTruststoreCertificates = getSampleJksTruststoreFile().getCertificates(); + Truststore p12Truststore = TestCertificateProvider.getSampleP12Truststore(); + + List<CertificateWithAlias> certificateFromJks = TestCertificateProvider + .getSampleJksTruststoreFile().getCertificates(); //when //then assertThatExceptionOfType(AliasConflictException.class) - .isThrownBy(() -> p12Truststore.addCertificate(jksTruststoreCertificates)); + .isThrownBy(() -> p12Truststore.addCertificates(certificateFromJks)); } - @Test void throwExceptionWhenFileNotContainsTruststoreEntry() throws ExitableException { //given - JavaTruststore p12Truststore = TestCertificateProvider.getSampleP12Keystore(); + Truststore p12Truststore = TestCertificateProvider.getSampleP12Keystore(); //when//then assertThatExceptionOfType(MissingTruststoreException.class) - .isThrownBy(p12Truststore::getCertificates); + .isThrownBy(() -> p12Truststore.getCertificates()); } } diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateControllerTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/PemTruststoreTest.java index 080fcca3..505a6826 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/PemCertificateControllerTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/PemTruststoreTest.java @@ -17,50 +17,70 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.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.truststoremerger.api.ExitableException; -import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias; -import org.onap.oom.truststoremerger.certification.file.TestCertificateProvider; -import org.onap.oom.truststoremerger.certification.file.exception.MissingTruststoreException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreDataOperationException; -import org.onap.oom.truststoremerger.certification.file.model.PemTruststore; +import org.onap.oom.truststoremerger.merger.exception.MissingTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreDataOperationException; +import org.onap.oom.truststoremerger.merger.exception.WriteTruststoreFileException; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; -class PemCertificateControllerTest { +class PemTruststoreTest { + + private static final int EXPECTED_ONE = 1; @Test - void getNotEmptyCertificateListShouldThrowExceptionWhenFileNotContainsCertificate() { + void getCertificatesShouldThrowExceptionWhenFileNotContainsCertificate() { //given - File emptyPemFile = TestCertificateProvider.getEmptyPemTruststoreFile().getFile(); - PemCertificateController pemCertificateController = new PemCertificateController(emptyPemFile); + File emptyPemFile = TestCertificateProvider.getEmptyPemFile(); + PemTruststore pemCertificate = new PemTruststore(emptyPemFile); //when//then assertThatExceptionOfType(MissingTruststoreException.class) - .isThrownBy(pemCertificateController::getNotEmptyCertificateList); + .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 - PemTruststore pemTruststore = TestCertificateProvider.getSamplePemTruststoreFile(); + Truststore pemTruststore = TestCertificateProvider.getSamplePemTruststoreFile(); + List<CertificateWithAlias> wrappedCertificates = pemTruststore.getCertificates(); - File notEmptyPemFile = pemTruststore.getFile(); List<Certificate> certificateList = unWrapCertificate(wrappedCertificates); - PemCertificateController pemCertificateController = new PemCertificateController(notEmptyPemFile); - String expected = TestCertificateProvider.getExpectedPemCertificateAsString(); + File notEmptyPemFile = TestCertificateProvider.getNotEmptyPemFile(); + PemTruststore pemCertificate = new PemTruststore(notEmptyPemFile); //when - String certificateTransformed = pemCertificateController.transformToStringInPemFormat(certificateList); + String certificateTransformed = pemCertificate.transformToStringInPemFormat(certificateList); //then + String expected = TestCertificateProvider.getExpectedPemCertificateAsString(); assertThat(certificateTransformed).isEqualTo(expected); } @@ -68,21 +88,34 @@ class PemCertificateControllerTest { void fileNotContainsPemCertificateShouldReturnTrueIfFileNotContainsCertificate() throws TruststoreDataOperationException { //given - File emptyPemFile = TestCertificateProvider.getEmptyPemTruststoreFile().getFile(); - PemCertificateController pemCertificateController = new PemCertificateController(emptyPemFile); + File emptyPemFile = TestCertificateProvider.getEmptyPemFile(); + PemTruststore pemCertificate = new PemTruststore(emptyPemFile); //when//then - assertThat(pemCertificateController.isFileWithoutPemCertificate()).isTrue(); + assertThat(pemCertificate.isFileWithoutPemCertificate()).isTrue(); } @Test void fileNotContainsPemCertificateShouldReturnFalseIfFileContainsCertificate() throws TruststoreDataOperationException { //given - File notEmptyPemFile = TestCertificateProvider.getSamplePemTruststoreFile().getFile(); - PemCertificateController pemCertificateController = new PemCertificateController(notEmptyPemFile); + File notEmptyPemFile = TestCertificateProvider.getNotEmptyPemFile(); + PemTruststore pemCertificate = new PemTruststore(notEmptyPemFile); //when//then - assertThat(pemCertificateController.isFileWithoutPemCertificate()).isFalse(); + 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) { diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TestCertificateProvider.java index b5893230..abd77d19 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/TestCertificateProvider.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TestCertificateProvider.java @@ -17,7 +17,10 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file; +package org.onap.oom.truststoremerger.merger.model; + +import static org.onap.oom.truststoremerger.api.CertificateConstants.JKS_TYPE; +import static org.onap.oom.truststoremerger.api.CertificateConstants.PKCS12_TYPE; import java.io.File; import java.io.IOException; @@ -25,26 +28,22 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; -import org.onap.oom.truststoremerger.certification.file.model.JavaTruststore; -import org.onap.oom.truststoremerger.certification.file.model.PemTruststore; -import org.onap.oom.truststoremerger.certification.file.provider.JavaCertificateStoreController; -import org.onap.oom.truststoremerger.certification.file.provider.CertificateStoreControllerFactory; -import org.onap.oom.truststoremerger.certification.file.provider.PemCertificateController; +import org.onap.oom.truststoremerger.merger.exception.KeystoreInstanceException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; -public class TestCertificateProvider { +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 = "88y9v5D8H3SG6bZWRVHDfOAo"; + 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 = "Foh49MJNYI7S_pEzE9gvUDSu"; + 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 = "EOyuFbuYDyq_EhpboM72RHua"; + 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"; @@ -52,99 +51,95 @@ public class TestCertificateProvider { 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"; - private static final CertificateStoreControllerFactory certificateStoreControllerFactory = new CertificateStoreControllerFactory(); + 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"; - public static JavaTruststore getSampleP12Truststore() throws LoadTruststoreException, KeystoreInstanceException { - return createP12TruststoreInstance(SAMPLE_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD); + private TestCertificateProvider() { } - public static JavaTruststore getSampleP12Keystore() throws LoadTruststoreException, KeystoreInstanceException { - return createP12TruststoreInstance(SAMPLE_P12_KEYSTORE_FILE_PATH, SAMPLE_P12_KEYSTORE_PASSWORD); + public static Truststore getSampleP12Truststore() + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + return createJavaTruststore(SAMPLE_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD_PATH, PKCS12_TYPE); } - public static JavaTruststore createTmpP12TruststoreFile() - throws IOException, LoadTruststoreException, KeystoreInstanceException { - copyFile(SAMPLE_P12_TRUSTSTORE_FILE_PATH, TMP_P12_TRUSTSTORE_FILE_PATH); - return createP12TruststoreInstance(TMP_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD); + public static Truststore getSampleP12Keystore() + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + return createJavaTruststore(SAMPLE_P12_KEYSTORE_FILE_PATH, SAMPLE_P12_KEYSTORE_PASSWORD_PATH, PKCS12_TYPE); } - public static JavaTruststore getTmpP12TruststoreFile() throws LoadTruststoreException, KeystoreInstanceException { - return createP12TruststoreInstance(TMP_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD); - } - - private static JavaTruststore createP12TruststoreInstance(String filePath, String password) - throws LoadTruststoreException, KeystoreInstanceException { - File certFile = getFile(filePath); - JavaCertificateStoreController storeController = certificateStoreControllerFactory - .createLoadedPkcs12CertificateStoreController(certFile, password); - return new JavaTruststore(certFile, storeController); + 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 PemTruststore getSamplePemTruststoreFile() { + public static Truststore getSamplePemTruststoreFile() { return getPemTruststoreInstance(SAMPLE_PEM_TRUSTSTORE_FILE_PATH); } - public static PemTruststore getEmptyPemTruststoreFile() { - return getPemTruststoreInstance(EMPTY_PEM_TRUSTSTORE_FILE_PATH); - } - - public static PemTruststore createEmptyTmpPemTruststoreFile() throws IOException { + 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 PemTruststore createTmpPemTruststoreFile() throws IOException { + 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 PemTruststore getTmpPemTruststoreFile() { - return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH); - } - - public static PemTruststore getPemWithPrivateKeyTruststoreFile() { - return getPemTruststoreInstance(SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH); - } - public static String getExpectedPemCertificateAsString() throws IOException { Path samplePemFilePath = Paths.get(SAMPLE_PEM_TRUSTSTORE_FILE_PATH); return Files.readString(samplePemFilePath); } - public static JavaTruststore getSampleJksTruststoreFile() - throws LoadTruststoreException, KeystoreInstanceException { - return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); + public static Truststore getSampleJksTruststoreFile() + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { + return createJavaTruststore(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD_PATH, JKS_TYPE); } - public static JavaTruststore getSampleJksTruststoreFileWithUniqueAlias() - throws LoadTruststoreException, KeystoreInstanceException { - return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, - SAMPLE_JKS_TRUSTSTORE_PASSWORD); + 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 JavaTruststore createTmpJksTruststoreFileWithUniqAlias() - throws IOException, LoadTruststoreException, KeystoreInstanceException { + public static Truststore createTmpJksTruststoreFileWithUniqAlias() + throws IOException, LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { copyFile(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, TMP_JKS_TRUSTSTORE_FILE_PATH); - return createJksTruststoreInstance(TMP_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD); + return 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 JavaTruststore createJksTruststoreInstance(String filePath, String password) - throws LoadTruststoreException, KeystoreInstanceException { + private static Truststore createJavaTruststore(String filePath, String password, String instanceType) + throws LoadTruststoreException, KeystoreInstanceException, PasswordReaderException { File certFile = getFile(filePath); - JavaCertificateStoreController storeController = certificateStoreControllerFactory - .createLoadedJksCertificateStoreController(certFile, password); - return new JavaTruststore(certFile, storeController); + return JavaTruststoreFactory.create(certFile, password, instanceType); } - private static PemTruststore getPemTruststoreInstance(String tmpPemTruststoreFilePath) { + private static Truststore getPemTruststoreInstance( + String tmpPemTruststoreFilePath) { File file = getFile(tmpPemTruststoreFilePath); - return new PemTruststore(file, new PemCertificateController(file)); + return new PemTruststore(file); } private static void copyFile(String sourcePath, String destPath) throws IOException { diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFactoryTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactoryTest.java index b2063cc3..eb6c847a 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/TruststoreFactoryTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreFactoryTest.java @@ -18,25 +18,18 @@ */ -package org.onap.oom.truststoremerger.certification.file.provider; +package org.onap.oom.truststoremerger.merger.model; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; -import org.onap.oom.truststoremerger.certification.file.TruststoreFileFactory; -import org.onap.oom.truststoremerger.certification.file.model.JavaTruststore; -import org.onap.oom.truststoremerger.certification.file.model.PemTruststore; -import org.onap.oom.truststoremerger.certification.file.model.Truststore; -import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException; -import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException; - -import java.io.File; -import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException; -import org.onap.oom.truststoremerger.certification.file.exception.TruststoreFileFactoryException; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import org.onap.oom.truststoremerger.merger.exception.KeystoreInstanceException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreFileFactoryException; @ExtendWith(MockitoExtension.class) class TruststoreFactoryTest { @@ -50,66 +43,66 @@ class TruststoreFactoryTest { 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"; - private TruststoreFileFactory truststoreFileFactory; - - @BeforeEach - void setUp() { - truststoreFileFactory = new TruststoreFileFactory(new FileManager(), new PasswordReader()); - } - @Test void shouldReturnCorrectJksTruststoreForJksFile() throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException { - Truststore truststore = truststoreFileFactory - .create(TRUSTSTORE_JKS_PATH, TRUSTSTORE_JKS_PASS_PATH); - assertThat(truststore).isInstanceOf(JavaTruststore.class); - JavaTruststore jksTruststore = (JavaTruststore) truststore; - assertThat(jksTruststore.getFile()).isEqualTo(new File(TRUSTSTORE_JKS_PATH)); + //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 { - Truststore truststore = truststoreFileFactory - .create(TRUSTSTORE_P12_PATH, - TRUSTSTORE_P12_PASS_PATH); - assertThat(truststore).isInstanceOf(JavaTruststore.class); + //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 { - Truststore truststore = truststoreFileFactory - .create(TRUSTSTORE_PEM_PATH, - EMPTY_PASS_PATH); - assertThat(truststore).isInstanceOf(PemTruststore.class); + //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( - () -> truststoreFileFactory.create(TRUSTSTORE_P12_PATH, EMPTY_PASS_PATH) + () -> TruststoreFactory.create(TRUSTSTORE_P12_PATH, EMPTY_PASS_PATH) ); } @Test void shouldThrowExceptionForInvalidJksPassPath() { assertThatExceptionOfType(PasswordReaderException.class).isThrownBy( - () -> truststoreFileFactory.create(TRUSTSTORE_JKS_PATH, EMPTY_PASS_PATH) + () -> TruststoreFactory.create(TRUSTSTORE_JKS_PATH, EMPTY_PASS_PATH) ); } @Test void shouldThrowExceptionForUnknownTruststoreExtension() { assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy( - () -> truststoreFileFactory.create(TRUSTSTORE_UNKNOWN_EXTENSION_PATH, TRUSTSTORE_JKS_PASS_PATH) + () -> TruststoreFactory + .create(TRUSTSTORE_UNKNOWN_EXTENSION_PATH, TRUSTSTORE_JKS_PASS_PATH) ); } @Test void shouldThrowExceptionForNonExistingTruststoreFile() { assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy( - () -> truststoreFileFactory.create(NON_EXISTING_TRUSTSTORE_PATH, TRUSTSTORE_JKS_PASS_PATH) + () -> TruststoreFactory.create(NON_EXISTING_TRUSTSTORE_PATH, TRUSTSTORE_JKS_PASS_PATH) ); } diff --git a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreTest.java new file mode 100644 index 00000000..6c294834 --- /dev/null +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/TruststoreTest.java @@ -0,0 +1,204 @@ +/*============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.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.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE; + +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.truststoremerger.api.ExitableException; +import org.onap.oom.truststoremerger.merger.exception.CreateBackupException; +import org.onap.oom.truststoremerger.merger.exception.KeystoreInstanceException; +import org.onap.oom.truststoremerger.merger.exception.LoadTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.MissingTruststoreException; +import org.onap.oom.truststoremerger.merger.exception.PasswordReaderException; +import org.onap.oom.truststoremerger.merger.exception.TruststoreDataOperationException; +import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias; +import org.onap.oom.truststoremerger.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(TestCertificateProvider.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/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGeneratorTest.java index f05cbc86..4ff3e750 100644 --- a/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/certification/file/provider/entry/PemAliasGeneratorTest.java +++ b/trustStoreMerger/src/test/java/org/onap/oom/truststoremerger/merger/model/certificate/PemAliasGeneratorTest.java @@ -17,7 +17,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.oom.truststoremerger.certification.file.provider.entry; +package org.onap.oom.truststoremerger.merger.model.certificate; import static org.assertj.core.api.Assertions.assertThat; diff --git a/trustStoreMerger/src/test/resources/keystore.pass b/trustStoreMerger/src/test/resources/keystore.pass new file mode 100644 index 00000000..665ff8e6 --- /dev/null +++ b/trustStoreMerger/src/test/resources/keystore.pass @@ -0,0 +1 @@ +Foh49MJNYI7S_pEzE9gvUDSu
\ No newline at end of file |