Skip to content

Commit

Permalink
feat: weblate integration
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuzynow committed Nov 22, 2023
1 parent cf11913 commit 91290c4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.caritas.cob.mailservice.config.apiclient.TranlationMangementServiceApiClient;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.annotations.Cacheable;

@Service
Expand All @@ -37,10 +34,10 @@ public class TranslationService {
@Value("${weblate.component}")
private String component;

private final RestTemplate restTemplate;
private final @NonNull TranlationMangementServiceApiClient tranlationMangementServiceApiClient;

public TranslationService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
public TranslationService(TranlationMangementServiceApiClient tranlationMangementServiceApiClient) {
this.tranlationMangementServiceApiClient = tranlationMangementServiceApiClient;
}

@Cacheable(value = "translations")
Expand Down Expand Up @@ -79,7 +76,7 @@ public Optional<Map<String, String>> tryFetchTranslations(String languageCode) {

private String fetchTranslationsAsString(String languageCode) {
try {
return tryFetchTranslationsFromTranslationManagementService(project, component,
return tranlationMangementServiceApiClient.tryFetchTranslationsFromTranslationManagementService(project, component,
languageCode);
} catch (HttpClientErrorException e) {
if (HttpStatus.NOT_FOUND.equals(e.getStatusCode())) {
Expand All @@ -93,17 +90,7 @@ private String fetchTranslationsAsString(String languageCode) {
}
}

private String tryFetchTranslationsFromTranslationManagementService(String project,
String component, String languageCode) {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Token " + apiKey);
String url = apiUrl + "translations/" + project + "/" + component + "/test" + "/" + languageCode
+ "/file.json";

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,
new HttpEntity<>(headers), String.class);
return response.getBody();
}

private String fetchDefaultTranslations(String translationComponentName, String languageCode) {
var inputStream = TranslationService.class.getResourceAsStream(
Expand All @@ -116,7 +103,7 @@ private String fetchDefaultTranslations(String translationComponentName, String
.readLines(inputStream, StandardCharsets.UTF_8.displayName());
return String.join("", fileLines);
} catch (IOException ex) {
throw new RuntimeException(String.format(
throw new IllegalStateException(String.format(
"Json file with translations could not be loaded, translation component name: %s",
translationComponentName), ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.caritas.cob.mailservice.config.apiclient;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@RequiredArgsConstructor
public class TranlationMangementServiceApiClient {

@Value("${weblate.api.url}")
private String apiUrl;

@Value("${weblate.api.key}")
private String apiKey;



private final @NonNull RestTemplate restTemplate;

public String tryFetchTranslationsFromTranslationManagementService(String project,
String component, String languageCode) {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Token " + apiKey);
String url = apiUrl + "translations/" + project + "/" + component + "/test" + "/" + languageCode
+ "/file.json";

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,
new HttpEntity<>(headers), String.class);
return response.getBody();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand All @@ -11,23 +13,28 @@
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 java.util.List;
import java.util.Map;
import javax.servlet.http.Cookie;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.client.HttpClientErrorException;

@SpringBootTest
@AutoConfigureMockMvc
Expand All @@ -48,12 +55,22 @@ class MailControllerE2EIT {
@Qualifier("emailsender")
private JavaMailSender javaMailSender;

@MockBean
private TranlationMangementServiceApiClient tranlationMangementServiceApiClient;

@Captor
private ArgumentCaptor<MimeMessagePreparator> mimeMessagePrepCaptor;

private MailsDTO mailsDTO;
private Map<String, List<Map<String, Object>>> mailsDTOMap;

@BeforeEach
void setUp() {
Mockito.doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)).when(
tranlationMangementServiceApiClient).tryFetchTranslationsFromTranslationManagementService(
anyString(), anyString(), anyString());
}

@Test
void sendMailsShouldRespondWithOkWhenEmailListIsEmpty() throws Exception {
givenAnEmptyEmailList();
Expand Down

0 comments on commit 91290c4

Please sign in to comment.