parameters controller

This commit is contained in:
Meutel 2023-10-22 15:32:40 +02:00
parent 0cbcfdad6d
commit ba66fd20d1
11 changed files with 369 additions and 7 deletions

View File

@ -1,12 +1,13 @@
<?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"
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>net.meutel.recettes</groupId>
<artifactId>api</artifactId>
@ -15,6 +16,8 @@
<description>Application de gestion de recettes - API</description>
<properties>
<java.version>21</java.version>
<springdoc.version>1.6.8</springdoc.version>
<swagger-ui.version>4.10.3</swagger-ui.version>
</properties>
<dependencies>
<dependency>
@ -45,6 +48,45 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<!--SpringDoc
dependencies -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
<!-- @Nullable annotation -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<!-- Bean Validation API support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>
</dependencies>
<build>
@ -53,7 +95,49 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.2.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<skipValidateSpec>true</skipValidateSpec>
<inputSpec>../doc/api/openapi/openapi.yaml</inputSpec>
<generatorName>spring</generatorName>
<configOptions>
<openApiNullable>false</openApiNullable>
<interfaceOnly>true</interfaceOnly>
<useSpringBoot3>true</useSpringBoot3>
<apiPackage>net.meutel.recettes.api</apiPackage>
<modelPackage>net.meutel.recettes.api.model</modelPackage>
<hateoas>true</hateoas>
<performBeanValidation>true</performBeanValidation>
<useOptional>true</useOptional>
<useSwaggerUI>false</useSwaggerUI>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
</project>

View File

@ -0,0 +1,28 @@
package net.meutel.recettes.api.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import net.meutel.recettes.api.ParametersApi;
import net.meutel.recettes.api.model.RecetteParam;
import net.meutel.recettes.api.service.ParametersService;
@RestController
public class ParametersApiController implements ParametersApi {
private final Logger LOG = LoggerFactory.getLogger(ParametersApiController.class);
@Autowired
private ParametersService service;
@Override
public ResponseEntity<List<RecetteParam>> listRecetteParamsByType(String paramType) {
LOG.info("list params: " + paramType);
return ResponseEntity.ok(service.loadAllParameters(paramType));
}
}

View File

@ -0,0 +1,55 @@
package net.meutel.recettes.api.enitity;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "parameters")
public class RecetteParamEntity {
@Id
public String id;
public String _type;
public String name;
public RecetteParamEntity() {
}
public RecetteParamEntity(String id, String _type, String name) {
this.id = id;
this._type = _type;
this.name = name;
}
@Override
public String toString() {
return "RecetteParamEntity [id=" + id + ", _type=" + _type + ", name=" + name + "]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String get_type() {
return _type;
}
public void set_type(String _type) {
this._type = _type;
}
}

View File

@ -0,0 +1,19 @@
package net.meutel.recettes.api.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import net.meutel.recettes.api.enitity.RecetteParamEntity;
import net.meutel.recettes.api.model.RecetteParam;
@Mapper(componentModel = "spring")
public interface RecetteParamMapper {
@Mapping(target = "atType", source = "entity._type")
@Mapping(target = "add", ignore = true)
RecetteParam fromEntity(RecetteParamEntity entity);
@Mapping(target = "_type", source = "model.atType")
RecetteParamEntity toEntity(RecetteParam model);
}

View File

@ -0,0 +1,15 @@
package net.meutel.recettes.api.repository;
import java.util.stream.Stream;
import org.springframework.data.mongodb.repository.MongoRepository;
import net.meutel.recettes.api.enitity.RecetteParamEntity;
public interface RecetteParamRepository extends MongoRepository<RecetteParamEntity, String> {
Stream<RecetteParamEntity> findBy_type(String atType);
}

View File

@ -0,0 +1,14 @@
package net.meutel.recettes.api.service;
import java.util.List;
import net.meutel.recettes.api.model.RecetteParam;
public interface ParametersService {
default List<RecetteParam> loadAllParameters(final String paramType) {
return List.of();
};
}

View File

@ -0,0 +1,28 @@
package net.meutel.recettes.api.service;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import net.meutel.recettes.api.mapper.RecetteParamMapper;
import net.meutel.recettes.api.model.RecetteParam;
import net.meutel.recettes.api.repository.RecetteParamRepository;
@Service
public class ParametersServiceImpl implements ParametersService {
private final RecetteParamRepository repo;
private final RecetteParamMapper mapper;
public ParametersServiceImpl(RecetteParamRepository repo, RecetteParamMapper mapper) {
this.repo = repo;
this.mapper = mapper;
}
@Override
public List<RecetteParam> loadAllParameters(final String paramType) {
return repo.findBy_type(paramType).map(mapper::fromEntity).collect(Collectors.toList());
}
}

View File

@ -1 +1,3 @@
spring.data.mongodb.database=recettes
logging.level.org.springframework.data.mongodb=DEBUG
management.endpoint.health.show-details=always

View File

@ -0,0 +1,61 @@
package net.meutel.recettes.api.controller;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
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;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import net.meutel.recettes.api.model.RecetteParam;
import net.meutel.recettes.api.service.ParametersService;
@SpringBootTest
@AutoConfigureMockMvc
public class ParameterControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ParametersService service;
@Test
public void listRecetteParamsByType_any_emptyArray() throws Exception {
this.mockMvc.perform(get("/parameters/TEST"))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$").isEmpty());
}
@Test
public void listRecetteParamsByType_exists_array() throws Exception {
when(service.loadAllParameters("TEST"))
.thenReturn(Stream.of("p1", "p2")
.map(n -> new RecetteParam().name(n).atType("TEST").id(n))
.collect(toList()));
this.mockMvc.perform(get("/parameters/TEST"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$").isNotEmpty())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$.[*].name").exists())
.andExpect(jsonPath("$.[*].id").exists())
.andExpect(jsonPath("$.[*].@type").exists())
.andExpect(jsonPath("$.[1].@type", equalTo("TEST")))
.andExpect(jsonPath("$.[0].name", equalTo("p1")));
}
}

View File

@ -0,0 +1,56 @@
package net.meutel.recettes.api.service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import java.util.List;
import net.meutel.recettes.api.enitity.RecetteParamEntity;
import net.meutel.recettes.api.model.RecetteParam;
import net.meutel.recettes.api.repository.RecetteParamRepository;
@SpringBootTest
public class ParametersServiceImplTest {
@MockBean
RecetteParamRepository repo;
@Autowired
ParametersService tested;
@Test
void loadAllParameters_exists_list() {
var p1 = new RecetteParamEntity("1", "TEST", "p1");
var p2 = new RecetteParamEntity("2", "TEST", "p2");
when(repo.findBy_type("TEST"))
.thenReturn(List.of(p1, p2).stream());
var result = tested.loadAllParameters("TEST");
assertThat(result)
.isNotNull()
.hasSize(2);
assertThat(result).element(0)
.returns("TEST", RecetteParam::getAtType)
.returns("p1", RecetteParam::getName);
assertThat(result).element(1)
.returns("TEST", RecetteParam::getAtType)
.returns("p2", RecetteParam::getName);
}
@Test
void loadAllParameters_notExists_empty() {
var result = tested.loadAllParameters("TEST");
assertThat(result)
.isNotNull()
.isEmpty();
}
}

View File

@ -66,7 +66,7 @@ paths:
- parameter
summary: Parameters by type
description: Returns a list of parameters of given type
operationId: listParametersByType
operationId: listRecetteParamsByType
parameters:
- name: paramType
in: path
@ -82,11 +82,11 @@ paths:
schema:
type: array
items:
$ref: '#/components/schemas/Parameter'
$ref: '#/components/schemas/RecetteParam'
components:
schemas:
Parameter:
RecetteParam:
type: object
required:
- id