Skip to content

Commit

Permalink
FLAG 78 : Add unit test for patient flags
Browse files Browse the repository at this point in the history
FLAG 78 : updated the patient flag unit test

FLAG 78 : updated the patient flag unit test

FLAG 78 : added authentication in flagService test

FLAG 78 : update test dataset

FLAG 78: remove comment

FLAG 78: added static imports

FLAG 78: remove unwanted imports
  • Loading branch information
ManojLL authored Jun 30, 2024
1 parent e969c97 commit 2f31624
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 94 deletions.
207 changes: 134 additions & 73 deletions api/src/test/java/org/openmrs/module/patientflags/FlagServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package org.openmrs.module.patientflags;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.Assert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.junit.Before;
import org.junit.Test;
import org.openmrs.Cohort;
import org.openmrs.Patient;
import org.openmrs.api.PatientService;
import org.openmrs.api.context.Context;
import org.openmrs.module.patientflags.Flag;
import org.openmrs.module.patientflags.Priority;
import org.openmrs.module.patientflags.Tag;

import org.openmrs.module.patientflags.api.FlagService;
import org.openmrs.module.patientflags.filter.Filter;
import org.openmrs.test.BaseModuleContextSensitiveTest;


Expand All @@ -20,129 +26,184 @@
*/
public class FlagServiceTest extends BaseModuleContextSensitiveTest {

// TODO: create some actual methods that test the flagging of patients?
protected static final String XML_DATASET_PATH = "org/openmrs/module/patientflags/include/";

private static final String TEST_DATASET_FILE = XML_DATASET_PATH + "patientflagtest-dataset.xml";

private FlagService flagService;

/**
* Tests of the Flags
*/

@Before
public void initTestData() throws Exception {
initializeInMemoryDatabase();
executeDataSet(TEST_DATASET_FILE);
flagService = Context.getService(FlagService.class);
authenticate();
}

/**
* Tests of the generateFlagsForPatient(Patient patient) method
*/
@Test
public void generateFlagsForPatient_shouldAcceptNullParameter() throws Exception {
Context.getService(FlagService.class).generateFlagsForPatient(new Patient(), null);
public void generateFlagsForPatient_shouldAcceptNullParameter() {
Patient patient = Context.getService(PatientService.class).getPatient(2);
List<Flag> flags = flagService.generateFlagsForPatient(patient, null);
assertFalse(flags.isEmpty());
}

@Test
public void generateFlagsForPatient_shouldReturnListOfFlags() {
Map<Object, Object> context = new HashMap<>();
Patient patient = Context.getService(PatientService.class).getPatient(2);
List<Flag> flags = flagService.generateFlagsForPatient(patient, context);
assertFalse(flags.isEmpty());
}

/**
* Tests of the getFlaggedPatients(Flag flag) method
*/
@Test
public void getFlaggedPatients_shouldAcceptNullFlagParameter() throws Exception {
Context.getService(FlagService.class).getFlaggedPatients(new Flag(), null);
public void getFlaggedPatients_shouldAcceptNullFlagParameter() {
Flag flag = flagService.getFlag(1);
Cohort cohort = flagService.getFlaggedPatients(flag, null);
assertFalse(cohort.isEmpty());
}

@Test
public void getFlaggedPatients_shouldReturnListOfFlags() {
Map<Object, Object> context = new HashMap<>();
Flag flag = flagService.getFlag(1);
Cohort cohort = flagService.getFlaggedPatients(flag, context);
assertFalse(cohort.isEmpty());
}

/**
* Tests of the getFlaggedPatients(List<Flag> flags) method
*/
@Test
public void getFlaggedPatients_shouldAcceptNullFlagListParameter() throws Exception {
Context.getService(FlagService.class).getFlaggedPatients(new ArrayList<Flag>(), null);
public void getFlaggedPatients_shouldAcceptNullFlagListParameter() {
List<Flag> flags = flagService.getAllFlags();
Cohort cohort = flagService.getFlaggedPatients(flags, null);
assertFalse(cohort.isEmpty());
}

@Test
public void getFlaggedPatients_shouldReturnListOfFlagsListParameter() {
Map<Object, Object> context = new HashMap<>();
List<Flag> flags = flagService.getAllFlags();
Cohort cohort = flagService.getFlaggedPatients(flags, context);
assertFalse(cohort.isEmpty());
}


/**
* Tests of methods the save and retrieve flag prioritiess
*/
@Test
public void savePriority_shouldSaveNewPriority() throws Exception {
Priority priority = new Priority("test", "test", 1);
Context.getService(FlagService.class).savePriority(priority);
public void savePriority_shouldSaveNewPriority() {
Priority priority = new Priority("High", "style='background-color:red'", 1);
flagService.savePriority(priority);

// get all the priorities
List<Priority> priorities = Context.getService(FlagService.class).getAllPriorities();
// since there is no test data loaded, the first (and only) priorites should be the "test" one we just added
Assert.assertEquals("test", priorities.get(0).getName());
// get newly saved priority by it's name
Priority savedPriority = flagService.getPriorityByName("High");
assertNotNull(savedPriority);
assertEquals(priority.getName(), savedPriority.getName());
}

/**
* Tests of methods that save and retrieve flags
*/
@Test
public void saveFlag_shouldSaveNewFlag() throws Exception{
// insert a "test" flag
Context.getService(FlagService.class).saveFlag(createTestFlag());
public void saveFlag_shouldSaveNewFlag() {
Flag flag = createTestFlag();
flagService.saveFlag(flag);

// get all flags
List<Flag> flags = Context.getService(FlagService.class).getAllFlags();
// since there is no test data loaded, the first (and only) flag should be the "test" one we just added
Assert.assertEquals("test", flags.get(0).getName());
// get newly saved flag
Flag savedFlag = flagService.getFlagByName("test");
assertEquals(flag.getName(), savedFlag.getName());
}

@Test
public void saveFlag_shouldUpdateFlag() throws Exception {
// insert a "test" flag
Context.getService(FlagService.class).saveFlag(createTestFlag());

// get all flags
List<Flag> flags = Context.getService(FlagService.class).getAllFlags();

// since there is no test data loaded, the first (and only) flag should be the "test" one we just added
// lets try changing the name of this flag
flags.get(0).setName("testagain");
public void saveFlag_shouldUpdateFlag() {
Flag flag = flagService.getFlag(1);
flag.setName("Drug allergy");

// save this updated flag
Context.getService(FlagService.class).saveFlag(flags.get(0));
flagService.saveFlag(flag);

// get all flags again
flags = Context.getService(FlagService.class).getAllFlags();
// get flag again
Flag updatedFlag = flagService.getFlag(1);
// confirm that the name has been changed again
Assert.assertEquals("testagain", flags.get(0).getName());
assertEquals("Drug allergy", updatedFlag.getName());
}

@Test
public void getFlag_shouldGetFlag() throws Exception{
// create our test flag again
Context.getService(FlagService.class).saveFlag(createTestFlag());
public void getAllFlags_shouldGetAllFlags() {
List<Flag> flags = flagService.getAllFlags();
assertFalse(flags.isEmpty());
}

// fetch it again
List<Flag> flags = Context.getService(FlagService.class).getAllFlags();
@Test
public void removeFlag_shouldRemoveFlag() {
Flag flag = flagService.getFlag(2);
flagService.purgeFlag(flag.getFlagId());

// now, see if we change fetch the same flag by it's id
Flag flag = Context.getService(FlagService.class).getFlag(flags.get(0).getFlagId());
Assert.assertEquals("test", flag.getName());
// fetch flags again
Flag removedFlag = flagService.getFlag(2);

// when fetching a removed flag it should return null object
assertNull(removedFlag);
}

@Test
public void removeFlag_shouldRemoveFlag() throws Exception{
// create our test flag again
Context.getService(FlagService.class).saveFlag(createTestFlag());
public void getFlagsByFilter_shouldGetAllFlagsForNullFilter() {
List<Flag> flags = flagService.getFlagsByFilter(null);
List<Flag> allFlags = flagService.getAllFlags();
assertEquals(flags.size(), allFlags.size());
}

// fetch it
List<Flag> flags = Context.getService(FlagService.class).getAllFlags();

// now, lets try to remove the flag we just created
Context.getService(FlagService.class).purgeFlag(flags.get(0).getFlagId());

// fetch flags again
flags = Context.getService(FlagService.class).getAllFlags();

// the list should be empty, as we have deleted the only flag we created
Assert.assertEquals(0, flags.size());
@Test
public void getFlagsByFilter_shouldGetFilteredFlags() {
Filter filter = new Filter();
List<Flag> flags = flagService.getFlagsByFilter(filter);
assertFalse(flags.isEmpty());
}


@Test
public void getFlag_shouldReturnFlagById() {
Integer flagId = 1;
Flag flag = flagService.getFlag(flagId);
assertNotNull(flag);
assertEquals(flagId, flag.getFlagId());
}

@Test
public void getFlagByUuid_shouldReturnFlagByUuid() {
String uuid = "da7f524f-27ce-4bb2-86d6-6d1d05312bd5";
Flag flag = flagService.getFlagByUuid(uuid);
assertNotNull(flag);
assertEquals(uuid, flag.getUuid());
}

/**
* Tests of methods that save and retrieve tags
*/
@Test
public void addOrUpdateTag_shouldSaveNewTag() throws Exception{
public void addOrUpdateTag_shouldSaveNewTag() {
// insert a "test" tag
Context.getService(FlagService.class).saveTag(new Tag("test"));

// get all tags
List<Tag> tags = Context.getService(FlagService.class).getAllTags();
// since there is no test data loaded, the first (and only) tag should be the "test" one we just added
Assert.assertEquals("test",tags.get(0).getName());
assertEquals("test",tags.get(0).getName());
}

@Test
public void addOrUpdateTag_shouldUpdateTag() throws Exception{
public void addOrUpdateTag_shouldUpdateTag() {
// insert a "test" tag
Context.getService(FlagService.class).saveTag(new Tag("test"));

Expand All @@ -159,11 +220,11 @@ public void addOrUpdateTag_shouldUpdateTag() throws Exception{
// get all tags
tags = Context.getService(FlagService.class).getAllTags();
// confirm that the name has been changed again
Assert.assertEquals("testagain", tags.get(0).getName());
assertEquals("testagain", tags.get(0).getName());
}

@Test
public void getTag_shouldGetTag() throws Exception{
public void getTag_shouldGetTag() {
// insert a "test" tag
Context.getService(FlagService.class).saveTag(new Tag("test"));

Expand All @@ -172,11 +233,11 @@ public void getTag_shouldGetTag() throws Exception{

// now, see if we can fetch the same tag by it's id
Tag tag = Context.getService(FlagService.class).getTag(tags.get(0).getTagId());
Assert.assertEquals("test", tag.getName());
assertEquals("test", tag.getName());
}

@Test
public void removeTag_shouldRemoveTag() throws Exception{
public void removeTag_shouldRemoveTag() {
// insert a "test" tag
Context.getService(FlagService.class).saveTag(new Tag("test"));

Expand All @@ -190,7 +251,7 @@ public void removeTag_shouldRemoveTag() throws Exception{
tags = Context.getService(FlagService.class).getAllTags();

// the list should be empty, as we have deleted the only flag we created
Assert.assertEquals(0, tags.size());
assertEquals(0, tags.size());
}

/*
Expand All @@ -200,7 +261,7 @@ public void removeTag_shouldRemoveTag() throws Exception{
// TODO: add tests of actual filters here

@Test
public void getFlagsByFilter_shouldAcceptNullParameter() throws Exception{
public void getFlagsByFilter_shouldAcceptNullParameter() {
Context.getService(FlagService.class).getFlagsByFilter(null);
}

Expand All @@ -211,7 +272,7 @@ public void getFlagsByFilter_shouldAcceptNullParameter() throws Exception{
private Flag createTestFlag() {
// create & save priority to use
Priority priority = new Priority("test", "test", 1);
Context.getService(FlagService.class).savePriority(priority);
flagService.savePriority(priority);

// now create the flag
Flag flag = new Flag("test", "test", "test");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<patient patient_id="2" creator="1" date_created="2005-09-22 00:00:00.0" changed_by="1" date_changed="2008-08-18 12:29:59.0" voided="false"/>
<person person_id="2" gender="M" birthdate="1975-04-08 00:00:00.0" dead="false" creator="1" date_created="2005-09-22 00:00:00.0" voided="false" uuid="da7f524f-27ce-4bb2-86d6-6d1d05312bd5"/>
<person person_id="2" gender="M" birthdate="1975-04-08 00:00:00.0" dead="false" creator="1" date_created="2005-09-22 00:00:00.0" voided="false" uuid="da7f524f-27ce-4bb2-86d6-6d1d05312bd5"/>

<!-- concepts taken from LogicBasicTest.concepts.xml -->
<concept concept_id="1" retired="false" datatype_id="1" class_id="1" is_set="false" creator="1" date_created="2005-01-01 00:00:00.0" uuid="5c43a33f-82c9-427d-92cd-02b3e0e3e1f9"/>
<concept concept_id="2" retired="false" datatype_id="2" class_id="2" is_set="false" creator="1" date_created="2005-01-01 00:00:00.0" uuid="0362ad75-bf39-4958-bcd6-4ea5c0de476b"/>
<concept concept_id="3" retired="false" datatype_id="1" class_id="1" is_set="false" creator="1" date_created="2005-01-01 00:00:00.0" uuid="bc28d511-51d6-4e57-ab82-ec047e702a2d"/>
<concept_class concept_class_id="1" name="Finding" description="A medical discovery" creator="1" date_created="2005-01-01 00:00:00.0" retired="false" uuid="8ffbbe0d-b5b4-4a50-959f-6b80e0b8e272"/>
<concept_class concept_class_id="2" name="Question" description="Question on a form" creator="1" date_created="2005-01-01 00:00:00.0" retired="false" uuid="7f23a539-2a40-44c6-8295-2755f9adea13"/>
<concept_datatype concept_datatype_id="1" name="Numeric" hl7_abbreviation="NM" description="Numeric value, including integer or float" creator="1" date_created="2005-01-01 00:00:00.0" retired="false" uuid="e0787956-0b2f-4ba6-b17c-4dff2064f9fc"/>
<concept_datatype concept_datatype_id="2" name="Coded" hl7_abbreviation="CWE" description="Coded answer" creator="1" date_created="2005-01-01 00:00:00.0" retired="false" uuid="fd66ac86-80e1-4cb8-8a58-36cd677a7522"/>
<concept_description concept_description_id="1" concept_id="1" description="# of cd4 cells in the body" locale="en" creator="1" date_created="2005-01-01 00:00:00.0" uuid="24045af5-e3a5-4096-a009-cfd49978585e"/>
<concept_description concept_description_id="2" concept_id="2" description="Active meds" locale="en" creator="1" date_created="2005-01-01 00:00:00.0" uuid="b1345fb4-f9ec-4801-9f92-bbf75e8a11d4"/>
<concept_description concept_description_id="3" concept_id="3" description="mass" locale="en" creator="1" date_created="2005-01-01 00:00:00.0" uuid="61c5cdc6-f5ef-48af-8c90-c8f807001ee9"/>
<concept_name concept_id="1" locale_preferred="true" name="HEMOGLOBIN" locale="en" creator="1" date_created="2005-01-01 00:00:00.0" concept_name_id="1" voided="false" uuid="d4784150-3fdd-4e7f-a41e-7eb4ec8bd6fd"/>
<concept_name concept_id="2" locale_preferred="true" name="CURRENT ANTIRETROVIRAL DRUGS USED FOR TREATMENT" locale="en" creator="1" date_created="2005-01-01 00:00:00.0" concept_name_id="2" voided="false" uuid="ceb058e2-f636-47ea-acd0-b6906c7f4401"/>
<concept_name concept_id="3" locale_preferred="true" name="WEIGHT (KG)" locale="en" creator="1" date_created="2005-01-01 00:00:00.0" concept_name_id="3" voided="false" uuid="0586cb3c-9115-4e77-8329-f5455e1c641e"/>
<concept_name_tag concept_name_tag_id="4" tag="preferred" description="preferred name in a language" creator="1" date_created="2007-05-01 00:00:00.0" voided="false" uuid="54d67a06-62d9-426b-a2e5-e3cccbff10f9"/>
<concept_name_tag_map concept_name_id="1" concept_name_tag_id="4"/>
<concept_name_tag_map concept_name_id="2" concept_name_tag_id="4"/>
<concept_name_tag_map concept_name_id="3" concept_name_tag_id="4"/>

<patientflags_flag flag_id="1" name="SQL Sample" evaluator="org.openmrs.module.patientflags.evaluator.SQLFlagEvaluator" criteria="select e.patient_id from encounter e where e.encounter_datetime > now()" message="Test" enabled="1" creator="1" date_created="2005-09-22 00:00:00.0" retired="false" uuid="da7f524f-27ce-4bb2-86d6-6d1d05312bd5"/>
<patientflags_flag flag_id="2" name="Invalid SQL Sample" evaluator="org.openmrs.module.patientflags.evaluator.SQLFlagEvaluator" criteria="select * from encounter e where e.encounter_datetime > now()" message="Test" enabled="1" creator="1" date_created="2005-09-22 00:00:00.0" retired="false" uuid="da7f524f-27ce-4bb2-86d6-6d1d05312bd5"/>
<patientflags_flag flag_id="3" name="Groovy Sample" evaluator="org.openmrs.module.patientflags.evaluator.GroovyFlagEvaluator" criteria="return null;" message="Test" enabled="1" creator="1" date_created="2005-09-22 00:00:00.0" retired="false" uuid="da7f524f-27ce-4bb2-86d6-6d1d05312bd5"/>
<patientflags_flag flag_id="4" name="Invalid Groovy Sample" evaluator="org.openmrs.module.patientflags.evaluator.GroovyFlagEvaluator" criteria="bogus" message="Test" enabled="1" creator="1" date_created="2005-09-22 00:00:00.0" retired="false" uuid="da7f524f-27ce-4bb2-86d6-6d1d05312bd5"/>

</dataset>
Loading

0 comments on commit 2f31624

Please sign in to comment.