Search in sources :

Example 1 with F_ORIGINAL_ASSIGNEE_REF

use of com.evolveum.midpoint.xml.ns._public.common.common_3.CaseWorkItemType.F_ORIGINAL_ASSIGNEE_REF in project midpoint by Evolveum.

the class AbstractWfTestPolicy method executeTest.

protected <F extends FocusType> void executeTest(String testName, TestDetails testDetails, int expectedSubTaskCount) throws Exception {
    // GIVEN
    prepareNotifications();
    dummyAuditService.clear();
    Task modelTask = taskManager.createTaskInstance(AbstractWfTestPolicy.class.getName() + "." + testName);
    modelTask.setOwner(userAdministrator);
    OperationResult result = new OperationResult("execution");
    LensContext<F> modelContext = testDetails.createModelContext(result);
    display("Model context at test start", modelContext);
    // this has problems with deleting assignments by ID
    //assertFocusModificationSanity(modelContext);
    // WHEN
    HookOperationMode mode = clockwork.run(modelContext, modelTask, result);
    // THEN
    display("Model context after first clockwork.run", modelContext);
    assertEquals("Unexpected state of the context", ModelState.PRIMARY, modelContext.getState());
    assertEquals("Wrong mode after clockwork.run in " + modelContext.getState(), HookOperationMode.BACKGROUND, mode);
    modelTask.refresh(result);
    display("Model task after first clockwork.run", modelTask);
    String rootTaskOid = wfTaskUtil.getRootTaskOid(modelTask);
    assertNotNull("Root task OID is not set in model task", rootTaskOid);
    Task rootTask = taskManager.getTask(rootTaskOid, result);
    assertTrue("Root task is not persistent", rootTask.isPersistent());
    UriStack uriStack = rootTask.getOtherHandlersUriStack();
    if (!testDetails.executeImmediately()) {
        assertEquals("Invalid handler at stack position 0", ModelOperationTaskHandler.MODEL_OPERATION_TASK_URI, uriStack.getUriStackEntry().get(0).getHandlerUri());
    } else {
        assertTrue("There should be no handlers for root tasks with immediate execution mode", uriStack == null || uriStack.getUriStackEntry().isEmpty());
    }
    ModelContext rootModelContext = testDetails.executeImmediately() ? null : wfTaskUtil.getModelContext(rootTask, result);
    if (!testDetails.executeImmediately()) {
        assertNotNull("Model context is not present in root task", rootModelContext);
    } else {
        assertNull("Model context is present in root task (execution mode = immediate)", rootModelContext);
    }
    List<Task> subtasks = rootTask.listSubtasks(result);
    Task task0 = findAndRemoveTask0(subtasks, testDetails);
    assertEquals("Incorrect number of subtasks", expectedSubTaskCount, subtasks.size());
    final Collection<SelectorOptions<GetOperationOptions>> options1 = resolveItemsNamed(new ItemPath(T_PARENT, F_OBJECT_REF), new ItemPath(T_PARENT, F_TARGET_REF), F_ASSIGNEE_REF, F_ORIGINAL_ASSIGNEE_REF, new ItemPath(T_PARENT, F_REQUESTER_REF));
    List<WorkItemType> workItems = modelService.searchContainers(WorkItemType.class, null, options1, modelTask, result);
    testDetails.afterFirstClockworkRun(rootTask, subtasks, workItems, result);
    if (testDetails.executeImmediately()) {
        if (task0 != null) {
            waitForTaskClose(task0, 20000);
        }
        testDetails.afterTask0Finishes(rootTask, result);
    }
    for (int i = 0; i < subtasks.size(); i++) {
        Task subtask = subtasks.get(i);
        PrismProperty<ObjectTreeDeltasType> deltas = subtask.getTaskPrismObject().findProperty(new ItemPath(F_WORKFLOW_CONTEXT, F_PROCESSOR_SPECIFIC_STATE, F_DELTAS_TO_PROCESS));
        assertNotNull("There are no modifications in subtask #" + i + ": " + subtask, deltas);
        assertEquals("Incorrect number of modifications in subtask #" + i + ": " + subtask, 1, deltas.getRealValues().size());
        // todo check correctness of the modification?
        // now check the workflow state
        String pid = wfTaskUtil.getProcessId(subtask);
        assertNotNull("Workflow process instance id not present in subtask " + subtask, pid);
        List<org.activiti.engine.task.Task> tasks = activitiEngine.getTaskService().createTaskQuery().processInstanceId(pid).list();
        assertFalse("activiti task not found", tasks.isEmpty());
        for (org.activiti.engine.task.Task task : tasks) {
            String executionId = task.getExecutionId();
            display("Execution id = " + executionId);
            Boolean approve = testDetails.decideOnApproval(executionId, task);
            if (approve != null) {
                workflowManager.completeWorkItem(task.getId(), approve, null, null, null, result);
                login(userAdministrator);
                break;
            }
        }
    }
    // alternative way of approvals executions
    if (CollectionUtils.isNotEmpty(testDetails.getApprovalSequence())) {
        List<ApprovalInstruction> instructions = new ArrayList<>(testDetails.getApprovalSequence());
        while (!instructions.isEmpty()) {
            List<WorkItemType> currentWorkItems = modelService.searchContainers(WorkItemType.class, null, options1, modelTask, result);
            boolean matched = false;
            main: for (ApprovalInstruction approvalInstruction : instructions) {
                for (WorkItemType workItem : currentWorkItems) {
                    if (approvalInstruction.matches(workItem)) {
                        if (approvalInstruction.beforeApproval != null) {
                            approvalInstruction.beforeApproval.run();
                        }
                        login(getUserFromRepo(approvalInstruction.approverOid));
                        workflowManager.completeWorkItem(workItem.getExternalId(), approvalInstruction.approval, null, null, null, result);
                        if (approvalInstruction.afterApproval != null) {
                            approvalInstruction.afterApproval.run();
                        }
                        login(userAdministrator);
                        matched = true;
                        instructions.remove(approvalInstruction);
                        break main;
                    }
                }
            }
            if (!matched) {
                fail("None of approval instructions " + instructions + " matched any of current work items: " + currentWorkItems);
            }
        }
    }
    waitForTaskClose(rootTask, 60000);
    subtasks = rootTask.listSubtasks(result);
    findAndRemoveTask0(subtasks, testDetails);
    testDetails.afterRootTaskFinishes(rootTask, subtasks, result);
    notificationManager.setDisabled(true);
    // Check audit
    display("Audit", dummyAuditService);
    display("Output context", modelContext);
}
Also used : Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ModelContext(com.evolveum.midpoint.model.api.context.ModelContext) HookOperationMode(com.evolveum.midpoint.model.api.hooks.HookOperationMode) WorkItemType(com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemType) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 2 with F_ORIGINAL_ASSIGNEE_REF

use of com.evolveum.midpoint.xml.ns._public.common.common_3.CaseWorkItemType.F_ORIGINAL_ASSIGNEE_REF in project midpoint by Evolveum.

the class TestUserChangeApprovalLegacy method assertWfContextAfterClockworkRun.

protected void assertWfContextAfterClockworkRun(Task rootTask, List<Task> subtasks, OperationResult result, String... processNames) throws Exception {
    final Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(new ItemPath(F_WORKFLOW_CONTEXT, F_WORK_ITEM), createRetrieve());
    Task opTask = taskManager.createTaskInstance();
    TaskType rootTaskType = modelService.getObject(TaskType.class, rootTask.getOid(), options, opTask, result).asObjectable();
    display("rootTask", rootTaskType);
    assertTrue("unexpected process instance id in root task", rootTaskType.getWorkflowContext() == null || rootTaskType.getWorkflowContext().getProcessInstanceId() == null);
    assertEquals("Wrong # of wf subtasks w.r.t processNames (" + Arrays.asList(processNames) + ")", processNames.length, subtasks.size());
    int i = 0;
    for (Task subtask : subtasks) {
        TaskType subtaskType = modelService.getObject(TaskType.class, subtask.getOid(), options, opTask, result).asObjectable();
        display("Subtask #" + (i + 1) + ": ", subtaskType);
        checkTask(subtaskType, subtask.toString(), processNames[i++]);
        WfTestUtil.assertRef("requester ref", subtaskType.getWorkflowContext().getRequesterRef(), USER_ADMINISTRATOR_OID, false, false);
    }
    final Collection<SelectorOptions<GetOperationOptions>> options1 = resolveItemsNamed(new ItemPath(T_PARENT, F_OBJECT_REF), new ItemPath(T_PARENT, F_TARGET_REF), F_ASSIGNEE_REF, F_ORIGINAL_ASSIGNEE_REF, new ItemPath(T_PARENT, F_REQUESTER_REF));
    List<WorkItemType> workItems = modelService.searchContainers(WorkItemType.class, null, options1, opTask, result);
    assertEquals("Wrong # of work items", processNames.length, workItems.size());
    i = 0;
    for (WorkItemType workItem : workItems) {
        display("Work item #" + (i + 1) + ": ", workItem);
        display("Task ref", WfContextUtil.getTask(workItem));
        WfTestUtil.assertRef("object reference", WfContextUtil.getObjectRef(workItem), USER_JACK_OID, true, true);
        WfTestUtil.assertRef("target reference", WfContextUtil.getTargetRef(workItem), ROLE_R1_OID, true, true);
        // name is not known, as it is not stored in activiti (only OID is)
        WfTestUtil.assertRef("assignee reference", workItem.getOriginalAssigneeRef(), R1BOSS_OID, false, true);
        //WfTestUtil.assertRef("task reference", workItem.getTaskRef(), null, false, true);
        final TaskType subtaskType = WfContextUtil.getTask(workItem);
        checkTask(subtaskType, "task in workItem", processNames[i++]);
        WfTestUtil.assertRef("requester ref", subtaskType.getWorkflowContext().getRequesterRef(), USER_ADMINISTRATOR_OID, false, true);
    }
}
Also used : Task(com.evolveum.midpoint.task.api.Task) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) WorkItemType(com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemType)

Example 3 with F_ORIGINAL_ASSIGNEE_REF

use of com.evolveum.midpoint.xml.ns._public.common.common_3.CaseWorkItemType.F_ORIGINAL_ASSIGNEE_REF in project midpoint by Evolveum.

the class AbstractWfTestPolicy method executeTest.

protected <F extends FocusType> OperationResult executeTest(TestDetails testDetails, int expectedSubTaskCount) throws Exception {
    // GIVEN
    prepareNotifications();
    dummyAuditService.clear();
    Task opTask = getTestTask();
    boolean USE_FULL_TRACING = false;
    // noinspection ConstantConditions
    if (USE_FULL_TRACING) {
        setModelAndWorkflowLoggingTracing(opTask);
    } else {
        testDetails.setTracing(opTask);
    }
    opTask.setOwner(userAdministrator);
    OperationResult result = opTask.getResult();
    LensContext<F> modelContext = testDetails.createModelContext(result);
    displayDumpable("Model context at test start", modelContext);
    // this has problems with deleting assignments by ID
    // assertFocusModificationSanity(modelContext);
    // WHEN
    HookOperationMode mode;
    clockworkMedic.enterModelMethod(true);
    try {
        mode = clockwork.run(modelContext, opTask, result);
    } finally {
        clockworkMedic.exitModelMethod(true);
    }
    // THEN
    displayDumpable("Model context after first clockwork.run", modelContext);
    assertEquals("Unexpected state of the context", ModelState.PRIMARY, modelContext.getState());
    assertEquals("Wrong mode after clockwork.run in " + modelContext.getState(), HookOperationMode.BACKGROUND, mode);
    opTask.refresh(result);
    display("Model task after first clockwork.run", opTask);
    CaseType rootCase = testHelper.getRootCase(result);
    List<CaseType> subcases = miscHelper.getSubcases(rootCase, result);
    CaseType case0 = WfTestHelper.findAndRemoveCase0(subcases);
    assertEquals("Incorrect number of subtasks", expectedSubTaskCount, subcases.size());
    final Collection<SelectorOptions<GetOperationOptions>> options1 = schemaService.getOperationOptionsBuilder().item(T_PARENT, F_OBJECT_REF).resolve().item(T_PARENT, F_TARGET_REF).resolve().item(F_ASSIGNEE_REF).resolve().item(F_ORIGINAL_ASSIGNEE_REF).resolve().item(T_PARENT, F_REQUESTOR_REF).resolve().build();
    List<CaseWorkItemType> workItems = new // to assure modifiable result list
    ArrayList<>(modelService.searchContainers(CaseWorkItemType.class, getOpenItemsQuery(), options1, opTask, result));
    displayDumpable("changes by state after first clockwork run", approvalsManager.getChangesByState(rootCase, modelInteractionService, prismContext, opTask, result));
    testDetails.afterFirstClockworkRun(rootCase, case0, subcases, workItems, opTask, result);
    if (testDetails.executeImmediately()) {
        if (case0 != null) {
            testHelper.waitForCaseClose(case0, 20000);
        }
        displayDumpable("changes by state after case0 finishes", approvalsManager.getChangesByState(rootCase, modelInteractionService, prismContext, opTask, result));
        testDetails.afterCase0Finishes(rootCase, opTask, result);
    }
    for (int i = 0; i < subcases.size(); i++) {
        CaseType subcase = subcases.get(i);
        PrismProperty<ObjectTreeDeltasType> deltas = subcase.asPrismObject().findProperty(ItemPath.create(F_APPROVAL_CONTEXT, F_DELTAS_TO_APPROVE));
        assertNotNull("There are no modifications in subcase #" + i + ": " + subcase, deltas);
        assertEquals("Incorrect number of modifications in subcase #" + i + ": " + subcase, 1, deltas.getRealValues().size());
        // todo check correctness of the modification?
        // now check the workflow state
        String caseOid = subcase.getOid();
        List<CaseWorkItemType> caseWorkItems = getWorkItemsForCase(caseOid, null, result);
        assertFalse("work item not found", caseWorkItems.isEmpty());
        for (CaseWorkItemType caseWorkItem : caseWorkItems) {
            Boolean approve = testDetails.decideOnApproval(caseWorkItem);
            if (approve != null) {
                caseManager.completeWorkItem(WorkItemId.create(caseOid, caseWorkItem.getId()), new AbstractWorkItemOutputType(prismContext).outcome(ApprovalUtils.toUri(approve)), null, opTask, result);
                login(userAdministrator);
                break;
            }
        }
    }
    // alternative way of approvals executions
    if (CollectionUtils.isNotEmpty(testDetails.getApprovalSequence())) {
        List<ApprovalInstruction> instructions = new ArrayList<>(testDetails.getApprovalSequence());
        while (!instructions.isEmpty()) {
            List<CaseWorkItemType> currentWorkItems = modelService.searchContainers(CaseWorkItemType.class, getOpenItemsQuery(), options1, opTask, result);
            boolean matched = false;
            Collection<ApprovalInstruction> instructionsToConsider = testDetails.strictlySequentialApprovals() ? singleton(instructions.get(0)) : instructions;
            main: for (ApprovalInstruction approvalInstruction : instructionsToConsider) {
                for (CaseWorkItemType workItem : currentWorkItems) {
                    if (approvalInstruction.matches(workItem)) {
                        if (approvalInstruction.beforeApproval != null) {
                            approvalInstruction.beforeApproval.run();
                        }
                        login(getUserFromRepo(approvalInstruction.approverOid));
                        System.out.println("Completing work item " + WorkItemId.of(workItem) + " using " + approvalInstruction);
                        caseManager.completeWorkItem(WorkItemId.of(workItem), new AbstractWorkItemOutputType(prismContext).outcome(ApprovalUtils.toUri(approvalInstruction.approval)).comment(approvalInstruction.comment), null, opTask, result);
                        if (approvalInstruction.afterApproval != null) {
                            approvalInstruction.afterApproval.run();
                        }
                        login(userAdministrator);
                        matched = true;
                        instructions.remove(approvalInstruction);
                        break main;
                    }
                }
            }
            if (!matched) {
                fail("None of approval instructions " + instructionsToConsider + " matched any of current work items: " + currentWorkItems);
            }
        }
    }
    CaseType rootCaseAfter = testHelper.waitForCaseClose(rootCase, 60000);
    subcases = miscHelper.getSubcases(rootCaseAfter, result);
    WfTestHelper.findAndRemoveCase0(subcases);
    displayDumpable("changes by state after root case finishes", approvalsManager.getChangesByState(rootCaseAfter, modelInteractionService, prismContext, opTask, result));
    testDetails.afterRootCaseFinishes(rootCaseAfter, subcases, opTask, result);
    notificationManager.setDisabled(true);
    // Check audit
    displayDumpable("Audit", dummyAuditService);
    displayDumpable("Output context", modelContext);
    return result;
}
Also used : Task(com.evolveum.midpoint.task.api.Task) F_ASSIGNEE_REF(com.evolveum.midpoint.xml.ns._public.common.common_3.CaseWorkItemType.F_ASSIGNEE_REF) F_ORIGINAL_ASSIGNEE_REF(com.evolveum.midpoint.xml.ns._public.common.common_3.CaseWorkItemType.F_ORIGINAL_ASSIGNEE_REF) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) HookOperationMode(com.evolveum.midpoint.model.api.hooks.HookOperationMode) CaseType(com.evolveum.midpoint.xml.ns._public.common.common_3.CaseType) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions)

Aggregations

SelectorOptions (com.evolveum.midpoint.schema.SelectorOptions)3 Task (com.evolveum.midpoint.task.api.Task)3 HookOperationMode (com.evolveum.midpoint.model.api.hooks.HookOperationMode)2 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)2 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 WorkItemType (com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemType)2 ModelContext (com.evolveum.midpoint.model.api.context.ModelContext)1 CaseType (com.evolveum.midpoint.xml.ns._public.common.common_3.CaseType)1 F_ASSIGNEE_REF (com.evolveum.midpoint.xml.ns._public.common.common_3.CaseWorkItemType.F_ASSIGNEE_REF)1 F_ORIGINAL_ASSIGNEE_REF (com.evolveum.midpoint.xml.ns._public.common.common_3.CaseWorkItemType.F_ORIGINAL_ASSIGNEE_REF)1