From b52c576da8ed9f77d80fe7a68a62dcd316cea90f Mon Sep 17 00:00:00 2001 From: BrenoEpic Date: Fri, 2 Feb 2024 00:53:49 -0300 Subject: [PATCH] refactor Translation Response --- .../io/github/brenoepics/at4j/AzureApi.java | 5 +- .../brenoepics/at4j/core/AzureApiImpl.java | 7 +- .../at4j/data/request/TranslateParams.java | 13 ++-- .../data/response/TranslationResponse.java | 72 ++++++++---------- .../at4j/data/response/TranslationResult.java | 76 +++++++++++++++++++ .../github/brenoepics/at4j/AzureApiTest.java | 15 ++-- .../at4j/core/AzureApiImplTest.java | 3 +- ...seTest.java => TranslationResultTest.java} | 12 +-- 8 files changed, 136 insertions(+), 67 deletions(-) create mode 100644 src/main/java/io/github/brenoepics/at4j/data/response/TranslationResult.java rename src/test/java/io/github/brenoepics/at4j/data/response/{TranslationResponseTest.java => TranslationResultTest.java} (77%) diff --git a/src/main/java/io/github/brenoepics/at4j/AzureApi.java b/src/main/java/io/github/brenoepics/at4j/AzureApi.java index ba36c99..c1f7445 100644 --- a/src/main/java/io/github/brenoepics/at4j/AzureApi.java +++ b/src/main/java/io/github/brenoepics/at4j/AzureApi.java @@ -8,6 +8,7 @@ import io.github.brenoepics.at4j.data.request.DetectLanguageParams; import io.github.brenoepics.at4j.data.request.TranslateParams; import io.github.brenoepics.at4j.data.response.TranslationResponse; +import io.github.brenoepics.at4j.data.response.TranslationResult; import java.util.Collection; import java.util.List; @@ -71,9 +72,9 @@ public interface AzureApi { * Translates the given text from the given source language to the given target language. * * @param params The {@link TranslateParams} to translate. - * @return The {@link TranslationResponse} containing the translation. + * @return The {@link TranslationResult} containing the translation. */ - CompletableFuture>> translate(TranslateParams params); + CompletableFuture> translate(TranslateParams params); /** * Gets the available languages for translation. diff --git a/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java b/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java index 402fba9..053116f 100644 --- a/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java +++ b/src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java @@ -14,6 +14,7 @@ import io.github.brenoepics.at4j.data.request.DetectLanguageParams; import io.github.brenoepics.at4j.data.request.TranslateParams; import io.github.brenoepics.at4j.data.response.TranslationResponse; +import io.github.brenoepics.at4j.data.response.TranslationResult; import io.github.brenoepics.at4j.util.rest.RestEndpoint; import io.github.brenoepics.at4j.util.rest.RestMethod; import io.github.brenoepics.at4j.util.rest.RestRequest; @@ -89,13 +90,13 @@ public ThreadPool getThreadPool() { } @Override - public CompletableFuture>> translate(TranslateParams params) { + public CompletableFuture> translate(TranslateParams params) { if (params.getTexts() == null || params.getTexts().isEmpty()) { return CompletableFuture.completedFuture(Optional.empty()); } - RestRequest>> request = - new RestRequest>>( + RestRequest> request = + new RestRequest>( this, RestMethod.POST, RestEndpoint.TRANSLATE) .setBody(params.getBody()); params.getQueryParameters().forEach(request::addQueryParameter); diff --git a/src/main/java/io/github/brenoepics/at4j/data/request/TranslateParams.java b/src/main/java/io/github/brenoepics/at4j/data/request/TranslateParams.java index 535d7c2..1fbcb6c 100644 --- a/src/main/java/io/github/brenoepics/at4j/data/request/TranslateParams.java +++ b/src/main/java/io/github/brenoepics/at4j/data/request/TranslateParams.java @@ -11,6 +11,7 @@ import io.github.brenoepics.at4j.data.request.optional.ProfanityMarker; import io.github.brenoepics.at4j.data.request.optional.TextType; import io.github.brenoepics.at4j.data.response.TranslationResponse; +import io.github.brenoepics.at4j.data.response.TranslationResult; import io.github.brenoepics.at4j.util.rest.RestRequestResult; import java.util.*; @@ -289,12 +290,12 @@ public JsonNode getBody() { return body; } - public Optional> handleTranslations( - RestRequestResult>> response) { + public Optional handleTranslations( + RestRequestResult> response) { if (response.getJsonBody().isNull() || response.getJsonBody().isEmpty()) return Optional.empty(); - List responses = new ArrayList<>(); + TranslationResponse responses = new TranslationResponse(); getTexts() .forEach( (index, baseText) -> { @@ -308,15 +309,15 @@ public Optional> handleTranslations( if (jsonNode.has("detectedLanguage")) { JsonNode detectedLanguage = jsonNode.get("detectedLanguage"); - responses.add( - new TranslationResponse( + responses.addResult( + new TranslationResult( baseText, DetectedLanguage.ofJSON((ObjectNode) detectedLanguage), translations)); return; } - responses.add(new TranslationResponse(baseText, translations)); + responses.addResult(new TranslationResult(baseText, translations)); }); return Optional.of(responses); diff --git a/src/main/java/io/github/brenoepics/at4j/data/response/TranslationResponse.java b/src/main/java/io/github/brenoepics/at4j/data/response/TranslationResponse.java index 1907d50..14c1dee 100644 --- a/src/main/java/io/github/brenoepics/at4j/data/response/TranslationResponse.java +++ b/src/main/java/io/github/brenoepics/at4j/data/response/TranslationResponse.java @@ -1,76 +1,66 @@ package io.github.brenoepics.at4j.data.response; -import io.github.brenoepics.at4j.data.DetectedLanguage; -import io.github.brenoepics.at4j.data.Translation; - -import java.util.Collection; +import java.util.ArrayList; +import java.util.List; /** - * This class represents a response from a translation service. It contains the detected language of - * the input and a collection of translations. + * This class represents a response that contains a list of translation results. */ public class TranslationResponse { + // List to store the translation results + private final List resultList; /** - * The detected language of the input text. It can be null if the language could not be detected. - * - * @see DetectedLanguage + * Default constructor that initializes an empty list of translation results. */ - private DetectedLanguage detectedLanguage = null; - - private final String baseText; - - // A collection of translations for the input text. - private final Collection translations; + public TranslationResponse() { + resultList = new ArrayList<>(); + } /** - * Constructs a TranslationResponse with a detected language and a collection of translations. + * Constructor that initializes the list of translation results with the provided list. * - * @param detectedLanguage The detected language of the input text. - * @param translations A collection of translations for the input text. + * @param results The list of translation results to be stored. */ - public TranslationResponse( - String baseText, DetectedLanguage detectedLanguage, Collection translations) { - this.baseText = baseText; - this.detectedLanguage = detectedLanguage; - this.translations = translations; + public TranslationResponse(List results) { + resultList = results; } /** - * Constructs a TranslationResponse with a collection of translations. The detected language is - * set to null. + * Method to add a translation result to the list. * - * @param translations A collection of translations for the input text. + * @param result The translation result to be added. */ - public TranslationResponse(String baseText, Collection translations) { - this.baseText = baseText; - this.translations = translations; + public void addResult(TranslationResult result) { + resultList.add(result); } /** - * Returns the detected language of the input text. + * Method to retrieve the list of translation results. * - * @return The detected language of the input text. + * @return The list of translation results. */ - public DetectedLanguage getDetectedLanguage() { - return detectedLanguage; + public List getResultList() { + return resultList; } /** - * Returns the collection of translations for the input text. + * Static factory method to create a new TranslationResponse with the provided list of results. * - * @return The collection of translations for the input text. + * @param results The list of translation results to be stored. + * @return A new TranslationResponse instance. */ - public Collection getTranslations() { - return translations; + public static TranslationResponse of(List results) { + return new TranslationResponse(results); } /** - * Returns the base texts that were translated + * Static factory method to create a new TranslationResponse with the provided result. * - * @return the base text + * @param result The translation result to be stored. + * @return A new TranslationResponse instance. */ - public String getBaseText() { - return baseText; + public static TranslationResponse of(TranslationResult result) { + return new TranslationResponse(List.of(result)); } } diff --git a/src/main/java/io/github/brenoepics/at4j/data/response/TranslationResult.java b/src/main/java/io/github/brenoepics/at4j/data/response/TranslationResult.java new file mode 100644 index 0000000..1ee9986 --- /dev/null +++ b/src/main/java/io/github/brenoepics/at4j/data/response/TranslationResult.java @@ -0,0 +1,76 @@ +package io.github.brenoepics.at4j.data.response; + +import io.github.brenoepics.at4j.data.DetectedLanguage; +import io.github.brenoepics.at4j.data.Translation; + +import java.util.Collection; + +/** + * This class represents a response from a translation service. It contains the detected language of + * the input and a collection of translations. + */ +public class TranslationResult { + + /** + * The detected language of the input text. It can be null if the language could not be detected. + * + * @see DetectedLanguage + */ + private DetectedLanguage detectedLanguage = null; + + private final String baseText; + + // A collection of translations for the input text. + private final Collection translations; + + /** + * Constructs a TranslationResponse with a detected language and a collection of translations. + * + * @param detectedLanguage The detected language of the input text. + * @param translations A collection of translations for the input text. + */ + public TranslationResult( + String baseText, DetectedLanguage detectedLanguage, Collection translations) { + this.baseText = baseText; + this.detectedLanguage = detectedLanguage; + this.translations = translations; + } + + /** + * Constructs a TranslationResponse with a collection of translations. The detected language is + * set to null. + * + * @param translations A collection of translations for the input text. + */ + public TranslationResult(String baseText, Collection translations) { + this.baseText = baseText; + this.translations = translations; + } + + /** + * Returns the detected language of the input text. + * + * @return The detected language of the input text. + */ + public DetectedLanguage getDetectedLanguage() { + return detectedLanguage; + } + + /** + * Returns the collection of translations for the input text. + * + * @return The collection of translations for the input text. + */ + public Collection getTranslations() { + return translations; + } + + /** + * Returns the base texts that were translated + * + * @return the base text + */ + public String getBaseText() { + return baseText; + } +} diff --git a/src/test/java/io/github/brenoepics/at4j/AzureApiTest.java b/src/test/java/io/github/brenoepics/at4j/AzureApiTest.java index 1793bf4..c4c0b73 100644 --- a/src/test/java/io/github/brenoepics/at4j/AzureApiTest.java +++ b/src/test/java/io/github/brenoepics/at4j/AzureApiTest.java @@ -1,5 +1,7 @@ package io.github.brenoepics.at4j; +import static org.junit.jupiter.api.Assertions.*; + import io.github.brenoepics.at4j.azure.BaseURL; import io.github.brenoepics.at4j.azure.lang.Language; import io.github.brenoepics.at4j.data.DetectedLanguage; @@ -7,16 +9,13 @@ import io.github.brenoepics.at4j.data.request.DetectLanguageParams; import io.github.brenoepics.at4j.data.request.TranslateParams; import io.github.brenoepics.at4j.data.response.TranslationResponse; - import java.util.Collection; import java.util.List; import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; - import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; class AzureApiTest { @Test @@ -70,7 +69,7 @@ void translateEmptyKey() { AzureApi api = new AzureApiBuilder().baseURL(BaseURL.GLOBAL).setKey("").region("test").build(); TranslateParams params = new TranslateParams("test", List.of("pt")).setSourceLanguage("en"); - CompletableFuture>> translation = api.translate(params); + CompletableFuture> translation = api.translate(params); assertThrows(CompletionException.class, translation::join); api.disconnect(); } @@ -88,9 +87,9 @@ void translateHelloWorld() { AzureApi api = builder.build(); TranslateParams params = new TranslateParams("Hello World!", List.of("pt", "es")); - Optional> translate = api.translate(params).join(); + Optional translate = api.translate(params).join(); assertTrue(translate.isPresent()); - assertEquals(2, translate.get().get(0).getTranslations().size()); + assertEquals(2, translate.get().getResultList().get(0).getTranslations().size()); } @Test @@ -107,10 +106,10 @@ void translateMultiText() { TranslateParams params = new TranslateParams(List.of("Hello World!", "How are you?"), List.of("pt", "es")); - Optional> translate = api.translate(params).join(); + Optional translate = api.translate(params).join(); assertTrue(translate.isPresent()); - assertEquals(2, translate.get().size()); + assertEquals(2, translate.get().getResultList().size()); } @Test diff --git a/src/test/java/io/github/brenoepics/at4j/core/AzureApiImplTest.java b/src/test/java/io/github/brenoepics/at4j/core/AzureApiImplTest.java index dc881b9..0b50562 100644 --- a/src/test/java/io/github/brenoepics/at4j/core/AzureApiImplTest.java +++ b/src/test/java/io/github/brenoepics/at4j/core/AzureApiImplTest.java @@ -4,6 +4,7 @@ import io.github.brenoepics.at4j.core.exceptions.AzureException; import io.github.brenoepics.at4j.data.request.TranslateParams; import io.github.brenoepics.at4j.data.response.TranslationResponse; +import io.github.brenoepics.at4j.data.response.TranslationResult; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; @@ -40,7 +41,7 @@ void returnsEmptyOnInvalidInput() { } }); - CompletableFuture>> response = + CompletableFuture> response = azureApi.translate(translateParams); assertFalse(response.join().isPresent()); diff --git a/src/test/java/io/github/brenoepics/at4j/data/response/TranslationResponseTest.java b/src/test/java/io/github/brenoepics/at4j/data/response/TranslationResultTest.java similarity index 77% rename from src/test/java/io/github/brenoepics/at4j/data/response/TranslationResponseTest.java rename to src/test/java/io/github/brenoepics/at4j/data/response/TranslationResultTest.java index eab2616..1e97200 100644 --- a/src/test/java/io/github/brenoepics/at4j/data/response/TranslationResponseTest.java +++ b/src/test/java/io/github/brenoepics/at4j/data/response/TranslationResultTest.java @@ -10,14 +10,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -class TranslationResponseTest { +class TranslationResultTest { @Test void createsTranslationResponseWithDetectedLanguageAndTranslations() { DetectedLanguage detectedLanguage = new DetectedLanguage("en", 1.0f); Translation translation = new Translation("pt", "Olá, mundo!"); - TranslationResponse response = - new TranslationResponse(translation.getText(), detectedLanguage, List.of(translation)); + TranslationResult response = + new TranslationResult(translation.getText(), detectedLanguage, List.of(translation)); assertEquals(detectedLanguage, response.getDetectedLanguage()); assertEquals(1, response.getTranslations().size()); @@ -27,8 +27,8 @@ void createsTranslationResponseWithDetectedLanguageAndTranslations() { @Test void createsTranslationResponseWithTranslationsOnly() { Translation translation = new Translation("pt", "Olá, mundo!"); - TranslationResponse response = - new TranslationResponse(translation.getText(), List.of(translation)); + TranslationResult response = + new TranslationResult(translation.getText(), List.of(translation)); assertNull(response.getDetectedLanguage()); assertEquals(1, response.getTranslations().size()); @@ -37,7 +37,7 @@ void createsTranslationResponseWithTranslationsOnly() { @Test void returnsEmptyTranslationsWhenNoneProvided() { - TranslationResponse response = new TranslationResponse("", Collections.emptyList()); + TranslationResult response = new TranslationResult("", Collections.emptyList()); assertEquals(0, response.getTranslations().size()); } }