11package io .crops .warmletter .domain .share .service ;
2-
32import io .crops .warmletter .domain .auth .facade .AuthFacade ;
43import io .crops .warmletter .domain .share .cache .PostLikeRedisManager ;
54import io .crops .warmletter .domain .share .dto .response .SharePostLikeResponse ;
1110import org .mockito .InjectMocks ;
1211import org .mockito .Mock ;
1312import org .mockito .junit .jupiter .MockitoExtension ;
14-
1513import static org .junit .jupiter .api .Assertions .*;
1614import static org .mockito .Mockito .*;
1715
@@ -56,6 +54,7 @@ void getLikeCountAndStatus_Success() {
5654 SharePostLikeResponse mockResponse = new SharePostLikeResponse (5L , true );
5755
5856 when (authFacade .getCurrentUserId ()).thenReturn (memberId );
57+ when (redisManager .isLiked (sharePostId , memberId )).thenReturn (true );
5958 when (sharePostLikeRepository .getLikeCountAndStatus (sharePostId , memberId ))
6059 .thenReturn (mockResponse );
6160
@@ -67,6 +66,7 @@ void getLikeCountAndStatus_Success() {
6766 assertEquals (5L , result .getLikeCount ());
6867 assertTrue (result .isLiked ());
6968 verify (authFacade ).getCurrentUserId ();
69+ verify (redisManager ).isLiked (sharePostId , memberId );
7070 verify (sharePostLikeRepository ).getLikeCountAndStatus (sharePostId , memberId );
7171 }
7272
@@ -79,6 +79,7 @@ void getLikeCountAndStatus_NotLiked() {
7979 SharePostLikeResponse mockResponse = new SharePostLikeResponse (10L , false );
8080
8181 when (authFacade .getCurrentUserId ()).thenReturn (memberId );
82+ when (redisManager .isLiked (sharePostId , memberId )).thenReturn (false );
8283 when (sharePostLikeRepository .getLikeCountAndStatus (sharePostId , memberId ))
8384 .thenReturn (mockResponse );
8485
@@ -90,6 +91,7 @@ void getLikeCountAndStatus_NotLiked() {
9091 assertEquals (10L , result .getLikeCount ());
9192 assertFalse (result .isLiked ());
9293 verify (authFacade ).getCurrentUserId ();
94+ verify (redisManager ).isLiked (sharePostId , memberId );
9395 verify (sharePostLikeRepository ).getLikeCountAndStatus (sharePostId , memberId );
9496 }
9597
@@ -106,6 +108,31 @@ void getLikeCountAndStatus_NullPostId() {
106108 });
107109
108110 verify (authFacade ).getCurrentUserId ();
111+ verify (redisManager , never ()).isLiked (any (), any ());
109112 verify (sharePostLikeRepository , never ()).getLikeCountAndStatus (any (), any ());
110113 }
114+ @ Test
115+ @ DisplayName ("좋아요 개수와 상태 조회 - Redis는 false, DB는 true일 때 redis 우선 " )
116+ void getLikeCountAndStatus_RedisNotLikedDbLiked () {
117+ // given
118+ Long sharePostId = 1L ;
119+ Long memberId = 1L ;
120+ SharePostLikeResponse mockResponse = new SharePostLikeResponse (10L , true );
121+
122+ when (authFacade .getCurrentUserId ()).thenReturn (memberId );
123+ when (redisManager .isLiked (sharePostId , memberId )).thenReturn (false );
124+ when (sharePostLikeRepository .getLikeCountAndStatus (sharePostId , memberId ))
125+ .thenReturn (mockResponse );
126+
127+ // when
128+ SharePostLikeResponse result = sharePostLikeService .getLikeCountAndStatus (sharePostId );
129+
130+ // then
131+ assertNotNull (result );
132+ assertEquals (10L , result .getLikeCount ());
133+ assertFalse (result .isLiked ());
134+ verify (authFacade ).getCurrentUserId ();
135+ verify (redisManager ).isLiked (sharePostId , memberId );
136+ verify (sharePostLikeRepository ).getLikeCountAndStatus (sharePostId , memberId );
137+ }
111138}
0 commit comments