Search in sources :

Example 41 with PrismObject

use of com.evolveum.midpoint.prism.PrismObject in project midpoint by Evolveum.

the class TestRefinedSchema method assertAccountShadow.

private void assertAccountShadow(PrismObject<ShadowType> accObject, PrismObject<ResourceType> resource, PrismContext prismContext) throws SchemaException, JAXBException {
    ResourceType resourceType = resource.asObjectable();
    QName objectClassQName = new QName(ResourceTypeUtil.getResourceNamespace(resourceType), "AccountObjectClass");
    PrismAsserts.assertPropertyValue(accObject, ShadowType.F_NAME, createPolyString("jack"));
    PrismAsserts.assertPropertyValue(accObject, ShadowType.F_OBJECT_CLASS, objectClassQName);
    PrismAsserts.assertPropertyValue(accObject, ShadowType.F_INTENT, SchemaConstants.INTENT_DEFAULT);
    PrismContainer<?> attributes = accObject.findOrCreateContainer(SchemaConstants.C_ATTRIBUTES);
    assertEquals("Wrong type of <attributes> definition in account", ResourceAttributeContainerDefinitionImpl.class, attributes.getDefinition().getClass());
    ResourceAttributeContainerDefinition attrDef = (ResourceAttributeContainerDefinition) attributes.getDefinition();
    assertAttributeDefs(attrDef, resourceType, null, LayerType.MODEL);
    PrismAsserts.assertPropertyValue(attributes, SchemaTestConstants.ICFS_NAME, "uid=jack,ou=People,dc=example,dc=com");
    PrismAsserts.assertPropertyValue(attributes, getAttrQName(resource, "cn"), "Jack Sparrow");
    PrismAsserts.assertPropertyValue(attributes, getAttrQName(resource, "givenName"), "Jack");
    PrismAsserts.assertPropertyValue(attributes, getAttrQName(resource, "sn"), "Sparrow");
    PrismAsserts.assertPropertyValue(attributes, getAttrQName(resource, "uid"), "jack");
    assertEquals("JAXB class name doesn't match (1)", ShadowType.class, accObject.getCompileTimeClass());
    accObject.checkConsistence();
    ShadowType accObjectType = accObject.asObjectable();
    assertEquals("Wrong JAXB name", createPolyStringType("jack"), accObjectType.getName());
    assertEquals("Wrong JAXB objectClass", objectClassQName, accObjectType.getObjectClass());
    ShadowAttributesType attributesType = accObjectType.getAttributes();
    assertNotNull("null ResourceObjectShadowAttributesType (JAXB)", attributesType);
    List<Object> attributeElements = attributesType.getAny();
    TestUtil.assertElement(attributeElements, SchemaTestConstants.ICFS_NAME, "uid=jack,ou=People,dc=example,dc=com");
    TestUtil.assertElement(attributeElements, getAttrQName(resource, "cn"), "Jack Sparrow");
    TestUtil.assertElement(attributeElements, getAttrQName(resource, "givenName"), "Jack");
    TestUtil.assertElement(attributeElements, getAttrQName(resource, "sn"), "Sparrow");
    TestUtil.assertElement(attributeElements, getAttrQName(resource, "uid"), "jack");
    String accString = PrismTestUtil.serializeObjectToString(accObjectType.asPrismObject());
    System.out.println("Result of JAXB marshalling:\n" + accString);
    accObject.checkConsistence(true, true, ConsistencyCheckScope.THOROUGH);
}
Also used : QName(javax.xml.namespace.QName) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) ShadowAttributesType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowAttributesType) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) PrismObject(com.evolveum.midpoint.prism.PrismObject)

Example 42 with PrismObject

use of com.evolveum.midpoint.prism.PrismObject in project midpoint by Evolveum.

the class GenerateExpressionEvaluator 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 {
    StringPolicyType stringPolicyType = null;
    ObjectReferenceType generateEvaluatorValuePolicyRef = generateEvaluatorType.getValuePolicyRef();
    if (generateEvaluatorValuePolicyRef != null) {
        if (generateEvaluatorType.getValuePolicyRef() != null) {
            ValuePolicyType valuePolicyType = objectResolver.resolve(generateEvaluatorValuePolicyRef, ValuePolicyType.class, null, "resolving value policy reference in generateExpressionEvaluator", context.getTask(), context.getResult());
            stringPolicyType = valuePolicyType.getStringPolicy();
        }
    }
    // would be generated
    if (stringPolicyType == null) {
        StringPolicyResolver stringPolicyResolver = context.getStringPolicyResolver();
        if (stringPolicyResolver != null) {
            stringPolicyType = stringPolicyResolver.resolve();
        }
    }
    elementStringPolicy = stringPolicyType;
    // } else {
    // stringPolicyType = elementStringPolicy;
    // }
    //
    String stringValue = null;
    GenerateExpressionEvaluatorModeType mode = generateEvaluatorType.getMode();
    Item<V, D> output = outputDefinition.instantiate();
    if (mode == null || mode == GenerateExpressionEvaluatorModeType.POLICY) {
        PrismObject<? extends ObjectType> object = getObject(context);
        // TODO: generate value based on stringPolicyType (if not null)
        if (stringPolicyType != null) {
            if (isNotEmptyMinLength(stringPolicyType)) {
                stringValue = valuePolicyGenerator.generate(output.getPath(), stringPolicyType, DEFAULT_LENGTH, true, object, context.getContextDescription(), context.getTask(), context.getResult());
            } else {
                stringValue = valuePolicyGenerator.generate(output.getPath(), stringPolicyType, DEFAULT_LENGTH, false, object, context.getContextDescription(), context.getTask(), context.getResult());
            }
            context.getResult().computeStatus();
            if (context.getResult().isError()) {
                throw new ExpressionEvaluationException("Failed to generate value according to policy: " + stringPolicyType.getDescription() + ". " + context.getResult().getMessage());
            }
        }
        if (stringValue == null) {
            int length = DEFAULT_LENGTH;
            RandomString randomString = new RandomString(length);
            stringValue = randomString.nextString();
        }
    } else if (mode == GenerateExpressionEvaluatorModeType.UUID) {
        UUID randomUUID = UUID.randomUUID();
        stringValue = randomUUID.toString();
    } else {
        throw new ExpressionEvaluationException("Unknown mode for generate expression: " + mode);
    }
    Object value = ExpressionUtil.convertToOutputValue(stringValue, outputDefinition, protector);
    if (output instanceof PrismProperty) {
        PrismPropertyValue<Object> pValue = new PrismPropertyValue<Object>(value);
        ((PrismProperty<Object>) output).add(pValue);
    } else {
        throw new UnsupportedOperationException("Can only generate values of property, not " + output.getClass());
    }
    return ItemDelta.toDeltaSetTriple(output, null);
}
Also used : StringPolicyType(com.evolveum.midpoint.xml.ns._public.common.common_3.StringPolicyType) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ValuePolicyType(com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType) UUID(java.util.UUID) RandomString(com.evolveum.midpoint.util.RandomString) StringPolicyResolver(com.evolveum.midpoint.repo.common.expression.StringPolicyResolver) RandomString(com.evolveum.midpoint.util.RandomString) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) PrismObject(com.evolveum.midpoint.prism.PrismObject) UUID(java.util.UUID) GenerateExpressionEvaluatorModeType(com.evolveum.midpoint.xml.ns._public.common.common_3.GenerateExpressionEvaluatorModeType) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 43 with PrismObject

use of com.evolveum.midpoint.prism.PrismObject in project midpoint by Evolveum.

the class LazyXPathVariableResolver method convertToXml.

// May return primitive types or DOM Node
public static Object convertToXml(Object variableValue, QName variableName, final PrismContext prismContext, String contextDescription) throws SchemaException {
    try {
        if (variableValue instanceof Objectable) {
            variableValue = ((Objectable) variableValue).asPrismObject();
        }
        if (variableValue instanceof PrismObject) {
            PrismObject<?> prismObject = (PrismObject<?>) variableValue;
            variableValue = prismObject.getPrismContext().domSerializer().serialize(prismObject);
        } else if (variableValue instanceof PrismProperty<?>) {
            PrismProperty<?> prismProperty = (PrismProperty<?>) variableValue;
            final List<Element> elementList = new ArrayList<Element>();
            for (PrismPropertyValue<?> value : prismProperty.getValues()) {
                Element valueElement = prismContext.domSerializer().serialize(value, prismProperty.getElementName());
                elementList.add(valueElement);
            }
            NodeList nodeList = new AdHocNodeList(elementList);
            variableValue = nodeList;
        } else if (variableValue instanceof PrismValue) {
            PrismValue pval = (PrismValue) variableValue;
            if (pval.getParent() == null) {
                // Set a fake parent to allow serialization
                pval.setParent(new AdHocItemable(prismContext));
            }
            variableValue = prismContext.domSerializer().serialize(pval, variableName);
        }
        if (!((variableValue instanceof Node) || variableValue instanceof NodeList) && !(variableValue.getClass().getPackage().getName().startsWith("java."))) {
            throw new SchemaException("Unable to convert value of variable " + variableName + " to XML, still got " + variableValue.getClass().getName() + ":" + variableValue + " value at the end");
        }
        return variableValue;
    } catch (SchemaException e) {
        if (variableValue != null && variableValue instanceof DebugDumpable) {
            LOGGER.trace("Value of variable {}:\n{}", variableName, ((DebugDumpable) variableValue).debugDump());
        }
        throw new SchemaException(e.getMessage() + " while processing variable " + variableName + " with value " + variableValue + " in " + contextDescription, e);
    } catch (RuntimeException e) {
        if (variableValue != null && variableValue instanceof DebugDumpable) {
            LOGGER.trace("Value of variable {}:\n{}", variableName, ((DebugDumpable) variableValue).debugDump());
        }
        throw new RuntimeException(e.getClass().getName() + ": " + e.getMessage() + " while processing variable " + variableName + " with value " + variableValue + " in " + contextDescription, e);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DebugDumpable(com.evolveum.midpoint.util.DebugDumpable) PrismValue(com.evolveum.midpoint.prism.PrismValue) PrismObject(com.evolveum.midpoint.prism.PrismObject) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) Objectable(com.evolveum.midpoint.prism.Objectable) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 44 with PrismObject

use of com.evolveum.midpoint.prism.PrismObject in project midpoint by Evolveum.

the class AbstractSearchExpressionEvaluator method executeSearchAttempt.

private <O extends ObjectType> List<V> executeSearchAttempt(final List<PrismObject> rawResult, Class<O> targetTypeClass, final QName targetTypeQName, ObjectQuery query, boolean searchOnResource, boolean tryAlsoRepository, final List<ItemDelta<V, D>> additionalAttributeDeltas, final ExpressionEvaluationContext params, String contextDescription, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
    final List<V> list = new ArrayList<V>();
    Collection<SelectorOptions<GetOperationOptions>> options = new ArrayList<>();
    if (!searchOnResource) {
        options.add(SelectorOptions.create(GetOperationOptions.createNoFetch()));
    }
    extendOptions(options, searchOnResource);
    ResultHandler<O> handler = new ResultHandler<O>() {

        @Override
        public boolean handle(PrismObject<O> object, OperationResult parentResult) {
            if (rawResult != null) {
                rawResult.add(object);
            }
            list.add(createPrismValue(object.getOid(), targetTypeQName, additionalAttributeDeltas, params));
            return true;
        }
    };
    try {
        objectResolver.searchIterative(targetTypeClass, query, options, handler, task, result);
    } catch (IllegalStateException e) {
        // this comes from checkConsistence methods
        throw new IllegalStateException(e.getMessage() + " in " + contextDescription, e);
    } catch (SchemaException e) {
        throw new SchemaException(e.getMessage() + " in " + contextDescription, e);
    } catch (SystemException e) {
        throw new SystemException(e.getMessage() + " in " + contextDescription, e);
    } catch (CommunicationException | ConfigurationException | SecurityViolationException e) {
        if (searchOnResource && tryAlsoRepository) {
            options = SelectorOptions.createCollection(GetOperationOptions.createNoFetch());
            try {
                objectResolver.searchIterative(targetTypeClass, query, options, handler, task, result);
            } catch (SchemaException e1) {
                throw new SchemaException(e1.getMessage() + " in " + contextDescription, e1);
            } catch (CommunicationException | ConfigurationException | SecurityViolationException e1) {
                // shadow for group doesn't exist? (MID-2107)
                throw new ExpressionEvaluationException("Unexpected expression exception " + e + ": " + e.getMessage(), e);
            }
        } else {
            throw new ExpressionEvaluationException("Unexpected expression exception " + e + ": " + e.getMessage(), e);
        }
    } catch (ObjectNotFoundException e) {
        throw e;
    }
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Assignment expression resulted in {} objects, using query:\n{}", list.size(), query.debugDump());
    }
    return list;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) PrismObject(com.evolveum.midpoint.prism.PrismObject) SystemException(com.evolveum.midpoint.util.exception.SystemException) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 45 with PrismObject

use of com.evolveum.midpoint.prism.PrismObject in project midpoint by Evolveum.

the class TestCorrelationConfiramtionEvaluator method test001CorrelationOrFilter.

@Test
public void test001CorrelationOrFilter() throws Exception {
    String TEST_NAME = "test001CorrelationOrFilter";
    TestUtil.displayTestTile(this, TEST_NAME);
    Task task = taskManager.createTaskInstance(TEST_NAME);
    OperationResult result = task.getResult();
    importObjectFromFile(USER_JACK_FILE);
    PrismObject<UserType> userType = repositoryService.getObject(UserType.class, USER_JACK_OID, null, result);
    //assert jack
    assertNotNull(userType);
    ShadowType shadow = parseObjectType(ACCOUNT_SHADOW_JACK_DUMMY_FILE, ShadowType.class);
    ConditionalSearchFilterType filter = PrismTestUtil.parseAtomicValue(new File(CORRELATION_OR_FILTER), ConditionalSearchFilterType.COMPLEX_TYPE);
    List<ConditionalSearchFilterType> filters = new ArrayList<>();
    filters.add(filter);
    ResourceType resourceType = parseObjectType(RESOURCE_DUMMY_FILE, ResourceType.class);
    IntegrationTestTools.display("Queries", filters);
    // WHEN
    List<PrismObject<UserType>> matchedUsers = evaluator.findFocusesByCorrelationRule(UserType.class, shadow, filters, resourceType, getSystemConfiguration(), task, result);
    // THEN
    assertNotNull("Correlation evaluator returned null collection of matched users.", matchedUsers);
    assertEquals("Found more than one user.", 1, matchedUsers.size());
    PrismObject<UserType> jack = matchedUsers.get(0);
    assertUser(jack, "c0c010c0-d34d-b33f-f00d-111111111111", "jack", "Jack Sparrow", "Jack", "Sparrow");
}
Also used : Task(com.evolveum.midpoint.task.api.Task) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) ConditionalSearchFilterType(com.evolveum.midpoint.xml.ns._public.common.common_3.ConditionalSearchFilterType) ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResourceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) PrismObject(com.evolveum.midpoint.prism.PrismObject) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) File(java.io.File) Test(org.testng.annotations.Test) AbstractInternalModelIntegrationTest(com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)

Aggregations

PrismObject (com.evolveum.midpoint.prism.PrismObject)696 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)484 Test (org.testng.annotations.Test)317 Task (com.evolveum.midpoint.task.api.Task)307 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)288 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)147 ArrayList (java.util.ArrayList)113 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)92 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)72 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)72 SearchResultMetadata (com.evolveum.midpoint.schema.SearchResultMetadata)68 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)61 List (java.util.List)61 QName (javax.xml.namespace.QName)60 SystemException (com.evolveum.midpoint.util.exception.SystemException)46 File (java.io.File)46 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)44 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)42 ObjectPaging (com.evolveum.midpoint.prism.query.ObjectPaging)40 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)38