|
| 1 | +package org.openmrs.module.lamp; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNull; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | +import static org.mockito.Mockito.*; |
| 8 | + |
| 9 | +import java.util.Date; |
| 10 | + |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | +import org.mockito.Mock; |
| 15 | +import org.openmrs.Concept; |
| 16 | +import org.openmrs.Encounter; |
| 17 | +import org.openmrs.EncounterType; |
| 18 | +import org.openmrs.Location; |
| 19 | +import org.openmrs.Patient; |
| 20 | +import org.openmrs.PatientProgram; |
| 21 | +import org.openmrs.Program; |
| 22 | +import org.openmrs.ProgramWorkflow; |
| 23 | +import org.openmrs.ProgramWorkflowState; |
| 24 | +import org.openmrs.User; |
| 25 | +import org.openmrs.api.ConceptService; |
| 26 | +import org.openmrs.api.ProgramWorkflowService; |
| 27 | +import org.openmrs.api.context.Context; |
| 28 | +import org.powermock.api.mockito.PowerMockito; |
| 29 | +import org.powermock.core.classloader.annotations.PowerMockIgnore; |
| 30 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 31 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 32 | + |
| 33 | +@RunWith(PowerMockRunner.class) |
| 34 | +@PrepareForTest({ Context.class, Utils.class }) |
| 35 | +@PowerMockIgnore({ "javax.management.*", "javax.script.*" }) |
| 36 | +public class ChildNutritionProgramStrategyTest { |
| 37 | + |
| 38 | + @Mock |
| 39 | + private ProgramWorkflowService mockProgramWorkflowService; |
| 40 | + |
| 41 | + @Mock |
| 42 | + private ConceptService mockConceptService; |
| 43 | + |
| 44 | + private ChildNutritionProgramStrategy childNutritionProgramStrategy; |
| 45 | + |
| 46 | + private Encounter buildEncounter(boolean childNutritionType) { |
| 47 | + EncounterType type = new EncounterType(); |
| 48 | + type.setUuid(childNutritionType ? LampConfig.CHILD_NUTRITION_ENCOUNTER_TYPE_UUID : "some-other-type"); |
| 49 | + |
| 50 | + Patient patient = new Patient(999); |
| 51 | + Location location = new Location(7); |
| 52 | + location.setName("Clinic"); |
| 53 | + |
| 54 | + Encounter encounter = new Encounter(); |
| 55 | + encounter.setEncounterType(type); |
| 56 | + encounter.setPatient(patient); |
| 57 | + encounter.setEncounterDatetime(new Date()); |
| 58 | + encounter.setLocation(location); |
| 59 | + return encounter; |
| 60 | + } |
| 61 | + |
| 62 | + @Before |
| 63 | + public void setup() { |
| 64 | + PowerMockito.mockStatic(Context.class); |
| 65 | + PowerMockito.mockStatic(Utils.class); |
| 66 | + |
| 67 | + when(Context.getProgramWorkflowService()).thenReturn(mockProgramWorkflowService); |
| 68 | + when(Context.getConceptService()).thenReturn(mockConceptService); |
| 69 | + |
| 70 | + childNutritionProgramStrategy = new ChildNutritionProgramStrategy(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void shouldExitWhenEncounterTypeIsNotChildNutrition() { |
| 75 | + Encounter encounter = buildEncounter(false); |
| 76 | + childNutritionProgramStrategy.execute(encounter, new User(), new Date(), "reason"); |
| 77 | + verify(mockProgramWorkflowService, never()).getProgramByUuid(anyString()); |
| 78 | + verify(mockProgramWorkflowService, never()).savePatientProgram(any(PatientProgram.class)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void shouldExitWhenProgramIsNull() { |
| 83 | + Encounter encounter = buildEncounter(true); |
| 84 | + when(mockProgramWorkflowService.getProgramByUuid(LampConfig.PROGRAM_CHILD_NUTRITION_UUID)).thenReturn(null); |
| 85 | + |
| 86 | + childNutritionProgramStrategy.execute(encounter, new User(), new Date(), "reason"); |
| 87 | + |
| 88 | + verify(mockProgramWorkflowService, times(1)).getProgramByUuid(LampConfig.PROGRAM_CHILD_NUTRITION_UUID); |
| 89 | + verify(mockProgramWorkflowService, never()).savePatientProgram(any(PatientProgram.class)); |
| 90 | + verify(mockConceptService, never()).getConceptByUuid(anyString()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void shouldExitWhenMalnutritionConceptIsNull() { |
| 95 | + Encounter encounter = buildEncounter(true); |
| 96 | + when(mockProgramWorkflowService.getProgramByUuid(LampConfig.PROGRAM_CHILD_NUTRITION_UUID)).thenReturn(new Program()); |
| 97 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_MALNUTRITION_STATUS_UUID)).thenReturn( |
| 98 | + null); |
| 99 | + |
| 100 | + childNutritionProgramStrategy.execute(encounter, new User(), new Date(), "reason"); |
| 101 | + |
| 102 | + verify(mockProgramWorkflowService, never()).savePatientProgram(any(PatientProgram.class)); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void shouldExitWhenReasonForDischargeConceptIsNull() { |
| 107 | + Encounter encounter = buildEncounter(true); |
| 108 | + Program program = new Program(); |
| 109 | + when(mockProgramWorkflowService.getProgramByUuid(LampConfig.PROGRAM_CHILD_NUTRITION_UUID)).thenReturn(program); |
| 110 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_MALNUTRITION_STATUS_UUID)).thenReturn( |
| 111 | + new Concept(101)); |
| 112 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_REASON_FOR_DISCHARGE_UUID)).thenReturn( |
| 113 | + null); |
| 114 | + |
| 115 | + childNutritionProgramStrategy.execute(encounter, new User(), new Date(), "reason"); |
| 116 | + |
| 117 | + verify(mockProgramWorkflowService, never()).savePatientProgram(any(PatientProgram.class)); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void shouldExitWhenBothValuesAreNull() { |
| 122 | + Encounter encounter = buildEncounter(true); |
| 123 | + Program program = new Program(); |
| 124 | + when(mockProgramWorkflowService.getProgramByUuid(LampConfig.PROGRAM_CHILD_NUTRITION_UUID)).thenReturn(program); |
| 125 | + Concept malC = new Concept(101); |
| 126 | + Concept reasonC = new Concept(102); |
| 127 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_MALNUTRITION_STATUS_UUID)).thenReturn( |
| 128 | + malC); |
| 129 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_REASON_FOR_DISCHARGE_UUID)).thenReturn( |
| 130 | + reasonC); |
| 131 | + |
| 132 | + PatientProgram patientProgram = new PatientProgram(); |
| 133 | + PowerMockito.when( |
| 134 | + Utils.getOrCreateActiveProgramEnrollment(eq(mockProgramWorkflowService), eq(encounter.getPatient()), |
| 135 | + eq(program), any(Date.class))).thenReturn(patientProgram); |
| 136 | + |
| 137 | + PowerMockito.when(Utils.findLatestCodedObsValue(encounter, malC)).thenReturn(null); |
| 138 | + PowerMockito.when(Utils.findLatestCodedObsValue(encounter, reasonC)).thenReturn(null); |
| 139 | + |
| 140 | + childNutritionProgramStrategy.execute(encounter, new User(), new Date(), "reason"); |
| 141 | + |
| 142 | + verify(mockProgramWorkflowService, never()).savePatientProgram(any(PatientProgram.class)); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void shouldExitWhenWorkflowIsNull() { |
| 147 | + Encounter encounter = buildEncounter(true); |
| 148 | + Program program = new Program(); |
| 149 | + when(mockProgramWorkflowService.getProgramByUuid(LampConfig.PROGRAM_CHILD_NUTRITION_UUID)).thenReturn(program); |
| 150 | + |
| 151 | + Concept malC = new Concept(101); |
| 152 | + Concept reasonC = new Concept(102); |
| 153 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_MALNUTRITION_STATUS_UUID)).thenReturn( |
| 154 | + malC); |
| 155 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_REASON_FOR_DISCHARGE_UUID)).thenReturn( |
| 156 | + reasonC); |
| 157 | + |
| 158 | + PatientProgram patientProgram = new PatientProgram(); |
| 159 | + PowerMockito.when( |
| 160 | + Utils.getOrCreateActiveProgramEnrollment(eq(mockProgramWorkflowService), eq(encounter.getPatient()), |
| 161 | + eq(program), any(Date.class))).thenReturn(patientProgram); |
| 162 | + |
| 163 | + PowerMockito.when(Utils.findLatestCodedObsValue(encounter, malC)).thenReturn(new Concept(201)); |
| 164 | + PowerMockito.when(Utils.findLatestCodedObsValue(encounter, reasonC)).thenReturn(null); |
| 165 | + |
| 166 | + PowerMockito.when(Utils.getWorkflowByUuid(program, LampConfig.WORKFLOW_CHILD_NUTRITION_UUID)).thenReturn(null); |
| 167 | + |
| 168 | + childNutritionProgramStrategy.execute(encounter, new User(), new Date(), "reason"); |
| 169 | + |
| 170 | + verify(mockProgramWorkflowService, never()).savePatientProgram(any(PatientProgram.class)); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void shouldExitWhenTargetStateFromMalnutritionStatusIsNull() { |
| 175 | + Encounter encounter = buildEncounter(true); |
| 176 | + Program program = new Program(); |
| 177 | + when(mockProgramWorkflowService.getProgramByUuid(LampConfig.PROGRAM_CHILD_NUTRITION_UUID)).thenReturn(program); |
| 178 | + |
| 179 | + Concept malC = new Concept(101); |
| 180 | + Concept reasonC = new Concept(102); |
| 181 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_MALNUTRITION_STATUS_UUID)).thenReturn( |
| 182 | + malC); |
| 183 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_REASON_FOR_DISCHARGE_UUID)).thenReturn( |
| 184 | + reasonC); |
| 185 | + |
| 186 | + PatientProgram pp = new PatientProgram(); |
| 187 | + PowerMockito.when( |
| 188 | + Utils.getOrCreateActiveProgramEnrollment(eq(mockProgramWorkflowService), eq(encounter.getPatient()), |
| 189 | + eq(program), any(Date.class))).thenReturn(pp); |
| 190 | + |
| 191 | + Concept malValue = new Concept(201); |
| 192 | + PowerMockito.when(Utils.findLatestCodedObsValue(encounter, malC)).thenReturn(malValue); |
| 193 | + PowerMockito.when(Utils.findLatestCodedObsValue(encounter, reasonC)).thenReturn(null); |
| 194 | + |
| 195 | + ProgramWorkflow wf = new ProgramWorkflow(); |
| 196 | + PowerMockito.when(Utils.getWorkflowByUuid(program, LampConfig.WORKFLOW_CHILD_NUTRITION_UUID)).thenReturn(wf); |
| 197 | + |
| 198 | + PowerMockito.when(Utils.getStateByConcept(wf, malValue)).thenReturn(null); |
| 199 | + |
| 200 | + childNutritionProgramStrategy.execute(encounter, new User(), new Date(), "reason"); |
| 201 | + |
| 202 | + verify(mockProgramWorkflowService, never()).savePatientProgram(any(PatientProgram.class)); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void shouldExitWhenTargetStateFromReasonForDischargeValueIsNull() { |
| 207 | + Encounter encounter = buildEncounter(true); |
| 208 | + Program program = new Program(); |
| 209 | + when(mockProgramWorkflowService.getProgramByUuid(LampConfig.PROGRAM_CHILD_NUTRITION_UUID)).thenReturn(program); |
| 210 | + |
| 211 | + Concept malC = new Concept(101); |
| 212 | + Concept reasonC = new Concept(102); |
| 213 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_MALNUTRITION_STATUS_UUID)).thenReturn( |
| 214 | + malC); |
| 215 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_REASON_FOR_DISCHARGE_UUID)).thenReturn( |
| 216 | + reasonC); |
| 217 | + |
| 218 | + PatientProgram pp = new PatientProgram(); |
| 219 | + PowerMockito.when( |
| 220 | + Utils.getOrCreateActiveProgramEnrollment(eq(mockProgramWorkflowService), eq(encounter.getPatient()), |
| 221 | + eq(program), any(Date.class))).thenReturn(pp); |
| 222 | + |
| 223 | + // malnutrition value null, reason value present |
| 224 | + PowerMockito.when(Utils.findLatestCodedObsValue(encounter, malC)).thenReturn(null); |
| 225 | + Concept reasonValue = new Concept(301); |
| 226 | + PowerMockito.when(Utils.findLatestCodedObsValue(encounter, reasonC)).thenReturn(reasonValue); |
| 227 | + |
| 228 | + ProgramWorkflow wf = new ProgramWorkflow(); |
| 229 | + PowerMockito.when(Utils.getWorkflowByUuid(program, LampConfig.WORKFLOW_CHILD_NUTRITION_UUID)).thenReturn(wf); |
| 230 | + |
| 231 | + PowerMockito.when(Utils.getStateByConcept(wf, reasonValue)).thenReturn(null); |
| 232 | + |
| 233 | + childNutritionProgramStrategy.execute(encounter, new User(), new Date(), "reason"); |
| 234 | + |
| 235 | + verify(mockProgramWorkflowService, never()).savePatientProgram(any(PatientProgram.class)); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + public void shouldNotSetDateCompletedWhenReachedTargetFromMalnutritionStatus() { |
| 240 | + Encounter encounter = buildEncounter(true); |
| 241 | + Date now = new Date(); |
| 242 | + Program program = new Program(); |
| 243 | + when(mockProgramWorkflowService.getProgramByUuid(LampConfig.PROGRAM_CHILD_NUTRITION_UUID)).thenReturn(program); |
| 244 | + |
| 245 | + Concept malC = new Concept(101); |
| 246 | + Concept reasonC = new Concept(102); |
| 247 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_MALNUTRITION_STATUS_UUID)).thenReturn( |
| 248 | + malC); |
| 249 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_REASON_FOR_DISCHARGE_UUID)).thenReturn( |
| 250 | + reasonC); |
| 251 | + |
| 252 | + PatientProgram pp = new PatientProgram(); |
| 253 | + PowerMockito.when( |
| 254 | + Utils.getOrCreateActiveProgramEnrollment(eq(mockProgramWorkflowService), eq(encounter.getPatient()), |
| 255 | + eq(program), any(Date.class))).thenReturn(pp); |
| 256 | + |
| 257 | + Concept malValue = new Concept(201); |
| 258 | + malValue.setUuid(LampConfig.CONCEPT_REACHED_TARGET_GOAL_WEIGHT_UUID); |
| 259 | + PowerMockito.when(Utils.findLatestCodedObsValue(encounter, malC)).thenReturn(malValue); |
| 260 | + PowerMockito.when(Utils.findLatestCodedObsValue(encounter, reasonC)).thenReturn(null); |
| 261 | + |
| 262 | + ProgramWorkflow wf = new ProgramWorkflow(); |
| 263 | + PowerMockito.when(Utils.getWorkflowByUuid(program, LampConfig.WORKFLOW_CHILD_NUTRITION_UUID)).thenReturn(wf); |
| 264 | + |
| 265 | + ProgramWorkflowState state = new ProgramWorkflowState(); |
| 266 | + state.setConcept(malValue); |
| 267 | + PowerMockito.when(Utils.getStateByConcept(wf, malValue)).thenReturn(state); |
| 268 | + |
| 269 | + childNutritionProgramStrategy.execute(encounter, new User(), now, "reason"); |
| 270 | + |
| 271 | + // dateCompleted should NOT be set because it came from malnutrition path |
| 272 | + assertNull(pp.getDateCompleted()); |
| 273 | + assertFalse(state.getTerminal()); |
| 274 | + |
| 275 | + // transition & save |
| 276 | + assertEquals(encounter.getLocation(), pp.getLocation()); |
| 277 | + verify(mockProgramWorkflowService, times(1)).savePatientProgram(pp); |
| 278 | + } |
| 279 | + |
| 280 | + @Test |
| 281 | + public void shouldSetDateCompletedWhenReachedTargetFromReasonForDischarge() { |
| 282 | + Encounter encounter = buildEncounter(true); |
| 283 | + Date now = new Date(); |
| 284 | + Program program = new Program(); |
| 285 | + when(mockProgramWorkflowService.getProgramByUuid(LampConfig.PROGRAM_CHILD_NUTRITION_UUID)).thenReturn(program); |
| 286 | + |
| 287 | + Concept malC = new Concept(101); |
| 288 | + Concept reasonC = new Concept(102); |
| 289 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_MALNUTRITION_STATUS_UUID)).thenReturn( |
| 290 | + malC); |
| 291 | + when(mockConceptService.getConceptByUuid(LampConfig.CONCEPT_CHILD_NUTRITION_REASON_FOR_DISCHARGE_UUID)).thenReturn( |
| 292 | + reasonC); |
| 293 | + |
| 294 | + PatientProgram pp = new PatientProgram(); |
| 295 | + PowerMockito.when( |
| 296 | + Utils.getOrCreateActiveProgramEnrollment(eq(mockProgramWorkflowService), eq(encounter.getPatient()), |
| 297 | + eq(program), any(Date.class))).thenReturn(pp); |
| 298 | + |
| 299 | + // malnutrition null; reason == REACHED_TARGET_GOAL_WEIGHT |
| 300 | + PowerMockito.when(Utils.findLatestCodedObsValue(encounter, malC)).thenReturn(null); |
| 301 | + |
| 302 | + Concept reasonValue = new Concept(301); |
| 303 | + reasonValue.setUuid(LampConfig.CONCEPT_REACHED_TARGET_GOAL_WEIGHT_UUID); |
| 304 | + PowerMockito.when(Utils.findLatestCodedObsValue(encounter, reasonC)).thenReturn(reasonValue); |
| 305 | + |
| 306 | + ProgramWorkflow wf = new ProgramWorkflow(); |
| 307 | + PowerMockito.when(Utils.getWorkflowByUuid(program, LampConfig.WORKFLOW_CHILD_NUTRITION_UUID)).thenReturn(wf); |
| 308 | + |
| 309 | + ProgramWorkflowState state = new ProgramWorkflowState(); |
| 310 | + state.setConcept(reasonValue); |
| 311 | + state.setTerminal(true); |
| 312 | + PowerMockito.when(Utils.getStateByConcept(wf, reasonValue)).thenReturn(state); |
| 313 | + |
| 314 | + childNutritionProgramStrategy.execute(encounter, new User(), now, "reason"); |
| 315 | + |
| 316 | + // dateCompleted should NOT be set because it came from malnutrition path |
| 317 | + assertNull(pp.getDateCompleted()); |
| 318 | + assertTrue(state.getTerminal()); |
| 319 | + |
| 320 | + // transition & save |
| 321 | + assertEquals(encounter.getLocation(), pp.getLocation()); |
| 322 | + verify(mockProgramWorkflowService, times(1)).savePatientProgram(pp); |
| 323 | + } |
| 324 | +} |
0 commit comments