Skip to content

Commit

Permalink
add OpenAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin POCHAT committed Mar 27, 2022
1 parent df50a08 commit 1a881b7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 167 deletions.
56 changes: 22 additions & 34 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<java.version>14</java.version>
<spring-version>2.2.7.RELEASE</spring-version>
<spring-security-version>5.2.4.RELEASE</spring-security-version>
<spring-restdocs-asciidoctor-version>2.0.4.RELEASE</spring-restdocs-asciidoctor-version>
<springboot-maven-plugin-version>2.3.4.RELEASE</springboot-maven-plugin-version>
<springdoc-openapi-maven-plugin-version>1.3</springdoc-openapi-maven-plugin-version>
<spring-openapi-version>1.6.6</spring-openapi-version>
<springboot-maven-plugin-version>2.3.4.RELEASE</springboot-maven-plugin-version>
<liquibase-version>4.8.0</liquibase-version>
<jackson-version>2.10.4</jackson-version>
Expand Down Expand Up @@ -75,11 +77,6 @@
<artifactId>jackson-datatype-hibernate5</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand Down Expand Up @@ -115,6 +112,11 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -142,26 +144,14 @@
</plugin>

<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-docs</id>
<phase>package</phase>
<id>integration-test</id>
<goals>
<goal>process-asciidoc</goal>
<goal>generate</goal>
</goals>
<configuration>
<backend>html</backend>
<doctype>book</doctype>
<attributes>
<snippets>${snippetsDirectory}</snippets>
<preserveDirectories>true</preserveDirectories>
<toc/>
</attributes>
<sourceDirectory>src/docs/asciidocs</sourceDirectory>
<outputDirectory>target/generated-docs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Expand Down Expand Up @@ -204,11 +194,6 @@
<release>${java.version}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-version}</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand All @@ -228,6 +213,16 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-version}</version>
<configuration>
<environmentVariables>
<spring.jpa.properties.hibernate.dialect>org.hibernate.dialect.H2Dialect</spring.jpa.properties.hibernate.dialect>
</environmentVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-maven-plugin</artifactId>
<version>${springdoc-openapi-maven-plugin-version}</version>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
Expand Down Expand Up @@ -344,13 +339,6 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<version>${spring-restdocs-asciidoctor-version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

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;

import static org.hamcrest.Matchers.*;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;


@SpringBootTest
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
@ExtendWith({SpringExtension.class})
@Sql(value = {
"/sql/create/com/localeat/domains/security/security_test_data.sql",
"/sql/create/com/localeat/domains/farm/farm_test_data.sql",
Expand All @@ -44,10 +41,8 @@ public class TestAndDocDeliveryController {
private MockMvc mockMvc;

@BeforeEach
public void setUp(WebApplicationContext webApplicationContext,
RestDocumentationContextProvider restDocumentation) {
public void setUp(WebApplicationContext webApplicationContext) {
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(documentationConfiguration(restDocumentation))
.build();
}

Expand All @@ -64,11 +59,7 @@ public void getAllPublicDeliveriesWithoutAccessKey() throws Exception {
.andExpect(status().isOk())
.andExpect(jsonPath("$[?(@.id==1)]", not(empty())))
.andExpect(jsonPath("$[?(@.id==2)]", not(empty())))
.andExpect(jsonPath("$[?(@.id==3)]", empty()))
.andDo(document(
"get-public-deliveries",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
.andExpect(jsonPath("$[?(@.id==3)]", empty()));
}

@Test
Expand All @@ -84,11 +75,7 @@ public void getAllPublicDeliveriesWithAccessKey() throws Exception {
.andExpect(status().isOk())
.andExpect(jsonPath("$[?(@.id==1)]", not(empty())))
.andExpect(jsonPath("$[?(@.id==2)]", not(empty())))
.andExpect(jsonPath("$[?(@.id==3)]", not(empty())))
.andDo(document(
"get-public-deliveries-with-access-key",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
.andExpect(jsonPath("$[?(@.id==3)]", not(empty())));
}

@Test
Expand Down Expand Up @@ -232,11 +219,7 @@ public void getOrdersByDelivery() throws Exception {
" } ],\n" +
" \"orders\" : [ ]\n" +
" }\n" +
"} ]"))
.andDo(document(
"get-orders-by-delivery",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
"} ]"));
}

@Test
Expand All @@ -249,32 +232,24 @@ public void getOrdersByDeliveryWithUnauthorizedAccount() throws Exception {
.perform(get("/accounts/2/deliveries/1/orders")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isUnauthorized())
.andDo(document(
"get-orders-by-delivery-with-unauthorized-account",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
.andExpect(status().isUnauthorized());
}

@Test
public void saveDeliveryOrder() throws Exception {
// given
String requestBody =
"{\n" +
" \"id\" : 1,\n" +
" \"status\" : \"PAYED\"" +
" }";
" \"id\" : 1,\n" +
" \"status\" : \"PAYED\""+
" }";

// when, then
this.mockMvc
.perform(post("/accounts/1/deliveries/1/orders")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isOk())
.andDo(document(
"get-orders-by-delivery-with-unauthorized-account",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
.andExpect(status().isOk());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,19 @@
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
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.MockMvcResultMatchers.status;

@SpringBootTest
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
@ExtendWith({SpringExtension.class})
@Sql(value = {
"/sql/create/com/localeat/domains/security/security_test_data.sql",
"/sql/create/com/localeat/domains/farm/farm_test_data.sql",
Expand All @@ -44,11 +39,9 @@ public class TestAndDocOrderController {
private MockMvc mockMvc;

@BeforeEach
public void setUp(WebApplicationContext webApplicationContext,
RestDocumentationContextProvider restDocumentation) {
public void setUp(WebApplicationContext webApplicationContext) {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(documentationConfiguration(restDocumentation))
.build();
}

Expand All @@ -74,11 +67,7 @@ public void createOrder() throws Exception {
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isOk())
.andDo(document(
"create-order",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
.andExpect(status().isOk());
}

@Test
Expand Down Expand Up @@ -217,10 +206,6 @@ public void getOrderByCustomer() throws Exception {
" } ],\n" +
" \"orders\" : [ ]\n" +
" }\n" +
"} ]"))
.andDo(document(
"get-orders-by-customer",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
"} ]"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,19 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.restdocs.RestDocumentationContextProvider;
import org.springframework.restdocs.RestDocumentationExtension;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
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;

@SpringBootTest
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
@ExtendWith({SpringExtension.class})
@Sql(value = {
"/sql/create/com/localeat/domains/security/security_test_data.sql",
"/sql/create/com/localeat/domains/farm/farm_test_data.sql",
Expand All @@ -37,10 +32,8 @@ public class TestAndDocProductController {
private MockMvc mockMvc;

@BeforeEach
public void setUp(WebApplicationContext webApplicationContext,
RestDocumentationContextProvider restDocumentation) {
public void setUp(WebApplicationContext webApplicationContext) {
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(documentationConfiguration(restDocumentation))
.build();
}

Expand All @@ -60,11 +53,7 @@ public void getProductTemplatesByBreeder() throws Exception {
" \"id\" : 1,\n" +
" \"name\" : \"La ferme de la Riviere\"\n" +
" }\n" +
"} ]"))
.andDo(document(
"get-product-templates-by-breeder",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
"} ]"));
}


Expand All @@ -89,11 +78,7 @@ public void createProductTemplate() throws Exception {
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isOk())
.andDo(document(
"create-product-template",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
.andExpect(status().isOk());
}

@Test
Expand All @@ -112,11 +97,7 @@ public void getProductsByBreeder() throws Exception {
" \"id\" : 1,\n" +
" \"name\" : \"La ferme de la Riviere\"\n" +
" }\n" +
"} ]"))
.andDo(document(
"get-products-by-breeder",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
"} ]"));
}

@Test
Expand All @@ -140,10 +121,6 @@ public void createProduct() throws Exception {
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody))
.andExpect(status().isOk())
.andDo(document(
"create-product",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
.andExpect(status().isOk());
}
}
Loading

0 comments on commit 1a881b7

Please sign in to comment.