Search in sources :

Example 11 with ExpressionVariables

use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.

the class TestFilterExpression method evaluateExpressionAssertFilter.

private ObjectFilter evaluateExpressionAssertFilter(String filename, String input, Class<? extends ObjectFilter> expectedType, Task task, OperationResult result) throws SchemaException, IOException, ObjectNotFoundException, ExpressionEvaluationException {
    PrismContext prismContext = PrismTestUtil.getPrismContext();
    SearchFilterType filterType = PrismTestUtil.parseAtomicValue(new File(TEST_DIR, filename), SearchFilterType.COMPLEX_TYPE);
    ObjectFilter filter = QueryJaxbConvertor.createObjectFilter(UserType.class, filterType, prismContext);
    Map<QName, Object> params = new HashMap<>();
    PrismPropertyValue<String> pval = null;
    if (input != null) {
        pval = new PrismPropertyValue<String>(input);
    }
    params.put(ExpressionConstants.VAR_INPUT, pval);
    ExpressionVariables variables = new ExpressionVariables();
    variables.addVariableDefinitions(params);
    // WHEN
    ObjectFilter evaluatedFilter = ExpressionUtil.evaluateFilterExpressions(filter, variables, expressionFactory, prismContext, "evaluating filter with null value not allowed", task, result);
    // THEN
    display("Evaluated filter", evaluatedFilter);
    AssertJUnit.assertTrue("Expression should be evaluated to " + expectedType + ", but was " + evaluatedFilter, expectedType.isAssignableFrom(evaluatedFilter.getClass()));
    return evaluatedFilter;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) SearchFilterType(com.evolveum.prism.xml.ns._public.query_3.SearchFilterType) HashMap(java.util.HashMap) PrismContext(com.evolveum.midpoint.prism.PrismContext) QName(javax.xml.namespace.QName) PrismObject(com.evolveum.midpoint.prism.PrismObject) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) File(java.io.File)

Example 12 with ExpressionVariables

use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.

the class TestExpression method testIterationCondition.

@Test
public void testIterationCondition() throws Exception {
    final String TEST_NAME = "testIterationCondition";
    TestUtil.displayTestTile(TEST_NAME);
    // GIVEN
    OperationResult result = new OperationResult(TestExpression.class.getName() + "." + TEST_NAME);
    rememberScriptExecutionCount();
    ExpressionType expressionType = PrismTestUtil.parseAtomicValue(EXPRESSION_ITERATION_CONDITION_FILE, ExpressionType.COMPLEX_TYPE);
    PrismPropertyDefinition<Boolean> outputDefinition = new PrismPropertyDefinitionImpl<>(ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_BOOLEAN, prismContext);
    Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, outputDefinition, TEST_NAME, null, result);
    ExpressionVariables variables = new ExpressionVariables();
    PrismObject<UserType> user = PrismTestUtil.parseObject(USER_JACK_FILE);
    variables.addVariableDefinition(ExpressionConstants.VAR_FOCUS, user);
    variables.addVariableDefinition(ExpressionConstants.VAR_USER, user);
    PrismObject<ShadowType> account = PrismTestUtil.parseObject(ACCOUNT_JACK_DUMMYFILE);
    variables.addVariableDefinition(ExpressionConstants.VAR_SHADOW, account);
    variables.addVariableDefinition(ExpressionConstants.VAR_ITERATION, 1);
    variables.addVariableDefinition(ExpressionConstants.VAR_ITERATION_TOKEN, "001");
    ExpressionEvaluationContext expressionContext = new ExpressionEvaluationContext(null, variables, TEST_NAME, null, result);
    // WHEN
    PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> outputTriple = expression.evaluate(expressionContext);
    // THEN
    assertNotNull(outputTriple);
    outputTriple.checkConsistence();
    // Make sure that the script is executed only once. There is no delta in the variables, no need to do it twice.
    assertScriptExecutionIncrement(1);
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) Test(org.testng.annotations.Test)

Example 13 with ExpressionVariables

use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.

the class TestExpressionUtil method createVariables.

private ExpressionVariables createVariables() throws SchemaException, IOException {
    ExpressionVariables variables = new ExpressionVariables();
    variables.addVariableDefinition(ExpressionConstants.VAR_USER, createUser());
    return variables;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables)

Example 14 with ExpressionVariables

use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.

the class TestExpressionUtil method createVariablesOdo.

private ExpressionVariables createVariablesOdo() throws SchemaException, IOException {
    ExpressionVariables variables = new ExpressionVariables();
    PrismObject<UserType> userOld = createUser();
    ObjectDelta<UserType> delta = ObjectDelta.createModificationReplaceProperty(UserType.class, userOld.getOid(), UserType.F_FULL_NAME, PrismTestUtil.getPrismContext(), PrismTestUtil.createPolyString("Captain Jack Sparrow"));
    ObjectDeltaObject<UserType> odo = new ObjectDeltaObject<UserType>(userOld, delta, null);
    odo.recompute();
    variables.addVariableDefinition(ExpressionConstants.VAR_USER, odo);
    return variables;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ObjectDeltaObject(com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)

Example 15 with ExpressionVariables

use of com.evolveum.midpoint.repo.common.expression.ExpressionVariables in project midpoint by Evolveum.

the class AbstractSearchIterativeTaskHandler method runInternal.

public TaskRunResult runInternal(Task coordinatorTask) {
    OperationResult opResult = new OperationResult(taskOperationPrefix + ".run");
    opResult.setStatus(OperationResultStatus.IN_PROGRESS);
    TaskRunResult runResult = new TaskRunResult();
    runResult.setOperationResult(opResult);
    H resultHandler;
    try {
        resultHandler = createHandler(runResult, coordinatorTask, opResult);
    } catch (SecurityViolationException | SchemaException | RuntimeException e) {
        LOGGER.error("{}: Error while creating a result handler: {}", taskName, e.getMessage(), e);
        opResult.recordFatalError("Error while creating a result handler: " + e.getMessage(), e);
        runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
        runResult.setProgress(coordinatorTask.getProgress());
        return runResult;
    }
    if (resultHandler == null) {
        // the error should already be in the runResult
        return runResult;
    }
    // copying relevant configuration items from task to handler
    resultHandler.setEnableIterationStatistics(isEnableIterationStatistics());
    resultHandler.setEnableSynchronizationStatistics(isEnableSynchronizationStatistics());
    resultHandler.setEnableActionsExecutedStatistics(isEnableActionsExecutedStatistics());
    boolean cont = initializeRun(resultHandler, runResult, coordinatorTask, opResult);
    if (!cont) {
        return runResult;
    }
    // TODO: error checking - already running
    if (coordinatorTask.getOid() == null) {
        throw new IllegalArgumentException("Transient tasks cannot be run by " + AbstractSearchIterativeTaskHandler.class + ": " + coordinatorTask);
    }
    handlers.put(coordinatorTask.getOid(), resultHandler);
    ObjectQuery query;
    try {
        query = createQuery(resultHandler, runResult, coordinatorTask, opResult);
    } catch (SchemaException ex) {
        logErrorAndSetResult(runResult, resultHandler, "Schema error while creating a search filter", ex, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    }
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("{}: using a query (before evaluating expressions):\n{}", taskName, DebugUtil.debugDump(query));
    }
    if (query == null) {
        // the error should already be in the runResult
        return runResult;
    }
    try {
        // TODO consider which variables should go here (there's no focus, shadow, resource - only configuration)
        if (ExpressionUtil.hasExpressions(query.getFilter())) {
            PrismObject<SystemConfigurationType> configuration = systemObjectCache.getSystemConfiguration(opResult);
            ExpressionVariables variables = Utils.getDefaultExpressionVariables(null, null, null, configuration != null ? configuration.asObjectable() : null);
            try {
                ExpressionEnvironment<?> env = new ExpressionEnvironment<>(coordinatorTask, opResult);
                ModelExpressionThreadLocalHolder.pushExpressionEnvironment(env);
                query = ExpressionUtil.evaluateQueryExpressions(query, variables, expressionFactory, prismContext, "evaluate query expressions", coordinatorTask, opResult);
            } finally {
                ModelExpressionThreadLocalHolder.popExpressionEnvironment();
            }
        }
    } catch (SchemaException | ObjectNotFoundException | ExpressionEvaluationException e) {
        logErrorAndSetResult(runResult, resultHandler, "Error while evaluating expressions in a search filter", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    }
    Class<? extends ObjectType> type = getType(coordinatorTask);
    Collection<SelectorOptions<GetOperationOptions>> queryOptions = createQueryOptions(resultHandler, runResult, coordinatorTask, opResult);
    boolean useRepository = useRepositoryDirectly(resultHandler, runResult, coordinatorTask, opResult);
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("{}: searching {} with options {}, using query:\n{}", taskName, type, queryOptions, query.debugDump());
    }
    try {
        // counting objects can be within try-catch block, because the handling is similar to handling errors within searchIterative
        Long expectedTotal = null;
        if (countObjectsOnStart) {
            if (!useRepository) {
                Integer expectedTotalInt = modelObjectResolver.countObjects(type, query, queryOptions, coordinatorTask, opResult);
                if (expectedTotalInt != null) {
                    // conversion would fail on null
                    expectedTotal = (long) expectedTotalInt;
                }
            } else {
                expectedTotal = (long) repositoryService.countObjects(type, query, opResult);
            }
            LOGGER.trace("{}: expecting {} objects to be processed", taskName, expectedTotal);
        }
        runResult.setProgress(0);
        coordinatorTask.setProgress(0);
        if (expectedTotal != null) {
            coordinatorTask.setExpectedTotal(expectedTotal);
        }
        try {
            coordinatorTask.savePendingModifications(opResult);
        } catch (ObjectAlreadyExistsException e) {
            // other exceptions are handled in the outer try block
            throw new IllegalStateException("Unexpected ObjectAlreadyExistsException when updating task progress/expectedTotal", e);
        }
        resultHandler.createWorkerThreads(coordinatorTask, opResult);
        if (!useRepository) {
            modelObjectResolver.searchIterative((Class<O>) type, query, queryOptions, resultHandler, coordinatorTask, opResult);
        } else {
            // TODO think about this
            repositoryService.searchObjectsIterative(type, query, (ResultHandler) resultHandler, null, false, opResult);
        }
        resultHandler.completeProcessing(coordinatorTask, opResult);
    } catch (ObjectNotFoundException e) {
        // This is bad. The resource does not exist. Permanent problem.
        logErrorAndSetResult(runResult, resultHandler, "Object not found", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    } catch (CommunicationException e) {
        // Error, but not critical. Just try later.
        logErrorAndSetResult(runResult, resultHandler, "Communication error", e, OperationResultStatus.PARTIAL_ERROR, TaskRunResultStatus.TEMPORARY_ERROR);
        return runResult;
    } catch (SchemaException e) {
        // Not sure about this. But most likely it is a misconfigured resource or connector
        // It may be worth to retry. Error is fatal, but may not be permanent.
        logErrorAndSetResult(runResult, resultHandler, "Error dealing with schema", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.TEMPORARY_ERROR);
        return runResult;
    } catch (RuntimeException e) {
        // Can be anything ... but we can't recover from that.
        // It is most likely a programming error. Does not make much sense to retry.
        logErrorAndSetResult(runResult, resultHandler, "Internal error", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    } catch (ConfigurationException e) {
        // Not sure about this. But most likely it is a misconfigured resource or connector
        // It may be worth to retry. Error is fatal, but may not be permanent.
        logErrorAndSetResult(runResult, resultHandler, "Configuration error", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.TEMPORARY_ERROR);
        return runResult;
    } catch (SecurityViolationException e) {
        logErrorAndSetResult(runResult, resultHandler, "Security violation", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    } catch (ExpressionEvaluationException e) {
        logErrorAndSetResult(runResult, resultHandler, "Expression error", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    }
    // TODO: check last handler status
    handlers.remove(coordinatorTask.getOid());
    runResult.setProgress(resultHandler.getProgress());
    runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);
    if (logFinishInfo) {
        String finishMessage = "Finished " + taskName + " (" + coordinatorTask + "). ";
        String statistics = "Processed " + resultHandler.getProgress() + " objects in " + resultHandler.getWallTime() / 1000 + " seconds, got " + resultHandler.getErrors() + " errors.";
        if (resultHandler.getProgress() > 0) {
            statistics += " Average time for one object: " + resultHandler.getAverageTime() + " milliseconds" + " (wall clock time average: " + resultHandler.getWallAverageTime() + " ms).";
        }
        if (!coordinatorTask.canRun()) {
            statistics += " Task was interrupted during processing.";
        }
        opResult.createSubresult(taskOperationPrefix + ".statistics").recordStatus(OperationResultStatus.SUCCESS, statistics);
        TaskHandlerUtil.appendLastFailuresInformation(taskOperationPrefix, coordinatorTask, opResult);
        LOGGER.info("{}", finishMessage + statistics);
    }
    try {
        finish(resultHandler, runResult, coordinatorTask, opResult);
    } catch (SchemaException e) {
        logErrorAndSetResult(runResult, resultHandler, "Schema error while finishing the run", e, OperationResultStatus.FATAL_ERROR, TaskRunResultStatus.PERMANENT_ERROR);
        return runResult;
    }
    LOGGER.trace("{} run finished (task {}, run result {})", taskName, coordinatorTask, runResult);
    return runResult;
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) TaskRunResult(com.evolveum.midpoint.task.api.TaskRunResult) ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ExpressionEnvironment(com.evolveum.midpoint.model.impl.expr.ExpressionEnvironment) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) SystemConfigurationType(com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType)

Aggregations

ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)65 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)17 QName (javax.xml.namespace.QName)15 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)10 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)10 Test (org.testng.annotations.Test)10 AbstractInternalModelIntegrationTest (com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)9 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)9 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)9 Task (com.evolveum.midpoint.task.api.Task)8 ArrayList (java.util.ArrayList)8 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)7 PrismObject (com.evolveum.midpoint.prism.PrismObject)6 PrismPropertyDefinitionImpl (com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl)6 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)6 PrismContext (com.evolveum.midpoint.prism.PrismContext)5 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)5 SystemException (com.evolveum.midpoint.util.exception.SystemException)5 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)5 ScriptExpression (com.evolveum.midpoint.model.common.expression.script.ScriptExpression)4