Search in sources :

Example 41 with LensContext

use of com.evolveum.midpoint.model.impl.lens.LensContext in project midpoint by Evolveum.

the class TestObjectLifecycleApprovalGlobal method test610DeleteInducementsFromCaptain.

@Test
public void test610DeleteInducementsFromCaptain() throws Exception {
    login(userAdministrator);
    PrismObject<RoleType> captainBefore = getRole(roleCaptainOid);
    ObjectDelta<RoleType> captainDelta = prismContext.deltaFor(RoleType.class).item(RoleType.F_INDUCEMENT).delete(cloneCollectionMembers(captainBefore.findContainer(RoleType.F_INDUCEMENT).getValues())).asObjectDeltaCast(roleCaptainOid);
    executeTest(new TestDetails() {

        @Override
        protected LensContext createModelContext(OperationResult result) throws Exception {
            LensContext<RoleType> lensContext = createLensContext(RoleType.class);
            addFocusDeltaToContext(lensContext, captainDelta);
            lensContext.setOptions(executeOptions().executeImmediatelyAfterApproval());
            return lensContext;
        }

        @Override
        protected void afterFirstClockworkRun(CaseType rootCase, CaseType case0, List<CaseType> subcases, List<CaseWorkItemType> workItems, Task opTask, OperationResult result) {
            display("subtasks", subcases);
            display("work items", workItems);
        // todo some asserts here
        }

        @Override
        protected void afterCase0Finishes(CaseType rootCase, Task opTask, OperationResult result) {
        // nothing here
        }

        @Override
        protected void afterRootCaseFinishes(CaseType rootCase, List<CaseType> subcases, Task opTask, OperationResult result) {
        // nothing here
        }

        @Override
        protected boolean executeImmediately() {
            return true;
        }

        @Override
        public boolean strictlySequentialApprovals() {
            return true;
        }

        @SuppressWarnings("Duplicates")
        @Override
        public List<ApprovalInstruction> getApprovalSequence() {
            List<ApprovalInstruction> instructions = new ArrayList<>();
            // this is step 1 in inducement part
            instructions.add(new ApprovalInstruction(new ExpectedWorkItem(USER_ADMINISTRATOR_OID, null, new ExpectedTask(null, "Changing inducement")), true, USER_ADMINISTRATOR_OID, null));
            // this is step 2 in inducement part
            instructions.add(new ApprovalInstruction(new ExpectedWorkItem(USER_BOB_OID, null, new ExpectedTask(null, "Changing inducement")), true, USER_ADMINISTRATOR_OID, null));
            // this is step 1 in inducement part (2nd)
            instructions.add(new ApprovalInstruction(new ExpectedWorkItem(USER_ADMINISTRATOR_OID, null, new ExpectedTask(null, "Changing inducement")), true, USER_ADMINISTRATOR_OID, null));
            // this is step 2 in inducement part (2nd)
            instructions.add(new ApprovalInstruction(new ExpectedWorkItem(USER_BOB_OID, null, new ExpectedTask(null, "Changing inducement")), true, USER_ADMINISTRATOR_OID, null));
            return instructions;
        }
    }, 2);
    // TODO some more asserts
    PrismObject<RoleType> captainAfter = getRole(roleCaptainOid);
    display("Captain role", captainAfter);
    assertEquals("Wrong risk level", "high", captainAfter.asObjectable().getRiskLevel());
    assertInducements(captainAfter, 0);
}
Also used : ExpectedTask(com.evolveum.midpoint.wf.impl.ExpectedTask) Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ExpectedWorkItem(com.evolveum.midpoint.wf.impl.ExpectedWorkItem) LensContext(com.evolveum.midpoint.model.impl.lens.LensContext) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) IOException(java.io.IOException) ApprovalInstruction(com.evolveum.midpoint.wf.impl.ApprovalInstruction) ArrayList(java.util.ArrayList) List(java.util.List) ExpectedTask(com.evolveum.midpoint.wf.impl.ExpectedTask) Test(org.testng.annotations.Test)

Example 42 with LensContext

use of com.evolveum.midpoint.model.impl.lens.LensContext in project midpoint by Evolveum.

the class TestObjectLifecycleAdvanced method test020ActivateIncompleteRole.

@Test
public void test020ActivateIncompleteRole() throws Exception {
    login(userAdministrator);
    Task task = getTestTask();
    OperationResult result = getTestOperationResult();
    @SuppressWarnings({ "raw" }) ObjectDelta<RoleType> activateRoleDelta = prismContext.deltaFor(RoleType.class).item(RoleType.F_LIFECYCLE_STATE).replace(SchemaConstants.LIFECYCLE_ACTIVE).asObjectDelta(roleEmployeeOid);
    RecordingProgressListener recordingListener = new RecordingProgressListener();
    try {
        modelService.executeChanges(Collections.singleton(activateRoleDelta), null, task, Collections.singleton(recordingListener), result);
        fail("unexpected success");
    } catch (PolicyViolationException e) {
        System.out.println("Got expected exception: " + e.getMessage());
    }
    // noinspection unchecked
    LensContext<RoleType> context = (LensContext<RoleType>) recordingListener.getModelContext();
    System.out.println(context.dumpFocusPolicyRules(0));
    EvaluatedPolicyRule incompleteActivationRule = context.getFocusContext().getObjectPolicyRules().stream().filter(rule -> "disallow-incomplete-role-activation".equals(rule.getName())).findFirst().orElseThrow(() -> new AssertionError("rule not found"));
    // objectState + or
    assertEquals("Wrong # of triggers in incompleteActivationRule", 2, incompleteActivationRule.getTriggers().size());
}
Also used : EvaluatedPolicyRule(com.evolveum.midpoint.model.api.context.EvaluatedPolicyRule) ExpectedTask(com.evolveum.midpoint.wf.impl.ExpectedTask) Task(com.evolveum.midpoint.task.api.Task) RecordingProgressListener(com.evolveum.midpoint.model.impl.util.RecordingProgressListener) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolicyViolationException(com.evolveum.midpoint.util.exception.PolicyViolationException) LensContext(com.evolveum.midpoint.model.impl.lens.LensContext) Test(org.testng.annotations.Test)

Example 43 with LensContext

use of com.evolveum.midpoint.model.impl.lens.LensContext in project midpoint by Evolveum.

the class TestObjectLifecycleAdvanced method test045ActivateCompleteRole.

@Test
public void test045ActivateCompleteRole() throws Exception {
    login(userAdministrator);
    ObjectDelta<RoleType> activateRoleDelta = prismContext.deltaFor(RoleType.class).item(RoleType.F_LIFECYCLE_STATE).replace(SchemaConstants.LIFECYCLE_ACTIVE).item(RoleType.F_DESCRIPTION).replace("hi").asObjectDelta(roleEmployeeOid);
    executeTest(new TestDetails() {

        @Override
        protected LensContext createModelContext(OperationResult result) throws Exception {
            LensContext<RoleType> lensContext = createLensContext(RoleType.class);
            addFocusDeltaToContext(lensContext, activateRoleDelta);
            return lensContext;
        }

        @Override
        protected void afterFirstClockworkRun(CaseType rootCase, CaseType case0, List<CaseType> subcases, List<CaseWorkItemType> workItems, Task opTask, OperationResult result) throws Exception {
            ExpectedTask expectedTask = new ExpectedTask(null, "Matching state: after operation (\"active lifecycleState\")");
            ExpectedWorkItem expectedWorkItem = new ExpectedWorkItem(userEmployeeOwnerOid, null, expectedTask);
            assertWfContextAfterClockworkRun(rootCase, subcases, workItems, null, Collections.singletonList(expectedTask), Collections.singletonList(expectedWorkItem));
            ApprovalContextType wfc = subcases.get(0).getApprovalContext();
            assertEquals("Wrong # of attached policy rules entries", 1, wfc.getPolicyRules().getEntry().size());
            SchemaAttachedPolicyRuleType attachedRule = wfc.getPolicyRules().getEntry().get(0);
            assertEquals(1, attachedRule.getStageMin().intValue());
            assertEquals(1, attachedRule.getStageMax().intValue());
            assertEquals("Wrong # of attached triggers", 1, attachedRule.getRule().getTrigger().size());
            EvaluatedPolicyRuleTriggerType trigger = attachedRule.getRule().getTrigger().get(0);
            assertEquals("Wrong constraintKind in trigger", PolicyConstraintKindType.TRANSITION, trigger.getConstraintKind());
            CaseWorkItemType workItem = subcases.get(0).getWorkItem().get(0);
            assertEquals("Wrong # of additional information", 0, workItem.getAdditionalInformation().size());
        }

        @Override
        protected void afterCase0Finishes(CaseType rootCase, Task opTask, OperationResult result) {
        // assertNoObject(employee);
        }

        @Override
        protected void afterRootCaseFinishes(CaseType rootCase, List<CaseType> subcases, Task opTask, OperationResult result) {
        // assertObject(employee);
        }

        @Override
        protected boolean executeImmediately() {
            return false;
        }

        @Override
        protected Boolean decideOnApproval(CaseWorkItemType caseWorkItem) throws Exception {
            login(getUser(userEmployeeOwnerOid));
            return true;
        }
    }, 1);
}
Also used : ExpectedTask(com.evolveum.midpoint.wf.impl.ExpectedTask) Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ExpectedWorkItem(com.evolveum.midpoint.wf.impl.ExpectedWorkItem) LensContext(com.evolveum.midpoint.model.impl.lens.LensContext) PolicyViolationException(com.evolveum.midpoint.util.exception.PolicyViolationException) ExpectedTask(com.evolveum.midpoint.wf.impl.ExpectedTask) Test(org.testng.annotations.Test)

Example 44 with LensContext

use of com.evolveum.midpoint.model.impl.lens.LensContext in project midpoint by Evolveum.

the class BasePrimaryChangeAspect method resolveReferenceFromFilter.

private <O extends ObjectType, F extends ObjectType> List<ObjectReferenceType> resolveReferenceFromFilter(Class<O> clazz, SearchFilterType filter, String sourceDescription, LensContext<F> lensContext, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException {
    ModelExpressionThreadLocalHolder.pushExpressionEnvironment(new ExpressionEnvironment.ExpressionEnvironmentBuilder<F, PrismValue, ItemDefinition<?>>().lensContext(lensContext).currentResult(result).currentTask(task).build());
    try {
        PrismObject<SystemConfigurationType> systemConfiguration = systemObjectCache.getSystemConfiguration(result);
        VariablesMap variables = ModelImplUtils.getDefaultVariablesMap(getFocusObjectable(lensContext), null, null, systemConfiguration.asObjectable(), prismContext);
        ObjectFilter origFilter = prismContext.getQueryConverter().parseFilter(filter, clazz);
        ObjectFilter evaluatedFilter = ExpressionUtil.evaluateFilterExpressions(origFilter, variables, MiscSchemaUtil.getExpressionProfile(), mappingFactory.getExpressionFactory(), prismContext, " evaluating approverRef filter expression ", task, result);
        if (evaluatedFilter == null) {
            throw new SchemaException("Filter could not be evaluated in approverRef in " + sourceDescription + "; original filter = " + origFilter);
        }
        SearchResultList<PrismObject<O>> targets = repositoryService.searchObjects(clazz, prismContext.queryFactory().createQuery(evaluatedFilter), null, result);
        return targets.stream().map(object -> ObjectTypeUtil.createObjectRef(object, prismContext)).collect(Collectors.toList());
    } finally {
        ModelExpressionThreadLocalHolder.popExpressionEnvironment();
    }
}
Also used : ExpressionEnvironment(com.evolveum.midpoint.model.common.expression.ExpressionEnvironment) ConfigurationHelper(com.evolveum.midpoint.wf.impl.processors.ConfigurationHelper) ModelHelper(com.evolveum.midpoint.wf.impl.processors.ModelHelper) java.util(java.util) RelationResolver(com.evolveum.midpoint.wf.impl.processes.itemApproval.RelationResolver) com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Autowired(org.springframework.beans.factory.annotation.Autowired) Trace(com.evolveum.midpoint.util.logging.Trace) ModelExpressionThreadLocalHolder(com.evolveum.midpoint.model.common.expression.ModelExpressionThreadLocalHolder) DebugUtil(com.evolveum.midpoint.util.DebugUtil) com.evolveum.midpoint.util.exception(com.evolveum.midpoint.util.exception) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) ModelImplUtils(com.evolveum.midpoint.model.impl.util.ModelImplUtils) ExpressionEnvironment(com.evolveum.midpoint.model.common.expression.ExpressionEnvironment) ModelContext(com.evolveum.midpoint.model.api.context.ModelContext) MappingFactory(com.evolveum.midpoint.model.common.mapping.MappingFactory) LensContext(com.evolveum.midpoint.model.impl.lens.LensContext) ObjectTypeUtil(com.evolveum.midpoint.schema.util.ObjectTypeUtil) ApprovalSchemaHelper(com.evolveum.midpoint.wf.impl.processes.itemApproval.ApprovalSchemaHelper) RelationRegistry(com.evolveum.midpoint.schema.RelationRegistry) MiscSchemaUtil(com.evolveum.midpoint.schema.util.MiscSchemaUtil) Qualifier(org.springframework.beans.factory.annotation.Qualifier) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) BeanNameAware(org.springframework.beans.factory.BeanNameAware) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism) ReferenceResolver(com.evolveum.midpoint.wf.impl.processes.itemApproval.ReferenceResolver) S_AtomicFilterExit(com.evolveum.midpoint.prism.query.builder.S_AtomicFilterExit) Task(com.evolveum.midpoint.task.api.Task) Collectors(java.util.stream.Collectors) SearchResultList(com.evolveum.midpoint.schema.SearchResultList) PrimaryChangeProcessor(com.evolveum.midpoint.wf.impl.processors.primary.PrimaryChangeProcessor) SearchFilterType(com.evolveum.prism.xml.ns._public.query_3.SearchFilterType) SystemObjectCache(com.evolveum.midpoint.model.common.SystemObjectCache) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) PostConstruct(javax.annotation.PostConstruct) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) QName(javax.xml.namespace.QName) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Aggregations

LensContext (com.evolveum.midpoint.model.impl.lens.LensContext)44 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)36 Task (com.evolveum.midpoint.task.api.Task)32 Test (org.testng.annotations.Test)24 ExpectedTask (com.evolveum.midpoint.wf.impl.ExpectedTask)12 List (java.util.List)12 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)10 ExpectedWorkItem (com.evolveum.midpoint.wf.impl.ExpectedWorkItem)10 ArrayList (java.util.ArrayList)10 ModelContext (com.evolveum.midpoint.model.api.context.ModelContext)9 ApprovalInstruction (com.evolveum.midpoint.wf.impl.ApprovalInstruction)8 LensProjectionContext (com.evolveum.midpoint.model.impl.lens.LensProjectionContext)7 PolicyViolationException (com.evolveum.midpoint.util.exception.PolicyViolationException)7 IOException (java.io.IOException)7 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)6 JAXBException (javax.xml.bind.JAXBException)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 AbstractUninitializedCertificationTest (com.evolveum.midpoint.certification.test.AbstractUninitializedCertificationTest)5 PrismObject (com.evolveum.midpoint.prism.PrismObject)5 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)5