From 3a0ae1187e8a66935020645acaac057890bcc910 Mon Sep 17 00:00:00 2001 From: BrenoEpic Date: Thu, 8 Feb 2024 13:30:38 -0300 Subject: [PATCH] test: add Params Testing --- .../request/DetectLanguageParamsTest.java | 45 +++++++ .../data/request/TranslateParamsTest.java | 124 ++++++++++++++++++ 2 files changed, 169 insertions(+) diff --git a/src/test/java/io/github/brenoepics/at4j/data/request/DetectLanguageParamsTest.java b/src/test/java/io/github/brenoepics/at4j/data/request/DetectLanguageParamsTest.java index bb50a60..ceff230 100644 --- a/src/test/java/io/github/brenoepics/at4j/data/request/DetectLanguageParamsTest.java +++ b/src/test/java/io/github/brenoepics/at4j/data/request/DetectLanguageParamsTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.brenoepics.at4j.AzureApi; import io.github.brenoepics.at4j.core.AzureApiImpl; @@ -58,4 +59,48 @@ void handleResponse_returnsDetectResponseForValidJsonBody() throws IOException { assertTrue(result.isPresent()); assertEquals("en", result.get().getFirst().getLanguageCode()); } + + @Test + void shouldReturnCorrectTextsWhenMultipleTextsAreAdded() { + DetectLanguageParams params = new DetectLanguageParams("Hello"); + params.addText("Bonjour"); + assertEquals("Hello", params.getTexts().get(1)); + assertEquals("Bonjour", params.getTexts().get(2)); + } + + @Test + void shouldReturnNullBodyWhenNoTextsAreAdded() { + DetectLanguageParams params = new DetectLanguageParams(new ArrayList<>()); + assertNull(params.getBody()); + } + + @Test + void shouldHandleResponseWithEmptyJsonBody() { + DetectLanguageParams params = new DetectLanguageParams("Hello"); + RestRequestResult mockResponse = mock(RestRequestResult.class); + JsonNode mockJsonBody = mock(JsonNode.class); + + when(mockResponse.getJsonBody()).thenReturn(mockJsonBody); + when(mockJsonBody.isEmpty()).thenReturn(true); + + Optional result = params.handleResponse(mockResponse); + + assertFalse(result.isPresent()); + } + + @Test + void shouldHandleResponseWithValidJsonBody() { + DetectLanguageParams params = new DetectLanguageParams("Hello"); + RestRequestResult mockResponse = mock(RestRequestResult.class); + JsonNode mockJsonBody = mock(JsonNode.class); + JsonNode mockDetectionNode = mock(JsonNode.class); + + when(mockResponse.getJsonBody()).thenReturn(mockJsonBody); + when(mockJsonBody.get(0)).thenReturn(mockDetectionNode); + when(mockDetectionNode.has("language")).thenReturn(true); + + Optional result = params.handleResponse(mockResponse); + + assertTrue(result.isPresent()); + } } \ No newline at end of file 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 f678381..7a67078 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,14 +1,19 @@ package io.github.brenoepics.at4j.data.request; +import com.fasterxml.jackson.databind.JsonNode; 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; +import io.github.brenoepics.at4j.data.response.TranslationResponse; +import io.github.brenoepics.at4j.util.rest.RestRequestResult; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.*; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class TranslateParamsTest { @@ -97,4 +102,123 @@ void shouldGetQueryParameters() { assertEquals(expectedParams, params.getQueryParameters()); } + + @Test + void whenTextsAreSet_thenTextsShouldBeCorrectlyMapped() { + TranslateParams params = new TranslateParams(List.of("Hello", "Bonjour"), List.of("fr", "de")); + assertEquals("Hello", params.getTexts().get(1)); + assertEquals("Bonjour", params.getTexts().get(2)); + } + + @Test + void whenNoTextTypeIsSet_thenDefaultShouldBeNull() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + assertNull(params.getTextType()); + } + + @Test + void whenNoProfanityActionIsSet_thenDefaultShouldBeNull() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + assertNull(params.getProfanityAction()); + } + + @Test + void whenNoProfanityMarkerIsSet_thenDefaultShouldBeNull() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + assertNull(params.getProfanityMarker()); + } + + @Test + void whenNoIncludeAlignmentIsSet_thenDefaultShouldBeNull() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + assertNull(params.getIncludeAlignment()); + } + + @Test + void whenNoIncludeSentenceLengthIsSet_thenDefaultShouldBeNull() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + assertNull(params.getIncludeSentenceLength()); + } + + @Test + void whenNoSourceLanguageIsSet_thenDefaultShouldBeNull() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + assertNull(params.getSourceLanguage()); + } + + @Test + void whenNoTargetLanguagesAreSet_thenDefaultShouldBeNull() { + TranslateParams params = new TranslateParams("Hello", null); + assertNull(params.getTargetLanguages()); + } + + @Test + void whenNoSuggestedFromLanguageIsSet_thenDefaultShouldBeNull() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + assertNull(params.getSuggestedFromLanguage()); + } + + @Test + void whenQueryParametersAreEmpty_thenShouldReturnEmptyMap() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + assertTrue(params.getQueryParameters().isEmpty()); + } + + @Test + void shouldHandleResponseCorrectly() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + RestRequestResult mockResponse = mock(RestRequestResult.class); + JsonNode mockJsonBody = mock(JsonNode.class); + JsonNode mockTranslationNode = mock(JsonNode.class); + JsonNode mockDetectedLanguageNode = mock(JsonNode.class); + + when(mockResponse.getJsonBody()).thenReturn(mockJsonBody); + when(mockJsonBody.get(0)).thenReturn(mockTranslationNode); + when(mockTranslationNode.has("translations")).thenReturn(true); + when(mockTranslationNode.get("detectedLanguage")).thenReturn(mockDetectedLanguageNode); + + Assertions.assertThrows(NullPointerException.class, () -> params.handleResponse(mockResponse)); + } + + @Test + void shouldHandleNullResponse() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + RestRequestResult mockResponse = mock(RestRequestResult.class); + + when(mockResponse.getJsonBody()).thenReturn(null); + + Assertions.assertThrows(NullPointerException.class, () -> params.handleResponse(mockResponse)); + } + + @Test + void shouldHandleEmptyJsonBody() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + RestRequestResult mockResponse = mock(RestRequestResult.class); + JsonNode mockJsonBody = mock(JsonNode.class); + + when(mockResponse.getJsonBody()).thenReturn(mockJsonBody); + when(mockJsonBody.isEmpty()).thenReturn(true); + + Optional result = params.handleResponse(mockResponse); + + assertFalse(result.isPresent()); + } + + @Test + void shouldHandleJsonBodyWithoutTranslations() { + TranslateParams params = new TranslateParams("Hello", List.of("fr")); + RestRequestResult mockResponse = mock(RestRequestResult.class); + JsonNode mockJsonBody = mock(JsonNode.class); + JsonNode mockTranslationNode = mock(JsonNode.class); + + when(mockResponse.getJsonBody()).thenReturn(mockJsonBody); + when(mockJsonBody.get(0)).thenReturn(mockTranslationNode); + when(mockTranslationNode.has("translations")).thenReturn(false); + + Optional result = params.handleResponse(mockResponse); + + assertTrue(result.isPresent()); + assertNotNull(result.get()); + assertThrows(IndexOutOfBoundsException.class, result.get()::getFirstResult); + } }