Skip to content

Commit

Permalink
Merge pull request #63 from Lotte-Feelmycode/feature/setting
Browse files Browse the repository at this point in the history
[feature/setting] Spring Rest Docs, Logging설정 추가 및 테스트
  • Loading branch information
chd830 authored Sep 27, 2022
2 parents 260f37b + dca9b09 commit b0eaf91
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 24 deletions.
88 changes: 71 additions & 17 deletions build.gradle
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'
}

7 changes: 7 additions & 0 deletions src/docs/asciidoc/example.adoc
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']
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import com.feelmycode.parabole.global.api.ParaboleResponse;
import com.feelmycode.parabole.global.error.exception.ParaboleException;
import com.feelmycode.parabole.service.ProductService;
import java.nio.charset.Charset;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -26,6 +24,7 @@
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/product")
@Slf4j
public class ProductController {

private final ProductService productService;
Expand Down Expand Up @@ -73,18 +72,23 @@ public ResponseEntity<ParaboleResponse> getProductList(@RequestParam(required =
// TODO: 셀러정보를 받아서 product 추가하기
@PostMapping
public ResponseEntity<ParaboleResponse> createProduct(@RequestBody Product product) {


log.info("TEST {}", "INFO");
log.warn("TEST {}", "WARN");
log.error("TEST {}", "ERROR");
log.error("TEST {}({})", "ERROR", "ERROR");

productService.saveProduct(product);

return ParaboleResponse.CommonResponse(HttpStatus.CREATED, true, "상품 생성");
}

// TODO: 셀러정보를 받아서 product 수정하기
@PatchMapping
public ResponseEntity<ParaboleResponse>updateProduct(@RequestBody Product product) {

productService.updateProduct(product);

HttpHeaders header = new HttpHeaders();
header.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));

return ParaboleResponse.CommonResponse(HttpStatus.OK, true, "상품 수정");
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/log4j2.xml
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>
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()))
);
}

}

0 comments on commit b0eaf91

Please sign in to comment.