-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from Lotte-Feelmycode/feature/setting
[feature/setting] Spring Rest Docs, Logging설정 추가 및 테스트
- Loading branch information
Showing
5 changed files
with
184 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,97 @@ | ||
buildscript { | ||
ext { | ||
restdocsApiSpecVersion = '0.16.2' | ||
} | ||
} | ||
plugins { | ||
id 'org.springframework.boot' version '2.7.3' | ||
id 'io.spring.dependency-management' version '1.0.13.RELEASE' | ||
id 'java' | ||
id 'org.asciidoctor.convert' version '1.5.9.2' | ||
id 'com.epages.restdocs-api-spec' version '0.11.3' | ||
} | ||
|
||
group = 'com.feelmycode.parabole' | ||
version = '0.0.1-SNAPSHOT' | ||
sourceCompatibility = '17' | ||
|
||
configurations { | ||
compileOnly { | ||
extendsFrom annotationProcessor | ||
} | ||
all { | ||
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' | ||
} | ||
compileOnly { | ||
extendsFrom annotationProcessor | ||
} | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
implementation 'org.springframework.boot:spring-boot-starter-validation' | ||
implementation 'mysql:mysql-connector-java' | ||
implementation 'junit:junit:4.13.1' | ||
compileOnly 'org.projectlombok:lombok' | ||
annotationProcessor 'org.projectlombok:lombok' | ||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
implementation 'org.springframework.boot:spring-boot-starter-validation' | ||
implementation 'mysql:mysql-connector-java' | ||
implementation 'junit:junit:4.13.1' | ||
implementation 'com.epages:restdocs-api-spec-mockmvc:0.16.2' | ||
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.3' | ||
implementation 'org.springframework.boot:spring-boot-starter-log4j2' | ||
|
||
asciidoctor 'org.springframework.restdocs:spring-restdocs-asciidoctor' | ||
|
||
compileOnly 'org.projectlombok:lombok' | ||
|
||
annotationProcessor 'org.projectlombok:lombok' | ||
|
||
testImplementation 'org.springframework.boot:spring-boot-starter-test' | ||
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc' | ||
} | ||
|
||
ext { | ||
set('snippetsDir', file("build/generated-snippets")) | ||
} | ||
|
||
asciidoctor { | ||
inputs.dir snippetsDir | ||
dependsOn test | ||
} | ||
|
||
test { | ||
outputs.dir snippetsDir | ||
useJUnitPlatform() | ||
} | ||
|
||
bootJar { | ||
dependsOn asciidoctor | ||
|
||
copy { | ||
from "build/generated_snippets" | ||
into "src/main/resources/static/docs/" | ||
} | ||
} | ||
|
||
tasks.named('test') { | ||
useJUnitPlatform() | ||
useJUnitPlatform() | ||
} | ||
|
||
openapi3 { | ||
server = 'http://localhost:8080' | ||
title = 'The Parabole' | ||
description = 'The Parabole API description' | ||
version = '0.1.0' | ||
format = 'yaml' | ||
outputDirectory = "." | ||
outputFileNamePrefix = "swagger" | ||
copy { | ||
from "build/api-spec" | ||
into "src/main/resources/static/docs" | ||
} | ||
} | ||
processResources.dependsOn('ymlSecurity') | ||
|
||
task ymlSecurity(type: Copy) { | ||
from './src/main/resources/submodule-security' | ||
include '*.yml' | ||
into './src/main/resources' | ||
from './src/main/resources/submodule-security' | ||
include '*.yml' | ||
into './src/main/resources' | ||
} | ||
|
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,7 @@ | ||
= Product | ||
|
||
=== 상품 목록 | ||
operation::product-list[snippets='http-request,path-parameters,http-response,response-fields'] | ||
|
||
=== 상품 등록 | ||
operation::product[snippets='http-request,path-parameters,http-response,response-fields'] |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<Configuration status="INFO"> | ||
<Properties> | ||
<Property name="LOG_PATTERN">%d{HH:mm:ss.SSSZ} [%t] %-5level %logger{36} - %msg%n</Property> | ||
</Properties> | ||
<Appenders> | ||
<Console name="ConsoleLog" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="${LOG_PATTERN}" charset="UTF-8"/> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Root level="info"> | ||
<AppenderRef ref="ConsoleLog" /> | ||
<AppenderRef ref="FileLog" /> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
78 changes: 78 additions & 0 deletions
78
src/test/java/com/feelmycode/parabole/controller/ProductControllerTest.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,78 @@ | ||
package com.feelmycode.parabole.controller; | ||
|
||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; | ||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; | ||
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; | ||
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse; | ||
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.feelmycode.parabole.domain.Product; | ||
import com.feelmycode.parabole.service.ProductService; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.restdocs.JUnitRestDocumentation; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
@AutoConfigureRestDocs | ||
public class ProductControllerTest { | ||
|
||
private MockMvc mockMvc; | ||
@Autowired | ||
ObjectMapper objectMapper; | ||
@MockBean | ||
private ProductService productService; | ||
@Rule | ||
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(); | ||
@Autowired | ||
private WebApplicationContext context; | ||
|
||
@Before | ||
public void setUp() { | ||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) | ||
.apply(documentationConfiguration(this.restDocumentation)) | ||
.build(); | ||
} | ||
|
||
@Test | ||
@DisplayName("상품 목록 조회") | ||
public void getProductList() throws Exception{ | ||
this.mockMvc.perform(get("/api/v1/product/list")) | ||
.andDo(print()) | ||
.andDo(document("product-list", | ||
preprocessResponse(prettyPrint()))) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
public void createProduct() throws Exception { | ||
Product product = new Product(1L, 1L, 1, 50L, "국밥", "https://img.url", "순대국밥", 2000L); | ||
this.mockMvc.perform(post("/api/v1/product") | ||
.content(String.valueOf(MediaType.APPLICATION_JSON)) | ||
.content(this.objectMapper.writeValueAsString(product))) | ||
.andExpect(status().isOk()) | ||
.andDo(print()) | ||
.andDo(document("product", | ||
preprocessRequest(prettyPrint()), | ||
preprocessResponse(prettyPrint())) | ||
); | ||
} | ||
|
||
} |