From e23d7f8efb72c694509d3f05c6114bce2b784ba7 Mon Sep 17 00:00:00 2001 From: Trixi Turny Date: Sun, 9 Aug 2020 18:46:32 +0100 Subject: [PATCH] BAEL-4321 demo app for yaml to pojo --- configuration/yaml-to-pojo/README.md | 9 ++++ configuration/yaml-to-pojo/pom.xml | 54 +++++++++++++++++++ .../java/yamltopojo/demo/DemoApplication.java | 16 ++++++ .../demo/config/TshirtSizeConfig.java | 27 ++++++++++ .../demo/controller/TshirtSizeController.java | 22 ++++++++ .../demo/service/SizeConverterImpl.java | 22 ++++++++ .../demo/service/SizeConverterService.java | 8 +++ .../src/main/resources/application.yml | 30 +++++++++++ .../yamltopojo/demo/DemoApplicationTests.java | 13 +++++ .../controller/TshirtSizeControllerTest.java | 38 +++++++++++++ 10 files changed, 239 insertions(+) create mode 100644 configuration/yaml-to-pojo/README.md create mode 100644 configuration/yaml-to-pojo/pom.xml create mode 100644 configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/DemoApplication.java create mode 100644 configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/config/TshirtSizeConfig.java create mode 100644 configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/controller/TshirtSizeController.java create mode 100644 configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/service/SizeConverterImpl.java create mode 100644 configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/service/SizeConverterService.java create mode 100644 configuration/yaml-to-pojo/src/main/resources/application.yml create mode 100644 configuration/yaml-to-pojo/src/test/java/yamltopojo/demo/DemoApplicationTests.java create mode 100644 configuration/yaml-to-pojo/src/test/java/yamltopojo/demo/controller/TshirtSizeControllerTest.java diff --git a/configuration/yaml-to-pojo/README.md b/configuration/yaml-to-pojo/README.md new file mode 100644 index 0000000000..9dba74a7e5 --- /dev/null +++ b/configuration/yaml-to-pojo/README.md @@ -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. \ No newline at end of file diff --git a/configuration/yaml-to-pojo/pom.xml b/configuration/yaml-to-pojo/pom.xml new file mode 100644 index 0000000000..f6b55718be --- /dev/null +++ b/configuration/yaml-to-pojo/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.2.RELEASE + + + yaml-to-pojo + demo + 0.0.1-SNAPSHOT + demo + Demo project for YAML into POJO + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + org.springframework.boot + spring-boot-starter-web + RELEASE + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/DemoApplication.java b/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/DemoApplication.java new file mode 100644 index 0000000000..ec8df793c2 --- /dev/null +++ b/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/DemoApplication.java @@ -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); + } + +} diff --git a/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/config/TshirtSizeConfig.java b/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/config/TshirtSizeConfig.java new file mode 100644 index 0000000000..8f8d2e5b39 --- /dev/null +++ b/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/config/TshirtSizeConfig.java @@ -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 simpleMapping; + + private final Map> complexMapping; + + + public TshirtSizeConfig(Map simpleMapping, Map> complexMapping) { + this.simpleMapping = simpleMapping; + this.complexMapping = complexMapping; + } + + public Map getSimpleMapping() { + return simpleMapping; + } + + public Map> getComplexMapping() { + return complexMapping; + } +} diff --git a/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/controller/TshirtSizeController.java b/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/controller/TshirtSizeController.java new file mode 100644 index 0000000000..d555549bd4 --- /dev/null +++ b/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/controller/TshirtSizeController.java @@ -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); + } + +} diff --git a/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/service/SizeConverterImpl.java b/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/service/SizeConverterImpl.java new file mode 100644 index 0000000000..8f95e4253b --- /dev/null +++ b/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/service/SizeConverterImpl.java @@ -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); + } +} diff --git a/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/service/SizeConverterService.java b/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/service/SizeConverterService.java new file mode 100644 index 0000000000..3e24681cbe --- /dev/null +++ b/configuration/yaml-to-pojo/src/main/java/yamltopojo/demo/service/SizeConverterService.java @@ -0,0 +1,8 @@ +package yamltopojo.demo.service; + + +public interface SizeConverterService { + + int convertSize(String label, String countryCode); + +} diff --git a/configuration/yaml-to-pojo/src/main/resources/application.yml b/configuration/yaml-to-pojo/src/main/resources/application.yml new file mode 100644 index 0000000000..edd200389e --- /dev/null +++ b/configuration/yaml-to-pojo/src/main/resources/application.yml @@ -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 diff --git a/configuration/yaml-to-pojo/src/test/java/yamltopojo/demo/DemoApplicationTests.java b/configuration/yaml-to-pojo/src/test/java/yamltopojo/demo/DemoApplicationTests.java new file mode 100644 index 0000000000..dfe980c05c --- /dev/null +++ b/configuration/yaml-to-pojo/src/test/java/yamltopojo/demo/DemoApplicationTests.java @@ -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() { + } + +} diff --git a/configuration/yaml-to-pojo/src/test/java/yamltopojo/demo/controller/TshirtSizeControllerTest.java b/configuration/yaml-to-pojo/src/test/java/yamltopojo/demo/controller/TshirtSizeControllerTest.java new file mode 100644 index 0000000000..3c1ef6dff5 --- /dev/null +++ b/configuration/yaml-to-pojo/src/test/java/yamltopojo/demo/controller/TshirtSizeControllerTest.java @@ -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); + + } +} \ No newline at end of file