diff --git a/src/main/java/com/moabam/api/application/notification/NotificationService.java b/src/main/java/com/moabam/api/application/notification/NotificationService.java index 3296fce5..41ca7539 100644 --- a/src/main/java/com/moabam/api/application/notification/NotificationService.java +++ b/src/main/java/com/moabam/api/application/notification/NotificationService.java @@ -28,8 +28,9 @@ @Transactional(readOnly = true) public class NotificationService { - private static final String KNOCK_BODY = "%s님이 콕 찔렀습니다."; - private static final String CERTIFY_TIME_BODY = "%s방 인증 시간입니다."; + private static final String COMMON_TITLE = "모아밤"; + private static final String KNOCK_BODY = "%s방에서 %s님이 콕 찔렀어요~"; + private static final String CERTIFY_TIME_BODY = "%s방 인증 시간~"; private final ClockHolder clockHolder; private final FcmService fcmService; @@ -40,18 +41,21 @@ public class NotificationService { @Transactional public void sendKnock(Long roomId, Long targetId, Long memberId, String memberNickname) { - roomService.validateRoomById(roomId); + String roomTitle = roomService.findRoom(roomId).getTitle(); validateConflictKnock(roomId, targetId, memberId); String fcmToken = fcmService.findTokenByMemberId(targetId) .orElseThrow(() -> new NotFoundException(ErrorMessage.NOT_FOUND_FCM_TOKEN)); - fcmService.sendAsync(fcmToken, String.format(KNOCK_BODY, memberNickname)); + String notificationTitle = roomId.toString(); + String notificationBody = String.format(KNOCK_BODY, roomTitle, memberNickname); + fcmService.sendAsync(fcmToken, notificationTitle, notificationBody); notificationRepository.saveKnock(roomId, targetId, memberId); } public void sendCouponIssueResult(Long memberId, String couponName, String body) { String fcmToken = fcmService.findTokenByMemberId(memberId).orElse(null); - fcmService.sendAsync(fcmToken, String.format(body, couponName)); + String notificationBody = String.format(body, couponName); + fcmService.sendAsync(fcmToken, COMMON_TITLE, notificationBody); } @Scheduled(cron = "0 50 * * * *") @@ -61,9 +65,10 @@ public void sendCertificationTime() { participants.parallelStream().forEach(participant -> { String roomTitle = participant.getRoom().getTitle(); + String notificationTitle = participant.getRoom().getId().toString(); String notificationBody = String.format(CERTIFY_TIME_BODY, roomTitle); String fcmToken = fcmService.findTokenByMemberId(participant.getMemberId()).orElse(null); - fcmService.sendAsync(fcmToken, notificationBody); + fcmService.sendAsync(fcmToken, notificationTitle, notificationBody); }); } diff --git a/src/main/java/com/moabam/api/application/room/RoomService.java b/src/main/java/com/moabam/api/application/room/RoomService.java index 658d7e0e..63179715 100644 --- a/src/main/java/com/moabam/api/application/room/RoomService.java +++ b/src/main/java/com/moabam/api/application/room/RoomService.java @@ -161,12 +161,6 @@ public boolean checkIfParticipant(Long memberId, Long roomId) { } } - public void validateRoomById(Long roomId) { - if (!roomRepository.existsById(roomId)) { - throw new NotFoundException(ROOM_NOT_FOUND); - } - } - public Room findRoom(Long roomId) { return roomRepository.findById(roomId) .orElseThrow(() -> new NotFoundException(ROOM_NOT_FOUND)); diff --git a/src/main/java/com/moabam/api/infrastructure/fcm/FcmMapper.java b/src/main/java/com/moabam/api/infrastructure/fcm/FcmMapper.java index f977fc84..6b6a33a6 100644 --- a/src/main/java/com/moabam/api/infrastructure/fcm/FcmMapper.java +++ b/src/main/java/com/moabam/api/infrastructure/fcm/FcmMapper.java @@ -9,11 +9,9 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class FcmMapper { - private static final String NOTIFICATION_TITLE = "모아밤"; - - public static Notification toNotification(String body) { + public static Notification toNotification(String title, String body) { return Notification.builder() - .setTitle(NOTIFICATION_TITLE) + .setTitle(title) .setBody(body) .build(); } diff --git a/src/main/java/com/moabam/api/infrastructure/fcm/FcmService.java b/src/main/java/com/moabam/api/infrastructure/fcm/FcmService.java index cc575968..6b9fa73b 100644 --- a/src/main/java/com/moabam/api/infrastructure/fcm/FcmService.java +++ b/src/main/java/com/moabam/api/infrastructure/fcm/FcmService.java @@ -33,8 +33,8 @@ public Optional findTokenByMemberId(Long targetId) { return Optional.ofNullable(fcmRepository.findTokenByMemberId(targetId)); } - public void sendAsync(String fcmToken, String notificationBody) { - Notification notification = FcmMapper.toNotification(notificationBody); + public void sendAsync(String fcmToken, String notificationTitle, String notificationBody) { + Notification notification = FcmMapper.toNotification(notificationTitle, notificationBody); if (fcmToken != null) { Message message = FcmMapper.toMessage(notification, fcmToken); diff --git a/src/test/java/com/moabam/api/application/notification/NotificationServiceTest.java b/src/test/java/com/moabam/api/application/notification/NotificationServiceTest.java index 2a49ab6f..10e0cdc9 100644 --- a/src/test/java/com/moabam/api/application/notification/NotificationServiceTest.java +++ b/src/test/java/com/moabam/api/application/notification/NotificationServiceTest.java @@ -19,6 +19,7 @@ import com.moabam.api.application.room.RoomService; import com.moabam.api.domain.notification.repository.NotificationRepository; import com.moabam.api.domain.room.Participant; +import com.moabam.api.domain.room.Room; import com.moabam.api.domain.room.repository.ParticipantSearchRepository; import com.moabam.api.infrastructure.fcm.FcmService; import com.moabam.global.auth.model.AuthMember; @@ -29,6 +30,7 @@ import com.moabam.global.error.model.ErrorMessage; import com.moabam.support.annotation.WithMember; import com.moabam.support.common.FilterProcessExtension; +import com.moabam.support.fixture.RoomFixture; @ExtendWith({MockitoExtension.class, FilterProcessExtension.class}) class NotificationServiceTest { @@ -57,7 +59,9 @@ class NotificationServiceTest { @Test void sendKnock_success() { // Given - willDoNothing().given(roomService).validateRoomById(any(Long.class)); + Room room = RoomFixture.room(); + + given(roomService.findRoom(any(Long.class))).willReturn(room); given(fcmService.findTokenByMemberId(any(Long.class))).willReturn(Optional.of("FCM-TOKEN")); given(notificationRepository.existsKnockByKey(any(Long.class), any(Long.class), any(Long.class))) .willReturn(false); @@ -66,7 +70,7 @@ void sendKnock_success() { notificationService.sendKnock(1L, 1L, 2L, "nickName"); // Then - verify(fcmService).sendAsync(any(String.class), any(String.class)); + verify(fcmService).sendAsync(any(String.class), any(String.class), any(String.class)); verify(notificationRepository).saveKnock(any(Long.class), any(Long.class), any(Long.class)); } @@ -74,7 +78,7 @@ void sendKnock_success() { @Test void sendKnock_Room_NotFoundException() { // Given - willThrow(NotFoundException.class).given(roomService).validateRoomById(any(Long.class)); + given(roomService.findRoom(any(Long.class))).willThrow(NotFoundException.class); // When & Then assertThatThrownBy(() -> notificationService.sendKnock(1L, 1L, 2L, "nickName")) @@ -85,7 +89,9 @@ void sendKnock_Room_NotFoundException() { @Test void sendKnock_FcmToken_NotFoundException() { // Given - willDoNothing().given(roomService).validateRoomById(any(Long.class)); + Room room = RoomFixture.room(); + + given(roomService.findRoom(any(Long.class))).willReturn(room); given(fcmService.findTokenByMemberId(any(Long.class))).willReturn(Optional.empty()); given(notificationRepository.existsKnockByKey(any(Long.class), any(Long.class), any(Long.class))) .willReturn(false); @@ -100,7 +106,9 @@ void sendKnock_FcmToken_NotFoundException() { @Test void sendKnock_ConflictException() { // Given - willDoNothing().given(roomService).validateRoomById(any(Long.class)); + Room room = RoomFixture.room(); + + given(roomService.findRoom(any(Long.class))).willReturn(room); given(notificationRepository.existsKnockByKey(any(Long.class), any(Long.class), any(Long.class))) .willReturn(true); @@ -120,7 +128,7 @@ void sendCouponIssueResult_success() { notificationService.sendCouponIssueResult(1L, "couponName", successIssueResult); // Then - verify(fcmService).sendAsync(any(String.class), any(String.class)); + verify(fcmService).sendAsync(any(String.class), any(String.class), any(String.class)); } @DisplayName("로그아웃된 사용자에게 쿠폰 이슈 결과를 성공적으로 전송한다. - Void") @@ -133,7 +141,7 @@ void sendCouponIssueResult_fcmToken_null() { notificationService.sendCouponIssueResult(1L, "couponName", successIssueResult); // Then - verify(fcmService).sendAsync(isNull(), any(String.class)); + verify(fcmService).sendAsync(isNull(), any(String.class), any(String.class)); } @DisplayName("특정 인증 시간에 해당하는 방 사용자들에게 알림을 성공적으로 보낸다. - Void") @@ -149,7 +157,8 @@ void sendCertificationTime_success(List participants) { notificationService.sendCertificationTime(); // Then - verify(fcmService, times(3)).sendAsync(any(String.class), any(String.class)); + verify(fcmService, times(3)) + .sendAsync(any(String.class), any(String.class), any(String.class)); } @DisplayName("특정 인증 시간에 해당하는 방 사용자들의 토큰값이 없다. - Void") @@ -165,7 +174,8 @@ void sendCertificationTime_NoFirebaseMessaging(List participants) { notificationService.sendCertificationTime(); // Then - verify(fcmService, times(0)).sendAsync(any(String.class), any(String.class)); + verify(fcmService, times(0)) + .sendAsync(any(String.class), any(String.class), any(String.class)); } @WithMember diff --git a/src/test/java/com/moabam/api/infrastructure/fcm/FcmServiceTest.java b/src/test/java/com/moabam/api/infrastructure/fcm/FcmServiceTest.java index 942cf849..9720b843 100644 --- a/src/test/java/com/moabam/api/infrastructure/fcm/FcmServiceTest.java +++ b/src/test/java/com/moabam/api/infrastructure/fcm/FcmServiceTest.java @@ -79,7 +79,7 @@ void findTokenByMemberId_success() { @Test void sendAsync_success() { // When - fcmService.sendAsync("FCM-TOKEN", "알림"); + fcmService.sendAsync("FCM-TOKEN", "title", "body"); // Then verify(firebaseMessaging).sendAsync(any(Message.class)); @@ -89,7 +89,7 @@ void sendAsync_success() { @Test void sendAsync_null() { // When - fcmService.sendAsync(null, "알림"); + fcmService.sendAsync(null, "titile", "body"); // Then verify(firebaseMessaging, times(0)).sendAsync(any(Message.class)); diff --git a/src/test/java/com/moabam/support/fixture/RoomFixture.java b/src/test/java/com/moabam/support/fixture/RoomFixture.java index dbe7213d..b286c880 100644 --- a/src/test/java/com/moabam/support/fixture/RoomFixture.java +++ b/src/test/java/com/moabam/support/fixture/RoomFixture.java @@ -30,6 +30,7 @@ public static Room room() { public static Room room(int certifyTime) { return Room.builder() + .id(1L) .title("testTitle") .roomType(RoomType.MORNING) .certifyTime(certifyTime)