Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

찾아보니 메서드 호출 시 전달된 인자를 가로채서 들고 있는 객체네요! 좋은 거 같아요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다!


assertEquals("test-bucket", captor.getValue().bucket());
assertEquals("club123/logo/abc.png", captor.getValue().key());
}
}
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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());
}
}


Loading