Allow usage of XMLUnit placeholders in request and response matchers. (#1417)
Signed-off-by: @corneil
This commit is contained in:
@@ -51,6 +51,12 @@
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.xmlunit</groupId>
|
||||
<artifactId>xmlunit-placeholders</artifactId>
|
||||
<version>${xmlunit.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
|
||||
@@ -28,6 +28,8 @@ import org.springframework.xml.transform.TransformerHelper;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xmlunit.builder.DiffBuilder;
|
||||
import org.xmlunit.diff.Diff;
|
||||
import org.xmlunit.diff.DifferenceEvaluators;
|
||||
import org.xmlunit.placeholder.PlaceholderDifferenceEvaluator;
|
||||
|
||||
/**
|
||||
* Matches {@link Source} payloads.
|
||||
@@ -68,6 +70,8 @@ public class PayloadDiffMatcher extends DiffMatcher {
|
||||
return DiffBuilder.compare(expectedDocument) //
|
||||
.withTest(actualDocument) //
|
||||
.ignoreWhitespace() //
|
||||
.withDifferenceEvaluator(
|
||||
DifferenceEvaluators.chain(new PlaceholderDifferenceEvaluator(), DifferenceEvaluators.Default))
|
||||
.checkForSimilar() //
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ import org.springframework.xml.transform.TransformerHelper;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xmlunit.builder.DiffBuilder;
|
||||
import org.xmlunit.diff.Diff;
|
||||
import org.xmlunit.diff.DifferenceEvaluators;
|
||||
import org.xmlunit.placeholder.PlaceholderDifferenceEvaluator;
|
||||
|
||||
/**
|
||||
* Matches {@link Source} SOAP envelopes.
|
||||
@@ -56,7 +58,12 @@ public class SoapEnvelopeDiffMatcher extends AbstractSoapMessageMatcher {
|
||||
|
||||
Document actualDocument = soapMessage.getDocument();
|
||||
Document expectedDocument = createDocumentFromSource(expected);
|
||||
Diff diff = DiffBuilder.compare(expectedDocument).ignoreWhitespace().withTest(actualDocument).checkForSimilar()
|
||||
Diff diff = DiffBuilder.compare(expectedDocument)
|
||||
.ignoreWhitespace()
|
||||
.withTest(actualDocument)
|
||||
.withDifferenceEvaluator(
|
||||
DifferenceEvaluators.chain(new PlaceholderDifferenceEvaluator(), DifferenceEvaluators.Default))
|
||||
.checkForSimilar()
|
||||
.build();
|
||||
assertTrue("Envelopes are different, " + diff.toString(), !diff.hasDifferences());
|
||||
}
|
||||
|
||||
@@ -43,6 +43,21 @@ public class PayloadDiffMatcherTest {
|
||||
verify(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchWithXmlIgnore() {
|
||||
|
||||
var xml = "<element xmlns='http://example.com'>%s</element>";
|
||||
WebServiceMessage message = createMock(WebServiceMessage.class);
|
||||
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(xml.formatted("1234"))).times(2);
|
||||
replay(message);
|
||||
|
||||
var matcher = new PayloadDiffMatcher(new StringSource(xml.formatted("${xmlunit.ignore}")));
|
||||
matcher.match(message);
|
||||
|
||||
verify(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchIgnoringWhitespace() {
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -30,6 +31,8 @@ import org.springframework.xml.transform.StringSource;
|
||||
import org.springframework.xml.transform.TransformerHelper;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SoapEnvelopeDiffMatcherTest {
|
||||
|
||||
@Test
|
||||
@@ -76,4 +79,26 @@ public class SoapEnvelopeDiffMatcherTest {
|
||||
matcher.match(message);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchWithXmlIgnore() throws TransformerException, IOException {
|
||||
String xml = """
|
||||
<?xml version='1.0'?>
|
||||
<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'>
|
||||
<soap:Header><header xmlns='http://example.com'/></soap:Header>
|
||||
<soap:Body><payload xmlns='http://example.com'>%s</payload></soap:Body>
|
||||
</soap:Envelope>""";
|
||||
|
||||
String actual = String.format(xml, "1");
|
||||
DOMResult result = new DOMResult();
|
||||
TransformerHelper transformerHelper = new TransformerHelper();
|
||||
transformerHelper.transform(new StringSource(actual), result);
|
||||
SoapMessage message = createMock(SoapMessage.class);
|
||||
expect(message.getDocument()).andReturn((Document) result.getNode()).once();
|
||||
replay(message);
|
||||
|
||||
String expected = String.format(xml, "${xmlunit.ignore}");
|
||||
SoapEnvelopeDiffMatcher matcher = new SoapEnvelopeDiffMatcher(new StringSource(expected));
|
||||
matcher.match(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -457,7 +457,7 @@ The `RequestMatchers` class provides the following request matchers:
|
||||
| Expects any sort of request.
|
||||
|
||||
| `payload()`
|
||||
| Expects a given request payload.
|
||||
| Expects a given request payload. May include https://github.com/xmlunit/user-guide/wiki/Placeholders[XMLUnit Placeholders]
|
||||
|
||||
| `validPayload()`
|
||||
| Expects the request payload to validate against given XSD schemas.
|
||||
@@ -468,6 +468,9 @@ The `RequestMatchers` class provides the following request matchers:
|
||||
| `soapHeader()`
|
||||
| Expects a given SOAP header to exist in the request message.
|
||||
|
||||
| `soapEnvelope()`
|
||||
| Expects a given SOAP payload. May include https://github.com/xmlunit/user-guide/wiki/Placeholders[XMLUnit Placeholders]
|
||||
|
||||
| `connectionTo()`
|
||||
| Expects a connection to the given URL.
|
||||
|===
|
||||
|
||||
@@ -1244,7 +1244,7 @@ The `ResponseMatchers` class provides the following response matchers:
|
||||
| Description
|
||||
|
||||
| `payload()`
|
||||
| Expects a given response payload.
|
||||
| Expects a given response payload. May include https://github.com/xmlunit/user-guide/wiki/Placeholders[XMLUnit Placeholders].
|
||||
|
||||
| `validPayload()`
|
||||
| Expects the response payload to validate against given XSD schemas.
|
||||
@@ -1255,6 +1255,9 @@ The `ResponseMatchers` class provides the following response matchers:
|
||||
| `soapHeader()`
|
||||
| Expects a given SOAP header to exist in the response message.
|
||||
|
||||
| `soapEnvelope()`
|
||||
| Expects a given SOAP payload. May include https://github.com/xmlunit/user-guide/wiki/Placeholders[XMLUnit Placeholders].
|
||||
|
||||
| `noFault()`
|
||||
| Expects that the response message does not contain a SOAP Fault.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user