|
| 1 | +package eatda.service.common; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.mockito.ArgumentMatchers.any; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.mockito.Mockito.verify; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import eatda.exception.BusinessErrorCode; |
| 12 | +import eatda.exception.BusinessException; |
| 13 | +import java.io.IOException; |
| 14 | +import java.net.URL; |
| 15 | +import java.time.Duration; |
| 16 | +import org.junit.jupiter.api.BeforeEach; |
| 17 | +import org.junit.jupiter.api.Nested; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 20 | +import org.mockito.ArgumentCaptor; |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 23 | +import org.springframework.mock.web.MockMultipartFile; |
| 24 | +import software.amazon.awssdk.core.exception.SdkClientException; |
| 25 | +import software.amazon.awssdk.core.sync.RequestBody; |
| 26 | +import software.amazon.awssdk.services.s3.S3Client; |
| 27 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 28 | +import software.amazon.awssdk.services.s3.presigner.S3Presigner; |
| 29 | +import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest; |
| 30 | +import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest; |
| 31 | + |
| 32 | +@ExtendWith(MockitoExtension.class) |
| 33 | +class ImageServiceTest { |
| 34 | + |
| 35 | + private static final String TEST_BUCKET = "test-bucket"; |
| 36 | + |
| 37 | + @Mock |
| 38 | + private S3Client s3Client; |
| 39 | + |
| 40 | + @Mock |
| 41 | + private S3Presigner s3Presigner; |
| 42 | + private ImageService imageService; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void setUp() { |
| 46 | + imageService = new ImageService(s3Client, TEST_BUCKET, s3Presigner); |
| 47 | + } |
| 48 | + |
| 49 | + @Nested |
| 50 | + class FileUpload { |
| 51 | + |
| 52 | + @Test |
| 53 | + void 허용된_이미지_타입이면_정상적으로_업로드되고_생성된_Key를_반환한다() throws IOException { |
| 54 | + String originalFilename = "test-image.jpg"; |
| 55 | + String contentType = "image/jpeg"; |
| 56 | + String domain = "stores"; |
| 57 | + MockMultipartFile file = new MockMultipartFile( |
| 58 | + "image", originalFilename, contentType, "image-content".getBytes() |
| 59 | + ); |
| 60 | + |
| 61 | + String key = imageService.upload(file, domain); |
| 62 | + |
| 63 | + ArgumentCaptor<PutObjectRequest> putObjectRequestCaptor = ArgumentCaptor.forClass(PutObjectRequest.class); |
| 64 | + verify(s3Client).putObject(putObjectRequestCaptor.capture(), any(RequestBody.class)); |
| 65 | + PutObjectRequest capturedRequest = putObjectRequestCaptor.getValue(); |
| 66 | + |
| 67 | + assertAll( |
| 68 | + () -> assertThat(key).matches(domain + "/[a-f0-9\\-]{36}\\.jpg"), |
| 69 | + () -> assertThat(capturedRequest.key()).isEqualTo(key), |
| 70 | + () -> assertThat(capturedRequest.bucket()).isEqualTo(TEST_BUCKET), |
| 71 | + () -> assertThat(capturedRequest.contentType()).isEqualTo(contentType) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void 허용되지_않은_파일_타입이면_BusinessException을_던진다() { |
| 77 | + MockMultipartFile file = new MockMultipartFile( |
| 78 | + "file", "test.txt", "text/plain", "file-content".getBytes() |
| 79 | + ); |
| 80 | + |
| 81 | + BusinessException exception = assertThrows(BusinessException.class, |
| 82 | + () -> imageService.upload(file, "etc")); |
| 83 | + |
| 84 | + assertThat(exception.getErrorCode()).isEqualTo(BusinessErrorCode.INVALID_IMAGE_TYPE); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Nested |
| 89 | + class GeneratePresignedUrl { |
| 90 | + |
| 91 | + @Test |
| 92 | + void 유효한_key로_요청_시_Presigned_URL을_성공적으로_반환한다() throws Exception { |
| 93 | + String key = "stores/image.jpg"; |
| 94 | + String expectedUrlString = "https://example.com/presigned-url-for-image.jpg"; |
| 95 | + URL expectedUrl = new URL(expectedUrlString); |
| 96 | + |
| 97 | + PresignedGetObjectRequest presignedRequestResult = mock(PresignedGetObjectRequest.class); |
| 98 | + |
| 99 | + when(presignedRequestResult.url()).thenReturn(expectedUrl); |
| 100 | + when(s3Presigner.presignGetObject(any(GetObjectPresignRequest.class))) |
| 101 | + .thenReturn(presignedRequestResult); |
| 102 | + |
| 103 | + String presignedUrl = imageService.getPresignedUrl(key); |
| 104 | + |
| 105 | + ArgumentCaptor<GetObjectPresignRequest> presignRequestCaptor = |
| 106 | + ArgumentCaptor.forClass(GetObjectPresignRequest.class); |
| 107 | + verify(s3Presigner).presignGetObject(presignRequestCaptor.capture()); |
| 108 | + GetObjectPresignRequest capturedPresignRequest = presignRequestCaptor.getValue(); |
| 109 | + |
| 110 | + assertAll( |
| 111 | + () -> assertThat(presignedUrl).isEqualTo(expectedUrlString), |
| 112 | + () -> assertThat(capturedPresignRequest.getObjectRequest().key()).isEqualTo(key), |
| 113 | + () -> assertThat(capturedPresignRequest.getObjectRequest().bucket()).isEqualTo(TEST_BUCKET), |
| 114 | + () -> assertThat(capturedPresignRequest.signatureDuration()).isEqualTo(Duration.ofMinutes(30)) |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void Presigner가_예외를_던지면_BusinessException으로_전환하여_던진다() { |
| 120 | + String key = "stores/image.jpg"; |
| 121 | + |
| 122 | + when(s3Presigner.presignGetObject(any(GetObjectPresignRequest.class))) |
| 123 | + .thenThrow(SdkClientException.create("AWS SDK 통신 실패")); |
| 124 | + |
| 125 | + BusinessException exception = assertThrows(BusinessException.class, |
| 126 | + () -> imageService.getPresignedUrl(key)); |
| 127 | + |
| 128 | + assertThat(exception.getErrorCode()).isEqualTo(BusinessErrorCode.PRESIGNED_URL_GENERATION_FAILED); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments