BAEL-4321 demo app for yaml to pojo

This commit is contained in:
Trixi Turny 2020-08-09 18:46:32 +01:00
parent 2ddbdb5b4e
commit e23d7f8efb
10 changed files with 239 additions and 0 deletions

View File

@ -0,0 +1,9 @@
This is a demo application for using YAML configuration for defining values in a POJO class.
The application has an endpoint to provide T-shirt size conversion for label and countrycode.
If you run this service locally you can call this endpoint on:
`localhost:8080/convertSize?label=M&countryCode=fr`
It should return the size as int.

View File

@ -0,0 +1,54 @@
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>yaml-to-pojo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for YAML into POJO</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,16 @@
package yamltopojo.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import yamltopojo.demo.config.TshirtSizeConfig;
@SpringBootApplication
@EnableConfigurationProperties(TshirtSizeConfig.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

View File

@ -0,0 +1,27 @@
package yamltopojo.demo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Map;
@ConfigurationProperties(prefix = "t-shirt-size")
public class TshirtSizeConfig {
private final Map<String, Integer> simpleMapping;
private final Map<String, Map<String, Integer>> complexMapping;
public TshirtSizeConfig(Map<String, Integer> simpleMapping, Map<String, Map<String, Integer>> complexMapping) {
this.simpleMapping = simpleMapping;
this.complexMapping = complexMapping;
}
public Map<String, Integer> getSimpleMapping() {
return simpleMapping;
}
public Map<String, Map<String, Integer>> getComplexMapping() {
return complexMapping;
}
}

View File

@ -0,0 +1,22 @@
package yamltopojo.demo.controller;
import org.springframework.web.bind.annotation.*;
import yamltopojo.demo.service.SizeConverterService;
@RestController
@RequestMapping(value = "/")
public class TshirtSizeController {
private SizeConverterService service;
public TshirtSizeController(SizeConverterService service) {
this.service = service;
}
@RequestMapping(value ="convertSize", method = RequestMethod.GET)
public int convertSize(@RequestParam(value = "label") final String label,
@RequestParam(value = "countryCode", required = false) final String countryCode) {
return service.convertSize(label, countryCode);
}
}

View File

@ -0,0 +1,22 @@
package yamltopojo.demo.service;
import org.springframework.stereotype.Service;
import yamltopojo.demo.config.TshirtSizeConfig;
@Service
public class SizeConverterImpl implements SizeConverterService {
private TshirtSizeConfig tshirtSizeConfig;
public SizeConverterImpl(TshirtSizeConfig tshirtSizeConfig) {
this.tshirtSizeConfig = tshirtSizeConfig;
}
public int convertSize(String label, String countryCode) {
if(countryCode == null) {
return tshirtSizeConfig.getSimpleMapping().get(label);
}
return tshirtSizeConfig.getComplexMapping().get(label).get(countryCode);
}
}

View File

@ -0,0 +1,8 @@
package yamltopojo.demo.service;
public interface SizeConverterService {
int convertSize(String label, String countryCode);
}

View File

@ -0,0 +1,30 @@
t-shirt-size:
simple-mapping:
XS: 6
S: 8
M: 10
L: 12
XL: 14
complex-mapping:
XS:
uk: 6
fr: 34
us: 2
S:
uk: 8
fr: 36
us: 4
M:
uk: 10
fr: 38
us: 6
L:
uk: 12
fr: 40
us: 8
XL:
uk: 14
fr: 42
us: 10

View File

@ -0,0 +1,13 @@
package yamltopojo.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
}
}

View File

@ -0,0 +1,38 @@
package yamltopojo.demo.controller;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import yamltopojo.demo.service.SizeConverterService;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class TshirtSizeControllerTest {
@Mock
private SizeConverterService service;
@InjectMocks
private TshirtSizeController tested;
@Test
void convertSize() {
// Given
String label = "S";
String countryCode = "fr";
int result = 36;
//
when(service.convertSize(label, countryCode)).thenReturn(result);
int actual = tested.convertSize(label, countryCode);
// Then
assertEquals(actual, result);
}
}