Generate asciidoc

Generate asciidoc

1. Dependencies

 <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <scope>test</scope>
        </dependency>
        

rest-assured is for RestAssured.
e.g. get remote accesstoken by RestAssured

2. Plugin

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>${project.groupId}</groupId>
                                    <artifactId>${project.artifactId}</artifactId>
                                    <version>${project.version}</version>
                                    <!--  type>war</type-->
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-docs</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>src/main/asciidoc</sourceDirectory>
                            <backend>html</backend>
                            <attributes>
                                <snippets>${project.build.directory}/snippets</snippets>
                            </attributes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.outputDirectory}/static/docs
                            </outputDirectory>
                            <resources>
                                <resource>
                                    <directory>
                                        ${project.build.directory}/generated-docs
                                    </directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

3. Code Sample

package com.zihong;

import org.junit.Before;
import org.junit.Rule;

import io.restassured.RestAssured;
import io.restassured.response.Response;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.headers.HeaderDocumentation;
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.restdocs.operation.preprocess.Preprocessors;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.restdocs.request.RequestDocumentation;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.dxc.vpc.missioncontrol.app.Application;

import java.util.HashMap;
import java.util.Map;

import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = Application.class)
public class VendorControllerTest {

   @Rule
	public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("target/snippets");
	 
    @Autowired
    private WebApplicationContext wac;

    @Autowired
    private FilterChainProxy springSecurityFilterChain;
    
    private MockMvc mockMvc;

    private static final String CLIENT_ID = "clientId";
    private static final String CLIENT_SECRET = "secret";
    private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
    
    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders
        		.webAppContextSetup(this.wac)
        		.apply(MockMvcRestDocumentation.documentationConfiguration(this.restDocumentation)) 
        		.addFilter(springSecurityFilterChain).build();
    }

    @Test
    public void testGetVenderById() throws Exception {
        final String accessToken = obtainAccessToken(CLIENT_ID, "admin", "admin");
        System.out.println("token:" + accessToken);
        mockMvc.perform(RestDocumentationRequestBuilders.get("/api/v1.0/vendors/{id}", "123456789")
                .header("Authorization", "Bearer " + accessToken)
                .contentType(CONTENT_TYPE)
                .accept(CONTENT_TYPE))
                .andExpect(status().isOk())
				.andDo(MockMvcRestDocumentation.document("{class-name}/{method-name}", 
					HeaderDocumentation.requestHeaders( 
							headerWithName("Authorization").description(
									"Bearer + accessToken")),
					RequestDocumentation.pathParameters(RequestDocumentation.parameterWithName("id").description("The id of vendor")),
					responseFields(
							fieldWithPath("uuid").description("uuid").type(JsonFieldType.STRING),
							fieldWithPath("name").description("name").type(JsonFieldType.STRING),
							fieldWithPath("vendor_id").description("vendor_id").type(JsonFieldType.STRING),
							fieldWithPath("display_name").description("display_name").type(JsonFieldType.STRING),
							fieldWithPath("description").description("description").type(JsonFieldType.STRING),
							fieldWithPath("status").description("status").type(JsonFieldType.STRING),
							fieldWithPath("create_date").description("create_date").type(JsonFieldType.STRING),
							fieldWithPath("modified_date").description("modified_date").type(JsonFieldType.STRING),
							fieldWithPath("valid_until").description("valid_until").type(JsonFieldType.STRING),
							fieldWithPath("deleted").description("deleted").type(JsonFieldType.BOOLEAN)
							)
				));
    }

    private String obtainAccessToken(String clientId, String username, String password) {
        final Map<String, String> params = new HashMap<String, String>();
        params.put("grant_type", "password");
        params.put("client_id", clientId);
        params.put("username", username);
        params.put("password", password);
        RestAssured.useRelaxedHTTPSValidation();
        final Response response = RestAssured.given().auth().preemptive().basic(clientId, CLIENT_SECRET).and().with().params(params).when().post("https://204.104.116.185:8088/oauth/token");
        return response.jsonPath().getString("access_token");
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值