-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/Spring-Rest-Docs-설정-#98
- Loading branch information
Showing
11 changed files
with
204 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[[Forum-API]] | ||
== Forum API | ||
|
||
[[게시판-불러오기]] | ||
=== 게시판 불러오기 | ||
|
||
operation::forum-controller-test/게시판_목록_조회[snippets=http-request,path-parameters,request-fields,request-body] | ||
|
||
==== 성공 | ||
|
||
operation::forum-controller-test/게시판_목록_조회[snippets='response-fields-data,response-body'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
= REST Docs | ||
:asciidoc: src/docs/asciidoc | ||
:toc: left | ||
:toclevels: 2 | ||
:source-highlighter: highlightjs | ||
|
||
include::{asciidoc}/community/forum/forumApi.adoc[] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
wingle/src/test/java/kr/co/wingle/util/ControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package kr.co.wingle.util; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
@Disabled | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) | ||
@AutoConfigureMockMvc | ||
public abstract class ControllerTest { | ||
|
||
@Autowired | ||
protected ObjectMapper mapper; | ||
|
||
@Autowired | ||
protected MockMvc mockMvc; | ||
|
||
protected String createJson(Object dto) throws JsonProcessingException { | ||
return mapper.writeValueAsString(dto); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
wingle/src/test/java/kr/co/wingle/util/RestDocsConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package kr.co.wingle.util; | ||
|
||
import static org.springframework.restdocs.snippet.Attributes.*; | ||
|
||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation; | ||
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; | ||
import org.springframework.restdocs.operation.preprocess.Preprocessors; | ||
|
||
@TestConfiguration | ||
public class RestDocsConfig { | ||
|
||
@Bean | ||
public RestDocumentationResultHandler write() { | ||
return MockMvcRestDocumentation.document( | ||
// 조각이 생성되는 디렉토리 명을 클래스명/메서드 명으로 정함 | ||
"{class-name}/{method-name}", | ||
// json pretty하게 출력 | ||
Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), | ||
Preprocessors.preprocessResponse(Preprocessors.prettyPrint()) | ||
); | ||
} | ||
|
||
// 제약조건 | ||
public static final Attribute field( | ||
final String key, | ||
final String value) { | ||
return new Attribute(key, value); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
wingle/src/test/java/kr/co/wingle/util/RestDocsTestSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package kr.co.wingle.util; | ||
|
||
import static org.springframework.restdocs.payload.PayloadDocumentation.*; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.restdocs.RestDocumentationContextProvider; | ||
import org.springframework.restdocs.RestDocumentationExtension; | ||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation; | ||
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; | ||
import org.springframework.restdocs.payload.FieldDescriptor; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import org.springframework.web.filter.CharacterEncodingFilter; | ||
|
||
@Disabled | ||
@Import(RestDocsConfig.class) | ||
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class}) | ||
public class RestDocsTestSupport extends ControllerTest { | ||
|
||
@Autowired | ||
protected RestDocumentationResultHandler restDocs; | ||
|
||
@BeforeEach | ||
void setUp(final WebApplicationContext context, | ||
final RestDocumentationContextProvider provider) { | ||
this.mockMvc = MockMvcBuilders.webAppContextSetup(context) | ||
.apply(MockMvcRestDocumentation.documentationConfiguration(provider)) // rest docs 설정 주입 | ||
.alwaysDo(MockMvcResultHandlers.print()) // andDo(print()) 코드 포함 | ||
.alwaysDo(restDocs) // pretty 패턴과 문서 디렉토리 명 정해준것 적용 | ||
.addFilters(new CharacterEncodingFilter("UTF-8", true)) // 한글 깨짐 방지 | ||
.build(); | ||
} | ||
|
||
protected RestDocumentationResultHandler getResponseFields(FieldDescriptor[] data) { | ||
return restDocs.document( | ||
responseFields(beneathPath("data") // response의 data 필드 하위 내용 문서화 | ||
.withSubsectionId("data") // response-fields-data.adoc 파일 생성 | ||
, data) | ||
); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
wingle/src/test/resources/org/springframework/restdocs/templates/request-fields.snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//@formatter:off | ||
|=== | ||
|필드명|타입|필수여부|제약조건|설명 | ||
{{#fields}} | ||
|{{#tableCellContent}}`+{{path}}+`{{/tableCellContent}} | ||
|{{#tableCellContent}}`+{{type}}+`{{/tableCellContent}} | ||
|{{#tableCellContent}}{{^optional}}true{{/optional}}{{#optional}}false{{/optional}}{{/tableCellContent}} | ||
|{{#tableCellContent}}{{#constraints}}{{.}}{{/constraints}}{{/tableCellContent}} | ||
|{{#tableCellContent}}{{description}}{{/tableCellContent}} | ||
{{/fields}} | ||
|=== |
9 changes: 9 additions & 0 deletions
9
wingle/src/test/resources/org/springframework/restdocs/templates/request-parameters.snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//@formatter:off | ||
|=== | ||
|파라미터|필수여부|설명 | ||
{{#parameters}} | ||
|{{#tableCellContent}}`+{{name}}+`{{/tableCellContent}} | ||
|{{#tableCellContent}}{{^optional}}true{{/optional}}{{#optional}}false{{/optional}}{{/tableCellContent}} | ||
|{{#tableCellContent}}{{description}}{{/tableCellContent}} | ||
{{/parameters}} | ||
|=== |
10 changes: 10 additions & 0 deletions
10
wingle/src/test/resources/org/springframework/restdocs/templates/response-fields.snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//@formatter:off | ||
|=== | ||
|필드명|타입|필수여부|설명 | ||
{{#fields}} | ||
|{{#tableCellContent}}`+{{path}}+`{{/tableCellContent}} | ||
|{{#tableCellContent}}`+{{type}}+`{{/tableCellContent}} | ||
|{{#tableCellContent}}{{^optional}}true{{/optional}}{{#optional}}false{{/optional}}{{/tableCellContent}} | ||
|{{#tableCellContent}}{{description}}{{/tableCellContent}} | ||
{{/fields}} | ||
|=== |