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());
}
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);
}
}
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());
}
}
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);
}
}
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);
}
}
Aggregations