private void setUpCallLog(CallDirection callDirection) { String patientDocId = "patientDocId"; String clinicId = "clinicId"; callLog = new CallLog(patientDocId); callLog.patientId("patientId"); initiatedTime = DateUtil.newDateTime(2011, 10, 10, 10, 9, 10); startTime = DateUtil.newDateTime(2011, 10, 10, 10, 10, 10); callLog.setStartTime(initiatedTime); callLog.setEndTime(DateUtil.now()); callLog.setCallDirection(callDirection); callLog.clinicId(clinicId); callLog.setPhoneNumber("1234567890"); callLog.callLanguage("en"); final CallEvent callEvent = mock(CallEvent.class); when(callEvent.getTimeStamp()).thenReturn(startTime); CallEventCustomData customData = new CallEventCustomData(); customData.add(TAMAIVRContext.MESSAGE_CATEGORY_NAME, "All Messages"); when(callEvent.getData()).thenReturn(customData); callLog.setCallEvents( new ArrayList<CallEvent>() { { add(callEvent); } }); Clinic clinic = new Clinic(clinicId); Patient patient = PatientBuilder.startRecording() .withId(patientDocId) .withPatientId("patientId") .withGender(Gender.newGender("Male")) .withClinic(clinic) .withTravelTimeToClinicInDays(1) .withTravelTimeToClinicInHours(1) .withTravelTimeToClinicInMinutes(1) .withDateOfBirth(DateUtil.today().minusYears(40)) .build(); CallLogView callLogView = mock(CallLogView.class); clinic.setName("clinicName"); when(allIVRLanguages.getByCode("en")).thenReturn(IVRLanguage.newIVRLanguage("English", "en")); when(allPatients.getAll()).thenReturn(asList(patient)); when(callLogViewMapper.toCallLogView(asList(callLog))).thenReturn(asList(callLogView)); callLogSummaryBuilder = new CallLogSummaryBuilder(allPatients, new Patients(allPatients.getAll()), allIVRLanguages); }
@Test public void shouldScheduleDailyAdherenceQualityJobsForAPatientOnDailyReminderStartingFromTodayIfTreatmentAdviceStartDateIsInPast() { final String patientId = "123456"; Patient patient = PatientBuilder.startRecording() .withDefaults() .withId(patientId) .withCallPreference(CallPreference.DailyPillReminder) .build(); final LocalDate startDate = DateUtil.today().minusDays(2); final LocalDate endDate = DateUtil.today().plusDays(2); final DateTime timeFewMillisBack = DateUtil.now().minusMillis(1000); TreatmentAdvice advice = getTreatmentAdvice(startDate, endDate); schedulerService.scheduleJobForDeterminingAdherenceQualityInDailyPillReminder(patient, advice); final ArgumentCaptor<CronSchedulableJob> cronArgCaptor = ArgumentCaptor.forClass(CronSchedulableJob.class); verify(motechSchedulerService).safeScheduleJob(cronArgCaptor.capture()); CronSchedulableJob jobScheduledWithParams = cronArgCaptor.getValue(); final MotechEvent motechEventInScheduledJob = jobScheduledWithParams.getMotechEvent(); Map<String, Object> paramsInScheduledJob = motechEventInScheduledJob.getParameters(); assertEquals( "Should setup the cron expression to run at every midnight.", jobScheduledWithParams.getCronExpression(), "0 0 0 * * ?"); DateTime actualTimeWhenTriggerWasActivated = DateUtil.newDateTime(jobScheduledWithParams.getStartTime()); assertTrue( "Since the advice has already started in past, we should schedule it starting now, which is after a time few milli seconds back.", timeFewMillisBack.isBefore(actualTimeWhenTriggerWasActivated)); DateTime rightNow = DateUtil.now(); assertTrue(rightNow.isEqual(actualTimeWhenTriggerWasActivated)); assertEquals(jobScheduledWithParams.getEndTime(), endDate.plusDays(1).toDate()); assertEquals(paramsInScheduledJob.get(EventKeys.EXTERNAL_ID_KEY), patientId); assertEquals( motechEventInScheduledJob.getSubject(), TAMAConstants.DAILY_ADHERENCE_IN_RED_ALERT_SUBJECT); }