-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileUploadServiceS3Tests.java
83 lines (70 loc) · 2.87 KB
/
FileUploadServiceS3Tests.java
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
package devops.workshop.photocompressor.UnitTests;
import devops.workshop.photocompressor.services.FileUploadServiceS3;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = FileUploadServiceS3.class)
@TestPropertySource(properties = {"image.source.folder=src/test/resources/images"})
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class FileUploadServiceS3Tests {
@Autowired
private FileUploadServiceS3 fileUploadServiceS3;
private String resourcePath = "./src/test/resources/TestData";
private String imagePath = "src/test/resources/images/";
private String uploadedFileName = "test_image.jpg";
public MultipartFile getMultipartFile() {
String name = uploadedFileName;
String originalFileName = "test_image.jpg";
String contentType = "image/jpg";
byte[] content = null;
try {
content = Files.readAllBytes(Paths.get(resourcePath + "/" + originalFileName));
} catch (final IOException e) {
}
return new MockMultipartFile(name, originalFileName, contentType, content);
}
@Test
@Order(1)
public void uploadFileFromMultipartFileTestCase() throws IOException {
// Given a Multipart file
MultipartFile multipartFile = getMultipartFile();
// When running upload
fileUploadServiceS3.upload(multipartFile);
// Expect an image in the images folder
byte[] file = Files.readAllBytes(Paths.get(imagePath + multipartFile.getName()));
Assert.assertNotEquals(
"The image uploaded is Null.",
0,
file.length
);
}
@Test
@Order(2)
public void cleanUploadedFileTestCase() throws IOException {
// Given the file uploaded by the previous test
File file = Paths.get(imagePath + uploadedFileName).toFile();
// When running clean
fileUploadServiceS3.clean(file);
// Expect no image in the folder
Assert.assertThrows(
"The image uploaded was not deleted.",
NoSuchFileException.class,
() -> {Files.readAllBytes(Paths.get(imagePath + uploadedFileName));}
);
}
}