I hope you don't mind me coming back to this issue, as it kind of piqued my interest.
Firstly I always felt a bit uncomfortable with the idea of altering the response XML to make the comparison, and secondly I noticed in you were using a third party library XMLUnit. This is something I had not come across before your post, and was unable to research at the time of answering your question, but since then have had time to investigate.
From the XMLUnit web page: "The most important part is a diff-engine that provides you with full control over what kind of difference is important to you and which part of the generated document to compare with which part of your reference document."
Seeing as you had already gone to the trouble of installing the XMLUnit libraries, and were using the diff engine, I thought there should be a way of leveraging the XMLUnit functionality to do what you wanted.
The following code answers your original question, without having to resort to hacking your response XML. What I really like is that (and I hope you'll agree) it that the code is very readable and you can easily understand what the intention is. I must admit that with your original code, to me it was not very obvious what the intent was.
import javax.xml.transform.Source
import org.w3c.dom.Attr
import org.w3c.dom.Node
import org.xmlunit.builder.DiffBuilder
import org.xmlunit.builder.Input
import org.xmlunit.util.Predicate
// Fixed control XML to compare responses too.
def controlXmlText = '''<response>
<payload>
<selfcare>
<subscription id="2306" status="24">
<charging-method>3</charging-method>
<duration-code>2</duration-code>
<rate resource="GBP" tax-rate="0.185">2.37</rate>
<pricepoint/>
<purchase-date>2017-04-18T14:12:39+05:30</purchase-date>
<expiry-date>2017-04-25T23:59:59+05:30</expiry-date>
<package-id>ReservedPackage</package-id>
<b2b-partner>
<id>B2B_Google</id>
</b2b-partner>
<partner-id>B2B_Google</partner-id>
</subscription>
</selfcare>
</payload>
</response>'''
// Response XML has differences in the two date elements and the id attributes, which
// are ignored by the filters below, edit other elements and attributes to see the
// assertion below identify other differences.
def responseXmlText = '''<response>
<payload>
<selfcare>
<subscription id="2310" status="24">
<charging-method>3</charging-method>
<duration-code>2</duration-code>
<rate resource="GBP" tax-rate="0.185">2.37</rate>
<pricepoint/>
<purchase-date>2017-04-19T14:12:39+06:30</purchase-date>
<expiry-date>2017-04-26T23:59:59+06:30</expiry-date>
<package-id>ReservedPackage</package-id>
<b2b-partner>
<id>B2B_Google</id>
</b2b-partner>
<partner-id>B2B_Google</partner-id>
</subscription>
</selfcare>
</payload>
</response>'''
// Control and response sources, this demo is just from text strings, See:
// http://www.xmlunit.org/api/java/2.3.0/org/xmlunit/builder/Input.html
// For details of all other input source.
def controlXml = Input.from(controlXmlText).build();
def responseXml = Input.from(responseXmlText).build();
def nodeFilter = new Predicate<Node>() {
public boolean test(Node element) {
// List of element names to ignore.
def elementsToIgnore = ['purchase-date', 'expiry-date']
return !elementsToIgnore.contains(element.getLocalName())
}
}
def attributeFilter = new Predicate<Attr>() {
public boolean test(Attr attribute) {
// List of attributes names to ignore (regardless of element).
def attributesToIgnore = ['id']
return !attributesToIgnore.contains(attribute.getLocalName())
}
}
def diff = DiffBuilder.compare(controlXml)
.withTest(responseXml)
.withNodeFilter(nodeFilter)
.withAttributeFilter(attributeFilter)
.ignoreComments()
.ignoreWhitespace()
.build()
assert !diff.hasDifferences()
I was really just investigating XMLUnit for my own interest, but I thought I'd post back here what I found as someone might find it useful. If so here is the user guide and javadocs.