Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed behavior for PayloadDiffMatcher #1203

Closed
RazorNd opened this issue May 31, 2021 · 12 comments
Closed

Changed behavior for PayloadDiffMatcher #1203

RazorNd opened this issue May 31, 2021 · 12 comments
Assignees
Milestone

Comments

@RazorNd
Copy link

RazorNd commented May 31, 2021

You changed the behavior for PayloadDiffMatcher by removing the static initialization block from DiffMatcher. Whitespaces are no longer ignored when checking a payload. Changes in current commit

RazorNd referenced this issue May 31, 2021
Also upgrade XMLUnit to the 2.x version, using its AssertJ integration.
@ondrejlerch
Copy link

I suggest to put back static { XMLUnit.setIgnoreWhitespace(true); } to DiffMatcher to avoid broken build investigation during spring-ws update as in my case. At least, it should be clearly documented in migration notes in case this is a feature and not a bug.

@gregturn
Copy link
Contributor

gregturn commented Jan 13, 2023

Possible duplicate of #1193. See if Spring WS 3.1.5 resolves the problem.

@mathieu-amblard
Copy link

Hello,

I have the same issue after migrating from Spring Boot 2.7.7 (Spring WS 3.1.4) to Spring Boot 2.7.8 (Spring WS 3.1.5).

Here is my test setup :

import org.custommonkey.xmlunit.XMLUnit;
import static org.springframework.ws.test.client.RequestMatchers.payload;

@SpringBootTest(...)
class MyTest {

    static {
        XMLUnit.setIgnoreWhitespace(true);
    }

    @Test
    void test() {
        MockWebServiceServer mockWSServer = MockWebServiceServer.createServer(...);
        Source expectedRequestPayload = new ResourceSource(new ClassPathResource("request.xml"));
        Source responsePayload = new ResourceSource(new ClassPathResource("response.xml"));
        mockWSServer.expect(payload(expectedRequestPayload)).andRespond(withPayload(responsePayload));
        // ...
    }
}

org.springframework.ws.test.client.RequestMatchers.payload; uses org.custommonkey.xmlunit.Diff; with Spring WS 3.1.4 behind the scene whereas it uses org.xmlunit.diff.Diff; (xmlunit 2) with Spring WS 3.1.5.

I do not find any equivalent to XMLUnit.setIgnoreWhitespace(true); for xmlunit 2.

Do you please have any suggestions ?

Thank you in advance

@marconak-itera
Copy link

We are using WhitespaceStrippedSource

        var searchRequestContent = new WhitespaceStrippedSource(new StringSource(
                Files.readString(searchRequest.getFile().toPath(), StandardCharsets.UTF_8)
        ));

@mathieu-amblard
Copy link

@marconak-itera Works like a charm 👍 !
Thank you very much.

@bgK
Copy link

bgK commented Feb 8, 2023

@gregturn, the new XmlUnit 2 implementation of PayloadDiffMatcher does not ignore whitespaces whereas the new implementation of SoapEnvelopeDiffMatcher does. It seems to me PayloadDiffMatcher should be fixed to ignore whitespaces to restore the initial behavior:

diff --git a/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/xmlunit2/PayloadDiffMatcher.java b/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/xmlunit2/PayloadDiffMatcher.java
index 45721351..bb276fff 100644
--- a/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/xmlunit2/PayloadDiffMatcher.java
+++ b/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/xmlunit2/PayloadDiffMatcher.java
@@ -59,6 +59,7 @@ public class PayloadDiffMatcher extends DiffMatcher {
                Document expectedDocument = createDocumentFromSource(expected);
                Document actualDocument = createDocumentFromSource(payload);
                return DiffBuilder.compare(expectedDocument) //
+                               .ignoreWhitespace() //
                                .withTest(actualDocument) //
                                .checkForSimilar() //
                                .build();

@RazorNd
Copy link
Author

RazorNd commented Feb 8, 2023

@gregturn sorry for the long answer. I check version 3.1.5, but behavior for PayloadDiffMatcher in this version is still different from what it was in version 3.0.10.RELEASE.

@gregturn
Copy link
Contributor

Can you provide a detailed test case illustrating the issue? That would help pinpoint the issue.

@RazorNd
Copy link
Author

RazorNd commented Apr 23, 2023

Test case that illustrating this issue:

Endpoint

package ru.razornd.ws;

import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import org.springframework.xml.transform.StringSource;

import javax.xml.transform.Source;

@Endpoint
public class TestEndpoint {
    @PayloadRoot(localPart = "Request")
    @ResponsePayload
    public Source example() {
        return new StringSource("""
                <response>
                  <success>true</success>
                </response>
                """);
    }
}

Test class

package ru.razornd.ws;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.webservices.server.WebServiceServerTest;
import org.springframework.ws.test.server.MockWebServiceClient;
import org.springframework.xml.transform.StringSource;

import static org.springframework.ws.test.server.RequestCreators.withPayload;
import static org.springframework.ws.test.server.ResponseMatchers.payload;

@WebServiceServerTest(endpoints = TestEndpoint.class)
class TestEndpointTest {

    @Autowired
    MockWebServiceClient client;

    @Test
    void example() {
        client.sendRequest(withPayload(new StringSource("<Request><Example>42</Example></Request>")))
                .andExpect(payload(new StringSource("<response><success>true</success></response>")));

    }
}

if you run this test against version 3.0.10.RELEASE spring-ws-test it will succeed. But if you raise the version (for example, to 3.1.1), then an error will begin to appear that the messages are different.

Messages are different, org.custommonkey.xmlunit.Diff
[different] Expected number of child nodes '1' but was '3' - comparing <response...> at /response[1] to <response...> at /response[1]

Payload: <response>
  <success>true</success>
</response>
org.springframework.ws.test.support.SourceAssertionError: Messages are different, org.custommonkey.xmlunit.Diff
[different] Expected number of child nodes '1' but was '3' - comparing <response...> at /response[1] to <response...> at /response[1]

Payload: <response>
  <success>true</success>
</response>

That is, in other words, before version 3.1.1 you ignored spaces, and after that you started to take them into account when comparing messages.

@mfejzer
Copy link
Contributor

mfejzer commented Jun 7, 2023

I've opened pull request with fix and minimal test case - without ".ignoreWhitespace() " it fails with "Messages are different, Expected child nodelist length '3' but was '1' ".
Could anybody review?

gregturn added a commit that referenced this issue Jul 14, 2023
@gregturn
Copy link
Contributor

Merged to main.

@gregturn gregturn self-assigned this Jul 14, 2023
@gregturn gregturn added this to the 4.0.6 milestone Jul 14, 2023
gregturn added a commit that referenced this issue Jul 14, 2023
@gregturn
Copy link
Contributor

Backported to 3.1.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants