Search in sources :

Example 6 with TaskManager

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

the class ProcessingTaskTest method testReadEmptyList.

@Test
void testReadEmptyList() {
    TaskManager taskManager = Mockito.mock(TaskManager.class);
    TaskScheduler taskScheduler = Mockito.mock(TaskScheduler.class);
    NotificationAccessor notificationManager = new MockProcessingNotificationAccessor(List.of());
    StaticJobAccessor jobAccessor = Mockito.mock(StaticJobAccessor.class);
    Mockito.when(jobAccessor.hasJobsByFrequency(Mockito.any())).thenReturn(true);
    NotificationDetailExtractionDelegator extractionDelegator = new NotificationDetailExtractionDelegator(blackDuckResponseResolver, List.of());
    NotificationProcessor notificationProcessor = new NotificationProcessor(extractionDelegator, null, null, null, null, null);
    ProcessingTask task = createTask(taskScheduler, notificationManager, notificationProcessor, taskManager, jobAccessor);
    DateRange dateRange = task.getDateRange();
    ProcessingTask processingTask = Mockito.spy(task);
    List<AlertNotificationModel> actualModelList = processingTask.read(dateRange, AlertPagedModel.DEFAULT_PAGE_NUMBER, AlertPagedModel.DEFAULT_PAGE_SIZE).getModels();
    assertEquals(Collections.emptyList(), actualModelList);
}
Also used : AlertNotificationModel(com.synopsys.integration.alert.common.rest.model.AlertNotificationModel) DateRange(com.synopsys.integration.alert.common.message.model.DateRange) TaskManager(com.synopsys.integration.alert.api.task.TaskManager) DefaultNotificationAccessor(com.synopsys.integration.alert.database.api.DefaultNotificationAccessor) NotificationAccessor(com.synopsys.integration.alert.common.persistence.accessor.NotificationAccessor) StaticJobAccessor(com.synopsys.integration.alert.database.api.StaticJobAccessor) NotificationProcessor(com.synopsys.integration.alert.processor.api.NotificationProcessor) TaskScheduler(org.springframework.scheduling.TaskScheduler) NotificationDetailExtractionDelegator(com.synopsys.integration.alert.processor.api.detail.NotificationDetailExtractionDelegator) Test(org.junit.jupiter.api.Test)

Example 7 with TaskManager

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

the class ProcessingTaskTest method testPagedRead.

@Test
void testPagedRead() throws IOException {
    TaskManager taskManager = Mockito.mock(TaskManager.class);
    TaskScheduler taskScheduler = Mockito.mock(TaskScheduler.class);
    NotificationAccessor notificationManager = new MockProcessingNotificationAccessor(List.of());
    StaticJobAccessor jobAccessor = Mockito.mock(StaticJobAccessor.class);
    Mockito.when(jobAccessor.hasJobsByFrequency(Mockito.any())).thenReturn(true);
    NotificationDetailExtractionDelegator extractionDelegator = new NotificationDetailExtractionDelegator(blackDuckResponseResolver, List.of());
    NotificationProcessor notificationProcessor = new NotificationProcessor(extractionDelegator, null, null, null, null, null);
    ProcessingTask task = createTask(taskScheduler, notificationManager, notificationProcessor, taskManager, jobAccessor);
    int count = 20;
    List<AlertNotificationModel> allModels = new ArrayList<>(count);
    for (int index = 0; index < count; index++) {
        String notificationJson = TestResourceUtils.readFileToString("json/projectVersionNotification.json");
        AlertNotificationModel model = new AlertNotificationModel(Integer.valueOf(index).longValue(), 1L, "BlackDuck", "BlackDuck_1", "PROJECT_VERSION", notificationJson, DateUtils.createCurrentDateTimestamp(), DateUtils.createCurrentDateTimestamp(), false);
        allModels.add(model);
    }
    notificationManager.saveAllNotifications(allModels);
    DateRange dateRange = task.getDateRange();
    int pageSize = 5;
    int totalPages = count / pageSize;
    List<AlertNotificationModel> testModels = new ArrayList<>(count);
    for (int currentPage = 0; currentPage < totalPages; currentPage++) {
        AlertPagedModel<AlertNotificationModel> pagedModel = task.read(dateRange, currentPage, pageSize);
        assertEquals(totalPages, pagedModel.getTotalPages());
        assertEquals(currentPage, pagedModel.getCurrentPage());
        assertEquals(pageSize, pagedModel.getPageSize());
        testModels.addAll(pagedModel.getModels());
    }
    assertEquals(allModels.size(), testModels.size());
    assertTrue(allModels.containsAll(testModels));
}
Also used : AlertNotificationModel(com.synopsys.integration.alert.common.rest.model.AlertNotificationModel) DefaultNotificationAccessor(com.synopsys.integration.alert.database.api.DefaultNotificationAccessor) NotificationAccessor(com.synopsys.integration.alert.common.persistence.accessor.NotificationAccessor) ArrayList(java.util.ArrayList) TaskScheduler(org.springframework.scheduling.TaskScheduler) DateRange(com.synopsys.integration.alert.common.message.model.DateRange) TaskManager(com.synopsys.integration.alert.api.task.TaskManager) StaticJobAccessor(com.synopsys.integration.alert.database.api.StaticJobAccessor) NotificationProcessor(com.synopsys.integration.alert.processor.api.NotificationProcessor) NotificationDetailExtractionDelegator(com.synopsys.integration.alert.processor.api.detail.NotificationDetailExtractionDelegator) Test(org.junit.jupiter.api.Test)

Example 8 with TaskManager

use of com.synopsys.integration.alert.api.task.TaskManager 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 9 with TaskManager

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

the class TaskActionTest method testReadForbiddenTasks.

@Test
public void testReadForbiddenTasks() {
    TaskManagementDescriptorKey descriptorKey = new TaskManagementDescriptorKey();
    AuthorizationManager authorizationManager = Mockito.mock(AuthorizationManager.class);
    Mockito.when(authorizationManager.hasReadPermission(Mockito.eq(ConfigContextEnum.GLOBAL), Mockito.eq(descriptorKey))).thenReturn(Boolean.FALSE);
    TaskManager taskManager = new TaskManager();
    TaskActions actions = new TaskActions(descriptorKey, authorizationManager, taskManager);
    ActionResponse<MultiTaskMetaDataModel> response = actions.getTasks();
    assertTrue(response.isError());
    assertFalse(response.hasContent());
}
Also used : TaskManagementDescriptorKey(com.synopsys.integration.alert.component.tasks.TaskManagementDescriptorKey) TaskManager(com.synopsys.integration.alert.api.task.TaskManager) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) Test(org.junit.jupiter.api.Test)

Example 10 with TaskManager

use of com.synopsys.integration.alert.api.task.TaskManager 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)

Aggregations

TaskManager (com.synopsys.integration.alert.api.task.TaskManager)18 Test (org.junit.jupiter.api.Test)17 ScheduledTask (com.synopsys.integration.alert.api.task.ScheduledTask)11 TaskScheduler (org.springframework.scheduling.TaskScheduler)6 DefaultNotificationAccessor (com.synopsys.integration.alert.database.api.DefaultNotificationAccessor)5 StaticJobAccessor (com.synopsys.integration.alert.database.api.StaticJobAccessor)5 NotificationProcessor (com.synopsys.integration.alert.processor.api.NotificationProcessor)5 NotificationDetailExtractionDelegator (com.synopsys.integration.alert.processor.api.detail.NotificationDetailExtractionDelegator)5 DateRange (com.synopsys.integration.alert.common.message.model.DateRange)4 NotificationAccessor (com.synopsys.integration.alert.common.persistence.accessor.NotificationAccessor)4 AlertNotificationModel (com.synopsys.integration.alert.common.rest.model.AlertNotificationModel)4 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)2 TaskManagementDescriptorKey (com.synopsys.integration.alert.component.tasks.TaskManagementDescriptorKey)2 ArrayList (java.util.ArrayList)2 ProviderSchedulingManager (com.synopsys.integration.alert.api.provider.lifecycle.ProviderSchedulingManager)1 StatefulProvider (com.synopsys.integration.alert.api.provider.state.StatefulProvider)1 TaskMetaData (com.synopsys.integration.alert.api.task.TaskMetaData)1 ConfigurationModelConfigurationAccessor (com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor)1 ProviderDataAccessor (com.synopsys.integration.alert.common.persistence.accessor.ProviderDataAccessor)1 ConfigurationFieldModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel)1