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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aaf.certservice.client.certification.writer;
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;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.util.List;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.api.Assertions.assertThatThrownBy;
36 class CertFileWriterTest {
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)";
43 private File outputDirectory = new File(OUTPUT_PATH);
46 void createDirectory() {
47 outputDirectory.mkdir();
52 List.of(outputDirectory.listFiles()).forEach(f -> f.delete());
53 outputDirectory.delete();
57 void certFileWriterShouldCreateFilesWithDataInGivenLocation()
58 throws IOException, CertFileWriterException {
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);
65 certFileWriter.saveData(data, TRUSTSTORE_P12);
68 assertThat(truststore.exists()).isTrue();
69 assertThat(Files.readAllBytes(Path.of(OUTPUT_PATH + TRUSTSTORE_P12))).isEqualTo(data);
73 void certFileWriterShouldThrowPemToPKCS12ConverterExceptionWhenOutputDirectoryDoesNotExist() {
75 final byte[] data = new byte[]{-128, 1, 2, 3, 0};
76 CertFileWriter certFileWriter = new CertFileWriter(OUTPUT_PATH + "thisPathDoesNotExist/");
79 assertThatThrownBy(() -> certFileWriter.saveData(data, TRUSTSTORE_P12))
80 .isInstanceOf(CertFileWriterException.class).hasMessage(ERROR_MESSAGE);