Move ArtifcatsCreationProvider one level higher
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / aaf / certservice / client / certification / conversion / PemArtifactsCreatorTest.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.Test;
23 import org.onap.aaf.certservice.client.api.ExitableException;
24 import org.onap.aaf.certservice.client.certification.PrivateKeyToPemEncoder;
25 import org.onap.aaf.certservice.client.certification.writer.CertFileWriter;
26
27 import java.security.PrivateKey;
28 import java.util.List;
29
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34
35 class PemArtifactsCreatorTest {
36     private static final String KEYSTORE_PEM = "keystore.pem";
37     private static final String TRUSTSTORE_PEM = "truststore.pem";
38     private static final String KEY_PEM = "key.pem";
39     private static final String KEY = "my private key";
40     private CertFileWriter certFileWriter = mock(CertFileWriter.class);
41     private PrivateKey privateKey = mock(PrivateKey.class);
42     private PrivateKeyToPemEncoder pkEncoder = mock(PrivateKeyToPemEncoder.class);
43
44     @Test
45     void pemArtifactsCreatorShouldCallRequiredMethods() throws ExitableException {
46         // given
47         final PemArtifactsCreator creator = new PemArtifactsCreator(certFileWriter, pkEncoder);
48
49         // when
50         when(pkEncoder.encodePrivateKeyToPem(privateKey)).thenReturn(KEY);
51         creator.create(List.of("one", "two"), List.of("three", "four"), privateKey);
52
53         // then
54         verify(certFileWriter, times(1)).saveData("one\ntwo".getBytes(), KEYSTORE_PEM);
55         verify(certFileWriter, times(1)).saveData("three\nfour".getBytes(), TRUSTSTORE_PEM);
56         verify(certFileWriter, times(1)).saveData(KEY.getBytes(), KEY_PEM);
57     }
58 }