-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tests for the TranslationResponseMap Class
* edited build.sbt file to include mockito inline mocks in ordre to mock the public static method MedicalController.translate
- Loading branch information
Showing
3 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
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
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
87 changes: 87 additions & 0 deletions
87
test/unit/app/femr/business/services/TranslationResponseMapTest.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,87 @@ | ||
package unit.app.femr.business.services; | ||
|
||
import femr.ui.controllers.MedicalController; | ||
import femr.util.translation.TranslationResponseMap; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import play.libs.Json; | ||
import java.util.Map; | ||
import static org.junit.Assert.*; | ||
|
||
public class TranslationResponseMapTest { | ||
|
||
private TranslationResponseMap translationResponseMap; | ||
private MockedStatic<MedicalController> medicalControllerMock; | ||
|
||
@Before | ||
public void setUp() { | ||
// Initialize the mock for the static method | ||
medicalControllerMock = Mockito.mockStatic(MedicalController.class); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
// Close the static mock after the tests | ||
medicalControllerMock.close(); | ||
} | ||
|
||
@Test | ||
public void testSameLanguage_noTranslation() { | ||
String fromLanguage = "en"; | ||
String toLanguage = "en"; | ||
String text = "Hello"; | ||
|
||
translationResponseMap = new TranslationResponseMap(fromLanguage, toLanguage, text); | ||
|
||
Map<String, Object> responseMap = translationResponseMap.getResponseMap(); | ||
JsonNode responseJson = translationResponseMap.getResponseJson(); | ||
|
||
assertEquals("SameToSame", responseMap.get("translation")); | ||
assertEquals(Json.toJson(responseMap), responseJson); | ||
} | ||
|
||
@Test | ||
public void testDifferentLanguages_translationOccurs() { | ||
String fromLanguage = "en"; | ||
String toLanguage = "es"; | ||
String text = "Hello"; | ||
String expectedTranslation = "Hola"; | ||
|
||
// mock the static translate method output | ||
medicalControllerMock.when(() -> MedicalController.translate(text, fromLanguage, toLanguage)).thenReturn(expectedTranslation); | ||
|
||
translationResponseMap = new TranslationResponseMap(fromLanguage, toLanguage, text); | ||
|
||
Map<String, Object> responseMap = translationResponseMap.getResponseMap(); | ||
JsonNode responseJson = translationResponseMap.getResponseJson(); | ||
|
||
assertEquals(expectedTranslation, responseMap.get("translation")); | ||
assertEquals(Json.toJson(responseMap), responseJson); | ||
} | ||
|
||
@Test | ||
public void testRtlLanguages() { | ||
String fromLanguage = "en"; | ||
String toLanguage = "ar"; | ||
String text = "Hello"; | ||
String expectedTranslation = "مرحبا"; | ||
|
||
// mock the static translate method output | ||
medicalControllerMock.when(() -> MedicalController.translate(text, fromLanguage, toLanguage)).thenReturn(expectedTranslation); | ||
|
||
translationResponseMap = new TranslationResponseMap(fromLanguage, toLanguage, text); | ||
|
||
Map<String, Object> responseMap = translationResponseMap.getResponseMap(); | ||
JsonNode responseJson = translationResponseMap.getResponseJson(); | ||
|
||
assertTrue((Boolean) responseMap.get("toLanguageIsRtl")); // ar isRtl is True | ||
assertFalse((Boolean) responseMap.get("fromLanguageIsRtl")); // en isRtl is False | ||
assertEquals(expectedTranslation, responseMap.get("translation")); | ||
assertEquals(Json.toJson(responseMap), responseJson); | ||
} | ||
|
||
} |