use of com.evolveum.midpoint.prism.PrismContext in project midpoint by Evolveum.
the class WfExpressionEvaluationHelper method evaluateExpression.
@SuppressWarnings("unchecked")
@NotNull
public <T> List<T> evaluateExpression(ExpressionType expressionType, ExpressionVariables variables, String contextDescription, Class<T> clazz, QName typeName, Function<Object, Object> additionalConvertor, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
ExpressionFactory expressionFactory = getExpressionFactory();
PrismContext prismContext = expressionFactory.getPrismContext();
PrismPropertyDefinition<String> resultDef = new PrismPropertyDefinitionImpl<>(new QName(SchemaConstants.NS_C, "result"), typeName, prismContext);
Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(expressionType, resultDef, contextDescription, task, result);
ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, contextDescription, task, result);
context.setAdditionalConvertor(additionalConvertor);
PrismValueDeltaSetTriple<PrismPropertyValue<String>> exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, context, task, result);
return exprResultTriple.getZeroSet().stream().map(ppv -> (T) ppv.getRealValue()).collect(Collectors.toList());
}
use of com.evolveum.midpoint.prism.PrismContext in project midpoint by Evolveum.
the class GcpExpressionHelper method evaluateBooleanExpression.
private boolean evaluateBooleanExpression(ExpressionType expressionType, ExpressionVariables expressionVariables, String opContext, Task taskFromModel, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
PrismContext prismContext = expressionFactory.getPrismContext();
QName resultName = new QName(SchemaConstants.NS_C, "result");
PrismPropertyDefinition<Boolean> resultDef = new PrismPropertyDefinitionImpl(resultName, DOMUtil.XSD_BOOLEAN, prismContext);
Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, resultDef, opContext, taskFromModel, result);
ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, opContext, taskFromModel, result);
PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, taskFromModel, result);
Collection<PrismPropertyValue<Boolean>> exprResult = exprResultTriple.getZeroSet();
if (exprResult.size() == 0) {
return false;
} else if (exprResult.size() > 1) {
throw new IllegalStateException("Expression should return exactly one boolean value; it returned " + exprResult.size() + " ones");
}
Boolean boolResult = exprResult.iterator().next().getValue();
return boolResult != null ? boolResult : false;
}
use of com.evolveum.midpoint.prism.PrismContext in project midpoint by Evolveum.
the class ExportObjects method execute.
public boolean execute() throws UnsupportedEncodingException, FileNotFoundException {
System.out.println("Starting objects export.");
File file = new File(filePath);
if (file.exists() || file.canRead()) {
System.out.println("XML file already exists, export won't be done.");
return false;
}
final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(CONTEXTS);
final OutputStreamWriter stream = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
try {
System.out.println("Loading spring contexts.");
System.out.println("Set repository.");
RepositoryService repository = context.getBean("repositoryService", RepositoryService.class);
ResultHandler<ObjectType> handler = new ResultHandler<ObjectType>() {
PrismContext prismContext = context.getBean(PrismContext.class);
@Override
public boolean handle(PrismObject<ObjectType> object, OperationResult parentResult) {
String displayName = getDisplayName(object);
System.out.println("Exporting object " + displayName);
OperationResult resultExport = new OperationResult("Export " + displayName);
try {
String stringObject = prismContext.serializeObjectToString(object, PrismContext.LANG_XML);
stream.write("\t" + stringObject + "\n");
} catch (Exception ex) {
System.out.println("Failed to parse objects to string for xml. Reason: " + ex);
resultExport.recordFatalError("Failed to parse objects to string for xml. Reason: ", ex);
}
return true;
}
};
stream.write(createHeaderForXml());
OperationResult result = new OperationResult("search set");
System.out.println("Creating xml file " + file.getName());
repository.searchObjectsIterative(ObjectType.class, null, handler, null, false, result);
stream.write("</objects>");
System.out.println("Created xml file " + file.getName());
} catch (Exception ex) {
System.out.println("Exception occurred during context loading, reason: " + ex.getMessage());
ex.printStackTrace();
} finally {
destroyContext(context);
if (stream != null) {
IOUtils.closeQuietly(stream);
}
}
System.out.println("Objects export finished.");
return true;
}
use of com.evolveum.midpoint.prism.PrismContext in project midpoint by Evolveum.
the class TestMigrator method testUserCredentials.
@Test
public void testUserCredentials() throws Exception {
Migrator migrator = createMigrator();
PrismContext prismContext = PrismTestUtil.getPrismContext();
PrismObject<UserType> oldUser = prismContext.parseObject(new File(TEST_DIR + "/user-migrate-credentials.xml"));
PrismObject<UserType> newUser = migrator.migrate(oldUser);
UserType newUserType = newUser.asObjectable();
assertNull("Credentials in migrated object must be null.", newUserType.getCredentials());
assertNotNull("Migrated user must contain assignment.", newUserType.getAssignment());
assertEquals("Migrated user must contain 1 assignment.", newUserType.getAssignment().size(), 1);
AssignmentType superUserRole = newUserType.getAssignment().get(0);
assertNotNull("Target ref in the user's assignment must not be null.", superUserRole.getTargetRef());
assertEquals(superUserRole.getTargetRef().getOid(), SystemObjectsType.ROLE_SUPERUSER.value());
}
use of com.evolveum.midpoint.prism.PrismContext in project midpoint by Evolveum.
the class TestMigrator method assertSimpleMigration.
private <O extends ObjectType> void assertSimpleMigration(File fileOld, File fileNew) throws SchemaException, IOException {
// GIVEN
Migrator migrator = createMigrator();
PrismContext prismContext = PrismTestUtil.getPrismContext();
PrismObject<O> objectOld = prismContext.parseObject(fileOld);
// WHEN
PrismObject<O> objectNew = migrator.migrate(objectOld);
// THEN
IntegrationTestTools.display("Migrated object " + fileOld.getName(), objectNew);
assertNotNull("No migrated object " + fileOld.getName(), objectNew);
IntegrationTestTools.display("Migrated object " + fileOld.getName(), objectNew);
String migratedXml = PrismTestUtil.serializeObjectToString(objectNew, PrismContext.LANG_XML);
IntegrationTestTools.display("Migrated object XML " + fileOld.getName(), migratedXml);
PrismObject<O> expectedObject = prismContext.parseObject(fileNew);
IntegrationTestTools.display("Expected object " + fileOld.getName(), expectedObject);
String expectedXml = PrismTestUtil.serializeObjectToString(expectedObject, PrismContext.LANG_XML);
IntegrationTestTools.display("Expected object XML " + fileOld.getName(), expectedXml);
List<String> expectedXmlLines = MiscUtil.splitLines(expectedXml);
Patch patch = DiffUtils.diff(expectedXmlLines, MiscUtil.splitLines(migratedXml));
List<String> diffLines = DiffUtils.generateUnifiedDiff(fileOld.getPath(), fileNew.getPath(), expectedXmlLines, patch, 3);
IntegrationTestTools.display("XML textual diff", StringUtils.join(diffLines, '\n'));
PrismAsserts.assertEquivalent("Unexpected migration result for " + fileOld.getName(), expectedObject, objectNew);
assertEquals("Unexpected element name for " + fileOld.getName(), expectedObject.getElementName(), objectNew.getElementName());
}
Aggregations