Search in sources :

Example 1 with CommandLineScriptType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.CommandLineScriptType in project midpoint by Evolveum.

the class CommandLineScriptExecutor method expandMacros.

private String expandMacros(CommandLineScriptType scriptType, VariablesMap variables, String shortDesc, Task task, OperationResult result) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
    String code = scriptType.getCode();
    for (ProvisioningScriptArgumentType macroDef : scriptType.getMacro()) {
        String macroName = macroDef.getName();
        QName macroQName = new QName(SchemaConstants.NS_C, macroName);
        String expressionOutput = "";
        MutablePrismPropertyDefinition<String> outputDefinition = prismContext.definitionFactory().createPropertyDefinition(ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_STRING);
        outputDefinition.setMaxOccurs(1);
        Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(macroDef, outputDefinition, MiscSchemaUtil.getExpressionProfile(), shortDesc, task, result);
        Collection<Source<?, ?>> sources = new ArrayList<>(1);
        ExpressionEvaluationContext context = new ExpressionEvaluationContext(sources, variables, shortDesc, task);
        TypedValue defaultObjectValAndDef = variables.get(macroName);
        if (defaultObjectValAndDef != null) {
            Object defaultObjectVal = defaultObjectValAndDef.getValue();
            Item sourceItem;
            if (defaultObjectVal instanceof Item) {
                sourceItem = (Item) defaultObjectVal;
            } else if (defaultObjectVal instanceof String) {
                MutablePrismPropertyDefinition<String> sourceDefinition = prismContext.definitionFactory().createPropertyDefinition(ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_STRING);
                sourceDefinition.setMaxOccurs(1);
                PrismProperty<String> sourceProperty = sourceDefinition.instantiate();
                sourceProperty.setRealValue(defaultObjectVal.toString());
                sourceItem = sourceProperty;
            } else {
                sourceItem = null;
            }
            Source<?, ?> defaultSource = new Source<>(sourceItem, null, sourceItem, macroQName, defaultObjectValAndDef.getDefinition());
            context.setDefaultSource(defaultSource);
            sources.add(defaultSource);
        }
        PrismValueDeltaSetTriple<PrismPropertyValue<String>> outputTriple = expression.evaluate(context, result);
        LOGGER.trace("Result of the expression evaluation: {}", outputTriple);
        if (outputTriple != null) {
            Collection<PrismPropertyValue<String>> nonNegativeValues = outputTriple.getNonNegativeValues();
            if (!nonNegativeValues.isEmpty()) {
                Collection<String> expressionOutputs = PrismValueCollectionsUtil.getRealValuesOfCollection((Collection) nonNegativeValues);
                if (!expressionOutputs.isEmpty()) {
                    expressionOutput = StringUtils.join(expressionOutputs, ",");
                }
            }
        }
        code = replaceMacro(code, macroDef.getName(), expressionOutput);
    }
    return code;
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) ProvisioningScriptArgumentType(com.evolveum.midpoint.xml.ns._public.common.common_3.ProvisioningScriptArgumentType) Source(com.evolveum.midpoint.repo.common.expression.Source) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue)

Example 2 with CommandLineScriptType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.CommandLineScriptType in project midpoint by Evolveum.

the class TestCommandLine method test110RedirExecuteEcho.

@Test
public void test110RedirExecuteEcho() throws Exception {
    if (!isOsUnix()) {
        displaySkip();
        return;
    }
    Task task = getTestTask();
    OperationResult result = task.getResult();
    CommandLineScriptType scriptType = getScript(REPORT_REDIR_ECHO_FILE);
    VariablesMap variables = VariablesMap.create(prismContext, VAR_HELLOTEXT, "Hello World", PrimitiveType.STRING);
    // WHEN
    when();
    commandLineScriptExecutor.executeScript(scriptType, variables, "test", task, result);
    // THEN
    then();
    assertSuccess(result);
    File targetFile = new File(MidPointTestConstants.TARGET_DIR_PATH, "echo-out");
    assertTrue("Target file is not there", targetFile.exists());
    String targetFileContent = FileUtils.readFileToString(targetFile, StandardCharsets.UTF_8);
    assertEquals("Wrong target file content", "Hello World", targetFileContent);
}
Also used : Task(com.evolveum.midpoint.task.api.Task) CommandLineScriptType(com.evolveum.midpoint.xml.ns._public.common.common_3.CommandLineScriptType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) File(java.io.File) AbstractIntegrationTest(com.evolveum.midpoint.test.AbstractIntegrationTest) Test(org.testng.annotations.Test)

Example 3 with CommandLineScriptType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.CommandLineScriptType in project midpoint by Evolveum.

the class TestCommandLine method test100PlainExecuteEcho.

@Test
public void test100PlainExecuteEcho() throws Exception {
    given();
    if (!isOsUnix()) {
        displaySkip();
        return;
    }
    Task task = getTestTask();
    OperationResult result = task.getResult();
    CommandLineScriptType scriptType = getScript(REPORT_PLAIN_ECHO_FILE);
    // WHEN
    when();
    commandLineScriptExecutor.executeScript(scriptType, null, "test", task, result);
    // THEN
    then();
    assertSuccess(result);
}
Also used : Task(com.evolveum.midpoint.task.api.Task) CommandLineScriptType(com.evolveum.midpoint.xml.ns._public.common.common_3.CommandLineScriptType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) AbstractIntegrationTest(com.evolveum.midpoint.test.AbstractIntegrationTest) Test(org.testng.annotations.Test)

Aggregations

OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 Task (com.evolveum.midpoint.task.api.Task)2 AbstractIntegrationTest (com.evolveum.midpoint.test.AbstractIntegrationTest)2 CommandLineScriptType (com.evolveum.midpoint.xml.ns._public.common.common_3.CommandLineScriptType)2 Test (org.testng.annotations.Test)2 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)1 Source (com.evolveum.midpoint.repo.common.expression.Source)1 TypedValue (com.evolveum.midpoint.schema.expression.TypedValue)1 VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)1 ProvisioningScriptArgumentType (com.evolveum.midpoint.xml.ns._public.common.common_3.ProvisioningScriptArgumentType)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1