From b5404a87bfd62a73754253feb67b0165dab4ba39 Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Mon, 15 Jan 2024 11:11:18 +0100 Subject: [PATCH 1/7] feat: OB-8353 * Add custom translation paths on the application.properties file * Adapt DefaultTranslationsService.java and TranlationMangementServiceApiClient to use the added custom translation paths added --- .../service/DefaultTranslationsService.java | 30 ++++++++++++++++++- .../TranlationMangementServiceApiClient.java | 14 +++++++-- src/main/resources/application.properties | 4 ++- ...rmal.json => mailservice.de_informal.json} | 0 4 files changed, 44 insertions(+), 4 deletions(-) rename src/main/resources/i18n/{mailservice.de@informal.json => mailservice.de_informal.json} (100%) diff --git a/src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java b/src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java index 5505463..f1a330a 100644 --- a/src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java +++ b/src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java @@ -2,21 +2,32 @@ import de.caritas.cob.mailservice.api.model.Dialect; import de.caritas.cob.mailservice.config.apiclient.TranlationMangementServiceApiClient; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.List; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Slf4j @Service public class DefaultTranslationsService { + @Value("${template.custom.default.translations.path}") + private String customTranslationsPath; + + @Value("${template.use.custom.resources.path}") + private boolean useCustomResourcesPath; + public String fetchDefaultTranslations(String translationComponentName, String languageCode, Dialect dialect) { - InputStream inputStream = tryFetchDefaultTranslationWithFallbackToEmptyDialect(translationComponentName, languageCode, dialect); + InputStream inputStream = useCustomResourcesPath ? tryFetchExternalTranslationWithFallbackToEmptyDialect(translationComponentName, languageCode, dialect) : + tryFetchDefaultTranslationWithFallbackToEmptyDialect(translationComponentName, languageCode, dialect); if (inputStream == null) { return "{}"; } @@ -31,6 +42,23 @@ public String fetchDefaultTranslations(String translationComponentName, String l } } + private InputStream tryFetchExternalTranslationWithFallbackToEmptyDialect(String translationComponentName, String languageCode, + Dialect dialect) { + InputStream inputStream = buildStreamForExternalPath(translationComponentName, languageCode, dialect); + return inputStream != null ? inputStream : buildStreamForExternalPath(translationComponentName, languageCode, null); + } + + private FileInputStream buildStreamForExternalPath(String translationComponentName, String languageCode, Dialect dialect) { + try { + String filename = String.format("%s/%s.%s%s.json", customTranslationsPath, translationComponentName.toLowerCase(), languageCode, TranlationMangementServiceApiClient.getDialectSuffix(dialect)); + return new FileInputStream(filename); + } catch (FileNotFoundException e) { + log.warn("Default translations for component {}, language {} not found in external path {}", translationComponentName, + languageCode, customTranslationsPath); + return null; + } + } + private InputStream tryFetchDefaultTranslationWithFallbackToEmptyDialect(String translationComponentName, String languageCode, Dialect dialect) { InputStream inputStream = getInputStream(translationComponentName, languageCode, diff --git a/src/main/java/de/caritas/cob/mailservice/config/apiclient/TranlationMangementServiceApiClient.java b/src/main/java/de/caritas/cob/mailservice/config/apiclient/TranlationMangementServiceApiClient.java index 80184e8..5b4041b 100644 --- a/src/main/java/de/caritas/cob/mailservice/config/apiclient/TranlationMangementServiceApiClient.java +++ b/src/main/java/de/caritas/cob/mailservice/config/apiclient/TranlationMangementServiceApiClient.java @@ -32,7 +32,7 @@ public String tryFetchTranslationsFromTranslationManagementService(String projec String component, String languageCode, Dialect dialect) { HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "Token " + apiKey); - String url = apiUrl + "translations/" + project + "/" + component + "/" + languageCode + getDialectSuffix(dialect) + String url = apiUrl + "translations/" + project + "/" + component + "/" + languageCode + getWeblateDialectSuffix(dialect) + "/file.json"; log.info("Calling url to fetch translations: {}", url); @@ -41,7 +41,7 @@ public String tryFetchTranslationsFromTranslationManagementService(String projec return response.getBody(); } - public static String getDialectSuffix(Dialect dialect) { + public static String getWeblateDialectSuffix(Dialect dialect) { if (dialect == null) { return StringUtils.EMPTY; } @@ -50,4 +50,14 @@ public static String getDialectSuffix(Dialect dialect) { } return StringUtils.EMPTY; } + + public static String getDialectSuffix(Dialect dialect) { + if (dialect == null) { + return StringUtils.EMPTY; + } + if (dialect == Dialect.INFORMAL) { + return "_informal"; + } + return StringUtils.EMPTY; + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 63ea274..d244cd4 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -62,6 +62,8 @@ csrf.cookie.property= # refactoring template.use.custom.resources.path= template.custom.resources.path= +template.custom.default.translations.path= +translation.management.system.enabled= weblate.api.url= weblate.api.key= @@ -74,4 +76,4 @@ management.endpoints.web.exposure.include=health management.health.probes.enabled=true spring.cache.jcache.config=classpath:ehcache.xml -logging.level.net.sf.ehcache=info \ No newline at end of file +logging.level.net.sf.ehcache=info diff --git a/src/main/resources/i18n/mailservice.de@informal.json b/src/main/resources/i18n/mailservice.de_informal.json similarity index 100% rename from src/main/resources/i18n/mailservice.de@informal.json rename to src/main/resources/i18n/mailservice.de_informal.json From 57516eef31484a6c9f161ce7d7fafbfa4abfe9bb Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Mon, 15 Jan 2024 11:26:41 +0100 Subject: [PATCH 2/7] feat: OB-8353 * Add translationManagementSystemEnabled check on TranslationService to skip the translationManagementServiceApiClient call if the value is false * fixed typo on TranslationManagementServiceApiClient's class name --- .../service/DefaultTranslationsService.java | 6 +++--- .../api/service/TranslationService.java | 18 ++++++++++++------ ...TranslationManagementServiceApiClient.java} | 4 +--- .../api/controller/MailControllerE2EIT.java | 7 +++---- 4 files changed, 19 insertions(+), 16 deletions(-) rename src/main/java/de/caritas/cob/mailservice/config/apiclient/{TranlationMangementServiceApiClient.java => TranslationManagementServiceApiClient.java} (97%) diff --git a/src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java b/src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java index f1a330a..9eea574 100644 --- a/src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java +++ b/src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java @@ -1,7 +1,7 @@ package de.caritas.cob.mailservice.api.service; import de.caritas.cob.mailservice.api.model.Dialect; -import de.caritas.cob.mailservice.config.apiclient.TranlationMangementServiceApiClient; +import de.caritas.cob.mailservice.config.apiclient.TranslationManagementServiceApiClient; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -50,7 +50,7 @@ private InputStream tryFetchExternalTranslationWithFallbackToEmptyDialect(String private FileInputStream buildStreamForExternalPath(String translationComponentName, String languageCode, Dialect dialect) { try { - String filename = String.format("%s/%s.%s%s.json", customTranslationsPath, translationComponentName.toLowerCase(), languageCode, TranlationMangementServiceApiClient.getDialectSuffix(dialect)); + String filename = String.format("%s/%s.%s%s.json", customTranslationsPath, translationComponentName.toLowerCase(), languageCode, TranslationManagementServiceApiClient.getDialectSuffix(dialect)); return new FileInputStream(filename); } catch (FileNotFoundException e) { log.warn("Default translations for component {}, language {} not found in external path {}", translationComponentName, @@ -85,7 +85,7 @@ private InputStream getInputStream(String translationComponentName, String langu Dialect dialect) { String translationFilename = getTranslationFilename( translationComponentName + "." + languageCode - + TranlationMangementServiceApiClient.getDialectSuffix(dialect)); + + TranslationManagementServiceApiClient.getDialectSuffix(dialect)); return TranslationService.class.getResourceAsStream(translationFilename); } diff --git a/src/main/java/de/caritas/cob/mailservice/api/service/TranslationService.java b/src/main/java/de/caritas/cob/mailservice/api/service/TranslationService.java index 84cf634..a7b6c28 100644 --- a/src/main/java/de/caritas/cob/mailservice/api/service/TranslationService.java +++ b/src/main/java/de/caritas/cob/mailservice/api/service/TranslationService.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import de.caritas.cob.mailservice.api.model.Dialect; -import de.caritas.cob.mailservice.config.apiclient.TranlationMangementServiceApiClient; +import de.caritas.cob.mailservice.config.apiclient.TranslationManagementServiceApiClient; import java.util.Map; import java.util.Optional; import lombok.NonNull; @@ -32,13 +32,16 @@ public class TranslationService { @Value("${weblate.component}") private String component; - private final @NonNull TranlationMangementServiceApiClient tranlationMangementServiceApiClient; + @Value("${translation.management.system.enabled}") + private boolean translationManagementSystemEnabled; + + private final @NonNull TranslationManagementServiceApiClient translationManagementServiceApiClient; private final @NonNull DefaultTranslationsService defaultTranslationsService; public TranslationService( - TranlationMangementServiceApiClient tranlationMangementServiceApiClient, DefaultTranslationsService defaultTranslationsService) { - this.tranlationMangementServiceApiClient = tranlationMangementServiceApiClient; + TranslationManagementServiceApiClient translationManagementServiceApiClient, DefaultTranslationsService defaultTranslationsService) { + this.translationManagementServiceApiClient = translationManagementServiceApiClient; this.defaultTranslationsService = defaultTranslationsService; } @@ -85,9 +88,12 @@ private String fetchTranslationsAsString(String languageCode, Dialect dialect) { private String fetchDefaultTranslationsFromTranslationsManagementSystem(String languageCode, Dialect dialect) { try { - return tranlationMangementServiceApiClient.tryFetchTranslationsFromTranslationManagementService( + log.info("Fetching translations. Translation management system enabled value: {}", + translationManagementSystemEnabled); + return translationManagementSystemEnabled ? translationManagementServiceApiClient.tryFetchTranslationsFromTranslationManagementService( project, component, - languageCode, dialect); + languageCode, dialect) : defaultTranslationsService.fetchDefaultTranslations(component, languageCode, + dialect); } catch (HttpClientErrorException e) { if (HttpStatus.NOT_FOUND.equals(e.getStatusCode())) { log.warn( diff --git a/src/main/java/de/caritas/cob/mailservice/config/apiclient/TranlationMangementServiceApiClient.java b/src/main/java/de/caritas/cob/mailservice/config/apiclient/TranslationManagementServiceApiClient.java similarity index 97% rename from src/main/java/de/caritas/cob/mailservice/config/apiclient/TranlationMangementServiceApiClient.java rename to src/main/java/de/caritas/cob/mailservice/config/apiclient/TranslationManagementServiceApiClient.java index 5b4041b..400c38d 100644 --- a/src/main/java/de/caritas/cob/mailservice/config/apiclient/TranlationMangementServiceApiClient.java +++ b/src/main/java/de/caritas/cob/mailservice/config/apiclient/TranslationManagementServiceApiClient.java @@ -16,7 +16,7 @@ @Service @RequiredArgsConstructor @Slf4j -public class TranlationMangementServiceApiClient { +public class TranslationManagementServiceApiClient { @Value("${weblate.api.url}") private String apiUrl; @@ -24,8 +24,6 @@ public class TranlationMangementServiceApiClient { @Value("${weblate.api.key}") private String apiKey; - - private final @NonNull RestTemplate restTemplate; public String tryFetchTranslationsFromTranslationManagementService(String project, diff --git a/src/test/java/de/caritas/cob/mailservice/api/controller/MailControllerE2EIT.java b/src/test/java/de/caritas/cob/mailservice/api/controller/MailControllerE2EIT.java index 4972ee4..c94b66c 100644 --- a/src/test/java/de/caritas/cob/mailservice/api/controller/MailControllerE2EIT.java +++ b/src/test/java/de/caritas/cob/mailservice/api/controller/MailControllerE2EIT.java @@ -14,7 +14,7 @@ import de.caritas.cob.mailservice.api.model.MailDTO; import de.caritas.cob.mailservice.api.model.MailsDTO; import de.caritas.cob.mailservice.api.model.TemplateDataDTO; -import de.caritas.cob.mailservice.config.apiclient.TranlationMangementServiceApiClient; +import de.caritas.cob.mailservice.config.apiclient.TranslationManagementServiceApiClient; import java.util.List; import java.util.Map; import javax.servlet.http.Cookie; @@ -57,7 +57,7 @@ class MailControllerE2EIT { private JavaMailSender javaMailSender; @MockBean - private TranlationMangementServiceApiClient tranlationMangementServiceApiClient; + private TranslationManagementServiceApiClient translationManagementServiceApiClient; @Captor private ArgumentCaptor mimeMessagePrepCaptor; @@ -67,8 +67,7 @@ class MailControllerE2EIT { @BeforeEach void setUp() { - Mockito.doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)).when( - tranlationMangementServiceApiClient).tryFetchTranslationsFromTranslationManagementService( + Mockito.doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)).when(translationManagementServiceApiClient).tryFetchTranslationsFromTranslationManagementService( anyString(), anyString(), anyString(), any(Dialect.class)); } From 0489c8a2e0985fc0ff7d78ce1d32ee0dc6d99a69 Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Mon, 15 Jan 2024 15:32:30 +0100 Subject: [PATCH 3/7] feat: OB-8353 * Add unit tests for the class TranslationService regarding the logic added --- .../api/service/TranslationServiceTest.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java diff --git a/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java b/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java new file mode 100644 index 0000000..e5daac0 --- /dev/null +++ b/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java @@ -0,0 +1,75 @@ +package de.caritas.cob.mailservice.api.service; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.util.ReflectionTestUtils; + +import de.caritas.cob.mailservice.api.model.Dialect; +import de.caritas.cob.mailservice.config.apiclient.TranslationManagementServiceApiClient; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@SpringBootTest +@ActiveProfiles("testing") +class TranslationServiceTest { + + @Autowired + private TranslationService translationService; + + @MockBean + private TranslationManagementServiceApiClient translationManagementServiceApiClient; + + @MockBean + private DefaultTranslationsService defaultTranslationsService; + + public final String TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_NAME = "translationManagementSystemEnabled"; + + @BeforeEach + public void setUp() { + translationService = new TranslationService(translationManagementServiceApiClient, defaultTranslationsService); + } + + @Test + void fetchTranslations_Should_call_tryFetchTranslationsFromTranslationManagementService_When_translationManagementSystemEnabled_is_true() { + + //Arrange + final boolean TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE = true; + ReflectionTestUtils.setField(translationService, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_NAME, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE); + when(translationManagementServiceApiClient.tryFetchTranslationsFromTranslationManagementService(any(), any(), any(), any())).thenReturn("{\"mail.label.header\": \"Beratung\"}"); + when(defaultTranslationsService.fetchDefaultTranslations(any(), any(), any())).thenReturn("{\"mail.label.header\": \"Beratung\"}"); + + //Act + translationService.fetchTranslations("test", Dialect.INFORMAL); + + //Assert + verify(translationManagementServiceApiClient).tryFetchTranslationsFromTranslationManagementService(any(), any(), any(), any()); + verify(defaultTranslationsService, never()).fetchDefaultTranslations(any(), any(), any()); + + } + + @Test + void fetchTranslations_Should_call_fetchDefaultTranslations_When_translationManagementSystemEnabled_is_false() { + + //Arrange + final boolean TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE = false; + ReflectionTestUtils.setField(translationService, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_NAME, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE); + when(translationManagementServiceApiClient.tryFetchTranslationsFromTranslationManagementService(any(), any(), any(), any())).thenReturn("{\"mail.label.header\": \"Beratung\"}"); + when(defaultTranslationsService.fetchDefaultTranslations(any(), any(), any())).thenReturn("{\"mail.label.header\": \"Beratung\"}"); + + //Act + translationService.fetchTranslations("test", Dialect.INFORMAL); + + //Assert + verify(translationManagementServiceApiClient, never()).tryFetchTranslationsFromTranslationManagementService(any(), any(), any(), any()); + verify(defaultTranslationsService).fetchDefaultTranslations(any(), any(), any()); + + } + +} From 98e052d5e85b0bf6f9ff04aeaa3846fe3dfc21f6 Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Tue, 16 Jan 2024 09:57:07 +0100 Subject: [PATCH 4/7] feat: OB-8353 * Correct unit tests for the TranslationService * Add translation system property to the test properties file * rename the DefaultTranslationsServiceIT file --- .../resources/application-testing.properties | 1 + ...java => DefaultTranslationsServiceIT.java} | 6 ++--- .../api/service/TranslationServiceTest.java | 23 ++++++++----------- 3 files changed, 13 insertions(+), 17 deletions(-) rename src/test/java/de/caritas/cob/mailservice/api/service/{DefaultTranslationsServiceTest.java => DefaultTranslationsServiceIT.java} (93%) diff --git a/src/main/resources/application-testing.properties b/src/main/resources/application-testing.properties index b6c0ddd..3fb4796 100644 --- a/src/main/resources/application-testing.properties +++ b/src/main/resources/application-testing.properties @@ -6,6 +6,7 @@ mail.exchange.version=Exchange2010_SP2 template.use.custom.resources.path=false template.custom.resources.path=false +translation.management.system.enabled=false csrf.header.property=csrfHeader csrf.cookie.property=csrfCookie diff --git a/src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceTest.java b/src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceIT.java similarity index 93% rename from src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceTest.java rename to src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceIT.java index f9554c7..0d3b8a4 100644 --- a/src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceTest.java +++ b/src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceIT.java @@ -10,10 +10,10 @@ @SpringBootTest @ActiveProfiles("testing") -class DefaultTranslationsServiceTest { +class DefaultTranslationsServiceIT { @Autowired - DefaultTranslationsService defaultTranslationsService; + private DefaultTranslationsService defaultTranslationsService; @Test void fetchDefaultTranslations_Should_FetchTranlsationsForInformalGerman() { @@ -45,4 +45,4 @@ void fetchDefaultTranslations_Should_FetchTranlsationsForEnglish() { assertThat(translations).contains("has assigned you {0} new advice seeker."); } -} \ No newline at end of file +} diff --git a/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java b/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java index e5daac0..58f93a7 100644 --- a/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java +++ b/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java @@ -2,31 +2,28 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.context.ActiveProfiles; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.test.util.ReflectionTestUtils; import de.caritas.cob.mailservice.api.model.Dialect; import de.caritas.cob.mailservice.config.apiclient.TranslationManagementServiceApiClient; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; -@SpringBootTest -@ActiveProfiles("testing") +@ExtendWith(MockitoExtension.class) class TranslationServiceTest { - @Autowired private TranslationService translationService; - @MockBean + @Mock private TranslationManagementServiceApiClient translationManagementServiceApiClient; - @MockBean + @Mock private DefaultTranslationsService defaultTranslationsService; public final String TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_NAME = "translationManagementSystemEnabled"; @@ -43,14 +40,13 @@ void fetchTranslations_Should_call_tryFetchTranslationsFromTranslationManagement final boolean TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE = true; ReflectionTestUtils.setField(translationService, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_NAME, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE); when(translationManagementServiceApiClient.tryFetchTranslationsFromTranslationManagementService(any(), any(), any(), any())).thenReturn("{\"mail.label.header\": \"Beratung\"}"); - when(defaultTranslationsService.fetchDefaultTranslations(any(), any(), any())).thenReturn("{\"mail.label.header\": \"Beratung\"}"); //Act translationService.fetchTranslations("test", Dialect.INFORMAL); //Assert verify(translationManagementServiceApiClient).tryFetchTranslationsFromTranslationManagementService(any(), any(), any(), any()); - verify(defaultTranslationsService, never()).fetchDefaultTranslations(any(), any(), any()); + verifyNoInteractions(defaultTranslationsService); } @@ -60,14 +56,13 @@ void fetchTranslations_Should_call_fetchDefaultTranslations_When_translationMana //Arrange final boolean TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE = false; ReflectionTestUtils.setField(translationService, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_NAME, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE); - when(translationManagementServiceApiClient.tryFetchTranslationsFromTranslationManagementService(any(), any(), any(), any())).thenReturn("{\"mail.label.header\": \"Beratung\"}"); when(defaultTranslationsService.fetchDefaultTranslations(any(), any(), any())).thenReturn("{\"mail.label.header\": \"Beratung\"}"); //Act translationService.fetchTranslations("test", Dialect.INFORMAL); //Assert - verify(translationManagementServiceApiClient, never()).tryFetchTranslationsFromTranslationManagementService(any(), any(), any(), any()); + verifyNoInteractions(translationManagementServiceApiClient); verify(defaultTranslationsService).fetchDefaultTranslations(any(), any(), any()); } From 1b16e087a405330e32b92f7f2f391a4427cb160a Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Wed, 17 Jan 2024 16:56:22 +0100 Subject: [PATCH 5/7] feat: OB-8353 * Move getDialectSuffix method from TranslationManagementServiceApiClient to DefaultTranslationsService * Added tests to DefaultTranslationsServiceIT --- .../service/DefaultTranslationsService.java | 23 ++++++-- ...TranslationManagementServiceApiClient.java | 14 +---- .../service/DefaultTranslationsServiceIT.java | 57 +++++++++++++++++++ .../api/service/TranslationServiceTest.java | 45 ++++++++++++--- 4 files changed, 114 insertions(+), 25 deletions(-) diff --git a/src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java b/src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java index 9eea574..4765b95 100644 --- a/src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java +++ b/src/main/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsService.java @@ -1,19 +1,20 @@ package de.caritas.cob.mailservice.api.service; -import de.caritas.cob.mailservice.api.model.Dialect; -import de.caritas.cob.mailservice.config.apiclient.TranslationManagementServiceApiClient; - import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.List; -import lombok.extern.slf4j.Slf4j; + import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import de.caritas.cob.mailservice.api.model.Dialect; +import lombok.extern.slf4j.Slf4j; + @Slf4j @Service public class DefaultTranslationsService { @@ -50,7 +51,7 @@ private InputStream tryFetchExternalTranslationWithFallbackToEmptyDialect(String private FileInputStream buildStreamForExternalPath(String translationComponentName, String languageCode, Dialect dialect) { try { - String filename = String.format("%s/%s.%s%s.json", customTranslationsPath, translationComponentName.toLowerCase(), languageCode, TranslationManagementServiceApiClient.getDialectSuffix(dialect)); + String filename = String.format("%s/%s.%s%s.json", customTranslationsPath, translationComponentName.toLowerCase(), languageCode, getDialectSuffix(dialect)); return new FileInputStream(filename); } catch (FileNotFoundException e) { log.warn("Default translations for component {}, language {} not found in external path {}", translationComponentName, @@ -85,10 +86,20 @@ private InputStream getInputStream(String translationComponentName, String langu Dialect dialect) { String translationFilename = getTranslationFilename( translationComponentName + "." + languageCode - + TranslationManagementServiceApiClient.getDialectSuffix(dialect)); + + getDialectSuffix(dialect)); return TranslationService.class.getResourceAsStream(translationFilename); } + public static String getDialectSuffix(Dialect dialect) { + if (dialect == null) { + return StringUtils.EMPTY; + } + if (dialect == Dialect.INFORMAL) { + return "_informal"; + } + return StringUtils.EMPTY; + } + private String getTranslationFilename(String templateName) { return "/i18n/" + templateName.toLowerCase() + ".json"; } diff --git a/src/main/java/de/caritas/cob/mailservice/config/apiclient/TranslationManagementServiceApiClient.java b/src/main/java/de/caritas/cob/mailservice/config/apiclient/TranslationManagementServiceApiClient.java index 400c38d..8ec5776 100644 --- a/src/main/java/de/caritas/cob/mailservice/config/apiclient/TranslationManagementServiceApiClient.java +++ b/src/main/java/de/caritas/cob/mailservice/config/apiclient/TranslationManagementServiceApiClient.java @@ -30,7 +30,7 @@ public String tryFetchTranslationsFromTranslationManagementService(String projec String component, String languageCode, Dialect dialect) { HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "Token " + apiKey); - String url = apiUrl + "translations/" + project + "/" + component + "/" + languageCode + getWeblateDialectSuffix(dialect) + String url = apiUrl + "translations/" + project + "/" + component + "/" + languageCode + getTranslationManagementServiceDialectSuffix(dialect) + "/file.json"; log.info("Calling url to fetch translations: {}", url); @@ -39,7 +39,7 @@ public String tryFetchTranslationsFromTranslationManagementService(String projec return response.getBody(); } - public static String getWeblateDialectSuffix(Dialect dialect) { + public static String getTranslationManagementServiceDialectSuffix(Dialect dialect) { if (dialect == null) { return StringUtils.EMPTY; } @@ -48,14 +48,4 @@ public static String getWeblateDialectSuffix(Dialect dialect) { } return StringUtils.EMPTY; } - - public static String getDialectSuffix(Dialect dialect) { - if (dialect == null) { - return StringUtils.EMPTY; - } - if (dialect == Dialect.INFORMAL) { - return "_informal"; - } - return StringUtils.EMPTY; - } } diff --git a/src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceIT.java b/src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceIT.java index 0d3b8a4..976a42d 100644 --- a/src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceIT.java +++ b/src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceIT.java @@ -3,10 +3,14 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import de.caritas.cob.mailservice.api.model.Dialect; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.util.ReflectionTestUtils; @SpringBootTest @ActiveProfiles("testing") @@ -15,6 +19,20 @@ class DefaultTranslationsServiceIT { @Autowired private DefaultTranslationsService defaultTranslationsService; + public final String USE_CUSTOM_RESOURCES_PATH_FIELD_NAME = "useCustomResourcesPath"; + public final String CUSTOM_TRANSLATIONS_PATH_FIELD_NAME = "customTranslationsPath"; + + @BeforeEach + public void setUp() { + defaultTranslationsService = new DefaultTranslationsService(); + ReflectionTestUtils.setField(defaultTranslationsService, CUSTOM_TRANSLATIONS_PATH_FIELD_NAME, System.getProperty("user.dir") + "/src/main/resources/i18n"); + } + + @AfterEach + public void clean() { + ReflectionTestUtils.setField(defaultTranslationsService, USE_CUSTOM_RESOURCES_PATH_FIELD_NAME, false); + } + @Test void fetchDefaultTranslations_Should_FetchTranlsationsForInformalGerman() { @@ -45,4 +63,43 @@ void fetchDefaultTranslations_Should_FetchTranlsationsForEnglish() { assertThat(translations).contains("has assigned you {0} new advice seeker."); } + @Test + void fetchDefaultTranslations_Should_fetchExternalTranslationsForFormalIfDialectNullGerman_When_useCustomResourcesPath_is_true(){ + + ReflectionTestUtils.setField(defaultTranslationsService, USE_CUSTOM_RESOURCES_PATH_FIELD_NAME, true); + + String translations = defaultTranslationsService.fetchDefaultTranslations("mailservice", "de", + null); + + assertThat(translations).isNotNull(); + assertThat(translations).contains("Sie haben eine neue Nachricht in Ihren Beratungen"); + + } + + @Test + void fetchDefaultTranslations_Should_fetchExternalTranslationsForEnglish_When_useCustomResourcesPath_is_true(){ + + ReflectionTestUtils.setField(defaultTranslationsService, USE_CUSTOM_RESOURCES_PATH_FIELD_NAME, true); + + String translations = defaultTranslationsService.fetchDefaultTranslations("mailservice", "en", + null); + + assertThat(translations).isNotNull(); + assertThat(translations).contains("You have a new message in your counselings"); + + } + + @Test + void fetchDefaultTranslations_Should_fetchExternalTranslationsForInformalGerman_When_useCustomResourcesPath_is_true(){ + + ReflectionTestUtils.setField(defaultTranslationsService, USE_CUSTOM_RESOURCES_PATH_FIELD_NAME, true); + + String translations = defaultTranslationsService.fetchDefaultTranslations("mailservice", "de", + Dialect.INFORMAL); + + assertThat(translations).isNotNull(); + assertThat(translations).contains("Du hast eine neue Nachricht in Deinen Beratungen"); + + } + } diff --git a/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java b/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java index 58f93a7..fc14f90 100644 --- a/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java +++ b/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java @@ -5,7 +5,9 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.client.HttpClientErrorException; import de.caritas.cob.mailservice.api.model.Dialect; import de.caritas.cob.mailservice.config.apiclient.TranslationManagementServiceApiClient; @@ -13,6 +15,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @@ -34,34 +37,62 @@ public void setUp() { } @Test - void fetchTranslations_Should_call_tryFetchTranslationsFromTranslationManagementService_When_translationManagementSystemEnabled_is_true() { + void fetchTranslations_Should_call_translationManagementServiceApiClient_When_translationManagementSystemEnabled_is_true() { + + // Given - //Arrange final boolean TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE = true; ReflectionTestUtils.setField(translationService, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_NAME, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE); when(translationManagementServiceApiClient.tryFetchTranslationsFromTranslationManagementService(any(), any(), any(), any())).thenReturn("{\"mail.label.header\": \"Beratung\"}"); - //Act + // When + translationService.fetchTranslations("test", Dialect.INFORMAL); - //Assert + // Then + verify(translationManagementServiceApiClient).tryFetchTranslationsFromTranslationManagementService(any(), any(), any(), any()); verifyNoInteractions(defaultTranslationsService); } + @Test + void fetchTranslations_Should_call_fetchDefaultTranslations_When_translationManagementSystemEnabled_is_true_but_translationManagementServiceApiClient_Throws_HttpClientErrorException() { + + // Given + + final boolean TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE = true; + ReflectionTestUtils.setField(translationService, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_NAME, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE); + when(translationManagementServiceApiClient.tryFetchTranslationsFromTranslationManagementService(any(), any(), any(), any())).thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)); + when(defaultTranslationsService.fetchDefaultTranslations(any(), any(), any())).thenReturn("{\"mail.label.header\": \"Beratung\"}"); + + // When + + translationService.fetchTranslations("test", Dialect.INFORMAL); + + // Then + + verify(translationManagementServiceApiClient).tryFetchTranslationsFromTranslationManagementService(any(), any(), any(), any()); + verify(defaultTranslationsService).fetchDefaultTranslations(any(), any(), any()); + verifyNoMoreInteractions(translationManagementServiceApiClient); + + } + @Test void fetchTranslations_Should_call_fetchDefaultTranslations_When_translationManagementSystemEnabled_is_false() { - //Arrange + // Given + final boolean TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE = false; ReflectionTestUtils.setField(translationService, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_NAME, TRANSLATION_MANAGEMENT_SYSTEM_ENABLED_FIELD_VALUE); when(defaultTranslationsService.fetchDefaultTranslations(any(), any(), any())).thenReturn("{\"mail.label.header\": \"Beratung\"}"); - //Act + // When + translationService.fetchTranslations("test", Dialect.INFORMAL); - //Assert + // Then + verifyNoInteractions(translationManagementServiceApiClient); verify(defaultTranslationsService).fetchDefaultTranslations(any(), any(), any()); From 1c0b2116b19ba63df8552133f71f5d077857d9fb Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Thu, 18 Jan 2024 08:07:37 +0100 Subject: [PATCH 6/7] feat: OB-8353 * Move getDialectSuffix method from TranslationManagementServiceApiClient to DefaultTranslationsService * Added tests to DefaultTranslationsServiceIT --- .../cob/mailservice/api/service/TranslationServiceTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java b/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java index fc14f90..eea8d00 100644 --- a/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java +++ b/src/test/java/de/caritas/cob/mailservice/api/service/TranslationServiceTest.java @@ -97,5 +97,4 @@ void fetchTranslations_Should_call_fetchDefaultTranslations_When_translationMana verify(defaultTranslationsService).fetchDefaultTranslations(any(), any(), any()); } - } From 12d1ef5c7ba02d785ebc0c039c4d9448963dbe97 Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Thu, 18 Jan 2024 08:16:47 +0100 Subject: [PATCH 7/7] feat: OB-8353 * Add space between the method name and "{" --- .../api/service/DefaultTranslationsServiceIT.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceIT.java b/src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceIT.java index 976a42d..d413fc8 100644 --- a/src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceIT.java +++ b/src/test/java/de/caritas/cob/mailservice/api/service/DefaultTranslationsServiceIT.java @@ -64,7 +64,7 @@ void fetchDefaultTranslations_Should_FetchTranlsationsForEnglish() { } @Test - void fetchDefaultTranslations_Should_fetchExternalTranslationsForFormalIfDialectNullGerman_When_useCustomResourcesPath_is_true(){ + void fetchDefaultTranslations_Should_fetchExternalTranslationsForFormalIfDialectNullGerman_When_useCustomResourcesPath_is_true() { ReflectionTestUtils.setField(defaultTranslationsService, USE_CUSTOM_RESOURCES_PATH_FIELD_NAME, true); @@ -77,7 +77,7 @@ void fetchDefaultTranslations_Should_fetchExternalTranslationsForFormalIfDialect } @Test - void fetchDefaultTranslations_Should_fetchExternalTranslationsForEnglish_When_useCustomResourcesPath_is_true(){ + void fetchDefaultTranslations_Should_fetchExternalTranslationsForEnglish_When_useCustomResourcesPath_is_true() { ReflectionTestUtils.setField(defaultTranslationsService, USE_CUSTOM_RESOURCES_PATH_FIELD_NAME, true); @@ -90,7 +90,7 @@ void fetchDefaultTranslations_Should_fetchExternalTranslationsForEnglish_When_us } @Test - void fetchDefaultTranslations_Should_fetchExternalTranslationsForInformalGerman_When_useCustomResourcesPath_is_true(){ + void fetchDefaultTranslations_Should_fetchExternalTranslationsForInformalGerman_When_useCustomResourcesPath_is_true() { ReflectionTestUtils.setField(defaultTranslationsService, USE_CUSTOM_RESOURCES_PATH_FIELD_NAME, true);