From a9de143306acaeea367fe01c15bd00f2b3e9ac15 Mon Sep 17 00:00:00 2001 From: Alberic Martel <35294154+AlbericMartel@users.noreply.github.com> Date: Thu, 8 Dec 2022 17:18:39 +0100 Subject: [PATCH] Fix failing soapEnvelopeDiffMatcher The SoapEnvelopeDiffMatcher was untested at the MockWebServiceServer level. Upgrading to xmlunit2 fixed the issue. Related: #1193 --- pom.xml | 5 +- spring-ws-test/pom.xml | 8 +- .../ws/test/support/matcher/DiffMatcher.java | 4 +- .../support/matcher/PayloadDiffMatcher.java | 8 +- .../matcher/SoapEnvelopeDiffMatcher.java | 16 +-- .../test/client/MockWebServiceServerTest.java | 116 ++++++++++++++---- 6 files changed, 113 insertions(+), 44 deletions(-) diff --git a/pom.xml b/pom.xml index cc4c4b99a..a7d19e7ee 100644 --- a/pom.xml +++ b/pom.xml @@ -122,8 +122,7 @@ 2.4.1 3.0.1 2.2.2 - 1.6 - 2.7.0 + 2.9.0 1.3.7 0.0.3 @@ -219,7 +218,7 @@ org.xmlunit xmlunit-assertj - ${xmlunit.version} + ${xmlunit2.version} test diff --git a/spring-ws-test/pom.xml b/spring-ws-test/pom.xml index 80adaea9d..2b52a4dc5 100644 --- a/spring-ws-test/pom.xml +++ b/spring-ws-test/pom.xml @@ -35,9 +35,9 @@ - xmlunit - xmlunit - ${xmlunit1.version} + org.xmlunit + xmlunit-core + ${xmlunit2.version} @@ -76,4 +76,4 @@ - \ No newline at end of file + diff --git a/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/DiffMatcher.java b/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/DiffMatcher.java index 1a8d04ab4..d032cb8bf 100644 --- a/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/DiffMatcher.java +++ b/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/DiffMatcher.java @@ -18,8 +18,8 @@ import static org.springframework.ws.test.support.AssertionErrors.*; -import org.custommonkey.xmlunit.Diff; import org.springframework.ws.WebServiceMessage; +import org.xmlunit.diff.Diff; /** * Implementation of {@link WebServiceMessageMatcher} based on XMLUnit's {@link Diff}. @@ -33,7 +33,7 @@ public abstract class DiffMatcher implements WebServiceMessageMatcher { public final void match(WebServiceMessage message) throws AssertionError { Diff diff = createDiff(message); - assertTrue("Messages are different, " + diff.toString(), diff.similar(), "Payload", message.getPayloadSource()); + assertTrue("Messages are different, " + diff.toString(), !diff.hasDifferences(), "Payload", message.getPayloadSource()); } /** diff --git a/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/PayloadDiffMatcher.java b/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/PayloadDiffMatcher.java index d9e7d46a4..1514482a2 100644 --- a/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/PayloadDiffMatcher.java +++ b/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/PayloadDiffMatcher.java @@ -22,11 +22,12 @@ import javax.xml.transform.TransformerException; import javax.xml.transform.dom.DOMResult; -import org.custommonkey.xmlunit.Diff; import org.springframework.util.Assert; import org.springframework.ws.WebServiceMessage; import org.springframework.xml.transform.TransformerHelper; import org.w3c.dom.Document; +import org.xmlunit.builder.DiffBuilder; +import org.xmlunit.diff.Diff; /** * Matches {@link Source} payloads. @@ -58,7 +59,10 @@ protected final Diff createDiff(WebServiceMessage message) { protected Diff createDiff(Source payload) { Document expectedDocument = createDocumentFromSource(expected); Document actualDocument = createDocumentFromSource(payload); - return new Diff(expectedDocument, actualDocument); + return DiffBuilder.compare(expectedDocument) + .withTest(actualDocument) + .checkForSimilar() + .build(); } private Document createDocumentFromSource(Source source) { diff --git a/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/SoapEnvelopeDiffMatcher.java b/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/SoapEnvelopeDiffMatcher.java index 9860c2920..b942ea9c7 100644 --- a/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/SoapEnvelopeDiffMatcher.java +++ b/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/SoapEnvelopeDiffMatcher.java @@ -24,12 +24,12 @@ import javax.xml.transform.TransformerException; import javax.xml.transform.dom.DOMResult; -import org.custommonkey.xmlunit.Diff; -import org.custommonkey.xmlunit.XMLUnit; import org.springframework.util.Assert; import org.springframework.ws.soap.SoapMessage; import org.springframework.xml.transform.TransformerHelper; import org.w3c.dom.Document; +import org.xmlunit.builder.DiffBuilder; +import org.xmlunit.diff.Diff; /** * Matches {@link Source} SOAP envelopes. @@ -43,10 +43,6 @@ public class SoapEnvelopeDiffMatcher extends AbstractSoapMessageMatcher { private final TransformerHelper transformerHelper = new TransformerHelper(); - static { - XMLUnit.setIgnoreWhitespace(true); - } - public SoapEnvelopeDiffMatcher(Source expected) { Assert.notNull(expected, "'expected' must not be null"); @@ -58,8 +54,12 @@ protected void match(SoapMessage soapMessage) throws IOException, AssertionError Document actualDocument = soapMessage.getDocument(); Document expectedDocument = createDocumentFromSource(expected); - Diff diff = new Diff(expectedDocument, actualDocument); - assertTrue("Envelopes are different, " + diff.toString(), diff.similar()); + Diff diff = DiffBuilder.compare(expectedDocument) + .ignoreWhitespace() + .withTest(actualDocument) + .checkForSimilar() + .build(); + assertTrue("Envelopes are different, " + diff.toString(), !diff.hasDifferences()); } private Document createDocumentFromSource(Source source) { diff --git a/spring-ws-test/src/test/java/org/springframework/ws/test/client/MockWebServiceServerTest.java b/spring-ws-test/src/test/java/org/springframework/ws/test/client/MockWebServiceServerTest.java index f0bc7cf1c..92ec2b98c 100644 --- a/spring-ws-test/src/test/java/org/springframework/ws/test/client/MockWebServiceServerTest.java +++ b/spring-ws-test/src/test/java/org/springframework/ws/test/client/MockWebServiceServerTest.java @@ -21,6 +21,7 @@ import static org.springframework.ws.test.client.RequestMatchers.*; import static org.springframework.ws.test.client.ResponseCreators.*; +import jakarta.xml.bind.annotation.XmlRootElement; import jakarta.xml.soap.MessageFactory; import java.net.URI; @@ -36,6 +37,7 @@ import org.springframework.context.support.StaticApplicationContext; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.Resource; +import org.springframework.oxm.jaxb.Jaxb2Marshaller; import org.springframework.ws.client.core.WebServiceTemplate; import org.springframework.ws.client.core.support.WebServiceGatewaySupport; import org.springframework.ws.soap.SoapMessage; @@ -46,14 +48,14 @@ import org.springframework.xml.transform.StringSource; import org.xmlunit.assertj.XmlAssert; -public class MockWebServiceServerTest { +class MockWebServiceServerTest { private WebServiceTemplate template; private MockWebServiceServer server; @BeforeEach - public void setUp() { + void setUp() { template = new WebServiceTemplate(); template.setDefaultUri("http://example.com"); @@ -62,7 +64,7 @@ public void setUp() { } @Test - public void createServerWebServiceTemplate() { + void createServerWebServiceTemplate() { WebServiceTemplate template = new WebServiceTemplate(); @@ -72,7 +74,7 @@ public void createServerWebServiceTemplate() { } @Test - public void createServerGatewaySupport() { + void createServerGatewaySupport() { MyClient client = new MyClient(); @@ -82,7 +84,7 @@ public void createServerGatewaySupport() { } @Test - public void createServerApplicationContextWebServiceTemplate() { + void createServerApplicationContextWebServiceTemplate() { StaticApplicationContext applicationContext = new StaticApplicationContext(); applicationContext.registerSingleton("webServiceTemplate", WebServiceTemplate.class); @@ -94,7 +96,7 @@ public void createServerApplicationContextWebServiceTemplate() { } @Test - public void createServerApplicationContextWebServiceGatewaySupport() { + void createServerApplicationContextWebServiceGatewaySupport() { StaticApplicationContext applicationContext = new StaticApplicationContext(); applicationContext.registerSingleton("myClient", MyClient.class); @@ -105,7 +107,7 @@ public void createServerApplicationContextWebServiceGatewaySupport() { } @Test - public void createServerApplicationContextEmpty() { + void createServerApplicationContextEmpty() { assertThatIllegalArgumentException().isThrownBy(() -> { @@ -118,7 +120,7 @@ public void createServerApplicationContextEmpty() { } @Test - public void mocks() throws Exception { + void mocks() throws Exception { URI uri = URI.create("http://example.com"); @@ -143,7 +145,7 @@ public void mocks() throws Exception { } @Test - public void payloadMatch() { + void payloadMatch() { Source request = new StringSource(""); Source response = new StringSource(""); @@ -157,7 +159,7 @@ public void payloadMatch() { } @Test - public void payloadNonMatch() { + void payloadNonMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> { @@ -172,7 +174,7 @@ public void payloadNonMatch() { } @Test - public void soapHeaderMatch() { + void soapHeaderMatch() { final QName soapHeaderName = new QName("http://example.com", "mySoapHeader"); @@ -186,7 +188,7 @@ public void soapHeaderMatch() { } @Test - public void soapHeaderNonMatch() { + void soapHeaderNonMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> { @@ -200,7 +202,7 @@ public void soapHeaderNonMatch() { } @Test - public void connectionMatch() { + void connectionMatch() { String uri = "http://example.com"; server.expect(connectionTo(uri)); @@ -210,7 +212,7 @@ public void connectionMatch() { } @Test - public void connectionNonMatch() { + void connectionNonMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> { @@ -224,7 +226,7 @@ public void connectionNonMatch() { } @Test - public void unexpectedConnection() { + void unexpectedConnection() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> { @@ -239,7 +241,7 @@ public void unexpectedConnection() { } @Test - public void xsdMatch() throws Exception { + void xsdMatch() throws Exception { Resource schema = new ByteArrayResource( "" @@ -253,7 +255,7 @@ public void xsdMatch() throws Exception { } @Test - public void xsdNonMatch() { + void xsdNonMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> { @@ -270,7 +272,7 @@ public void xsdNonMatch() { } @Test - public void xpathExistsMatch() { + void xpathExistsMatch() { final Map ns = Collections.singletonMap("ns", "http://example.com"); @@ -281,7 +283,7 @@ public void xpathExistsMatch() { } @Test - public void xpathExistsNonMatch() { + void xpathExistsNonMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> { @@ -295,7 +297,7 @@ public void xpathExistsNonMatch() { } @Test - public void anythingMatch() { + void anythingMatch() { Source request = new StringSource(""); Source response = new StringSource(""); @@ -311,7 +313,7 @@ public void anythingMatch() { } @Test - public void recordWhenReplay() { + void recordWhenReplay() { assertThatIllegalStateException().isThrownBy(() -> { @@ -331,7 +333,43 @@ public void recordWhenReplay() { } @Test - public void verifyFailure() { + void soapEnvelopeMatch() { + final Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); + marshaller.setClassesToBeBound(EnvelopeMatcherRequest.class, EnvelopeMatcherResponse.class); + + template = new WebServiceTemplate(marshaller); + template.setDefaultUri("http://example.com"); + server = MockWebServiceServer.createServer(template); + + Source expectedSoapRequest = new StringSource(""" + + + + + 123456 + + + """); + Source soapResponse = new StringSource(""" + + + + 654321 + + + """); + + server.expect(soapEnvelope(expectedSoapRequest)).andRespond(withSoapEnvelope(soapResponse)); + + final EnvelopeMatcherRequest r = new EnvelopeMatcherRequest(); + r.setMyData("123456"); + final EnvelopeMatcherResponse rs = (EnvelopeMatcherResponse) template.marshalSendAndReceive(r); + + assertThat(rs.getMyData()).isEqualTo("654321"); + } + + @Test + void verifyFailure() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> { @@ -341,12 +379,12 @@ public void verifyFailure() { } @Test - public void verifyOnly() { + void verifyOnly() { server.verify(); } @Test - public void fault() { + void fault() { assertThatExceptionOfType(SoapFaultClientException.class).isThrownBy(() -> { @@ -359,7 +397,35 @@ public void fault() { }); } - public static class MyClient extends WebServiceGatewaySupport { + static class MyClient extends WebServiceGatewaySupport { + + + } + @XmlRootElement(name = "EnvelopeMatcherRequest") + private static class EnvelopeMatcherRequest { + + private String myData; + + public String getMyData() { + return myData; + } + + public void setMyData(final String myData) { + this.myData = myData; + } + } + + @XmlRootElement(name = "EnvelopeMatcherResponse") + private static class EnvelopeMatcherResponse { + + private String myData; + + public String getMyData() { + return myData; + } + public void setMyData(final String myData) { + this.myData = myData; + } } }