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
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.oom.certservice.client.certification.writer;
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;
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 NOT_EXISTING_OUTPUT_PATH = OUTPUT_PATH + "directoryDoesNotExist/";
41 private static final String TRUSTSTORE_P12 = "truststore.p12";
42 private File outputDirectory = new File(OUTPUT_PATH);
46 deleteDirectoryRecursive(outputDirectory);
50 @ValueSource(strings = {OUTPUT_PATH, NOT_EXISTING_OUTPUT_PATH})
51 void certFileWriterShouldCreateFilesWithDataInGivenLocation(String outputPath)
52 throws IOException, CertFileWriterException {
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};
59 certFileWriter.saveData(data, TRUSTSTORE_P12);
62 assertThat(truststore.exists()).isTrue();
63 assertThat(Files.readAllBytes(Path.of(outputPath + TRUSTSTORE_P12))).isEqualTo(data);
66 private void deleteDirectoryRecursive(File dirForDeletion) {
67 List.of(dirForDeletion.listFiles()).forEach(file -> {
68 if (file.isDirectory()) {
69 deleteDirectoryRecursive(file);
73 dirForDeletion.delete();