1 /*============LICENSE_START=======================================================
2 * oom-truststore-merger
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.postprocessor.copier;
23 import java.io.IOException;
24 import java.nio.charset.Charset;
25 import java.util.Collections;
26 import org.apache.commons.io.FileUtils;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.io.TempDir;
29 import org.onap.oom.certservice.postprocessor.common.FileTools;
30 import org.onap.oom.certservice.postprocessor.configuration.model.AppConfiguration;
31 import org.onap.oom.certservice.postprocessor.copier.exception.KeystoreFileCopyException;
32 import org.onap.oom.certservice.postprocessor.copier.exception.KeystoreNotExistException;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
37 public class KeystoreCopierTest {
39 private static final String SOURCE_CONTENT = "source content";
40 private static final String DESTINATION_CONTENT = "destination content";
45 private KeystoreCopier copier = new KeystoreCopier(new FileTools());
48 void shouldDoNothingForEmptySourceFileList() {
49 AppConfiguration configuration = createEmptyConfiguration();
51 copier.copyKeystores(configuration);
53 assertThat(dir.listFiles()).isEmpty();
58 void shouldCopyFileAndCreateBackup() throws IOException {
59 File source = createFile("source.p12", SOURCE_CONTENT);
60 File destination = createFile("destination.p12", DESTINATION_CONTENT);
61 File backup = declareFile("destination.p12.bak");
62 AppConfiguration configuration = createConfiguration(source, destination);
64 copier.copyKeystores(configuration);
66 assertThat(readFile(destination)).isEqualTo(readFile(source));
67 assertThat(backup.exists()).isTrue();
68 assertThat(readFile(backup)).isEqualTo(DESTINATION_CONTENT);
72 void shouldCopyFileWithoutCreatingBackup() throws IOException {
73 File source = createFile("source.p12", SOURCE_CONTENT);
74 File destination = declareFile("destination.p12");
75 File backup = declareFile("destination.p12.bak");
76 AppConfiguration configuration = createConfiguration(source, destination);
78 copier.copyKeystores(configuration);
80 assertThat(destination.exists()).isTrue();
81 assertThat(readFile(destination)).isEqualTo(readFile(source));
82 assertThat(backup.exists()).isFalse();
86 void shouldThrowKeystoreNotExistException() throws IOException {
87 File source = declareFile("source.p12");
88 File destination = declareFile("destination.p12");
89 File backup = declareFile("destination.p12.bak");
90 AppConfiguration configuration = createConfiguration(source, destination);
92 assertThatExceptionOfType(KeystoreNotExistException.class).isThrownBy( () ->
93 copier.copyKeystores(configuration)
96 assertThat(source.exists()).isFalse();
97 assertThat(destination.exists()).isFalse();
98 assertThat(backup.exists()).isFalse();
102 void shouldThrowKeystoreFileCopyException() throws IOException {
103 File source = createFile("source.p12", SOURCE_CONTENT);
104 source.setReadable(false);
105 File destination = declareFile("destination.p12");
106 File backup = declareFile("destination.p12.bak");
107 AppConfiguration configuration = createConfiguration(source, destination);
109 assertThatExceptionOfType(KeystoreFileCopyException.class).isThrownBy( () ->
110 copier.copyKeystores(configuration)
113 assertThat(source.exists()).isTrue();
114 assertThat(destination.exists()).isFalse();
115 assertThat(backup.exists()).isFalse();
118 private AppConfiguration createConfiguration(File source, File destination) {
119 return new AppConfiguration(Collections.emptyList(), Collections.emptyList(),
120 Collections.singletonList(source.getAbsolutePath()),
121 Collections.singletonList(destination.getAbsolutePath()));
124 private AppConfiguration createEmptyConfiguration() {
125 return new AppConfiguration(Collections.emptyList(), Collections.emptyList(),
126 Collections.emptyList(),
127 Collections.emptyList());
130 private String readFile(File file) throws IOException {
131 return FileUtils.readFileToString(file, Charset.defaultCharset());
134 private File declareFile(String name) {
135 return new File(dir.getAbsolutePath() + File.pathSeparator + name);
138 private File createFile(String name, String content) throws IOException {
139 File file = new File(dir.getAbsolutePath() + File.pathSeparator + name);
140 if (file.createNewFile()) {
141 FileUtils.write(file, content, Charset.defaultCharset());
143 throw new IllegalStateException("File could not be created: " + file.getAbsolutePath());