Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pet 80 chore : RestDocs 설정추가 #31

Merged
merged 3 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
590 changes: 590 additions & 0 deletions Api-Module/src/main/resources/static/docs/Auth-API.html

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Auth-Module/auth-adaptor/src/docs/asciidoc/Auth-API.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[[Auth-API]]
== Auth-API

[[Auth-소셜로그인]]
=== Auth 소셜 로그인

operation::o-auth-controller-test/o-auth-login[snippents='http-request.adoc,http-response.adoc']
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.pawith.authmodule.adaptor.api;

import com.navercorp.fixturemonkey.FixtureMonkey;
import com.navercorp.fixturemonkey.api.introspector.ConstructorPropertiesArbitraryIntrospector;
import com.pawith.authmodule.application.dto.OAuthResponse;
import com.pawith.authmodule.application.dto.Provider;
import com.pawith.authmodule.application.port.in.OAuthUseCase;
import com.pawith.testmodule.BaseRestDocsTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;

import static org.mockito.BDDMockito.given;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@Slf4j
@WebMvcTest(OAuthController.class)
@DisplayName("OAuthController 테스트")
class OAuthControllerTest extends BaseRestDocsTest {

private static final String OAUTH_URL = "/oauth/{provider}";
private static final String OAUTH_REQUEST_ACCESS_TOKEN_PARAM_NAME = "accessToken";

@MockBean
OAuthUseCase oAuthUseCase;

@Test
@DisplayName("OAuth 로그인")
void oAuthLogin() throws Exception {
//given
final Provider testProvider = FixtureMonkey.create().giveMeOne(Provider.class);
final String OAUTH_ACCESS_TOKEN = FixtureMonkey.create().giveMeOne(String.class);
final OAuthResponse testOAuthResponse = getFixtureMonkey().giveMeOne(OAuthResponse.class);
MockHttpServletRequestBuilder request = get(OAUTH_URL, testProvider)
.param(OAUTH_REQUEST_ACCESS_TOKEN_PARAM_NAME, OAUTH_ACCESS_TOKEN);
given(oAuthUseCase.oAuthLogin(testProvider, OAUTH_ACCESS_TOKEN)).willReturn(testOAuthResponse);
//when
ResultActions result = mvc.perform(request);
//then
result.andExpect(status().isOk())
.andDo(print())
.andDo(resultHandler.document(
requestParameters(
parameterWithName(OAUTH_REQUEST_ACCESS_TOKEN_PARAM_NAME).description("OAuth 접근 토큰")
),
pathParameters(
parameterWithName("provider").description("OAuth 제공자")
),
responseFields(
fieldWithPath("accessToken").description("access 토큰"),
fieldWithPath("refreshToken").description("refresh 토큰")
)
));

}

private FixtureMonkey getFixtureMonkey() {
return FixtureMonkey.builder()
.objectIntrospector(ConstructorPropertiesArbitraryIntrospector.INSTANCE)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pawith.authmodule.adaptor.api;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestSpringBootApplicationConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class IgnoredPathConsts {

@Getter
private static Map<String, HttpMethod> ignoredPath = Map.of(
"/docs/**", HttpMethod.GET,
"/oauth/**", HttpMethod.GET,
"/jwt", HttpMethod.GET,
"/actuator/**", HttpMethod.GET
Expand Down
2 changes: 2 additions & 0 deletions Auth-Module/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
subprojects {
dependencies {
implementation project(":Common-Module")
testImplementation project(":Common-Module:Test-Module")


// spring security
implementation "org.springframework.boot:spring-boot-starter-security"
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.pawith.testmodule;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
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.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;

@AutoConfigureRestDocs
@Import({RestDocsConfig.class})
@ExtendWith(RestDocumentationExtension.class)
public class BaseRestDocsTest {

@Autowired
protected RestDocumentationResultHandler resultHandler;

protected MockMvc mvc;

@BeforeEach
void setUp(final WebApplicationContext applicationContext,
final RestDocumentationContextProvider provider){
this.mvc = MockMvcBuilders.webAppContextSetup(applicationContext)
.apply(MockMvcRestDocumentation.documentationConfiguration(provider).uris().withPort(8080))
.alwaysDo(MockMvcResultHandlers.print())
.alwaysDo(resultHandler)
.addFilters(new CharacterEncodingFilter("UTF-8", true))
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.pawith.testmodule;

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 restDocumentationResultHandler(){
return MockMvcRestDocumentation.document(
"{class-name}/{method-name}",
Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
Preprocessors.preprocessResponse(Preprocessors.prettyPrint())
);
}
}
46 changes: 29 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ allprojects {
repositories {
mavenCentral()
}

}

springBoot {
Expand All @@ -32,9 +33,16 @@ subprojects {
bootJar.enabled=true
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
asciidoctorExtensions
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-test'
// jpa
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.mysql:mysql-connector-j'
Expand All @@ -50,34 +58,38 @@ subprojects {
testAnnotationProcessor 'org.projectlombok:lombok'

// restdocs
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
asciidoctorExtensions 'org.springframework.restdocs:spring-restdocs-asciidoctor'
implementation 'org.springframework.restdocs:spring-restdocs-mockmvc'

// Fixture testing tool
testImplementation("com.navercorp.fixturemonkey:fixture-monkey-starter:0.6.3")
}


configurations {
compileOnly {
extendsFrom annotationProcessor
}
// RestDocs 설정
ext {
snippetsDir = file('build/generated-snippets')
}

repositories {
mavenCentral()
test {
useJUnitPlatform()
outputs.dir snippetsDir
}

ext {
set('snippetsDir', file("build/generated-snippets"))
asciidoctor {
dependsOn test
configurations 'asciidoctorExtensions'
inputs.dir snippetsDir

baseDirFollowsSourceFile()
}

tasks.named('test') {
outputs.dir snippetsDir
useJUnitPlatform()
tasks.register("asciidoctorToApiModule", Copy){
dependsOn asciidoctor
from file("build/docs/asciidoc/")
into rootProject.file("Api-Module/src/main/resources/static/docs")
}

tasks.named('asciidoctor') {
inputs.dir snippetsDir
dependsOn test
build{
dependsOn asciidoctorToApiModule
}
}
4 changes: 1 addition & 3 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,4 @@ include 'Domain-Module:User-Module:user-application'
findProject('Domain-Module:User-Module:user-application')?.name = 'user-application'

include 'Common-Module'
include 'Domain-Module:User-Module:user-application:test'
findProject(':Domain-Module:User-Module:user-application:test')?.name = 'test'

include 'Common-Module:Test-Module'