Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/brenoepics/at4j
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Jan 16, 2024
2 parents 1dd5c6c + 754eb04 commit 85966c4
Showing 1 changed file with 51 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,54 @@

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());
}
}
@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());
}
}

0 comments on commit 85966c4

Please sign in to comment.