From 09253f74f0290cc7378765c37fc1f3e42204218f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=9C=EC=9D=B4=EB=AF=B8?= <63184334+JJ503@users.noreply.github.com> Date: Fri, 15 Sep 2023 21:51:52 +0900 Subject: [PATCH] =?UTF-8?q?!hotfix:=20=EA=B2=BD=EB=A7=A4=20=EC=9D=B4?= =?UTF-8?q?=EB=AF=B8=EC=A7=80=20url=20=EC=B2=98=EB=A6=AC=EC=97=90=20?= =?UTF-8?q?=EB=B0=A9=EB=B2=95=20=EB=B3=80=EA=B2=BD=20(#385)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 경매 이미지 url 처리에 방법 변경 * test: 테스트 실패 문제 해결 --- .../ddang/image/application/ImageService.java | 21 ++++++++----- .../application/ProfileImageServiceTest.java | 30 ++++++++++--------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/backend/ddang/src/main/java/com/ddang/ddang/image/application/ImageService.java b/backend/ddang/src/main/java/com/ddang/ddang/image/application/ImageService.java index 486475cff..e871a2936 100644 --- a/backend/ddang/src/main/java/com/ddang/ddang/image/application/ImageService.java +++ b/backend/ddang/src/main/java/com/ddang/ddang/image/application/ImageService.java @@ -1,8 +1,7 @@ package com.ddang.ddang.image.application; -import com.ddang.ddang.image.domain.ProfileImage; -import com.ddang.ddang.image.application.exception.ImageNotFoundException; import com.ddang.ddang.image.domain.AuctionImage; +import com.ddang.ddang.image.domain.ProfileImage; import com.ddang.ddang.image.infrastructure.persistence.JpaAuctionImageRepository; import com.ddang.ddang.image.infrastructure.persistence.JpaImageRepository; import lombok.RequiredArgsConstructor; @@ -28,9 +27,12 @@ public class ImageService { public Resource readProfileImage(final Long id) throws MalformedURLException { final ProfileImage profileImage = imageRepository.findById(id) - .orElseThrow(() -> new ImageNotFoundException( - "지정한 이미지를 찾을 수 없습니다." - )); + .orElse(null); + + if (profileImage == null) { + return null; + } + final String fullPath = findFullPath(profileImage.getImage().getStoreName()); return new UrlResource(FILE_PROTOCOL_PREFIX + fullPath); @@ -38,9 +40,12 @@ public Resource readProfileImage(final Long id) throws MalformedURLException { public Resource readAuctionImage(final Long id) throws MalformedURLException { final AuctionImage auctionImage = auctionImageRepository.findById(id) - .orElseThrow(() -> new ImageNotFoundException( - "지정한 이미지를 찾을 수 없습니다." - )); + .orElse(null); + + if (auctionImage == null) { + return null; + } + final String fullPath = findFullPath(auctionImage.getImage().getStoreName()); return new UrlResource(FILE_PROTOCOL_PREFIX + fullPath); diff --git a/backend/ddang/src/test/java/com/ddang/ddang/image/application/ProfileImageServiceTest.java b/backend/ddang/src/test/java/com/ddang/ddang/image/application/ProfileImageServiceTest.java index 93f697550..cb206bcf5 100644 --- a/backend/ddang/src/test/java/com/ddang/ddang/image/application/ProfileImageServiceTest.java +++ b/backend/ddang/src/test/java/com/ddang/ddang/image/application/ProfileImageServiceTest.java @@ -1,10 +1,6 @@ package com.ddang.ddang.image.application; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import com.ddang.ddang.image.application.exception.ImageNotFoundException; import com.ddang.ddang.image.domain.AuctionImage; import com.ddang.ddang.image.domain.ProfileImage; import com.ddang.ddang.image.infrastructure.persistence.JpaAuctionImageRepository; @@ -17,6 +13,10 @@ import org.springframework.core.io.Resource; import org.springframework.transaction.annotation.Transactional; +import java.net.MalformedURLException; + +import static org.assertj.core.api.Assertions.assertThat; + @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @Transactional @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) @@ -47,14 +47,15 @@ class ProfileImageServiceTest { } @Test - void 지정한_아이디에_해당하는_이미지가_없는_경우_예외가_발생한다() { + void 지정한_아이디에_해당하는_이미지가_없는_경우_null을_반환한다() throws MalformedURLException { // given final Long invalidImageId = -999L; - // when & then - assertThatThrownBy(() -> imageService.readProfileImage(invalidImageId)) - .isInstanceOf(ImageNotFoundException.class) - .hasMessage("지정한 이미지를 찾을 수 없습니다."); + // when + final Resource actual = imageService.readProfileImage(invalidImageId); + + // then + assertThat(actual).isNull(); } @Test @@ -72,13 +73,14 @@ class ProfileImageServiceTest { } @Test - void 지정한_아이디에_해당하는_경매_이미지가_없는_경우_예외가_발생한다() { + void 지정한_아이디에_해당하는_경매_이미지가_없는_경우_null을_반환한다() throws MalformedURLException { // given final Long invalidAuctionImageId = -999L; - // when & then - assertThatThrownBy(() -> imageService.readAuctionImage(invalidAuctionImageId)) - .isInstanceOf(ImageNotFoundException.class) - .hasMessage("지정한 이미지를 찾을 수 없습니다."); + // when + final Resource actual = imageService.readAuctionImage(invalidAuctionImageId); + + // then + assertThat(actual).isNull(); } }