From ef16cf1587a59c8c2df94d098ab73e1af3911521 Mon Sep 17 00:00:00 2001 From: Marius Catalin Munteanu Date: Sat, 25 Apr 2020 12:23:24 +0300 Subject: [PATCH] BAEL-3016 Thymeleaf Formating Currencies --- .../currencies/CurrenciesController.java | 22 ++++++ .../templates/currencies/currencies.html | 21 ++++++ .../CurrenciesControllerIntegrationTest.java | 68 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 spring-thymeleaf-3/src/main/java/com/baeldung/thymeleaf/currencies/CurrenciesController.java create mode 100644 spring-thymeleaf-3/src/main/resources/templates/currencies/currencies.html create mode 100644 spring-thymeleaf-3/src/test/java/com/baeldung/thymeleaf/currencies/CurrenciesControllerIntegrationTest.java diff --git a/spring-thymeleaf-3/src/main/java/com/baeldung/thymeleaf/currencies/CurrenciesController.java b/spring-thymeleaf-3/src/main/java/com/baeldung/thymeleaf/currencies/CurrenciesController.java new file mode 100644 index 0000000000..206cf32683 --- /dev/null +++ b/spring-thymeleaf-3/src/main/java/com/baeldung/thymeleaf/currencies/CurrenciesController.java @@ -0,0 +1,22 @@ +package com.baeldung.thymeleaf.currencies; + +import java.util.List; +import java.util.Locale; +import java.util.Set; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +public class CurrenciesController { + + @GetMapping(value = "/currency") + public String exchange( + @RequestParam(value = "amount", required = false) String amount, + @RequestParam(value = "amountList", required = false) List amountList, + Locale locale) { + + return "currencies/currencies"; + } +} diff --git a/spring-thymeleaf-3/src/main/resources/templates/currencies/currencies.html b/spring-thymeleaf-3/src/main/resources/templates/currencies/currencies.html new file mode 100644 index 0000000000..c2f44265dd --- /dev/null +++ b/spring-thymeleaf-3/src/main/resources/templates/currencies/currencies.html @@ -0,0 +1,21 @@ + + + + + Currency table + + +

Currency format by Locale

+

+ +

Currency Arrays format by Locale

+

+ +

Remove decimal values

+

+ +

Replace decimal points

+

+ + \ No newline at end of file diff --git a/spring-thymeleaf-3/src/test/java/com/baeldung/thymeleaf/currencies/CurrenciesControllerIntegrationTest.java b/spring-thymeleaf-3/src/test/java/com/baeldung/thymeleaf/currencies/CurrenciesControllerIntegrationTest.java new file mode 100644 index 0000000000..02bf8a9ee0 --- /dev/null +++ b/spring-thymeleaf-3/src/test/java/com/baeldung/thymeleaf/currencies/CurrenciesControllerIntegrationTest.java @@ -0,0 +1,68 @@ +package com.baeldung.thymeleaf.currencies; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc(printOnlyOnFailure = false) +public class CurrenciesControllerIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void whenCallCurrencyWithSpanishLocale_ThenReturnProperCurrency() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/currency") + .header("Accept-Language", "es-ES") + .param("amount", "10032.5")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("10.032,50 €"))); + } + + @Test + public void whenCallCurrencyWithUSALocale_ThenReturnProperCurrency() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/currency") + .header("Accept-Language", "en-US") + .param("amount", "10032.5")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("$10,032.50"))); + } + + @Test + public void whenCallCurrencyWithRomanianLocaleWithArrays_ThenReturnLocaleCurrencies() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/currency") + .header("Accept-Language", "ro-RO") + .param("amountList", "10", "20", "30")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("10,00 RON, 20,00 RON, 30,00 RON"))); + } + + @Test + public void whenCallCurrencyWithUSALocaleWithoutDecimal_ThenReturnCurrencyWithoutTrailingZeros() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/currency") + .header("Accept-Language", "en-US") + .param("amount", "10032")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("$10,032"))); + } + + @Test + public void whenCallCurrencyWithUSALocale_ThenReturnReplacedDecimalPoint() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/currency") + .header("Accept-Language", "en-US") + .param("amount", "1.5")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("1,5"))); + } +}