Search in sources :

Example 1 with EventHandler

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"));
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Objectable(com.evolveum.midpoint.prism.Objectable) ArrayList(java.util.ArrayList) EventHandler(com.evolveum.midpoint.common.validator.EventHandler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Test(org.testng.annotations.Test)

Example 2 with EventHandler

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();
}
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 3 with EventHandler

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;
}
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 4 with EventHandler

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();
}
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)

Example 5 with EventHandler

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());
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) QName(javax.xml.namespace.QName) 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) Test(org.testng.annotations.Test)

Aggregations

EventHandler (com.evolveum.midpoint.common.validator.EventHandler)6 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)6 Element (org.w3c.dom.Element)6 Node (org.w3c.dom.Node)6 Objectable (com.evolveum.midpoint.prism.Objectable)5 PrismObject (com.evolveum.midpoint.prism.PrismObject)5 Validator (com.evolveum.midpoint.common.validator.Validator)4 FileInputStream (java.io.FileInputStream)2 Test (org.testng.annotations.Test)2 ResourceValidator (com.evolveum.midpoint.model.api.validator.ResourceValidator)1 PrismContext (com.evolveum.midpoint.prism.PrismContext)1 PrismPropertyDefinition (com.evolveum.midpoint.prism.PrismPropertyDefinition)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