Skip to content

Commit

Permalink
refactor: LocalDateTime 요청/응답 formatting문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
Ohjintaek committed Aug 10, 2023
1 parent 76a3dd6 commit 07c05dc
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 26 deletions.
4 changes: 2 additions & 2 deletions backend/kirikiri/src/docs/asciidoc/roadmap.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ operation::roadmap-read-api-test/로드맵의_골룸_목록을_조건에_따라_

=== *9-1* 성공

operation::roadmap-read-api-test/로드맵의_리뷰들을_조회한다[snippets='http-request,http-response,request-fields,query-parameters,response-fields']
operation::roadmap-read-api-test/로드맵의_리뷰들을_조회한다[snippets='http-request,http-response,path-parameters,query-parameters,response-fields']

=== *9-1* 실패 - 존재하지 않는 로드맵인 경우

operation::roadmap-read-api-test/로드맵_리뷰_조회_시_유효하지_않은_로드맵_아이디일_경우_예외를_반환한다[snippets='http-request,http-response,path-parameters,request-fields,response-fields']
operation::roadmap-read-api-test/로드맵_리뷰_조회_시_유효하지_않은_로드맵_아이디일_경우_예외를_반환한다[snippets='http-request,http-response,path-parameters,query-parameters,response-fields']
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public ResponseEntity<List<RoadmapGoalRoomResponse>> findGoalRoomsByFilterType(
@GetMapping("/{roadmapId}/reviews")
public ResponseEntity<List<RoadmapReviewResponse>> findRoadmapReviews(
@PathVariable final Long roadmapId,
@RequestBody final CustomReviewScrollRequest reviewScrollRequest) {
@ModelAttribute final CustomReviewScrollRequest reviewScrollRequest
) {
final List<RoadmapReviewResponse> responses = roadmapReadService.findRoadmapReviews(roadmapId,
reviewScrollRequest);
return ResponseEntity.ok(responses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import jakarta.validation.constraints.NotNull;
import java.time.LocalDateTime;
import org.springframework.format.annotation.DateTimeFormat;

public record CustomReviewScrollRequest(
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS")
LocalDateTime lastCreatedAt,
Double lastReviewRate,
@NotNull(message = "사이즈를 입력해 주세요.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
public record RoadmapReviewResponse(
Long id,
MemberResponse member,
//@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS")
LocalDateTime createdAt,
String content,
Double rate
Expand Down
2 changes: 1 addition & 1 deletion backend/kirikiri/src/main/resources/properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;
Expand All @@ -22,7 +21,6 @@
import co.kirikiri.exception.NotFoundException;
import co.kirikiri.service.RoadmapCreateService;
import co.kirikiri.service.RoadmapReadService;
import co.kirikiri.service.dto.CustomReviewScrollRequest;
import co.kirikiri.service.dto.CustomScrollRequest;
import co.kirikiri.service.dto.ErrorResponse;
import co.kirikiri.service.dto.member.response.MemberResponse;
Expand Down Expand Up @@ -553,8 +551,6 @@ class RoadmapReadApiTest extends ControllerTestHelper {
@Test
void 로드맵의_리뷰들을_조회한다() throws Exception {
// given
final String reviewScrollRequest = objectMapper.writeValueAsString(
new CustomReviewScrollRequest(null, null, 10));
final List<RoadmapReviewResponse> expected = List.of(
new RoadmapReviewResponse(1L, new MemberResponse(1L, "작성자1", "image1-file-path"),
LocalDateTime.of(2023, 8, 15, 12, 30, 0, 123456), "리뷰 내용", 4.5),
Expand All @@ -568,20 +564,20 @@ class RoadmapReadApiTest extends ControllerTestHelper {
// when
final String response = mockMvc.perform(
get(API_PREFIX + "/roadmaps/{roadmapId}/reviews", 1L)
.content(reviewScrollRequest)
.param("size", "10")
.contentType(MediaType.APPLICATION_JSON)
.contextPath(API_PREFIX))
.andExpect(status().isOk())
.andDo(documentationResultHandler.document(
pathParameters(
parameterWithName("roadmapId").description("로드맵 아이디")
),
requestFields(
fieldWithPath("lastCreatedAt").optional()
queryParameters(
parameterWithName("lastCreatedAt").optional()
.description("이전 요청에서 받았던 리뷰 중 가장 옛날 날짜(첫 요청에는 없어도 상관없음)"),
fieldWithPath("lastReviewRate").optional()
.description("이전에 가장 마지막으로 조회한 리뷰의 별점(첫 요청에는 없어도 상관없음"),
fieldWithPath("size").description("한 번에 조회할 리뷰갯수")
parameterWithName("lastReviewRate").optional()
.description("이전에 가장 마지막으로 조회한 리뷰의 별점(첫 요청에는 없어도 상관없음)"),
parameterWithName("size").description("한 번에 조회할 리뷰갯수")
),
responseFields(
fieldWithPath("[0].id").description("리뷰 아이디"),
Expand Down Expand Up @@ -609,28 +605,26 @@ class RoadmapReadApiTest extends ControllerTestHelper {
@Test
void 로드맵_리뷰_조회__유효하지_않은_로드맵_아이디일_경우_예외를_반환한다() throws Exception {
// given
final String reviewScrollRequest = objectMapper.writeValueAsString(
new CustomReviewScrollRequest(null, null, 10));
when(roadmapReadService.findRoadmapReviews(anyLong(), any()))
.thenThrow(new NotFoundException("존재하지 않는 로드맵입니다. roadmapId = 1"));

// when
final String response = mockMvc.perform(
get(API_PREFIX + "/roadmaps/{roadmapId}/reviews", 1L)
.content(reviewScrollRequest)
.param("size", "10")
.contentType(MediaType.APPLICATION_JSON)
.contextPath(API_PREFIX))
.andExpect(status().isNotFound())
.andDo(documentationResultHandler.document(
pathParameters(
parameterWithName("roadmapId").description("로드맵 아이디")
),
requestFields(
fieldWithPath("lastCreatedAt").optional()
queryParameters(
parameterWithName("lastCreatedAt").optional()
.description("이전에 가장 마지막으로 조회한 리뷰의 생성일자(첫 요청에는 없어도 상관없음)"),
fieldWithPath("lastReviewRate").optional()
.description("이전에 가장 마지막으로 조회한 리뷰의 별점(첫 요청에는 없어도 상관없음"),
fieldWithPath("size").description("한 번에 조회할 리뷰갯수")
parameterWithName("lastReviewRate").optional()
.description("이전에 가장 마지막으로 조회한 리뷰의 별점(첫 요청에는 없어도 상관없음)"),
parameterWithName("size").description("한 번에 조회할 리뷰갯수")
),
responseFields(
fieldWithPath("message").description("예외 메시지")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -836,8 +837,8 @@ public RoadmapReadIntegrationTest(final MemberRepository memberRepository,
});

final LocalDateTime localDateTime = 첫번째_로드맵_리뷰_조회_응답값.get(1).createdAt();
final CustomReviewScrollRequest 두번째_스크롤_요청 = new CustomReviewScrollRequest(첫번째_로드맵_리뷰_조회_응답값.get(1).createdAt()
, null, 2);
final CustomReviewScrollRequest 두번째_스크롤_요청 = new CustomReviewScrollRequest(첫번째_로드맵_리뷰_조회_응답값.get(1).createdAt(),
null, 2);
final ExtractableResponse<Response> 두번째_로드맵_리뷰_조회_응답 = 로드맵_리뷰를_조회한다(저장된_로드맵.getId(), 두번째_스크롤_요청);
final List<RoadmapReviewResponse> 두번째_로드맵_리뷰_조회_응답값 = jsonToClass(두번째_로드맵_리뷰_조회_응답.asString(),
new TypeReference<>() {
Expand Down Expand Up @@ -1257,13 +1258,26 @@ public RoadmapReadIntegrationTest(final MemberRepository memberRepository,
}
private ExtractableResponse<Response> 로드맵_리뷰를_조회한다(final Long 로드맵_아이디, final CustomReviewScrollRequest 스크롤_요청) {
final String 시간_데이터_문자열 = LocalDateTime을_지정된_문자열_형식으로_변환한다(스크롤_요청.lastCreatedAt());
return given().log().all()
.contentType(MediaType.APPLICATION_JSON_VALUE)
.when()
.body(스크롤_요청)
.param("lastCreatedAt", 시간_데이터_문자열)
.param("lastReviewRate", 스크롤_요청.lastReviewRate())
.param("size", 스크롤_요청.size())
.get("/api/roadmaps/{roadmapId}/reviews", 로드맵_아이디)
.then()
.log().all()
.extract();
}
private String LocalDateTime을_지정된_문자열_형식으로_변환한다(final LocalDateTime 시간_데이터) {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
String 시간_데이터_문자열 = null;
if (시간_데이터 != null) {
시간_데이터_문자열 = 시간_데이터.format(formatter);
}
return 시간_데이터_문자열;
}
}

0 comments on commit 07c05dc

Please sign in to comment.