INT-558 XmlPayloadValidatingRouter
This commit is contained in:
@@ -15,17 +15,23 @@
|
||||
*/
|
||||
package org.springframework.integration.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLAssert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.core.MessagingException;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.InputSource;
|
||||
@@ -80,5 +86,30 @@ public class DefaultXmlPayloadConverterTests {
|
||||
Node n = converter.convertToNode(testDocument);
|
||||
XMLAssert.assertXMLEqual(testDocument, (Document) n);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetSourcePassingDocumet() throws Exception{
|
||||
Source source = converter.convertToSource(testDocument);
|
||||
assertEquals(DOMSource.class, source.getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSourcePassingString() throws Exception{
|
||||
Source source = converter.convertToSource(testDocumentAsString);
|
||||
assertEquals(StringSource.class, source.getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSourcePassingSource() throws Exception{
|
||||
SAXSource passedInSource = new SAXSource();
|
||||
Source source = converter.convertToSource(passedInSource);
|
||||
assertEquals(source, passedInSource);
|
||||
}
|
||||
|
||||
@Test(expected=MessagingException.class)
|
||||
public void testInvalidPayload(){
|
||||
converter.convertToSource(12);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.xml.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class XmlPayloadValidatingRouterParserTests {
|
||||
|
||||
String channelConfig = "<si:channel id='test-input'/> <si:channel id='validOutputChannel'><si:queue capacity='10'/></si:channel> <si:channel id='invalidOutputChannel'><si:queue capacity='10'/></si:channel>";
|
||||
|
||||
@Autowired @Qualifier("test-input")
|
||||
MessageChannel inputChannel;
|
||||
|
||||
@Autowired @Qualifier("validOutputChannel")
|
||||
QueueChannel validOutputChannel;
|
||||
|
||||
@Autowired @Qualifier("invalidOutputChannel")
|
||||
QueueChannel invalidOutputChannel;
|
||||
|
||||
|
||||
ConfigurableApplicationContext appContext;
|
||||
|
||||
public EventDrivenConsumer buildContext(String routerDef){
|
||||
appContext = TestXmlApplicationContextHelper.getTestAppContext( channelConfig + routerDef);
|
||||
appContext.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
|
||||
EventDrivenConsumer consumer = (EventDrivenConsumer) appContext.getBean("router");
|
||||
consumer.start();
|
||||
return consumer;
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown(){
|
||||
if(appContext != null){
|
||||
appContext.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidMessage() throws Exception {
|
||||
Document doc = XmlTestUtil.getDocumentForString("<greeting>hello</greeting>");
|
||||
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
|
||||
buildContext("<si-xml:validating-router id='router' input-channel='test-input' valid-channel='validOutputChannel' invalid-channel='invalidOutputChannel' schema-location='org/springframework/integration/xml/config/validationTestsSchema.xsd' />");
|
||||
inputChannel.send(docMessage);
|
||||
assertEquals("Wrong number of messages", 1, validOutputChannel.getMesssageCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidMessage() throws Exception {
|
||||
Document doc = XmlTestUtil.getDocumentForString("<greeting><other/></greeting>");
|
||||
GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
|
||||
buildContext("<si-xml:validating-router id='router' input-channel='test-input' valid-channel='validOutputChannel' invalid-channel='invalidOutputChannel' schema-location='org/springframework/integration/xml/config/validationTestsSchema.xsd' />");
|
||||
inputChannel.send(docMessage);
|
||||
assertEquals("Wrong number of messages", 1, invalidOutputChannel.getMesssageCount());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.org/validationTestsSchema" xmlns:tns="http://www.example.org/validationTestsSchema" elementFormDefault="qualified">
|
||||
|
||||
<xsd:element name="greeting" type="xsd:string"/>
|
||||
|
||||
</xsd:schema>
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.xml.router;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
public class SchemaValidatorTests {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testValidMessageWithXsd() throws Exception{
|
||||
SchemaValidator validator = new SchemaValidator(new ClassPathResource("validationTestsSchema.xsd", SchemaValidator.class), XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
Source source = XmlTestUtil.getDomSourceForString("<greeting>hello</greeting>");
|
||||
assertTrue("Document expected to be valid " ,validator.isValid(source)) ;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidMessageWithXsd() throws Exception{
|
||||
SchemaValidator validator = new SchemaValidator(new ClassPathResource("validationTestsSchema.xsd", SchemaValidator.class), XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
Source source = XmlTestUtil.getDomSourceForString("<notInSchema>hello</notInSchema>");
|
||||
assertFalse("Document not expected to be valid " ,validator.isValid(source)) ;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidXml() throws Exception {
|
||||
SchemaValidator validator = new SchemaValidator(new ClassPathResource("validationTestsSchema.xsd", SchemaValidator.class), XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
Source source =new StringSource("something else");
|
||||
assertFalse("Document not expected to be valid " ,validator.isValid(source)) ;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package org.springframework.integration.xml.router;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
|
||||
import org.junit.Before;
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
|
||||
public class XmlPayloadValidatingRouterTests {
|
||||
|
||||
String validChannelName = "VALID";
|
||||
|
||||
String invalidChannelName = "INVALID";
|
||||
|
||||
Source testSource;
|
||||
|
||||
Message<Source> testMessage;
|
||||
|
||||
@Before
|
||||
public void setUp(){
|
||||
testSource = new SAXSource();
|
||||
testMessage = MessageBuilder.withPayload(testSource).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidMessage(){
|
||||
StubValidator validator = new StubValidator(true);
|
||||
XmlPayloadValidatingRouter router = new XmlPayloadValidatingRouter(validChannelName, invalidChannelName, validator);
|
||||
String returnedChannelName = router.determineTargetChannelName(testMessage);
|
||||
assertEquals("Wrong channel name", validChannelName, returnedChannelName);
|
||||
assertEquals("Source not passed to validator ", testSource, validator.passedIn);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidMessage(){
|
||||
StubValidator validator = new StubValidator(false);
|
||||
XmlPayloadValidatingRouter router = new XmlPayloadValidatingRouter(validChannelName, invalidChannelName, validator);
|
||||
String returnedChannelName = router.determineTargetChannelName(testMessage);
|
||||
assertEquals("Wrong channel name", invalidChannelName, returnedChannelName);
|
||||
assertEquals("Source not passed to validator ", testSource, validator.passedIn);
|
||||
}
|
||||
|
||||
|
||||
static class StubValidator implements XmlValidator {
|
||||
|
||||
private final boolean validationResult;
|
||||
|
||||
Source passedIn;
|
||||
|
||||
public StubValidator(boolean validationResult) {
|
||||
this.validationResult = validationResult;
|
||||
}
|
||||
|
||||
public boolean isValid(Source source) {
|
||||
passedIn = source;
|
||||
return validationResult;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.org/validationTestsSchema" xmlns:tns="http://www.example.org/validationTestsSchema" elementFormDefault="qualified">
|
||||
|
||||
<xsd:element name="greeting" type="xsd:string"/>
|
||||
|
||||
</xsd:schema>
|
||||
Reference in New Issue
Block a user