Search in sources :

Example 16 with NameItemPathSegment

use of com.evolveum.midpoint.prism.path.NameItemPathSegment in project midpoint by Evolveum.

the class TestSchemaDelta method testDeleteInducementConstructionSameNullIdApplyToObject.

@Test
public void testDeleteInducementConstructionSameNullIdApplyToObject() throws Exception {
    final String TEST_NAME = "testDeleteInducementConstructionSameNullIdApplyToObject";
    displayTestTile(TEST_NAME);
    // GIVEN
    PrismObject<RoleType> role = PrismTestUtil.parseObject(ROLE_CONSTRUCTION_FILE);
    //Delta
    ConstructionType construction = new ConstructionType();
    ObjectReferenceType resourceRef = new ObjectReferenceType();
    resourceRef.setOid(ROLE_CONSTRUCTION_RESOURCE_OID);
    resourceRef.setType(ObjectTypes.RESOURCE.getTypeQName());
    construction.setResourceRef(resourceRef);
    // No container ID
    ObjectDelta<RoleType> roleDelta = ObjectDelta.createModificationDeleteContainer(RoleType.class, ROLE_CONSTRUCTION_OID, new ItemPath(new NameItemPathSegment(RoleType.F_INDUCEMENT), new IdItemPathSegment(ROLE_CONSTRUCTION_INDUCEMENT_ID), new NameItemPathSegment(AssignmentType.F_CONSTRUCTION)), getPrismContext(), construction);
    // WHEN
    roleDelta.applyTo(role);
    // THEN
    System.out.println("Role after delta application:");
    System.out.println(role.debugDump());
    assertEquals("Wrong OID", ROLE_CONSTRUCTION_OID, role.getOid());
    PrismAsserts.assertPropertyValue(role, UserType.F_NAME, PrismTestUtil.createPolyString("Construction"));
    PrismContainer<AssignmentType> inducementContainer = role.findContainer(RoleType.F_INDUCEMENT);
    assertNotNull("No inducement", inducementContainer);
    assertEquals("Unexpected number of inducement values", 1, inducementContainer.size());
    PrismContainerValue<AssignmentType> inducementValue = inducementContainer.getValues().iterator().next();
    AssignmentType inducement = inducementValue.asContainerable();
    ConstructionType constructionAfter = inducement.getConstruction();
    // construction should be gone (the error is that it is empty and not gone)
    assertNull("Construction is not gone", constructionAfter);
}
Also used : ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) RoleType(com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType) ConstructionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConstructionType) IdItemPathSegment(com.evolveum.midpoint.prism.path.IdItemPathSegment) AssignmentType(com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) Test(org.testng.annotations.Test)

Example 17 with NameItemPathSegment

use of com.evolveum.midpoint.prism.path.NameItemPathSegment in project midpoint by Evolveum.

the class PrismReferenceValue method find.

@Override
public Object find(ItemPath path) {
    if (path == null || path.isEmpty()) {
        return this;
    }
    ItemPathSegment first = path.first();
    if (!(first instanceof NameItemPathSegment)) {
        throw new IllegalArgumentException("Attempt to resolve inside the reference value using a non-name path " + path + " in " + this);
    }
    QName subName = ((NameItemPathSegment) first).getName();
    if (compareLocalPart(F_OID, subName)) {
        return this.getOid();
    } else if (compareLocalPart(F_TYPE, subName)) {
        return this.getTargetType();
    } else if (compareLocalPart(F_RELATION, subName)) {
        return this.getRelation();
    } else {
        throw new IllegalArgumentException("Attempt to resolve inside the reference value using a unrecognized path " + path + " in " + this);
    }
}
Also used : ItemPathSegment(com.evolveum.midpoint.prism.path.ItemPathSegment) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) QName(javax.xml.namespace.QName) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment)

Example 18 with NameItemPathSegment

use of com.evolveum.midpoint.prism.path.NameItemPathSegment in project midpoint by Evolveum.

the class PathExpressionEvaluator method evaluate.

/* (non-Javadoc)
	 * @see com.evolveum.midpoint.common.expression.ExpressionEvaluator#evaluate(java.util.Collection, java.util.Map, boolean, java.lang.String, com.evolveum.midpoint.schema.result.OperationResult)
	 */
@Override
public PrismValueDeltaSetTriple<V> evaluate(ExpressionEvaluationContext context) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
    ItemDeltaItem<?, ?> resolveContext = null;
    if (context.getSources() != null && context.getSources().size() == 1) {
        Source<?, ?> source = context.getSources().iterator().next();
        if (path.isEmpty()) {
            PrismValueDeltaSetTriple<V> outputTriple = (PrismValueDeltaSetTriple<V>) source.toDeltaSetTriple();
            return outputTriple.clone();
        }
        resolveContext = source;
    }
    Map<QName, Object> variablesAndSources = ExpressionUtil.compileVariablesAndSources(context);
    ItemPath resolvePath = path;
    ItemPathSegment first = path.first();
    if (first instanceof NameItemPathSegment && first.isVariable()) {
        QName variableName = ((NameItemPathSegment) first).getName();
        Object variableValue;
        if (variablesAndSources.containsKey(variableName)) {
            variableValue = variablesAndSources.get(variableName);
        } else if (QNameUtil.matchAny(variableName, variablesAndSources.keySet())) {
            QName fullVariableName = QNameUtil.resolveNs(variableName, variablesAndSources.keySet());
            variableValue = variablesAndSources.get(fullVariableName);
        } else {
            throw new ExpressionEvaluationException("No variable with name " + variableName + " in " + context.getContextDescription());
        }
        if (variableValue == null) {
            return null;
        }
        if (variableValue instanceof Item || variableValue instanceof ItemDeltaItem<?, ?>) {
            resolveContext = ExpressionUtil.toItemDeltaItem(variableValue, objectResolver, "path expression in " + context.getContextDescription(), context.getResult());
        } else if (variableValue instanceof PrismPropertyValue<?>) {
            PrismValueDeltaSetTriple<V> outputTriple = new PrismValueDeltaSetTriple<>();
            outputTriple.addToZeroSet((V) variableValue);
            return ExpressionUtil.toOutputTriple(outputTriple, outputDefinition, context.getAdditionalConvertor(), null, protector, prismContext);
        } else {
            throw new ExpressionEvaluationException("Unexpected variable value " + variableValue + " (" + variableValue.getClass() + ")");
        }
        resolvePath = path.rest();
    }
    if (resolveContext == null) {
        return null;
    }
    while (!resolvePath.isEmpty()) {
        if (resolveContext.isContainer()) {
            resolveContext = resolveContext.findIdi(resolvePath.head());
            resolvePath = resolvePath.tail();
            if (resolveContext == null) {
                throw new ExpressionEvaluationException("Cannot find item using path " + path + " in " + context.getContextDescription());
            }
        } else if (resolveContext.isStructuredProperty()) {
            // The output path does not really matter. The delta will be converted to triple anyway
            // But the path cannot be null, oherwise the code will die
            resolveContext = resolveContext.resolveStructuredProperty(resolvePath, (PrismPropertyDefinition) outputDefinition, new ItemPath());
            break;
        } else if (resolveContext.isNull()) {
            break;
        } else {
            throw new ExpressionEvaluationException("Cannot resolve path " + resolvePath + " on " + resolveContext + " in " + context.getContextDescription());
        }
    }
    PrismValueDeltaSetTriple<V> outputTriple = ItemDelta.toDeltaSetTriple((Item<V, D>) resolveContext.getItemOld(), (ItemDelta<V, D>) resolveContext.getDelta());
    if (outputTriple == null) {
        return null;
    }
    return ExpressionUtil.toOutputTriple(outputTriple, outputDefinition, context.getAdditionalConvertor(), null, protector, prismContext);
}
Also used : ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) QName(javax.xml.namespace.QName) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) ItemPathSegment(com.evolveum.midpoint.prism.path.ItemPathSegment) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) Item(com.evolveum.midpoint.prism.Item) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 19 with NameItemPathSegment

use of com.evolveum.midpoint.prism.path.NameItemPathSegment in project midpoint by Evolveum.

the class TestOrgStruct method test412JackChangeMinistryOfOffenseMemberToManagerByModifyingAssignment.

// this test should generate a SchemaException (modifying targetRef in assignment should be prohibited)
@Test
public void test412JackChangeMinistryOfOffenseMemberToManagerByModifyingAssignment() throws Exception {
    final String TEST_NAME = "test412JackChangeMinistryOfOffenseMemberToManagerByModifyingAssignment";
    TestUtil.displayTestTile(this, TEST_NAME);
    Task task = taskManager.createTaskInstance(TestOrgStruct.class.getName() + "." + TEST_NAME);
    OperationResult result = task.getResult();
    PrismObject<UserType> jack = getUser(USER_JACK_OID);
    Long id = findAssignmentIdForTarget(jack, ORG_MINISTRY_OF_OFFENSE_OID);
    Collection<ItemDelta<?, ?>> modifications = new ArrayList<>();
    PrismReferenceDefinition referenceDefinition = getUserDefinition().findItemDefinition(new ItemPath(UserType.F_ASSIGNMENT, AssignmentType.F_TARGET_REF), PrismReferenceDefinition.class);
    ReferenceDelta referenceDelta = new ReferenceDelta(new ItemPath(new NameItemPathSegment(UserType.F_ASSIGNMENT), new IdItemPathSegment(id), new NameItemPathSegment(AssignmentType.F_TARGET_REF)), referenceDefinition, prismContext);
    PrismReferenceValue oldValue = new PrismReferenceValue(ORG_MINISTRY_OF_OFFENSE_OID, OrgType.COMPLEX_TYPE);
    PrismReferenceValue newValue = new PrismReferenceValue(ORG_MINISTRY_OF_OFFENSE_OID, OrgType.COMPLEX_TYPE);
    newValue.setRelation(SchemaConstants.ORG_MANAGER);
    referenceDelta.addValueToDelete(oldValue);
    referenceDelta.addValueToAdd(newValue);
    modifications.add(referenceDelta);
    ObjectDelta<UserType> userDelta = ObjectDelta.createModifyDelta(USER_JACK_OID, modifications, UserType.class, prismContext);
    Collection<ObjectDelta<? extends ObjectType>> deltas = MiscSchemaUtil.createCollection(userDelta);
    // WHEN
    try {
        modelService.executeChanges(deltas, null, task, result);
        AssertJUnit.fail("executeChanges should fail but it did not.");
    } catch (SchemaException e) {
    // ok!
    } catch (Exception e) {
        AssertJUnit.fail("executeChanges failed in the wrong way (expected SchemaException): " + e);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Task(com.evolveum.midpoint.task.api.Task) ArrayList(java.util.ArrayList) ReferenceDelta(com.evolveum.midpoint.prism.delta.ReferenceDelta) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) PolicyViolationException(com.evolveum.midpoint.util.exception.PolicyViolationException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) PrismReferenceDefinition(com.evolveum.midpoint.prism.PrismReferenceDefinition) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) PrismReferenceValue(com.evolveum.midpoint.prism.PrismReferenceValue) IdItemPathSegment(com.evolveum.midpoint.prism.path.IdItemPathSegment) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) Test(org.testng.annotations.Test) AbstractInitializedModelIntegrationTest(com.evolveum.midpoint.model.intest.AbstractInitializedModelIntegrationTest)

Example 20 with NameItemPathSegment

use of com.evolveum.midpoint.prism.path.NameItemPathSegment in project midpoint by Evolveum.

the class ExpressionUtil method resolvePath.

public static Object resolvePath(ItemPath path, ExpressionVariables variables, Object defaultContext, ObjectResolver objectResolver, String shortDesc, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException {
    Object root = defaultContext;
    ItemPath relativePath = path;
    ItemPathSegment first = path.first();
    String varDesc = "default context";
    if (first.isVariable()) {
        QName varName = ((NameItemPathSegment) first).getName();
        varDesc = "variable " + PrettyPrinter.prettyPrint(varName);
        relativePath = path.rest();
        if (variables.containsKey(varName)) {
            root = variables.get(varName);
        } else {
            throw new SchemaException("No variable with name " + varName + " in " + shortDesc);
        }
    }
    if (root == null) {
        return null;
    }
    if (relativePath.isEmpty()) {
        return root;
    }
    if (root instanceof ObjectReferenceType) {
        root = resolveReference((ObjectReferenceType) root, objectResolver, varDesc, shortDesc, task, result);
    }
    if (root instanceof Objectable) {
        return (((Objectable) root).asPrismObject()).find(relativePath);
    }
    if (root instanceof PrismObject<?>) {
        return ((PrismObject<?>) root).find(relativePath);
    } else if (root instanceof PrismContainer<?>) {
        return ((PrismContainer<?>) root).find(relativePath);
    } else if (root instanceof PrismContainerValue<?>) {
        return ((PrismContainerValue<?>) root).find(relativePath);
    } else if (root instanceof Item<?, ?>) {
        // Except for container (which is handled above)
        throw new SchemaException("Cannot apply path " + relativePath + " to " + root + " in " + shortDesc);
    } else if (root instanceof ObjectDeltaObject<?>) {
        return ((ObjectDeltaObject<?>) root).findIdi(relativePath);
    } else if (root instanceof ItemDeltaItem<?, ?>) {
        return ((ItemDeltaItem<?, ?>) root).findIdi(relativePath);
    } else {
        throw new IllegalArgumentException("Unexpected root " + root + " (relative path:" + relativePath + ") in " + shortDesc);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QName(javax.xml.namespace.QName) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) ItemPathSegment(com.evolveum.midpoint.prism.path.ItemPathSegment) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Aggregations

NameItemPathSegment (com.evolveum.midpoint.prism.path.NameItemPathSegment)69 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)61 IdItemPathSegment (com.evolveum.midpoint.prism.path.IdItemPathSegment)46 Test (org.testng.annotations.Test)35 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)22 QName (javax.xml.namespace.QName)20 Task (com.evolveum.midpoint.task.api.Task)19 ItemPathSegment (com.evolveum.midpoint.prism.path.ItemPathSegment)15 AbstractInitializedModelIntegrationTest (com.evolveum.midpoint.model.intest.AbstractInitializedModelIntegrationTest)14 TestTriggerTask (com.evolveum.midpoint.model.intest.TestTriggerTask)11 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)10 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)10 UserType (com.evolveum.midpoint.prism.foo.UserType)7 AssignmentType (com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType)7 AssignmentType (com.evolveum.midpoint.prism.foo.AssignmentType)6 MidPointPrincipal (com.evolveum.midpoint.security.api.MidPointPrincipal)6 RoleType (com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType)6 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)5 ObjectReferenceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType)5 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)4