Skip to content

Commit

Permalink
feat : 파일 여러개 업로드 정상 흐름 실패 테스트 작성 #122
Browse files Browse the repository at this point in the history
  • Loading branch information
tlgur committed Feb 10, 2024
1 parent c1d71d6 commit 6469e8a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/skklub/admin/service/FileUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public FileName uploadOne(MultipartFile multipartFile) throws ServerSideExceptio
}


public List<FileName> uploadAll(MultipartFile... multipartFiles) {
public List<FileName> uploadAll(List<MultipartFile> multipartFiles) {
return null;
}

Expand Down
54 changes: 52 additions & 2 deletions src/test/java/com/skklub/admin/s3/unit/FileUploaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import scala.Int;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
Expand Down Expand Up @@ -81,7 +86,7 @@ void S3Import() throws IOException {

/**
* input : Random Byte File
* expect result : Save Success & return Random UUID
* expect result : Save Success & return With Random UUID
*/
@Test
@DisplayName("파일 한개 업로드 - 정상")
Expand All @@ -92,8 +97,8 @@ public void uploadOne_Default_Success() throws Exception{
byte[] contents = new byte[1024];
random.nextBytes(contents);
MockMultipartFile file = new MockMultipartFile(name + "." + extension, contents);

log.info("file : {}", file.getName());
log.info("file : {}", file.getContentType());
//mocking

//when
Expand All @@ -105,4 +110,49 @@ public void uploadOne_Default_Success() throws Exception{
assertThat("파일 저장 검증", amazonS3.doesObjectExist(bucket, fileName.getUploadedName()), equalTo(true));
}

/**
* input : 100 Random Byte Files
* expect result : Save Success & return With Random UUID
*/
@Test
@DisplayName("파일 여러개 업로드 - 정상")
public void uploadAll_Default_Success() throws Exception{
//given\
Integer fileCount = 100;
List<MultipartFile> files = new ArrayList<>();
String extension = "testFIleExtension";
byte[] contents = new byte[1024];
String testFIleNamePrefix = "testFIleName";
for (int i = 0; i < fileCount; i++) {
String name = testFIleNamePrefix + i;
random.nextBytes(contents);
files.add(new MockMultipartFile(name + "." + extension, contents));
}

//mocking

//when
List<FileName> fileNames = fileUploader.uploadAll(files);

//then
assertThat("변환된 파일 총 개수 검증", fileNames.size(), equalTo(fileCount));
List<String> multipartFileNames = files.stream()
.map(MultipartFile::getName)
.collect(Collectors.toList());
List<String> savedOriginalNames = fileNames.stream()
.map(FileName::getOriginalName)
.collect(Collectors.toList());
assertThat("원본 파일명 유지 검증 - List contains", multipartFileNames.containsAll(savedOriginalNames), equalTo(true));
savedOriginalNames
.forEach(originalFileName -> {
assertThat("원본 파일명 유지 검증 - Prefix", originalFileName, containsString(testFIleNamePrefix));
});
fileNames.stream()
.map(FileName::getUploadedName)
.forEach(uploadFileName -> {
assertThat("가상 파일명 생성 검증", uploadFileName, containsString("." + extension));
assertThat("파일 저장 검증", amazonS3.doesObjectExist(bucket, uploadFileName), equalTo(true));
});

}
}

0 comments on commit 6469e8a

Please sign in to comment.