Skip to content

Commit

Permalink
LegacyValidator: added support for Containerable
Browse files Browse the repository at this point in the history
EventHandler parameter migrated from PrismContainer to Containerable.
It's very messy to work with PrismContainer vs its values (which is
needed in case of AuditEventRecordType import in Ninja), on the other
hand it's easy to switch back to prism world from Containerable
or Objectable if needed.
  • Loading branch information
virgo47 committed Dec 6, 2021
1 parent 0b9149f commit fea5778
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,6 @@ public abstract class PageBase extends WebPage implements ModelServiceLocator {

private static final String DOT_CLASS = PageBase.class.getName() + ".";
private static final String OPERATION_LOAD_USER = DOT_CLASS + "loadUser";
protected static final String OPERATION_LOAD_VIEW_COLLECTION_REF = DOT_CLASS + "loadViewCollectionRef";
private static final String OPERATION_LOAD_WORK_ITEM_COUNT = DOT_CLASS + "loadWorkItemCount";
private static final String OPERATION_LOAD_CERT_WORK_ITEM_COUNT = DOT_CLASS + "loadCertificationWorkItemCount";

private static final String ID_TITLE = "title";
private static final String ID_MAIN_HEADER = "mainHeader";
Expand Down Expand Up @@ -1454,8 +1451,7 @@ public <T> void parseObject(String lexicalRepresentation, final Holder<T> object
if (isListOfObjects) {
objectHolder.setValue((T) list);
}
EventHandler<PrismObject<Objectable>, Objectable> handler = new EventHandler<>() {

EventHandler<Objectable> handler = new EventHandler<>() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree,
OperationResult objectResult) {
Expand All @@ -1464,13 +1460,12 @@ public EventResult preMarshall(Element objectElement, Node postValidationTree,

@Override
public EventResult postMarshall(
PrismObject<Objectable> object, Element objectElement, OperationResult objectResult) {
Objectable object, Element objectElement, OperationResult objectResult) {
if (isListOfObjects) {
list.add(object);
list.add(object.asPrismObject());
} else {
@SuppressWarnings({ "unchecked", "raw" })
T value = (T) object.asObjectable();
objectHolder.setValue(value);
//noinspection unchecked
objectHolder.setValue((T) object);
}
return EventResult.cont();
}
Expand All @@ -1479,10 +1474,10 @@ public EventResult postMarshall(
public void handleGlobalError(OperationResult currentResult) {
}
};
LegacyValidator validator = new LegacyValidator(getPrismContext(), handler);
LegacyValidator<?> validator = new LegacyValidator<>(getPrismContext(), handler);
validator.setVerbose(true);
validator.setValidateSchema(validateSchema);
validator.validate(lexicalRepresentation, result, OperationConstants.IMPORT_OBJECT); // TODO the operation name
validator.validate(lexicalRepresentation, result, OperationConstants.IMPORT_OBJECT); // TODO the operation name

result.computeStatus();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.common.validator;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.schema.result.OperationResult;

/**
Expand All @@ -23,7 +21,7 @@
*
* @author Radovan Semancik
*/
public interface EventHandler<T extends PrismContainer<C>, C extends Containerable> {
public interface EventHandler<T extends Containerable> {

/**
* Call-back called after deserializing to DOM and static schema validation but before unmarshal to JAXB.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2020 Evolveum and contributors
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
Expand Down Expand Up @@ -29,9 +29,9 @@
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.Objectable;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismParserNoIO;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
Expand All @@ -53,7 +53,7 @@
*
* @author Radovan Semancik
*/
public class LegacyValidator {
public class LegacyValidator<T extends Containerable> {

private static final Trace LOGGER = TraceManager.getTrace(LegacyValidator.class);
private static final Charset INPUT_STREAM_CHARSET = StandardCharsets.UTF_8;
Expand All @@ -69,7 +69,7 @@ public class LegacyValidator {
private boolean validateSchemas = true;
private boolean validateName = true;
private boolean allowAnyType = false;
private EventHandler handler;
private EventHandler<T> handler;
private javax.xml.validation.Validator xsdValidator;
private long progress = 0;
private long errors = 0;
Expand All @@ -82,7 +82,7 @@ public LegacyValidator(PrismContext prismContext) {
initialize();
}

public LegacyValidator(PrismContext prismContext, EventHandler handler) {
public LegacyValidator(PrismContext prismContext, EventHandler<T> handler) {
this.prismContext = prismContext;
this.handler = handler;
initialize();
Expand All @@ -95,11 +95,11 @@ private void initialize() {
xsdValidator = prismContext.getSchemaRegistry().getJavaxSchemaValidator();
}

public EventHandler getHandler() {
public EventHandler<T> getHandler() {
return handler;
}

public void setHandler(EventHandler handler) {
public void setHandler(EventHandler<T> handler) {
this.handler = handler;
}

Expand Down Expand Up @@ -252,6 +252,8 @@ public void validate(InputStream inputStream, OperationResult validatorResult, S
objectResult.recordFatalError(e);
}
throw e;
} finally {
objectResult.close();
}

if (objectResult.isError()) {
Expand Down Expand Up @@ -321,10 +323,8 @@ private EventResult readFromStreamAndValidate(XMLStreamReader stream, OperationR

objectResult.addContext(END_LINE_NUMBER, stream.getLocation().getLineNumber());

// This element may not have complete namespace definitions for a
// stand-alone
// processing, therefore copy namespace definitions from the root
// element
// This element may not have complete namespace definitions for a stand-alone
// processing, therefore copy namespace definitions from the root element.
Element objectElement = DOMUtil.getFirstChildElement(objectDoc);
DOMUtil.setNamespaceDeclarations(objectElement, rootNamespaceDeclarations);

Expand Down Expand Up @@ -382,28 +382,29 @@ private EventResult validateObjectInternal(Element objectElement, OperationResul
if (compatMode) {
parser = parser.compat();
}
PrismObject<? extends Objectable> object = parser.parse();
T containerable = parser.parseRealValue();

try {
object.checkConsistence();
} catch (RuntimeException e) {
objectResult.recordFatalError("Internal object inconsistence, probably a parser bug: " + e.getMessage(), e);
return EventResult.skipObject(e.getMessage());
}
objectResult.addContext(OperationResult.CONTEXT_OBJECT, containerable.toString());

Objectable objectType = object.asObjectable();
objectResult.addContext(OperationResult.CONTEXT_OBJECT, object.toString());
if (containerable instanceof Objectable) {
Objectable objectable = (Objectable) containerable;

if (verbose) {
LOGGER.trace("Processing OID {}", objectType.getOid());
try {
objectable.asPrismObject().checkConsistence();
} catch (RuntimeException e) {
objectResult.recordFatalError("Internal object inconsistence, probably a parser bug: " + e.getMessage(), e);
return EventResult.skipObject(e.getMessage());
}
if (verbose) {
LOGGER.trace("Processing OID {}", objectable.getOid());
}
validateObject(objectable, objectResult);
}

validateObject(objectType, objectResult);

if (handler != null) {
EventResult cont;
try {
cont = handler.postMarshall(object, objectElement, objectResult);
cont = handler.postMarshall(containerable, objectElement, objectResult);
} catch (RuntimeException e) {
// Make sure that unhandled exceptions are recorded in object result before they are rethrown
objectResult.recordFatalError("Internal error: postMarshall call failed: " + e.getMessage(), e);
Expand Down Expand Up @@ -488,8 +489,6 @@ public void validateObject(Objectable object, OperationResult objectResult) {
// TODO: more checks

objectResult.recomputeStatus("Object validation has failed", "Validation warning");
objectResult.recordSuccessIfUnknown();

}

// BIG checks - checks that create subresults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public void setup() throws SchemaException, SAXException, IOException {
@Test
public void resource1Valid() throws Exception {
OperationResult result = createOperationResult();
EventHandler<PrismObject<Objectable>, Objectable> handler = new EventHandler<>() {

EventHandler<Objectable> handler = new EventHandler<>() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree,
OperationResult objectResult) {
Expand All @@ -70,11 +69,12 @@ public EventResult preMarshall(Element objectElement, Node postValidationTree,

@Override
public EventResult postMarshall(
PrismObject<Objectable> object, Element objectElement, OperationResult objectResult) {
displayDumpable("Validating resource:", object);
object.checkConsistence();
Objectable object, Element objectElement, OperationResult objectResult) {
PrismObject<?> prismObject = object.asPrismObject();
displayDumpable("Validating resource:", prismObject);
prismObject.checkConsistence();

PrismContainer<?> extensionContainer = object.getExtension();
PrismContainer<?> extensionContainer = prismObject.getExtension();
PrismProperty<Integer> menProp = extensionContainer.findProperty(
new ItemName("http://myself.me/schemas/whatever", "menOnChest"));
assertNotNull("No men on a dead man chest!", menProp);
Expand Down Expand Up @@ -103,7 +103,7 @@ public void handlerTest() throws Exception {
final List<String> postMarshallHandledOids = new ArrayList<>();
final List<String> preMarshallHandledOids = new ArrayList<>();

EventHandler<PrismObject<Objectable>, Objectable> handler = new EventHandler<>() {
EventHandler<Objectable> handler = new EventHandler<>() {
@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
preMarshallHandledOids.add(objectElement.getAttribute("oid"));
Expand All @@ -112,7 +112,7 @@ public EventResult preMarshall(Element objectElement, Node postValidationTree, O

@Override
public EventResult postMarshall(
PrismObject<Objectable> object, Element objectElement, OperationResult objectResult) {
Objectable object, Element objectElement, OperationResult objectResult) {
displayDumpable("Handler processing " + object + ", result:", objectResult);
postMarshallHandledOids.add(object.getOid());
return EventResult.cont();
Expand Down Expand Up @@ -184,13 +184,13 @@ public void schemaViolation() throws Exception {
}

/**
* Same data as schemaViolation test, but this will set s lower threshold to stop after just two erros.
* Same data as schemaViolation test, but this will set s lower threshold to stop after just two errors.
*/
@Test
public void testStopOnErrors() throws Exception {
OperationResult result = createOperationResult();

LegacyValidator validator = new LegacyValidator(PrismTestUtil.getPrismContext());
LegacyValidator<?> validator = new LegacyValidator<>(PrismTestUtil.getPrismContext());
validator.setVerbose(false);
validator.setStopAfterErrors(2);

Expand All @@ -215,19 +215,20 @@ public void noName() throws Exception {
}

private void validateFile(String filename, OperationResult result) throws FileNotFoundException {
validateFile(filename, (EventHandler<?, ?>) null, result);
validateFile(filename, (EventHandler<?>) null, result);
}

private void validateFile(String filename, EventHandler<?, ?> handler, OperationResult result) throws FileNotFoundException {
LegacyValidator validator = new LegacyValidator(PrismTestUtil.getPrismContext());
private <T extends Containerable> void validateFile(
String filename, EventHandler<T> handler, OperationResult result) throws FileNotFoundException {
LegacyValidator<T> validator = new LegacyValidator<>(PrismTestUtil.getPrismContext());
if (handler != null) {
validator.setHandler(handler);
}
validator.setVerbose(false);
validateFile(filename, validator, result);
}

private void validateFile(String filename, LegacyValidator validator, OperationResult result) throws FileNotFoundException {
private void validateFile(String filename, LegacyValidator<?> validator, OperationResult result) throws FileNotFoundException {
String filepath = BASE_PATH + filename;

display("Validating " + filename);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* Copyright (C) 2010-2021 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.validator.test;

import java.io.File;
Expand All @@ -12,6 +18,7 @@

import com.evolveum.midpoint.common.validator.EventHandler;
import com.evolveum.midpoint.common.validator.LegacyValidator;
import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.util.PrismTestUtil;
import com.evolveum.midpoint.schema.MidPointPrismContextFactory;
import com.evolveum.midpoint.schema.constants.MidPointConstants;
Expand Down Expand Up @@ -100,11 +107,12 @@ protected void validateNodeFailure(String name, String file, String expected) th
}

protected void validateFile(String filename, OperationResult result) throws FileNotFoundException {
validateFile(filename, (EventHandler) null, result);
validateFile(filename, (EventHandler<?>) null, result);
}

protected void validateFile(String filename, EventHandler handler, OperationResult result) throws FileNotFoundException {
LegacyValidator validator = new LegacyValidator(PrismTestUtil.getPrismContext());
protected <T extends Containerable> void validateFile(
String filename, EventHandler<T> handler, OperationResult result) throws FileNotFoundException {
LegacyValidator<T> validator = new LegacyValidator<>(PrismTestUtil.getPrismContext());
if (handler != null) {
validator.setHandler(handler);
}
Expand All @@ -120,9 +128,8 @@ private void validateFile(String filename, LegacyValidator validator, OperationR

String filepath = BASE_PATH + filename;
System.out.println("Validating " + filename);
FileInputStream fis = null;
File file = new File(filepath);
fis = new FileInputStream(file);
FileInputStream fis = new FileInputStream(file);
validator.validate(fis, result, OBJECT_RESULT_OPERATION_NAME);
if (!result.isSuccess()) {
displayDumpable("Errors:", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public boolean handleError(Throwable t) {
}
parentResult.computeStatus(errors.get() + " errors, " + successes.get() + " passed");
} else {
EventHandler<PrismObject<Objectable>, Objectable> handler = new EventHandler<>() {
EventHandler<Objectable> handler = new EventHandler<>() {

@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
Expand All @@ -156,8 +156,9 @@ public EventResult preMarshall(Element objectElement, Node postValidationTree, O

@Override
public EventResult postMarshall(
PrismObject<Objectable> object, Element objectElement, OperationResult objectResult) {
return importParsedObject(object, objectResult, options, task);
Objectable object, Element objectElement, OperationResult objectResult) {
//noinspection unchecked
return importParsedObject(object.asPrismObject(), objectResult, options, task);
}

@Override
Expand All @@ -166,7 +167,7 @@ public void handleGlobalError(OperationResult currentResult) {
}
};

LegacyValidator validator = new LegacyValidator(prismContext, handler);
LegacyValidator<?> validator = new LegacyValidator<>(prismContext, handler);
validator.setVerbose(true);
if (options != null) {
validator.setValidateSchema(isTrue(options.isValidateStaticSchema()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ public void test765ModifyConfigurationDiffExpressionRawPrismContextParse() throw
public void test767ModifyConfigurationDiffExpressionRawValidatorParse() throws Exception {
modifyConfigurationDiffExpressionRaw(xml -> {
final Holder<PrismObject<ResourceType>> objectHolder = new Holder<>();
EventHandler<PrismObject<ResourceType>, ResourceType> handler = new EventHandler<>() {
EventHandler<ResourceType> handler = new EventHandler<>() {

@Override
public EventResult preMarshall(Element objectElement, Node postValidationTree,
Expand All @@ -941,16 +941,16 @@ public EventResult preMarshall(Element objectElement, Node postValidationTree,

@Override
public EventResult postMarshall(
PrismObject<ResourceType> object, Element objectElement, OperationResult objectResult) {
objectHolder.setValue(object);
ResourceType object, Element objectElement, OperationResult objectResult) {
objectHolder.setValue(object.asPrismObject());
return EventResult.cont();
}

@Override
public void handleGlobalError(OperationResult currentResult) {
}
};
LegacyValidator validator = new LegacyValidator(prismContext, handler);
LegacyValidator<ResourceType> validator = new LegacyValidator<>(prismContext, handler);
validator.setVerbose(true);
validator.setValidateSchema(false);
OperationResult result = createOperationResult("validator");
Expand Down

0 comments on commit fea5778

Please sign in to comment.