Skip to content

Commit 9e8e2d7

Browse files
committed
test 게시물 조회시 사용자 좋아요 상태 반영
- redis에서 먼저 캐싱을 조회하고, Repo를 조회하여 최신인 현재 저장된 캐싱 값을 먼저 상태로 반환하도록 설정.
1 parent ea627bc commit 9e8e2d7

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/test/java/io/crops/warmletter/domain/share/service/SharePostLikeServiceTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package io.crops.warmletter.domain.share.service;
2-
32
import io.crops.warmletter.domain.auth.facade.AuthFacade;
43
import io.crops.warmletter.domain.share.cache.PostLikeRedisManager;
54
import io.crops.warmletter.domain.share.dto.response.SharePostLikeResponse;
@@ -11,7 +10,6 @@
1110
import org.mockito.InjectMocks;
1211
import org.mockito.Mock;
1312
import org.mockito.junit.jupiter.MockitoExtension;
14-
1513
import static org.junit.jupiter.api.Assertions.*;
1614
import 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

Comments
 (0)