[OOM-CMPv2] Create KeystoreCopier
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / copier / KeystoreCopierTest.java
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
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.truststoremerger.copier;
21
22 import java.io.File;
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.truststoremerger.common.FileTools;
30 import org.onap.oom.truststoremerger.configuration.model.AppConfiguration;
31 import org.onap.oom.truststoremerger.copier.exception.KeystoreFileCopyException;
32 import org.onap.oom.truststoremerger.copier.exception.KeystoreNotExistException;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
36
37 public class KeystoreCopierTest {
38
39     private static final String SOURCE_CONTENT = "source content";
40     private static final String DESTINATION_CONTENT = "destination content";
41
42     @TempDir
43     File dir;
44
45     private KeystoreCopier copier = new KeystoreCopier(new FileTools());
46
47     @Test
48     void shouldDoNothingForEmptySourceFileList() {
49         AppConfiguration configuration = createEmptyConfiguration();
50
51         copier.copyKeystores(configuration);
52
53         assertThat(dir.listFiles()).isEmpty();
54     }
55
56
57     @Test
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);
63
64         copier.copyKeystores(configuration);
65
66         assertThat(readFile(destination)).isEqualTo(readFile(source));
67         assertThat(backup.exists()).isTrue();
68         assertThat(readFile(backup)).isEqualTo(DESTINATION_CONTENT);
69     }
70
71     @Test
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);
77
78         copier.copyKeystores(configuration);
79
80         assertThat(destination.exists()).isTrue();
81         assertThat(readFile(destination)).isEqualTo(readFile(source));
82         assertThat(backup.exists()).isFalse();
83     }
84
85     @Test
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);
91
92         assertThatExceptionOfType(KeystoreNotExistException.class).isThrownBy( () ->
93             copier.copyKeystores(configuration)
94         );
95
96         assertThat(source.exists()).isFalse();
97         assertThat(destination.exists()).isFalse();
98         assertThat(backup.exists()).isFalse();
99     }
100
101     @Test
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);
108
109         assertThatExceptionOfType(KeystoreFileCopyException.class).isThrownBy( () ->
110             copier.copyKeystores(configuration)
111         );
112
113         assertThat(source.exists()).isTrue();
114         assertThat(destination.exists()).isFalse();
115         assertThat(backup.exists()).isFalse();
116     }
117
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()));
122     }
123
124     private AppConfiguration createEmptyConfiguration() {
125         return new AppConfiguration(Collections.emptyList(), Collections.emptyList(),
126             Collections.emptyList(),
127             Collections.emptyList());
128     }
129
130     private String readFile(File file) throws IOException {
131         return FileUtils.readFileToString(file, Charset.defaultCharset());
132     }
133
134     private File declareFile(String name) {
135         return new File(dir.getAbsolutePath() + File.pathSeparator + name);
136     }
137
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());
142         } else {
143             throw new IllegalStateException("File could not be created: " + file.getAbsolutePath());
144         }
145         return file;
146     }
147 }