-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f858e98
commit 68ab4cc
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/test/java/com/github/brenoepics/at4j/data/DetectedLanguageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |