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 static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
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;
35 class CertFileWriterTest {
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)";
42 private File outputDirectory = new File(OUTPUT_PATH);
45 void createDirectory() {
46 outputDirectory.mkdir();
51 List.of(outputDirectory.listFiles()).forEach(f -> f.delete());
52 outputDirectory.delete();
56 void certFileWriterShouldCreateFilesWithDataInGivenLocation()
57 throws IOException, CertFileWriterException {
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);
64 certFileWriter.saveData(data, TRUSTSTORE_P12);
67 assertThat(truststore.exists()).isTrue();
68 assertThat(Files.readAllBytes(Path.of(OUTPUT_PATH + TRUSTSTORE_P12))).isEqualTo(data);
72 void certFileWriterShouldThrowPemToPKCS12ConverterExceptionWhenOutputDirectoryDoesNotExist() {
74 final byte[] data = new byte[]{-128, 1, 2, 3, 0};
75 CertFileWriter certFileWriter = new CertFileWriter(OUTPUT_PATH + "thisPathDoesNotExist/");
78 assertThatThrownBy(() -> certFileWriter.saveData(data, TRUSTSTORE_P12))
79 .isInstanceOf(CertFileWriterException.class).hasMessage(ERROR_MESSAGE);