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());