Merge "Top up of certServiceClient version"
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / aaf / certservice / client / certification / conversion / PKCS12FilesCreatorTest.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.assertj.core.api.Assertions.assertThatThrownBy;
23 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.nio.charset.StandardCharsets;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.util.List;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException;
37
38 class PKCS12FilesCreatorTest {
39
40     private static final String RESOURCES_PATH = "src/test/resources";
41     private static final String OUTPUT_PATH = RESOURCES_PATH + "/generatedFiles/";
42     private static final String KEYSTORE_PATH = OUTPUT_PATH + "keystore.jks";
43     private static final String KEYSTORE_PASS_PATH = OUTPUT_PATH + "keystore.pass";
44     private static final String TRUSTSTORE_PATH = OUTPUT_PATH + "truststore.jks";
45     private static final String TRUSTSTORE_PASS_PATH = OUTPUT_PATH + "truststore.pass";
46     private static final String ERROR_MESSAGE = "java.io.FileNotFoundException: src/test/resources/generatedFiles/thisPathDoesNotExist/keystore.jks (No such file or directory)";
47
48     private File outputDirectory = new File(OUTPUT_PATH);
49
50     @BeforeEach
51     void createDirectory() {
52         outputDirectory.mkdir();
53     }
54
55     @AfterEach
56     void cleanUpFiles() {
57         List.of(outputDirectory.listFiles()).forEach(f -> f.delete());
58         outputDirectory.delete();
59     }
60
61     @Test
62     void saveKeystoreDataShouldCreateFilesWithDataInGivenLocation() throws PemToPKCS12ConverterException, IOException {
63         // given
64         final byte[] data = new byte[]{-128, 1, 127};
65         final String password = "onap123";
66         File keystore = new File(KEYSTORE_PATH);
67         File keystorePass = new File(KEYSTORE_PASS_PATH);
68         PKCS12FilesCreator filesCreator = new PKCS12FilesCreator(OUTPUT_PATH);
69
70         // when
71         filesCreator.saveKeystoreData(data, password);
72
73         // then
74         assertTrue(keystore.exists());
75         assertTrue(keystorePass.exists());
76         assertArrayEquals(data, Files.readAllBytes(Path.of(KEYSTORE_PATH)));
77         assertEquals(password, Files.readString(Path.of(KEYSTORE_PASS_PATH), StandardCharsets.UTF_8));
78     }
79
80     @Test
81     void saveTruststoreDataShouldCreateFilesWithDataInGivenLocation()
82         throws PemToPKCS12ConverterException, IOException {
83         // given
84         final byte[] data = new byte[]{-128, 1, 2, 3, 127};
85         final String password = "nokia321";
86         File truststore = new File(TRUSTSTORE_PATH);
87         File truststorePass = new File(TRUSTSTORE_PASS_PATH);
88         PKCS12FilesCreator filesCreator = new PKCS12FilesCreator(OUTPUT_PATH);
89
90         // when
91         filesCreator.saveTruststoreData(data, password);
92
93         // then
94         assertTrue(truststore.exists());
95         assertTrue(truststorePass.exists());
96         assertArrayEquals(data, Files.readAllBytes(Path.of(TRUSTSTORE_PATH)));
97         assertEquals(password, Files.readString(Path.of(TRUSTSTORE_PASS_PATH), StandardCharsets.UTF_8));
98     }
99
100     @Test
101     void saveKeystoreDataShouldThrowPemToPKCS12ConverterExceptionWhenOutputDirectoryDoesNotExist() {
102         // given
103         final byte[] data = new byte[]{-128, 1, 2, 3, 0};
104         final String password = "123aikon";
105         PKCS12FilesCreator filesCreator = new PKCS12FilesCreator(OUTPUT_PATH + "thisPathDoesNotExist/");
106
107         // when then
108         assertThatThrownBy(() -> filesCreator.saveKeystoreData(data, password))
109             .isInstanceOf(PemToPKCS12ConverterException.class).hasMessage(ERROR_MESSAGE);
110     }
111 }