443f5627b86677024f43bda6f07d307a30cc66ff
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / aaf / certservice / client / certification / writer / CertFileWriterTest.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.writer;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.util.List;
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException;
34
35 class CertFileWriterTest {
36
37     private static final String RESOURCES_PATH = "src/test/resources";
38     private static final String OUTPUT_PATH = RESOURCES_PATH + "/generatedFiles/";
39     private static final String TRUSTSTORE_P12 = "truststore.p12";
40     private static final String ERROR_MESSAGE = "java.io.FileNotFoundException: src/test/resources/generatedFiles/thisPathDoesNotExist/truststore.p12 (No such file or directory)";
41
42     private File outputDirectory = new File(OUTPUT_PATH);
43
44     @BeforeEach
45     void createDirectory() {
46         outputDirectory.mkdir();
47     }
48
49     @AfterEach
50     void cleanUpFiles() {
51         List.of(outputDirectory.listFiles()).forEach(f -> f.delete());
52         outputDirectory.delete();
53     }
54
55     @Test
56     void certFileWriterShouldCreateFilesWithDataInGivenLocation()
57         throws IOException, CertFileWriterException {
58         // given
59         final byte[] data = new byte[]{-128, 1, 2, 3, 127};
60         File truststore = new File(OUTPUT_PATH + TRUSTSTORE_P12);
61         CertFileWriter certFileWriter = new CertFileWriter(OUTPUT_PATH);
62
63         // when
64         certFileWriter.saveData(data, TRUSTSTORE_P12);
65
66         // then
67         assertThat(truststore.exists()).isTrue();
68         assertThat(Files.readAllBytes(Path.of(OUTPUT_PATH + TRUSTSTORE_P12))).isEqualTo(data);
69     }
70
71     @Test
72     void certFileWriterShouldThrowPemToPKCS12ConverterExceptionWhenOutputDirectoryDoesNotExist() {
73         // given
74         final byte[] data = new byte[]{-128, 1, 2, 3, 0};
75         CertFileWriter certFileWriter = new CertFileWriter(OUTPUT_PATH + "thisPathDoesNotExist/");
76
77         // when then
78         assertThatThrownBy(() -> certFileWriter.saveData(data, TRUSTSTORE_P12))
79             .isInstanceOf(CertFileWriterException.class).hasMessage(ERROR_MESSAGE);
80     }
81 }