From 882fd09f9af0ead06b98d65d0ee2b9f483981edd Mon Sep 17 00:00:00 2001 From: java-saeng Date: Sun, 1 Oct 2023 22:05:13 +0900 Subject: [PATCH] =?UTF-8?q?test=20:=20NotificationEventListenerTest=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=ED=95=98=EA=B3=A0=20Event=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20Event=20publish=20=ED=86=B5=ED=95=A9=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #663 --- .../EventServiceEventIntegrationTest.java | 168 ++++++++++++++++++ .../NotificationEventListenerTest.java | 87 --------- 2 files changed, 168 insertions(+), 87 deletions(-) create mode 100644 backend/emm-sale/src/test/java/com/emmsale/event/application/EventServiceEventIntegrationTest.java delete mode 100644 backend/emm-sale/src/test/java/com/emmsale/notification/application/NotificationEventListenerTest.java diff --git a/backend/emm-sale/src/test/java/com/emmsale/event/application/EventServiceEventIntegrationTest.java b/backend/emm-sale/src/test/java/com/emmsale/event/application/EventServiceEventIntegrationTest.java new file mode 100644 index 000000000..b2d192789 --- /dev/null +++ b/backend/emm-sale/src/test/java/com/emmsale/event/application/EventServiceEventIntegrationTest.java @@ -0,0 +1,168 @@ +package com.emmsale.event.application; + +import static com.emmsale.tag.TagFixture.IOS; +import static com.emmsale.tag.TagFixture.백엔드; +import static com.emmsale.tag.TagFixture.안드로이드; +import static com.emmsale.tag.TagFixture.프론트엔드; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import com.emmsale.event.application.dto.EventDetailRequest; +import com.emmsale.event.domain.EventMode; +import com.emmsale.event.domain.EventType; +import com.emmsale.event.domain.PaymentType; +import com.emmsale.helper.ServiceIntegrationTestHelper; +import com.emmsale.member.domain.InterestTag; +import com.emmsale.member.domain.InterestTagRepository; +import com.emmsale.member.domain.Member; +import com.emmsale.member.domain.MemberRepository; +import com.emmsale.notification.domain.Notification; +import com.emmsale.tag.application.dto.TagRequest; +import com.emmsale.tag.domain.Tag; +import com.emmsale.tag.domain.TagRepository; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.List; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +class EventServiceEventIntegrationTest extends ServiceIntegrationTestHelper { + + @Autowired + private EventService eventService; + @Autowired + private InterestTagRepository interestTagRepository; + @Autowired + private MemberRepository memberRepository; + @Autowired + private TagRepository tagRepository; + + @Test + @DisplayName("publish(Event) : 행사 이벤트가 발행되면 성공적으로 Event가 Listen 될 수 있다.") + void test_publish_event() throws Exception { + //given + //member1 -> 안드로이드, member2 -> 안드로이드 백엔드, member3 -> 프론트엔드 + //안드로이드, 백엔드 태그를 가진 Event 추가 + //member1, member2만 알림이 감 + + final Member member1 = memberRepository.findById(1L).get(); + final Member member2 = memberRepository.findById(2L).get(); + final Member member3 = memberRepository.save(new Member(1444L, "imageUrl", "username")); + final Tag 안드로이드 = tagRepository.save(안드로이드()); + final Tag 백엔드 = tagRepository.save(백엔드()); + final Tag 프론트엔드 = tagRepository.save(프론트엔드()); + interestTagRepository.save(new InterestTag(member1, 안드로이드)); + interestTagRepository.save(new InterestTag(member2, 안드로이드)); + interestTagRepository.save(new InterestTag(member2, 백엔드)); + interestTagRepository.save(new InterestTag(member3, 프론트엔드)); + + final EventDetailRequest eventDetailRequest = new EventDetailRequest( + "인프콘 2023", + "코엑스", + "https://~~~", + LocalDateTime.parse("2023-06-01T12:00:00"), + LocalDateTime.parse("2023-09-01T12:00:00"), + LocalDateTime.parse("2023-05-01T12:00:00"), + LocalDateTime.parse("2023-06-01T12:00:00"), + List.of( + new TagRequest(안드로이드().getName()), + new TagRequest(백엔드().getName()) + ), // Assuming you don't have a direct TagRequest list from Event. + "https://image.url", + EventType.CONFERENCE, + EventMode.ON_OFFLINE, + PaymentType.FREE_PAID, + "인프런" + ); + + //when + eventService.addEvent(eventDetailRequest, null, LocalDate.now()); + + //then + verify(firebaseCloudMessageClient, times(2)) + .sendMessageTo(any(Notification.class), anyLong()); + } + + @Test + @DisplayName("publish(Event) : 행사 생성 이벤트가 발생했을 때, 사용자가 관심있어하는 태그가 행사에 없을 경우 알림이 가지 않는다.") + void test_publish_event_no_notification_event_has_no_interest_tag() throws Exception { + //given + //member1 -> 안드로이드, member2 -> 안드로이드 백엔드, member3 -> 프론트엔드 + //IOS 태그를 가진 Event 추가 + //아무도 알림이 안감 + final Member member1 = memberRepository.findById(1L).get(); + final Member member2 = memberRepository.findById(2L).get(); + final Member member3 = memberRepository.save(new Member(1444L, "imageUrl", "username")); + final Tag 안드로이드 = tagRepository.save(안드로이드()); + final Tag 백엔드 = tagRepository.save(백엔드()); + final Tag 프론트엔드 = tagRepository.save(프론트엔드()); + final Tag IOS = tagRepository.save(IOS()); + interestTagRepository.save(new InterestTag(member1, 안드로이드)); + interestTagRepository.save(new InterestTag(member2, 안드로이드)); + interestTagRepository.save(new InterestTag(member2, 백엔드)); + interestTagRepository.save(new InterestTag(member3, 프론트엔드)); + + final EventDetailRequest eventDetailRequest = new EventDetailRequest( + "인프콘 2023", + "코엑스", + "https://~~~", + LocalDateTime.parse("2023-06-01T12:00:00"), + LocalDateTime.parse("2023-09-01T12:00:00"), + LocalDateTime.parse("2023-05-01T12:00:00"), + LocalDateTime.parse("2023-06-01T12:00:00"), + List.of( + new TagRequest(IOS().getName()) + ), // Assuming you don't have a direct TagRequest list from Event. + "https://image.url", + EventType.CONFERENCE, + EventMode.ON_OFFLINE, + PaymentType.FREE_PAID, + "인프런" + ); + + //when + eventService.addEvent(eventDetailRequest, null, LocalDate.now()); + + //then + verify(firebaseCloudMessageClient, times(0)) + .sendMessageTo(any(Notification.class), anyLong()); + } + + @Test + @DisplayName("publish(Event) : 행사 생성 이벤트가 발생했을 때, 사용자가 관심있어하는 태그가 없을 경우 알림이 가지 않는다.") + void test_publish_event_no_notification_member_has_no_interest_tag() throws Exception { + //given + //member1 -> 태그 없음 + //IOS 태그를 가진 Event 추가 + //아무도 알림이 안감 + tagRepository.save(IOS()); + + final EventDetailRequest eventDetailRequest = new EventDetailRequest( + "인프콘 2023", + "코엑스", + "https://~~~", + LocalDateTime.parse("2023-06-01T12:00:00"), + LocalDateTime.parse("2023-09-01T12:00:00"), + LocalDateTime.parse("2023-05-01T12:00:00"), + LocalDateTime.parse("2023-06-01T12:00:00"), + List.of( + new TagRequest(IOS().getName()) + ), // Assuming you don't have a direct TagRequest list from Event. + "https://image.url", + EventType.CONFERENCE, + EventMode.ON_OFFLINE, + PaymentType.FREE_PAID, + "인프런" + ); + + //when + eventService.addEvent(eventDetailRequest, null, LocalDate.now()); + + //then + verify(firebaseCloudMessageClient, times(0)) + .sendMessageTo(any(Notification.class), anyLong()); + } +} diff --git a/backend/emm-sale/src/test/java/com/emmsale/notification/application/NotificationEventListenerTest.java b/backend/emm-sale/src/test/java/com/emmsale/notification/application/NotificationEventListenerTest.java deleted file mode 100644 index f86abb9e3..000000000 --- a/backend/emm-sale/src/test/java/com/emmsale/notification/application/NotificationEventListenerTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.emmsale.notification.application; - -import static org.junit.jupiter.api.Assertions.assertAll; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import com.emmsale.event_publisher.MessageNotificationEvent; -import com.emmsale.event_publisher.UpdateNotificationEvent; -import com.emmsale.helper.ServiceIntegrationTestHelper; -import com.emmsale.notification.domain.NotificationRepository; -import com.emmsale.notification.domain.UpdateNotification; -import com.emmsale.notification.domain.UpdateNotificationRepository; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.time.LocalDateTime; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; - -class NotificationEventListenerTest extends ServiceIntegrationTestHelper { - - @Autowired - private NotificationEventListener notificationEventListener; - @Autowired - private UpdateNotificationRepository updateNotificationRepository; - private FirebaseCloudMessageClient mockingFirebaseCloudMessageClient; - @Autowired - private NotificationRepository notificationRepository; - @Autowired - private ObjectMapper objectMapper; - - @BeforeEach - void setUp() { - mockingFirebaseCloudMessageClient = mock(FirebaseCloudMessageClient.class); - - notificationEventListener = new NotificationEventListener( - updateNotificationRepository, mockingFirebaseCloudMessageClient, - notificationRepository, objectMapper - ); - } - - @Test - @DisplayName("createUpdateNotification() : UpdateNotificationEvent 이벤트가 발생했을 때 해당 메서드가 호출된다.") - void test_createUpdateNotification() throws Exception { - //given - final UpdateNotificationEvent event = new UpdateNotificationEvent( - 1L, - 2L, - "comment", - LocalDateTime.now() - ); - - //when - notificationEventListener.createUpdateNotification(event); - - //then - assertAll( - () -> assertEquals(1, updateNotificationRepository.findAll().size()), - () -> verify(mockingFirebaseCloudMessageClient, times(1)) - .sendMessageTo((UpdateNotification) any()) - ); - } - - @Test - @DisplayName("createMessageNotification() : MessageNotificationEvent 이벤트가 발생했을 때 해당 메서드가 호출된다.") - void test_createMessageNotification() throws Exception { - //given - final MessageNotificationEvent event = new MessageNotificationEvent( - "rooId", - "content", - 3L, - 4L, - LocalDateTime.now() - ); - - //when - notificationEventListener.createMessageNotification(event); - - //then - verify(mockingFirebaseCloudMessageClient, times(1)) - .sendMessageTo(eq(event)); - } -}