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