aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Wrobel <tomasz.wrobel@nokia.com>2020-06-09 15:37:46 +0200
committerTomasz Wrobel <tomasz.wrobel@nokia.com>2020-06-16 14:37:56 +0200
commitb288b7ab24f33af72e9c0fedecbb9979d1b4afc7 (patch)
tree1344fef298151973e7d2626f79571f63d6a7cca2
parent1a890623eff92322a033943ba8e46d2e33b9c2c5 (diff)
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 <tomasz.wrobel@nokia.com> Change-Id: If2b2fa50d551e72f19319d781bfb6079d07c7b83
-rw-r--r--certServiceClient/README.md2
-rw-r--r--certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java16
-rw-r--r--certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java3
-rw-r--r--certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreator.java (renamed from certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreatorFactory.java)18
-rw-r--r--certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProvider.java66
-rw-r--r--certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreator.java (renamed from certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreator.java)16
-rw-r--r--certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CertOutputTypeNotSupportedException.java35
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProviderTest.java52
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreatorTest.java80
-rw-r--r--certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreatorTest.java100
10 files changed, 283 insertions, 105 deletions
diff --git a/certServiceClient/README.md b/certServiceClient/README.md
index 849db4f1..5a1d2ad7 100644
--- a/certServiceClient/README.md
+++ b/certServiceClient/README.md
@@ -71,3 +71,5 @@ docker logs aaf-certservice-client
7 Fail in PKCS12 conversion
8 Fail in Private Key to PEM Encoding
9 Wrong TLS configuration
+10 Invalid value of the OUTPUT_TYPE parameter
+11 Certificate creation type is not supported
diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java
index 1b5b8ee3..27e8a4f0 100644
--- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java
+++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/CertServiceClient.java
@@ -23,8 +23,7 @@ import org.onap.aaf.certservice.client.api.ExitableException;
import org.onap.aaf.certservice.client.certification.CsrFactory;
import org.onap.aaf.certservice.client.certification.KeyPairFactory;
import org.onap.aaf.certservice.client.certification.PrivateKeyToPemEncoder;
-import org.onap.aaf.certservice.client.certification.conversion.KeystoreTruststoreCreator;
-import org.onap.aaf.certservice.client.certification.conversion.KeystoreTruststoreCreatorFactory;
+import org.onap.aaf.certservice.client.certification.conversion.ArtifactsCreatorProvider;
import org.onap.aaf.certservice.client.common.Base64Encoder;
import org.onap.aaf.certservice.client.configuration.EnvsForClient;
import org.onap.aaf.certservice.client.configuration.EnvsForCsr;
@@ -78,12 +77,15 @@ public class CertServiceClient {
base64Encoder.encode(csrFactory.createCsrInPem(keyPair)),
base64Encoder.encode(pkEncoder.encodePrivateKeyToPem(keyPair.getPrivate())));
- KeystoreTruststoreCreator filesCreator = new KeystoreTruststoreCreatorFactory(
- clientConfiguration.getCertsOutputPath()).create();
- filesCreator.createKeystore(certServiceData.getCertificateChain(), keyPair.getPrivate());
- filesCreator.createTruststore(certServiceData.getTrustedCertificates());
+ ArtifactsCreatorProvider
+ .getCreator(clientConfiguration.getOutputType(),
+ clientConfiguration.getCertsOutputPath())
+ .create(certServiceData.getCertificateChain(),
+ certServiceData.getTrustedCertificates(),
+ keyPair.getPrivate());
+
} catch (ExitableException e) {
- LOGGER.error("Cert Service Client fail in execution: ", e);
+ LOGGER.error("Cert Service Client fails in execution: ", e);
appExitHandler.exit(e.applicationExitStatus());
}
appExitHandler.exit(SUCCESS);
diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java
index 41217e76..00057829 100644
--- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java
+++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/api/ExitStatus.java
@@ -30,7 +30,8 @@ public enum ExitStatus {
PKCS12_CONVERSION_EXCEPTION(7,"Fail in PKCS12 conversion"),
PK_TO_PEM_ENCODING_EXCEPTION(8,"Fail in Private Key to PEM Encoding"),
TLS_CONFIGURATION_EXCEPTION(9, "Invalid TLS configuration"),
- OUTPUT_TYPE_PARAMETER_VALIDATION_EXCEPTION(10, "Invalid value of the OUTPUT_TYPE parameter");
+ OUTPUT_TYPE_PARAMETER_VALIDATION_EXCEPTION(10, "Invalid value of the OUTPUT_TYPE parameter"),
+ CERT_OUTPUT_TYPE_NOT_SUPPORTED_EXCEPTION(11, "Certificate creation type is not supported");
private final int value;
private final String message;
diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreatorFactory.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreator.java
index 8c719535..8907c481 100644
--- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreatorFactory.java
+++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreator.java
@@ -16,20 +16,14 @@
* limitations under the License.
* ============LICENSE_END=========================================================
*/
-
package org.onap.aaf.certservice.client.certification.conversion;
-public class KeystoreTruststoreCreatorFactory {
- private final String outputPath;
+import org.onap.aaf.certservice.client.api.ExitableException;
- public KeystoreTruststoreCreatorFactory(String outputPath) {
- this.outputPath = outputPath;
- }
+import java.security.PrivateKey;
+import java.util.List;
- public KeystoreTruststoreCreator create() {
- return new KeystoreTruststoreCreator(
- new PKCS12FilesCreator(outputPath),
- new RandomPasswordGenerator(),
- new PemToPKCS12Converter());
- }
+public interface ArtifactsCreator {
+ void create(List<String> keystoreData, List<String> truststoreData, PrivateKey privateKey)
+ throws ExitableException;
}
diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProvider.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProvider.java
new file mode 100644
index 00000000..6fbf373b
--- /dev/null
+++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/ArtifactsCreatorProvider.java
@@ -0,0 +1,66 @@
+/*============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.onap.aaf.certservice.client.certification.exception.CertOutputTypeNotSupportedException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+
+public enum ArtifactsCreatorProvider {
+
+ P12 {
+ @Override
+ ArtifactsCreator create(String outputPath) {
+ return new PKCS12ArtifactsCreator(
+ new PKCS12FilesCreator(outputPath),
+ new RandomPasswordGenerator(),
+ new PemToPKCS12Converter());
+ }
+ },
+ JKS {
+ @Override
+ ArtifactsCreator create(String outputPath) {
+ return null;
+ }
+ },
+ PEM {
+ @Override
+ ArtifactsCreator create(String outputPath) {
+ return null;
+ }
+ };
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ArtifactsCreatorProvider.class);
+
+ public static ArtifactsCreator getCreator(String outputType, String outputPath)
+ throws CertOutputTypeNotSupportedException {
+ try {
+ LOGGER.info("Artifact creation type selected: {}", outputType);
+ return valueOf(outputType).create(outputPath);
+ } catch (IllegalArgumentException e) {
+ LOGGER.error("Artifact creation type: {} is not supported. Supported types: {}",
+ outputType, Arrays.toString(values()));
+ throw new CertOutputTypeNotSupportedException(e);
+ }
+ }
+
+ abstract ArtifactsCreator create(String outputPath);
+}
diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreator.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreator.java
index 43784609..c07dfd11 100644
--- a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/KeystoreTruststoreCreator.java
+++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/conversion/PKCS12ArtifactsCreator.java
@@ -23,7 +23,7 @@ import java.security.PrivateKey;
import java.util.List;
import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException;
-public class KeystoreTruststoreCreator {
+public class PKCS12ArtifactsCreator implements ArtifactsCreator {
private static final String CERTIFICATE_ALIAS = "certificate";
private static final String TRUSTED_CERTIFICATE_ALIAS = "trusted-certificate-";
@@ -32,21 +32,27 @@ public class KeystoreTruststoreCreator {
private final PemToPKCS12Converter converter;
private final PKCS12FilesCreator creator;
- public KeystoreTruststoreCreator(PKCS12FilesCreator creator, RandomPasswordGenerator generator,
- PemToPKCS12Converter converter) {
+ public PKCS12ArtifactsCreator(PKCS12FilesCreator creator, RandomPasswordGenerator generator,
+ PemToPKCS12Converter converter) {
this.generator = generator;
this.converter = converter;
this.creator = creator;
}
- public void createKeystore(List<String> data, PrivateKey privateKey)
+ @Override
+ public void create(List<String> keystoreData, List<String> truststoreData, PrivateKey privateKey) throws PemToPKCS12ConverterException {
+ createKeystore(keystoreData,privateKey);
+ createTruststore(truststoreData);
+ }
+
+ private void createKeystore(List<String> data, PrivateKey privateKey)
throws PemToPKCS12ConverterException {
Password password = generator.generate(PASSWORD_LENGTH);
creator.saveKeystoreData(converter.convertKeystore(data, password, CERTIFICATE_ALIAS, privateKey),
password.getCurrentPassword());
}
- public void createTruststore(List<String> data)
+ private void createTruststore(List<String> data)
throws PemToPKCS12ConverterException {
Password password = generator.generate(PASSWORD_LENGTH);
creator.saveTruststoreData(converter.convertTruststore(data, password, TRUSTED_CERTIFICATE_ALIAS),
diff --git a/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CertOutputTypeNotSupportedException.java b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CertOutputTypeNotSupportedException.java
new file mode 100644
index 00000000..3c9581ac
--- /dev/null
+++ b/certServiceClient/src/main/java/org/onap/aaf/certservice/client/certification/exception/CertOutputTypeNotSupportedException.java
@@ -0,0 +1,35 @@
+/*============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.exception;
+
+import org.onap.aaf.certservice.client.api.ExitStatus;
+import org.onap.aaf.certservice.client.api.ExitableException;
+
+public class CertOutputTypeNotSupportedException extends ExitableException {
+ private static final ExitStatus EXIT_STATUS = ExitStatus.CERT_OUTPUT_TYPE_NOT_SUPPORTED_EXCEPTION;
+
+ public CertOutputTypeNotSupportedException(Throwable e) {
+ super(e);
+ }
+
+ public ExitStatus applicationExitStatus() {
+ return EXIT_STATUS;
+ }
+}
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<String> 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<String> 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<String> SAMPLE_KEYSTORE_CERTIFICATE_CHAIN = List.of("a", "b");
+ private static final List<String> 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);
+ }
+}