From 0bc3c5cd4f6b841392bef66c67a6c176b43c1229 Mon Sep 17 00:00:00 2001 From: Manoj Lakshan <48247516+ManojLL@users.noreply.github.com> Date: Wed, 7 Aug 2024 13:03:03 +0530 Subject: [PATCH] FLAG-79: added unit test for sql flag evaluation class --- .../evaluator/SqlFlagEvaluatorTest.java | 149 ++++++++++++++++++ .../patientflags/include/flagtest-dataset.xml | 12 +- 2 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 api/src/test/java/org/openmrs/module/patientflags/evaluator/SqlFlagEvaluatorTest.java diff --git a/api/src/test/java/org/openmrs/module/patientflags/evaluator/SqlFlagEvaluatorTest.java b/api/src/test/java/org/openmrs/module/patientflags/evaluator/SqlFlagEvaluatorTest.java new file mode 100644 index 0000000..fcf902e --- /dev/null +++ b/api/src/test/java/org/openmrs/module/patientflags/evaluator/SqlFlagEvaluatorTest.java @@ -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 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 context = new HashMap<>(); + + sqlFlagEvaluator.eval(flag, patient, context); + } + + @Test + public void evalCohort_shouldReturnCohortWithNullCohortParameter() { + Flag flag = Context.getService(FlagService.class).getFlag(5); + Map 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 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 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()); + } +} diff --git a/api/src/test/resources/org/openmrs/module/patientflags/include/flagtest-dataset.xml b/api/src/test/resources/org/openmrs/module/patientflags/include/flagtest-dataset.xml index 68b8eae..1dbb833 100644 --- a/api/src/test/resources/org/openmrs/module/patientflags/include/flagtest-dataset.xml +++ b/api/src/test/resources/org/openmrs/module/patientflags/include/flagtest-dataset.xml @@ -4,9 +4,17 @@ - + + + + + + + + - \ No newline at end of file + +