add files to springdoc

This commit is contained in:
sanitaso 2022-02-25 12:54:20 +01:00
parent f74400ee6e
commit de6d367e41
9 changed files with 280 additions and 0 deletions

View File

@ -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/

View File

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-disable-logging</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.baeldung</groupId>
<artifactId>swaggerResponseAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>swaggerResponseAPI</name>
<description>swaggerResponseAPI</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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<Product> getProductsList() {
return productService.getProductsList();
}
}

View File

@ -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;
}

View File

@ -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<Product> productsList = new ArrayList<>();
public Product addProducts(Product product) {
productsList.add(product);
return product;
}
public List<Product> getProductsList() {
return productsList;
}
}

View File

@ -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;
}
}

View File

@ -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() {
}
}