|
| 1 | +package com.econo_4factorial.newproject.user.service; |
| 2 | + |
| 3 | +import com.amazonaws.services.s3.AmazonS3Client; |
| 4 | +import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; |
| 5 | +import com.econo_4factorial.newproject.user.domain.ImageFileFormat; |
| 6 | +import com.econo_4factorial.newproject.user.dto.PresignedUrlDTO; |
| 7 | +import com.econo_4factorial.newproject.user.dto.ProfileImageUrlDTO; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.DisplayName; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 12 | +import org.mockito.InjectMocks; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 15 | +import org.springframework.test.util.ReflectionTestUtils; |
| 16 | + |
| 17 | +import java.net.MalformedURLException; |
| 18 | +import java.net.URL; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 22 | +import static org.mockito.ArgumentMatchers.*; |
| 23 | +import static org.mockito.BDDMockito.given; |
| 24 | +import static org.mockito.Mockito.*; |
| 25 | + |
| 26 | +@ExtendWith(MockitoExtension.class) |
| 27 | +class S3ServiceTest { |
| 28 | + |
| 29 | + @InjectMocks |
| 30 | + private S3Service s3Service; |
| 31 | + |
| 32 | + @Mock |
| 33 | + private AmazonS3Client amazonS3Client; |
| 34 | + |
| 35 | + @Mock |
| 36 | + private UserService userService; |
| 37 | + |
| 38 | + private final String BUCKET = "test-bucket"; |
| 39 | + private final String DEFAULT_PROFILE_KEY = "default/profile.png"; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + void setUp() { |
| 43 | + ReflectionTestUtils.setField(s3Service, "bucket", BUCKET); |
| 44 | + ReflectionTestUtils.setField(s3Service, "defaultProfileKey", DEFAULT_PROFILE_KEY); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @DisplayName("Presigned URL이 정상적으로 생성되어야 한다") |
| 49 | + void createPresignedUrl_Success() throws MalformedURLException { |
| 50 | + // given |
| 51 | + Long userId = 1L; |
| 52 | + ImageFileFormat fileFormat = ImageFileFormat.JPG; |
| 53 | + URL mockUrl = new URL("https://test-bucket.s3.amazonaws.com/test-path"); |
| 54 | + given(amazonS3Client.generatePresignedUrl(any(GeneratePresignedUrlRequest.class))).willReturn(mockUrl); |
| 55 | + |
| 56 | + // when |
| 57 | + PresignedUrlDTO result = s3Service.createPresignedUrl(userId, fileFormat); |
| 58 | + |
| 59 | + // then |
| 60 | + assertThat(result.presignedUrl()).isEqualTo(mockUrl.toString()); |
| 61 | + assertThat(result.fileName()).contains("profile/" + userId + "/"); |
| 62 | + assertThat(result.fileName()).endsWith(".jpeg"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + @DisplayName("DB에 파일명이 없을 때 기본 이미지 URL을 반환해야 한다") |
| 67 | + void getImageUrl_ReturnsDefault_WhenNoFileName() throws MalformedURLException { |
| 68 | + // given |
| 69 | + Long userId = 1L; |
| 70 | + given(userService.getUserProfileFileName(userId)).willReturn(null); |
| 71 | + given(amazonS3Client.getUrl(BUCKET, DEFAULT_PROFILE_KEY)).willReturn(new URL("https://s3.com/" + DEFAULT_PROFILE_KEY)); |
| 72 | + |
| 73 | + // when |
| 74 | + ProfileImageUrlDTO result = s3Service.getImageUrl(userId); |
| 75 | + |
| 76 | + // then |
| 77 | + assertThat(result.profileImageUrl()).contains(DEFAULT_PROFILE_KEY); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + @DisplayName("새 이미지를 저장할 때 기존 이미지가 있다면 삭제해야 한다") |
| 82 | + void saveFileNameToEntity_DeletesOldImage() { |
| 83 | + // given |
| 84 | + Long userId = 1L; |
| 85 | + String oldFileName = "profile/1/old-uuid.jpeg"; |
| 86 | + String newFileName = "profile/1/new-uuid.jpeg"; |
| 87 | + |
| 88 | + given(amazonS3Client.doesObjectExist(BUCKET, newFileName)).willReturn(true); |
| 89 | + given(userService.getUserProfileFileName(userId)).willReturn(oldFileName); |
| 90 | + |
| 91 | + // when |
| 92 | + s3Service.saveFileNameToEntity(userId, newFileName); |
| 93 | + |
| 94 | + // then |
| 95 | + verify(amazonS3Client).deleteObject(BUCKET, oldFileName); |
| 96 | + verify(userService).updateUserProfileFileName(userId, newFileName); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + @DisplayName("새 이미지를 저장할 때 기존 이미지가 기본 이미지라면 삭제하지 않아야 한다") |
| 101 | + void saveFileNameToEntity_DoesNotDeleteDefaultImage() { |
| 102 | + // given |
| 103 | + Long userId = 1L; |
| 104 | + String newFileName = "profile/1/new-uuid.jpeg"; |
| 105 | + |
| 106 | + given(amazonS3Client.doesObjectExist(BUCKET, newFileName)).willReturn(true); |
| 107 | + given(userService.getUserProfileFileName(userId)).willReturn(DEFAULT_PROFILE_KEY); |
| 108 | + |
| 109 | + // when |
| 110 | + s3Service.saveFileNameToEntity(userId, newFileName); |
| 111 | + |
| 112 | + // then |
| 113 | + verify(amazonS3Client, never()).deleteObject(eq(BUCKET), anyString()); |
| 114 | + verify(userService).updateUserProfileFileName(userId, newFileName); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + @DisplayName("S3에 존재하지 않는 파일명을 저장하려 하면 예외가 발생해야 한다") |
| 119 | + void saveFileNameToEntity_ThrowsException_WhenFileNotExists() { |
| 120 | + // given |
| 121 | + Long userId = 1L; |
| 122 | + String fileName = "non-existent-file.jpeg"; |
| 123 | + given(amazonS3Client.doesObjectExist(BUCKET, fileName)).willReturn(false); |
| 124 | + |
| 125 | + // when & then |
| 126 | + assertThatThrownBy(() -> s3Service.saveFileNameToEntity(userId, fileName)) |
| 127 | + .isInstanceOf(IllegalStateException.class) |
| 128 | + .hasMessageContaining("Image does not exist"); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + @DisplayName("프로필 이미지 삭제 시 S3와 DB 모두에서 지워져야 한다") |
| 133 | + void deleteImageUrl_Success() { |
| 134 | + // given |
| 135 | + Long userId = 1L; |
| 136 | + String fileName = "profile/1/uuid.jpeg"; |
| 137 | + given(userService.getUserProfileFileName(userId)).willReturn(fileName); |
| 138 | + given(amazonS3Client.doesObjectExist(BUCKET, fileName)).willReturn(true); |
| 139 | + |
| 140 | + // when |
| 141 | + s3Service.deleteImageUrl(userId); |
| 142 | + |
| 143 | + // then |
| 144 | + verify(amazonS3Client).deleteObject(BUCKET, fileName); |
| 145 | + verify(userService).deleteUserProfileFileName(userId); |
| 146 | + } |
| 147 | +} |
0 commit comments