Search in sources :

Example 51 with IdmLongRunningTaskDto

use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.

the class DefaultLongRunningTaskManagerIntegrationTest method testCancelPreviouslyRunnedTask.

@Test
public void testCancelPreviouslyRunnedTask() {
    IdmLongRunningTaskDto taskOne = new IdmLongRunningTaskDto();
    taskOne.setResult(new OperationResult.Builder(OperationState.RUNNING).build());
    taskOne.setInstanceId(configurationService.getInstanceId());
    taskOne.setTaskType(TestSimpleLongRunningTaskExecutor.class.getCanonicalName());
    taskOne.setRunning(true);
    taskOne = service.save(taskOne);
    // 
    IdmLongRunningTaskDto taskTwo = new IdmLongRunningTaskDto();
    taskTwo.setResult(new OperationResult.Builder(OperationState.RUNNING).build());
    taskTwo.setInstanceId("different-instance");
    taskTwo.setTaskType(TestSimpleLongRunningTaskExecutor.class.getCanonicalName());
    taskTwo.setRunning(true);
    taskTwo = service.save(taskTwo);
    // 
    manager.init();
    // 
    taskOne = service.get(taskOne.getId());
    taskTwo = service.get(taskTwo.getId());
    // 
    assertEquals(OperationState.CANCELED, taskOne.getResultState());
    assertFalse(taskOne.isRunning());
    assertEquals(OperationState.RUNNING, taskTwo.getResultState());
    assertTrue(taskTwo.isRunning());
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 52 with IdmLongRunningTaskDto

use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManagerIntegrationTest method testDependentTaskNoExecutionAfterInitiatorFails.

@Test
public void testDependentTaskNoExecutionAfterInitiatorFails() throws Exception {
    helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, false);
    try {
        LongRunningTaskExecuteDependentProcessor processor = context.getBean(LongRunningTaskExecuteDependentProcessor.class);
        // 
        IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
        filter.setOperationState(OperationState.CREATED);
        filter.setTaskType(TestSchedulableTask.class.getCanonicalName());
        filter.setFrom(new DateTime());
        List<IdmLongRunningTaskDto> createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(0L, createdLrts.size());
        // 
        Task task = createTask("dependent-task-initiator");
        DependentTaskTrigger trigger = new DependentTaskTrigger();
        trigger.setInitiatorTaskId(task.getId());
        // 
        // initiator = dependent task => circular execution
        manager.createTrigger(task.getId(), trigger);
        manager.runTask(task.getId());
        helper.waitForResult(getContinueFunction());
        createdLrts = longRunningTaskService.find(filter, null).getContent();
        Assert.assertEquals(1L, createdLrts.size());
        IdmLongRunningTaskDto lrt = createdLrts.get(0);
        lrt.setResult(new OperationResult(OperationState.EXCEPTION));
        // not executed
        EventResult<IdmLongRunningTaskDto> result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.BLOCKED));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.NOT_EXECUTED));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.CANCELED));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.CREATED));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        lrt.setResult(new OperationResult(OperationState.RUNNING));
        result = processor.process(new LongRunningTaskEvent(LongRunningTaskEventType.END, lrt));
        Assert.assertEquals(OperationState.NOT_EXECUTED, result.getResults().get(0).getState());
        // 
        longRunningTaskManager.cancel(lrt.getId());
        // 
        longRunningTaskService.find(filter, null).getContent();
        // cancel - clean up
        Assert.assertEquals(0L, longRunningTaskService.find(filter, null).getTotalElements());
    } finally {
        helper.setConfigurationValue(SchedulerConfiguration.PROPERTY_TASK_ASYNCHRONOUS_ENABLED, true);
    }
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) FutureTask(java.util.concurrent.FutureTask) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) LongRunningFutureTask(eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask) LongRunningTaskExecuteDependentProcessor(eu.bcvsolutions.idm.core.scheduler.event.processor.LongRunningTaskExecuteDependentProcessor) DependentTaskTrigger(eu.bcvsolutions.idm.core.scheduler.api.dto.DependentTaskTrigger) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) LongRunningTaskEvent(eu.bcvsolutions.idm.core.scheduler.api.event.LongRunningTaskEvent) DateTime(org.joda.time.DateTime) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 53 with IdmLongRunningTaskDto

use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.

the class ExecuteScriptTaskExecutorTest method testExeciteScriptWithResult.

@Test
public void testExeciteScriptWithResult() {
    // remove previous long running task
    List<IdmLongRunningTaskDto> tasks = longRunningTaskService.find(null).getContent();
    for (IdmLongRunningTaskDto task : tasks) {
        longRunningTaskService.delete(task);
    }
    IdmScriptDto scriptDto = new IdmScriptDto();
    scriptDto.setCode(TEST_SCRIPT_CODE + "_3");
    scriptDto.setName(TEST_SCRIPT_CODE + "_3");
    scriptDto.setCategory(IdmScriptCategory.SYSTEM);
    StringBuilder builder = new StringBuilder();
    builder.append("import eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto;\n");
    builder.append("task.setCounter(0l);\n");
    builder.append("task.setCount(5l);\n");
    builder.append("for (int index = 0; index < 5; index++) {\n");
    builder.append("    IdmIdentityDto dto = new IdmIdentityDto();\n");
    builder.append("    dto.setUsername('test-execute-' + index);\n");
    builder.append("    dto.setLastName('test-execute-' + index);\n");
    builder.append("    dto.setFirstName('' + index);\n");
    builder.append("    identityService.save(dto);\n");
    builder.append("    task.increaseCounter();\n");
    builder.append("    task.updateState();\n");
    builder.append("}\n");
    scriptDto.setScript(builder.toString());
    scriptDto = scriptService.save(scriptDto);
    prepareAuthForTestScript(scriptDto);
    IdmScriptAuthorityDto authDto = new IdmScriptAuthorityDto();
    authDto.setType(ScriptAuthorityType.SERVICE);
    authDto.setClassName("eu.bcvsolutions.idm.core.model.service.impl.DefaultIdmIdentityService");
    authDto.setScript(scriptDto.getId());
    authDto.setService("identityService");
    scriptAuthorityService.save(authDto);
    authDto = new IdmScriptAuthorityDto();
    authDto.setType(ScriptAuthorityType.CLASS_NAME);
    authDto.setClassName("eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto");
    authDto.setScript(scriptDto.getId());
    scriptAuthorityService.save(authDto);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("scriptCode", TEST_SCRIPT_CODE + "_3");
    taskExecutor.init(parameters);
    LongRunningFutureTask<Boolean> futureTask = manager.execute(taskExecutor);
    try {
        assertEquals(Boolean.TRUE, futureTask.getFutureTask().get());
    } catch (InterruptedException | ExecutionException e) {
        fail(e.getMessage());
    }
    IdmLongRunningTaskDto longRunningTask = longRunningTaskService.get(taskExecutor.getLongRunningTaskId());
    assertEquals(OperationState.EXECUTED, longRunningTask.getResult().getState());
    assertEquals(5, longRunningTask.getCount().longValue());
    assertEquals(5, longRunningTask.getCounter().longValue());
    IdmIdentityFilter identityFilter = new IdmIdentityFilter();
    identityFilter.setText("test-execute-");
    List<IdmIdentityDto> identities = identityService.find(identityFilter, new PageRequest(0, 20, new Sort(Direction.ASC, IdmIdentity_.firstName.getName()))).getContent();
    assertEquals(5, identities.size());
    for (int index = 0; index < 5; index++) {
        assertEquals(String.valueOf(index), identities.get(index).getFirstName());
    }
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) HashMap(java.util.HashMap) IdmScriptAuthorityDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptAuthorityDto) IdmIdentityFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmIdentityFilter) PageRequest(org.springframework.data.domain.PageRequest) IdmScriptDto(eu.bcvsolutions.idm.core.api.dto.IdmScriptDto) Sort(org.springframework.data.domain.Sort) ExecutionException(java.util.concurrent.ExecutionException) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 54 with IdmLongRunningTaskDto

use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.

the class PasswordExpirationWarningIntegrationTest method testNotSendWarningMessageToDisabledIdentity.

@Test
public void testNotSendWarningMessageToDisabledIdentity() {
    // prepare date
    IdmIdentityDto identity = helper.createIdentity();
    // 
    try {
        IdmPasswordDto password = passwordService.findOneByIdentity(identity.getId());
        password.setValidTill(new LocalDate().plusDays(1));
        passwordService.save(password);
        // disable identity
        identity.setDisabled(true);
        identityService.save(identity);
        // prepare task
        IdmScheduledTaskDto scheduledTask = scheduledTaskService.save(SchedulerTestUtils.createIdmScheduledTask(UUID.randomUUID().toString()));
        IdmLongRunningTaskDto longRunningTask = longRunningService.save(SchedulerTestUtils.createIdmLongRunningTask(scheduledTask, PasswordExpirationWarningTaskExecutor.class));
        PasswordExpirationWarningTaskExecutor executor = AutowireHelper.autowireBean(new PasswordExpirationWarningTaskExecutor());
        executor.setLongRunningTaskId(longRunningTask.getId());
        executor.init(ImmutableMap.of(PasswordExpirationWarningTaskExecutor.PARAMETER_DAYS_BEFORE, "2"));
        // first process
        Boolean result = executor.process();
        Page<IdmProcessedTaskItemDto> logItems = itemService.findLogItems(longRunningTask, null);
        // check
        Assert.assertTrue(result);
        Assert.assertFalse(logItems.getContent().stream().map(IdmProcessedTaskItemDto::getReferencedEntityId).anyMatch(password.getId()::equals));
    } finally {
        identityService.delete(identity);
    }
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) IdmScheduledTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto) IdmPasswordDto(eu.bcvsolutions.idm.core.api.dto.IdmPasswordDto) IdmProcessedTaskItemDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) LocalDate(org.joda.time.LocalDate) Test(org.junit.Test) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)

Example 55 with IdmLongRunningTaskDto

use of eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto in project CzechIdMng by bcvsolutions.

the class PasswordExpiredIntegrationTest method testSimpleWarningMessageDry.

@Test
public void testSimpleWarningMessageDry() {
    // prepare date
    IdmIdentityDto identity = helper.createIdentity();
    // 
    try {
        IdmPasswordDto password = passwordService.findOneByIdentity(identity.getId());
        password.setValidTill(new LocalDate().minusDays(1));
        passwordService.save(password);
        // prepare task
        IdmScheduledTaskDto scheduledTask = scheduledTaskService.save(SchedulerTestUtils.createIdmScheduledTask(UUID.randomUUID().toString()));
        IdmLongRunningTaskDto longRunningTask = longRunningService.save(SchedulerTestUtils.createIdmLongRunningTask(scheduledTask, PasswordExpiredTaskExecutor.class));
        PasswordExpiredTaskExecutor executor = AutowireHelper.autowireBean(new PasswordExpiredTaskExecutor());
        executor.setLongRunningTaskId(longRunningTask.getId());
        executor.init(ImmutableMap.of(PasswordExpirationWarningTaskExecutor.PARAMETER_DAYS_BEFORE, "2"));
        // first process
        Boolean result = executor.process();
        Page<IdmProcessedTaskItemDto> queueItems = itemService.findQueueItems(scheduledTask, null);
        Page<IdmProcessedTaskItemDto> logItems = itemService.findLogItems(longRunningTask, null);
        // first check
        Assert.assertTrue(result);
        Assert.assertTrue(executor.getCount() > 0);
        Assert.assertTrue(queueItems.getTotalElements() > 0);
        Assert.assertTrue(logItems.getTotalElements() > 0);
        Assert.assertTrue(logItems.getContent().stream().map(IdmProcessedTaskItemDto::getReferencedEntityId).anyMatch(password.getId()::equals));
        // second process
        longRunningTask = longRunningService.save(SchedulerTestUtils.createIdmLongRunningTask(scheduledTask, PasswordExpiredTaskExecutor.class));
        executor.setLongRunningTaskId(longRunningTask.getId());
        executor.init(new HashMap<>());
        result = executor.process();
        itemService.findQueueItems(scheduledTask, null);
        logItems = itemService.findLogItems(longRunningTask, null);
        // second check
        Assert.assertTrue(result);
        Assert.assertEquals(Long.valueOf(0), executor.getCount());
        Assert.assertTrue(queueItems.getTotalElements() > 0);
        Assert.assertEquals(0, logItems.getTotalElements());
    } finally {
        identityService.delete(identity);
    }
}
Also used : IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) IdmScheduledTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto) IdmPasswordDto(eu.bcvsolutions.idm.core.api.dto.IdmPasswordDto) IdmProcessedTaskItemDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) LocalDate(org.joda.time.LocalDate) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Aggregations

IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)56 Test (org.junit.Test)32 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)29 IdmScheduledTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmScheduledTaskDto)14 IdmProcessedTaskItemDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmProcessedTaskItemDto)13 IdmLongRunningTaskFilter (eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter)13 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)11 OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)10 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)9 Transactional (org.springframework.transaction.annotation.Transactional)7 LocalDate (org.joda.time.LocalDate)6 LongRunningFutureTask (eu.bcvsolutions.idm.core.scheduler.api.dto.LongRunningFutureTask)5 IdmPasswordDto (eu.bcvsolutions.idm.core.api.dto.IdmPasswordDto)4 Task (eu.bcvsolutions.idm.core.scheduler.api.dto.Task)4 UUID (java.util.UUID)4 OperationState (eu.bcvsolutions.idm.core.api.domain.OperationState)3 DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)3 IdmIdentityContractDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)3 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)3 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)3