From bec0b404cdfb81656d8296ed5c1b64710d793d05 Mon Sep 17 00:00:00 2001 From: hoa0217 Date: Thu, 4 Apr 2024 02:18:03 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20AmqpRejectAndDontRequeueException?= =?UTF-8?q?=20=EC=9C=BC=EB=A1=9C=20=EC=98=88=EC=99=B8=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 기존엔 MemberService에서 Member를 찾았으나 NotFoundException은 DLX로 보내지못함. 일단 Amqp 예외를 던지자. Related: #70 --- .../modoospace/alarm/service/AlarmService.java | 14 ++++++++------ .../alarm/consumer/AlarmConsumerTest.java | 15 ++++++--------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/modoospace/alarm/service/AlarmService.java b/src/main/java/com/modoospace/alarm/service/AlarmService.java index 009eb12..297ae65 100644 --- a/src/main/java/com/modoospace/alarm/service/AlarmService.java +++ b/src/main/java/com/modoospace/alarm/service/AlarmService.java @@ -10,18 +10,18 @@ import com.modoospace.common.exception.SSEConnectError; import com.modoospace.config.redis.aspect.CachePrefixEvict; import com.modoospace.member.domain.Member; -import com.modoospace.member.service.MemberService; +import com.modoospace.member.domain.MemberRepository; +import java.io.IOException; +import java.util.Optional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.AmqpRejectAndDontRequeueException; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; -import java.io.IOException; -import java.util.Optional; - @Slf4j @RequiredArgsConstructor @Service @@ -30,7 +30,7 @@ public class AlarmService { private static final Long DEFAULT_TIMEOUT = 60L * 1000 * 60; private static final String ALARM_NAME = "sse"; - private final MemberService memberService; + private final MemberRepository memberRepository; private final AlarmRepository alarmRepository; private final AlarmQueryRepository alarmQueryRepository; private final EmitterLocalCacheRepository emitterRepository; @@ -54,7 +54,9 @@ public SseEmitter connectAlarm(String loginEmail) { @Transactional @CachePrefixEvict(cacheNames = "searchAlarms", key = "#alarmEvent.email") public void saveAndSend(AlarmEvent alarmEvent) { - Member member = memberService.findMemberByEmail(alarmEvent.getEmail()); + Member member = memberRepository.findByEmail(alarmEvent.getEmail()) + .orElseThrow(() -> new AmqpRejectAndDontRequeueException( + "사용자" + alarmEvent.getEmail() + "를 찾을 수 없습니다.")); Alarm alarm = alarmRepository.save(alarmEvent.toEntity()); send(member.getEmail(), AlarmResponse.of(alarm)); diff --git a/src/test/java/com/modoospace/alarm/consumer/AlarmConsumerTest.java b/src/test/java/com/modoospace/alarm/consumer/AlarmConsumerTest.java index eac689e..62fd997 100644 --- a/src/test/java/com/modoospace/alarm/consumer/AlarmConsumerTest.java +++ b/src/test/java/com/modoospace/alarm/consumer/AlarmConsumerTest.java @@ -1,26 +1,25 @@ package com.modoospace.alarm.consumer; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + import com.modoospace.AbstractIntegrationContainerBaseTest; import com.modoospace.alarm.controller.dto.AlarmEvent; import com.modoospace.alarm.domain.Alarm; import com.modoospace.alarm.domain.AlarmRepository; import com.modoospace.alarm.domain.AlarmType; -import com.modoospace.common.exception.NotFoundEntityException; import com.modoospace.member.domain.Member; import com.modoospace.member.domain.MemberRepository; import com.modoospace.member.domain.Role; +import java.util.List; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.amqp.AmqpRejectAndDontRequeueException; import org.springframework.beans.factory.annotation.Autowired; import org.testcontainers.shaded.com.fasterxml.jackson.core.JsonProcessingException; import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - class AlarmConsumerTest extends AbstractIntegrationContainerBaseTest { @Autowired @@ -65,7 +64,6 @@ public void handler_ifMember_saveAlarm() throws JsonProcessingException { .containsExactly("test facility"); } - /* @DisplayName("Consumer는 가입되지 않은 멤버의 메시지를 소비할 경우 Exception을 던진다.") @Test public void handler_ifNotMember_throwException() throws JsonProcessingException { @@ -78,7 +76,6 @@ public void handler_ifNotMember_throwException() throws JsonProcessingException String message = objectMapper.writeValueAsString(alarmEvent); assertThatThrownBy(() -> alarmConsumer.handler(message)) - .isInstanceOf(NotFoundEntityException.class); + .isInstanceOf(AmqpRejectAndDontRequeueException.class); } - */ } \ No newline at end of file