From 68ab4cc654d66f37f12a7bd9b6a4f0191826b5ef Mon Sep 17 00:00:00 2001 From: BrenoEpic Date: Mon, 15 Jan 2024 23:56:42 -0300 Subject: [PATCH] Create DetectedLanguageTest.java --- .../at4j/data/DetectedLanguageTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/test/java/com/github/brenoepics/at4j/data/DetectedLanguageTest.java diff --git a/src/test/java/com/github/brenoepics/at4j/data/DetectedLanguageTest.java b/src/test/java/com/github/brenoepics/at4j/data/DetectedLanguageTest.java new file mode 100644 index 0000000..68fa541 --- /dev/null +++ b/src/test/java/com/github/brenoepics/at4j/data/DetectedLanguageTest.java @@ -0,0 +1,58 @@ +package com.github.brenoepics.at4j.data; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class DetectedLanguageTest { + + @Test + void ofJSON_withValidJson_returnsDetectedLanguage() throws JsonProcessingException { + String json = "{\"language\":\"en\",\"score\":0.9,\"isTranslationSupported\":true,\"isTransliterationSupported\":false}"; + ObjectMapper mapper = new ObjectMapper(); + ObjectNode jsonNode = mapper.convertValue(mapper.readTree(json), ObjectNode.class); + + DetectedLanguage detectedLanguage = DetectedLanguage.ofJSON(jsonNode); + + assertEquals("en", detectedLanguage.getLanguageCode()); + assertEquals(0.9, detectedLanguage.getScore(), 0.0001); + assertTrue(detectedLanguage.isTranslationSupported()); + assertFalse(detectedLanguage.isTransliterationSupported()); + } + + @Test + void ofJSON_withMissingFields_returnsDetectedLanguageWithDefaults() throws JsonProcessingException { + String json = "{\"language\":\"en\",\"score\":0.9}"; + ObjectMapper mapper = new ObjectMapper(); + ObjectNode jsonNode = mapper.convertValue(mapper.readTree(json), ObjectNode.class); + + DetectedLanguage detectedLanguage = DetectedLanguage.ofJSON(jsonNode); + + assertEquals("en", detectedLanguage.getLanguageCode()); + assertEquals(0.9, detectedLanguage.getScore(), 0.0001); + assertFalse(detectedLanguage.isTranslationSupported()); + assertFalse(detectedLanguage.isTransliterationSupported()); + } + + @Test + void ofJSON_withInvalidJson_returnsNull() throws JsonProcessingException { + String json = "{\"invalid\":\"json\"}"; + ObjectMapper mapper = new ObjectMapper(); + ObjectNode jsonNode = mapper.convertValue(mapper.readTree(json), ObjectNode.class); + + DetectedLanguage detectedLanguage = DetectedLanguage.ofJSON(jsonNode); + + assertNull(detectedLanguage); + } + + @Test + void toString_returnsCorrectFormat() { + DetectedLanguage detectedLanguage = new DetectedLanguage("en", 0.9f, true, false); + + String expected = "DetectedLanguage{language='en', score=0.9, isTranslationSupported=true, isTransliterationSupported=false}"; + assertEquals(expected, detectedLanguage.toString()); + } +} \ No newline at end of file