Merge pull request #9891 from Trixi-Turny/BAEL-4321-yaml-to-pojo

BAEL-4321 yaml config to map in pojo demo
This commit is contained in:
davidmartinezbarua 2020-08-18 10:43:51 -03:00 committed by GitHub
commit 3f3bbe4a0d
9 changed files with 217 additions and 0 deletions

View File

@ -56,6 +56,7 @@
<module>spring-boot-performance</module>
<module>spring-boot-properties</module>
<module>spring-boot-properties-2</module>
<module>spring-boot-properties-3</module>
<module>spring-boot-property-exp</module>
<module>spring-boot-runtime</module>
<module>spring-boot-security</module>

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>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<artifactId>spring-boot-properties-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-properties-3</name>
<description>Spring Boot Properties Module</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 com.baeldung.boot.properties;
import com.baeldung.boot.properties.config.TshirtSizeConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@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 com.baeldung.boot.properties.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,21 @@
package com.baeldung.boot.properties.controller;
import org.springframework.web.bind.annotation.*;
import com.baeldung.boot.properties.service.SizeConverterService;
@RestController
@RequestMapping(value = "/")
public class TshirtSizeController {
private final 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 com.baeldung.boot.properties.service;
import org.springframework.stereotype.Service;
import com.baeldung.boot.properties.config.TshirtSizeConfig;
@Service
public class SizeConverterImpl implements SizeConverterService {
private final 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.toLowerCase());
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.boot.properties.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,38 @@
package com.baeldung.boot.properties.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 com.baeldung.boot.properties.service.SizeConverterService;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class TshirtSizeControllerUnitTest {
@Mock
private SizeConverterService service;
@InjectMocks
private TshirtSizeController tested;
@Test
void whenConvertSize_thenOK() {
// Given
String label = "S";
String countryCode = "fr";
int result = 36;
// When
when(service.convertSize(label, countryCode)).thenReturn(result);
int actual = tested.convertSize(label, countryCode);
// Then
assertEquals(actual, result);
}
}