Skip to content

Commit

Permalink
test: add Params Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Feb 8, 2024
1 parent 79c3484 commit 3a0ae11
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<DetectResponse> 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<DetectResponse> result = params.handleResponse(mockResponse);

assertTrue(result.isPresent());
}
}
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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<TranslationResponse> 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<TranslationResponse> result = params.handleResponse(mockResponse);

assertTrue(result.isPresent());
assertNotNull(result.get());
assertThrows(IndexOutOfBoundsException.class, result.get()::getFirstResult);
}
}

0 comments on commit 3a0ae11

Please sign in to comment.