Search in sources :

Example 6 with ValidationStage

use of com.adaptris.transform.validate.ValidationStage in project interlok by adaptris.

the class XmlRuleValidatorTest method testListContentValidation_DataInList.

@Test
public void testListContentValidation_DataInList() throws Exception {
    XmlRuleValidator validator = new XmlRuleValidator();
    validator.setXmlDocumentFactoryConfig(DocumentBuilderFactoryBuilder.newInstance());
    validator.setValidationStages(Arrays.asList(new ValidationStage[] { new ValidationStage(XPATH_ITERATION_CHILDREN_OF_ZEUS, XPATH_CHILDREN_OF_HERA, new SimpleListContentValidation(CHILDREN_OF_HERA)) }));
    AdaptrisMessage msg = new DefaultMessageFactory().newMessage(XML_FAMILY_TREE);
    XmlValidationService service = new XmlValidationService(validator);
    try {
        ExampleServiceCase.execute(service, msg);
    } catch (ServiceException e) {
        fail("RuleValidationService failure when expecting success");
    }
}
Also used : DefaultMessageFactory(com.adaptris.core.DefaultMessageFactory) ServiceException(com.adaptris.core.ServiceException) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) SimpleListContentValidation(com.adaptris.transform.validate.SimpleListContentValidation) ValidationStage(com.adaptris.transform.validate.ValidationStage) Test(org.junit.Test)

Example 7 with ValidationStage

use of com.adaptris.transform.validate.ValidationStage in project interlok by adaptris.

the class XmlRuleValidatorTest method testIsNullContentValidation_DataIsNull.

@Test
public void testIsNullContentValidation_DataIsNull() throws Exception {
    XmlRuleValidator validator = new XmlRuleValidator();
    // Hades had no children; poor god.
    ValidationStage vs = new ValidationStage(XPATH_ITERATION_CHILDREN_OF_HADES, XPATH_CHILD_NAME, new IsNullContentValidation());
    validator.addValidationStage(vs);
    AdaptrisMessage msg = new DefaultMessageFactory().newMessage(XML_FAMILY_TREE);
    XmlValidationService service = new XmlValidationService(validator);
    try {
        ExampleServiceCase.execute(service, msg);
    } catch (ServiceException e) {
        fail("RuleValidationService failure when expecting success");
    }
}
Also used : DefaultMessageFactory(com.adaptris.core.DefaultMessageFactory) IsNullContentValidation(com.adaptris.transform.validate.IsNullContentValidation) ServiceException(com.adaptris.core.ServiceException) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ValidationStage(com.adaptris.transform.validate.ValidationStage) Test(org.junit.Test)

Example 8 with ValidationStage

use of com.adaptris.transform.validate.ValidationStage in project interlok by adaptris.

the class XmlRuleValidatorTest method testListContentValidation_WithNamespace.

@Test
public void testListContentValidation_WithNamespace() throws Exception {
    XmlRuleValidator validator = new XmlRuleValidator();
    validator.setNamespaceContext(createNamespaceHolder());
    ValidationStage vs = new ValidationStage(XPATH_NS_ITERATION_TITAN_NAMES, XPATH_NS_TITAN_NAME, new SimpleListContentValidation(THE_TWELVE_TITANS), new NotNullContentValidation());
    validator.addValidationStage(vs);
    AdaptrisMessage msg = new DefaultMessageFactory().newMessage(XML_WITH_NAMESEPACE);
    XmlValidationService service = new XmlValidationService(validator);
    try {
        ExampleServiceCase.execute(service, msg);
    } catch (ServiceException e) {
        fail("RuleValidationService failure when expecting success");
    }
}
Also used : DefaultMessageFactory(com.adaptris.core.DefaultMessageFactory) ServiceException(com.adaptris.core.ServiceException) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) SimpleListContentValidation(com.adaptris.transform.validate.SimpleListContentValidation) NotNullContentValidation(com.adaptris.transform.validate.NotNullContentValidation) ValidationStage(com.adaptris.transform.validate.ValidationStage) Test(org.junit.Test)

Example 9 with ValidationStage

use of com.adaptris.transform.validate.ValidationStage in project interlok by adaptris.

the class XmlRuleValidatorTest method testListContentValidation_WithNamespaceDataNotInList.

@Test
public void testListContentValidation_WithNamespaceDataNotInList() throws Exception {
    XmlRuleValidator validator = new XmlRuleValidator();
    validator.setNamespaceContext(createNamespaceHolder());
    ValidationStage vs = new ValidationStage(XPATH_NS_ITERATION_OLYMPIAN_NAMES, XPATH_NS_OLYMPIAN_NAME, new SimpleListContentValidation(THE_TWELVE_TITANS), new NotNullContentValidation());
    validator.addValidationStage(vs);
    AdaptrisMessage msg = new DefaultMessageFactory().newMessage(XML_WITH_NAMESEPACE);
    XmlValidationService service = new XmlValidationService(validator);
    try {
        ExampleServiceCase.execute(service, msg);
        fail("RuleValidationService success when expecting exception");
    } catch (ServiceException e) {
        // expected
        ;
    }
}
Also used : DefaultMessageFactory(com.adaptris.core.DefaultMessageFactory) ServiceException(com.adaptris.core.ServiceException) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) SimpleListContentValidation(com.adaptris.transform.validate.SimpleListContentValidation) NotNullContentValidation(com.adaptris.transform.validate.NotNullContentValidation) ValidationStage(com.adaptris.transform.validate.ValidationStage) Test(org.junit.Test)

Example 10 with ValidationStage

use of com.adaptris.transform.validate.ValidationStage in project interlok by adaptris.

the class XmlRuleValidator method validate.

@Override
public void validate(AdaptrisMessage msg) throws CoreException {
    try {
        NamespaceContext namespaceCtx = SimpleNamespaceContext.create(getNamespaceContext(), msg);
        DocumentBuilderFactoryBuilder builder = documentFactoryBuilder(namespaceCtx);
        Document doc = createDocument(msg, builder);
        XPath xp = XPath.newXPathInstance(builder, namespaceCtx);
        for (int stageIndex = 0; stageIndex < validationStages.size(); stageIndex++) {
            ValidationStage v = validationStages.get(stageIndex);
            NodeList n = xp.selectNodeList(doc, v.getIterationXpath());
            validate(n, v.getIterationXpath(), v.failOnIteratorFailure());
            for (int i = 0; i < n.getLength(); i++) {
                Node node = n.item(i);
                String contents = xp.selectSingleTextItem(node, v.getElementXpath());
                for (ContentValidation cv : v.getRules()) {
                    if (!cv.isValid(contents)) {
                        throw new ServiceException(ERR_MSG.replaceAll(I_COUNT, "" + i).replaceAll(I_XP, Matcher.quoteReplacement(v.getIterationXpath())).replaceAll(E_XP, Matcher.quoteReplacement(v.getElementXpath())).replaceAll(CONTENTS, Matcher.quoteReplacement(contents)).replaceAll(VALIDATION_MSG, Matcher.quoteReplacement(cv.getMessage())));
                    }
                }
            }
        }
    } catch (Exception e) {
        ExceptionHelper.rethrowCoreException(e);
    }
}
Also used : XPath(com.adaptris.util.text.xml.XPath) ServiceException(com.adaptris.core.ServiceException) NamespaceContext(javax.xml.namespace.NamespaceContext) SimpleNamespaceContext(com.adaptris.util.text.xml.SimpleNamespaceContext) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DocumentBuilderFactoryBuilder(com.adaptris.core.util.DocumentBuilderFactoryBuilder) XmlHelper.createDocument(com.adaptris.core.util.XmlHelper.createDocument) Document(org.w3c.dom.Document) ContentValidation(com.adaptris.transform.validate.ContentValidation) ServiceException(com.adaptris.core.ServiceException) CoreException(com.adaptris.core.CoreException) ValidationStage(com.adaptris.transform.validate.ValidationStage)

Aggregations

ValidationStage (com.adaptris.transform.validate.ValidationStage)18 ServiceException (com.adaptris.core.ServiceException)16 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)15 DefaultMessageFactory (com.adaptris.core.DefaultMessageFactory)15 Test (org.junit.Test)15 NotNullContentValidation (com.adaptris.transform.validate.NotNullContentValidation)8 SimpleListContentValidation (com.adaptris.transform.validate.SimpleListContentValidation)6 NotInListContentValidation (com.adaptris.transform.validate.NotInListContentValidation)3 RegexpContentValidation (com.adaptris.transform.validate.RegexpContentValidation)3 DocumentBuilderFactoryBuilder (com.adaptris.core.util.DocumentBuilderFactoryBuilder)2 IsNullContentValidation (com.adaptris.transform.validate.IsNullContentValidation)2 CoreException (com.adaptris.core.CoreException)1 BasicXmlSchemaValidator (com.adaptris.core.transform.schema.BasicXmlSchemaValidator)1 XmlHelper.createDocument (com.adaptris.core.util.XmlHelper.createDocument)1 ContentValidation (com.adaptris.transform.validate.ContentValidation)1 KeyValuePair (com.adaptris.util.KeyValuePair)1 KeyValuePairSet (com.adaptris.util.KeyValuePairSet)1 SimpleNamespaceContext (com.adaptris.util.text.xml.SimpleNamespaceContext)1 XPath (com.adaptris.util.text.xml.XPath)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1