citrusframework / citrus

Framework for automated integration tests with focus on messaging integration
https://citrusframework.org
Apache License 2.0
456 stars 134 forks source link

Citrus ignores XML encoding #744

Open bthdimension opened 3 years ago

bthdimension commented 3 years ago

Citrus Version 2.8.0

Expected behavior When validating XML payloads using the "payload()" method, I expect Citrus to respect the encoding set in the XML declaration.

Actual behavior Citrus ignores the encoding and uses the default encoding. My current workaround is therefore to manually set the encoding via the citrus-application.properties file. This is dangerous, as the XML file might not always have that specific encoding. Tests that should fail might therefore pass.

Test case sample In the following example, I merge two XMLs using an XSLT in a Camel route. The XML file loaded by Citrus ignores the UTF-8 encoding, the one read by Camel honors it. When validating the response, some of the values merged from the file loaded by Camel are seen as faulty with Errors like this: Node value not equal for element 'vorname', expected 'Véréna' but was 'Véréna'

context.addRouteDefinition(new RouteDefinition()
                .from("direct:testXsltMerge")
                .enrich("language:constant:resource:classpath:data/file1.xml",
                        xsltMergeStrategy));
send("camel:sync:direct:testXsltMerge")
        .payload(new ClassPathResource("data/file2.xml"));
receive("camel:sync:direct:testXsltMerge")
        .payload(new ClassPathResource("data/expectedResult.xml"));
christophd commented 3 years ago

You can add the charset in the send/receive actions like this:

send("camel:sync:direct:testXsltMerge")
        .payload(new ClassPathResource("data/file2.xml"), StandardCharset.UTF-8);
receive("camel:sync:direct:testXsltMerge")
        .payload(new ClassPathResource("data/expectedResult.xml"), StandardCharset.UTF-8);

The problem is that the file resource is read independent of its content nature (XML, plaintext, Json, ...). You could of course also add your own custom logic that reads the file content with XML specific encoding taken into account. You would then pass the read file content as a string to the send/receive action.