Move ArtifcatsCreationProvider one level higher
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / aaf / certservice / client / certification / conversion / ConvertedArtifactsCreatorTest.java
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
18  */
19
20 package org.onap.aaf.certservice.client.certification.conversion;
21
22 import org.junit.jupiter.api.BeforeEach;
23 import org.junit.jupiter.api.Test;
24 import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException;
25 import org.onap.aaf.certservice.client.certification.exception.PemConversionException;
26 import org.onap.aaf.certservice.client.certification.writer.CertFileWriter;
27
28 import java.security.PrivateKey;
29 import java.util.List;
30
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.times;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35
36 class ConvertedArtifactsCreatorTest {
37
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-";
41
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";
48
49     private CertFileWriter certFileWriter;
50     private RandomPasswordGenerator passwordGenerator;
51     private PemConverter converter;
52     private PrivateKey privateKey;
53     private ConvertedArtifactsCreator artifactsCreator;
54
55
56     @BeforeEach
57     void setUp() {
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);
63     }
64
65     @Test
66     void convertedArtifactCreatorShouldTryCreateFileWithGivenExtension()
67             throws CertFileWriterException, PemConversionException {
68         //given
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);
74
75         //when
76         artifactsCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
77
78         //then
79         verify(certFileWriter, times(1))
80                 .saveData(SAMPLE_KEYSTORE_BYTES, keystoreFileName);
81     }
82
83     @Test
84     void convertedArtifactsCreatorShouldCallConverterAndFilesCreatorMethods()
85             throws PemConversionException, CertFileWriterException {
86         // given
87         mockPasswordGeneratorAndPemConverter();
88         final String keystoreP12 = "keystore.p12";
89         final String keystorePass = "keystore.pass";
90
91         //when
92         artifactsCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
93
94         // then
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);
103     }
104
105     @Test
106     void convertedArtifactsCreatorShouldCallPasswordGeneratorTwice()
107             throws PemConversionException, CertFileWriterException {
108         // given
109         mockPasswordGeneratorAndPemConverter();
110
111         //when
112         artifactsCreator.create(SAMPLE_KEYSTORE_CERTIFICATE_CHAIN, SAMPLE_TRUSTED_CERTIFICATE_CHAIN, privateKey);
113
114         // then
115         verify(passwordGenerator, times(2)).generate(PASSWORD_LENGTH);
116     }
117
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);
124     }
125 }