2

I want to compare two XML files in SoapUI (Groovy) which are similar but the child nodes are not in sequence. I'm using XML unit v2.3.0.

XML1:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
</env:Header>
<soap:Body>
<Details>
<RateType RateTypeID="6">
    <RateType>AAAAA</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
<RateType RateTypeID="3">
    <RateType>BBB</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
<RateType RateTypeID="41">
    <RateType>CCC</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
<RateType RateTypeID="43">
    <RateType>DDD</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
</Details>
</soap:Body>
</soap:Envelope>

XML2:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/03/addressing" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
</env:Header>
<soap:Body>
<Details>
<RateType RateTypeID="41">
    <RateType>CCC</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
<RateType RateTypeID="43">
    <RateType>DDD</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
<RateType RateTypeID="6">
    <RateType>AAAAA</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
<RateType RateTypeID="3">
    <RateType>BBB</RateType>
    <BaseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <UOM>percent</UOM>
</RateType>
</Details>
</soap:Body>
</soap:Envelope>

In the above example both the XML's are similar in content but only the sequence differs. I want to compare both of them to know if they are equal.

When I ran the below code:

Diff myDiffSimilar = DiffBuilder.compare(XML1))
            .withTest(XML2)
            .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder().whenElementIsNamed("Rate").thenUse(ElementSelectors.selectorForElementNamed("RateValue", ElementSelectors.byNameAndAllAttributes)).build()))
            .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder().whenElementIsNamed("RateType").thenUse(ElementSelectors.selectorForElementNamed("RateType", ElementSelectors.byNameAndAllAttributes)).build()))
            .checkForSimilar().build();

log.info myDiffSimilar.getDifferences().toString();

It gives me the following output

[Expected child '{http://schemas.xmlsoap.org/soap/envelope/}Envelope' but was 'null' - comparing <soap:Envelope...> at /Envelope[1] to <NULL> (DIFFERENT), Expected child 'null' but was '{http://schemas.xmlsoap.org/soap/envelope/}Envelope' - comparing <NULL> to <soap:Envelope...> at /Envelope[1] (DIFFERENT)]

Can someone advice me on the element selector/conditional builder that should be used in this scenario?

KRS
  • 21
  • 3
  • Welcome to Stack Overflow! It would certainly help for anyone who would like to reproduce your output if you could post your XML as text and not as an image. – haindl Nov 30 '16 at 21:28
  • Possible duplicate - http://stackoverflow.com/questions/40743664/groovy-compare-soap-response-with-xml-file – Rao Dec 01 '16 at 12:05
  • @Rao Not Really... Tried that already... Here is the output.. Expected child nodelist length '17' but was '16' - comparing.......................... true false – KRS Dec 02 '16 at 01:42
  • I could compare successfully. Would it be possible for a screen shot of full attack trace. – Rao Dec 02 '16 at 03:10

1 Answers1

0

Try to use this :

Diff diff = DiffBuilder.compare(actual)
            .withTest(expected)
            .ignoreComments()
            .ignoreWhitespace()
            .checkForSimilar()
            .withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(), ElementSelectors.byName))
            .build();

assert diff.hasDifferences()==false
Hamza Amami
  • 94
  • 4
  • 21