diff --git a/Build.sbt b/Build.sbt index 7a5fa30cd..4d4b0ccc2 100644 --- a/Build.sbt +++ b/Build.sbt @@ -21,7 +21,7 @@ val appDependencies = Seq( "org.mindrot" % "jbcrypt" % "0.3m", "org.apache.commons" % "commons-collections4" % "4.0", "org.apache.commons" % "commons-text" % "1.3", - "org.mockito" % "mockito-core" % "3.5.13", + "org.mockito" % "mockito-inline" % "3.5.13", "com.google.code.gson" % "gson" % "2.3.1", "com.itextpdf" % "itextpdf" % "5.5.6", "com.itextpdf.tool" % "xmlworker" % "5.5.6", diff --git a/app/femr/util/translation/TranslationResponseMap.java b/app/femr/util/translation/TranslationResponseMap.java index b8fd4fc8f..e8021f45e 100644 --- a/app/femr/util/translation/TranslationResponseMap.java +++ b/app/femr/util/translation/TranslationResponseMap.java @@ -45,7 +45,7 @@ private void populateTranslation() { } } - private Map getResponseMap() { + public Map getResponseMap() { return this.responseMap; } diff --git a/test/unit/app/femr/business/services/TranslationResponseMapTest.java b/test/unit/app/femr/business/services/TranslationResponseMapTest.java new file mode 100644 index 000000000..19efe699a --- /dev/null +++ b/test/unit/app/femr/business/services/TranslationResponseMapTest.java @@ -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 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 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 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 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); + } + +}