Search in sources :

Example 26 with TaskDefinition

use of org.openmrs.scheduler.TaskDefinition in project openmrs-core by openmrs.

the class SchedulerFormValidatorTest method validate_shouldFailValidationIfRepeatIntervalIsNullOrEmptyOrWhitespace.

/**
 * @see SchedulerFormValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfRepeatIntervalIsNullOrEmptyOrWhitespace() {
    TaskDefinition def = new TaskDefinition();
    def.setName("Chores");
    def.setTaskClass("org.openmrs.scheduler.tasks.HelloWorldTask");
    Errors errors = new BindException(def, "def");
    new SchedulerFormValidator().validate(def, errors);
    Assert.assertTrue(errors.hasFieldErrors("repeatInterval"));
    def.setTaskClass(" ");
    errors = new BindException(def, "def");
    new SchedulerFormValidator().validate(def, errors);
    Assert.assertTrue(errors.hasFieldErrors("repeatInterval"));
}
Also used : Errors(org.springframework.validation.Errors) TaskDefinition(org.openmrs.scheduler.TaskDefinition) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 27 with TaskDefinition

use of org.openmrs.scheduler.TaskDefinition in project openmrs-core by openmrs.

the class SchedulerFormValidatorTest method validate_shouldFailValidationIfClassIsNotInstanceOfTask.

/**
 * @see SchedulerFormValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfClassIsNotInstanceOfTask() {
    TaskDefinition def = new TaskDefinition();
    def.setName("Chores");
    def.setRepeatInterval(3600000L);
    def.setTaskClass("org.openmrs.Obs");
    Errors errors = new BindException(def, "def");
    new SchedulerFormValidator().validate(def, errors);
    Assert.assertTrue(errors.hasFieldErrors("taskClass"));
    Assert.assertEquals("Scheduler.taskForm.classDoesNotImplementTask", errors.getFieldError("taskClass").getCode());
}
Also used : Errors(org.springframework.validation.Errors) TaskDefinition(org.openmrs.scheduler.TaskDefinition) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 28 with TaskDefinition

use of org.openmrs.scheduler.TaskDefinition in project openmrs-core by openmrs.

the class SchedulerFormValidatorTest method validate_shouldFailValidationIfClassIsNotAccessible.

/**
 * @see SchedulerFormValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfClassIsNotAccessible() {
    TaskDefinition def = new TaskDefinition();
    def.setName("Chores");
    def.setRepeatInterval(3600000L);
    // TODO: Find a way to trigger an IllegalAccessException
    def.setTaskClass("???");
    Errors errors = new BindException(def, "def");
    new SchedulerFormValidator().validate(def, errors);
    Assert.assertTrue(errors.hasFieldErrors("taskClass"));
    Assert.assertEquals("Scheduler.taskForm.classNotFoundException", errors.getFieldError("taskClass").getCode());
}
Also used : Errors(org.springframework.validation.Errors) TaskDefinition(org.openmrs.scheduler.TaskDefinition) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 29 with TaskDefinition

use of org.openmrs.scheduler.TaskDefinition in project openmrs-core by openmrs.

the class SchedulerFormValidatorTest method validate_shouldFailValidationIfTaskClassIsEmptyOrWhitespace.

/**
 * @see SchedulerFormValidator#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationIfTaskClassIsEmptyOrWhitespace() {
    TaskDefinition def = new TaskDefinition();
    def.setName("Chores");
    def.setRepeatInterval(3600000L);
    def.setTaskClass("");
    Errors errors = new BindException(def, "def");
    new SchedulerFormValidator().validate(def, errors);
    Assert.assertTrue(errors.hasFieldErrors("taskClass"));
    def.setTaskClass(" ");
    errors = new BindException(def, "def");
    new SchedulerFormValidator().validate(def, errors);
    Assert.assertTrue(errors.hasFieldErrors("taskClass"));
}
Also used : Errors(org.springframework.validation.Errors) TaskDefinition(org.openmrs.scheduler.TaskDefinition) BindException(org.springframework.validation.BindException) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 30 with TaskDefinition

use of org.openmrs.scheduler.TaskDefinition in project openmrs-core by openmrs.

the class TimerSchedulerServiceImplTest method scheduleTask_shouldHandleZeroRepeatInterval.

/**
 * Tests whether the TimerScheduler schedules tasks even if the repeatInterval is zero.
 *
 * @throws SchedulerException
 * @see TimerSchedulerServiceImpl#scheduleTask(TaskDefinition)
 */
@Test
public void scheduleTask_shouldHandleZeroRepeatInterval() throws SchedulerException {
    // Represents the start time of the task (right now)
    Calendar startTime = Calendar.getInstance();
    // Define repeatInterval as zero
    Long repeatInterval = 0L;
    String taskName = "TestTask";
    String className = "org.openmrs.scheduler.tasks.TestTask";
    Boolean startOnStartup = false;
    // Create the new task
    TaskDefinition taskDefinition = new TaskDefinition();
    taskDefinition.setName(taskName);
    taskDefinition.setTaskClass(className);
    taskDefinition.setStartTime(startTime.getTime());
    taskDefinition.setRepeatInterval(repeatInterval);
    taskDefinition.setStartOnStartup(startOnStartup);
    Task clientTask = null;
    clientTask = Context.getSchedulerService().scheduleTask(taskDefinition);
    // without this commit there seems to be a table lock left on the SCHEDULER_TASK_CONFIG table, see TRUNK-4212
    Context.flushSession();
    // Assert that the clientTask is not null, i.e. the sheduleTask was able to successfully schedule in case of zero repeatInterval.
    assertNotNull("The clientTask variable is null, so either the TimerSchedulerServiceImpl.scheduleTask method hasn't finished or didn't get run", clientTask);
}
Also used : TaskDefinition(org.openmrs.scheduler.TaskDefinition) Task(org.openmrs.scheduler.Task) Calendar(java.util.Calendar) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Aggregations

TaskDefinition (org.openmrs.scheduler.TaskDefinition)30 Test (org.junit.Test)12 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)11 BindException (org.springframework.validation.BindException)10 Errors (org.springframework.validation.Errors)10 SchedulerException (org.openmrs.scheduler.SchedulerException)9 SchedulerService (org.openmrs.scheduler.SchedulerService)7 Date (java.util.Date)6 APIException (org.openmrs.api.APIException)3 ObjectRetrievalFailureException (org.springframework.orm.ObjectRetrievalFailureException)3 HashSet (java.util.HashSet)2 Task (org.openmrs.scheduler.Task)2 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Criteria (org.hibernate.Criteria)1 MarkAppointmentsAsMissedOrCompletedTask (org.openmrs.module.mirebalais.task.MarkAppointmentsAsMissedOrCompletedTask)1 CloseStaleCreateRequestsTask (org.openmrs.module.paperrecord.CloseStaleCreateRequestsTask)1 CloseStalePullRequestsTask (org.openmrs.module.paperrecord.CloseStalePullRequestsTask)1 PihCloseStaleVisitsTask (org.openmrs.module.pihcore.task.PihCloseStaleVisitsTask)1 UpdateProviderRetiredStatesBasedOnAssociatedUserAccounts (org.openmrs.module.pihcore.task.UpdateProviderRetiredStatesBasedOnAssociatedUserAccounts)1