diff --git a/src/main/java/ject/componote/domain/comment/application/CommentImageEventListener.java b/src/main/java/ject/componote/domain/comment/application/CommentImageEventListener.java new file mode 100644 index 00000000..8fcc7e74 --- /dev/null +++ b/src/main/java/ject/componote/domain/comment/application/CommentImageEventListener.java @@ -0,0 +1,27 @@ +package ject.componote.domain.comment.application; + +import ject.componote.domain.comment.dto.image.event.CommentImageMoveEvent; +import ject.componote.domain.comment.model.CommentImage; +import ject.componote.infra.storage.application.StorageProducer; +import ject.componote.infra.storage.dto.move.request.ImageMoveRequest; +import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; +import org.springframework.transaction.event.TransactionPhase; +import org.springframework.transaction.event.TransactionalEventListener; + +@Component +@RequiredArgsConstructor +public class CommentImageEventListener { + private final StorageProducer storageProducer; + + @Async + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) + public void handleImageMove(final CommentImageMoveEvent event) { + final CommentImage image = event.image(); + if (image == null || image.isEmpty()) { + return; + } + storageProducer.sendImageMoveMessage(ImageMoveRequest.from(image)); + } +} diff --git a/src/main/java/ject/componote/domain/comment/application/CommentReplyCountEventHandler.java b/src/main/java/ject/componote/domain/comment/application/CommentReplyCountEventListener.java similarity index 96% rename from src/main/java/ject/componote/domain/comment/application/CommentReplyCountEventHandler.java rename to src/main/java/ject/componote/domain/comment/application/CommentReplyCountEventListener.java index 0f7b8746..22b719f6 100644 --- a/src/main/java/ject/componote/domain/comment/application/CommentReplyCountEventHandler.java +++ b/src/main/java/ject/componote/domain/comment/application/CommentReplyCountEventListener.java @@ -15,7 +15,7 @@ @Component @RequiredArgsConstructor -public class CommentReplyCountEventHandler { +public class CommentReplyCountEventListener { private final CommentRepository commentRepository; @Async diff --git a/src/main/java/ject/componote/domain/comment/application/CommentReportEventHandler.java b/src/main/java/ject/componote/domain/comment/application/CommentReportEventListener.java similarity index 96% rename from src/main/java/ject/componote/domain/comment/application/CommentReportEventHandler.java rename to src/main/java/ject/componote/domain/comment/application/CommentReportEventListener.java index a7c8c273..3f1011dd 100644 --- a/src/main/java/ject/componote/domain/comment/application/CommentReportEventHandler.java +++ b/src/main/java/ject/componote/domain/comment/application/CommentReportEventListener.java @@ -13,7 +13,7 @@ @Component @RequiredArgsConstructor @Transactional(readOnly = true) -public class CommentReportEventHandler { +public class CommentReportEventListener { private final CommentRepository commentRepository; @Async diff --git a/src/main/java/ject/componote/domain/comment/application/CommentService.java b/src/main/java/ject/componote/domain/comment/application/CommentService.java index 51c829a0..682111b5 100644 --- a/src/main/java/ject/componote/domain/comment/application/CommentService.java +++ b/src/main/java/ject/componote/domain/comment/application/CommentService.java @@ -10,6 +10,7 @@ import ject.componote.domain.comment.dto.find.response.CommentFindByComponentResponse; import ject.componote.domain.comment.dto.find.response.CommentFindByMemberResponse; import ject.componote.domain.comment.dto.find.response.CommentFindByParentResponse; +import ject.componote.domain.comment.dto.image.event.CommentImageMoveEvent; import ject.componote.domain.comment.dto.reply.event.CommentReplyCountIncreaseEvent; import ject.componote.domain.comment.dto.reply.event.CommentReplyNotificationEvent; import ject.componote.domain.comment.dto.update.request.CommentUpdateRequest; @@ -19,7 +20,10 @@ import ject.componote.domain.comment.model.CommentImage; import ject.componote.domain.comment.validation.CommenterValidation; import ject.componote.domain.common.dto.response.PageResponse; -import ject.componote.infra.storage.application.StorageService; +import ject.componote.domain.component.dao.ComponentRepository; +import ject.componote.domain.component.dto.event.ComponentCommentCountDecreaseEvent; +import ject.componote.domain.component.dto.event.ComponentCommentCountIncreaseEvent; +import ject.componote.domain.component.error.NotFoundComponentException; import lombok.RequiredArgsConstructor; import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.domain.Page; @@ -33,16 +37,17 @@ public class CommentService { private final ApplicationEventPublisher eventPublisher; private final CommentRepository commentRepository; - private final StorageService storageService; + private final ComponentRepository componentRepository; @Transactional public CommentCreateResponse create(final AuthPrincipal authPrincipal, final CommentCreateRequest request) { + validateComponentId(request); validateParentId(request); - final Comment comment = commentRepository.save( - CommentCreationStrategy.createBy(request, authPrincipal.id()) - ); - storageService.moveImage(comment.getImage()); + final Comment comment = commentRepository.save(CommentCreationStrategy.createBy(request, authPrincipal.id())); + // 아래 코드 이벤트 계층을 분리하여 리펙토링 예정 + eventPublisher.publishEvent(CommentImageMoveEvent.from(comment)); + eventPublisher.publishEvent(ComponentCommentCountIncreaseEvent.from(comment)); if (isReply(request)) { eventPublisher.publishEvent(CommentReplyCountIncreaseEvent.from(comment)); eventPublisher.publishEvent(CommentReplyNotificationEvent.from(comment)); @@ -80,7 +85,7 @@ public void update(final AuthPrincipal authPrincipal, final Long commentId, fina final CommentImage image = CommentImage.from(commentUpdateRequest.imageObjectKey()); if (!comment.equalsImage(image)) { - storageService.moveImage(image); + eventPublisher.publishEvent(CommentImageMoveEvent.from(comment)); } final CommentContent content = CommentContent.from(commentUpdateRequest.content()); // 가비지가 발생하지 않을까? @@ -90,7 +95,17 @@ public void update(final AuthPrincipal authPrincipal, final Long commentId, fina @CommenterValidation @Transactional public void delete(final AuthPrincipal authPrincipal, final Long commentId) { - commentRepository.deleteByIdAndMemberId(commentId, authPrincipal.id()); + final Long memberId = authPrincipal.id(); + final Comment comment = findCommentByIdAndMemberId(commentId, memberId); // 로직 수정 예정 + commentRepository.deleteByIdAndMemberId(commentId, memberId); + eventPublisher.publishEvent(ComponentCommentCountDecreaseEvent.from(comment)); + } + + private void validateComponentId(final CommentCreateRequest request) { + final Long componentId = request.componentId(); + if (!componentRepository.existsById(componentId)) { + throw new NotFoundComponentException(componentId); + } } private Comment findCommentByIdAndMemberId(final Long commentId, final Long memberId) { @@ -128,8 +143,9 @@ private void validateParentId(final CommentCreateRequest request) { } final Long parentId = request.parentId(); - if (!commentRepository.existsById(parentId)) { - throw new NotFoundParentCommentException(parentId); + final Long componentId = request.componentId(); + if (!commentRepository.existsByIdAndComponentId(parentId, componentId)) { + throw new NotFoundParentCommentException(); } } } diff --git a/src/main/java/ject/componote/domain/comment/dao/CommentRepository.java b/src/main/java/ject/componote/domain/comment/dao/CommentRepository.java index bb04504b..e2f39bd7 100644 --- a/src/main/java/ject/componote/domain/comment/dao/CommentRepository.java +++ b/src/main/java/ject/componote/domain/comment/dao/CommentRepository.java @@ -8,5 +8,6 @@ public interface CommentRepository extends JpaRepository, CommentQueryDsl { Optional findByIdAndMemberId(final Long id, final Long memberId); boolean existsByIdAndMemberId(final Long id, final Long memberId); + boolean existsByIdAndComponentId(final Long id, final Long componentId); void deleteByIdAndMemberId(final Long commentId, final Long memberId); } diff --git a/src/main/java/ject/componote/domain/comment/dto/image/event/CommentImageMoveEvent.java b/src/main/java/ject/componote/domain/comment/dto/image/event/CommentImageMoveEvent.java new file mode 100644 index 00000000..6838f5fb --- /dev/null +++ b/src/main/java/ject/componote/domain/comment/dto/image/event/CommentImageMoveEvent.java @@ -0,0 +1,10 @@ +package ject.componote.domain.comment.dto.image.event; + +import ject.componote.domain.comment.domain.Comment; +import ject.componote.domain.comment.model.CommentImage; + +public record CommentImageMoveEvent(CommentImage image) { + public static CommentImageMoveEvent from(final Comment comment) { + return new CommentImageMoveEvent(comment.getImage()); + } +} diff --git a/src/main/java/ject/componote/domain/comment/error/NotFoundParentCommentException.java b/src/main/java/ject/componote/domain/comment/error/NotFoundParentCommentException.java index dcace58d..c8829dee 100644 --- a/src/main/java/ject/componote/domain/comment/error/NotFoundParentCommentException.java +++ b/src/main/java/ject/componote/domain/comment/error/NotFoundParentCommentException.java @@ -3,7 +3,7 @@ import org.springframework.http.HttpStatus; public class NotFoundParentCommentException extends CommentException { - public NotFoundParentCommentException(final Long parentId) { - super("일치하는 부모 댓글을 찾을 수 없습니다. 댓글 ID: " + parentId, HttpStatus.NOT_FOUND); + public NotFoundParentCommentException() { + super("컴포넌트에 부모 댓글이 존재하지 않습니다.", HttpStatus.NOT_FOUND); } } diff --git a/src/main/java/ject/componote/domain/component/application/ComponentCommentCountEventListener.java b/src/main/java/ject/componote/domain/component/application/ComponentCommentCountEventListener.java new file mode 100644 index 00000000..8ca0a847 --- /dev/null +++ b/src/main/java/ject/componote/domain/component/application/ComponentCommentCountEventListener.java @@ -0,0 +1,35 @@ +package ject.componote.domain.component.application; + +import ject.componote.domain.component.dao.ComponentRepository; +import ject.componote.domain.component.domain.Component; +import ject.componote.domain.component.dto.event.ComponentCommentCountDecreaseEvent; +import ject.componote.domain.component.dto.event.ComponentCommentCountIncreaseEvent; +import ject.componote.domain.component.error.NotFoundComponentException; +import lombok.RequiredArgsConstructor; +import org.springframework.transaction.event.TransactionPhase; +import org.springframework.transaction.event.TransactionalEventListener; + +@org.springframework.stereotype.Component +@RequiredArgsConstructor +public class ComponentCommentCountEventListener { + private final ComponentRepository componentRepository; + + @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT) // cf) Spirng은 내부적으로 트랜잭션 정보를 ThreadLocal 변수에 저장하기 때문에 다른 쓰레드로 트랜잭션이 전파되지 않는다. + public void handleCommentCountIncreaseEvent(final ComponentCommentCountIncreaseEvent event) { + final Long componentId = event.componentId(); + final Component component = findComponentById(componentId); + component.increaseCommentCount(); + } + + @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT) + public void handleCommentCountDecreaseEvent(final ComponentCommentCountDecreaseEvent event) { + final Long componentId = event.componentId(); + final Component component = findComponentById(componentId); + component.decreaseCommentCount(); + } + + private Component findComponentById(final Long componentId) { + return componentRepository.findById(componentId) + .orElseThrow(() -> new NotFoundComponentException(componentId)); + } +} diff --git a/src/main/java/ject/componote/domain/component/application/ComponentService.java b/src/main/java/ject/componote/domain/component/application/ComponentService.java index 1311412f..3cddc8f4 100644 --- a/src/main/java/ject/componote/domain/component/application/ComponentService.java +++ b/src/main/java/ject/componote/domain/component/application/ComponentService.java @@ -7,7 +7,7 @@ import ject.componote.domain.component.dao.ComponentSummaryDao; import ject.componote.domain.component.domain.Component; import ject.componote.domain.component.domain.ComponentType; -import ject.componote.domain.component.dto.find.event.ComponentViewCountIncreaseEvent; +import ject.componote.domain.component.dto.event.ComponentViewCountIncreaseEvent; import ject.componote.domain.component.dto.find.request.ComponentSearchRequest; import ject.componote.domain.component.dto.find.response.ComponentDetailResponse; import ject.componote.domain.component.dto.find.response.ComponentSummaryResponse; diff --git a/src/main/java/ject/componote/domain/component/application/ComponentViewCountEventHandler.java b/src/main/java/ject/componote/domain/component/application/ComponentViewCountEventListener.java similarity index 89% rename from src/main/java/ject/componote/domain/component/application/ComponentViewCountEventHandler.java rename to src/main/java/ject/componote/domain/component/application/ComponentViewCountEventListener.java index 52169b03..a15c913e 100644 --- a/src/main/java/ject/componote/domain/component/application/ComponentViewCountEventHandler.java +++ b/src/main/java/ject/componote/domain/component/application/ComponentViewCountEventListener.java @@ -2,7 +2,7 @@ import ject.componote.domain.component.dao.ComponentRepository; import ject.componote.domain.component.domain.Component; -import ject.componote.domain.component.dto.find.event.ComponentViewCountIncreaseEvent; +import ject.componote.domain.component.dto.event.ComponentViewCountIncreaseEvent; import ject.componote.domain.component.error.NotFoundComponentException; import lombok.RequiredArgsConstructor; import org.springframework.context.event.EventListener; @@ -11,7 +11,7 @@ @org.springframework.stereotype.Component @RequiredArgsConstructor -public class ComponentViewCountEventHandler { +public class ComponentViewCountEventListener { private final ComponentRepository componentRepository; @Async diff --git a/src/main/java/ject/componote/domain/component/domain/Component.java b/src/main/java/ject/componote/domain/component/domain/Component.java index d98df419..74d6d31a 100644 --- a/src/main/java/ject/componote/domain/component/domain/Component.java +++ b/src/main/java/ject/componote/domain/component/domain/Component.java @@ -85,6 +85,14 @@ public void increaseViewCount() { this.viewCount.increase(); } + public void increaseCommentCount() { + this.commentCount.increase(); + } + + public void decreaseCommentCount() { + this.commentCount.decrease(); + } + private List parseMixedNames(final List mixedNames) { return mixedNames.stream() .map(MixedName::from) diff --git a/src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountDecreaseEvent.java b/src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountDecreaseEvent.java new file mode 100644 index 00000000..2e8cc61e --- /dev/null +++ b/src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountDecreaseEvent.java @@ -0,0 +1,9 @@ +package ject.componote.domain.component.dto.event; + +import ject.componote.domain.comment.domain.Comment; + +public record ComponentCommentCountDecreaseEvent(Long componentId) { + public static ComponentCommentCountDecreaseEvent from(final Comment comment) { + return new ComponentCommentCountDecreaseEvent(comment.getComponentId()); + } +} diff --git a/src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountIncreaseEvent.java b/src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountIncreaseEvent.java new file mode 100644 index 00000000..278d556b --- /dev/null +++ b/src/main/java/ject/componote/domain/component/dto/event/ComponentCommentCountIncreaseEvent.java @@ -0,0 +1,9 @@ +package ject.componote.domain.component.dto.event; + +import ject.componote.domain.comment.domain.Comment; + +public record ComponentCommentCountIncreaseEvent(Long componentId) { + public static ComponentCommentCountIncreaseEvent from(final Comment comment) { + return new ComponentCommentCountIncreaseEvent(comment.getComponentId()); + } +} diff --git a/src/main/java/ject/componote/domain/component/dto/find/event/ComponentViewCountIncreaseEvent.java b/src/main/java/ject/componote/domain/component/dto/event/ComponentViewCountIncreaseEvent.java similarity index 83% rename from src/main/java/ject/componote/domain/component/dto/find/event/ComponentViewCountIncreaseEvent.java rename to src/main/java/ject/componote/domain/component/dto/event/ComponentViewCountIncreaseEvent.java index 15315f7b..60cf2bca 100644 --- a/src/main/java/ject/componote/domain/component/dto/find/event/ComponentViewCountIncreaseEvent.java +++ b/src/main/java/ject/componote/domain/component/dto/event/ComponentViewCountIncreaseEvent.java @@ -1,4 +1,4 @@ -package ject.componote.domain.component.dto.find.event; +package ject.componote.domain.component.dto.event; import ject.componote.domain.component.domain.Component; diff --git a/src/test/java/ject/componote/domain/comment/application/CommentReplyCountEventListenerTest.java b/src/test/java/ject/componote/domain/comment/application/CommentReplyCountEventListenerTest.java new file mode 100644 index 00000000..86f5cd16 --- /dev/null +++ b/src/test/java/ject/componote/domain/comment/application/CommentReplyCountEventListenerTest.java @@ -0,0 +1,74 @@ +//package ject.componote.domain.comment.application; +// +//import ject.componote.domain.auth.domain.Member; +//import ject.componote.domain.comment.dao.CommentRepository; +//import ject.componote.domain.comment.domain.Comment; +//import ject.componote.domain.comment.dto.reply.event.CommentReplyCountIncreaseEvent; +//import ject.componote.domain.common.model.Count; +//import ject.componote.fixture.CommentFixture; +//import org.junit.jupiter.api.DisplayName; +//import org.junit.jupiter.api.Test; +//import org.junit.jupiter.api.extension.ExtendWith; +//import org.junit.jupiter.params.ParameterizedTest; +//import org.junit.jupiter.params.provider.EnumSource; +//import org.mockito.InjectMocks; +//import org.mockito.Mock; +//import org.mockito.junit.jupiter.MockitoExtension; +// +//import java.util.Optional; +// +//import static ject.componote.fixture.CommentFixture.답글_이미지O; +//import static ject.componote.fixture.MemberFixture.이메일X_회원; +//import static org.assertj.core.api.Assertions.assertThat; +//import static org.mockito.Mockito.doReturn; +//import static org.mockito.Mockito.spy; +// +//@ExtendWith(MockitoExtension.class) +//class CommentReplyCountEventListenerTest { +// @Mock +// CommentRepository commentRepository; +// +// @InjectMocks +// CommentReplyCountEventListener commentReplyCountEventListener; +// +// Member member = 이메일X_회원.생성(1L); +// +// @ParameterizedTest +// @DisplayName("대댓글 개수 증가 이벤트 처리") +// @EnumSource(value = CommentFixture.class) +// public void handleCommentReplyCountIncreaseEvent(final CommentFixture fixture) throws Exception { +// // given +// final Long memberId = member.getId(); +// final Comment parent = fixture.생성(memberId); +// final Long parentId = parent.getParentId(); +// +// final Comment comment = spy(답글_이미지O.생성()); +// doReturn(parentId).when(comment) +// .getParentId(); +// +// final CommentReplyCountIncreaseEvent event = CommentReplyCountIncreaseEvent.from(comment); +// final Count previousReplyCount = parent.getReplyCount(); +// +// // when +// doReturn(Optional.of(parent)).when(commentRepository) +// .findById(parent.getId()); +// commentReplyCountEventListener.handleCommentReplyCountIncreaseEvent(event); +// +// // then +// final Count currentReplyCount = parent.getReplyCount(); +// previousReplyCount.increase(); +// assertThat(currentReplyCount).isEqualTo(previousReplyCount); +// } +// +// @Test +// @DisplayName("대댓글 수 증가 이벤트 처리 시 댓글 ID가 잘못된 경우 예외 발생") +// public void handleCommentReplyCountIncreaseEventWhenInvalidCommentId() throws Exception { +// +// // given +// +// // when +// +// // then +// +// } +//} \ No newline at end of file diff --git a/src/test/java/ject/componote/domain/comment/application/CommentServiceTest.java b/src/test/java/ject/componote/domain/comment/application/CommentServiceTest.java index c8409bf8..f07bc0f1 100644 --- a/src/test/java/ject/componote/domain/comment/application/CommentServiceTest.java +++ b/src/test/java/ject/componote/domain/comment/application/CommentServiceTest.java @@ -13,6 +13,7 @@ import ject.componote.domain.comment.dto.create.response.CommentCreateResponse; import ject.componote.domain.comment.dto.find.response.CommentFindByComponentResponse; import ject.componote.domain.comment.dto.find.response.CommentFindByMemberResponse; +import ject.componote.domain.comment.dto.image.event.CommentImageMoveEvent; import ject.componote.domain.comment.dto.reply.event.CommentReplyCountIncreaseEvent; import ject.componote.domain.comment.dto.reply.event.CommentReplyNotificationEvent; import ject.componote.domain.comment.dto.update.request.CommentUpdateRequest; @@ -21,8 +22,11 @@ import ject.componote.domain.comment.model.CommentImage; import ject.componote.domain.common.dto.response.PageResponse; import ject.componote.domain.common.model.Count; +import ject.componote.domain.component.dao.ComponentRepository; +import ject.componote.domain.component.dto.event.ComponentCommentCountDecreaseEvent; +import ject.componote.domain.component.dto.event.ComponentCommentCountIncreaseEvent; +import ject.componote.domain.component.error.NotFoundComponentException; import ject.componote.fixture.CommentFixture; -import ject.componote.infra.storage.application.StorageService; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -59,7 +63,7 @@ class CommentServiceTest { CommentRepository commentRepository; @Mock - StorageService storageService; + ComponentRepository componentRepository; @InjectMocks CommentService commentService; @@ -74,6 +78,7 @@ class CommentServiceTest { public void create(final CommentFixture fixture) throws Exception { // given final CommentCreateRequest createRequest = fixture.toCreateRequest(); + final Long componentId = createRequest.componentId(); final Comment comment = fixture.생성(authPrincipal.id()); final CommentCreateResponse expect = CommentCreateResponse.from(comment); @@ -88,10 +93,14 @@ public void create(final CommentFixture fixture) throws Exception { .publishEvent(CommentReplyNotificationEvent.from(comment)); } + doReturn(true).when(componentRepository) + .existsById(componentId); doReturn(comment).when(commentRepository) .save(any()); - doNothing().when(storageService) - .moveImage(comment.getImage()); + doNothing().when(eventPublisher) + .publishEvent(ComponentCommentCountIncreaseEvent.from(comment)); + doNothing().when(eventPublisher) + .publishEvent(CommentImageMoveEvent.from(comment)); final CommentCreateResponse actual = commentService.create(authPrincipal, createRequest); // then @@ -103,16 +112,35 @@ public void create(final CommentFixture fixture) throws Exception { public void createWhenInvalidParentId() { // given final CommentCreateRequest createRequest = 답글_이미지X.toCreateRequest(); + final Long componentId = createRequest.componentId(); final Long parentId = createRequest.parentId(); // when doReturn(false).when(commentRepository) .existsById(parentId); + doReturn(true).when(componentRepository) + .existsById(componentId); + // then assertThatThrownBy(() -> commentService.create(authPrincipal, createRequest)) .isInstanceOf(NotFoundParentCommentException.class); } + @Test + @DisplayName("댓글 생성시 잘못된 componentId가 입력된 경우 예외 발생") + public void createWhenInvalidComponentId() { + // given + final CommentCreateRequest createRequest = 답글_이미지X.toCreateRequest(); + final Long componentId = createRequest.componentId(); + + // when + doReturn(false).when(componentRepository) + .existsById(componentId); + // then + assertThatThrownBy(() -> commentService.create(authPrincipal, createRequest)) + .isInstanceOf(NotFoundComponentException.class); + } + @Test @DisplayName("마이 페이지 댓글 페이징 조회") public void getCommentsByMemberId() throws Exception { @@ -198,6 +226,8 @@ public void updateImage(final CommentFixture fixture) throws Exception { // when doReturn(Optional.of(comment)).when(commentRepository) .findByIdAndMemberId(commentId, memberId); + doNothing().when(eventPublisher) + .publishEvent(CommentImageMoveEvent.from(comment)); // then assertDoesNotThrow( @@ -244,6 +274,8 @@ public void updateAll(final CommentFixture fixture) throws Exception { // when doReturn(Optional.of(comment)).when(commentRepository) .findByIdAndMemberId(commentId, memberId); + doNothing().when(eventPublisher) + .publishEvent(CommentImageMoveEvent.from(comment)); // then assertDoesNotThrow( @@ -263,8 +295,12 @@ public void delete(final CommentFixture fixture) throws Exception { final Long memberId = authPrincipal.id(); // when + doReturn(Optional.of(comment)).when(commentRepository) + .findByIdAndMemberId(commentId, memberId); doNothing().when(commentRepository) .deleteByIdAndMemberId(commentId, memberId); + doNothing().when(eventPublisher) + .publishEvent(ComponentCommentCountDecreaseEvent.from(comment)); // then assertDoesNotThrow( diff --git a/src/test/java/ject/componote/domain/component/application/ComponentServiceTest.java b/src/test/java/ject/componote/domain/component/application/ComponentServiceTest.java index 0f0c1ff5..ccd0e400 100644 --- a/src/test/java/ject/componote/domain/component/application/ComponentServiceTest.java +++ b/src/test/java/ject/componote/domain/component/application/ComponentServiceTest.java @@ -7,7 +7,7 @@ import ject.componote.domain.component.dao.ComponentSummaryDao; import ject.componote.domain.component.domain.Component; import ject.componote.domain.component.domain.ComponentType; -import ject.componote.domain.component.dto.find.event.ComponentViewCountIncreaseEvent; +import ject.componote.domain.component.dto.event.ComponentViewCountIncreaseEvent; import ject.componote.domain.component.dto.find.request.ComponentSearchRequest; import ject.componote.domain.component.dto.find.response.ComponentDetailResponse; import ject.componote.domain.component.dto.find.response.ComponentSummaryResponse; diff --git a/src/test/java/ject/componote/domain/component/application/ComponentViewCountEventHandlerTest.java b/src/test/java/ject/componote/domain/component/application/ComponentViewCountEventListenerTest.java similarity index 85% rename from src/test/java/ject/componote/domain/component/application/ComponentViewCountEventHandlerTest.java rename to src/test/java/ject/componote/domain/component/application/ComponentViewCountEventListenerTest.java index 861789d0..e746ed04 100644 --- a/src/test/java/ject/componote/domain/component/application/ComponentViewCountEventHandlerTest.java +++ b/src/test/java/ject/componote/domain/component/application/ComponentViewCountEventListenerTest.java @@ -3,7 +3,7 @@ import ject.componote.domain.common.model.Count; import ject.componote.domain.component.dao.ComponentRepository; import ject.componote.domain.component.domain.Component; -import ject.componote.domain.component.dto.find.event.ComponentViewCountIncreaseEvent; +import ject.componote.domain.component.dto.event.ComponentViewCountIncreaseEvent; import ject.componote.domain.component.error.NotFoundComponentException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -20,12 +20,12 @@ import static org.mockito.Mockito.doReturn; @ExtendWith(MockitoExtension.class) -class ComponentViewCountEventHandlerTest { +class ComponentViewCountEventListenerTest { @Mock ComponentRepository componentRepository; @InjectMocks - ComponentViewCountEventHandler componentViewCountEventHandler; + ComponentViewCountEventListener componentViewCountEventListener; Component component = INPUT_COMPONENT.생성(); @@ -40,7 +40,7 @@ public void handleViewCountIncrease() throws Exception { // when doReturn(Optional.of(component)).when(componentRepository) .findById(componentId); - componentViewCountEventHandler.handleViewCountIncrease(event); + componentViewCountEventListener.handleViewCountIncrease(event); // then final Count newViewCount = component.getViewCount(); @@ -60,7 +60,7 @@ public void handleViewCountIncreaseWhenInvalidComponentId() throws Exception { .findById(componentId); // then - assertThatThrownBy(() -> componentViewCountEventHandler.handleViewCountIncrease(event)) + assertThatThrownBy(() -> componentViewCountEventListener.handleViewCountIncrease(event)) .isInstanceOf(NotFoundComponentException.class); }