Skip to content

Commit

Permalink
[Weekly/11/Test/Review] ReviewController 단위테스트 작성 (#137)
Browse files Browse the repository at this point in the history
* feat: ReviewData 생성

* test: ReviewControllerUnitTest 작성
  • Loading branch information
ariimo authored Nov 14, 2024
1 parent 37cc442 commit edb8c7e
Show file tree
Hide file tree
Showing 11 changed files with 834 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public class ReviewController {

@GetMapping
public ResponseEntity<ApiResponseBody<ReviewSliceResponse>> getReviewsByMemberId(
@Authorize({MemberType.normal}) MemberIdentifier member,
@Authorize({MemberType.normal, MemberType.curator}) MemberIdentifier member,
@RequestParam(defaultValue = ParamDefaults.PAGE) Integer page,
@RequestParam(defaultValue = ParamDefaults.PAGE_SIZE) Integer size,
@RequestParam(defaultValue = ParamDefaults.LAST_ID) Long lastId) {
return ApiResponse.ok(
reviewService.getAllByMemberId(member.id(), lastId, PageRequest.of(page, size)));
reviewService.getAllByMemberId(member.id(), PageRequest.of(page, size), lastId));
}

@GetMapping("/events/{eventId}")
Expand All @@ -50,7 +50,7 @@ public ResponseEntity<ApiResponseBody<ReviewSliceResponse>> getReviewsByEventId(
@RequestParam(defaultValue = ParamDefaults.PAGE_SIZE) Integer size,
@RequestParam(defaultValue = ParamDefaults.LAST_ID) Long lastId) {
return ApiResponse.ok(
reviewService.getAllByEventId(eventId, lastId, PageRequest.of(page, size)));
reviewService.getAllByEventId(eventId, PageRequest.of(page, size), lastId));
}

@GetMapping("/{reviewId}")
Expand All @@ -61,32 +61,34 @@ public ResponseEntity<ApiResponseBody<ReviewResponse>> getReviewByReviewId(

@GetMapping("events")
public ResponseEntity<ApiResponseBody<ReviewEventSliceResponse>> getUnreviewedEventsByMemberId(
@Authorize(MemberType.normal) MemberIdentifier identifier,
@Authorize({MemberType.normal, MemberType.curator}) MemberIdentifier identifier,
@RequestParam(defaultValue = ParamDefaults.PAGE) Integer page,
@RequestParam(defaultValue = ParamDefaults.PAGE_SIZE) Integer size,
@RequestParam(defaultValue = ParamDefaults.LAST_ID) Long lastId){
return ApiResponse.ok(reviewService.getUnreviewedEventsByMemberId(identifier.id(), PageRequest.of(page, size), lastId));
@RequestParam(defaultValue = ParamDefaults.LAST_ID) Long lastId) {
return ApiResponse.ok(
reviewService.getUnreviewedEventsByMemberId(identifier.id(), PageRequest.of(page, size),
lastId));
}

@PostMapping
public ResponseEntity<ApiResponseBody<ReviewResponse>> createReview(
@Authorize(MemberType.normal) MemberIdentifier member,
@Authorize({MemberType.normal, MemberType.curator}) MemberIdentifier member,
@Valid @RequestBody ReviewCreateRequest reviewCreateRequest) {
return ApiResponse.created(reviewService.create(member.id(), reviewCreateRequest));
}

@PutMapping("/{reviewId}")
public ResponseEntity<ApiResponseBody<ReviewResponse>> updateReview(
@PathVariable("reviewId") Long reviewId,
@Authorize(MemberType.normal) MemberIdentifier member,
@Authorize({MemberType.normal, MemberType.curator}) MemberIdentifier member,
@Valid @RequestBody ReviewEditRequest reviewEditRequest) {
return ApiResponse.ok(reviewService.update(member.id(), reviewId, reviewEditRequest));
}

@DeleteMapping("/{reviewId}")
public ResponseEntity<ApiResponseBody<Void>> deleteReview(
@PathVariable("reviewId") Long reviewId,
@Authorize(MemberType.normal) MemberIdentifier member) {
@Authorize({MemberType.normal, MemberType.curator}) MemberIdentifier member) {
reviewService.delete(member.id(), reviewId);
return ApiResponse.noContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
import org.ktc2.cokaen.wouldyouin.review.persist.Review;

@Getter
@Builder
@Builder(toBuilder = true)
@EqualsAndHashCode
@ToString
public class ReviewCreateRequest {

@NotNull(message = "이벤트 ID는 필수입니다.")
private Long eventId;

@NotBlank(message = "별점은 필수입니다.")
@Min(value = 0, message = "별점은 0점 이상입니다. ")
@Max(value = 5, message = "별점은 5점 이하입니다. ")
private int score;
@NotNull(message = "별점은 필수입니다.")
@Min(value = 0, message = "별점은 0점 이상입니다.")
@Max(value = 5, message = "별점은 5점 이하입니다.")
private Integer score;

@NotBlank(message = "내용은 필수입니다. ")
@NotBlank(message = "내용은 필수입니다.")
@Size(min = 5, max = 50, message = "내용은 5자 이상 50자 이하입니다.")
private String content;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.EqualsAndHashCode;
Expand All @@ -15,12 +16,12 @@
@ToString
public class ReviewEditRequest {

@NotBlank(message = "별점은 필수입니다.")
@Min(value = 0, message = "별점은 0점 이상입니다. ")
@Max(value = 5, message = "별점은 5점 이하입니다. ")
@NotNull(message = "별점은 필수입니다.")
@Min(value = 0, message = "별점은 0점 이상입니다.")
@Max(value = 5, message = "별점은 5점 이하입니다.")
private Integer score;

@NotBlank(message = "내용은 필수입니다. ")
@NotBlank(message = "내용은 필수입니다.")
@Size(min = 5, max = 50, message = "내용은 5자 이상 50자 이하입니다.")
private String content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public ReviewResponse getById(Long reviewId) {
}

@Transactional(readOnly = true)
public ReviewSliceResponse getAllByMemberId(Long memberId, Long oldLastId, Pageable pageable) {
public ReviewSliceResponse getAllByMemberId(Long memberId, Pageable pageable, Long oldLastId) {
Slice<Review> reviews = reviewRepository.findByMemberIdOrderByReviewIdDesc(memberId,
oldLastId, pageable);
Long newLastId = getLastId(reviews, oldLastId);
return ReviewSliceResponse.from(reviews, reviews.getSize(), newLastId);
}

@Transactional(readOnly = true)
public ReviewSliceResponse getAllByEventId(Long eventId, Long oldLastId, Pageable pageable) {
public ReviewSliceResponse getAllByEventId(Long eventId, Pageable pageable, Long oldLastId) {
Slice<Review> reviews = reviewRepository.findByEventIdOrderByReviewIdDesc(eventId,
oldLastId, pageable);
Long newLastId = getLastId(reviews, oldLastId);
Expand All @@ -60,8 +60,10 @@ private Long getLastId(Slice<Review> reviews, Long oldLastId) {
}

@Transactional
public ReviewEventSliceResponse getUnreviewedEventsByMemberId(Long memberId, Pageable pageable, Long beforeLastId) {
Slice<Event> unreviewedEvents = reviewRepository.findUnreviewedEventsByMemberId(memberId, beforeLastId, pageable);
public ReviewEventSliceResponse getUnreviewedEventsByMemberId(Long memberId, Pageable pageable,
Long beforeLastId) {
Slice<Event> unreviewedEvents = reviewRepository.findUnreviewedEventsByMemberId(memberId,
beforeLastId, pageable);
Long newLastId = EventService.getLastId(unreviewedEvents, beforeLastId);
List<ReviewEventResponse> responses = unreviewedEvents.stream()
.map(ReviewEventResponse::from).toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.ktc2.cokaen.wouldyouin._global.testdata;

import org.ktc2.cokaen.wouldyouin._common.api.SliceInfo;
import org.ktc2.cokaen.wouldyouin._global.testdata.ReservationData.R.reservation1;
import org.ktc2.cokaen.wouldyouin._global.testdata.ReviewData.R.review1;

public class CommonData {

Expand All @@ -20,5 +22,25 @@ public static SliceInfo get() {
.build();
}
}

public static class reservation {

public static SliceInfo get() {
return SliceInfo.builder()
.sliceSize(10)
.lastId(reservation1.id)
.build();
}
}

public static class review {

public static SliceInfo get() {
return SliceInfo.builder()
.sliceSize(10)
.lastId(review1.id)
.build();
}
}
}
}
Loading

0 comments on commit edb8c7e

Please sign in to comment.