BAEL-4321 demo app for yaml to pojo
This commit is contained in:
parent
2ddbdb5b4e
commit
e23d7f8efb
9
configuration/yaml-to-pojo/README.md
Normal file
9
configuration/yaml-to-pojo/README.md
Normal 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.
|
54
configuration/yaml-to-pojo/pom.xml
Normal file
54
configuration/yaml-to-pojo/pom.xml
Normal 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>
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package yamltopojo.demo.service;
|
||||||
|
|
||||||
|
|
||||||
|
public interface SizeConverterService {
|
||||||
|
|
||||||
|
int convertSize(String label, String countryCode);
|
||||||
|
|
||||||
|
}
|
@ -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
|
@ -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() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user