Search in sources :

Example 6 with ScheduledTask

use of com.synopsys.integration.alert.api.task.ScheduledTask in project hub-alert by blackducksoftware.

the class TaskActionTest method testReadTasks.

@Test
public void testReadTasks() {
    Long expectedDelay = 1000L;
    TaskScheduler scheduler = Mockito.mock(TaskScheduler.class);
    ScheduledFuture scheduledFuture = Mockito.mock(ScheduledFuture.class);
    Mockito.when(scheduledFuture.isDone()).thenReturn(Boolean.FALSE);
    Mockito.when(scheduledFuture.getDelay(Mockito.eq(TimeUnit.MILLISECONDS))).thenReturn(expectedDelay);
    Mockito.when(scheduler.scheduleAtFixedRate(Mockito.any(), Mockito.anyLong())).thenReturn(scheduledFuture);
    ScheduledTask task = new ScheduledTask(scheduler) {

        @Override
        public void runTask() {
        }

        @Override
        public String scheduleCronExpression() {
            return ScheduledTask.EVERY_MINUTE_CRON_EXPRESSION;
        }
    };
    TaskManagementDescriptorKey descriptorKey = new TaskManagementDescriptorKey();
    AuthorizationManager authorizationManager = Mockito.mock(AuthorizationManager.class);
    Mockito.when(authorizationManager.hasReadPermission(Mockito.eq(ConfigContextEnum.GLOBAL), Mockito.eq(descriptorKey))).thenReturn(Boolean.TRUE);
    TaskManager taskManager = new TaskManager();
    taskManager.registerTask(task);
    taskManager.scheduleExecutionAtFixedRate(expectedDelay, task.getTaskName());
    TaskActions actions = new TaskActions(descriptorKey, authorizationManager, taskManager);
    ActionResponse<MultiTaskMetaDataModel> response = actions.getTasks();
    assertTrue(response.isSuccessful());
    assertTrue(response.hasContent());
    MultiTaskMetaDataModel tasksModel = response.getContent().orElse(new MultiTaskMetaDataModel(List.of()));
    TaskMetaData model = tasksModel.getTasks().stream().findFirst().orElse(null);
    assertNotNull(model);
    assertNotNull(task.getTaskName());
    assertEquals(task.getFormatedNextRunTime().orElse(""), model.getNextRunTime());
}
Also used : TaskManagementDescriptorKey(com.synopsys.integration.alert.component.tasks.TaskManagementDescriptorKey) TaskManager(com.synopsys.integration.alert.api.task.TaskManager) TaskMetaData(com.synopsys.integration.alert.api.task.TaskMetaData) ScheduledTask(com.synopsys.integration.alert.api.task.ScheduledTask) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) TaskScheduler(org.springframework.scheduling.TaskScheduler) ScheduledFuture(java.util.concurrent.ScheduledFuture) Test(org.junit.jupiter.api.Test)

Example 7 with ScheduledTask

use of com.synopsys.integration.alert.api.task.ScheduledTask in project hub-alert by blackducksoftware.

the class TaskManagerTest method testRegistration.

@Test
public void testRegistration() {
    ScheduledTask task = Mockito.mock(ScheduledTask.class);
    final String taskName = "a_task";
    Mockito.when(task.getTaskName()).thenReturn(taskName);
    TaskManager taskManager = new TaskManager();
    taskManager.registerTask(task);
    assertEquals(1, taskManager.getTaskCount());
    Optional<ScheduledTask> emptyTask = taskManager.unregisterTask("unknown_task");
    assertTrue(emptyTask.isEmpty());
    assertEquals(1, taskManager.getTaskCount());
    Optional<ScheduledTask> removedTask = taskManager.unregisterTask(taskName);
    assertTrue(removedTask.isPresent());
    assertEquals(0, taskManager.getTaskCount());
}
Also used : TaskManager(com.synopsys.integration.alert.api.task.TaskManager) ScheduledTask(com.synopsys.integration.alert.api.task.ScheduledTask) Test(org.junit.jupiter.api.Test)

Example 8 with ScheduledTask

use of com.synopsys.integration.alert.api.task.ScheduledTask in project hub-alert by blackducksoftware.

the class TaskManagerTest method testGetNextRunTime.

@Test
public void testGetNextRunTime() {
    ScheduledTask task = Mockito.mock(ScheduledTask.class);
    final String taskName = "a_task";
    Mockito.when(task.getTaskName()).thenReturn(taskName);
    TaskManager taskManager = new TaskManager();
    taskManager.registerTask(task);
    taskManager.getNextRunTime(taskName);
    Mockito.verify(task).getFormatedNextRunTime();
}
Also used : TaskManager(com.synopsys.integration.alert.api.task.TaskManager) ScheduledTask(com.synopsys.integration.alert.api.task.ScheduledTask) Test(org.junit.jupiter.api.Test)

Example 9 with ScheduledTask

use of com.synopsys.integration.alert.api.task.ScheduledTask in project hub-alert by blackducksoftware.

the class TaskManagerTest method testScheduleCronForUnknownTask.

@Test
public void testScheduleCronForUnknownTask() {
    ScheduledTask task = Mockito.mock(ScheduledTask.class);
    final String taskName = "a_task";
    final String cronExpression = "cron_expression";
    Mockito.when(task.getTaskName()).thenReturn(taskName);
    TaskManager taskManager = new TaskManager();
    taskManager.registerTask(task);
    taskManager.scheduleCronTask(cronExpression, "unknown_task");
    Mockito.verify(task, Mockito.times(0)).scheduleExecution(Mockito.anyString());
    taskManager.scheduleCronTask(cronExpression, null);
    Mockito.verify(task, Mockito.times(0)).scheduleExecution(Mockito.anyString());
}
Also used : TaskManager(com.synopsys.integration.alert.api.task.TaskManager) ScheduledTask(com.synopsys.integration.alert.api.task.ScheduledTask) Test(org.junit.jupiter.api.Test)

Example 10 with ScheduledTask

use of com.synopsys.integration.alert.api.task.ScheduledTask in project hub-alert by blackducksoftware.

the class TaskManagerTest method testScheduleFixedRate.

@Test
public void testScheduleFixedRate() {
    ScheduledTask task = Mockito.mock(ScheduledTask.class);
    final String taskName = "a_task";
    final long period = 999;
    Mockito.when(task.getTaskName()).thenReturn(taskName);
    TaskManager taskManager = new TaskManager();
    taskManager.registerTask(task);
    taskManager.scheduleExecutionAtFixedRate(period, taskName);
    Mockito.verify(task).scheduleExecutionAtFixedRate(Mockito.anyLong());
}
Also used : TaskManager(com.synopsys.integration.alert.api.task.TaskManager) ScheduledTask(com.synopsys.integration.alert.api.task.ScheduledTask) Test(org.junit.jupiter.api.Test)

Aggregations

ScheduledTask (com.synopsys.integration.alert.api.task.ScheduledTask)11 TaskManager (com.synopsys.integration.alert.api.task.TaskManager)11 Test (org.junit.jupiter.api.Test)11 TaskMetaData (com.synopsys.integration.alert.api.task.TaskMetaData)1 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)1 TaskManagementDescriptorKey (com.synopsys.integration.alert.component.tasks.TaskManagementDescriptorKey)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 TaskScheduler (org.springframework.scheduling.TaskScheduler)1