use of eu.bcvsolutions.idm.core.scheduler.task.impl.ProcessAutomaticRoleByTreeTaskExecutor in project CzechIdMng by bcvsolutions.
the class TreeNodeAfterMoveAutomaticRoleProcessor method process.
@Override
public EventResult<IdmTreeNodeDto> process(EntityEvent<IdmTreeNodeDto> event) {
IdmTreeNodeDto treeNode = event.getContent();
//
// preserve order => new automatic roles first
Set<UUID> automaticRoles = new LinkedHashSet<>();
// find currently defined automatic roles
Set<IdmRoleTreeNodeDto> newAutomaticRoles = getRoleTreeNodeService().getAutomaticRolesByTreeNode(treeNode.getId());
if (CollectionUtils.isNotEmpty(newAutomaticRoles)) {
automaticRoles.addAll(newAutomaticRoles.stream().map(IdmRoleTreeNodeDto::getId).collect(Collectors.toSet()));
}
// previously defined automatic roles
Set<UUID> previousAutomaticRoles = event.getSetProperty(PROPERTY_PREVIOUS_AUTOMATIC_ROLES, UUID.class);
if (CollectionUtils.isNotEmpty(previousAutomaticRoles)) {
automaticRoles.addAll(previousAutomaticRoles);
}
//
if (CollectionUtils.isEmpty(automaticRoles)) {
LOG.debug("Tree node [{}] was moved under new parent node [{}]. No automatic roles are affected.", treeNode.getId(), treeNode.getParent());
//
return new DefaultEventResult<>(event, this);
}
// flag can be processed afterwards
if (getBooleanProperty(AutomaticRoleManager.SKIP_RECALCULATION, event.getProperties())) {
automaticRoles.forEach(automaticRole -> {
LOG.debug("Automatic role [{}] recount is skipped after tree node [{}] was moved in tree structure. " + "State [AUTOMATIC_ROLE_SKIPPED] for automatic role will be created only.", automaticRole, treeNode.getId());
//
IdmEntityStateDto state = new IdmEntityStateDto();
state.setOwnerId(automaticRole);
state.setOwnerType(entityStateManager.getOwnerType(IdmRoleTreeNodeDto.class));
state.setResult(new OperationResultDto.Builder(OperationState.BLOCKED).setModel(new DefaultResultModel(CoreResultCode.AUTOMATIC_ROLE_SKIPPED)).build());
entityStateManager.saveState(null, state);
});
//
return new DefaultEventResult<>(event, this);
}
//
// process all affected automatic roles
ProcessAutomaticRoleByTreeTaskExecutor automaticRoleTask = AutowireHelper.createBean(ProcessAutomaticRoleByTreeTaskExecutor.class);
automaticRoleTask.setAutomaticRoles(Lists.newArrayList(automaticRoles));
executeTask(event, automaticRoleTask);
//
return new DefaultEventResult<>(event, this);
}
use of eu.bcvsolutions.idm.core.scheduler.task.impl.ProcessAutomaticRoleByTreeTaskExecutor in project CzechIdMng by bcvsolutions.
the class DefaultIdmTreeNodeServiceIntegrationTest method testAssignAutomaticRoleAfterNodeIsMovedWithDownRecursion.
@Test
public void testAssignAutomaticRoleAfterNodeIsMovedWithDownRecursion() {
IdmTreeNodeDto parentNode = getHelper().createTreeNode();
IdmTreeNodeDto node = getHelper().createTreeNode();
// define automatic role for parent
IdmRoleDto role = getHelper().createRole();
IdmRoleTreeNodeDto automaticRole = getHelper().createRoleTreeNode(role, parentNode, RecursionType.DOWN, true);
// create identity with contract on node
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
getHelper().createContract(identity, node);
// no role should be assigned now
List<IdmIdentityRoleDto> assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertTrue(assignedRoles.isEmpty());
//
node.setParent(parentNode.getId());
node = service.save(node);
//
assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertEquals(1, assignedRoles.size());
Assert.assertEquals(automaticRole.getId(), assignedRoles.get(0).getAutomaticRole());
//
IdmTreeNodeDto otherNode = getHelper().createTreeNode();
IdmRoleTreeNodeDto otherAutomaticRole = getHelper().createRoleTreeNode(role, otherNode, RecursionType.DOWN, true);
node.setParent(otherNode.getId());
node = service.save(node);
//
assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
UUID assignedRoleId = assignedRoles.get(0).getId();
Assert.assertEquals(1, assignedRoles.size());
Assert.assertEquals(otherAutomaticRole.getId(), assignedRoles.get(0).getAutomaticRole());
//
// recalculate role => nothing happend
ProcessAutomaticRoleByTreeTaskExecutor automaticRoleTask = AutowireHelper.createBean(ProcessAutomaticRoleByTreeTaskExecutor.class);
automaticRoleTask.setAutomaticRoles(Lists.newArrayList(otherAutomaticRole.getId()));
longRunningTaskManager.execute(automaticRoleTask);
//
assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertEquals(1, assignedRoles.size());
Assert.assertEquals(otherAutomaticRole.getId(), assignedRoles.get(0).getAutomaticRole());
Assert.assertEquals(assignedRoleId, assignedRoles.get(0).getId());
//
// move node deeper in sub tree => nothing should happend
IdmTreeNodeDto subNode = getHelper().createTreeNode(null, null, getHelper().createTreeNode(null, null, otherNode));
node.setParent(subNode.getId());
node = service.save(node);
//
assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertEquals(1, assignedRoles.size());
Assert.assertEquals(otherAutomaticRole.getId(), assignedRoles.get(0).getAutomaticRole());
Assert.assertEquals(assignedRoleId, assignedRoles.get(0).getId());
}
use of eu.bcvsolutions.idm.core.scheduler.task.impl.ProcessAutomaticRoleByTreeTaskExecutor in project CzechIdMng by bcvsolutions.
the class DefaultIdmIdentityContractServiceIntegrationTest method saveAutomaticRole.
/**
* Save automatic role with repository and manual create and wait for task
* @param automaticRole
* @return
*/
private IdmRoleTreeNodeDto saveAutomaticRole(IdmRoleTreeNodeDto automaticRole, boolean withLongRunningTask) {
// default name
automaticRole.setName("default");
IdmRoleTreeNodeDto roleTreeNode = roleTreeNodeService.saveInternal(automaticRole);
//
if (withLongRunningTask) {
ProcessAutomaticRoleByTreeTaskExecutor task = new ProcessAutomaticRoleByTreeTaskExecutor();
task.setAutomaticRoles(Lists.newArrayList(roleTreeNode.getId()));
taskManager.executeSync(task);
}
//
return roleTreeNodeService.get(roleTreeNode.getId());
}
use of eu.bcvsolutions.idm.core.scheduler.task.impl.ProcessAutomaticRoleByTreeTaskExecutor in project CzechIdMng by bcvsolutions.
the class DefaultIdmIdentityContractServiceIntegrationTest method testAcceptSimultaneousAutomaticRoleTask.
@Test(expected = AcceptedException.class)
public void testAcceptSimultaneousAutomaticRoleTask() {
IdmRoleTreeNodeDto automaticRole = getHelper().createRoleTreeNode(getHelper().createRole(), getHelper().createTreeNode(), true);
ProcessAutomaticRoleByTreeTaskExecutor taskExecutor = AutowireHelper.createBean(ProcessAutomaticRoleByTreeTaskExecutor.class);
taskExecutor.setAutomaticRoles(Lists.newArrayList(automaticRole.getId()));
//
IdmLongRunningTaskDto lrt = null;
try {
lrt = longRunningTaskManager.resolveLongRunningTask(taskExecutor, null, OperationState.RUNNING);
lrt.setRunning(true);
longRunningTaskService.save(lrt);
//
taskExecutor = AutowireHelper.createBean(ProcessAutomaticRoleByTreeTaskExecutor.class);
taskExecutor.setAutomaticRoles(Lists.newArrayList(automaticRole.getId()));
//
longRunningTaskManager.execute(taskExecutor);
} finally {
lrt.setRunning(false);
lrt = longRunningTaskService.save(lrt);
//
longRunningTaskService.delete(lrt);
}
}
use of eu.bcvsolutions.idm.core.scheduler.task.impl.ProcessAutomaticRoleByTreeTaskExecutor in project CzechIdMng by bcvsolutions.
the class DefaultIdmTreeNodeServiceIntegrationTest method testAssignAutomaticRoleOnPositionAfterNodeIsMovedWithDownRecursion.
@Test
public void testAssignAutomaticRoleOnPositionAfterNodeIsMovedWithDownRecursion() {
IdmTreeNodeDto parentNode = getHelper().createTreeNode();
IdmTreeNodeDto node = getHelper().createTreeNode();
// define automatic role for parent
IdmRoleDto role = getHelper().createRole();
IdmRoleTreeNodeDto automaticRole = getHelper().createRoleTreeNode(role, parentNode, RecursionType.DOWN, true);
// create identity with contract on node
IdmIdentityDto identity = getHelper().createIdentity((GuardedString) null);
getHelper().createContractPosition(getHelper().getPrimeContract(identity), node);
// no role should be assigned now
List<IdmIdentityRoleDto> assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertTrue(assignedRoles.isEmpty());
//
node.setParent(parentNode.getId());
node = service.save(node);
//
assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertEquals(1, assignedRoles.size());
Assert.assertEquals(automaticRole.getId(), assignedRoles.get(0).getAutomaticRole());
//
IdmTreeNodeDto otherNode = getHelper().createTreeNode();
IdmRoleTreeNodeDto otherAutomaticRole = getHelper().createRoleTreeNode(role, otherNode, RecursionType.DOWN, true);
node.setParent(otherNode.getId());
node = service.save(node);
//
assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
UUID assignedRoleId = assignedRoles.get(0).getId();
Assert.assertEquals(1, assignedRoles.size());
Assert.assertEquals(otherAutomaticRole.getId(), assignedRoles.get(0).getAutomaticRole());
//
// recalculate role => nothing happend
ProcessAutomaticRoleByTreeTaskExecutor automaticRoleTask = AutowireHelper.createBean(ProcessAutomaticRoleByTreeTaskExecutor.class);
automaticRoleTask.setAutomaticRoles(Lists.newArrayList(otherAutomaticRole.getId()));
longRunningTaskManager.execute(automaticRoleTask);
//
assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertEquals(1, assignedRoles.size());
Assert.assertEquals(otherAutomaticRole.getId(), assignedRoles.get(0).getAutomaticRole());
Assert.assertEquals(assignedRoleId, assignedRoles.get(0).getId());
//
// move node deeper in sub tree => nothing should happend
IdmTreeNodeDto subNode = getHelper().createTreeNode(null, null, getHelper().createTreeNode(null, null, otherNode));
node.setParent(subNode.getId());
node = service.save(node);
//
assignedRoles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertEquals(1, assignedRoles.size());
Assert.assertEquals(otherAutomaticRole.getId(), assignedRoles.get(0).getAutomaticRole());
Assert.assertEquals(assignedRoleId, assignedRoles.get(0).getId());
}
Aggregations