Skip to content

Commit

Permalink
Chore: Spring REST Docs 환경 세팅
Browse files Browse the repository at this point in the history
  • Loading branch information
chaewon-io committed Jul 29, 2024
1 parent d52c2e5 commit dcf63df
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
34 changes: 34 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'java'
id 'org.springframework.boot' version '3.3.1'
id 'io.spring.dependency-management' version '1.1.5'
id "org.asciidoctor.jvm.convert" version "3.3.2"
}

group = 'com.dnd'
Expand All @@ -14,6 +15,7 @@ java {
}

configurations {
asciidoctorExt
compileOnly {
extendsFrom annotationProcessor
}
Expand All @@ -32,8 +34,40 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

asciidoctorExt 'org.springframework.restdocs:spring-restdocs-asciidoctor'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
}

ext {
set('snippetsDir', file("build/generated-snippets"))
}

tasks.named('test') {
outputs.dir snippetsDir
useJUnitPlatform()
}

asciidoctor {
configurations 'asciidoctorExt'
baseDirFollowsSourceFile()
inputs.dir snippetsDir
dependsOn test
}

asciidoctor.doFirst {
delete file('src/main/resources/static/docs')
}

task createDocument(type: Copy) {
dependsOn asciidoctor
from file("build/docs/asciidoc")
into file("src/main/resources/static/docs")
}

bootJar {
dependsOn createDocument
from("${asciidoctor.outputDir}") {
into 'static/docs'
}
}
7 changes: 7 additions & 0 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
= Snappy REST Docs 문서

:doctype: book
:icons: font
:source-highlighter: highlightjs
:toc: left
:toclevels: 2
34 changes: 34 additions & 0 deletions src/test/java/com/dnd/dndphoto/config/ApiDocumentUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.dnd.dndphoto.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
import org.springframework.stereotype.Component;
import org.springframework.test.web.servlet.MockMvc;
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;

@Component
public class ApiDocumentUtils {

@Autowired
private RestDocumentationResultHandler restDocs;

// TODO: 컨트롤러 단위 or 통합 테스트
public MockMvc mockMvcSetup(final WebApplicationContext context,
final RestDocumentationContextProvider provider) {
return MockMvcBuilders.webAppContextSetup(context)
.apply(MockMvcRestDocumentation.documentationConfiguration(provider))
.alwaysDo(MockMvcResultHandlers.print())
.alwaysDo(restDocs)
.addFilters(new CharacterEncodingFilter("UTF-8", true))
.build();
}

public RestDocumentationResultHandler restDocs() {
return restDocs;
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/dnd/dndphoto/config/RestDocsConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dnd.dndphoto.config;

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;
import static org.springframework.restdocs.snippet.Attributes.Attribute;

@TestConfiguration
public class RestDocsConfiguration {

@Bean
public RestDocumentationResultHandler write() {
return MockMvcRestDocumentation.document(
"{class-name}/{method-name}",
Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
Preprocessors.preprocessResponse(Preprocessors.prettyPrint())
);
}

public static Attribute field(final String key, final String value) {
return new Attribute(key, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
==== Request Fields
|===
|Path|Type|Optional|Description

{{#fields}}

|{{#tableCellContent}}`+{{path}}+`{{/tableCellContent}}
|{{#tableCellContent}}`+{{type}}+`{{/tableCellContent}}
|{{#tableCellContent}}{{#optional}}O{{/optional}}{{/tableCellContent}}
|{{#tableCellContent}}{{description}}{{/tableCellContent}}

{{/fields}}

|===
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
==== Request Fields
|===
|Path|Type|Optional|Description

{{#fields}}

|{{#tableCellContent}}`+{{path}}+`{{/tableCellContent}}
|{{#tableCellContent}}`+{{type}}+`{{/tableCellContent}}
|{{#tableCellContent}}{{#optional}}O{{/optional}}{{/tableCellContent}}
|{{#tableCellContent}}{{description}}{{/tableCellContent}}

{{/fields}}

|===

0 comments on commit dcf63df

Please sign in to comment.