Skip to content

Commit

Permalink
FLAG-79: added unit test for sql flag evaluation class
Browse files Browse the repository at this point in the history
  • Loading branch information
ManojLL authored Aug 7, 2024
1 parent a530daa commit 0bc3c5c
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.patientflags.evaluator;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;
import org.openmrs.Cohort;
import org.openmrs.Patient;
import org.openmrs.api.APIException;
import org.openmrs.api.PatientService;
import org.openmrs.api.context.Context;
import org.openmrs.module.patientflags.Flag;
import org.openmrs.module.patientflags.FlagValidationResult;
import org.openmrs.module.patientflags.api.FlagService;
import org.openmrs.test.BaseModuleContextSensitiveTest;

import java.util.HashMap;
import java.util.Map;


public class SqlFlagEvaluatorTest extends BaseModuleContextSensitiveTest {

protected static final String XML_DATASET_PATH = "org/openmrs/module/patientflags/include/";

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

SQLFlagEvaluator sqlFlagEvaluator;

@Before
public void setup() throws Exception {
initializeInMemoryDatabase();
executeDataSet(TEST_DATASET_FILE);
authenticate();

sqlFlagEvaluator = new SQLFlagEvaluator();
}

@Test
public void eval_shouldEvaluateFLag() {
Flag flag = Context.getService(FlagService.class).getFlag(5);
Patient patient = Context.getService(PatientService.class).getPatient(2);
Map<Object, Object> context = new HashMap<>();

Boolean result = sqlFlagEvaluator.eval(flag, patient, context);
assertTrue(result);
}

@Test(expected = APIException.class)
public void eval_shouldThrowApiExceptionWhenPatientIsVoided() {
Flag flag = Context.getService(FlagService.class).getFlag(5);
Patient patient = Context.getService(PatientService.class).getPatient(1);
Map<Object, Object> context = new HashMap<>();

sqlFlagEvaluator.eval(flag, patient, context);
}

@Test
public void evalCohort_shouldReturnCohortWithNullCohortParameter() {
Flag flag = Context.getService(FlagService.class).getFlag(5);
Map<Object, Object> context = new HashMap<>();

Cohort resultCohort = sqlFlagEvaluator.evalCohort(flag, null, context);

assertNotNull(resultCohort);
assertFalse(resultCohort.isEmpty());
}

@Test
public void evalCohort_shouldReturnCohortWithCohortParameter() {
Flag flag = Context.getService(FlagService.class).getFlag(5);
Map<Object, Object> context = new HashMap<>();

Cohort cohort = new Cohort();
cohort.addMember(1);

Cohort resultCohort = sqlFlagEvaluator.evalCohort(flag, null, context);

assertNotNull(resultCohort);
assertFalse(resultCohort.isEmpty());
}

@Test(expected = APIException.class)
public void evalCohort_shouldThrowsException() {
Flag flag = Context.getService(FlagService.class).getFlag(6);
Map<Object, Object> context = new HashMap<>();

sqlFlagEvaluator.evalCohort(flag, null, context);
}

@Test
public void validate_shouldReturnFlagValidationResult() {
Flag flag = Context.getService(FlagService.class).getFlag(5);

FlagValidationResult result = sqlFlagEvaluator.validate(flag);
assertTrue(result.getResult());
}

@Test
public void validate_shouldReturnFalseResultWithMessage() {
Flag flag = Context.getService(FlagService.class).getFlag(2);

FlagValidationResult result = sqlFlagEvaluator.validate(flag);
assertFalse(result.getResult());
assertEquals("patientflags.errors.noPatientIdCriteria", result.getMessage());
}

@Test
public void validate_shouldReturnFalseResultWithLocalizedMessage() {
Flag flag = Context.getService(FlagService.class).getFlag(7);

FlagValidationResult result = sqlFlagEvaluator.validate(flag);
assertFalse(result.getResult());
}

@Test
public void evalMessage_ShouldReturnMessage() {
Flag flag = Context.getService(FlagService.class).getFlag(5);
Patient patient = Context.getService(PatientService.class).getPatient(2);

String message = sqlFlagEvaluator.evalMessage(flag, patient.getPatientId());

assertNotNull(message);
assertEquals(flag.getMessage(), message);
}

@Test(expected = APIException.class)
public void evalMessage_ShouldThrowsAPIExceptionForVoidedPatient() {
Flag flag = Context.getService(FlagService.class).getFlag(1);
Patient patient = Context.getService(PatientService.class).getPatient(1);

sqlFlagEvaluator.evalMessage(flag, patient.getPatientId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
<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"/>

<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"/>
<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"/>

<allergy allergy_id="1" patient_id="2" severity_concept_id="1" coded_allergen="1" allergen_type="DRUG" creator="1" date_created="2007-05-01 00:00:00.0" voided="false" uuid="7d87924e-e8df-4553-a956-95de80529735"/>

<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="The ${12} and ${34} message" 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"/>
<patientflags_flag flag_id="5" name="DRUG FLAG" evaluator="org.openmrs.module.patientflags.evaluator.SQLFlagEvaluator" criteria="SELECT a.patient_id FROM allergy a where a.allergen_type = 'DRUG';" 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="6" name="Invalid DRUG FLAG" evaluator="org.openmrs.module.patientflags.evaluator.SQLFlagEvaluator" criteria="SELECT a.patient_id FROM allergy a whe a.allergepe = 'DRUG';" 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="7" name="SQL Sample" evaluator="org.openmrs.module.patientflags.evaluator.SQLFlagEvaluator" criteria="select e.patient_Uuid 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="8" name="Groovy Sample" evaluator="org.openmrs.module.patientflags.evaluator.GroovyFlagEvaluator" criteria="result = new Cohort(); result.addMember(2); return org.openmrs.Cohort.union(result, testCohort); " message="Test" enabled="1" creator="1" date_created="2005-09-22 00:00:00.0" retired="false" uuid="da7f524f-27ce-4bb2-86d6-6d1d05312bd5"/>
</dataset>

</dataset>

0 comments on commit 0bc3c5c

Please sign in to comment.