Fix sonar issue Password
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / aaf / certservice / client / certification / conversion / KeystoreTruststoreCreatorTest.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 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;
26
27 import java.security.PrivateKey;
28 import java.util.List;
29 import org.junit.jupiter.api.Test;
30 import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException;
31
32 class KeystoreTruststoreCreatorTest {
33
34     private PKCS12FilesCreator filesCreator = mock(PKCS12FilesCreator.class);
35     private RandomPasswordGenerator passwordGenerator = mock(RandomPasswordGenerator.class);
36     private PemToPKCS12Converter converter = mock(PemToPKCS12Converter.class);
37     private PrivateKey privateKey = mock(PrivateKey.class);
38
39     @Test
40     void createKeystoreShouldCallRequiredMethods() throws PemToPKCS12ConverterException {
41         // given
42         final Password password = new Password("d9D_u8LooYaXH4G48DtN#vw0");
43         final List<String> certificates = List.of("a", "b");
44         final int passwordLength = 24;
45         final String alias = "certificate";
46         final byte[] keystoreBytes = "this is a keystore test".getBytes();
47         KeystoreTruststoreCreator creator = new KeystoreTruststoreCreator(filesCreator, passwordGenerator, converter);
48
49         // when
50         when(passwordGenerator.generate(passwordLength)).thenReturn(password);
51         when(converter.convertKeystore(certificates, password, alias, privateKey)).thenReturn(keystoreBytes);
52         creator.createKeystore(certificates, privateKey);
53
54         // then
55         verify(passwordGenerator, times(1)).generate(passwordLength);
56         verify(converter, times(1)).convertKeystore(certificates, password, alias, privateKey);
57         verify(filesCreator, times(1)).saveKeystoreData(keystoreBytes, password.getCurrentPassword());
58     }
59
60     @Test
61     void createTruststoreShouldCallRequiredMethods() throws PemToPKCS12ConverterException {
62         // given
63         final Password password = new Password("d9D_u8LooYaXH4G48DtN#vw0");
64         final List<String> certificates = List.of("a", "b");
65         final int passwordLength = 24;
66         final String alias = "trusted-certificate-";
67         final byte[] truststoreBytes = "this is a truststore test".getBytes();
68         KeystoreTruststoreCreator creator = new KeystoreTruststoreCreator(filesCreator, passwordGenerator, converter);
69
70         // when
71         when(passwordGenerator.generate(passwordLength)).thenReturn(password);
72         when(converter.convertTruststore(certificates, password, alias)).thenReturn(truststoreBytes);
73         creator.createTruststore(certificates);
74
75         // then
76         verify(passwordGenerator, times(1)).generate(passwordLength);
77         verify(converter, times(1)).convertTruststore(certificates, password, alias);
78         verify(filesCreator, times(1)).saveTruststoreData(truststoreBytes, password.getCurrentPassword());
79     }
80 }