Skip to content

Commit

Permalink
TEST : 리뷰 상세 댓글 정보 조회 컨트롤러 테스트 진행 (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
choidongkuen committed Dec 27, 2023
1 parent 9da1c74 commit 0e55b9a
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.List;

import static com.example.betteriter.global.common.code.status.ErrorStatus._METHOD_ARGUMENT_ERROR;
Expand Down Expand Up @@ -72,11 +71,19 @@ public ResponseDto<ReviewDetailResponse> getReviewDetail(
}

/* 리뷰 상세 좋아요 정보 조회 */
@GetMapping("/{reviewId}/detail/like")
public ResponseDto<List<ReviewLikeResponse>> getReviewDetailLike(
@GetMapping("/{reviewId}/detail/likes")
public ResponseDto<List<ReviewLikeResponse>> getReviewDetailLikes(
@PathVariable Long reviewId
) {
return ResponseDto.onSuccess(this.reviewService.getReviewDetailLikes(reviewId));
}

/* 리뷰 상세 댓글 정보 조회 */
@GetMapping("/{reviewId}/detail/comments")
public ResponseDto<List<ReviewCommentResponse>> getReviewDetailComments(
@PathVariable Long reviewId
) {
return ResponseDto.onSuccess(this.reviewService.getReviewDetailLike(reviewId));
return ResponseDto.onSuccess(this.reviewService.getReviewDetailComments(reviewId));
}

/* 리뷰 좋아요 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public void setReviewLikes(List<ReviewLike> reviewLikes) {
this.reviewLiked = reviewLikes;
}

public void setReviewsComment(List<Comment> comments) {
this.reviewComment = comments;
}

// 매주 월요일 자정 실행
@Scheduled(cron = "0 0 0 ? * MON")
public void resetClickCountsScheduler() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.betteriter.fo_domain.review.dto;

import com.example.betteriter.fo_domain.comment.domain.Comment;
import com.example.betteriter.fo_domain.user.domain.Users;
import com.example.betteriter.global.constant.Job;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -11,7 +12,7 @@

/**
* - 리뷰 댓글 데이터 응답 DTO
**/
**/
@Getter
@NoArgsConstructor
public class ReviewCommentResponse {
Expand All @@ -33,6 +34,16 @@ public ReviewCommentResponse(Long id, ReviewCommentUserInfoResponse reviewCommen
this.isMine = isMine;
}

public static ReviewCommentResponse from(Comment comment, boolean isMine) {
return ReviewCommentResponse.builder()
.id(comment.getId())
.reviewCommentUserInfoResponse(ReviewCommentUserInfoResponse.from(comment.getUsers()))
.comment(comment.getComment())
.createdAt(comment.getCreatedAt() == null ? null : comment.getCreatedAt().toLocalDate())
.isMine(isMine)
.build();
}

@Getter
@NoArgsConstructor
public static class ReviewCommentUserInfoResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.betteriter.bo_domain.menufacturer.service.ManufacturerService;
import com.example.betteriter.bo_domain.spec.service.SpecService;
import com.example.betteriter.fo_domain.comment.domain.Comment;
import com.example.betteriter.fo_domain.follow.service.FollowService;
import com.example.betteriter.fo_domain.review.domain.*;
import com.example.betteriter.fo_domain.review.dto.*;
Expand Down Expand Up @@ -141,7 +142,7 @@ public ReviewDetailResponse getReviewDetail(Long reviewId) {

/* 리뷰 상세 좋아요 조회 */
@Transactional(readOnly = true)
public List<ReviewLikeResponse> getReviewDetailLike(Long reviewId) {
public List<ReviewLikeResponse> getReviewDetailLikes(Long reviewId) {
// 1. reviewId 에 해당하는 리뷰 조회
Review review = this.findReviewById(reviewId);

Expand All @@ -151,6 +152,15 @@ public List<ReviewLikeResponse> getReviewDetailLike(Long reviewId) {
.collect(Collectors.toList());
}

/* 리뷰 상세 댓글 조회 */
@Transactional(readOnly = true)
public List<ReviewCommentResponse> getReviewDetailComments(Long reviewId) {
Users currentUser = this.getCurrentUser();
return this.findReviewById(reviewId).getReviewComment().stream()
.map(comment -> ReviewCommentResponse.from(comment, this.isCurrentUserCommentReview(comment, currentUser)))
.collect(Collectors.toList());
}

/* 리뷰 좋아요 */
@Transactional
public void reviewLike(Long reviewId) {
Expand Down Expand Up @@ -303,4 +313,9 @@ private boolean currentUserIsReviewWriter(Review review, Users currentUser) {
private boolean isCurrentUserFollowReviewWriter(Review review, Users currentUser) {
return this.followService.isFollow(currentUser, review.getWriter());
}

/* 댓글 작성자와 로그인한 유저가 동일한지 확인 */
private boolean isCurrentUserCommentReview(Comment comment, Users currentUser) {
return currentUser.getId().equals(comment.getUsers().getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ void getReviewDetailTest() throws Exception {
given(this.reviewService.getReviewDetail(anyLong()))
.willReturn(response);
// when & then
mockMvc.perform(get("/review/detail/{reviewId}", 1L))
mockMvc.perform(get("/review/{reviewId}/detail", 1L))
.andExpect(status().isOk())
.andDo(print())
.andExpect(jsonPath("$.isSuccess").value(true))
Expand Down Expand Up @@ -629,7 +629,7 @@ void getReviewDetailTest02() throws Exception {
given(this.reviewService.getReviewDetail(anyLong()))
.willReturn(response);
// when & then
mockMvc.perform(get("/review/detail/{reviewId}", 1L))
mockMvc.perform(get("/review/{reviewId}/detail/", 1L))
.andExpect(status().isOk())
.andDo(print())
.andExpect(jsonPath("$.isSuccess").value(true))
Expand Down Expand Up @@ -722,9 +722,8 @@ void getReviewDetailTest03() throws Exception {

@Test
@DisplayName("리뷰 상세 좋아요 조회를 한다.")
void test() throws Exception {
void getReviewDetailLikeControllerTest() throws Exception {
// given

ReviewLikeResponse response01 = ReviewLikeResponse.builder()
.userId(1L)
.nickname("nick01")
Expand All @@ -742,15 +741,49 @@ void test() throws Exception {

List<ReviewLikeResponse> response = List.of(response01, response02);

given(this.reviewService.getReviewDetailLike(anyLong()))
given(this.reviewService.getReviewDetailLikes(anyLong()))
.willReturn(response);
// when & then
mockMvc.perform(get("/review/{reviewId}/detail/like", 1L))
mockMvc.perform(get("/review/{reviewId}/detail/likes", 1L))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.isSuccess").value(true))
.andExpect(jsonPath("$.code").value("SUCCESS_200"))
.andExpect(jsonPath("$.result").isNotEmpty())
.andExpect(jsonPath("$.result[0].userId").value(1));
}

@Test
@DisplayName("리뷰 상세 댓글 조회를 한다.")
void getReviewDetailCommentsControllerTest() throws Exception {
// given


ReviewCommentResponse reviewCommentResponse01 = ReviewCommentResponse.builder()
.id(1L)
.reviewCommentUserInfoResponse(ReviewCommentResponse.ReviewCommentUserInfoResponse.builder()
.userId(1L)
.nickname("nick01")
.job(SW_DEVELOPER)
.profileImage("profileImage")
.build())
.comment("comment01")
.createdAt(LocalDate.now())
.isMine(true)
.build();

List<ReviewCommentResponse> response = List.of(reviewCommentResponse01);

given(this.reviewService.getReviewDetailComments(1L))
.willReturn(response);
// when & then
mockMvc.perform(get("/review/{reviewId}/detail/comments", 1L))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.isSuccess").value(true))
.andExpect(jsonPath("$.code").value("SUCCESS_200"))
.andExpect(jsonPath("$.result").isNotEmpty())
.andExpect(jsonPath("$.result[0].id").value(1))
.andExpect(jsonPath("$.result[0].reviewCommentUserInfo.userId").value(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.example.betteriter.bo_domain.spec.domain.Spec;
import com.example.betteriter.bo_domain.spec.domain.SpecData;
import com.example.betteriter.bo_domain.spec.service.SpecService;
import com.example.betteriter.fo_domain.comment.domain.Comment;
import com.example.betteriter.fo_domain.follow.service.FollowService;
import com.example.betteriter.fo_domain.review.domain.Review;
import com.example.betteriter.fo_domain.review.domain.ReviewImage;
Expand Down Expand Up @@ -122,7 +123,10 @@ private static Review createReview(long count) {
List<ReviewLike> reviewLikes = List.of(ReviewLike.builder().review(review).users(user01).build(),
ReviewLike.builder().review(review).users(user02).build());

List<Comment> comments = List.of(Comment.builder().users(user01).comment("comment01").orderNum(1).groupId(1).status(ACTIVE).build(),
Comment.builder().users(user02).comment("comment02").orderNum(2).groupId(2).status(ACTIVE).build());

review.setReviewsComment(comments);
review.setReviewLikes(reviewLikes);

review.setReviewImage(createReviewImage(review));
Expand Down Expand Up @@ -481,7 +485,7 @@ void reviewScrapServiceTest() {
}

@Test
@DisplayName("리뷰 상세 조회 좋아요 조회")
@DisplayName("리뷰 상세 조회 좋아요 조회을 한다.")
void getReviewDetailLike() {
// given
Review review = createReview(1L);
Expand All @@ -490,7 +494,7 @@ void getReviewDetailLike() {
.willReturn(Optional.of(review));

// when
List<ReviewLikeResponse> result = this.reviewService.getReviewDetailLike(1L);
List<ReviewLikeResponse> result = this.reviewService.getReviewDetailLikes(1L);

// then
assertThat(result).hasSize(2);
Expand All @@ -500,4 +504,26 @@ void getReviewDetailLike() {
System.out.println(reviewLikeResponse.getNickname());
}
}

@Test
@DisplayName("리뷰 상세 조회 댓글 조회를 한다.")
void test() {
// given
Review review = createReview(1L);

given(this.reviewRepository.findById(anyLong()))
.willReturn(Optional.of(review));

given(this.userService.getCurrentUser())
.willReturn(Users.builder().id(1L).email("email").roleType(ROLE_USER).build());
// when
List<ReviewCommentResponse> result = this.reviewService.getReviewDetailComments(1L);
// then
assertThat(result).hasSize(2);
verify(userService, times(1)).getCurrentUser();
for (ReviewCommentResponse reviewCommentResponse : result) {
System.out.println(reviewCommentResponse.getComment());
System.out.println(reviewCommentResponse.isMine());
}
}
}

0 comments on commit 0e55b9a

Please sign in to comment.