use of com.evolveum.midpoint.common.validator.EventHandler in project midpoint by Evolveum.
the class BasicValidatorTest method handlerTest.
@Test
public void handlerTest() throws Exception {
System.out.println("\n===[ handlerTest ]=====");
OperationResult result = new OperationResult(this.getClass().getName() + ".handlerTest");
final List<String> postMarshallHandledOids = new ArrayList<String>();
final List<String> preMarshallHandledOids = new ArrayList<String>();
EventHandler handler = new EventHandler() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
preMarshallHandledOids.add(objectElement.getAttribute("oid"));
return EventResult.cont();
}
@Override
public <T extends Objectable> EventResult postMarshall(PrismObject<T> object, Element objectElement, OperationResult objectResult) {
System.out.println("Handler processing " + object + ", result:");
System.out.println(objectResult.debugDump());
postMarshallHandledOids.add(object.getOid());
return EventResult.cont();
}
@Override
public void handleGlobalError(OperationResult currentResult) {
System.out.println("Handler got global error:");
System.out.println(currentResult.debugDump());
}
};
validateFile("three-objects.xml", handler, result);
System.out.println(result.debugDump());
AssertJUnit.assertTrue("Result is not success", result.isSuccess());
AssertJUnit.assertTrue(postMarshallHandledOids.contains("c0c010c0-d34d-b33f-f00d-111111111111"));
AssertJUnit.assertTrue(preMarshallHandledOids.contains("c0c010c0-d34d-b33f-f00d-111111111111"));
AssertJUnit.assertTrue(postMarshallHandledOids.contains("c0c010c0-d34d-b33f-f00d-111111111112"));
AssertJUnit.assertTrue(preMarshallHandledOids.contains("c0c010c0-d34d-b33f-f00d-111111111112"));
AssertJUnit.assertTrue(postMarshallHandledOids.contains("c0c010c0-d34d-b33f-f00d-111111111113"));
AssertJUnit.assertTrue(preMarshallHandledOids.contains("c0c010c0-d34d-b33f-f00d-111111111113"));
}
use of com.evolveum.midpoint.common.validator.EventHandler in project midpoint by Evolveum.
the class TestSamples method validate.
private void validate(File file) throws FileNotFoundException {
System.out.println("===> Validating file " + file.getPath());
EventHandler handler = new EventHandler() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
return EventResult.cont();
}
@Override
public <T extends Objectable> EventResult postMarshall(PrismObject<T> object, Element objectElement, OperationResult objectResult) {
// Try to marshall it back. This may detect some JAXB miscofiguration problems.
try {
String serializedString = PrismTestUtil.serializeObjectToString(object, PrismContext.LANG_XML);
} catch (SchemaException e) {
objectResult.recordFatalError("Object serialization failed", e);
}
return EventResult.cont();
}
@Override
public void handleGlobalError(OperationResult currentResult) {
// no reaction
}
};
Validator validator = new Validator(PrismTestUtil.getPrismContext());
validator.setVerbose(false);
validator.setAllowAnyType(true);
validator.setHandler(handler);
FileInputStream fis = new FileInputStream(file);
OperationResult result = new OperationResult(RESULT_OPERATION_NAME);
validator.validate(fis, result, OBJECT_RESULT_OPERATION_NAME);
if (!result.isSuccess()) {
// The error is most likely the first inner result. Therefore let's pull it out for convenience
String errorMessage = result.getMessage();
if (result.getSubresults() != null && !result.getSubresults().isEmpty()) {
if (result.getSubresults().get(0).getMessage() != null) {
errorMessage = result.getSubresults().get(0).getMessage();
}
}
System.out.println("ERROR: " + errorMessage);
System.out.println(result.debugDump());
Assert.fail(file.getPath() + ": " + errorMessage);
} else {
System.out.println("OK");
//System.out.println(result.dump());
}
System.out.println();
}
use of com.evolveum.midpoint.common.validator.EventHandler in project midpoint by Evolveum.
the class ImportObjects method execute.
public boolean execute() {
System.out.println("Starting objects import.");
File objects = new File(filePath);
if (!objects.exists() || !objects.canRead()) {
System.out.println("XML file with objects '" + objects.getAbsolutePath() + "' doesn't exist or can't be read.");
return false;
}
InputStream input = null;
ClassPathXmlApplicationContext context = null;
try {
System.out.println("Loading spring contexts.");
context = new ClassPathXmlApplicationContext(CONTEXTS);
InputStreamReader reader = new InputStreamReader(new FileInputStream(objects), "utf-8");
input = new ReaderInputStream(reader, reader.getEncoding());
final RepositoryService repository = context.getBean("repositoryService", RepositoryService.class);
PrismContext prismContext = context.getBean(PrismContext.class);
EventHandler handler = new EventHandler() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
return EventResult.cont();
}
@Override
public <T extends Objectable> EventResult postMarshall(PrismObject<T> object, Element objectElement, OperationResult objectResult) {
try {
String displayName = getDisplayName(object);
System.out.println("Importing object " + displayName);
repository.addObject((PrismObject<ObjectType>) object, null, objectResult);
} catch (Exception ex) {
objectResult.recordFatalError("Unexpected problem: " + ex.getMessage(), ex);
System.out.println("Exception occurred during import, reason: " + ex.getMessage());
ex.printStackTrace();
}
objectResult.recordSuccessIfUnknown();
if (objectResult.isAcceptable()) {
// Continue import
return EventResult.cont();
} else {
return EventResult.skipObject(objectResult.getMessage());
}
}
@Override
public void handleGlobalError(OperationResult currentResult) {
}
};
Validator validator = new Validator(prismContext, handler);
validator.setVerbose(true);
validator.setValidateSchema(validateSchema);
OperationResult result = new OperationResult("Import objeccts");
validator.validate(input, result, OperationConstants.IMPORT_OBJECT);
result.recomputeStatus();
if (!result.isSuccess()) {
System.out.println("Operation result was not success, dumping result.\n" + result.debugDump(3));
}
} catch (Exception ex) {
System.out.println("Exception occurred during import task, reason: " + ex.getMessage());
ex.printStackTrace();
} finally {
IOUtils.closeQuietly(input);
destroyContext(context);
}
System.out.println("Objects import finished.");
return true;
}
use of com.evolveum.midpoint.common.validator.EventHandler in project midpoint by Evolveum.
the class PageBase method validateObject.
protected <O extends ObjectType> void validateObject(String lexicalRepresentation, final Holder<PrismObject<O>> objectHolder, String language, boolean validateSchema, OperationResult result) {
if (language == null || PrismContext.LANG_JSON.equals(language) || PrismContext.LANG_YAML.equals(language)) {
PrismObject<O> object;
try {
object = getPrismContext().parserFor(lexicalRepresentation).language(language).parse();
object.checkConsistence();
objectHolder.setValue(object);
} catch (RuntimeException | SchemaException e) {
result.recordFatalError("Couldn't parse object: " + e.getMessage(), e);
}
return;
}
EventHandler handler = new EventHandler() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
return EventResult.cont();
}
@Override
public <T extends Objectable> EventResult postMarshall(PrismObject<T> object, Element objectElement, OperationResult objectResult) {
objectHolder.setValue((PrismObject<O>) object);
return EventResult.cont();
}
@Override
public void handleGlobalError(OperationResult currentResult) {
}
};
Validator validator = new Validator(getPrismContext(), handler);
validator.setVerbose(true);
validator.setValidateSchema(validateSchema);
validator.validateObject(lexicalRepresentation, result);
result.computeStatus();
}
use of com.evolveum.midpoint.common.validator.EventHandler in project midpoint by Evolveum.
the class BasicValidatorTest method resource1Valid.
@Test
public void resource1Valid() throws Exception {
System.out.println("\n===[ resource1Valid ]=====");
OperationResult result = new OperationResult(this.getClass().getName() + ".resource1Valid");
EventHandler handler = new EventHandler() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
return EventResult.cont();
}
@Override
public <T extends Objectable> EventResult postMarshall(PrismObject<T> object, Element objectElement, OperationResult objectResult) {
System.out.println("Validating resorce:");
System.out.println(object.debugDump());
object.checkConsistence();
PrismContainer<?> extensionContainer = object.getExtension();
PrismProperty<Integer> menProp = extensionContainer.findProperty(new QName("http://myself.me/schemas/whatever", "menOnChest"));
assertNotNull("No men on a dead man chest!", menProp);
assertEquals("Wrong number of men on a dead man chest", (Integer) 15, menProp.getAnyRealValue());
PrismPropertyDefinition menPropDef = menProp.getDefinition();
assertNotNull("Men on a dead man chest NOT defined", menPropDef);
assertEquals("Wrong type for men on a dead man chest definition", DOMUtil.XSD_INT, menPropDef.getTypeName());
assertTrue("Men on a dead man chest definition not dynamic", menPropDef.isDynamic());
return EventResult.cont();
}
@Override
public void handleGlobalError(OperationResult currentResult) {
/* nothing */
}
};
validateFile("resource-1-valid.xml", handler, result);
AssertJUnit.assertTrue(result.isSuccess());
}
Aggregations