Skip to content

Instantly share code, notes, and snippets.

@kaweesi
Created February 20, 2019 13:12
Show Gist options
  • Save kaweesi/8ded7bd4c567cb41aa8e91dd258d1eb1 to your computer and use it in GitHub Desktop.
Save kaweesi/8ded7bd4c567cb41aa8e91dd258d1eb1 to your computer and use it in GitHub Desktop.
package org.openmrs.module.eptsreports.reporting.unit.utils;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Spy;
import org.openmrs.Encounter;
import org.openmrs.Obs;
import org.openmrs.api.APIException;
import org.openmrs.calculation.patient.PatientCalculationContext;
import org.openmrs.calculation.patient.PatientCalculationService;
import org.openmrs.calculation.patient.PatientCalculationServiceImpl;
import org.openmrs.calculation.result.CalculationResultMap;
import org.openmrs.calculation.result.ListResult;
import org.openmrs.calculation.result.ObsResult;
import org.openmrs.calculation.result.SimpleResult;
import org.openmrs.module.eptsreports.reporting.calculation.BooleanResult;
import org.openmrs.module.eptsreports.reporting.cohort.definition.JembiObsDefinition;
import org.openmrs.module.eptsreports.reporting.helper.TestsHelper;
import org.openmrs.module.eptsreports.reporting.utils.EptsCalculationUtils;
import org.openmrs.module.reporting.data.encounter.definition.EncounterDatetimeDataDefinition;
import org.openmrs.module.reporting.data.patient.EvaluatedPatientData;
import org.openmrs.module.reporting.data.patient.definition.PatientDataDefinition;
import org.openmrs.module.reporting.data.patient.service.PatientDataService;
import org.openmrs.module.reporting.data.person.EvaluatedPersonData;
import org.openmrs.module.reporting.data.person.definition.ObsForPersonDataDefinition;
import org.openmrs.module.reporting.data.person.definition.PersonDataDefinition;
import org.openmrs.module.reporting.data.person.service.PersonDataService;
import org.openmrs.module.reporting.evaluation.EvaluationContext;
import org.openmrs.module.reporting.evaluation.EvaluationException;
import org.openmrs.test.BaseContextMockTest;
public class EptsCalculationUtilsTest extends BaseContextMockTest {
@Mock private PatientCalculationService service;
@Spy private TestsHelper testsHelper;
@Mock private PatientDataService patientDataService;
@Mock private PersonDataService personDataService;
private PatientCalculationContext calculationContext;
@Before
public void init() {
when(service.createCalculationContext())
.thenReturn(new PatientCalculationServiceImpl().new SimplePatientCalculationContext());
calculationContext = service.createCalculationContext();
calculationContext.setNow(testsHelper.getDate("2019-05-30 00:00:00.0"));
}
@Test
public void evaluateWithReportingShouldRightlyEvaluatePatientAndPersonData()
throws EvaluationException {
EvaluatedPatientData patientData = new EvaluatedPatientData();
EvaluatedPersonData personData = new EvaluatedPersonData();
Map<Integer, Object> data = new HashMap<Integer, Object>();
Obs obs = new Obs(1);
// represents any other object
Encounter encounter = new Encounter(1);
List<? extends Object> list = Arrays.asList("hi", "there", 4);
data.put(1, "");
data.put(2, obs);
data.put(3, false);
data.put(4, true);
data.put(5, encounter);
data.put(6, null);
data.put(7, list);
data.put(8, new Object());
patientData.setData(data);
personData.setData(data);
// evaluating patient data definition
when(patientDataService.evaluate(
any(PatientDataDefinition.class), any(EvaluationContext.class)))
.thenReturn(patientData);
CalculationResultMap patientResult =
EptsCalculationUtils.evaluateWithReporting(
new JembiObsDefinition(),
Arrays.asList(1, 2, 3, 4, 5, 6, 7),
null,
null,
calculationContext);
Assert.assertNotNull(patientResult);
evaluationTest(obs, encounter, list, patientResult);
// evaluating persondata defition
when(personDataService.evaluate(any(PersonDataDefinition.class), any(EvaluationContext.class)))
.thenReturn(personData);
CalculationResultMap personResult =
EptsCalculationUtils.evaluateWithReporting(
new ObsForPersonDataDefinition(),
Arrays.asList(1, 2, 3, 4, 5, 6, 7),
null,
null,
calculationContext);
Assert.assertNotNull(personResult);
evaluationTest(obs, encounter, list, personResult);
}
private void evaluationTest(
Obs obs, Encounter encounter, List<? extends Object> list, CalculationResultMap result) {
Assert.assertEquals("", result.get(1).getValue());
Assert.assertTrue(result.get(1) instanceof SimpleResult);
Assert.assertEquals(obs, result.get(2).getValue());
Assert.assertTrue(result.get(2) instanceof ObsResult);
Assert.assertFalse((boolean) result.get(3).getValue());
Assert.assertTrue(result.get(3) instanceof BooleanResult);
Assert.assertTrue((boolean) result.get(4).getValue());
Assert.assertTrue(result.get(4) instanceof BooleanResult);
Assert.assertEquals(encounter, result.get(5).getValue());
Assert.assertTrue(result.get(5) instanceof SimpleResult);
Assert.assertNull(result.get(6));
Assert.assertEquals(list.toString(), result.get(7).getValue().toString());
Assert.assertTrue(result.get(7) instanceof ListResult);
// should not include any objects outside the defined cohort
Assert.assertFalse(result.containsKey(8));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment