Search in sources :

Example 1 with Validator

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());
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Validator(com.evolveum.midpoint.common.validator.Validator) Test(org.testng.annotations.Test)

Example 2 with Validator

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);
}
Also used : Validator(com.evolveum.midpoint.common.validator.Validator)

Example 3 with Validator

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();
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Objectable(com.evolveum.midpoint.prism.Objectable) EventHandler(com.evolveum.midpoint.common.validator.EventHandler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Validator(com.evolveum.midpoint.common.validator.Validator) FileInputStream(java.io.FileInputStream)

Example 4 with Validator

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PrismContext(com.evolveum.midpoint.prism.PrismContext) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) EventHandler(com.evolveum.midpoint.common.validator.EventHandler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) FileInputStream(java.io.FileInputStream) PrismObject(com.evolveum.midpoint.prism.PrismObject) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Objectable(com.evolveum.midpoint.prism.Objectable) File(java.io.File) Validator(com.evolveum.midpoint.common.validator.Validator) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService)

Example 5 with Validator

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();
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Objectable(com.evolveum.midpoint.prism.Objectable) EventHandler(com.evolveum.midpoint.common.validator.EventHandler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Validator(com.evolveum.midpoint.common.validator.Validator) ResourceValidator(com.evolveum.midpoint.model.api.validator.ResourceValidator)

Aggregations

Validator (com.evolveum.midpoint.common.validator.Validator)6 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)5 EventHandler (com.evolveum.midpoint.common.validator.EventHandler)4 Element (org.w3c.dom.Element)4 Node (org.w3c.dom.Node)4 Objectable (com.evolveum.midpoint.prism.Objectable)3 PrismObject (com.evolveum.midpoint.prism.PrismObject)3 FileInputStream (java.io.FileInputStream)2 ResourceValidator (com.evolveum.midpoint.model.api.validator.ResourceValidator)1 PrismContext (com.evolveum.midpoint.prism.PrismContext)1 EncryptionException (com.evolveum.midpoint.prism.crypto.EncryptionException)1 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 RepositoryService (com.evolveum.midpoint.repo.api.RepositoryService)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)1 File (java.io.File)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1