Automation adds INFO.yaml
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / oom / certservice / client / certification / writer / CertFileWriterTest.java
1 /*============LICENSE_START=======================================================
2  * oom-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.oom.certservice.client.certification.writer;
21
22 import org.junit.jupiter.api.AfterEach;
23 import org.junit.jupiter.params.ParameterizedTest;
24 import org.junit.jupiter.params.provider.ValueSource;
25 import org.onap.oom.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 NOT_EXISTING_OUTPUT_PATH = OUTPUT_PATH + "directoryDoesNotExist/";
41     private static final String TRUSTSTORE_P12 = "truststore.p12";
42     private File outputDirectory = new File(OUTPUT_PATH);
43
44     @AfterEach
45     void cleanUpFiles() {
46         deleteDirectoryRecursive(outputDirectory);
47     }
48
49     @ParameterizedTest
50     @ValueSource(strings = {OUTPUT_PATH, NOT_EXISTING_OUTPUT_PATH})
51     void certFileWriterShouldCreateFilesWithDataInGivenLocation(String outputPath)
52             throws IOException, CertFileWriterException {
53         // given
54         File truststore = new File(outputPath + TRUSTSTORE_P12);
55         CertFileWriter certFileWriter = CertFileWriter.createWithDir(outputPath);
56         final byte[] data = new byte[]{-128, 1, 2, 3, 127};
57
58         // when
59         certFileWriter.saveData(data, TRUSTSTORE_P12);
60
61         // then
62         assertThat(truststore.exists()).isTrue();
63         assertThat(Files.readAllBytes(Path.of(outputPath + TRUSTSTORE_P12))).isEqualTo(data);
64     }
65
66     private void deleteDirectoryRecursive(File dirForDeletion) {
67         List.of(dirForDeletion.listFiles()).forEach(file -> {
68             if (file.isDirectory()) {
69                 deleteDirectoryRecursive(file);
70             }
71             file.delete();
72         });
73         dirForDeletion.delete();
74     }
75
76 }