Skip to content

Commit

Permalink
Allow usage of XMLUnit placeholders in request and response matchers. (
Browse files Browse the repository at this point in the history
…#1417)

Signed-off-by: @corneil
  • Loading branch information
flx5 authored May 30, 2024
1 parent 459df71 commit 5e73b4c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 3 deletions.
6 changes: 6 additions & 0 deletions spring-ws-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
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.
Expand Down Expand Up @@ -68,6 +70,8 @@ protected Diff createDiff(Source payload) {
return DiffBuilder.compare(expectedDocument) //
.withTest(actualDocument) //
.ignoreWhitespace() //
.withDifferenceEvaluator(
DifferenceEvaluators.chain(new PlaceholderDifferenceEvaluator(), DifferenceEvaluators.Default))
.checkForSimilar() //
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
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.
Expand All @@ -56,7 +58,12 @@ protected void match(SoapMessage soapMessage) throws IOException, AssertionError

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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ public void match() {
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() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand All @@ -30,6 +31,8 @@
import org.springframework.xml.transform.TransformerHelper;
import org.w3c.dom.Document;

import java.io.IOException;

public class SoapEnvelopeDiffMatcherTest {

@Test
Expand Down Expand Up @@ -76,4 +79,26 @@ public void nonMatch() {
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);
}
}
5 changes: 4 additions & 1 deletion src/main/asciidoctor/client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
|===
Expand Down
5 changes: 4 additions & 1 deletion src/main/asciidoctor/server.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.

Expand Down

0 comments on commit 5e73b4c

Please sign in to comment.