From b288b7ab24f33af72e9c0fedecbb9979d1b4afc7 Mon Sep 17 00:00:00 2001 From: Tomasz Wrobel Date: Tue, 9 Jun 2020 15:37:46 +0200 Subject: Refactor flow of cert files generation, based on OUTPUT_TYPE parameter -Add artifacts creator provider (strategy pattern) -Refactor KeystoreTruststoreCreator -Add new exception: CertOutputTypeNotSupported -Change Unit tests Issue-ID: AAF-1152 Signed-off-by: Tomasz Wrobel Change-Id: If2b2fa50d551e72f19319d781bfb6079d07c7b83 --- .../conversion/ArtifactsCreatorProviderTest.java | 52 +++++++++++ .../conversion/KeystoreTruststoreCreatorTest.java | 80 ----------------- .../conversion/PKCS12ArtifactsCreatorTest.java | 100 +++++++++++++++++++++ 3 files changed, 152 insertions(+), 80 deletions(-) create mode 100644 certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java delete mode 100644 certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreatorTest.java create mode 100644 certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java (limited to 'certServiceClient/src/test/java/org/onap') 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 new file mode 100644 index 00000000..eb572658 --- /dev/null +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java @@ -0,0 +1,52 @@ +/*============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 org.junit.jupiter.api.Test; +import org.onap.aaf.certservice.client.certification.exception.CertOutputTypeNotSupportedException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + + +class ArtifactsCreatorProviderTest { + + private static final String STRATEGY_P12 = "P12"; + private static final String TEST_PATH = "testPath"; + private static final String NOT_SUPPORTED_STRATEGY = "notSupported"; + + @Test + void getStrategyOfStringShouldReturnCorrectCreator() throws Exception { + + // when + ArtifactsCreator artifactsCreator = + ArtifactsCreatorProvider.getCreator(STRATEGY_P12, TEST_PATH); + // then + assertThat(artifactsCreator).isInstanceOf(PKCS12ArtifactsCreator.class); + } + + @Test + void notSupportedStrategyShouldThrowException() { + // when// then + assertThatExceptionOfType(CertOutputTypeNotSupportedException.class) + .isThrownBy(() -> ArtifactsCreatorProvider.getCreator(NOT_SUPPORTED_STRATEGY, TEST_PATH)); + + } +} diff --git a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreatorTest.java b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreatorTest.java deleted file mode 100644 index 5921c316..00000000 --- a/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreatorTest.java +++ /dev/null @@ -1,80 +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.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.certification.exception.PemToPKCS12ConverterException; - -class KeystoreTruststoreCreatorTest { - - private PKCS12FilesCreator filesCreator = mock(PKCS12FilesCreator.class); - private RandomPasswordGenerator passwordGenerator = mock(RandomPasswordGenerator.class); - private PemToPKCS12Converter converter = mock(PemToPKCS12Converter.class); - private PrivateKey privateKey = mock(PrivateKey.class); - - @Test - void createKeystoreShouldCallRequiredMethods() throws PemToPKCS12ConverterException { - // given - final Password password = new Password("d9D_u8LooYaXH4G48DtN#vw0"); - final List certificates = List.of("a", "b"); - final int passwordLength = 24; - final String alias = "certificate"; - final byte[] keystoreBytes = "this is a keystore test".getBytes(); - KeystoreTruststoreCreator creator = new KeystoreTruststoreCreator(filesCreator, passwordGenerator, converter); - - // when - when(passwordGenerator.generate(passwordLength)).thenReturn(password); - when(converter.convertKeystore(certificates, password, alias, privateKey)).thenReturn(keystoreBytes); - creator.createKeystore(certificates, privateKey); - - // then - verify(passwordGenerator, times(1)).generate(passwordLength); - verify(converter, times(1)).convertKeystore(certificates, password, alias, privateKey); - verify(filesCreator, times(1)).saveKeystoreData(keystoreBytes, password.getCurrentPassword()); - } - - @Test - void createTruststoreShouldCallRequiredMethods() throws PemToPKCS12ConverterException { - // given - final Password password = new Password("d9D_u8LooYaXH4G48DtN#vw0"); - final List certificates = List.of("a", "b"); - final int passwordLength = 24; - final String alias = "trusted-certificate-"; - final byte[] truststoreBytes = "this is a truststore test".getBytes(); - KeystoreTruststoreCreator creator = new KeystoreTruststoreCreator(filesCreator, passwordGenerator, converter); - - // when - when(passwordGenerator.generate(passwordLength)).thenReturn(password); - when(converter.convertTruststore(certificates, password, alias)).thenReturn(truststoreBytes); - creator.createTruststore(certificates); - - // then - verify(passwordGenerator, times(1)).generate(passwordLength); - verify(converter, times(1)).convertTruststore(certificates, password, alias); - verify(filesCreator, times(1)).saveTruststoreData(truststoreBytes, password.getCurrentPassword()); - } -} \ No newline at end of file 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 new file mode 100644 index 00000000..13ac0a6e --- /dev/null +++ b/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java @@ -0,0 +1,100 @@ +/*============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.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException; + +class PKCS12ArtifactsCreatorTest { + + private static final int PASSWORD_LENGTH = 24; + private static final String CERTIFICATE_ALIAS = "certificate"; + private static final String TRUSTED_CERTIFICATE_ALIAS = "trusted-certificate-"; + + private static final Password SAMPLE_PASSWORD = new Password("d9D_u8LooYaXH4G48DtN#vw0"); + private static final List SAMPLE_KEYSTORE_CERTIFICATE_CHAIN = List.of("a", "b"); + private static final List SAMPLE_TRUSTED_CERTIFICATE_CHAIN = List.of("c", "d"); + 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 RandomPasswordGenerator passwordGenerator; + private PemToPKCS12Converter converter; + private PrivateKey privateKey; + private PKCS12ArtifactsCreator artifactCreator; + + + @BeforeEach + void setUp() { + filesCreator = mock(PKCS12FilesCreator.class); + passwordGenerator = mock(RandomPasswordGenerator.class); + converter = mock(PemToPKCS12Converter.class); + privateKey = mock(PrivateKey.class); + artifactCreator = new PKCS12ArtifactsCreator(filesCreator, passwordGenerator, converter); + } + + @Test + void generateArtifactsShouldCallConverterAndFilesCreatorMethods() throws PemToPKCS12ConverterException { + // given + mockPasswordGeneratorAndPKSC12Converter(); + + //when + artifactCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey); + + // 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(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 { + // given + mockPasswordGeneratorAndPKSC12Converter(); + + //when + artifactCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey); + + // then + verify(passwordGenerator, times(2)).generate(PASSWORD_LENGTH); + } + + private void mockPasswordGeneratorAndPKSC12Converter() throws PemToPKCS12ConverterException { + when(passwordGenerator.generate(PASSWORD_LENGTH)).thenReturn(SAMPLE_PASSWORD); + when(converter.convertKeystore(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, CERTIFICATE_ALIAS, privateKey)) + .thenReturn(SAMPLE_KEYSTORE_BYTES); + when(converter.convertTruststore(SAMPLE_TRUSTED_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, TRUSTED_CERTIFICATE_ALIAS)) + .thenReturn(SAMPLE_TRUSTSTORE_BYTES); + } +} -- cgit 1.2.3-korg