1 /*============LICENSE_START=======================================================
2 * aaf-certservice-client
3 * ================================================================================
4 * Copyright (C) 2020 Nokia. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.aaf.certservice.client.certification.conversion;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
27 import java.security.PrivateKey;
28 import java.util.List;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException;
33 import org.onap.aaf.certservice.client.certification.exception.PemConversionException;
34 import org.onap.aaf.certservice.client.certification.writer.CertFileWriter;
36 class ConvertedArtifactsCreatorTest {
38 private static final int PASSWORD_LENGTH = 24;
39 private static final String CERTIFICATE_ALIAS = "certificate";
40 private static final String TRUSTED_CERTIFICATE_ALIAS = "trusted-certificate-";
42 private static final Password SAMPLE_PASSWORD = new Password("d9D_u8LooYaXH4G48DtN#vw0");
43 private static final List<String> SAMPLE_KEYSTORE_CERTIFICATE_CHAIN = List.of("a", "b");
44 private static final List<String> SAMPLE_TRUSTED_CERTIFICATE_CHAIN = List.of("c", "d");
45 private static final byte[] SAMPLE_KEYSTORE_BYTES = "this is a keystore test".getBytes();
46 private static final byte[] SAMPLE_TRUSTSTORE_BYTES = "this is a truststore test".getBytes();
47 private static final String P12_EXTENSION= "p12";
49 private CertFileWriter certFileWriter;
50 private RandomPasswordGenerator passwordGenerator;
51 private PemConverter converter;
52 private PrivateKey privateKey;
53 private ConvertedArtifactsCreator artifactsCreator;
58 certFileWriter = mock(CertFileWriter.class);
59 passwordGenerator = mock(RandomPasswordGenerator.class);
60 converter = mock(PemConverter.class);
61 privateKey = mock(PrivateKey.class);
62 artifactsCreator = new ConvertedArtifactsCreator(certFileWriter, passwordGenerator, converter, P12_EXTENSION);
66 void convertedArtifactCreatorShouldTryCreateFileWithGivenExtension()
67 throws CertFileWriterException, PemConversionException {
69 mockPasswordGeneratorAndPemConverter();
70 final String keystore = "keystore";
71 final String testExtension = "testExt";
72 final String keystoreFileName = String.format("%s.%s", keystore, testExtension);
73 artifactsCreator = new ConvertedArtifactsCreator(certFileWriter, passwordGenerator, converter, testExtension);
76 artifactsCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
79 verify(certFileWriter, times(1))
80 .saveData(SAMPLE_KEYSTORE_BYTES, keystoreFileName);
84 void convertedArtifactsCreatorShouldCallConverterAndFilesCreatorMethods()
85 throws PemConversionException, CertFileWriterException {
87 mockPasswordGeneratorAndPemConverter();
88 final String keystoreP12 = "keystore.p12";
89 final String keystorePass = "keystore.pass";
92 artifactsCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
95 verify(converter, times(1))
96 .convertKeystore(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, CERTIFICATE_ALIAS, privateKey);
97 verify(certFileWriter, times(1))
98 .saveData(SAMPLE_KEYSTORE_BYTES, keystoreP12);
99 verify(certFileWriter, times(1))
100 .saveData(SAMPLE_PASSWORD.getCurrentPassword().getBytes(), keystorePass);
101 verify(converter, times(1))
102 .convertTruststore(SAMPLE_TRUSTED_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, TRUSTED_CERTIFICATE_ALIAS);
106 void convertedArtifactsCreatorShouldCallPasswordGeneratorTwice()
107 throws PemConversionException, CertFileWriterException {
109 mockPasswordGeneratorAndPemConverter();
112 artifactsCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
115 verify(passwordGenerator, times(2)).generate(PASSWORD_LENGTH);
118 private void mockPasswordGeneratorAndPemConverter() throws PemConversionException {
119 when(passwordGenerator.generate(PASSWORD_LENGTH)).thenReturn(SAMPLE_PASSWORD);
120 when(converter.convertKeystore(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, CERTIFICATE_ALIAS, privateKey))
121 .thenReturn(SAMPLE_KEYSTORE_BYTES);
122 when(converter.convertTruststore(SAMPLE_TRUSTED_CERTIFICATE_CHAIN, SAMPLE_PASSWORD, TRUSTED_CERTIFICATE_ALIAS))
123 .thenReturn(SAMPLE_TRUSTSTORE_BYTES);