|
| 1 | +package org.runnect.server.external.aws; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import com.amazonaws.services.s3.AmazonS3; |
| 10 | +import com.amazonaws.services.s3.model.PutObjectRequest; |
| 11 | +import java.io.ByteArrayInputStream; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.net.URL; |
| 15 | +import org.junit.jupiter.api.BeforeEach; |
| 16 | +import org.junit.jupiter.api.DisplayName; |
| 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.Mock; |
| 21 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 22 | +import org.runnect.server.common.exception.BadRequestException; |
| 23 | +import org.runnect.server.common.exception.NotFoundException; |
| 24 | +import org.springframework.mock.web.MockMultipartFile; |
| 25 | +import org.springframework.test.util.ReflectionTestUtils; |
| 26 | +import org.springframework.web.multipart.MultipartFile; |
| 27 | + |
| 28 | +@ExtendWith(MockitoExtension.class) |
| 29 | +class S3ServiceTest { |
| 30 | + |
| 31 | + @Mock |
| 32 | + private AmazonS3 amazonS3; |
| 33 | + |
| 34 | + private S3Service s3Service; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setUp() { |
| 38 | + s3Service = new S3Service(amazonS3); |
| 39 | + ReflectionTestUtils.setField(s3Service, "bucket", "runnect-test-bucket"); |
| 40 | + } |
| 41 | + |
| 42 | + private void stubUploadedUrl() throws Exception { |
| 43 | + when(amazonS3.getUrl(any(), any())).thenReturn( |
| 44 | + new URL("https://runnect-test-bucket.s3.ap-northeast-2.amazonaws.com/course/image/test.jpg")); |
| 45 | + } |
| 46 | + |
| 47 | + @Nested |
| 48 | + @DisplayName("uploadImage") |
| 49 | + class UploadImage { |
| 50 | + |
| 51 | + @Test |
| 52 | + @DisplayName("μ μμ μΈ μ΄λ―Έμ§ νμΌμ΄λ©΄ μ
λ‘λνκ³ URLμ λ°ννλ€") |
| 53 | + void μ μ_μ
λ‘λ() throws Exception { |
| 54 | + stubUploadedUrl(); |
| 55 | + MultipartFile file = new MockMultipartFile("image", "photo.jpg", "image/jpeg", "content".getBytes()); |
| 56 | + |
| 57 | + String url = s3Service.uploadImage(file, "course"); |
| 58 | + |
| 59 | + assertThat(url).isEqualTo( |
| 60 | + "https://runnect-test-bucket.s3.ap-northeast-2.amazonaws.com/course/image/test.jpg"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName("λλ¬Έμ νμ₯μ(.PNG)λ μ μ μ
λ‘λλλ€") |
| 65 | + void λλ¬Έμ_νμ₯μ() throws Exception { |
| 66 | + stubUploadedUrl(); |
| 67 | + MultipartFile file = new MockMultipartFile("image", "photo.PNG", "image/png", "content".getBytes()); |
| 68 | + |
| 69 | + assertThat(s3Service.uploadImage(file, "course")).isNotNull(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @DisplayName("μ§μνμ§ μλ νμ₯μλ©΄ BadRequestException") |
| 74 | + void μ§μνμ§_μλ_νμ₯μ() { |
| 75 | + MultipartFile file = new MockMultipartFile("image", "photo.gif", "image/gif", "content".getBytes()); |
| 76 | + |
| 77 | + assertThatThrownBy(() -> s3Service.uploadImage(file, "course")) |
| 78 | + .isInstanceOf(BadRequestException.class); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @DisplayName("[λ²κ·Έ μμ κ²μ¦] νμΌλͺ
μ΄ nullμ΄λ©΄ 500(NPE) λμ NotFoundException") |
| 83 | + void νμΌλͺ
μ΄_null() { |
| 84 | + MultipartFile file = new MockMultipartFile("image", null, "image/jpeg", "content".getBytes()); |
| 85 | + |
| 86 | + assertThatThrownBy(() -> s3Service.uploadImage(file, "course")) |
| 87 | + .isInstanceOf(NotFoundException.class); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + @DisplayName("νμΌλͺ
μ΄ λΉ λ¬Έμμ΄μ΄λ©΄ NotFoundException") |
| 92 | + void νμΌλͺ
μ΄_λΉλ¬Έμμ΄() { |
| 93 | + MultipartFile file = new MockMultipartFile("image", "", "image/jpeg", "content".getBytes()); |
| 94 | + |
| 95 | + assertThatThrownBy(() -> s3Service.uploadImage(file, "course")) |
| 96 | + .isInstanceOf(NotFoundException.class); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + @DisplayName("[λ²κ·Έ μμ κ²μ¦] νμ₯μ(.)κ° μλ νμΌλͺ
μ΄λ©΄ 500(StringIndexOutOfBoundsException) λμ BadRequestException") |
| 101 | + void νμ₯μκ°_μλ_νμΌλͺ
() { |
| 102 | + MultipartFile file = new MockMultipartFile("image", "photo_without_extension", "image/jpeg", |
| 103 | + "content".getBytes()); |
| 104 | + |
| 105 | + assertThatThrownBy(() -> s3Service.uploadImage(file, "course")) |
| 106 | + .isInstanceOf(BadRequestException.class); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @DisplayName("νμΌ μ€νΈλ¦Όμ μ½λ μ€ μ€λ₯κ° λλ©΄ NotFoundException") |
| 111 | + void μ€νΈλ¦Ό_μ½κΈ°_μ€ν¨() throws IOException { |
| 112 | + MultipartFile file = mock(MultipartFile.class); |
| 113 | + when(file.getOriginalFilename()).thenReturn("photo.jpg"); |
| 114 | + when(file.getInputStream()).thenThrow(new IOException("disk error")); |
| 115 | + |
| 116 | + assertThatThrownBy(() -> s3Service.uploadImage(file, "course")) |
| 117 | + .isInstanceOf(NotFoundException.class); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments