Search in sources :

Example 1 with LegacyValidator

use of com.evolveum.midpoint.common.validator.LegacyValidator in project midpoint by Evolveum.

the class TestResources method test767ModifyConfigurationDiffExpressionRawValidatorParse.

/**
 * This is what GUI "Repository objects" page really does with XML.
 */
@Test
public void test767ModifyConfigurationDiffExpressionRawValidatorParse() throws Exception {
    modifyConfigurationDiffExpressionRaw(xml -> {
        final Holder<PrismObject<ResourceType>> objectHolder = new Holder<>();
        EventHandler<ResourceType> handler = new EventHandler<>() {

            @Override
            public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
                return EventResult.cont();
            }

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

            @Override
            public void handleGlobalError(OperationResult currentResult) {
            }
        };
        LegacyValidator<ResourceType> validator = new LegacyValidator<>(prismContext, handler);
        validator.setVerbose(true);
        validator.setValidateSchema(false);
        OperationResult result = createOperationResult("validator");
        validator.validateObject(xml, result);
        return objectHolder.getValue();
    });
}
Also used : LegacyValidator(com.evolveum.midpoint.common.validator.LegacyValidator) Holder(com.evolveum.midpoint.util.Holder) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) EventHandler(com.evolveum.midpoint.common.validator.EventHandler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Test(org.testng.annotations.Test)

Example 2 with LegacyValidator

use of com.evolveum.midpoint.common.validator.LegacyValidator in project midpoint by Evolveum.

the class ImportProducerWorker method processStream.

private void processStream(InputStream input) throws IOException {
    ApplicationContext appContext = context.getApplicationContext();
    PrismContext prismContext = appContext.getBean(PrismContext.class);
    MatchingRuleRegistry matchingRuleRegistry = appContext.getBean(MatchingRuleRegistry.class);
    EventHandler<T> handler = new EventHandler<>() {

        @Override
        public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
            currentOid = objectElement.getAttribute("oid");
            return EventResult.cont();
        }

        @Override
        public EventResult postMarshall(T object, Element objectElement, OperationResult objectResult) {
            try {
                if (filter != null) {
                    boolean match = ObjectQuery.match(object, filter, matchingRuleRegistry);
                    if (!match) {
                        operation.incrementSkipped();
                        return EventResult.skipObject("Object doesn't match filter");
                    }
                }
                if (!matchSelectedType(object.getClass())) {
                    operation.incrementSkipped();
                    return EventResult.skipObject("Type doesn't match");
                }
                queue.put(object);
            } catch (Exception ex) {
                throw new NinjaException(getErrorMessage() + ", reason: " + ex.getMessage(), ex);
            }
            currentOid = null;
            return stopAfterFound ? EventResult.skipObject() : EventResult.cont();
        }

        @Override
        public void handleGlobalError(OperationResult currentResult, Exception cause) {
            // This should not
            // Should we log error?
            operation.incrementError();
            String message = getErrorMessage();
            if (continueOnInputError) {
                if (context.isVerbose()) {
                    context.getLog().error(message, cause);
                } else {
                    context.getLog().error(message + ", reason: {}", cause.getMessage());
                }
            } else {
                // We need to throw runtime exception in order to stop validator, otherwise validator will continue
                // fill queue and this may result in deadlock
                operation.finish();
                throw new NinjaException(message + ", reason: " + cause.getMessage(), cause);
            }
        }
    };
    // FIXME: MID-5151: If validateSchema is false we are not validating unknown attributes on import
    LegacyValidator<?> validator = new LegacyValidator<>(prismContext, handler);
    validator.setValidateSchema(false);
    OperationResult result = operation.getResult();
    Charset charset = context.getCharset();
    Reader reader = new InputStreamReader(input, charset);
    validator.validate(new ReaderInputStream(reader, charset), result, result.getOperation());
}
Also used : PrismContext(com.evolveum.midpoint.prism.PrismContext) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) EventHandler(com.evolveum.midpoint.common.validator.EventHandler) Charset(java.nio.charset.Charset) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) ApplicationContext(org.springframework.context.ApplicationContext) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) LegacyValidator(com.evolveum.midpoint.common.validator.LegacyValidator) MatchingRuleRegistry(com.evolveum.midpoint.prism.match.MatchingRuleRegistry)

Example 3 with LegacyValidator

use of com.evolveum.midpoint.common.validator.LegacyValidator 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 errors.
 */
@Test
public void testStopOnErrors() throws Exception {
    OperationResult result = createOperationResult();
    LegacyValidator<?> validator = new LegacyValidator<>(PrismTestUtil.getPrismContext());
    validator.setVerbose(false);
    validator.setStopAfterErrors(2);
    validateFile("three-users-schema-violation.xml", validator, result);
    System.out.println(result.debugDump());
    assertFalse(result.isSuccess());
    assertEquals(2, result.getSubresults().size());
}
Also used : LegacyValidator(com.evolveum.midpoint.common.validator.LegacyValidator) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Test(org.testng.annotations.Test) AbstractUnitTest(com.evolveum.midpoint.tools.testng.AbstractUnitTest)

Example 4 with LegacyValidator

use of com.evolveum.midpoint.common.validator.LegacyValidator in project midpoint by Evolveum.

the class ObjectImporter method importObjectsInternal.

private void importObjectsInternal(InputStream input, String language, ImportOptionsType options, Task task, OperationResult parentResult) {
    if (options != null) {
        if (isTrue(options.isSummarizeErrors())) {
            parentResult.setSummarizeErrors(true);
        }
        if (isTrue(options.isSummarizeSucceses())) {
            parentResult.setSummarizeSuccesses(true);
        }
    }
    int stopAfterErrors = options != null && options.getStopAfterErrors() != null ? options.getStopAfterErrors() : 0;
    if (!PrismContext.LANG_XML.equals(language)) {
        AtomicInteger index = new AtomicInteger(0);
        AtomicInteger errors = new AtomicInteger(0);
        AtomicInteger successes = new AtomicInteger(0);
        PrismParser.ObjectHandler handler = new PrismParser.ObjectHandler() {

            @Override
            public boolean handleData(PrismObject<?> object) {
                OperationResult objectResult = parentResult.createSubresult(OperationConstants.IMPORT_OBJECT);
                objectResult.addContext("objectNumber", index.incrementAndGet());
                importParsedObject(object, objectResult, options, task);
                objectResult.computeStatusIfUnknown();
                objectResult.cleanupResult();
                parentResult.summarize();
                if (objectResult.isAcceptable()) {
                    successes.incrementAndGet();
                } else {
                    errors.incrementAndGet();
                }
                return stopAfterErrors == 0 || errors.get() < stopAfterErrors;
            }

            @Override
            public boolean handleError(Throwable t) {
                OperationResult objectResult = parentResult.createSubresult(OperationConstants.IMPORT_OBJECT);
                objectResult.addContext("objectNumber", index.incrementAndGet());
                objectResult.recordFatalError("Couldn't parse object", t);
                parentResult.summarize();
                errors.incrementAndGet();
                return stopAfterErrors == 0 || errors.get() < stopAfterErrors;
            }
        };
        PrismParser parser = prismContext.parserFor(input).language(language);
        if (options != null && options.isCompatMode() != null && options.isCompatMode()) {
            parser = parser.compat();
        }
        try {
            parser.parseObjectsIteratively(handler);
        } catch (SchemaException | IOException e) {
            parentResult.recordFatalError("Couldn't parse objects to be imported: " + e.getMessage(), e);
            LoggingUtils.logUnexpectedException(LOGGER, "Couldn't parse objects to be imported", e);
            return;
        }
        parentResult.computeStatus(errors.get() + " errors, " + successes.get() + " passed");
    } else {
        EventHandler<Objectable> handler = new EventHandler<>() {

            @Override
            public EventResult preMarshall(Element objectElement, Node postValidationTree, OperationResult objectResult) {
                return EventResult.cont();
            }

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

            @Override
            public void handleGlobalError(OperationResult currentResult) {
            // No reaction
            }
        };
        LegacyValidator<?> validator = new LegacyValidator<>(prismContext, handler);
        validator.setVerbose(true);
        if (options != null) {
            validator.setValidateSchema(isTrue(options.isValidateStaticSchema()));
            if (options.getModelExecutionOptions() != null && isFalse(options.getModelExecutionOptions().isRaw())) {
                // model will take care of this
                validator.setValidateName(false);
            }
        }
        validator.setStopAfterErrors(stopAfterErrors);
        if (options != null && options.isCompatMode() != null && options.isCompatMode()) {
            validator.setCompatMode(true);
        }
        validator.validate(input, parentResult, OperationConstants.IMPORT_OBJECT);
    }
}
Also used : Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) EventHandler(com.evolveum.midpoint.common.validator.EventHandler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LegacyValidator(com.evolveum.midpoint.common.validator.LegacyValidator)

Aggregations

LegacyValidator (com.evolveum.midpoint.common.validator.LegacyValidator)4 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)4 EventHandler (com.evolveum.midpoint.common.validator.EventHandler)3 Element (org.w3c.dom.Element)3 Node (org.w3c.dom.Node)3 Test (org.testng.annotations.Test)2 NinjaException (com.evolveum.midpoint.ninja.impl.NinjaException)1 PrismContext (com.evolveum.midpoint.prism.PrismContext)1 MatchingRuleRegistry (com.evolveum.midpoint.prism.match.MatchingRuleRegistry)1 AbstractUnitTest (com.evolveum.midpoint.tools.testng.AbstractUnitTest)1 Holder (com.evolveum.midpoint.util.Holder)1 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)1 ApplicationContext (org.springframework.context.ApplicationContext)1