Commit 3715a72b authored by Selvakumar Kamatchinathan's avatar Selvakumar Kamatchinathan
Browse files

spring rest docs

parent cb0dcc0f
......@@ -105,6 +105,12 @@
<artifactId>springfox-bean-validators</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......@@ -136,6 +142,26 @@
</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>
<sourceDocumentName>index.adoc</sourceDocumentName>
<backend>html</backend>
<attributes>
<snippets>${project.build.directory}/generated-snippets/</snippets>
</attributes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
......
= Getting Started With Spring REST Docs
This is an example output for the registry service
:snippets: ../../../target/generated-snippets
=== Create Entity using POST Requests
==== Phenotype
.request
include::{snippets}/should-create-phenotype/1/http-request.adoc[]
.response
include::{snippets}/should-create-phenotype/1/http-response.adoc[]
==== Property
.request
include::{snippets}/should-create-property/1/http-request.adoc[]
.response
include::{snippets}/should-create-property/1/http-response.adoc[]
== Query Entity using GET Requests
==== Phenotype
.request
include::{snippets}/should-query-phenotype/1/http-request.adoc[]
.response
include::{snippets}/should-query-phenotype/1/http-response.adoc[]
==== Property
.request
include::{snippets}/should-query-properties/1/http-request.adoc[]
.response
include::{snippets}/should-query-properties/1/http-response.adoc[]
== Update Entity using PATCH/PUT Requests
==== Phenotype
.request
include::{snippets}/should-update-phenotype/2/http-request.adoc[]
.response
include::{snippets}/should-update-phenotype/2/http-response.adoc[]
==== Property
.request
include::{snippets}/should-update-property/2/http-request.adoc[]
.response
include::{snippets}/should-update-property/2/http-response.adoc[]
== Partial update Entity using PATCH/PUT Requests
==== Phenotype
.request
include::{snippets}/should-partially-update-phenotype/2/http-request.adoc[]
.response
include::{snippets}/should-partially-update-phenotype/2/http-response.adoc[]
==== Property
.request
include::{snippets}/should-partially-update-property/2/http-request.adoc[]
.response
include::{snippets}/should-partially-update-property/2/http-response.adoc[]
== Delete Entity using DELETE Requests
==== Phenotype
.request
include::{snippets}/should-delete-phenotype/2/http-request.adoc[]
.response
include::{snippets}/should-delete-phenotype/2/http-response.adoc[]
==== Property
.request
include::{snippets}/should-delete-property/2/http-request.adoc[]
.response
include::{snippets}/should-delete-property/2/http-response.adoc[]
== Test Authorization
==== Accessing secured endpoints without authentication
.request
include::{snippets}/test-authorization/1/http-request.adoc[]
.response
include::{snippets}/test-authorization/1/http-response.adoc[]
==== Accessing secured endpoints with authentication token
===== User with ROLE_READ
.request
include::{snippets}/test-authorization/11/http-request.adoc[]
.response
include::{snippets}/test-authorization/11/http-response.adoc[]
.request
include::{snippets}/test-authorization/14/http-request.adoc[]
.response
include::{snippets}/test-authorization/14/http-response.adoc[]
===== User with ROLE_EDITOR
.request
include::{snippets}/test-authorization/15/http-request.adoc[]
.response
include::{snippets}/test-authorization/15/http-response.adoc[]
.request
include::{snippets}/test-authorization/19/http-request.adoc[]
.response
include::{snippets}/test-authorization/19/http-response.adoc[]
===== User with ROLE_ADMIN
.request
include::{snippets}/test-authorization/20/http-request.adoc[]
.response
include::{snippets}/test-authorization/20/http-response.adoc[]
......@@ -18,26 +18,37 @@
package uk.ac.ebi.ampt2d.registry;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import uk.ac.ebi.ampt2d.registry.entities.Phenotype;
import uk.ac.ebi.ampt2d.registry.entities.Property;
import uk.ac.ebi.ampt2d.registry.repositories.PhenotypeRepository;
import uk.ac.ebi.ampt2d.registry.repositories.PropertyRepository;
import uk.ac.ebi.ampt2d.registry.service.mail.MailService;
import javax.annotation.Resource;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doNothing;
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.annotation.DirtiesContext.ClassMode.AFTER_CLASS;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
......@@ -49,12 +60,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@RunWith(SpringRunner.class)
@SpringBootTest(properties = {"security.enabled=true", "spring.jpa.hibernate.ddl-auto=none"})
@AutoConfigureMockMvc
@AutoConfigureJsonTesters
@DirtiesContext(classMode = AFTER_CLASS)
public class PropertyRegistryServiceApplicationTests {
@Autowired
@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("target/generated-snippets");
private MockMvc mockMvc;
@Autowired
......@@ -75,11 +87,24 @@ public class PropertyRegistryServiceApplicationTests {
@MockBean
private MailService mailService;
@Autowired
private WebApplicationContext webApplicationContext;
@Resource
private FilterChainProxy springSecurityFilterChain;
@Before
public void setUp() throws Exception {
doNothing().when(mailService).send(anyString());
phenotypeRepository.deleteAll();
propertyRepository.deleteAll();
mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).apply
(documentationConfiguration(restDocumentation))
.alwaysDo(document("{method-name}/{step}"
, preprocessRequest(prettyPrint())
, preprocessResponse(prettyPrint())))
.addFilters(springSecurityFilterChain)
.build();
}
@Test
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment