From 5774cfd4b64301d41cf0213f4c34f426b2a1c296 Mon Sep 17 00:00:00 2001 From: HyuckJuneHong Date: Fri, 3 Nov 2023 00:10:19 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=BD=95=20=EC=B0=8C=EB=A5=B4?= =?UTF-8?q?=EA=B8=B0=20=EC=95=8C=EB=A6=BC=20=EC=A0=80=EC=9E=A5=20=EC=8B=9C?= =?UTF-8?q?,=20=ED=82=A4=EA=B0=92=20=EB=B0=8F=20=EB=A7=8C=EB=A3=8C?= =?UTF-8?q?=EC=8B=9C=EA=B0=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/application/NotificationService.java | 10 +++++----- .../repository/NotificationRepository.java | 16 +++++++--------- .../global/common/util/GlobalConstant.java | 1 + .../api/application/NotificationServiceTest.java | 14 ++++++++------ .../repository/NotificationRepositoryTest.java | 8 ++++---- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/moabam/api/application/NotificationService.java b/src/main/java/com/moabam/api/application/NotificationService.java index 675ac1ba..40e73a98 100644 --- a/src/main/java/com/moabam/api/application/NotificationService.java +++ b/src/main/java/com/moabam/api/application/NotificationService.java @@ -24,16 +24,16 @@ public class NotificationService { private final NotificationRepository notificationRepository; @Transactional - public void sendKnockNotification(MemberTest member, Long targetId) { + public void sendKnockNotification(MemberTest member, Long targetId, Long roomId) { validateFcmToken(targetId); - validateKnockNotification(member.memberId(), targetId); + validateKnockNotification(member.memberId(), targetId, roomId); String fcmToken = notificationRepository.findFcmTokenByMemberId(targetId); Notification notification = NotificationMapper.toKnockNotificationEntity(member.memberId()); Message message = NotificationMapper.toMessageEntity(notification, fcmToken); firebaseMessaging.sendAsync(message); - notificationRepository.saveKnockNotification(member.memberId(), targetId); + notificationRepository.saveKnockNotification(member.memberId(), targetId, roomId); } private void validateFcmToken(Long memberId) { @@ -42,8 +42,8 @@ private void validateFcmToken(Long memberId) { } } - private void validateKnockNotification(Long memberId, Long targetId) { - if (notificationRepository.existsKnockByMemberId(memberId, targetId)) { + private void validateKnockNotification(Long memberId, Long targetId, Long roomId) { + if (notificationRepository.existsKnockByMemberId(memberId, targetId, roomId)) { throw new ConflictException(ErrorMessage.KNOCK_CONFLICT); } } diff --git a/src/main/java/com/moabam/api/domain/repository/NotificationRepository.java b/src/main/java/com/moabam/api/domain/repository/NotificationRepository.java index 89e1ba94..77e2ec2f 100644 --- a/src/main/java/com/moabam/api/domain/repository/NotificationRepository.java +++ b/src/main/java/com/moabam/api/domain/repository/NotificationRepository.java @@ -15,7 +15,7 @@ @RequiredArgsConstructor public class NotificationRepository { - private static final long EXPIRE_KNOCK = 20; + private static final long EXPIRE_KNOCK = 12; private static final long EXPIRE_FCM_TOKEN = 60; private static final String TO = "_TO_"; @@ -30,12 +30,9 @@ public void saveFcmToken(Long key, String value) { ); } - public void saveKnockNotification(Long memberId, Long targetId) { - stringRedisRepository.save( - requireNonNull(memberId) + TO + requireNonNull(targetId), - BLANK, - requireNonNull(Duration.ofMinutes(EXPIRE_KNOCK)) - ); + public void saveKnockNotification(Long memberId, Long targetId, Long roomId) { + String key = requireNonNull(roomId) + UNDER_BAR + requireNonNull(memberId) + TO + requireNonNull(targetId); + stringRedisRepository.save(key, BLANK, requireNonNull(Duration.ofHours(EXPIRE_KNOCK))); } // TODO : 세연님 로그아웃 시, 해당 메서드 사용해서 해당 유저의 FCM TOKEN 삭제하시면 됩니다. @@ -51,7 +48,8 @@ public boolean existsFcmTokenByMemberId(Long memberId) { return stringRedisRepository.hasKey(String.valueOf(requireNonNull(memberId))); } - public boolean existsKnockByMemberId(Long memberId, Long targetId) { - return stringRedisRepository.hasKey(requireNonNull(memberId) + TO + requireNonNull(targetId)); + public boolean existsKnockByMemberId(Long memberId, Long targetId, Long roomId) { + String key = requireNonNull(roomId) + UNDER_BAR + requireNonNull(memberId) + TO + requireNonNull(targetId); + return stringRedisRepository.hasKey(key); } } diff --git a/src/main/java/com/moabam/global/common/util/GlobalConstant.java b/src/main/java/com/moabam/global/common/util/GlobalConstant.java index c9e5c2d0..5e1da51c 100644 --- a/src/main/java/com/moabam/global/common/util/GlobalConstant.java +++ b/src/main/java/com/moabam/global/common/util/GlobalConstant.java @@ -7,6 +7,7 @@ public class GlobalConstant { public static final String BLANK = ""; + public static final String UNDER_BAR = "_"; public static final String COMMA = ","; public static final String CHARSET_UTF_8 = ";charset=UTF-8"; } diff --git a/src/test/java/com/moabam/api/application/NotificationServiceTest.java b/src/test/java/com/moabam/api/application/NotificationServiceTest.java index 5c36626a..e2a54617 100644 --- a/src/test/java/com/moabam/api/application/NotificationServiceTest.java +++ b/src/test/java/com/moabam/api/application/NotificationServiceTest.java @@ -43,15 +43,16 @@ void setUp() { void notificationService_sendKnockNotification() { // Given given(notificationRepository.existsFcmTokenByMemberId(any(Long.class))).willReturn(true); - given(notificationRepository.existsKnockByMemberId(any(Long.class), any(Long.class))).willReturn(false); + given(notificationRepository.existsKnockByMemberId(any(Long.class), any(Long.class), any(Long.class))) + .willReturn(false); given(notificationRepository.findFcmTokenByMemberId(any(Long.class))).willReturn("FCM-TOKEN"); // When - notificationService.sendKnockNotification(memberTest, 2L); + notificationService.sendKnockNotification(memberTest, 2L, 1L); // Then verify(firebaseMessaging).sendAsync(any(Message.class)); - verify(notificationRepository).saveKnockNotification(any(Long.class), any(Long.class)); + verify(notificationRepository).saveKnockNotification(any(Long.class), any(Long.class), any(Long.class)); } @DisplayName("콕 찌를 상대의 FCM 토큰이 존재하지 않을 때, - NotFoundException") @@ -61,7 +62,7 @@ void notificationService_sendKnockNotification_NotFoundException() { given(notificationRepository.existsFcmTokenByMemberId(any(Long.class))).willReturn(false); // When & Then - assertThatThrownBy(() -> notificationService.sendKnockNotification(memberTest, 1L)) + assertThatThrownBy(() -> notificationService.sendKnockNotification(memberTest, 1L, 1L)) .isInstanceOf(NotFoundException.class) .hasMessage(ErrorMessage.FCM_TOKEN_NOT_FOUND.getMessage()); } @@ -71,10 +72,11 @@ void notificationService_sendKnockNotification_NotFoundException() { void notificationService_sendKnockNotification_ConflictException() { // Given given(notificationRepository.existsFcmTokenByMemberId(any(Long.class))).willReturn(true); - given(notificationRepository.existsKnockByMemberId(any(Long.class), any(Long.class))).willReturn(true); + given(notificationRepository.existsKnockByMemberId(any(Long.class), any(Long.class), any(Long.class))) + .willReturn(true); // When & Then - assertThatThrownBy(() -> notificationService.sendKnockNotification(memberTest, 1L)) + assertThatThrownBy(() -> notificationService.sendKnockNotification(memberTest, 1L, 1L)) .isInstanceOf(ConflictException.class) .hasMessage(ErrorMessage.KNOCK_CONFLICT.getMessage()); } diff --git a/src/test/java/com/moabam/api/domain/repository/NotificationRepositoryTest.java b/src/test/java/com/moabam/api/domain/repository/NotificationRepositoryTest.java index 2159400f..ecee5dfa 100644 --- a/src/test/java/com/moabam/api/domain/repository/NotificationRepositoryTest.java +++ b/src/test/java/com/moabam/api/domain/repository/NotificationRepositoryTest.java @@ -47,7 +47,7 @@ void notificationRepository_save_NullPointerException() { @Test void notificationRepository_saveKnockNotification() { // When - notificationRepository.saveKnockNotification(1L, 2L); + notificationRepository.saveKnockNotification(1L, 2L, 1L); // Then verify(stringRedisRepository).save(any(String.class), any(String.class), any(Duration.class)); @@ -57,7 +57,7 @@ void notificationRepository_saveKnockNotification() { @Test void notificationRepository_saveKnockNotification_NullPointerException() { // When & Then - assertThatThrownBy(() -> notificationRepository.saveKnockNotification(null, 2L)) + assertThatThrownBy(() -> notificationRepository.saveKnockNotification(null, 2L, 1L)) .isInstanceOf(NullPointerException.class); } @@ -119,7 +119,7 @@ void notificationRepository_existsFcmTokenByMemberId_NullPointerException() { @Test void notificationRepository_existsKnockByMemberId() { // When - notificationRepository.existsKnockByMemberId(1L, 2L); + notificationRepository.existsKnockByMemberId(1L, 2L, 1L); // Then verify(stringRedisRepository).hasKey(any(String.class)); @@ -129,7 +129,7 @@ void notificationRepository_existsKnockByMemberId() { @Test void notificationRepository_existsKnockByMemberId_NullPointerException() { // When & Then - assertThatThrownBy(() -> notificationRepository.existsKnockByMemberId(null, 2L)) + assertThatThrownBy(() -> notificationRepository.existsKnockByMemberId(null, 2L, 1L)) .isInstanceOf(NullPointerException.class); } }