use of com.evolveum.midpoint.common.validator.Validator in project midpoint by Evolveum.
the class BasicValidatorTest method testStopOnErrors.
/**
* Same data as schemaViolation test, but this will set s lower threshold to stop after just two erros.
*/
@Test
public void testStopOnErrors() throws Exception {
System.out.println("\n===[ testStopOnErrors ]=====");
OperationResult result = new OperationResult(this.getClass().getName() + ".testStopOnErrors");
Validator validator = new Validator(PrismTestUtil.getPrismContext());
validator.setVerbose(false);
validator.setStopAfterErrors(2);
validateFile("three-users-schema-violation.xml", null, validator, result);
System.out.println(result.debugDump());
assertFalse(result.isSuccess());
assertEquals(2, result.getSubresults().size());
}
use of com.evolveum.midpoint.common.validator.Validator in project midpoint by Evolveum.
the class BasicValidatorTest method validateFile.
private void validateFile(String filename, EventHandler handler, OperationResult result) throws FileNotFoundException {
Validator validator = new Validator(PrismTestUtil.getPrismContext());
if (handler != null) {
validator.setHandler(handler);
}
validator.setVerbose(false);
validateFile(filename, handler, validator, result);
}
use of com.evolveum.midpoint.common.validator.Validator 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.Validator 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.Validator 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();
}
Aggregations