Skip to content

Commit

Permalink
[#92] memesService에 RankingService 의존성 추가 및 테스트 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
JunRain2 committed May 27, 2024
1 parent ef13c2b commit 8a29d4b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
@Builder
public class MemesRankDto {
private Long memesId;
private Double score;
private Long score;

public static MemesRankDto of(ZSetOperations.TypedTuple<Long> zSet) {
return MemesRankDto.builder()
.memesId(zSet.getValue())
.score(zSet.getScore())
.score(zSet.getScore().longValue())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.example.memetory.domain.memes.service;

import static java.time.LocalDateTime.*;

import java.util.List;

import org.springframework.data.domain.Pageable;
Expand All @@ -13,6 +11,7 @@
import com.example.memetory.domain.member.service.MemberService;
import com.example.memetory.domain.meme.entity.Meme;
import com.example.memetory.domain.meme.service.MemeService;
import com.example.memetory.domain.memes.dto.MemesRankDto;
import com.example.memetory.domain.memes.dto.MemesServiceDto;
import com.example.memetory.domain.memes.dto.response.MemesInfoResponse;
import com.example.memetory.domain.memes.dto.response.MemesInfoSliceResponse;
Expand All @@ -30,6 +29,7 @@ public class MemesService {
private final MemberService memberService;
private final MemeService memeService;
private final MemesRepository memesRepository;
private final RankingService rankingService;

@Transactional
public MemesResponse registerMemes(MemesServiceDto memesServiceDto) {
Expand Down Expand Up @@ -97,14 +97,26 @@ public List<MemesInfoResponse> findTopMemesByLike() {

@Transactional(readOnly = true)
public List<MemesInfoResponse> findTopMemesByLikeForMonth() {
return memesRepository.findTopMemesOrderByLikeCountForPeriod(now().minusMonths(1));
List<MemesRankDto> memesRankDtoList = rankingService.findTopTenMemesLikeCountForMonth();

return convertMemesRankDtoListIntoMemesInfoResponseList(memesRankDtoList);
}

@Transactional(readOnly = true)
public List<MemesInfoResponse> findTopMemesByLikeForWeek() {
return memesRepository.findTopMemesOrderByLikeCountForPeriod(now().minusWeeks(1));
List<MemesRankDto> memesRankDtoList = rankingService.findTopTenMemesLikeCountForWeek();

return convertMemesRankDtoListIntoMemesInfoResponseList(memesRankDtoList);
}

//Todo memesIdList를 받아서 memesInfo로 순서 변환없이 변환하는 작업 필요
private List<MemesInfoResponse> convertMemesRankDtoListIntoMemesInfoResponseList(
List<MemesRankDto> memesRankDtoList) {

return memesRankDtoList.stream().map(this::convertMemesRankDtoInooMemesInfoResponse).toList();
}

private MemesInfoResponse convertMemesRankDtoInooMemesInfoResponse(MemesRankDto memesRank) {
Memes memes = findMemesFromMemesId(memesRank.getMemesId());
return MemesInfoResponse.fromMemesAndLikeCount(memes, memesRank.getScore());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -22,7 +24,9 @@
import com.example.memetory.domain.meme.entity.Meme;
import com.example.memetory.domain.meme.exception.AccessDeniedMemeException;
import com.example.memetory.domain.meme.service.MemeService;
import com.example.memetory.domain.memes.dto.MemesRankDto;
import com.example.memetory.domain.memes.dto.MemesServiceDto;
import com.example.memetory.domain.memes.dto.response.MemesInfoResponse;
import com.example.memetory.domain.memes.dto.response.MemesResponse;
import com.example.memetory.domain.memes.entity.Memes;
import com.example.memetory.domain.memes.exception.AccessDinedMemesException;
Expand All @@ -40,6 +44,8 @@ public class MemesServiceTest {
private MemesRepository memesRepository;
@Mock
private MemeService memeService;
@Mock
private RankingService rankingService;

private MemesServiceDto memesServiceDto;
private Member member;
Expand Down Expand Up @@ -138,4 +144,29 @@ void Given_notExistMemesId_When_findMemesResponse_Throw_MemesResponse() {
// then
assertThrows(NotFoundMemesException.class, () -> memesService.findMemesResponse(memesServiceDto));
}

@Test
@DisplayName("주간 탑 10 조회로 인한 List<MemesInfoResponse> 반환")
void When_findTopMemesByLikeForWeek_Then_List_MemesInfoResponse() {
// given
List<MemesRankDto> memesRankDtoList = generateMemesRankDtoList();
given(rankingService.findTopTenMemesLikeCountForWeek()).willReturn(memesRankDtoList);
given(memesRepository.findByMemesId(any())).willReturn(Optional.ofNullable(MEMES(member, meme)));

// when
List<MemesInfoResponse> result = memesService.findTopMemesByLikeForWeek();

// then
assertThat(result).hasSize(10);
assertThat(result.get(0)).isInstanceOf(MemesInfoResponse.class);
}

private List<MemesRankDto> generateMemesRankDtoList() {
List<MemesRankDto> expectedResult = new ArrayList<>();

for (long i = 1; i <= 10; i++) {
expectedResult.add(new MemesRankDto(i, i));
}
return expectedResult;
}
}

0 comments on commit 8a29d4b

Please sign in to comment.