diff options
author | 2020-06-17 10:48:20 +0200 | |
---|---|---|
committer | 2020-06-18 12:39:07 +0200 | |
commit | 212038b654728b79aa647e08da2562484c63c883 (patch) | |
tree | 56d0ddea09c5bcea18553b9cd0c3773f9270564d /certServiceClient/src/test/java/org/onap/aaf/certservice | |
parent | 04d9cae7bf2a54a8bb05cb36ca54d4555987903e (diff) |
Add PEM artifacts creation
with unit tests
Issue-ID: AAF-1152
Change-Id: I95afd62330f3111f916507d628d142262ff951cc
Signed-off-by: Joanna Jeremicz <joanna.jeremicz@nokia.com>
Diffstat (limited to 'certServiceClient/src/test/java/org/onap/aaf/certservice')
5 files changed, 167 insertions, 123 deletions
diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java index be00003b..133d90d2 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java @@ -26,16 +26,27 @@ import static org.assertj.core.api.Assertions.assertThat; class ArtifactsCreatorProviderTest { - private static final String STRATEGY_P12 = "P12"; + private static final String P12 = "P12"; + private static final String PEM = "PEM"; private static final String TEST_PATH = "testPath"; @Test - void getStrategyOfStringShouldReturnCorrectCreator(){ + void artifactsProviderShouldReturnP12Creator(){ // when ArtifactsCreator artifactsCreator = - ArtifactsCreatorProvider.getCreator(STRATEGY_P12, TEST_PATH); + ArtifactsCreatorProvider.getCreator(P12, TEST_PATH); // then assertThat(artifactsCreator).isInstanceOf(PKCS12ArtifactsCreator.class); } + + @Test + void artifactsProviderShouldReturnPemCreator(){ + + // when + ArtifactsCreator artifactsCreator = + ArtifactsCreatorProvider.getCreator(PEM, TEST_PATH); + // then + assertThat(artifactsCreator).isInstanceOf(PemArtifactsCreator.class); + } } diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java index 13ac0a6e..4a690e5f 100644 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java @@ -29,7 +29,9 @@ import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException; import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException; +import org.onap.aaf.certservice.client.certification.writer.CertFileWriter; class PKCS12ArtifactsCreatorTest { @@ -43,7 +45,7 @@ class PKCS12ArtifactsCreatorTest { private static final byte[] SAMPLE_KEYSTORE_BYTES = "this is a keystore test".getBytes(); private static final byte[] SAMPLE_TRUSTSTORE_BYTES = "this is a truststore test".getBytes(); - private PKCS12FilesCreator filesCreator; + private CertFileWriter certFileWriter; private RandomPasswordGenerator passwordGenerator; private PemToPKCS12Converter converter; private PrivateKey privateKey; @@ -52,17 +54,20 @@ class PKCS12ArtifactsCreatorTest { @BeforeEach void setUp() { - filesCreator = mock(PKCS12FilesCreator.class); + certFileWriter = mock(CertFileWriter.class); passwordGenerator = mock(RandomPasswordGenerator.class); converter = mock(PemToPKCS12Converter.class); privateKey = mock(PrivateKey.class); - artifactCreator = new PKCS12ArtifactsCreator(filesCreator, passwordGenerator, converter); + artifactCreator = new PKCS12ArtifactsCreator(certFileWriter, passwordGenerator, converter); } @Test - void generateArtifactsShouldCallConverterAndFilesCreatorMethods() throws PemToPKCS12ConverterException { + void artifactsCreatorShouldCauseCallOfConvertAndDataSaveMethods() + throws PemToPKCS12ConverterException, CertFileWriterException { // given mockPasswordGeneratorAndPKSC12Converter(); + final String keystoreP12 = "keystore.p12"; + final String keystorePass = "keystore.pass"; //when artifactCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey); @@ -70,16 +75,17 @@ class PKCS12ArtifactsCreatorTest { // then verify(converter, times(1)) .convertKeystore(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, CERTIFICATE_ALIAS, privateKey); - verify(filesCreator, times(1)) - .saveKeystoreData(SAMPLE_KEYSTORE_BYTES, SAMPLE_PASSWORD.getCurrentPassword()); + verify(certFileWriter, times(1)) + .saveData(SAMPLE_KEYSTORE_BYTES, keystoreP12); + verify(certFileWriter, times(1)) + .saveData(SAMPLE_PASSWORD.getCurrentPassword().getBytes(), keystorePass); verify(converter, times(1)) .convertTruststore(SAMPLE_TRUSTED_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, TRUSTED_CERTIFICATE_ALIAS); - verify(filesCreator, times(1)) - .saveTruststoreData(SAMPLE_TRUSTSTORE_BYTES, SAMPLE_PASSWORD.getCurrentPassword()); } @Test - void generateArtifactsMethodShouldCallPasswordGeneratorTwice() throws PemToPKCS12ConverterException { + void artifactsCreatorShouldCallPasswordGeneratorTwice() + throws PemToPKCS12ConverterException, CertFileWriterException { // given mockPasswordGeneratorAndPKSC12Converter(); diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreatorTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreatorTest.java deleted file mode 100644 index 8e6e03c6..00000000 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12FilesCreatorTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/*============LICENSE_START======================================================= - * aaf-certservice-client - * ================================================================================ - * 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.aaf.certservice.client.certification.conversion; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException; - -class PKCS12FilesCreatorTest { - - private static final String RESOURCES_PATH = "src/test/resources"; - private static final String OUTPUT_PATH = RESOURCES_PATH + "/generatedFiles/"; - private static final String KEYSTORE_PATH = OUTPUT_PATH + "keystore.jks"; - private static final String KEYSTORE_PASS_PATH = OUTPUT_PATH + "keystore.pass"; - private static final String TRUSTSTORE_PATH = OUTPUT_PATH + "truststore.jks"; - private static final String TRUSTSTORE_PASS_PATH = OUTPUT_PATH + "truststore.pass"; - private static final String ERROR_MESSAGE = "java.io.FileNotFoundException: src/test/resources/generatedFiles/thisPathDoesNotExist/keystore.jks (No such file or directory)"; - - private File outputDirectory = new File(OUTPUT_PATH); - - @BeforeEach - void createDirectory() { - outputDirectory.mkdir(); - } - - @AfterEach - void cleanUpFiles() { - List.of(outputDirectory.listFiles()).forEach(f -> f.delete()); - outputDirectory.delete(); - } - - @Test - void saveKeystoreDataShouldCreateFilesWithDataInGivenLocation() throws PemToPKCS12ConverterException, IOException { - // given - final byte[] data = new byte[]{-128, 1, 127}; - final String password = "onap123"; - File keystore = new File(KEYSTORE_PATH); - File keystorePass = new File(KEYSTORE_PASS_PATH); - PKCS12FilesCreator filesCreator = new PKCS12FilesCreator(OUTPUT_PATH); - - // when - filesCreator.saveKeystoreData(data, password); - - // then - assertTrue(keystore.exists()); - assertTrue(keystorePass.exists()); - assertArrayEquals(data, Files.readAllBytes(Path.of(KEYSTORE_PATH))); - assertEquals(password, Files.readString(Path.of(KEYSTORE_PASS_PATH), StandardCharsets.UTF_8)); - } - - @Test - void saveTruststoreDataShouldCreateFilesWithDataInGivenLocation() - throws PemToPKCS12ConverterException, IOException { - // given - final byte[] data = new byte[]{-128, 1, 2, 3, 127}; - final String password = "nokia321"; - File truststore = new File(TRUSTSTORE_PATH); - File truststorePass = new File(TRUSTSTORE_PASS_PATH); - PKCS12FilesCreator filesCreator = new PKCS12FilesCreator(OUTPUT_PATH); - - // when - filesCreator.saveTruststoreData(data, password); - - // then - assertTrue(truststore.exists()); - assertTrue(truststorePass.exists()); - assertArrayEquals(data, Files.readAllBytes(Path.of(TRUSTSTORE_PATH))); - assertEquals(password, Files.readString(Path.of(TRUSTSTORE_PASS_PATH), StandardCharsets.UTF_8)); - } - - @Test - void saveKeystoreDataShouldThrowPemToPKCS12ConverterExceptionWhenOutputDirectoryDoesNotExist() { - // given - final byte[] data = new byte[]{-128, 1, 2, 3, 0}; - final String password = "123aikon"; - PKCS12FilesCreator filesCreator = new PKCS12FilesCreator(OUTPUT_PATH + "thisPathDoesNotExist/"); - - // when then - assertThatThrownBy(() -> filesCreator.saveKeystoreData(data, password)) - .isInstanceOf(PemToPKCS12ConverterException.class).hasMessage(ERROR_MESSAGE); - } -}
\ No newline at end of file diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreatorTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreatorTest.java new file mode 100644 index 00000000..9963d245 --- /dev/null +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemArtifactsCreatorTest.java @@ -0,0 +1,57 @@ +/*============LICENSE_START======================================================= + * aaf-certservice-client + * ================================================================================ + * 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.aaf.certservice.client.certification.conversion; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.security.PrivateKey; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.onap.aaf.certservice.client.api.ExitableException; +import org.onap.aaf.certservice.client.certification.PrivateKeyToPemEncoder; +import org.onap.aaf.certservice.client.certification.writer.CertFileWriter; + +class PemArtifactsCreatorTest { + private final String KEYSTORE_PEM = "keystore.pem"; + private final String TRUSTSTORE_PEM = "truststore.pem"; + private final String KEY_PEM = "key.pem"; + private final String KEY = "my private key"; + private CertFileWriter certFileWriter = mock(CertFileWriter.class); + private PrivateKey privateKey = mock(PrivateKey.class); + private PrivateKeyToPemEncoder pkEncoder = mock(PrivateKeyToPemEncoder.class); + + @Test + void pemArtifactsCreatorShouldCallRequiredMethods() throws ExitableException { + // given + final PemArtifactsCreator creator = new PemArtifactsCreator(certFileWriter, pkEncoder); + + // when + when(pkEncoder.encodePrivateKeyToPem(privateKey)).thenReturn(KEY); + creator.create(List.of("one", "two"), List.of("three", "four"), privateKey); + + // then + verify(certFileWriter, times(1)).saveData("one\ntwo".getBytes(), KEYSTORE_PEM); + verify(certFileWriter, times(1)).saveData("three\nfour".getBytes(), TRUSTSTORE_PEM); + verify(certFileWriter, times(1)).saveData(KEY.getBytes(), KEY_PEM); + } +} diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriterTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriterTest.java new file mode 100644 index 00000000..443f5627 --- /dev/null +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/writer/CertFileWriterTest.java @@ -0,0 +1,81 @@ +/*============LICENSE_START======================================================= + * aaf-certservice-client + * ================================================================================ + * 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.aaf.certservice.client.certification.writer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException; + +class CertFileWriterTest { + + private static final String RESOURCES_PATH = "src/test/resources"; + private static final String OUTPUT_PATH = RESOURCES_PATH + "/generatedFiles/"; + private static final String TRUSTSTORE_P12 = "truststore.p12"; + private static final String ERROR_MESSAGE = "java.io.FileNotFoundException: src/test/resources/generatedFiles/thisPathDoesNotExist/truststore.p12 (No such file or directory)"; + + private File outputDirectory = new File(OUTPUT_PATH); + + @BeforeEach + void createDirectory() { + outputDirectory.mkdir(); + } + + @AfterEach + void cleanUpFiles() { + List.of(outputDirectory.listFiles()).forEach(f -> f.delete()); + outputDirectory.delete(); + } + + @Test + void certFileWriterShouldCreateFilesWithDataInGivenLocation() + throws IOException, CertFileWriterException { + // given + final byte[] data = new byte[]{-128, 1, 2, 3, 127}; + File truststore = new File(OUTPUT_PATH + TRUSTSTORE_P12); + CertFileWriter certFileWriter = new CertFileWriter(OUTPUT_PATH); + + // when + certFileWriter.saveData(data, TRUSTSTORE_P12); + + // then + assertThat(truststore.exists()).isTrue(); + assertThat(Files.readAllBytes(Path.of(OUTPUT_PATH + TRUSTSTORE_P12))).isEqualTo(data); + } + + @Test + void certFileWriterShouldThrowPemToPKCS12ConverterExceptionWhenOutputDirectoryDoesNotExist() { + // given + final byte[] data = new byte[]{-128, 1, 2, 3, 0}; + CertFileWriter certFileWriter = new CertFileWriter(OUTPUT_PATH + "thisPathDoesNotExist/"); + + // when then + assertThatThrownBy(() -> certFileWriter.saveData(data, TRUSTSTORE_P12)) + .isInstanceOf(CertFileWriterException.class).hasMessage(ERROR_MESSAGE); + } +} |