|
1 | 1 | package com.somemore.global.imageupload.service; |
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
4 | | -import static org.junit.jupiter.api.Assertions.assertNotNull; |
5 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
6 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
| 4 | +import static org.junit.jupiter.api.Assertions.*; |
7 | 5 | import static org.mockito.BDDMockito.given; |
8 | 6 | import static org.mockito.Mockito.any; |
9 | 7 | import static org.mockito.Mockito.mock; |
|
17 | 15 | import com.somemore.support.IntegrationTestSupport; |
18 | 16 | import java.io.IOException; |
19 | 17 | import java.io.InputStream; |
| 18 | +import java.net.URL; |
| 19 | + |
20 | 20 | import org.junit.jupiter.api.BeforeEach; |
21 | 21 | import org.junit.jupiter.api.DisplayName; |
22 | 22 | import org.junit.jupiter.api.Test; |
|
28 | 28 | import software.amazon.awssdk.core.sync.RequestBody; |
29 | 29 | import software.amazon.awssdk.services.s3.S3Client; |
30 | 30 | import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 31 | +import software.amazon.awssdk.services.s3.presigner.S3Presigner; |
| 32 | +import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest; |
| 33 | +import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest; |
31 | 34 |
|
32 | 35 | class ImageUploadServiceTest extends IntegrationTestSupport { |
33 | 36 |
|
@@ -99,4 +102,56 @@ void uploadImageWithEmptyFile() { |
99 | 102 | // then |
100 | 103 | assertThat(imgUrl).isEqualTo(ImageUploadService.DEFAULT_IMAGE_URL); |
101 | 104 | } |
| 105 | + |
| 106 | + @DisplayName("유효한 파일명으로 사전 서명된 URL을 생성할 수 있다.") |
| 107 | + @Test |
| 108 | + void getPresignedUrl_success() { |
| 109 | + // given |
| 110 | + String filename = "testImage.jpg"; |
| 111 | + |
| 112 | + // Mock the validator to return false (file name is valid) |
| 113 | + when(imageUploadValidator.isEmptyFileName(filename)).thenReturn(false); |
| 114 | + |
| 115 | + // Mock the S3Presigner to return a URL |
| 116 | + S3Presigner mockPresigner = mock(S3Presigner.class); |
| 117 | + ReflectionTestUtils.setField(imageUploadService, "s3Presigner", mockPresigner); |
| 118 | + |
| 119 | + PresignedGetObjectRequest mockPresignedRequest = mock(PresignedGetObjectRequest.class); |
| 120 | + URL mockUrl = mock(URL.class); |
| 121 | + |
| 122 | + when(mockUrl.toString()).thenReturn("https://test-bucket.s3.amazonaws.com/unique-test-image.jpg"); |
| 123 | + when(mockPresignedRequest.url()).thenReturn(mockUrl); |
| 124 | + when(mockPresigner.presignGetObject(any(GetObjectPresignRequest.class))).thenReturn(mockPresignedRequest); |
| 125 | + |
| 126 | + // when |
| 127 | + String presignedUrl = imageUploadService.getPresignedUrl(filename); |
| 128 | + |
| 129 | + // then |
| 130 | + assertNotNull(presignedUrl); |
| 131 | + assertTrue(presignedUrl.startsWith("https://test-bucket.s3.amazonaws.com/")); |
| 132 | + assertTrue(presignedUrl.endsWith(".jpg")); |
| 133 | + |
| 134 | + // Verify interactions |
| 135 | + verify(imageUploadValidator, times(1)).isEmptyFileName(filename); |
| 136 | + verify(mockPresigner, times(1)).presignGetObject(any(GetObjectPresignRequest.class)); |
| 137 | + } |
| 138 | + |
| 139 | + @DisplayName("파일명 검증에 실패하면 null을 반환한다.") |
| 140 | + @Test |
| 141 | + void getPresignedUrl_invalidFileName() { |
| 142 | + |
| 143 | + // given |
| 144 | + String filename = ""; |
| 145 | + |
| 146 | + when(imageUploadValidator.isEmptyFileName(filename)).thenReturn(true); |
| 147 | + |
| 148 | + // when |
| 149 | + String presignedUrl = imageUploadService.getPresignedUrl(filename); |
| 150 | + |
| 151 | + // then |
| 152 | + assertNull(presignedUrl); |
| 153 | + |
| 154 | + // Verify interactions |
| 155 | + verify(imageUploadValidator, times(1)).isEmptyFileName(filename); |
| 156 | + } |
102 | 157 | } |
0 commit comments