-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFileHelperTests.java
More file actions
332 lines (287 loc) · 11.2 KB
/
FileHelperTests.java
File metadata and controls
332 lines (287 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Copyright 2020-2026 IEXEC BLOCKCHAIN TECH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.iexec.common.utils;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import static com.iexec.common.utils.FileHelper.downloadFile;
import static org.assertj.core.api.Assertions.assertThat;
class FileHelperTests {
private static final String TEST_FOLDER = "/tmp/iexec-test";
private static final String HTTP_URL = "http://icons.iconarchive.com/icons/" +
"cjdowner/cryptocurrency-flat/512/iExec-RLC-RLC-icon.png";
private static final String HTTPS_URL = "https://icons.iconarchive.com/icons/" +
"cjdowner/cryptocurrency-flat/512/iExec-RLC-RLC-icon.png";
private static final String FILE_DIGEST =
"0x4d8401fd4484f07c202c0a2b9ce6907eabd69efae0cec3956f1a56a6b19a9daa";
private static final String ICON_PNG = "icon.png";
// clean the test repo before and after each test
@BeforeEach
void init() throws IOException {
FileUtils.deleteDirectory(new File(TEST_FOLDER));
}
@AfterEach
void tearDown() throws IOException {
FileUtils.deleteDirectory(new File(TEST_FOLDER));
}
@Test
void shouldCreateFileWithContent() throws IOException {
String data = "a test";
File file = FileHelper.createFileWithContent(TEST_FOLDER + "/test.txt", data);
assertThat(file)
.isNotNull()
.exists()
.isFile();
String content = Files.readString(Paths.get(file.getAbsolutePath()));
assertThat(content).isEqualTo(data);
}
// region createFolder
@Test
void shouldCreateFolder() {
String folderPath = TEST_FOLDER + "/folder";
boolean created = FileHelper.createFolder(folderPath);
File newFolder = new File(folderPath);
assertThat(created).isTrue();
assertThat(newFolder)
.isNotNull()
.exists()
.isDirectory();
// it should not change anything if the folder is already created
boolean createdAgain = FileHelper.createFolder(folderPath);
File existingFolder = new File(folderPath);
assertThat(createdAgain).isTrue();
assertThat(existingFolder)
.isNotNull()
.exists()
.isDirectory();
}
@Test
void shouldCreateFolderRecursively() {
String folderPath = TEST_FOLDER + "/folder1/folder2/folder3";
boolean created = FileHelper.createFolder(folderPath);
File newFolder = new File(folderPath);
assertThat(created).isTrue();
assertThat(newFolder)
.isNotNull()
.exists()
.isDirectory();
// it should not change anything if the folder is already created
boolean createdAgain = FileHelper.createFolder(folderPath);
File existingFolder = new File(folderPath);
assertThat(createdAgain).isTrue();
assertThat(existingFolder)
.isNotNull()
.exists()
.isDirectory();
}
// endregion
// region deleteFile
@Test
void shouldDeleteFile() {
String filePath = TEST_FOLDER + "/test.txt";
File file = FileHelper.createFileWithContent(filePath, "Hello world");
assertThat(file)
.isNotNull()
.exists()
.isFile();
boolean isDeleted = FileHelper.deleteFile(filePath);
File deletedFile = new File(filePath);
assertThat(isDeleted).isTrue();
assertThat(deletedFile).doesNotExist();
}
@Test
void shouldNotDeleteNonExistingFile() {
String filePath = TEST_FOLDER + "/test.txt";
boolean isDeleted = FileHelper.deleteFile(filePath);
File deletedFile = new File(filePath);
assertThat(isDeleted).isFalse();
assertThat(deletedFile).doesNotExist();
}
// endregion
// region deleteFolder
@Test
void shouldDeleteFolder() {
String folderPath = TEST_FOLDER + "/folder";
boolean created = FileHelper.createFolder(folderPath);
File newFolder = new File(folderPath);
assertThat(created).isTrue();
assertThat(newFolder)
.isNotNull()
.exists()
.isDirectory();
boolean isDeleted = FileHelper.deleteFolder(folderPath);
File deletedFolder = new File(folderPath);
assertThat(isDeleted).isTrue();
assertThat(deletedFolder).doesNotExist();
}
@Test
void shouldDeleteFoldersRecursively() {
String folderPath = TEST_FOLDER + "/folder1/folder2/folder3";
boolean created = FileHelper.createFolder(folderPath);
File newFolder = new File(folderPath);
assertThat(created).isTrue();
assertThat(newFolder)
.isNotNull()
.exists()
.isDirectory();
boolean isDeleted = FileHelper.deleteFolder(folderPath);
File deletedFolder = new File(folderPath);
assertThat(isDeleted).isTrue();
assertThat(deletedFolder).doesNotExist();
}
@Test
void shouldDoNothingWhenNonExistingFolder() {
String folderPath = TEST_FOLDER + "/folder";
boolean isDeleted = FileHelper.deleteFolder(folderPath);
File deletedFolder = new File(folderPath);
assertThat(isDeleted).isTrue();
assertThat(deletedFolder).doesNotExist();
}
// endregion
@Test
void shouldZipFolder() {
FileHelper.createFileWithContent(TEST_FOLDER + "/taskId/test.txt", "a test");
File zipFile = FileHelper.zipFolder(TEST_FOLDER + "/taskId");
assertThat(zipFile)
.isNotNull()
.exists()
.isFile();
assertThat(zipFile.getAbsolutePath()).isEqualTo(TEST_FOLDER + "/taskId.zip");
}
// region downloadFile(url, dir, name)
@Test
void shouldDownloadFileWithName() {
String downloadedFilePath = FileHelper.downloadFile(
HTTP_URL, TEST_FOLDER, ICON_PNG);
assertThat(downloadedFilePath).isEqualTo(TEST_FOLDER + "/icon.png");
assertThat(new File(downloadedFilePath)).exists();
// check that the correct file is downloaded
assertThat(FileHashUtils.sha256(new File(downloadedFilePath)))
.isEqualTo(FILE_DIGEST);
}
@Test
void shouldDownloadFileWithNameFromHttpsUrl() {
String downloadedFilePath = FileHelper.downloadFile(
HTTPS_URL, TEST_FOLDER, "token.svg");
assertThat(downloadedFilePath).isEqualTo(TEST_FOLDER + "/token.svg");
assertThat(new File(TEST_FOLDER + "/token.svg")).exists();
// check that the correct file is downloaded
assertThat(FileHashUtils.sha256(new File(downloadedFilePath)))
.isEqualTo(FILE_DIGEST);
}
@Test
void shouldNotDownloadFileWithEmptyUrl() {
assertThat(FileHelper.downloadFile("", TEST_FOLDER, "file.txt")).isEmpty();
assertThat(new File(TEST_FOLDER)).doesNotExist();
}
@Test
void shouldNotDownloadFileWithEmptyParentFolderPath() {
assertThat(FileHelper.downloadFile(HTTP_URL, "", ICON_PNG)).isEmpty();
assertThat(new File(TEST_FOLDER)).doesNotExist();
}
@Test
void shouldNotDownloadFileWithEmptyOutputFileName() {
assertThat(FileHelper.downloadFile(HTTP_URL, TEST_FOLDER, "")).isEmpty();
assertThat(new File(TEST_FOLDER)).doesNotExist();
}
@Test
void shouldNotDownloadFileWithBadUrl() {
assertThat(FileHelper.downloadFile("http://bad-url", TEST_FOLDER, "file.txt"))
.isEmpty();
assertThat(new File(TEST_FOLDER)).doesNotExist();
}
@Test
void shouldNotDownloadFileSinceCannotCreateFolder() {
String downloadedFilePath = FileHelper.downloadFile(
HTTP_URL, "/unauthorized", ICON_PNG);
assertThat(downloadedFilePath).isEmpty();
}
// endregion
// TODO
// @Test
// void shouldCleanCreatedParentFolderWhenFailingToWriteDownloadedFile() {}
// region readFileBytesFromUrl(url)
@Test
void shouldReadFileBytesFromUrl() {
byte[] bytes = FileHelper.readFileBytesFromUrl(HTTP_URL);
assertThat(bytes).isNotEmpty();
}
@Test
void shouldNotReadFileBytesFromBadUrl() {
byte[] bytes = FileHelper.readFileBytesFromUrl("http://bad-url");
assertThat(bytes).isEqualTo(new byte[0]);
}
// endregion
// region downloadFile(url, dir)
@Test
void shouldDownloadFileWithHttpUrl() {
String downloadedFilePath = downloadFile(HTTP_URL, TEST_FOLDER);
String filename = Paths.get(HTTP_URL).getFileName().toString();
assertThat(downloadedFilePath).isEqualTo(TEST_FOLDER + "/" + filename);
assertThat(new File(TEST_FOLDER + "/" + filename)).exists();
// check that the correct file is downloaded
assertThat(FileHashUtils.sha256(new File(downloadedFilePath)))
.isEqualTo(FILE_DIGEST);
}
@Test
void shouldDownloadFileWithHttpsUrl() {
String downloadedFilePath = downloadFile(HTTPS_URL, TEST_FOLDER);
String filename = Paths.get(HTTPS_URL).getFileName().toString();
assertThat(downloadedFilePath).isEqualTo(TEST_FOLDER + "/" + filename);
assertThat(new File(TEST_FOLDER + "/" + filename)).exists();
// check that the correct file is downloaded
assertThat(FileHashUtils.sha256(new File(downloadedFilePath)))
.isEqualTo(FILE_DIGEST);
}
// endregion
// region exists()
@Test
void shouldFindFolder() {
assertThat(FileHelper.exists("/tmp")).isTrue();
}
@Test
void shouldFindFile() {
File file = FileHelper.createFileWithContent(TEST_FOLDER + "/test.txt", "whatever");
assertThat(FileHelper.exists(file.getAbsolutePath())).isTrue();
}
@Test
void shouldNotFindFolder() {
assertThat(FileHelper.exists("/not-found")).isFalse();
}
// endregion
// region removeZipExtension(path)
@Test
void shouldRemoveZipExtension() {
String fileName = FileHelper.removeZipExtension("/some/where/file.zip");
assertThat(fileName).isEqualTo("/some/where/file");
}
@Test
void shouldNotRemoveZipExtensionSinceNoExtension() {
String fileName = FileHelper.removeZipExtension("/some/where/file");
assertThat(fileName).isEmpty();
}
@Test
void shouldNotRemoveZipExtensionSinceWrongExtension() {
String fileName = FileHelper.removeZipExtension("/some/where/file.bla");
assertThat(fileName).isEmpty();
}
// endregion
}