From 5e6f7d27187d004638e2892f9049cb7ca960c591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=99=8D=ED=98=81=EC=A4=80?= <31675711+HyuckJuneHong@users.noreply.github.com> Date: Sun, 26 Nov 2023 13:42:26 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=BF=A0=ED=8F=B0,=20=EC=95=8C?= =?UTF-8?q?=EB=A6=BC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=A0=91=EA=B7=BC=20?= =?UTF-8?q?=EC=A0=9C=EC=96=B4=EC=9E=90,=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=AA=85,=20=ED=81=B4=EB=9E=98=EC=8A=A4=EB=AA=85=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20(#148)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * style : Schedule 어노테이션 위치 변경 * refactor: 쿠폰 발행 기간 하루로 통일 및 쿠폰 정보 오픈 날짜 추가 * feat: 쿠폰 발행 가능 날짜 중복 체크 기능 추가 * refactor: Builder 삭제 * test: 쿠폰 관련 테스트 수정 * feat: 쿠폰 발행 관련 레포지토리 기능 구현 및 테스트 * test: 쿠폰 발행 관련 문자열 레디스 기능 구현 및 테스트 * feat: 쿠폰 발행 관련 ZSET 레디스 기능 구현 및 테스트 * test: 쿠폰 발행 컨트롤러 기능 테스트 * test: RestDoc 업데이트 * test: Github Actions 시, Redis ZSET 명령어 못찾는 테스트 Disable * refactor: 알림 및 쿠폰 테스트 코드 메서드명 변경 및 알림 콕 알림 키 변경 * refactor: LocalDate 코드 리뷰 반영 --- .../coupon/CouponManageService.java | 4 +- .../api/application/coupon/CouponService.java | 4 +- .../notification/NotificationService.java | 19 ++---- .../repository/CouponManageRepository.java | 12 ++-- .../repository/NotificationRepository.java | 23 ++++--- .../api/infrastructure/fcm/FcmRepository.java | 12 ++-- ...ository.java => ValueRedisRepository.java} | 2 +- src/main/resources/static/docs/coupon.html | 54 +++++++++------- src/main/resources/static/docs/index.html | 2 +- .../resources/static/docs/notification.html | 30 ++++----- .../coupon/CouponManageServiceTest.java | 15 ++--- .../application/coupon/CouponServiceTest.java | 14 ++-- .../notification/NotificationServiceTest.java | 41 ++++++------ .../moabam/api/domain/coupon/CouponTest.java | 8 +-- .../api/domain/coupon/CouponTypeTest.java | 6 +- .../api/domain/coupon/CouponWalletTest.java | 4 +- .../CouponManageRepositoryTest.java | 16 ++--- .../CouponSearchRepositoryTest.java | 8 +-- .../CouponWalletSearchRepositoryTest.java | 4 +- .../NotificationRepositoryTest.java | 64 ++++++++++++++----- .../dto/coupon/CreateCouponRequestTest.java | 2 +- .../infrastructure/fcm/FcmRepositoryTest.java | 40 +++++++----- .../infrastructure/fcm/FcmServiceTest.java | 14 ++-- ...est.java => ValueRedisRepositoryTest.java} | 30 ++++----- .../redis/ZSetRedisRepositoryTest.java | 14 ++-- .../presentation/CouponControllerTest.java | 12 ++-- .../NotificationControllerTest.java | 38 ++++++----- 27 files changed, 269 insertions(+), 223 deletions(-) rename src/main/java/com/moabam/api/infrastructure/redis/{StringRedisRepository.java => ValueRedisRepository.java} (95%) rename src/test/java/com/moabam/api/infrastructure/redis/{StringRedisRepositoryTest.java => ValueRedisRepositoryTest.java} (64%) diff --git a/src/main/java/com/moabam/api/application/coupon/CouponManageService.java b/src/main/java/com/moabam/api/application/coupon/CouponManageService.java index 3fd13b1b..f4d1ef92 100644 --- a/src/main/java/com/moabam/api/application/coupon/CouponManageService.java +++ b/src/main/java/com/moabam/api/application/coupon/CouponManageService.java @@ -35,7 +35,7 @@ public class CouponManageService { @Scheduled(fixedDelay = 1000) public void issue() { - LocalDate now = LocalDate.from(clockHolder.times()); + LocalDate now = clockHolder.date(); Optional isCoupon = couponRepository.findByStartAt(now); if (!canIssue(isCoupon)) { @@ -69,7 +69,7 @@ public void deleteCouponManage(String couponName) { } private void validateRegister(String couponName) { - LocalDate now = LocalDate.from(clockHolder.times()); + LocalDate now = clockHolder.date(); Optional coupon = couponRepository.findByStartAt(now); if (coupon.isEmpty() || !coupon.get().getName().equals(couponName)) { diff --git a/src/main/java/com/moabam/api/application/coupon/CouponService.java b/src/main/java/com/moabam/api/application/coupon/CouponService.java index b8310e7d..8659bebf 100644 --- a/src/main/java/com/moabam/api/application/coupon/CouponService.java +++ b/src/main/java/com/moabam/api/application/coupon/CouponService.java @@ -69,7 +69,7 @@ public Coupon getByWalletIdAndMemberId(Long couponWalletId, Long memberId) { } public List getAllByStatus(CouponStatusRequest request) { - LocalDate now = LocalDate.from(clockHolder.times()); + LocalDate now = clockHolder.date(); List coupons = couponSearchRepository.findAllByStatus(now, request); return coupons.stream() @@ -78,7 +78,7 @@ public List getAllByStatus(CouponStatusRequest request) { } private void validatePeriod(LocalDate startAt, LocalDate openAt) { - LocalDate now = LocalDate.from(clockHolder.times()); + LocalDate now = clockHolder.date(); if (!now.isBefore(startAt)) { throw new BadRequestException(ErrorMessage.INVALID_COUPON_START_AT_PERIOD); 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 11f408e4..a0857dd3 100644 --- a/src/main/java/com/moabam/api/application/notification/NotificationService.java +++ b/src/main/java/com/moabam/api/application/notification/NotificationService.java @@ -30,7 +30,6 @@ public class NotificationService { private static final String KNOCK_BODY = "%s님이 콕 찔렀습니다."; private static final String CERTIFY_TIME_BODY = "%s방 인증 시간입니다."; - private static final String KNOCK_KEY = "room_%s_member_%s_knocks_%s"; private final FcmService fcmService; private final RoomService roomService; @@ -41,13 +40,11 @@ public class NotificationService { @Transactional public void sendKnock(AuthMember member, Long targetId, Long roomId) { roomService.validateRoomById(roomId); - - String knockKey = generateKnockKey(member.id(), targetId, roomId); - validateConflictKnock(knockKey); + validateConflictKnock(member.id(), targetId, roomId); String fcmToken = fcmService.findTokenByMemberId(targetId); fcmService.sendAsync(fcmToken, String.format(KNOCK_BODY, member.nickname())); - notificationRepository.saveKnock(knockKey); + notificationRepository.saveKnock(member.id(), targetId, roomId); } @Scheduled(cron = "0 50 * * * *") @@ -68,8 +65,8 @@ public List getMyKnockStatusInRoom(Long memberId, Long roomId, List !participant.getMemberId().equals(memberId)) .toList(); - Predicate knockPredicate = targetId - -> notificationRepository.existsKnockByKey(generateKnockKey(memberId, targetId, roomId)); + Predicate knockPredicate = targetId -> + notificationRepository.existsKnockByKey(memberId, targetId, roomId); Map> knockStatus = filteredParticipants.stream() .map(Participant::getMemberId) @@ -78,13 +75,9 @@ public List getMyKnockStatusInRoom(Long memberId, Long roomId, List popMinQueue(String couponName, long count) { } public void deleteQueue(String couponName) { - stringRedisRepository.delete(requireNonNull(couponName)); + valueRedisRepository.delete(requireNonNull(couponName)); } public int increaseIssuedStock(String couponName) { String stockKey = String.format(STOCK_KEY, requireNonNull(couponName)); - return stringRedisRepository + return valueRedisRepository .increment(requireNonNull(stockKey)) .intValue(); } public int getIssuedStock(String couponName) { String stockKey = String.format(STOCK_KEY, requireNonNull(couponName)); - String stockValue = stringRedisRepository.get(requireNonNull(stockKey)); + String stockValue = valueRedisRepository.get(requireNonNull(stockKey)); if (stockValue == null) { return 0; @@ -58,6 +58,6 @@ public int getIssuedStock(String couponName) { public void deleteIssuedStock(String couponName) { String stockKey = String.format(STOCK_KEY, requireNonNull(couponName)); - stringRedisRepository.delete(requireNonNull(stockKey)); + valueRedisRepository.delete(requireNonNull(stockKey)); } } diff --git a/src/main/java/com/moabam/api/domain/notification/repository/NotificationRepository.java b/src/main/java/com/moabam/api/domain/notification/repository/NotificationRepository.java index aaffa123..30cdd5d3 100644 --- a/src/main/java/com/moabam/api/domain/notification/repository/NotificationRepository.java +++ b/src/main/java/com/moabam/api/domain/notification/repository/NotificationRepository.java @@ -7,7 +7,7 @@ import org.springframework.stereotype.Repository; -import com.moabam.api.infrastructure.redis.StringRedisRepository; +import com.moabam.api.infrastructure.redis.ValueRedisRepository; import lombok.RequiredArgsConstructor; @@ -15,19 +15,22 @@ @RequiredArgsConstructor public class NotificationRepository { + private static final String KNOCK_KEY = "room_%s_member_%s_knocks_%s"; private static final long EXPIRE_KNOCK = 12; - private final StringRedisRepository stringRedisRepository; + private final ValueRedisRepository valueRedisRepository; - public void saveKnock(String key) { - stringRedisRepository.save( - requireNonNull(key), - BLANK, - Duration.ofHours(EXPIRE_KNOCK) - ); + public void saveKnock(Long memberId, Long targetId, Long roomId) { + String knockKey = + String.format(KNOCK_KEY, requireNonNull(roomId), requireNonNull(memberId), requireNonNull(targetId)); + + valueRedisRepository.save(knockKey, BLANK, Duration.ofHours(EXPIRE_KNOCK)); } - public boolean existsKnockByKey(String key) { - return stringRedisRepository.hasKey(requireNonNull(key)); + public boolean existsKnockByKey(Long memberId, Long targetId, Long roomId) { + String knockKey = + String.format(KNOCK_KEY, requireNonNull(roomId), requireNonNull(memberId), requireNonNull(targetId)); + + return valueRedisRepository.hasKey(requireNonNull(knockKey)); } } diff --git a/src/main/java/com/moabam/api/infrastructure/fcm/FcmRepository.java b/src/main/java/com/moabam/api/infrastructure/fcm/FcmRepository.java index eaa5c55b..39b97666 100644 --- a/src/main/java/com/moabam/api/infrastructure/fcm/FcmRepository.java +++ b/src/main/java/com/moabam/api/infrastructure/fcm/FcmRepository.java @@ -6,7 +6,7 @@ import org.springframework.stereotype.Repository; -import com.moabam.api.infrastructure.redis.StringRedisRepository; +import com.moabam.api.infrastructure.redis.ValueRedisRepository; import lombok.RequiredArgsConstructor; @@ -16,10 +16,10 @@ public class FcmRepository { private static final long EXPIRE_FCM_TOKEN = 60; - private final StringRedisRepository stringRedisRepository; + private final ValueRedisRepository valueRedisRepository; public void saveToken(Long memberId, String fcmToken) { - stringRedisRepository.save( + valueRedisRepository.save( String.valueOf(requireNonNull(memberId)), requireNonNull(fcmToken), Duration.ofDays(EXPIRE_FCM_TOKEN) @@ -27,14 +27,14 @@ public void saveToken(Long memberId, String fcmToken) { } public void deleteTokenByMemberId(Long memberId) { - stringRedisRepository.delete(String.valueOf(requireNonNull(memberId))); + valueRedisRepository.delete(String.valueOf(requireNonNull(memberId))); } public String findTokenByMemberId(Long memberId) { - return stringRedisRepository.get(String.valueOf(requireNonNull(memberId))); + return valueRedisRepository.get(String.valueOf(requireNonNull(memberId))); } public boolean existsTokenByMemberId(Long memberId) { - return stringRedisRepository.hasKey(String.valueOf(requireNonNull(memberId))); + return valueRedisRepository.hasKey(String.valueOf(requireNonNull(memberId))); } } diff --git a/src/main/java/com/moabam/api/infrastructure/redis/StringRedisRepository.java b/src/main/java/com/moabam/api/infrastructure/redis/ValueRedisRepository.java similarity index 95% rename from src/main/java/com/moabam/api/infrastructure/redis/StringRedisRepository.java rename to src/main/java/com/moabam/api/infrastructure/redis/ValueRedisRepository.java index c5d4d9df..9f456e9a 100644 --- a/src/main/java/com/moabam/api/infrastructure/redis/StringRedisRepository.java +++ b/src/main/java/com/moabam/api/infrastructure/redis/ValueRedisRepository.java @@ -9,7 +9,7 @@ @Repository @RequiredArgsConstructor -public class StringRedisRepository { +public class ValueRedisRepository { private final RedisTemplate redisTemplate; diff --git a/src/main/resources/static/docs/coupon.html b/src/main/resources/static/docs/coupon.html index 5678c54e..1b26ba45 100644 --- a/src/main/resources/static/docs/coupon.html +++ b/src/main/resources/static/docs/coupon.html @@ -461,7 +461,7 @@

요청

POST /admins/coupons HTTP/1.1
 Content-Type: application/json;charset=UTF-8
-Content-Length: 183
+Content-Length: 175
 Host: localhost:8080
 
 {
@@ -479,9 +479,11 @@ 

응답

HTTP/1.1 201 Created
-Vary: Origin
-Vary: Access-Control-Request-Method
-Vary: Access-Control-Request-Headers
+Access-Control-Allow-Origin: +Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH +Access-Control-Allow-Headers: Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers, X-Requested-With,Content-Type, Referer +Access-Control-Allow-Credentials: true +Access-Control-Max-Age: 3600

@@ -496,7 +498,7 @@

쿠폰 삭제

요청

-
DELETE /admins/coupons/1 HTTP/1.1
+
DELETE /admins/coupons/27 HTTP/1.1
 Host: localhost:8080
@@ -504,9 +506,11 @@

응답

HTTP/1.1 200 OK
-Vary: Origin
-Vary: Access-Control-Request-Method
-Vary: Access-Control-Request-Headers
+Access-Control-Allow-Origin: +Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH +Access-Control-Allow-Headers: Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers, X-Requested-With,Content-Type, Referer +Access-Control-Allow-Credentials: true +Access-Control-Max-Age: 3600

@@ -522,7 +526,7 @@

특정 쿠폰 조회

요청

-
GET /coupons/26 HTTP/1.1
+
GET /coupons/16 HTTP/1.1
 Host: localhost:8080
@@ -530,11 +534,13 @@

응답

HTTP/1.1 200 OK
-Vary: Origin
-Vary: Access-Control-Request-Method
-Vary: Access-Control-Request-Headers
+Access-Control-Allow-Origin:
+Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
+Access-Control-Allow-Headers: Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers, X-Requested-With,Content-Type, Referer
+Access-Control-Allow-Credentials: true
+Access-Control-Max-Age: 3600
 Content-Type: application/json
-Content-Length: 215
+Content-Length: 205
 
 {
   "id" : 16,
@@ -565,7 +571,7 @@ 

요청

POST /coupons/search HTTP/1.1
 Content-Type: application/json;charset=UTF-8
-Content-Length: 44
+Content-Length: 41
 Host: localhost:8080
 
 {
@@ -578,11 +584,13 @@ 

응답

HTTP/1.1 200 OK
-Vary: Origin
-Vary: Access-Control-Request-Method
-Vary: Access-Control-Request-Headers
+Access-Control-Allow-Origin:
+Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
+Access-Control-Allow-Headers: Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers, X-Requested-With,Content-Type, Referer
+Access-Control-Allow-Credentials: true
+Access-Control-Max-Age: 3600
 Content-Type: application/json
-Content-Length: 216
+Content-Length: 206
 
 [ {
   "id" : 17,
@@ -623,9 +631,11 @@ 

응답

HTTP/1.1 400 Bad Request
-Vary: Origin
-Vary: Access-Control-Request-Method
-Vary: Access-Control-Request-Headers
+Access-Control-Allow-Origin:
+Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
+Access-Control-Allow-Headers: Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers, X-Requested-With,Content-Type, Referer
+Access-Control-Allow-Credentials: true
+Access-Control-Max-Age: 3600
 Content-Type: application/json
 Content-Length: 64
 
@@ -660,7 +670,7 @@ 

쿠폰 사용 (진행 중)

diff --git a/src/main/resources/static/docs/index.html b/src/main/resources/static/docs/index.html index 9996cde1..62f80fd6 100644 --- a/src/main/resources/static/docs/index.html +++ b/src/main/resources/static/docs/index.html @@ -616,7 +616,7 @@

diff --git a/src/main/resources/static/docs/notification.html b/src/main/resources/static/docs/notification.html index 3340a231..d56f528e 100644 --- a/src/main/resources/static/docs/notification.html +++ b/src/main/resources/static/docs/notification.html @@ -468,16 +468,12 @@

요청

응답

-
HTTP/1.1 404 Not Found
-Vary: Origin
-Vary: Access-Control-Request-Method
-Vary: Access-Control-Request-Headers
-Content-Type: application/json
-Content-Length: 64
-
-{
-  "message" : "해당 유저는 접속 중이 아닙니다."
-}
+
HTTP/1.1 200 OK
+Access-Control-Allow-Origin:
+Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
+Access-Control-Allow-Headers: Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers, X-Requested-With,Content-Type, Referer
+Access-Control-Allow-Credentials: true
+Access-Control-Max-Age: 3600
@@ -494,18 +490,20 @@

요청

POST /notifications HTTP/1.1
 Content-Type: application/x-www-form-urlencoded
 Host: localhost:8080
-Content-Length: 9
+Content-Length: 18
 
-fcmToken=
+fcmToken=FCM-TOKEN

응답

HTTP/1.1 200 OK
-Vary: Origin
-Vary: Access-Control-Request-Method
-Vary: Access-Control-Request-Headers
+Access-Control-Allow-Origin: +Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH +Access-Control-Allow-Headers: Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers, X-Requested-With,Content-Type, Referer +Access-Control-Allow-Credentials: true +Access-Control-Max-Age: 3600
@@ -515,7 +513,7 @@

응답

diff --git a/src/test/java/com/moabam/api/application/coupon/CouponManageServiceTest.java b/src/test/java/com/moabam/api/application/coupon/CouponManageServiceTest.java index 214ca0ab..e347aba6 100644 --- a/src/test/java/com/moabam/api/application/coupon/CouponManageServiceTest.java +++ b/src/test/java/com/moabam/api/application/coupon/CouponManageServiceTest.java @@ -5,7 +5,6 @@ import static org.mockito.BDDMockito.*; import java.time.LocalDate; -import java.time.LocalDateTime; import java.util.HashSet; import java.util.Optional; import java.util.Set; @@ -56,7 +55,7 @@ void issue_all_success() { Coupon coupon = CouponFixture.coupon(1000, 100); Set membersId = new HashSet<>(Set.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)); - given(clockHolder.times()).willReturn(LocalDateTime.now()); + given(clockHolder.date()).willReturn(LocalDate.now()); given(couponRepository.findByStartAt(any(LocalDate.class))).willReturn(Optional.of(coupon)); given(couponManageRepository.getIssuedStock(any(String.class))).willReturn(10); given(couponManageRepository.popMinQueue(any(String.class), any(long.class))).willReturn(membersId); @@ -73,7 +72,7 @@ void issue_all_success() { @Test void issue_notStartAt() { // Given - given(clockHolder.times()).willReturn(LocalDateTime.now()); + given(clockHolder.date()).willReturn(LocalDate.now()); given(couponRepository.findByStartAt(any(LocalDate.class))).willReturn(Optional.empty()); // When @@ -92,7 +91,7 @@ void issue_stockEnd() { // Given Coupon coupon = CouponFixture.coupon(1000, 100); - given(clockHolder.times()).willReturn(LocalDateTime.now()); + given(clockHolder.date()).willReturn(LocalDate.now()); given(couponRepository.findByStartAt(any(LocalDate.class))).willReturn(Optional.of(coupon)); given(couponManageRepository.getIssuedStock(any(String.class))).willReturn(coupon.getStock()); @@ -112,7 +111,7 @@ void issue_queue_stockENd() { Coupon coupon = CouponFixture.coupon(1000, 100); Set membersId = new HashSet<>(Set.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)); - given(clockHolder.times()).willReturn(LocalDateTime.now()); + given(clockHolder.date()).willReturn(LocalDate.now()); given(couponRepository.findByStartAt(any(LocalDate.class))).willReturn(Optional.of(coupon)); given(couponManageRepository.getIssuedStock(any(String.class))).willReturn(10); given(couponManageRepository.popMinQueue(any(String.class), any(long.class))).willReturn(membersId); @@ -133,7 +132,7 @@ void register_success() { AuthMember member = AuthorizationThreadLocal.getAuthMember(); Coupon coupon = CouponFixture.coupon(); - given(clockHolder.times()).willReturn(LocalDateTime.now()); + given(clockHolder.date()).willReturn(LocalDate.now()); given(couponRepository.findByStartAt(any(LocalDate.class))).willReturn(Optional.of(coupon)); // When @@ -150,7 +149,7 @@ void register_StartAt_BadRequestException() { // Given AuthMember member = AuthorizationThreadLocal.getAuthMember(); - given(clockHolder.times()).willReturn(LocalDateTime.now()); + given(clockHolder.date()).willReturn(LocalDate.now()); given(couponRepository.findByStartAt(any(LocalDate.class))).willReturn(Optional.empty()); // When & Then @@ -167,7 +166,7 @@ void register_Name_BadRequestException() { AuthMember member = AuthorizationThreadLocal.getAuthMember(); Coupon coupon = CouponFixture.coupon(); - given(clockHolder.times()).willReturn(LocalDateTime.now()); + given(clockHolder.date()).willReturn(LocalDate.now()); given(couponRepository.findByStartAt(any(LocalDate.class))).willReturn(Optional.of(coupon)); // When & Then diff --git a/src/test/java/com/moabam/api/application/coupon/CouponServiceTest.java b/src/test/java/com/moabam/api/application/coupon/CouponServiceTest.java index d929f46b..075323af 100644 --- a/src/test/java/com/moabam/api/application/coupon/CouponServiceTest.java +++ b/src/test/java/com/moabam/api/application/coupon/CouponServiceTest.java @@ -4,7 +4,6 @@ import static org.mockito.BDDMockito.*; import java.time.LocalDate; -import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @@ -63,7 +62,7 @@ void create_success() { CreateCouponRequest request = CouponFixture.createCouponRequest(); given(couponRepository.existsByName(any(String.class))).willReturn(false); - given(clockHolder.times()).willReturn(LocalDateTime.of(2022, 1, 1, 1, 1)); + given(clockHolder.date()).willReturn(LocalDate.of(2022, 1, 1)); // When couponService.create(admin, request); @@ -95,7 +94,7 @@ void create_Type_NotFoundException() { CreateCouponRequest request = CouponFixture.createCouponRequest("UNKNOWN", 2, 1); given(couponRepository.existsByName(any(String.class))).willReturn(false); - given(clockHolder.times()).willReturn(LocalDateTime.of(2022, 1, 1, 1, 1)); + given(clockHolder.date()).willReturn(LocalDate.of(2022, 1, 1)); // When & Then assertThatThrownBy(() -> couponService.create(admin, request)) @@ -144,7 +143,7 @@ void create_StartAt_BadRequestException() { AuthMember admin = AuthorizationThreadLocal.getAuthMember(); CreateCouponRequest request = CouponFixture.createCouponRequest(); - given(clockHolder.times()).willReturn(LocalDateTime.of(2025, 1, 1, 1, 1)); + given(clockHolder.date()).willReturn(LocalDate.of(2025, 1, 1)); given(couponRepository.existsByName(any(String.class))).willReturn(false); given(couponRepository.existsByStartAt(any(LocalDate.class))).willReturn(false); @@ -165,7 +164,7 @@ void create_OpenAt_BadRequestException() { given(couponRepository.existsByName(any(String.class))).willReturn(false); given(couponRepository.existsByStartAt(any(LocalDate.class))).willReturn(false); - given(clockHolder.times()).willReturn(LocalDateTime.of(2022, 1, 1, 1, 1)); + given(clockHolder.date()).willReturn(LocalDate.of(2022, 1, 1)); // When & Then assertThatThrownBy(() -> couponService.create(admin, request)) @@ -180,6 +179,7 @@ void delete_success() { // Given AuthMember admin = AuthorizationThreadLocal.getAuthMember(); Coupon coupon = CouponFixture.coupon(10, 100); + given(couponRepository.findById(any(Long.class))).willReturn(Optional.of(coupon)); // When @@ -209,6 +209,7 @@ void delete_Admin_NotFoundException() { void delete_NotFoundException() { // Given AuthMember admin = AuthorizationThreadLocal.getAuthMember(); + given(couponRepository.findById(any(Long.class))).willReturn(Optional.empty()); // When & Then @@ -222,6 +223,7 @@ void delete_NotFoundException() { void getById_success() { // Given Coupon coupon = CouponFixture.coupon(10, 100); + given(couponRepository.findById(any(Long.class))).willReturn(Optional.of(coupon)); // When @@ -251,7 +253,7 @@ void getAllByStatus_success(List coupons) { // Given CouponStatusRequest request = CouponFixture.couponStatusRequest(false, false); - given(clockHolder.times()).willReturn(LocalDateTime.now()); + given(clockHolder.date()).willReturn(LocalDate.now()); given(couponSearchRepository.findAllByStatus(any(LocalDate.class), any(CouponStatusRequest.class))) .willReturn(coupons); 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 60e7e65f..a85c0fc9 100644 --- a/src/test/java/com/moabam/api/application/notification/NotificationServiceTest.java +++ b/src/test/java/com/moabam/api/application/notification/NotificationServiceTest.java @@ -19,7 +19,6 @@ import com.moabam.api.domain.notification.repository.NotificationRepository; import com.moabam.api.domain.room.Participant; import com.moabam.api.domain.room.repository.ParticipantSearchRepository; -import com.moabam.api.infrastructure.fcm.FcmRepository; import com.moabam.api.infrastructure.fcm.FcmService; import com.moabam.global.auth.model.AuthMember; import com.moabam.global.auth.model.AuthorizationThreadLocal; @@ -34,43 +33,41 @@ class NotificationServiceTest { @InjectMocks - private NotificationService notificationService; + NotificationService notificationService; @Mock - private RoomService roomService; + RoomService roomService; @Mock - private FcmRepository fcmRepository; + FcmService fcmService; @Mock - private FcmService fcmService; + NotificationRepository notificationRepository; @Mock - private NotificationRepository notificationRepository; + ParticipantSearchRepository participantSearchRepository; @Mock - private ParticipantSearchRepository participantSearchRepository; - - @Mock - private ClockHolder clockHolder; + ClockHolder clockHolder; @WithMember - @DisplayName("성공적으로 상대에게 콕 알림을 보낸다. - Void") + @DisplayName("상대에게 콕 알림을 성공적으로 보낸다. - Void") @Test - void sendKnock() { + void sendKnock_success() { // Given AuthMember member = AuthorizationThreadLocal.getAuthMember(); willDoNothing().given(roomService).validateRoomById(any(Long.class)); - given(notificationRepository.existsKnockByKey(any(String.class))).willReturn(false); given(fcmService.findTokenByMemberId(any(Long.class))).willReturn("FCM-TOKEN"); + given(notificationRepository.existsKnockByKey(any(Long.class), any(Long.class), any(Long.class))) + .willReturn(false); // When notificationService.sendKnock(member, 2L, 1L); // Then verify(fcmService).sendAsync(any(String.class), any(String.class)); - verify(notificationRepository).saveKnock(any(String.class)); + verify(notificationRepository).saveKnock(any(Long.class), any(Long.class), any(Long.class)); } @WithMember @@ -79,6 +76,7 @@ void sendKnock() { void sendKnock_Room_NotFoundException() { // Given AuthMember member = AuthorizationThreadLocal.getAuthMember(); + willThrow(NotFoundException.class).given(roomService).validateRoomById(any(Long.class)); // When & Then @@ -94,7 +92,8 @@ void sendKnock_FcmToken_NotFoundException() { AuthMember member = AuthorizationThreadLocal.getAuthMember(); willDoNothing().given(roomService).validateRoomById(any(Long.class)); - given(notificationRepository.existsKnockByKey(any(String.class))).willReturn(false); + given(notificationRepository.existsKnockByKey(any(Long.class), any(Long.class), any(Long.class))) + .willReturn(false); given(fcmService.findTokenByMemberId(any(Long.class))) .willThrow(new NotFoundException(ErrorMessage.NOT_FOUND_FCM_TOKEN)); @@ -112,7 +111,8 @@ void sendKnock_ConflictException() { AuthMember member = AuthorizationThreadLocal.getAuthMember(); willDoNothing().given(roomService).validateRoomById(any(Long.class)); - given(notificationRepository.existsKnockByKey(any(String.class))).willReturn(true); + given(notificationRepository.existsKnockByKey(any(Long.class), any(Long.class), any(Long.class))) + .willReturn(true); // When & Then assertThatThrownBy(() -> notificationService.sendKnock(member, 1L, 1L)) @@ -123,7 +123,7 @@ void sendKnock_ConflictException() { @DisplayName("특정 인증 시간에 해당하는 방 사용자들에게 알림을 성공적으로 보낸다. - Void") @MethodSource("com.moabam.support.fixture.ParticipantFixture#provideParticipants") @ParameterizedTest - void sendCertificationTime(List participants) { + void sendCertificationTime_success(List participants) { // Given given(participantSearchRepository.findAllByRoomCertifyTime(any(Integer.class))).willReturn(participants); given(fcmService.findTokenByMemberId(any(Long.class))).willReturn("FCM-TOKEN"); @@ -161,7 +161,8 @@ void getMyKnockStatusInRoom_knocked(List participants) { // Given AuthMember member = AuthorizationThreadLocal.getAuthMember(); - given(notificationRepository.existsKnockByKey(any(String.class))).willReturn(true); + given(notificationRepository.existsKnockByKey(any(Long.class), any(Long.class), any(Long.class))) + .willReturn(true); // When List actual = notificationService.getMyKnockStatusInRoom(member.id(), 1L, participants); @@ -178,8 +179,8 @@ void getMyKnockStatusInRoom_notKnocked(List participants) { // Given AuthMember member = AuthorizationThreadLocal.getAuthMember(); - // given - given(notificationRepository.existsKnockByKey(any(String.class))).willReturn(false); + given(notificationRepository.existsKnockByKey(any(Long.class), any(Long.class), any(Long.class))) + .willReturn(false); // When List actual = notificationService.getMyKnockStatusInRoom(member.id(), 1L, participants); diff --git a/src/test/java/com/moabam/api/domain/coupon/CouponTest.java b/src/test/java/com/moabam/api/domain/coupon/CouponTest.java index 2ab0e438..e1e146e3 100644 --- a/src/test/java/com/moabam/api/domain/coupon/CouponTest.java +++ b/src/test/java/com/moabam/api/domain/coupon/CouponTest.java @@ -13,9 +13,9 @@ class CouponTest { - @DisplayName("쿠폰이 정상적으로 생성된다. - Coupon") + @DisplayName("쿠폰이 성공적으로 생성된다. - Coupon") @Test - void coupon() { + void coupon_success() { // Given LocalDate startAt = LocalDate.of(2023, 2, 1); LocalDate openAt = LocalDate.of(2023, 1, 1); @@ -44,7 +44,7 @@ void coupon() { @DisplayName("쿠폰 보너스 포인트가 1보다 작다. - BadRequestException") @Test - void coupon_validatePoint_Point_BadRequestException() { + void validatePoint_BadRequestException() { // When& Then assertThatThrownBy(() -> CouponFixture.coupon(0, 1)) .isInstanceOf(BadRequestException.class) @@ -53,7 +53,7 @@ void coupon_validatePoint_Point_BadRequestException() { @DisplayName("쿠폰 재고가 1보다 작다. - BadRequestException") @Test - void coupon_validatePoint_Stock_BadRequestException() { + void validateStock_BadRequestException() { // When& Then assertThatThrownBy(() -> CouponFixture.coupon(1, 0)) .isInstanceOf(BadRequestException.class) diff --git a/src/test/java/com/moabam/api/domain/coupon/CouponTypeTest.java b/src/test/java/com/moabam/api/domain/coupon/CouponTypeTest.java index 406bc061..960df900 100644 --- a/src/test/java/com/moabam/api/domain/coupon/CouponTypeTest.java +++ b/src/test/java/com/moabam/api/domain/coupon/CouponTypeTest.java @@ -10,9 +10,9 @@ class CouponTypeTest { - @DisplayName("존재하는 쿠폰을 가져온다. - CouponType") + @DisplayName("존재하는 쿠폰을 성공적으로 가져온다. - CouponType") @Test - void couponType_from() { + void from_success() { // When CouponType actual = CouponType.from(CouponType.GOLDEN_COUPON.getName()); @@ -22,7 +22,7 @@ void couponType_from() { @DisplayName("존재하지 않는 쿠폰을 가져온다. - NotFoundException") @Test - void couponType_from_NotFoundException() { + void from_NotFoundException() { // When & Then assertThatThrownBy(() -> CouponType.from("Not-Coupon")) .isInstanceOf(NotFoundException.class) diff --git a/src/test/java/com/moabam/api/domain/coupon/CouponWalletTest.java b/src/test/java/com/moabam/api/domain/coupon/CouponWalletTest.java index 059a9838..13bcd9e5 100644 --- a/src/test/java/com/moabam/api/domain/coupon/CouponWalletTest.java +++ b/src/test/java/com/moabam/api/domain/coupon/CouponWalletTest.java @@ -9,9 +9,9 @@ class CouponWalletTest { - @DisplayName("쿠폰 지갑 엔티티를 생성한다. - Void") + @DisplayName("쿠폰 지갑 엔티티를 성공적으로 생성한다. - Void") @Test - void couponWallet() { + void couponWallet_success() { // Given Coupon coupon = CouponFixture.coupon("CouponName", 1, 2); diff --git a/src/test/java/com/moabam/api/domain/coupon/repository/CouponManageRepositoryTest.java b/src/test/java/com/moabam/api/domain/coupon/repository/CouponManageRepositoryTest.java index 03c7cf49..2dd66862 100644 --- a/src/test/java/com/moabam/api/domain/coupon/repository/CouponManageRepositoryTest.java +++ b/src/test/java/com/moabam/api/domain/coupon/repository/CouponManageRepositoryTest.java @@ -16,7 +16,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.redis.core.ZSetOperations.TypedTuple; -import com.moabam.api.infrastructure.redis.StringRedisRepository; +import com.moabam.api.infrastructure.redis.ValueRedisRepository; import com.moabam.api.infrastructure.redis.ZSetRedisRepository; @ExtendWith(MockitoExtension.class) @@ -29,7 +29,7 @@ class CouponManageRepositoryTest { ZSetRedisRepository zSetRedisRepository; @Mock - StringRedisRepository stringRedisRepository; + ValueRedisRepository valueRedisRepository; @DisplayName("쿠폰 대기열에 사용자가 성공적으로 등록된다. - Void") @Test @@ -57,7 +57,7 @@ void addIfAbsentQueue_memberId_NullPointerException() { .isInstanceOf(NullPointerException.class); } - @DisplayName("쿠폰 대기열에서 10명을 꺼내고 삭제한다.") + @DisplayName("쿠폰 대기열에서 성공적으로 10명을 꺼내고 삭제한다.") @MethodSource("com.moabam.support.fixture.CouponFixture#provideTypedTuples") @ParameterizedTest void popMinQueue_success(Set> tuples) { @@ -86,7 +86,7 @@ void deleteQueue_success() { couponManageRepository.deleteQueue("couponName"); // Then - verify(stringRedisRepository).delete(any(String.class)); + verify(valueRedisRepository).delete(any(String.class)); } @DisplayName("쿠폰명이 Null인 대기열을 삭제한다. - NullPointerException") @@ -101,7 +101,7 @@ void deleteQueue_NullPointerException() { @Test void increaseIssuedStock_success() { // Given - given(stringRedisRepository.increment(any(String.class))).willReturn(77L); + given(valueRedisRepository.increment(any(String.class))).willReturn(77L); // When int actual = couponManageRepository.increaseIssuedStock("couponName"); @@ -122,7 +122,7 @@ void increaseIssuedStock_NullPointerException() { @Test void getIssuedStock_success() { // Given - given(stringRedisRepository.get(any(String.class))).willReturn("1"); + given(valueRedisRepository.get(any(String.class))).willReturn("1"); // When int actual = couponManageRepository.getIssuedStock("couponName"); @@ -135,7 +135,7 @@ void getIssuedStock_success() { @Test void getIssuedStock_zero() { // Given - given(stringRedisRepository.get(any(String.class))).willReturn(null); + given(valueRedisRepository.get(any(String.class))).willReturn(null); // When int actual = couponManageRepository.getIssuedStock("couponName"); @@ -159,7 +159,7 @@ void deleteIssuedStock_success() { couponManageRepository.deleteIssuedStock("couponName"); // Then - verify(stringRedisRepository).delete(any(String.class)); + verify(valueRedisRepository).delete(any(String.class)); } @DisplayName("쿠폰명이 Null인 할당된 쿠폰 재고를 삭제한다. - NullPointerException") diff --git a/src/test/java/com/moabam/api/domain/coupon/repository/CouponSearchRepositoryTest.java b/src/test/java/com/moabam/api/domain/coupon/repository/CouponSearchRepositoryTest.java index 22deb4da..67fdf033 100644 --- a/src/test/java/com/moabam/api/domain/coupon/repository/CouponSearchRepositoryTest.java +++ b/src/test/java/com/moabam/api/domain/coupon/repository/CouponSearchRepositoryTest.java @@ -22,15 +22,15 @@ class CouponSearchRepositoryTest { @Autowired - private CouponRepository couponRepository; + CouponRepository couponRepository; @Autowired - private CouponSearchRepository couponSearchRepository; + CouponSearchRepository couponSearchRepository; - @DisplayName("발급 가능한 쿠폰을 조회한다. - List") + @DisplayName("발급 가능한 쿠폰을 성공적으로 조회한다. - List") @MethodSource("com.moabam.support.fixture.CouponFixture#provideCoupons") @ParameterizedTest - void findAllByStatus(List coupons) { + void findAllByStatus_success(List coupons) { // Given CouponStatusRequest request = CouponFixture.couponStatusRequest(false, false); LocalDate now = LocalDate.of(2023, 7, 1); diff --git a/src/test/java/com/moabam/api/domain/coupon/repository/CouponWalletSearchRepositoryTest.java b/src/test/java/com/moabam/api/domain/coupon/repository/CouponWalletSearchRepositoryTest.java index db5176c7..b88d5897 100644 --- a/src/test/java/com/moabam/api/domain/coupon/repository/CouponWalletSearchRepositoryTest.java +++ b/src/test/java/com/moabam/api/domain/coupon/repository/CouponWalletSearchRepositoryTest.java @@ -23,9 +23,9 @@ class CouponWalletSearchRepositoryTest { @Autowired private CouponWalletSearchRepository couponWalletSearchRepository; - @DisplayName("회원의 특정 쿠폰 지갑을 조회한다.") + @DisplayName("회원의 특정 쿠폰 지갑을 성공적으로 조회한다.") @Test - void find_by_id_and_member_id() { + void findByIdAndMemberId_success() { // given Long id = 1L; Long memberId = 1L; diff --git a/src/test/java/com/moabam/api/domain/notification/repository/NotificationRepositoryTest.java b/src/test/java/com/moabam/api/domain/notification/repository/NotificationRepositoryTest.java index 6a9a376d..681a9aa5 100644 --- a/src/test/java/com/moabam/api/domain/notification/repository/NotificationRepositoryTest.java +++ b/src/test/java/com/moabam/api/domain/notification/repository/NotificationRepositoryTest.java @@ -12,50 +12,82 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import com.moabam.api.infrastructure.redis.StringRedisRepository; +import com.moabam.api.infrastructure.redis.ValueRedisRepository; @ExtendWith(MockitoExtension.class) class NotificationRepositoryTest { @InjectMocks - private NotificationRepository notificationRepository; + NotificationRepository notificationRepository; @Mock - private StringRedisRepository stringRedisRepository; + ValueRedisRepository valueRedisRepository; @DisplayName("콕 알림이 성공적으로 저장된다. - Void") @Test - void notificationRepository_saveKnockNotification() { + void saveKnock_success() { // When - notificationRepository.saveKnock("knockKey"); + notificationRepository.saveKnock(1L, 1L, 1L); // Then - verify(stringRedisRepository).save(any(String.class), any(String.class), any(Duration.class)); + verify(valueRedisRepository).save(any(String.class), any(String.class), any(Duration.class)); } - @DisplayName("콕 알림 저장 시, 필요한 값이 NULL 이다. - NullPointerException") + @DisplayName("콕 찌르는 사용자의 ID가 Null인 콕 알림을 저장한다. - NullPointerException") @Test - void notificationRepository_saveKnockNotification_NullPointerException() { + void saveKnock_MemberId_NullPointerException() { // When & Then - assertThatThrownBy(() -> notificationRepository.saveKnock(null)) + assertThatThrownBy(() -> notificationRepository.saveKnock(null, 1L, 1L)) .isInstanceOf(NullPointerException.class); } - @DisplayName("콕 알림 여부 체크를 정상적으로 확인한다. - Boolean") + @DisplayName("콕 찌를 대상의 ID가 Null인 콕 알림을 저장한다. - NullPointerException") @Test - void notificationRepository_existsKnockByMemberId() { + void saveKnock_TargetId_NullPointerException() { + // When & Then + assertThatThrownBy(() -> notificationRepository.saveKnock(1L, null, 1L)) + .isInstanceOf(NullPointerException.class); + } + + @DisplayName("방 ID가 Null인 콕 알림을 저장한다. - NullPointerException") + @Test + void saveKnock_RoomId_NullPointerException() { + // When & Then + assertThatThrownBy(() -> notificationRepository.saveKnock(1L, 2L, null)) + .isInstanceOf(NullPointerException.class); + } + + @DisplayName("콕 알림 여부 체크를 성공적으로 확인한다. - Boolean") + @Test + void existsKnockByKey_success() { // When - notificationRepository.existsKnockByKey("knock key"); + notificationRepository.existsKnockByKey(1L, 1L, 1L); // Then - verify(stringRedisRepository).hasKey(any(String.class)); + verify(valueRedisRepository).hasKey(any(String.class)); + } + + @DisplayName("콕 찌르는 사용자의 ID가 Null인 콕 알림 여부를 체크한다. - NullPointerException") + @Test + void existsKnockByKey_MemberId_NullPointerException() { + // When & Then + assertThatThrownBy(() -> notificationRepository.existsKnockByKey(null, 1L, 1L)) + .isInstanceOf(NullPointerException.class); + } + + @DisplayName("콕 찌를 상대 ID가 Null인 콕 알림 여부를 체크한다. - NullPointerException") + @Test + void existsKnockByKey_TargetId_NullPointerException() { + // When & Then + assertThatThrownBy(() -> notificationRepository.existsKnockByKey(1L, null, 1L)) + .isInstanceOf(NullPointerException.class); } - @DisplayName("콕 알림 여부 체크 시, 필요한 값이 NULL 이다. - NullPointerException") + @DisplayName("방 ID가 Null인 콕 알림 여부를 체크한다. - NullPointerException") @Test - void notificationRepository_existsKnockByMemberId_NullPointerException() { + void existsKnockByKey_RoomId_NullPointerException() { // When & Then - assertThatThrownBy(() -> notificationRepository.existsKnockByKey(null)) + assertThatThrownBy(() -> notificationRepository.existsKnockByKey(1L, 2L, null)) .isInstanceOf(NullPointerException.class); } } diff --git a/src/test/java/com/moabam/api/dto/coupon/CreateCouponRequestTest.java b/src/test/java/com/moabam/api/dto/coupon/CreateCouponRequestTest.java index 5fcf7561..3454611b 100644 --- a/src/test/java/com/moabam/api/dto/coupon/CreateCouponRequestTest.java +++ b/src/test/java/com/moabam/api/dto/coupon/CreateCouponRequestTest.java @@ -15,7 +15,7 @@ class CreateCouponRequestTest { @DisplayName("쿠폰 발급 가능 시작 날짜가 올바른 형식으로 입력된다. - yyyy-MM-dd") @Test - void createCouponRequest_StartAt() throws JsonProcessingException { + void startAt_success() throws JsonProcessingException { // Given ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JavaTimeModule()); diff --git a/src/test/java/com/moabam/api/infrastructure/fcm/FcmRepositoryTest.java b/src/test/java/com/moabam/api/infrastructure/fcm/FcmRepositoryTest.java index 4c6f4367..83f2e887 100644 --- a/src/test/java/com/moabam/api/infrastructure/fcm/FcmRepositoryTest.java +++ b/src/test/java/com/moabam/api/infrastructure/fcm/FcmRepositoryTest.java @@ -13,46 +13,54 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import com.moabam.api.infrastructure.redis.StringRedisRepository; +import com.moabam.api.infrastructure.redis.ValueRedisRepository; @ExtendWith(MockitoExtension.class) class FcmRepositoryTest { @InjectMocks - private FcmRepository fcmRepository; + FcmRepository fcmRepository; @Mock - private StringRedisRepository stringRedisRepository; + ValueRedisRepository valueRedisRepository; @DisplayName("FCM 토큰이 성공적으로 저장된다. - Void") @Test - void saveToken() { + void saveToken_success() { // When fcmRepository.saveToken(1L, "value1"); // Then - verify(stringRedisRepository).save(any(String.class), any(String.class), any(Duration.class)); + verify(valueRedisRepository).save(any(String.class), any(String.class), any(Duration.class)); } - @DisplayName("FCM 토큰 저장 시, 필요한 값이 NULL 이다. - NullPointerException") + @DisplayName("ID가 Null인 사용자가 FCM 토큰을 저장한다. - NullPointerException") @Test - void saveToken_NullPointerException() { + void saveToken_MemberId_NullPointerException() { // When & Then assertThatThrownBy(() -> fcmRepository.saveToken(null, "value")) .isInstanceOf(NullPointerException.class); } + @DisplayName("토큰이 Null인 FCM 토큰을 저장한다. - NullPointerException") + @Test + void saveToken_FcmToken_NullPointerException() { + // When & Then + assertThatThrownBy(() -> fcmRepository.saveToken(1L, null)) + .isInstanceOf(NullPointerException.class); + } + @DisplayName("FCM 토큰이 성공적으로 삭제된다. - Void") @Test - void deleteTokenByMemberId() { + void deleteTokenByMemberId_success() { // When fcmRepository.deleteTokenByMemberId(1L); // Then - verify(stringRedisRepository).delete(any(String.class)); + verify(valueRedisRepository).delete(any(String.class)); } - @DisplayName("FCM 토큰 삭제 시, 필요한 값이 NULL 이다. - NullPointerException") + @DisplayName("ID가 Null인 사용자가 FCM 토큰을 삭제한다.. - NullPointerException") @Test void deleteTokenByMemberId_NullPointerException() { // When & Then @@ -62,15 +70,15 @@ void deleteTokenByMemberId_NullPointerException() { @DisplayName("FCM 토큰을 성공적으로 조회된다. - (String) FCM TOKEN") @Test - void findTokenByMemberId() { + void findTokenByMemberId_success() { // When fcmRepository.findTokenByMemberId(1L); // Then - verify(stringRedisRepository).get(any(String.class)); + verify(valueRedisRepository).get(any(String.class)); } - @DisplayName("FCM 토큰 조회 시, 필요한 값이 NULL 이다. - NullPointerException") + @DisplayName("ID가 Null인 사용자가 FCM 토큰을 조회한다. - NullPointerException") @Test void findTokenByMemberId_NullPointerException() { // When & Then @@ -80,15 +88,15 @@ void findTokenByMemberId_NullPointerException() { @DisplayName("FCM 토큰 존재 여부를 성공적으로 확인한다. - Boolean") @Test - void existsTokenByMemberId() { + void existsTokenByMemberId_success() { // When fcmRepository.existsTokenByMemberId(1L); // Then - verify(stringRedisRepository).hasKey(any(String.class)); + verify(valueRedisRepository).hasKey(any(String.class)); } - @DisplayName("FCM 토큰 존재 여부 체크 시, 필요한 값이 NULL 이다. - NullPointerException") + @DisplayName("ID가 Null인 사용자가 FCM 토큰 존재 여부를 확인한다. - NullPointerException") @Test void existsTokenByMemberId_NullPointerException() { // When & Then 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 218e4ef7..c4b7cc09 100644 --- a/src/test/java/com/moabam/api/infrastructure/fcm/FcmServiceTest.java +++ b/src/test/java/com/moabam/api/infrastructure/fcm/FcmServiceTest.java @@ -20,18 +20,18 @@ class FcmServiceTest extends WithoutFilterSupporter { @Autowired - private FcmService fcmService; + FcmService fcmService; @MockBean - private FirebaseMessaging firebaseMessaging; + FirebaseMessaging firebaseMessaging; @MockBean - private FcmRepository fcmRepository; + FcmRepository fcmRepository; @WithMember @DisplayName("FCM 토큰이 성공적으로 저장된다. - Void") @Test - void saveToken() { + void saveToken_success() { // Given AuthMember authMember = AuthorizationThreadLocal.getAuthMember(); @@ -72,7 +72,7 @@ void saveToken_Null() { @DisplayName("FCM 토큰이 성공적으로 삭제된다. - Void") @Test - void deleteTokenByMemberId() { + void deleteTokenByMemberId_success() { // When fcmRepository.deleteTokenByMemberId(1L); @@ -82,7 +82,7 @@ void deleteTokenByMemberId() { @DisplayName("FCM 토큰을 성공적으로 조회된다. - (String) FCM TOKEN") @Test - void findTokenByMemberId() { + void findTokenByMemberId_success() { // When fcmRepository.findTokenByMemberId(1L); @@ -92,7 +92,7 @@ void findTokenByMemberId() { @DisplayName("비동기 FCM 알림을 성공적으로 보낸다. - Void") @Test - void sendAsync() { + void sendAsync_success() { // When fcmService.sendAsync("FCM-TOKEN", "알림"); diff --git a/src/test/java/com/moabam/api/infrastructure/redis/StringRedisRepositoryTest.java b/src/test/java/com/moabam/api/infrastructure/redis/ValueRedisRepositoryTest.java similarity index 64% rename from src/test/java/com/moabam/api/infrastructure/redis/StringRedisRepositoryTest.java rename to src/test/java/com/moabam/api/infrastructure/redis/ValueRedisRepositoryTest.java index 83b9d44a..c0566697 100644 --- a/src/test/java/com/moabam/api/infrastructure/redis/StringRedisRepositoryTest.java +++ b/src/test/java/com/moabam/api/infrastructure/redis/ValueRedisRepositoryTest.java @@ -13,11 +13,11 @@ import com.moabam.global.config.EmbeddedRedisConfig; -@SpringBootTest(classes = {EmbeddedRedisConfig.class, StringRedisRepository.class}) -class StringRedisRepositoryTest { +@SpringBootTest(classes = {EmbeddedRedisConfig.class, ValueRedisRepository.class}) +class ValueRedisRepositoryTest { @Autowired - StringRedisRepository stringRedisRepository; + ValueRedisRepository valueRedisRepository; String key = "key"; String value = "value"; @@ -26,17 +26,17 @@ class StringRedisRepositoryTest { @BeforeEach void setUp() { - stringRedisRepository.save(key, value, duration); + valueRedisRepository.save(key, value, duration); } @AfterEach void setDown() { - if (stringRedisRepository.hasKey(key)) { - stringRedisRepository.delete(key); + if (valueRedisRepository.hasKey(key)) { + valueRedisRepository.delete(key); } - if (stringRedisRepository.hasKey(stockKey)) { - stringRedisRepository.delete(stockKey); + if (valueRedisRepository.hasKey(stockKey)) { + valueRedisRepository.delete(stockKey); } } @@ -44,41 +44,41 @@ void setDown() { @Test void save_success() { // Then - assertThat(stringRedisRepository.get(key)).isEqualTo(value); + assertThat(valueRedisRepository.get(key)).isEqualTo(value); } @DisplayName("레디스의 특정 데이터가 성공적으로 조회된다. - String(Value)") @Test void get_success() { // When - String actual = stringRedisRepository.get(key); + String actual = valueRedisRepository.get(key); // Then - assertThat(actual).isEqualTo(stringRedisRepository.get(key)); + assertThat(actual).isEqualTo(valueRedisRepository.get(key)); } @DisplayName("레디스의 특정 데이터 존재 여부를 성공적으로 체크한다. - Boolean") @Test void hasKey_success() { // When & Then - assertThat(stringRedisRepository.hasKey("not found key")).isFalse(); + assertThat(valueRedisRepository.hasKey("not found key")).isFalse(); } @DisplayName("레디스의 특정 데이터가 성공적으로 삭제된다. - Void") @Test void delete_success() { // When - stringRedisRepository.delete(key); + valueRedisRepository.delete(key); // Then - assertThat(stringRedisRepository.hasKey(key)).isFalse(); + assertThat(valueRedisRepository.hasKey(key)).isFalse(); } @DisplayName("레디스의 특정 데이터의 값이 1 증가한다.") @Test void increment_success() { // When - Long actual = stringRedisRepository.increment(stockKey); + Long actual = valueRedisRepository.increment(stockKey); // Then assertThat(actual).isEqualTo(1L); diff --git a/src/test/java/com/moabam/api/infrastructure/redis/ZSetRedisRepositoryTest.java b/src/test/java/com/moabam/api/infrastructure/redis/ZSetRedisRepositoryTest.java index 5b94879d..cfc29bb4 100644 --- a/src/test/java/com/moabam/api/infrastructure/redis/ZSetRedisRepositoryTest.java +++ b/src/test/java/com/moabam/api/infrastructure/redis/ZSetRedisRepositoryTest.java @@ -15,14 +15,14 @@ import com.moabam.global.config.EmbeddedRedisConfig; -@SpringBootTest(classes = {EmbeddedRedisConfig.class, ZSetRedisRepository.class, StringRedisRepository.class}) +@SpringBootTest(classes = {EmbeddedRedisConfig.class, ZSetRedisRepository.class, ValueRedisRepository.class}) class ZSetRedisRepositoryTest { @Autowired ZSetRedisRepository zSetRedisRepository; @Autowired - StringRedisRepository stringRedisRepository; + ValueRedisRepository valueRedisRepository; @Autowired RedisTemplate redisTemplate; @@ -32,8 +32,8 @@ class ZSetRedisRepositoryTest { @AfterEach void afterEach() { - if (stringRedisRepository.hasKey(key)) { - stringRedisRepository.delete(key); + if (valueRedisRepository.hasKey(key)) { + valueRedisRepository.delete(key); } } @@ -44,7 +44,7 @@ void addIfAbsent_success() { zSetRedisRepository.addIfAbsent(key, value, 1); // Then - assertThat(stringRedisRepository.hasKey(key)).isTrue(); + assertThat(valueRedisRepository.hasKey(key)).isTrue(); } @DisplayName("이미 존재하는 값을 한 번 더 저장을 시도한다. - Void") @@ -72,7 +72,7 @@ void popMin_same_success() { // Then assertThat(actual).hasSize(3); - assertThat(stringRedisRepository.hasKey(key)).isFalse(); + assertThat(valueRedisRepository.hasKey(key)).isFalse(); } @Disabled @@ -106,6 +106,6 @@ void popMin_less_success() { // Then assertThat(actual).hasSize(3); - assertThat(stringRedisRepository.hasKey(key)).isTrue(); + assertThat(valueRedisRepository.hasKey(key)).isTrue(); } } diff --git a/src/test/java/com/moabam/api/presentation/CouponControllerTest.java b/src/test/java/com/moabam/api/presentation/CouponControllerTest.java index d9fa0bfa..4cbf4981 100644 --- a/src/test/java/com/moabam/api/presentation/CouponControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/CouponControllerTest.java @@ -8,6 +8,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; @@ -64,7 +65,8 @@ class CouponControllerTest extends WithoutFilterSupporter { void create_Coupon_success() throws Exception { // Given CreateCouponRequest request = CouponFixture.createCouponRequest(); - given(clockHolder.times()).willReturn(LocalDateTime.of(2022, 1, 1, 1, 1)); + + given(clockHolder.date()).willReturn(LocalDate.of(2022, 1, 1)); // When & Then mockMvc.perform(post("/admins/coupons") @@ -85,7 +87,7 @@ void create_Coupon_StartAt_BadRequestException() throws Exception { // Given CreateCouponRequest request = CouponFixture.createCouponRequest(); - given(clockHolder.times()).willReturn(LocalDateTime.of(2025, 1, 1, 1, 1)); + given(clockHolder.date()).willReturn(LocalDate.of(2025, 1, 1)); // When & Then mockMvc.perform(post("/admins/coupons") @@ -110,7 +112,7 @@ void create_Coupon_OpenAt_BadRequestException() throws Exception { String couponType = CouponType.GOLDEN_COUPON.getName(); CreateCouponRequest request = CouponFixture.createCouponRequest(couponType, 1, 1); - given(clockHolder.times()).willReturn(LocalDateTime.of(2022, 1, 1, 1, 1)); + given(clockHolder.date()).willReturn(LocalDate.of(2022, 1, 1)); // When & Then mockMvc.perform(post("/admins/coupons") @@ -272,7 +274,7 @@ void getAllByStatus_Coupon_success(List coupons) throws Exception { CouponStatusRequest request = CouponFixture.couponStatusRequest(false, false); couponRepository.saveAll(coupons); - given(clockHolder.times()).willReturn(LocalDateTime.of(2023, 3, 1, 1, 1)); + given(clockHolder.date()).willReturn(LocalDate.of(2023, 3, 1)); // When & Then mockMvc.perform(post("/coupons/search") @@ -296,7 +298,7 @@ void registerQueue_success() throws Exception { Coupon couponFixture = CouponFixture.coupon(); Coupon coupon = couponRepository.save(couponFixture); - given(clockHolder.times()).willReturn(LocalDateTime.of(2023, 2, 1, 1, 1)); + given(clockHolder.date()).willReturn(LocalDate.of(2023, 2, 1)); // When & Then mockMvc.perform(post("/coupons") diff --git a/src/test/java/com/moabam/api/presentation/NotificationControllerTest.java b/src/test/java/com/moabam/api/presentation/NotificationControllerTest.java index a1d00129..048353d8 100644 --- a/src/test/java/com/moabam/api/presentation/NotificationControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/NotificationControllerTest.java @@ -29,7 +29,7 @@ import com.moabam.api.domain.room.repository.RoomRepository; import com.moabam.api.infrastructure.fcm.FcmRepository; import com.moabam.api.infrastructure.fcm.FcmService; -import com.moabam.api.infrastructure.redis.StringRedisRepository; +import com.moabam.api.infrastructure.redis.ValueRedisRepository; import com.moabam.global.error.model.ErrorMessage; import com.moabam.support.annotation.WithMember; import com.moabam.support.common.WithoutFilterSupporter; @@ -43,41 +43,39 @@ @AutoConfigureRestDocs class NotificationControllerTest extends WithoutFilterSupporter { - private static final String KNOCK_KEY = "room_%s_member_%s_knocks_%s"; - @Autowired - private MockMvc mockMvc; + MockMvc mockMvc; @Autowired - private MemberRepository memberRepository; + MemberRepository memberRepository; @Autowired - private RoomRepository roomRepository; + RoomRepository roomRepository; @Autowired - private NotificationRepository notificationRepository; + NotificationRepository notificationRepository; @Autowired - private StringRedisRepository stringRedisRepository; + ValueRedisRepository valueRedisRepository; @Autowired - private FcmService fcmService; + FcmService fcmService; @Autowired - private FcmRepository fcmRepository; + FcmRepository fcmRepository; @MockBean - private FirebaseMessaging firebaseMessaging; + FirebaseMessaging firebaseMessaging; - private Member target; - private Room room; - private String knockKey; + Member target; + Room room; + String knockKey; @BeforeEach void setUp() { target = memberRepository.save(MemberFixture.member("123", "targetName")); room = roomRepository.save(RoomFixture.room()); - knockKey = String.format(KNOCK_KEY, room.getId(), 1, target.getId()); + knockKey = String.format("room_%s_member_%s_knocks_%s", room.getId(), 1, target.getId()); willReturn(null) .given(firebaseMessaging) @@ -87,13 +85,13 @@ void setUp() { @AfterEach void setDown() { fcmService.deleteTokenByMemberId(target.getId()); - stringRedisRepository.delete(knockKey); + valueRedisRepository.delete(knockKey); } @WithMember @DisplayName("POST - 성공적으로 FCM Token을 저장한다. - Void") @Test - void createFcmToken() throws Exception { + void createFcmToken_success() throws Exception { // When & Then mockMvc.perform(post("/notifications") .param("fcmToken", "FCM-TOKEN")) @@ -119,9 +117,9 @@ void createFcmToken_blank() throws Exception { } @WithMember - @DisplayName("GET - 성공적으로 상대에게 콕 알림을 보낸다. - Void") + @DisplayName("GET - 상대에게 콕 알림을 성공적으로 보낸다. - Void") @Test - void sendKnock() throws Exception { + void sendKnock_success() throws Exception { // Given fcmRepository.saveToken(target.getId(), "FCM_TOKEN"); @@ -156,7 +154,7 @@ void sendKnock_NotFoundException() throws Exception { void sendKnock_ConflictException() throws Exception { // Given fcmRepository.saveToken(target.getId(), "FCM_TOKEN"); - notificationRepository.saveKnock(knockKey); + notificationRepository.saveKnock(1L, target.getId(), room.getId()); // When & Then mockMvc.perform(get("/notifications/rooms/" + room.getId() + "/members/" + target.getId()))