Skip to content

Commit

Permalink
Merge pull request #138 from kakao-tech-campus-2nd-step3/test/VideoLo…
Browse files Browse the repository at this point in the history
…adTest

[Test] 비디오 도메인에 fetch join을 적용하고 부하 테스트를 진행해요
  • Loading branch information
BaeJunH0 authored Nov 11, 2024
2 parents e3b4533 + d9cfe57 commit 45d8fd5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 71 deletions.
69 changes: 31 additions & 38 deletions src/main/java/team7/inplace/video/application/VideoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -34,6 +33,7 @@ public class VideoService {
private final VideoRepository videoRepository;
private final PlaceRepository placeRepository;
private final InfluencerRepository influencerRepository;
private final Pageable pageable = PageRequest.of(0, 10);

@Transactional(readOnly = true)
public List<VideoInfo> getVideosBySurround(VideoSearchParams videoSearchParams) {
Expand Down Expand Up @@ -70,31 +70,24 @@ public List<VideoInfo> getVideosBySurround(VideoSearchParams videoSearchParams)
@Transactional(readOnly = true)
public List<VideoInfo> getAllVideosDesc() {
// id를 기준으로 내림차순 정렬하여 비디오 정보 불러오기
Page<Video> videos = videoRepository.findAllByOrderByIdDesc(
PageRequest.of(0, 10)
);
List<Video> videos = videoRepository.findTop10ByOrderByIdDesc(pageable);

// DTO 형식에 맞게 대입
return videos.getContent().stream().map(this::videoToInfo).toList();
return videos.stream().map(this::videoToInfo).toList();
}

@Transactional(readOnly = true)
public List<VideoInfo> getCoolVideo() {
// 조회수 증가량을 기준으로 오름차순 정렬하여 비디오 정보 불러오기
Page<Video> videos = videoRepository.findVideosByOrderByViewCountIncreaseDesc(
PageRequest.of(0, 10)
);
List<Video> videos = videoRepository.findTop10ByOrderByViewCountIncreaseDesc(pageable);

// DTO 형식에 맞게 대입
return videos.stream().map(this::videoToInfo).toList();
}

@Transactional(readOnly = true)
public List<VideoInfo> getVideosByMyInfluencer(List<Long> influencerIds) {
Page<Video> videos = videoRepository.findVideosByInfluencerIdIn(
influencerIds,
PageRequest.of(0, 10)
);
List<Video> videos = videoRepository.findTop10ByInfluencerIdIn(influencerIds, pageable);
return videos.stream().map(this::videoToInfo).toList();
}

Expand All @@ -104,28 +97,6 @@ public Page<VideoInfo> getPlaceNullVideo(Pageable pageable) {
return videos.map(this::videoToInfo);
}

private VideoInfo videoToInfo(Video savedVideo) {
Place place = savedVideo.getPlace();
if (Objects.isNull(place)) {
return new VideoInfo(
savedVideo.getId(),
"장소 정보 없음",
savedVideo.getVideoUrl(),
PlaceForVideo.of(-1L, "장소 정보 없음")
);
}
String alias = AliasUtil.makeAlias(
savedVideo.getInfluencer().getName(),
place.getCategory()
);
return new VideoInfo(
savedVideo.getId(),
alias,
savedVideo.getVideoUrl(),
PlaceForVideo.of(place.getId(), place.getName())
);
}

@Transactional
public void createVideos(List<Create> videoCommands, List<Long> placeIds) {
var videos = new ArrayList<Video>();
Expand Down Expand Up @@ -155,10 +126,6 @@ public void updateVideoViews(List<VideoCommand.UpdateViewCount> videoCommands) {
}
}

private boolean hasNoPlace(Long placeId) {
return placeId == -1;
}

@Transactional
public void addPlaceInfo(Long videoId, Long placeId) {
Video video = videoRepository.findById(videoId)
Expand All @@ -172,4 +139,30 @@ public void addPlaceInfo(Long videoId, Long placeId) {
public void deleteVideo(Long videoId) {
videoRepository.deleteById(videoId);
}

private VideoInfo videoToInfo(Video savedVideo) {
Place place = savedVideo.getPlace();
if (Objects.isNull(place)) {
return new VideoInfo(
savedVideo.getId(),
"장소 정보 없음",
savedVideo.getVideoUrl(),
PlaceForVideo.of(-1L, "장소 정보 없음")
);
}
String alias = AliasUtil.makeAlias(
savedVideo.getInfluencer().getName(),
place.getCategory()
);
return new VideoInfo(
savedVideo.getId(),
alias,
savedVideo.getVideoUrl(),
PlaceForVideo.of(place.getId(), place.getName())
);
}

private boolean hasNoPlace(Long placeId) {
return placeId == -1;
}
}
19 changes: 13 additions & 6 deletions src/main/java/team7/inplace/video/persistence/VideoRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import team7.inplace.place.domain.Place;
import team7.inplace.video.domain.Video;

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

public interface VideoRepository extends JpaRepository<Video, Long> {
@Query("SELECT v FROM Video v JOIN FETCH v.place JOIN FETCH v.influencer ORDER BY v.viewCountIncrease DESC")
List<Video> findTop10ByOrderByViewCountIncreaseDesc(Pageable pageable);

Page<Video> findVideosByInfluencerIdIn(List<Long> influencerIds, Pageable pageable);
@Query("SELECT v FROM Video v JOIN FETCH v.place JOIN FETCH v.influencer WHERE v.influencer.id IN :influencerIds")
List<Video> findTop10ByInfluencerIdIn(List<Long> influencerIds, Pageable pageable);

Page<Video> findAllByOrderByIdDesc(Pageable pageable);

Page<Video> findAllByPlaceIsNull(Pageable pageable);
@Query("SELECT v FROM Video v JOIN FETCH v.place JOIN FETCH v.influencer ORDER BY v.id DESC")
List<Video> findTop10ByOrderByIdDesc(Pageable pageable);

Optional<Video> findTopByPlaceOrderByIdDesc(Place place);

@Query(
value = "SELECT v FROM Video v JOIN FETCH v.influencer WHERE v.place IS NULL",
countQuery = "SELECT COUNT(v) FROM Video v"
)
Page<Video> findAllByPlaceIsNull(Pageable pageable);

List<Video> findByPlaceIdIn(List<Long> placeIds);

List<Video> findByPlaceId(Long placeId);

Page<Video> findVideosByOrderByViewCountIncreaseDesc(Pageable pageable);
}
40 changes: 27 additions & 13 deletions src/test/java/team7/inplace/video/application/VideoServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@
import java.util.Optional;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import team7.inplace.influencer.domain.Influencer;
import team7.inplace.influencer.persistence.InfluencerRepository;
import team7.inplace.place.application.dto.PlaceForVideo;
import team7.inplace.place.domain.*;
import team7.inplace.place.persistence.PlaceRepository;
import team7.inplace.security.application.dto.CustomOAuth2User;
import team7.inplace.user.domain.Role;
import team7.inplace.util.TestUtil;
import team7.inplace.video.application.dto.VideoInfo;
import team7.inplace.video.domain.Video;
Expand All @@ -38,15 +41,21 @@ public class VideoServiceTest {
private VideoRepository videoRepository;
@Mock
private PlaceRepository placeRepository;
@Mock
private InfluencerRepository influencerRepository;
@InjectMocks
private VideoService videoService;


@AfterEach
void tearDown() {
SecurityContextHolder.clearContext();
}

@Test
@DisplayName("getVideosBySurround Test")
void test1(){
// given
// 유저 정보 설정
setUser();
// 매개변수
VideoSearchParams videoSearchParams = new VideoSearchParams(
"10.0",
Expand Down Expand Up @@ -100,8 +109,8 @@ void test1(){
videoSearchParams.bottomRightLatitude(),
videoSearchParams.longitude(),
videoSearchParams.latitude(),
new ArrayList<>(),
new ArrayList<>(),
null,
null,
pageable)
).willAnswer(invocation -> places);

Expand Down Expand Up @@ -154,10 +163,9 @@ void test2(){
// method 1 실행 결과 값 설정
Pageable pageable = PageRequest.of(0, 10);
List<Video> videoList = Arrays.asList(video2, video1);
Page<Video> videos = new PageImpl<>(videoList, pageable, 2);

given(videoRepository.findAllByOrderByIdDesc(pageable)).willAnswer(
invocation -> videos
given(videoRepository.findTop10ByOrderByIdDesc(pageable)).willAnswer(
invocation -> videoList
);
// when
List<VideoInfo> videoInfos = videoService.getAllVideosDesc();
Expand Down Expand Up @@ -202,11 +210,10 @@ void test3() {
Video video3 = Video.from(influencers.get(1), place1, "url_3");
Video video4 = Video.from(influencers.get(2), place1, "url_4");
List<Video> videoList = Arrays.asList(video1, video2, video3);
Page<Video> videos = new PageImpl<>(videoList, pageable, 3);

// method 1 설정
given(videoRepository.findVideosByInfluencerIdIn(ids, pageable)).willAnswer(
invocation -> videos
given(videoRepository.findTop10ByInfluencerIdIn(ids, pageable)).willAnswer(
invocation -> videoList
);

// when
Expand Down Expand Up @@ -260,4 +267,11 @@ void test4(){
//
Assertions.assertThat(videoInfos.getContent().size()).isEqualTo(2);
}

private void setUser() {
CustomOAuth2User customOAuth2User = new CustomOAuth2User("test", 1L, Role.USER.getRoles());
Authentication authentication = new UsernamePasswordAuthenticationToken(customOAuth2User,
null);
SecurityContextHolder.getContext().setAuthentication(authentication);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class VideoRepositoryTest {
private VideoRepository videoRepository;
@Autowired
private PlaceRepository placeRepository;
private Pageable pageable = PageRequest.of(0, 10);
private final Pageable pageable = PageRequest.of(0, 10);

@BeforeEach
void init() {
Expand Down Expand Up @@ -106,34 +106,34 @@ void test1() {
List<Long> influencerIds = new ArrayList<>();
influencerIds.add(1L);

Page<Video> savedVideos = videoRepository.findVideosByInfluencerIdIn(
List<Video> savedVideos = videoRepository.findTop10ByInfluencerIdIn(
influencerIds,
pageable
);
// then
Assertions.assertThat(savedVideos.getContent().size()).isEqualTo(3);
Assertions.assertThat(savedVideos.size()).isEqualTo(3);
}

@Test
@DisplayName("findAllByOrderByIdDesc Test")
@DisplayName("findTop10ByOrderByIdDesc Test")
void test2() {
// given
/* Before Each */

// when
Page<Video> videos = videoRepository.findAllByOrderByIdDesc(pageable);
List<Video> videos = videoRepository.findTop10ByOrderByIdDesc(pageable);

// then
Long number = 5L;
for (Video video : videos.getContent()) {
for (Video video : videos) {
Assertions.assertThat(video.getId()).isEqualTo(number);
number -= 1L;
}
}

@Test
@DisplayName("findAllByPlaceIsNull Test")
void test3(){
void test3() {
// given

// when
Expand All @@ -148,7 +148,7 @@ void test4() {
// given

// when
Place place = placeRepository.findById(1L).orElseThrow(()->InplaceException.of(PlaceErrorCode.NOT_FOUND));
Place place = placeRepository.findById(1L).orElseThrow(() -> InplaceException.of(PlaceErrorCode.NOT_FOUND));
Video video = videoRepository.findTopByPlaceOrderByIdDesc(place).orElseThrow(NoSuchFieldError::new);

// then
Expand All @@ -158,7 +158,7 @@ void test4() {

@Test
@DisplayName("findByPlaceIdIn Test")
void test5(){
void test5() {
// given

// when
Expand All @@ -170,7 +170,7 @@ void test5(){

@Test
@DisplayName("findByPlaceId Test")
void test6(){
void test6() {
// given

// when
Expand All @@ -181,7 +181,7 @@ void test6(){

@Test
@DisplayName("findVideosByOrderByViewCountIncreaseDesc Test")
void test7(){
void test7() {
// given

// when
Expand All @@ -190,12 +190,12 @@ void test7(){
video1.updateViewCount(10L);
video2.updateViewCount(100L);

Page<Video> videos = videoRepository.findVideosByOrderByViewCountIncreaseDesc(pageable);
List<Video> videos = videoRepository.findTop10ByOrderByViewCountIncreaseDesc(PageRequest.of(0, 10));
// then
for (Video video : videos) {
System.out.println("video = " + video.getId() + " " + video.getVideoUrl() + " " + video.getViewCountIncrease());
}
Assertions.assertThat(videos.getContent().size()).isEqualTo(7);
Assertions.assertThat(videos.getContent().get(0)).isEqualTo(video2);
Assertions.assertThat(videos.size()).isEqualTo(5);
Assertions.assertThat(videos.get(0)).isEqualTo(video2);
}
}

0 comments on commit 45d8fd5

Please sign in to comment.