From de6d367e413f3ec302ac19030030e20c11fabe06 Mon Sep 17 00:00:00 2001 From: sanitaso Date: Fri, 25 Feb 2022 12:54:20 +0100 Subject: [PATCH] add files to springdoc --- .../swaggerResponseAPI/.gitignore | 33 ++++++++ .../swaggerResponseAPI/pom.xml | 69 +++++++++++++++++ .../SwaggerResponseApiApplication.java | 13 ++++ .../controller/ProductController.java | 39 ++++++++++ .../swaggerresponseapi/model/Product.java | 15 ++++ .../service/ProductService.java | 21 +++++ .../src/main/resources/application.properties | 1 + .../ProductIntegrationTest.java | 76 +++++++++++++++++++ .../SwaggerResponseApiApplicationTests.java | 13 ++++ 9 files changed, 280 insertions(+) create mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/.gitignore create mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/pom.xml create mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java create mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java create mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java create mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java create mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/resources/application.properties create mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/test/java/com/baeldung/swaggerresponseapi/ProductIntegrationTest.java create mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/test/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplicationTests.java diff --git a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/.gitignore b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/.gitignore new file mode 100644 index 0000000000..f60f3beecd --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +.mvn/wrapper/ +!**/src/main/**/target/ +!**/src/test/**/target/ +mvnv* + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/pom.xml b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/pom.xml new file mode 100644 index 0000000000..17544290e4 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + + com.baeldung.spring-boot-modules + spring-boot-disable-logging + 1.0.0-SNAPSHOT + + com.baeldung + swaggerResponseAPI + 0.0.1-SNAPSHOT + swaggerResponseAPI + swaggerResponseAPI + + 11 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + org.projectlombok + lombok + true + + + org.springdoc + springdoc-openapi-ui + 1.6.6 + + + junit + junit + 4.13.2 + test + + + com.fasterxml.jackson.core + jackson-databind + 2.13.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java new file mode 100644 index 0000000000..913544b459 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.swaggerresponseapi; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SwaggerResponseApiApplication { + + public static void main(String[] args) { + SpringApplication.run(SwaggerResponseApiApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java new file mode 100644 index 0000000000..3e9a3291d9 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java @@ -0,0 +1,39 @@ +package com.baeldung.swaggerresponseapi.controller; + +import com.baeldung.swaggerresponseapi.model.Product; +import com.baeldung.swaggerresponseapi.service.ProductService; + +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +public class ProductController { + private final ProductService productService; + + public ProductController(ProductService productService) { + this.productService = productService; + } + + @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Product successfully added!") }) + @PostMapping("/create") + public Product addProduct(@RequestBody Product product) { + return productService.addProducts(product); + } + + @ApiResponses(value = { @ApiResponse(content = { @Content(mediaType = "application/json", + array = @ArraySchema(schema = @Schema(implementation = Product.class))) }) }) + @GetMapping("/products") + public List getProductsList() { + return productService.getProductsList(); + } +} diff --git a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java new file mode 100644 index 0000000000..0a100582fc --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java @@ -0,0 +1,15 @@ +package com.baeldung.swaggerresponseapi.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class Product { + String code; + String name; +} diff --git a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java new file mode 100644 index 0000000000..5e7533d6f4 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java @@ -0,0 +1,21 @@ +package com.baeldung.swaggerresponseapi.service; + +import com.baeldung.swaggerresponseapi.model.Product; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class ProductService { + List productsList = new ArrayList<>(); + + public Product addProducts(Product product) { + productsList.add(product); + return product; + } + + public List getProductsList() { + return productsList; + } +} diff --git a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/resources/application.properties b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/resources/application.properties new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/test/java/com/baeldung/swaggerresponseapi/ProductIntegrationTest.java b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/test/java/com/baeldung/swaggerresponseapi/ProductIntegrationTest.java new file mode 100644 index 0000000000..ef6a8953e2 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/test/java/com/baeldung/swaggerresponseapi/ProductIntegrationTest.java @@ -0,0 +1,76 @@ +package com.baeldung.swaggerresponseapi; + +import com.baeldung.swaggerresponseapi.controller.ProductController; +import com.baeldung.swaggerresponseapi.model.Product; +import com.baeldung.swaggerresponseapi.service.ProductService; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +import java.util.Arrays; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.reset; +import static org.hamcrest.CoreMatchers.is; +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.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringRunner.class) +@WebMvcTest(ProductController.class) +public class ProductIntegrationTest { + + @Autowired + MockMvc mock; + + @MockBean + private ProductService productService; + + @Test + public void givenProduct_whenAddNewProduct_thenReturnValidStatusCode() throws Exception { + Product product = new Product("1001", "Milk"); + given(productService.addProducts(any(Product.class))).willReturn(product); + + mock.perform(post("/create").contentType(MediaType.APPLICATION_JSON) + .content(toJsonString(product))) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value("1001")); + + reset(productService); + } + + @Test + public void givenProductsList_whenGetAllProducts_thenReturnValidProducts() throws Exception { + Product product1 = new Product("1001", "Milk"); + Product product2 = new Product("2002", "Butter"); + + given(productService.getProductsList()).willReturn(Arrays.asList(product1, product2)); + + mock.perform(get("/products").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andDo(print()) + .andExpect(jsonPath("$[0].code", is(product1.getCode()))) + .andExpect(jsonPath("$[1].name", is(product2.getName()))); + + reset(productService); + } + + public String toJsonString(Product product) throws JsonProcessingException { + ObjectMapper om = new ObjectMapper(); + String json = om.writeValueAsString(product); + return json; + } +} diff --git a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/test/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplicationTests.java b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/test/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplicationTests.java new file mode 100644 index 0000000000..9e8c6ee0fa --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/test/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplicationTests.java @@ -0,0 +1,13 @@ +package com.baeldung.swaggerresponseapi; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SwaggerResponseApiApplicationTests { + + @Test + void contextLoads() { + } + +}