From 921b92c8f760ba304458ff7af87c54982e334281 Mon Sep 17 00:00:00 2001 From: BrenoEpic Date: Thu, 8 Feb 2024 14:11:20 -0300 Subject: [PATCH] test: add Param Tests --- .../data/request/TranslateParamsTest.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/src/test/java/io/github/brenoepics/at4j/data/request/TranslateParamsTest.java b/src/test/java/io/github/brenoepics/at4j/data/request/TranslateParamsTest.java index 7a67078..af7fc90 100644 --- a/src/test/java/io/github/brenoepics/at4j/data/request/TranslateParamsTest.java +++ b/src/test/java/io/github/brenoepics/at4j/data/request/TranslateParamsTest.java @@ -1,6 +1,12 @@ package io.github.brenoepics.at4j.data.request; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import io.github.brenoepics.at4j.azure.lang.Language; +import io.github.brenoepics.at4j.azure.lang.LanguageDirection; +import io.github.brenoepics.at4j.data.Translation; import io.github.brenoepics.at4j.data.request.optional.ProfanityAction; import io.github.brenoepics.at4j.data.request.optional.ProfanityMarker; import io.github.brenoepics.at4j.data.request.optional.TextType; @@ -66,6 +72,13 @@ void shouldSetAndGetSourceLanguage() { assertEquals("en", params.getSourceLanguage()); } + @Test + void shouldSetAndGetSourceLanguage2() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + params.setSourceLanguage(new Language("en", "English", "English", LanguageDirection.LTR)); + assertEquals("en", params.getSourceLanguage()); + } + @Test void shouldSetAndGetTargetLanguages() { TranslateParams params = new TranslateParams("Hello", List.of("fr")); @@ -73,6 +86,14 @@ void shouldSetAndGetTargetLanguages() { assertTrue(params.getTargetLanguages().containsAll(Arrays.asList("de", "it"))); } + @Test + void shouldSetAndGetTargetLanguages2() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + params.setTargetLanguages( + Collections.singleton(new Language("en", "English", "English", LanguageDirection.LTR))); + assertTrue(params.getTargetLanguages().contains("en")); + } + @Test void shouldSetAndGetSuggestedFromLanguage() { TranslateParams params = new TranslateParams("Hello", List.of("fr")); @@ -221,4 +242,60 @@ void shouldHandleJsonBodyWithoutTranslations() { assertNotNull(result.get()); assertThrows(IndexOutOfBoundsException.class, result.get()::getFirstResult); } + + @Test + void shouldHandleJsonBodyNullTranslations() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + RestRequestResult mockResponse = mock(RestRequestResult.class); + JsonNode mockJsonBody = mock(JsonNode.class); + when(mockResponse.getJsonBody()).thenReturn(mockJsonBody); + when(mockResponse.getJsonBody().isNull()).thenReturn(true); + + Optional result = params.handleResponse(mockResponse); + + assertFalse(result.isPresent()); + } + + @Test + void shouldHandleJsonBodyWithValidTranslations() throws JsonProcessingException { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + String json = + "[{\"detectedLanguage\":{\"language\":\"en\",\"score\":0.98},\"translations\":[{\"text\":\"Olá, mundo\",\"to\":\"pt\"}]}]"; + RestRequestResult mockResponse = mock(RestRequestResult.class); + ObjectMapper mapper = new ObjectMapper(); + JsonNode mockJsonBody = mapper.readTree(json); + + when(mockResponse.getJsonBody()).thenReturn(mockJsonBody); + + Optional result = params.handleResponse(mockResponse); + + assertTrue(result.isPresent()); + assertNotNull(result.get()); + assertEquals( + "Olá, mundo", + result + .get() + .getFirstResult() + .getFirstTranslation() + .orElse(new Translation("en", "err")) + .getText()); + } + + @Test + void shouldHandleJsonBodyWithNoValidTranslations() throws JsonProcessingException { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + String json = + "[{\"detectedLanguage\":{\"language\":\"en\",\"score\":0.98},\"translations\":[]}]"; + RestRequestResult mockResponse = mock(RestRequestResult.class); + ObjectMapper mapper = new ObjectMapper(); + JsonNode mockJsonBody = mapper.readTree(json); + + when(mockResponse.getJsonBody()).thenReturn(mockJsonBody); + + Optional result = params.handleResponse(mockResponse); + + assertTrue(result.isPresent()); + assertNotNull(result.get()); + assertTrue(result.get().getFirstResult().getTranslations().isEmpty()); + } }