jeudi 16 janvier 2020

How to ignore namespaces when testing a HttpRequest's body with the MockServer

After quite some debugging I found out that when adding new classes to my unmarshaller the namespace for the new class gets added to the xml when a webservice call is made through the spring webservice template. This does not cause the implementation to break but only my tests. The problem is that there are expected request xml files created for the mockserver in the tests. The mockserver tests the unmarshalled xml from the application against the manually created xml file. If it matches then it returns a specific response otherwise a null is returned which fails the test, as it should. I just don't want it to care about namespaces.

I have to go through all the created xml files and add that new namespace. It is a big project with 100+ xml files. Adding to the complexity, only certain xml files need the new namespace so if I do a global find and replace, other tests will break.

Here is an example of how the mockserver do the comparison. I am using the org.mockserver framework.

@Bean
public StreamSource TestRequestXML() {
    String fileName = "./some/test/resource/TestRequestXML.xml";
    return new StreamSource(Paths.get(fileName).toFile());
}

@Bean
public MockServerClient amendedMockServerClient(MockServerClient mockServerClient) {
    mockServerClient.when(request().withBody(xml(sourceToString(TestRequestXML()))))
            .respond(response().withStatusCode(200));
}

This is my unmarshaller bean

@Bean
public Jaxb2Marshaller unmarshaller() {
    marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(Foo.class, Bar.class, FooBar.class);
    marshaller.setLazyInit(false);
    return marshaller;
}

Here is an example of what an unmarshalled request looks like.

<req:RequestBatch schemaVersion="1.5E2"
                  xmlns:req="http://schemas.org/draft/request"
                  xmlns:foo="http://schemas.org/draft/foo"
                  xmlns:bar="http://schemas.org/draft/bar"
                  xmlns:foobar="http://schemas.org/draft/foobar">
    <foo:some>test</foo:some>
    <bar:some>test</bar:some>
</req:RequestBatch>

Here is an example of a created xml file that the mockserver expects.

<req:RequestBatch schemaVersion="1.5E2"
                  xmlns:req="http://schemas.org/draft/request"
                  xmlns:foo="http://schemas.org/draft/foo"
                  xmlns:bar="http://schemas.org/draft/bar">
    <foo:some>test</foo:some>
    <bar:some>test</bar:some>
</req:RequestBatch>

By adding the FooBar.class to the unmarshaller, the application now adds the FooBar namespace to the unmarsahlled request. To keep the mockserver from breaking my tests I need to add the FooBar namesapce to all the xml files for the tests the unmarshaller affects.

Instead of manually adding that namespace to all the specific xml files is there a smarter way to solve this problem?

Aucun commentaire:

Enregistrer un commentaire