diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 84f1f0e86c..f4e3e534c8 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -56,6 +56,7 @@ spring-boot-performance spring-boot-properties spring-boot-properties-2 + spring-boot-properties-3 spring-boot-property-exp spring-boot-runtime spring-boot-security diff --git a/spring-boot-modules/spring-boot-properties-3/pom.xml b/spring-boot-modules/spring-boot-properties-3/pom.xml new file mode 100644 index 0000000000..cf94e1fc1d --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + ../ + + + spring-boot-properties-3 + 0.0.1-SNAPSHOT + spring-boot-properties-3 + Spring Boot Properties Module + + + 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/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/DemoApplication.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/DemoApplication.java new file mode 100644 index 0000000000..cf2fb7f981 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/DemoApplication.java @@ -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); + } + +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/config/TshirtSizeConfig.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/config/TshirtSizeConfig.java new file mode 100644 index 0000000000..690763ab7b --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/config/TshirtSizeConfig.java @@ -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 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/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/controller/TshirtSizeController.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/controller/TshirtSizeController.java new file mode 100644 index 0000000000..6b713c5be8 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/controller/TshirtSizeController.java @@ -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); + } + +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/service/SizeConverterImpl.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/service/SizeConverterImpl.java new file mode 100644 index 0000000000..34f7fe2ded --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/service/SizeConverterImpl.java @@ -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()); + } +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/service/SizeConverterService.java b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/service/SizeConverterService.java new file mode 100644 index 0000000000..412199b176 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/main/java/com/baeldung/boot/properties/service/SizeConverterService.java @@ -0,0 +1,8 @@ +package com.baeldung.boot.properties.service; + + +public interface SizeConverterService { + + int convertSize(String label, String countryCode); + +} diff --git a/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml b/spring-boot-modules/spring-boot-properties-3/src/main/resources/application.yml new file mode 100644 index 0000000000..8779cb6b0c --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/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/spring-boot-modules/spring-boot-properties-3/src/test/java/com/baeldung/boot/properties/controller/TshirtSizeControllerTest.java b/spring-boot-modules/spring-boot-properties-3/src/test/java/com/baeldung/boot/properties/controller/TshirtSizeControllerTest.java new file mode 100644 index 0000000000..0b70ed8622 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-3/src/test/java/com/baeldung/boot/properties/controller/TshirtSizeControllerTest.java @@ -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); + + } +} \ No newline at end of file