Skip to content

Commit

Permalink
added tests for the TranslationResponseMap Class
Browse files Browse the repository at this point in the history
* edited build.sbt file to include mockito inline mocks in ordre to mock the public static method MedicalController.translate
  • Loading branch information
Har150n committed May 30, 2024
1 parent 72bd02f commit dd2c5ed
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion app/femr/util/translation/TranslationResponseMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private void populateTranslation() {
}
}

private Map<String, Object> getResponseMap() {
public Map<String, Object> getResponseMap() {
return this.responseMap;
}

Expand Down
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);
}

}

0 comments on commit dd2c5ed

Please sign in to comment.