-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
137 additions
and
0 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
src/test/java/com/kakao/golajuma/vote/domain/service/SearchVoteListServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package com.kakao.golajuma.vote.domain.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.kakao.golajuma.vote.infra.entity.Category; | ||
import com.kakao.golajuma.vote.infra.entity.VoteEntity; | ||
import com.kakao.golajuma.vote.infra.repository.VoteRepository; | ||
import com.kakao.golajuma.vote.web.dto.response.SearchVoteListResponse; | ||
import com.kakao.golajuma.vote.web.dto.response.VoteDto; | ||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.domain.SliceImpl; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class SearchVoteListServiceTest { | ||
|
||
@InjectMocks SearchVoteListService searchVoteListService; | ||
@Mock VoteRepository voteRepository; | ||
@Mock GetVoteService getVoteService; | ||
|
||
String keyword; | ||
Sort sort; | ||
Category category; | ||
VoteEntity voteEntity; | ||
List<VoteEntity> votes; | ||
Slice<VoteEntity> voteList; | ||
|
||
private void setup() { | ||
// VoteEntity 생성 | ||
voteEntity = | ||
VoteEntity.builder() | ||
.id(1L) | ||
.userId(1L) | ||
.voteTitle("title") | ||
.voteEndDate(LocalDateTime.now()) | ||
.category(Category.TOTAL) | ||
.build(); | ||
// vote list -> vote slice 생성 | ||
votes = new ArrayList<>(); | ||
votes.add(voteEntity); | ||
voteList = new SliceImpl<>(votes); | ||
|
||
VoteDto voteDto = new VoteDto(voteEntity, "username", "continue", true, true, "total"); | ||
|
||
// when | ||
when(getVoteService.getVote(any(), any())).thenReturn(voteDto); | ||
} | ||
|
||
private void normalThen(SearchVoteListResponse result) { | ||
assertEquals(1, result.getVotes().size()); | ||
assertEquals(true, result.getIsLast()); | ||
} | ||
|
||
@DisplayName("투표 검색 정상 요청 - 최신순, 전체 카테고리") | ||
@Test | ||
void searchVoteTest_currentAndTotalCategory() { | ||
setup(); | ||
// given | ||
keyword = "keyword"; | ||
sort = Sort.CURRENT; | ||
category = Category.TOTAL; | ||
|
||
// when | ||
when(voteRepository.searchVotesOrderByCreatedDate(any(), any())).thenReturn(voteList); | ||
|
||
SearchVoteListResponse result = | ||
searchVoteListService.searchVoteList(1L, 0, keyword, sort, category); | ||
// then | ||
normalThen(result); | ||
} | ||
|
||
@DisplayName("투표 검색 정상 요청 - 최신순, 특정 카테고리") | ||
@Test | ||
void searchVoteTest_currentAndSpecificCategory() { | ||
setup(); | ||
// given | ||
keyword = "keyword"; | ||
sort = Sort.CURRENT; | ||
category = Category.BUY; | ||
|
||
// when | ||
when(voteRepository.searchVotesByCategoryOrderByCreatedDate(any(), any(), any())) | ||
.thenReturn(voteList); | ||
|
||
SearchVoteListResponse result = | ||
searchVoteListService.searchVoteList(1L, 0, keyword, sort, category); | ||
// then | ||
normalThen(result); | ||
} | ||
|
||
@DisplayName("투표 검색 정상 요청 - 인기순, 전체 카테고리") | ||
@Test | ||
void searchVoteTest_popularAndTotalCategory() { | ||
setup(); | ||
// given | ||
keyword = "keyword"; | ||
sort = Sort.POPULAR; | ||
category = Category.TOTAL; | ||
|
||
// when | ||
when(voteRepository.searchVotesOrderByVoteTotalCount(any(), any())).thenReturn(voteList); | ||
|
||
SearchVoteListResponse result = | ||
searchVoteListService.searchVoteList(1L, 0, keyword, sort, category); | ||
// then | ||
normalThen(result); | ||
} | ||
|
||
@DisplayName("투표 검색 정상 요청 - 인기순, 특정 카테고리") | ||
@Test | ||
void searchVoteTest_popularAndSpecificCategory() { | ||
setup(); | ||
// given | ||
keyword = "keyword"; | ||
sort = Sort.POPULAR; | ||
category = Category.BUY; | ||
|
||
// when | ||
when(voteRepository.searchVotesByCategoryOrderByVoteTotalCount(any(), any(), any())) | ||
.thenReturn(voteList); | ||
|
||
SearchVoteListResponse result = | ||
searchVoteListService.searchVoteList(1L, 0, keyword, sort, category); | ||
// then | ||
normalThen(result); | ||
} | ||
} |