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;
}
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);
}
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);
}
Aggregations