From de6d367e413f3ec302ac19030030e20c11fabe06 Mon Sep 17 00:00:00 2001 From: sanitaso Date: Fri, 25 Feb 2022 12:54:20 +0100 Subject: [PATCH 1/5] 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() { + } + +} From f62de535543e8f04bfaa073ae2eaf185492fd0cd Mon Sep 17 00:00:00 2001 From: sanitaso Date: Fri, 25 Feb 2022 15:25:38 +0100 Subject: [PATCH 2/5] modify pom --- .../spring-boot-springdoc/swaggerResponseAPI/pom.xml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/pom.xml b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/pom.xml index 17544290e4..c168248df2 100644 --- a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/pom.xml +++ b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/pom.xml @@ -3,9 +3,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung.spring-boot-modules - spring-boot-disable-logging - 1.0.0-SNAPSHOT + org.springframework.boot + spring-boot-starter-parent + 2.6.3 + com.baeldung swaggerResponseAPI From a735229a8433bea1ab0f3076b18a774caddfcd92 Mon Sep 17 00:00:00 2001 From: sanitaso Date: Tue, 8 Mar 2022 15:10:36 +0100 Subject: [PATCH 3/5] change the module directory --- .../SwaggerResponseApiApplication.java | 0 .../controller/ProductController.java | 1 - .../swaggerresponseapi/model/Product.java | 28 +++++++ .../service/ProductService.java | 0 .../swaggerResponseAPI/.gitignore | 33 -------- .../swaggerResponseAPI/pom.xml | 70 ----------------- .../swaggerresponseapi/model/Product.java | 15 ---- .../src/main/resources/application.properties | 1 - .../ProductIntegrationTest.java | 76 ------------------- .../SwaggerResponseApiApplicationTests.java | 13 ---- 10 files changed, 28 insertions(+), 209 deletions(-) rename spring-boot-modules/spring-boot-springdoc/{swaggerResponseAPI => }/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java (100%) rename spring-boot-modules/spring-boot-springdoc/{swaggerResponseAPI => }/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java (92%) create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java rename spring-boot-modules/spring-boot-springdoc/{swaggerResponseAPI => }/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java (100%) delete mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/.gitignore delete mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/pom.xml delete mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java delete mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/resources/application.properties delete mode 100644 spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/test/java/com/baeldung/swaggerresponseapi/ProductIntegrationTest.java delete 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/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java similarity index 100% rename from spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java rename to spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java 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/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java similarity index 92% rename from spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java rename to spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java index 3e9a3291d9..364a7e8a66 100644 --- a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java @@ -24,7 +24,6 @@ public class ProductController { this.productService = productService; } - @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Product successfully added!") }) @PostMapping("/create") public Product addProduct(@RequestBody Product product) { return productService.addProducts(product); diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java new file mode 100644 index 0000000000..036ecbc853 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java @@ -0,0 +1,28 @@ +package com.baeldung.swaggerresponseapi.model; + +public class Product { + String code; + String name; + + public Product(String code, String name) { + this.code = code; + this.name = name; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = 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/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java similarity index 100% rename from spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java rename to spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java diff --git a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/.gitignore b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/.gitignore deleted file mode 100644 index f60f3beecd..0000000000 --- a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/.gitignore +++ /dev/null @@ -1,33 +0,0 @@ -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 deleted file mode 100644 index c168248df2..0000000000 --- a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/pom.xml +++ /dev/null @@ -1,70 +0,0 @@ - - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.6.3 - - - 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/model/Product.java b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java deleted file mode 100644 index 0a100582fc..0000000000 --- a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java +++ /dev/null @@ -1,15 +0,0 @@ -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/resources/application.properties b/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/resources/application.properties deleted file mode 100644 index 8b13789179..0000000000 --- a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - 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 deleted file mode 100644 index ef6a8953e2..0000000000 --- a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/test/java/com/baeldung/swaggerresponseapi/ProductIntegrationTest.java +++ /dev/null @@ -1,76 +0,0 @@ -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 deleted file mode 100644 index 9e8c6ee0fa..0000000000 --- a/spring-boot-modules/spring-boot-springdoc/swaggerResponseAPI/src/test/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.swaggerresponseapi; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class SwaggerResponseApiApplicationTests { - - @Test - void contextLoads() { - } - -} From bc543e9c79f33db2a671a2555fede3dd842ebe38 Mon Sep 17 00:00:00 2001 From: sanitaso Date: Tue, 8 Mar 2022 15:13:42 +0100 Subject: [PATCH 4/5] remove dir --- .../SwaggerResponseApiApplication.java | 13 ------- .../controller/ProductController.java | 38 ------------------- .../swaggerresponseapi/model/Product.java | 28 -------------- .../service/ProductService.java | 21 ---------- 4 files changed, 100 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java delete mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java delete mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java delete mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java deleted file mode 100644 index 913544b459..0000000000 --- a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java +++ /dev/null @@ -1,13 +0,0 @@ -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/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java deleted file mode 100644 index 364a7e8a66..0000000000 --- a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java +++ /dev/null @@ -1,38 +0,0 @@ -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; - } - - @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/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java deleted file mode 100644 index 036ecbc853..0000000000 --- a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.swaggerresponseapi.model; - -public class Product { - String code; - String name; - - public Product(String code, String name) { - this.code = code; - this.name = name; - } - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} - diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java deleted file mode 100644 index 5e7533d6f4..0000000000 --- a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java +++ /dev/null @@ -1,21 +0,0 @@ -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; - } -} From e3966f2efde01da3696cb110aefaae9c2e75becd Mon Sep 17 00:00:00 2001 From: sanitaso Date: Tue, 8 Mar 2022 15:15:11 +0100 Subject: [PATCH 5/5] add dir --- .../SwaggerResponseApiApplication.java | 13 +++++++ .../controller/ProductController.java | 38 +++++++++++++++++++ .../swaggerresponseapi/model/Product.java | 28 ++++++++++++++ .../service/ProductService.java | 21 ++++++++++ 4 files changed, 100 insertions(+) create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java create mode 100644 spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/SwaggerResponseApiApplication.java new file mode 100644 index 0000000000..913544b459 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/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/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java new file mode 100644 index 0000000000..364a7e8a66 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/controller/ProductController.java @@ -0,0 +1,38 @@ +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; + } + + @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/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java new file mode 100644 index 0000000000..036ecbc853 --- /dev/null +++ b/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/model/Product.java @@ -0,0 +1,28 @@ +package com.baeldung.swaggerresponseapi.model; + +public class Product { + String code; + String name; + + public Product(String code, String name) { + this.code = code; + this.name = name; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} + diff --git a/spring-boot-modules/spring-boot-springdoc/src/main/java/com/baeldung/swaggerresponseapi/service/ProductService.java b/spring-boot-modules/spring-boot-springdoc/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/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; + } +}