-
Notifications
You must be signed in to change notification settings - Fork 3
[test] 동아리 활동사진 이미지 다중업로드 기능 테스트코드를 작성한다 #910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lepitaaar
merged 2 commits into
develop/be
from
test/#909-club-photos-multi-upload-test-MOA-403
Dec 7, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
backend/src/test/java/moadong/media/service/CloudFlareImageServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| package moadong.media.service; | ||
|
|
||
| import moadong.club.entity.Club; | ||
| import moadong.club.repository.ClubRepository; | ||
| import moadong.global.exception.ErrorCode; | ||
| import moadong.global.exception.RestApiException; | ||
| import moadong.util.annotations.UnitTest; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Spy; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import org.springframework.test.util.ReflectionTestUtils; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; | ||
| import software.amazon.awssdk.services.s3.presigner.S3Presigner; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| @UnitTest | ||
| @ExtendWith(MockitoExtension.class) | ||
| public class CloudFlareImageServiceTest { | ||
|
|
||
| @Spy | ||
| @InjectMocks | ||
| CloudflareImageService cloudflareImageService; | ||
|
|
||
| @Mock | ||
| private ClubRepository clubRepository; | ||
|
|
||
| @Mock | ||
| private S3Client s3Client; | ||
|
|
||
| @Mock | ||
| private S3Presigner s3Presigner; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| ReflectionTestUtils.setField(cloudflareImageService, "viewEndpoint", "https://cdn.example.com/"); | ||
| ReflectionTestUtils.setField(cloudflareImageService, "bucketName", "test-bucket"); | ||
| ReflectionTestUtils.invokeMethod(cloudflareImageService, "init"); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| void 잘못된_이름을_넣으면_UNSUPPORTED_FILE_TYPE_을_반환한다(){ | ||
| //given | ||
| String fileName = "Hello"; | ||
| //when | ||
| RestApiException exception = assertThrows(RestApiException.class, | ||
| () -> ReflectionTestUtils.invokeMethod(cloudflareImageService, "validateFileName", fileName)); | ||
| //then | ||
| assertEquals(ErrorCode.UNSUPPORTED_FILE_TYPE, exception.getErrorCode()); | ||
| } | ||
|
|
||
| @Test | ||
| void File_이름이_비어있으면_FILE_NOT_FOUND를_반환한다() { | ||
| //given | ||
| //이름이 비어있는 파일 | ||
| String emptyFileName = ""; | ||
| //when | ||
| //이름이 비어있는 파일을 넣었을 때 | ||
| RestApiException exception = assertThrows(RestApiException.class, | ||
| () -> ReflectionTestUtils.invokeMethod(cloudflareImageService, "validateFileName", emptyFileName)); | ||
| //then | ||
| //에러코드가 같아야함 | ||
| assertEquals(ErrorCode.FILE_NOT_FOUND, exception.getErrorCode()); | ||
| } | ||
|
|
||
| @Test | ||
| void 잘못된_filePath_일때_deleteFile_을_실패한다 (){ | ||
| //given | ||
| Club club = new Club(); | ||
| String filePath = null; | ||
| //when | ||
| cloudflareImageService.deleteFile(club, filePath); | ||
| //then | ||
| verify(s3Client, never()).deleteObject(any(DeleteObjectRequest.class)); | ||
| } | ||
|
|
||
| @Test | ||
| void 접두_불일치면_deleteFile_을_실패한다 (){ | ||
| //given | ||
| Club club = new Club(); | ||
| String wrongFilePath ="https://other.example.com/club123/logo/abc.png"; | ||
|
|
||
| //when | ||
| cloudflareImageService.deleteFile(club, wrongFilePath); | ||
| //then | ||
| verify(s3Client, never()).deleteObject(any(DeleteObjectRequest.class)); | ||
| } | ||
|
|
||
| @Test | ||
| void 정상_url이면_삭제_성공 () { | ||
| //given | ||
| Club club = new Club(); | ||
| String filePath = "https://cdn.example.com/club123/logo/abc.png"; | ||
|
|
||
| //when | ||
| cloudflareImageService.deleteFile(club, filePath); | ||
|
|
||
| //then | ||
| ArgumentCaptor<DeleteObjectRequest> captor = ArgumentCaptor.forClass(DeleteObjectRequest.class); | ||
| verify(s3Client).deleteObject(captor.capture()); | ||
|
|
||
| assertEquals("test-bucket", captor.getValue().bucket()); | ||
| assertEquals("club123/logo/abc.png", captor.getValue().key()); | ||
| } | ||
| } | ||
78 changes: 78 additions & 0 deletions
78
backend/src/test/java/moadong/media/service/CloudflareImageServiceUpdateFeedsLimitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package moadong.media.service; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import moadong.club.entity.Club; | ||
| import moadong.club.entity.ClubRecruitmentInformation; | ||
| import moadong.club.repository.ClubRepository; | ||
| import moadong.global.exception.ErrorCode; | ||
| import moadong.global.exception.RestApiException; | ||
| import moadong.util.annotations.UnitTest; | ||
| import org.bson.types.ObjectId; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Spy; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.presigner.S3Presigner; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| @UnitTest | ||
| class CloudflareImageServiceUpdateFeedsLimitTest { | ||
|
|
||
| @Spy | ||
| @InjectMocks | ||
| private CloudflareImageService cloudflareImageService; | ||
|
|
||
| @Mock | ||
| private ClubRepository clubRepository; | ||
|
|
||
| @Mock | ||
| private S3Client s3Client; | ||
|
|
||
| @Mock | ||
| private S3Presigner s3Presigner; | ||
|
|
||
| private final int MAX_FEED_COUNT = 15; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 프로덕션 코드 쪽 상수를 직접 참조하는건 어떨까요?? |
||
|
|
||
| private Club club; | ||
| private ObjectId objectId; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| ClubRecruitmentInformation info = ClubRecruitmentInformation.builder() | ||
| .feedImages(List.of()) | ||
| .build(); | ||
| club = Club.builder().clubRecruitmentInformation(info).build(); | ||
| objectId = new ObjectId(); | ||
| } | ||
|
|
||
| @Test | ||
| void MAX_FEED_COUNT_이상의_피드를_업로드하면_TOO_MANY_FILES를_반환한다() { | ||
| // given | ||
| List<String> tooMany = Arrays.asList(new String[MAX_FEED_COUNT]); | ||
| ClubRecruitmentInformation info = ClubRecruitmentInformation.builder() | ||
| .feedImages(tooMany) | ||
| .build(); | ||
| club = Club.builder().clubRecruitmentInformation(info).build(); | ||
| when(clubRepository.findClubById(any())).thenReturn(Optional.of(club)); | ||
|
|
||
| // when | ||
| RestApiException exception = assertThrows(RestApiException.class, | ||
| () -> cloudflareImageService.updateFeeds(objectId.toHexString(), tooMany)); | ||
|
|
||
| // then | ||
| assertEquals(ErrorCode.TOO_MANY_FILES, exception.getErrorCode()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
찾아보니 메서드 호출 시 전달된 인자를 가로채서 들고 있는 객체네요! 좋은 거 같아요~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
감사합니다!