Search in sources :

Example 1 with ModelContext

use of com.evolveum.midpoint.model.api.context.ModelContext in project midpoint by Evolveum.

the class TestUserChangeApprovalLegacy method test090UserModifyModifyResourceAssignmentValidity.

/**
     * User modification: modifying validity of single security-sensitive resource assignment.
     */
@Test(enabled = true)
public void test090UserModifyModifyResourceAssignmentValidity() throws Exception {
    TestUtil.displayTestTile(this, "test090UserModifyModifyResourceAssignmentValidity");
    login(userAdministrator);
    final XMLGregorianCalendar validFrom = XmlTypeConverter.createXMLGregorianCalendar(2015, 2, 25, 10, 0, 0);
    final XMLGregorianCalendar validTo = XmlTypeConverter.createXMLGregorianCalendar(2015, 3, 25, 10, 0, 0);
    executeTest("test090UserModifyModifyResourceAssignmentValidity", USER_JACK_OID, new TestDetails() {

        @Override
        int subtaskCount() {
            return 1;
        }

        @Override
        boolean immediate() {
            return false;
        }

        @Override
        boolean checkObjectOnSubtasks() {
            return true;
        }

        @Override
        boolean removeAssignmentsBeforeTest() {
            return false;
        }

        @Override
        public LensContext createModelContext(Task task, OperationResult result) throws Exception {
            LensContext<UserType> context = createUserLensContext();
            fillContextWithUser(context, USER_JACK_OID, result);
            UserType jack = context.getFocusContext().getObjectOld().asObjectable();
            modifyAssignmentValidity(context, jack, validFrom, validTo);
            return context;
        }

        @Override
        public void assertsAfterClockworkRun(Task rootTask, List<Task> wfSubtasks, OperationResult result) throws Exception {
            ModelContext taskModelContext = wfTaskUtil.getModelContext(rootTask, result);
            assertEquals("There are modifications left in primary focus delta", 0, taskModelContext.getFocusContext().getPrimaryDelta().getModifications().size());
            UserType jack = getUser(USER_JACK_OID).asObjectable();
            checkNoAssignmentValidity(jack);
        }

        @Override
        void assertsRootTaskFinishes(Task task, List<Task> subtasks, OperationResult result) throws Exception {
            assertAssignedResource(USER_JACK_OID, RESOURCE_DUMMY_OID, task, result);
            UserType jack = getUser(USER_JACK_OID).asObjectable();
            checkAssignmentValidity(jack, validFrom, validTo);
            checkDummyTransportMessages("simpleUserNotifier", 1);
            //checkWorkItemAuditRecords(createResultMap(ROLE_R1_OID, WorkflowResult.APPROVED));
            checkUserApprovers(USER_JACK_OID, Arrays.asList(DUMMYBOSS_OID), result);
        }

        @Override
        boolean decideOnApproval(String executionId) throws Exception {
            login(getUser(DUMMYBOSS_OID));
            return true;
        }
    });
}
Also used : ModelContext(com.evolveum.midpoint.model.api.context.ModelContext) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) LensContext(com.evolveum.midpoint.model.impl.lens.LensContext) JAXBException(javax.xml.bind.JAXBException) Test(org.testng.annotations.Test)

Example 2 with ModelContext

use of com.evolveum.midpoint.model.api.context.ModelContext in project midpoint by Evolveum.

the class TestUserChangeApprovalLegacy method test041UserModifyPasswordChange.

@Test(enabled = true)
public void test041UserModifyPasswordChange() throws Exception {
    TestUtil.displayTestTile(this, "test041UserModifyPasswordChange");
    login(userAdministrator);
    PrismObject<UserType> jack = getUser(USER_JACK_OID);
    final ProtectedStringType originalPasswordValue = jack.asObjectable().getCredentials().getPassword().getValue();
    LOGGER.trace("password before test = " + originalPasswordValue);
    executeTest("test041UserModifyPasswordChange", USER_JACK_OID, new TestDetails() {

        @Override
        int subtaskCount() {
            return 1;
        }

        @Override
        boolean immediate() {
            return false;
        }

        @Override
        boolean checkObjectOnSubtasks() {
            return true;
        }

        @Override
        public LensContext createModelContext(Task task, OperationResult result) throws Exception {
            LensContext<UserType> context = createUserLensContext();
            fillContextWithUser(context, USER_JACK_OID, result);
            encryptAndAddFocusModificationToContext(context, REQ_USER_JACK_MODIFY_CHANGE_PASSWORD);
            //context.setOptions(ModelExecuteOptions.createNoCrypt());
            return context;
        }

        @Override
        public void assertsAfterClockworkRun(Task rootTask, List<Task> wfSubtasks, OperationResult result) throws Exception {
            ModelContext taskModelContext = wfTaskUtil.getModelContext(rootTask, result);
            assertEquals("There are modifications left in primary focus delta", 0, taskModelContext.getFocusContext().getPrimaryDelta().getModifications().size());
        }

        @Override
        void assertsRootTaskFinishes(Task task, List<Task> subtasks, OperationResult result) throws Exception {
            PrismObject<UserType> jack = getUser(USER_JACK_OID);
            ProtectedStringType afterTestPasswordValue = jack.asObjectable().getCredentials().getPassword().getValue();
            LOGGER.trace("password after test = " + afterTestPasswordValue);
            //assertNotNull("password was not set", afterTestPasswordValue.getEncryptedData());
            assertFalse("password was not changed", originalPasswordValue.getEncryptedDataType().equals(afterTestPasswordValue.getEncryptedDataType()));
            checkDummyTransportMessages("simpleUserNotifier", 1);
        }

        @Override
        boolean decideOnApproval(String executionId) throws Exception {
            login(getUser(USER_ADMINISTRATOR_OID));
            return true;
        }
    });
}
Also used : Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) LensContext(com.evolveum.midpoint.model.impl.lens.LensContext) JAXBException(javax.xml.bind.JAXBException) ModelContext(com.evolveum.midpoint.model.api.context.ModelContext) ProtectedStringType(com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType) Test(org.testng.annotations.Test)

Example 3 with ModelContext

use of com.evolveum.midpoint.model.api.context.ModelContext in project midpoint by Evolveum.

the class TestUserChangeApprovalLegacy method test050UserModifyAddRoleAndPasswordChange.

@Test(enabled = true)
public void test050UserModifyAddRoleAndPasswordChange() throws Exception {
    TestUtil.displayTestTile(this, "test050UserModifyAddRoleAndPasswordChange");
    login(userAdministrator);
    PrismObject<UserType> jack = getUser(USER_JACK_OID);
    final ProtectedStringType originalPasswordValue = jack.asObjectable().getCredentials().getPassword().getValue();
    LOGGER.trace("password before test = " + originalPasswordValue);
    executeTest("test050UserModifyAddRoleAndPasswordChange", USER_JACK_OID, new TestDetails() {

        @Override
        int subtaskCount() {
            return 2;
        }

        @Override
        boolean immediate() {
            return false;
        }

        @Override
        boolean checkObjectOnSubtasks() {
            return true;
        }

        @Override
        public LensContext createModelContext(Task task, OperationResult result) throws Exception {
            LensContext<UserType> context = createUserLensContext();
            fillContextWithUser(context, USER_JACK_OID, result);
            encryptAndAddFocusModificationToContext(context, REQ_USER_JACK_MODIFY_CHANGE_PASSWORD_2);
            addFocusModificationToContext(context, REQ_USER_JACK_MODIFY_ADD_ASSIGNMENT_ROLE1);
            //context.setOptions(ModelExecuteOptions.createNoCrypt());
            return context;
        }

        @Override
        public void assertsAfterClockworkRun(Task rootTask, List<Task> wfSubtasks, OperationResult result) throws Exception {
            ModelContext taskModelContext = wfTaskUtil.getModelContext(rootTask, result);
            assertEquals("There are modifications left in primary focus delta", 0, taskModelContext.getFocusContext().getPrimaryDelta().getModifications().size());
        }

        @Override
        void assertsRootTaskFinishes(Task task, List<Task> subtasks, OperationResult result) throws Exception {
            PrismObject<UserType> jack = getUser(USER_JACK_OID);
            ProtectedStringType afterTestPasswordValue = jack.asObjectable().getCredentials().getPassword().getValue();
            LOGGER.trace("password after test = " + afterTestPasswordValue);
            // todo why is password value not set?
            //assertNotNull("password was not set", afterTestPasswordValue.getEncryptedData());
            //assertFalse("password was not changed", originalPasswordValue.getEncryptedData().equals(afterTestPasswordValue.getEncryptedData()));
            assertAssignedRole(jack, ROLE_R1_OID);
            checkDummyTransportMessages("simpleUserNotifier", 1);
        }

        @Override
        boolean decideOnApproval(String executionId) throws Exception {
            LightweightObjectRef targetRef = (LightweightObjectRef) activitiEngine.getRuntimeService().getVariable(executionId, CommonProcessVariableNames.VARIABLE_TARGET_REF);
            if (targetRef != null && RoleType.COMPLEX_TYPE.equals(targetRef.toObjectReferenceType().getType())) {
                return decideOnRoleApproval(executionId);
            } else {
                login(getUser(USER_ADMINISTRATOR_OID));
                return true;
            }
        }
    });
}
Also used : Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) LensContext(com.evolveum.midpoint.model.impl.lens.LensContext) JAXBException(javax.xml.bind.JAXBException) LightweightObjectRef(com.evolveum.midpoint.wf.impl.processes.common.LightweightObjectRef) ModelContext(com.evolveum.midpoint.model.api.context.ModelContext) ProtectedStringType(com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType) Test(org.testng.annotations.Test)

Example 4 with ModelContext

use of com.evolveum.midpoint.model.api.context.ModelContext in project midpoint by Evolveum.

the class TestUserChangeApprovalLegacy method test070UserModifyAssignment.

@Test(enabled = true)
public void test070UserModifyAssignment() throws Exception {
    TestUtil.displayTestTile(this, "test070UserModifyAssignment");
    login(userAdministrator);
    removeAllAssignments(USER_JACK_OID, new OperationResult("dummy"));
    assignRoleRaw(USER_JACK_OID, ROLE_R1_OID);
    final XMLGregorianCalendar validFrom = XmlTypeConverter.createXMLGregorianCalendar(2015, 2, 25, 10, 0, 0);
    final XMLGregorianCalendar validTo = XmlTypeConverter.createXMLGregorianCalendar(2015, 3, 25, 10, 0, 0);
    executeTest("test070UserModifyAssignment", USER_JACK_OID, new TestDetails() {

        @Override
        int subtaskCount() {
            return 1;
        }

        @Override
        boolean immediate() {
            return false;
        }

        @Override
        boolean checkObjectOnSubtasks() {
            return true;
        }

        @Override
        boolean removeAssignmentsBeforeTest() {
            return false;
        }

        @Override
        public LensContext createModelContext(Task task, OperationResult result) throws Exception {
            LensContext<UserType> context = createUserLensContext();
            fillContextWithUser(context, USER_JACK_OID, result);
            UserType jack = context.getFocusContext().getObjectOld().asObjectable();
            modifyAssignmentValidity(context, jack, validFrom, validTo);
            return context;
        }

        @Override
        public void assertsAfterClockworkRun(Task rootTask, List<Task> wfSubtasks, OperationResult result) throws Exception {
            ModelContext taskModelContext = wfTaskUtil.getModelContext(rootTask, result);
            assertEquals("There are modifications left in primary focus delta", 0, taskModelContext.getFocusContext().getPrimaryDelta().getModifications().size());
            UserType jack = getUser(USER_JACK_OID).asObjectable();
            checkNoAssignmentValidity(jack);
        }

        @Override
        void assertsRootTaskFinishes(Task task, List<Task> subtasks, OperationResult result) throws Exception {
            UserType jack = getUser(USER_JACK_OID).asObjectable();
            checkAssignmentValidity(jack, validFrom, validTo);
        // TODO
        //checkDummyTransportMessages("simpleUserNotifier", 1);
        }

        @Override
        boolean decideOnApproval(String executionId) throws Exception {
            login(getUser(R1BOSS_OID));
            return true;
        }
    });
}
Also used : ModelContext(com.evolveum.midpoint.model.api.context.ModelContext) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) LensContext(com.evolveum.midpoint.model.impl.lens.LensContext) JAXBException(javax.xml.bind.JAXBException) Test(org.testng.annotations.Test)

Example 5 with ModelContext

use of com.evolveum.midpoint.model.api.context.ModelContext 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)

Aggregations

ModelContext (com.evolveum.midpoint.model.api.context.ModelContext)23 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)13 Task (com.evolveum.midpoint.task.api.Task)11 LensContext (com.evolveum.midpoint.model.impl.lens.LensContext)8 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)8 JAXBException (javax.xml.bind.JAXBException)6 Test (org.testng.annotations.Test)6 ModelInteractionService (com.evolveum.midpoint.model.api.ModelInteractionService)4 ModelService (com.evolveum.midpoint.model.api.ModelService)4 CommonException (com.evolveum.midpoint.util.exception.CommonException)4 MidPointApplication (com.evolveum.midpoint.web.security.MidPointApplication)4 HookOperationMode (com.evolveum.midpoint.model.api.hooks.HookOperationMode)3 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)3 ProtectedStringType (com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType)3 ModelEvent (com.evolveum.midpoint.notifications.api.events.ModelEvent)2 PrismObject (com.evolveum.midpoint.prism.PrismObject)2 ObjectDeltaOperation (com.evolveum.midpoint.schema.ObjectDeltaOperation)2 HttpConnectionInformation (com.evolveum.midpoint.security.api.HttpConnectionInformation)2 SecurityContextManager (com.evolveum.midpoint.security.api.SecurityContextManager)2