cancel
Showing results for 
Search instead for 
Did you mean: 

How to ignore nodes with It values when comparing two xmls with XMLUnit2

SOLVED
Occasional Contributor

How to ignore nodes with It values when comparing two xmls with XMLUnit2

Comparing two xml files in soapui using groovy with XMLUnit2, Have some weird cases which are different in both files, but are acceptable. So have to ignore/skip those.

 

ResXML1:

<alpha:Errors>
    <alpha:Error ShortText="SHiff" Type="11">Generic error occured.</alpha:Error>
    <alpha:Error ShortText="SHiff" Type="12">Generic error occured.</alpha:Error>
</alpha:Errors>
<ShiftAttributes>
    <ShiftAttribute ShiftAttributeCode="41"/>
    <ShiftAttribute ShiftAttributeCode="17"/>
</ShiftAttributes>
<alpha:PriceInfo AgeQualifyingCode="10" Amount="19999.00" DiscountTypes="3" AMZAmount="225.00" NetAmount="19999.00" MCH="1" ValueAddAmount="150.00"/>


ResXML2:

<alpha:Warnings>
    <alpha:Warning ShortText="SHiff" Type="11">Generic error occurred.</alpha:Warning>
</alpha:Warnings>
<ShiftAttributes>
    <ShiftAttribute ShiftAttributeCode="17"/>
    <ShiftAttribute ShiftAttributeCode="41"/>
</ShiftAttributes>
<alpha:PriceInfo AgeQualifyingCode="10" Amount="19999.0" DiscountTypes="3" AMZAmount="225.0" NetAmount="19999.0" MCH="1" ValueAddAmount="150.0"/>



If anyone have any idea, please help me in below conditions.

1) How to ignore child attribute order? tried different nodematchers(byXpath, byNameandText etc..), but no luck.
<ShiftAttribute ShiftAttributeCode="41"/>
<ShiftAttribute ShiftAttributeCode="17"/>

2) How to ignore decimal value, 100.0 & 100.00. Tried to use BigDecimalElementDifferenceEvaluator, but cannot able to complete in soapui.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Occasional Contributor

Re: How to ignore nodes with It values when comparing two xmls with XMLUnit2

Found solution for first part of the above question

diff = DiffBuilder.compare(testxml)
.withTest(resxml)
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder().whenElementIsNamed("ShiftAttribute").thenUse(ElementSelectors.byNameAndAttributes("ShiftAttributeCode")).elseUse(ElementSelectors.byName).build()))
.build();

View solution in original post

3 REPLIES 3
Highlighted
Community Hero

Re: How to ignore nodes with It values when comparing two xmls with XMLUnit2

It would be easy if you can show what you tried to say the above is not working.


Regards,
Rao.
Highlighted
Occasional Contributor

Re: How to ignore nodes with It values when comparing two xmls with XMLUnit2

@nmrao 

Thanks for reply, I tried below codes,

1) For Decimal:

class BigDecimalElementDifferenceEvaluator implements DifferenceEvaluator {

private String elementName;

public BigDecimalElementDifferenceEvaluator(String elementName) {
this.elementName = elementName;
}

@Override
public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) {
if (outcome == ComparisonResult.EQUAL) return outcome; // only evaluate differences.
final Node controlNode = comparison.getControlDetails().getTarget();
final Node testNode = comparison.getTestDetails().getTarget();
if (controlNode.getParentNode() instanceof Element && testNode.getParentNode() instanceof Element) {
Element controlElement = (Element) controlNode.getParentNode();
Element testElement = (Element) testNode.getParentNode();
if (controlElement.getNodeName().equals(elementName)) {
final String controlValue = controlElement.getTextContent();
final String testValue = testElement.getTextContent();
if (new BigDecimal(controlValue).compareTo(new BigDecimal(testValue)) == 0) {
return ComparisonResult.SIMILAR;
}
}
}
return outcome;
}
}


//Implementation

Diff myDiff = DiffBuilder.compare(control).withTest(test)
.withDifferenceEvaluator(new BigDecimalElementDifferenceEvaluator("amount"))
.checkForSimilar()
.build();

 

Error Getting: Caught exception during comparision.

 

2) Element Order

.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder().whenElementIsNamed("ShiftAttributes").thenUse(ElementSelectors.byXPath('./ShiftAttribute', ElementSelectors.byNameAndAllAttributes)).elseUse(ElementSelectors.byName).build()))

Error: But it's not handling order.

Highlighted
Occasional Contributor

Re: How to ignore nodes with It values when comparing two xmls with XMLUnit2

Found solution for first part of the above question

diff = DiffBuilder.compare(testxml)
.withTest(resxml)
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder().whenElementIsNamed("ShiftAttribute").thenUse(ElementSelectors.byNameAndAttributes("ShiftAttributeCode")).elseUse(ElementSelectors.byName).build()))
.build();

View solution in original post