SWS-544 - Added XPath validation
This commit is contained in:
@@ -23,6 +23,7 @@ import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.xml.transform.TransformerObjectSupport;
|
||||
|
||||
import org.custommonkey.xmlunit.Diff;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
|
||||
import static org.springframework.ws.mock.client.Assert.assertTrue;
|
||||
import static org.springframework.ws.mock.client.Assert.fail;
|
||||
@@ -35,6 +36,11 @@ import static org.springframework.ws.mock.client.Assert.fail;
|
||||
*/
|
||||
abstract class DiffMatcher extends TransformerObjectSupport implements RequestMatcher {
|
||||
|
||||
static {
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
}
|
||||
|
||||
|
||||
public final void match(URI uri, WebServiceMessage request) throws IOException, AssertionError {
|
||||
try {
|
||||
Diff diff = createDiff(request);
|
||||
|
||||
@@ -56,15 +56,18 @@ class MockWebServiceMessageSender implements WebServiceMessageSender {
|
||||
return currentConnection;
|
||||
}
|
||||
|
||||
/** Always returns {@code true}. */
|
||||
/**
|
||||
* Always returns {@code true}.
|
||||
*/
|
||||
public boolean supports(URI uri) {
|
||||
return true;
|
||||
}
|
||||
|
||||
MockSenderConnection expectNewConnection() {
|
||||
Assert.state(connectionIterator == null, "Can not expect another connection, the test is already underway");
|
||||
MockSenderConnection connection = new MockSenderConnection();
|
||||
expectedConnections.add(connection);
|
||||
return connection;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import javax.xml.transform.Source;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.client.core.WebServiceTemplate;
|
||||
import org.springframework.xml.transform.ResourceSource;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
@@ -65,6 +66,18 @@ public abstract class WebServiceMock {
|
||||
|
||||
// RequestMatchers
|
||||
|
||||
/**
|
||||
* Expects any request.
|
||||
*
|
||||
* @return the request matcher
|
||||
*/
|
||||
public static RequestMatcher anything() {
|
||||
return new RequestMatcher() {
|
||||
public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Expects the given {@link Source} XML payload.
|
||||
*
|
||||
@@ -121,7 +134,7 @@ public abstract class WebServiceMock {
|
||||
/**
|
||||
* Expects the given XPath expression to (not) exist or be evaluated to a value.
|
||||
*
|
||||
* @param xpathExpression the XPath expression
|
||||
* @param xpathExpression the XPath expression
|
||||
* @param namespaceMapping the namespaces
|
||||
* @return the XPath expectations, to be further configured
|
||||
*/
|
||||
|
||||
@@ -203,7 +203,7 @@ public class WebServiceMockTest {
|
||||
template.sendSourceAndReceiveToResult(new StringSource("<request xmlns='http://example.com'/>"),
|
||||
new StringResult());
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void xpathExistsNonMatch() throws Exception {
|
||||
final Map<String, String> ns = Collections.singletonMap("ns", "http://example.com");
|
||||
@@ -214,5 +214,32 @@ public class WebServiceMockTest {
|
||||
new StringResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anythingMatch() throws Exception {
|
||||
Source request = new StringSource("<request xmlns='http://example.com'/>");
|
||||
Source response = new StringSource("<response xmlns='http://example.com'/>");
|
||||
|
||||
expect(anything()).andRespond(withPayload(response));
|
||||
|
||||
StringResult result = new StringResult();
|
||||
template.sendSourceAndReceiveToResult(request, result);
|
||||
assertXMLEqual(result.toString(), response.toString());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void recordWhenReplay() throws Exception {
|
||||
Source request = new StringSource("<request xmlns='http://example.com'/>");
|
||||
Source response = new StringSource("<response xmlns='http://example.com'/>");
|
||||
|
||||
expect(anything()).andRespond(withPayload(response));
|
||||
expect(anything()).andRespond(withPayload(response));
|
||||
|
||||
StringResult result = new StringResult();
|
||||
template.sendSourceAndReceiveToResult(request, result);
|
||||
assertXMLEqual(result.toString(), response.toString());
|
||||
|
||||
expect(anything()).andRespond(withPayload(response));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user