Skip to content

Commit

Permalink
REST Docs 커스텀 필드를 추가하고, document identifier를 자동 생성 설정한다. (#21)
Browse files Browse the repository at this point in the history
* refactor: test 후 generated-snippets 삭제

* feat: 출제된 시험 요약 목록 조회 테스트

* feat: 나의 시험 제출 요약 목록 조회 테스트

* refactor: asciidocs 태그 통일

* refactor: 테스트에서 page, next query parameter optional 추가

* refactor: document tag 자동 생성되게 변경

* refactor: query parameters, response fields에 optional 필드 추가

* refactor: asciidocs 자동 생성된 태그로 수정
  • Loading branch information
alstn113 committed Jan 10, 2025
1 parent 5c35bc7 commit aee24bd
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 93 deletions.
1 change: 1 addition & 0 deletions server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ dependencies {

test {
useJUnitPlatform()
delete snippetsDir // generated-snippets 디렉토리 삭제
outputs.dir snippetsDir
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/docs/asciidoc/auth.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

=== 나의 정보 조회

operation::api/v1/auth/get-my-info[]
operation::auth-document-test/get-my-info[]

=== 로그아웃

operation::api/v1/auth/logout[]
operation::auth-document-test/logout[]
22 changes: 13 additions & 9 deletions server/src/docs/asciidoc/exam.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,40 @@

=== 출제된 시험 요약 목록 조회

operation::api/v1/exams/get-published-exam-summaries[]
operation::exam-document-test/get-published-exam-summaries[]

=== 내가 출제한 시험 요약 목록 조회

operation::api/v1/exams/get-my-exam-summaries[]
operation::exam-document-test/get-my-exam-summaries[]

=== 시험 상세 조회

operation::api/v1/exams/get-exam-detail[]
operation::exam-document-test/get-exam-detail[]

=== 시험 상세 정보와 답안을 함께 조회

operation::api/v1/exams/get-exam-detail-with-answers[]
operation::exam-document-test/get-exam-detail-with-answers[]

=== 출제된 시험 요약 목록 조회

operation::exam-document-test/get-submitted-exam-summaries[]

=== 시험 생성

operation::api/v1/exams/create-exam[]
operation::exam-document-test/create-exam[]

=== 시험 출제

operation::api/v1/exams/publish[]
operation::exam-document-test/publish[]

=== 시험 문제 수정

operation::api/v1/exams/questions[]
operation::exam-document-test/update-questions[]

=== 시험 제목 수정

operation::api/v1/exams/update-title[]
operation::exam-document-test/update-title[]

=== 시험 설명 수정

operation::api/v1/exams/update-description[]
operation::exam-document-test/update-description[]
2 changes: 1 addition & 1 deletion server/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
:toclevels: 2
:sectlinks:

= Fluffy API 문서
= Fluffy API 명세서

include::auth.adoc[]
include::oauth2.adoc[]
Expand Down
4 changes: 2 additions & 2 deletions server/src/docs/asciidoc/oauth2.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

=== OAuth2 로그인 페이지로 리다이렉트

operation::api/v1/auth/oauth2/redirect[]
operation::o-auth2-document-test/oauth2-redirect[]

=== OAuth2 로그인 콜백 처리

operation::api/v1/auth/oauth2/callback[]
operation::o-auth2-document-test/oauth2-callback[]
10 changes: 7 additions & 3 deletions server/src/docs/asciidoc/submission.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

=== 시험 제출 요약 목록 조회

operation::api/v1/exams/get-submission-summaries[]
operation::submission-document-test/get-summaries[]

=== 시험 제출 상세 조회

operation::api/v1/exams/get-submission-detail[]
operation::submission-document-test/get-detail[]

=== 나의 시험 제출 요약 목록 조회

operation::submission-document-test/get-my-submission-summaries[]

=== 시험 제출

operation::api/v1/exams/submit[]
operation::submission-document-test/submit[]
10 changes: 2 additions & 8 deletions server/src/test/java/com/fluffy/auth/api/AuthDocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -47,13 +45,11 @@ void getMyInfo() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("access_token", "{ACCESS_TOKEN}"))
)
.andDo(print())
.andExpectAll(
status().isOk(),
content().json(objectMapper.writeValueAsString(response))
)
.andDo(document(
"api/v1/auth/get-my-info",
.andDo(restDocs.document(
responseFields(
fieldWithPath("id").description("사용자 식별자"),
fieldWithPath("email").description("사용자 이메일"),
Expand All @@ -80,13 +76,11 @@ void logout() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("access_token", "{ACCESS_TOKEN}"))
)
.andDo(print())
.andExpectAll(
status().isOk(),
header().doesNotExist("access_token")
)
.andDo(document(
"api/v1/auth/logout"
.andDo(restDocs.document(
));
}
}
105 changes: 69 additions & 36 deletions server/src/test/java/com/fluffy/exam/api/ExamDocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
Expand Down Expand Up @@ -38,6 +37,7 @@
import com.fluffy.exam.domain.ExamStatus;
import com.fluffy.exam.domain.dto.AuthorDto;
import com.fluffy.exam.domain.dto.ExamSummaryDto;
import com.fluffy.exam.domain.dto.SubmittedExamSummaryDto;
import com.fluffy.global.response.PageInfo;
import com.fluffy.global.response.PageResponse;
import com.fluffy.support.AbstractDocumentTest;
Expand Down Expand Up @@ -85,16 +85,14 @@ void getPublishedExamSummaries() throws Exception {
.param("size", "2")
.accept(MediaType.APPLICATION_JSON)
)
.andDo(print())
.andExpectAll(
status().isOk(),
content().json(objectMapper.writeValueAsString(response))
)
.andDo(document(
"api/v1/exams/get-published-exam-summaries",
.andDo(restDocs.document(
queryParameters(
parameterWithName("page").description("페이지 번호"),
parameterWithName("size").description("페이지당 항목 수")
parameterWithName("page").description("페이지 번호").optional(),
parameterWithName("size").description("페이지당 항목 수").optional()
),
responseFields(
fieldWithPath("pageInfo").description("페이지 정보"),
Expand Down Expand Up @@ -134,25 +132,20 @@ void getMyExamSummaries() throws Exception {
);
PageResponse<ExamSummaryDto> response = new PageResponse<>(pageInfo, summaries);

when(examQueryService.getPublishedExamSummaries(any()))
.thenReturn(response);

when(examQueryService.getPublishedExamSummaries(any()))
when(examQueryService.getMyExamSummaries(any(), any(), any()))
.thenReturn(response);

mockMvc.perform(get("/api/v1/exams")
mockMvc.perform(get("/api/v1/exams/mine")
.param("page", "0")
.param("size", "2")
.accept(MediaType.APPLICATION_JSON)
.cookie(new Cookie("accessToken", "{ACCESS_TOKEN}"))
)
.andDo(print())
.andExpect(status().isOk())
.andDo(document(
"api/v1/exams/get-published-exam-summaries",
.andDo(restDocs.document(
queryParameters(
parameterWithName("page").description("페이지 번호"),
parameterWithName("size").description("페이지당 항목 수")
parameterWithName("page").description("페이지 번호").optional(),
parameterWithName("size").description("페이지당 항목 수").optional()
),
responseFields(
fieldWithPath("pageInfo").description("페이지 정보"),
Expand Down Expand Up @@ -205,13 +198,11 @@ void getExamDetail() throws Exception {
.param("size", "2")
.accept(MediaType.APPLICATION_JSON)
)
.andDo(print())
.andExpectAll(
status().isOk(),
content().json(objectMapper.writeValueAsString(response))
)
.andDo(document(
"api/v1/exams/get-exam-detail",
.andDo(restDocs.document(
pathParameters(
parameterWithName("examId").description("시험 ID")
),
Expand Down Expand Up @@ -273,13 +264,11 @@ void getExamDetailWithAnswers() throws Exception {
.accept(MediaType.APPLICATION_JSON)
.cookie(new Cookie("accessToken", "{ACCESS_TOKEN}"))
)
.andDo(print())
.andExpectAll(
status().isOk(),
content().json(objectMapper.writeValueAsString(response))
)
.andDo(document(
"api/v1/exams/get-exam-detail-with-answers",
.andDo(restDocs.document(
pathParameters(
parameterWithName("examId").description("시험 ID")
),
Expand All @@ -304,6 +293,59 @@ void getExamDetailWithAnswers() throws Exception {
));
}

@Test
@DisplayName("출제된 시험 요약 목록을 조회할 수 있다.")
void getSubmittedExamSummaries() throws Exception {
PageInfo pageInfo = new PageInfo(0, 1, 1, false, false);
List<SubmittedExamSummaryDto> summaries = List.of(new SubmittedExamSummaryDto(
1L,
"시험1",
"설명1",
new AuthorDto(1L, "작성자1", "https://avatar.com"),
3L,
LocalDateTime.now())
);
PageResponse<SubmittedExamSummaryDto> response = new PageResponse<>(pageInfo, summaries);

when(examQueryService.getSubmittedExamSummaries(any(), any()))
.thenReturn(response);

mockMvc.perform(get("/api/v1/exams/submitted")
.param("page", "0")
.param("size", "9")
.cookie(new Cookie("accessToken", "{ACCESS_TOKEN}"))
)
.andExpectAll(
status().isOk(),
content().json(objectMapper.writeValueAsString(response))
)
.andDo(restDocs.document(
queryParameters(
parameterWithName("page").description("페이지 번호").optional(),
parameterWithName("size").description("페이지당 항목 수").optional()
),
responseFields(
fieldWithPath("pageInfo").description("페이지 정보"),
fieldWithPath("pageInfo.currentPage").description("현재 페이지 번호"),
fieldWithPath("pageInfo.totalPages").description("전체 페이지 수"),
fieldWithPath("pageInfo.totalElements").description("전체 항목 수"),
fieldWithPath("pageInfo.hasNext").description("다음 페이지 존재 여부"),
fieldWithPath("pageInfo.hasPrevious").description("이전 페이지 존재 여부"),

fieldWithPath("content").description("내용"),
fieldWithPath("content[].examId").description("시험 ID"),
fieldWithPath("content[].title").description("시험 제목"),
fieldWithPath("content[].description").description("시험 설명"),
fieldWithPath("content[].author").description("작성자 정보"),
fieldWithPath("content[].submissionCount").description("제출 수"),
fieldWithPath("content[].lastSubmissionDate").description("마지막 제출일"),
fieldWithPath("content[].author.id").description("작성자 ID"),
fieldWithPath("content[].author.name").description("작성자 이름"),
fieldWithPath("content[].author.avatarUrl").description("작성자 아바타 URL")
)
));
}

@Test
@DisplayName("시험을 생성할 수 있다.")
void createExam() throws Exception {
Expand All @@ -319,14 +361,12 @@ void createExam() throws Exception {
.cookie(new Cookie("accessToken", "{ACCESS_TOKEN}"))
.content(objectMapper.writeValueAsString(request))
)
.andDo(print())
.andExpectAll(
status().isCreated(),
header().string("Location", "/api/v1/exams/1"),
content().json(objectMapper.writeValueAsString(response))
)
.andDo(document(
"api/v1/exams/create-exam",
.andDo(restDocs.document(
requestFields(
fieldWithPath("title").description("시험 제목")
),
Expand Down Expand Up @@ -365,8 +405,7 @@ void publish() throws Exception {
.andExpectAll(
status().isOk()
)
.andDo(document(
"api/v1/exams/publish",
.andDo(restDocs.document(
pathParameters(
parameterWithName("examId").description("시험 ID")
),
Expand Down Expand Up @@ -405,12 +444,10 @@ void updateQuestions() throws Exception {
.cookie(new Cookie("accessToken", "{ACCESS_TOKEN}"))
.content(objectMapper.writeValueAsString(request))
)
.andDo(print())
.andExpectAll(
status().isNoContent()
)
.andDo(document(
"api/v1/exams/questions",
.andDo(restDocs.document(
pathParameters(
parameterWithName("examId").description("시험 ID")
),
Expand Down Expand Up @@ -438,10 +475,8 @@ void updateTitle() throws Exception {
.cookie(new Cookie("accessToken", "{ACCESS_TOKEN}"))
.content(objectMapper.writeValueAsString(request))
)
.andDo(print())
.andExpect(status().isOk())
.andDo(document(
"api/v1/exams/update-title",
.andDo(restDocs.document(
pathParameters(
parameterWithName("examId").description("시험 ID")
),
Expand All @@ -461,10 +496,8 @@ void updateDescription() throws Exception {
.cookie(new Cookie("accessToken", "{ACCESS_TOKEN}"))
.content(objectMapper.writeValueAsString(request))
)
.andDo(print())
.andExpect(status().isOk())
.andDo(document(
"api/v1/exams/update-description",
.andDo(restDocs.document(
pathParameters(
parameterWithName("examId").description("시험 ID")
),
Expand Down
Loading

0 comments on commit aee24bd

Please sign in to comment.