Hi Derek,
One other option is to use the Hl7V2MessageCompare class from the
hapi-testpanel subproject. This should probably get moved to the main HAPI
library at some point, so it's a bit annoying to import for now, but it's
certainly very robust. We use it internally at UHN quite a bit.
You can see a bit about it's output here (note that the Hl7V2MessageCompare
class itself has no dependency on swing, so it works just fine in a unit
test): http://hl7api.sourceforge.net/hapi-testpanel/hl7v2filediff.html
Cheers,
James
On Wed, Aug 8, 2012 at 1:56 PM, Rahul Somasunderam <
[email protected]> wrote:
> I wrote up a method using LightHL7Lib:
>
> public void assertMessagesEqual(Hl7Record expected, Hl7Record actual) {
> List<String> headers = expected.listSegments();
> headers.addAll(actual.listSegments());
> assertEquals(expected.listSegments(), actual.listSegments());
> Set<String> segments = new TreeSet<String>(headers);
>
> for (String segmentName : segments) {
> if (segmentName.contains("|")) {
> break;
> }
> int i = 0;
> while (true) {
> i++;
> Hl7Segment expectedSeg = expected.getSegment(segmentName, i);
> Hl7Segment actualSeg = actual.getSegment(segmentName, i);
>
> if (expectedSeg == null && actualSeg == null) {
> break;
> }
> if (expectedSeg == null || actualSeg == null) {
> assertEquals("Comparing " + segmentName + "[" + i +
> "].",expectedSeg, actualSeg);
> }
>
> assertNotNull("Comparing " + segmentName + "[" + i + "].",
> actualSeg);
>
> int j ;
> if (segmentName.equals("MSH")) {
> j = 2;
> } else {
> j = 1;
> }
>
> int maxFields = expectedSeg.fieldAll().length;
> if (maxFields < actualSeg.fieldAll().length) {
> maxFields = actualSeg.fieldAll().length;
> }
> for ( /*Use j as is*/; j <= maxFields ; j++) {
> if (expectedSeg.field(j) == null /*||
> expectedSeg.field(j).toString().length() == 0*/) {
> continue;
> }
>
> /* Check if field exists */
> assertNotNull("Comparing " + segmentName + "[" + i + "]." + j,
> actualSeg.field(j));
>
> if (expectedSeg.field(j).getCompCount() > 1) {
> /* Check components */
> for (int k = 0; k < expectedSeg.field(j).getCompCount(); k++) {
> if (expectedSeg.field(j)
> .getComp(k + 1) == null /*||
> expectedSeg.field(j).getComp(k+1).toString().length() == 0*/) {
> continue;
> }
> assertNotNull("Comparing " + segmentName + "[" + i + "]." +
> j + "." + k,
> actualSeg.field(j).getComp(k + 1));
> if (expectedSeg.field(j).getComp(k + 1).getSubcompCount() >
> 1) {
> /* Check subcomponents */
> for (int l = 0; l < expectedSeg.field(j).getComp(k +
> 1).getSubcompCount(); l++) {
> if (expectedSeg.field(j).getComp(k + 1).getSubcomp(l +
> 1) == null
> /*||
> expectedSeg.field(j).getComp(k+1).getSubcomp(l+1).toString().length() ==
> 0*/) {
> continue;
> }
> assertNotNull("Comparing " + segmentName + "[" + i +
> "]." + j + "." + k + "." + l,
> actualSeg.field(j).getComp(k + 1).getSubcomp(l + 1));
> assertEquals("Comparing " + segmentName + "[" + i + "]."
> + j + "." + k + "." + l,
> expectedSeg.field(j).getComp(k + 1).getSubcomp(l +
> 1).toString(),
> actualSeg.field(j).getComp(k + 1).getSubcomp(l +
> 1).toString());
>
> }
> } else {
> assertEquals("Comparing " + segmentName + "[" + i + "]." +
> j + "." + (k + 1),
> expectedSeg.field(j).getComp(k + 1).toString().trim(),
> actualSeg.field(j).getComp(k + 1).toString().trim());
> }
> }
> } else {
> assertEquals("Comparing " + segmentName + "[" + i + "]." + j,
> expectedSeg.field(j).toString().trim(),
> actualSeg.field(j).toString().trim());
> }
> }
> }
> }
> }
>
> It gives detailed information on which segment and which field are
> different. Better than plain diff, but not using HAPI.
>
> R,
> rahul
>
> On Aug 8, 2012, at 8:58 AM, Derek Mahar wrote:
>
> Is comparing the string representation of a HAPI message the only way to
> compare messages? We noticed that HAPI messages don't redefine method
> equals(), so comparisons using JUnit assertions or Mockito verify() calls
> must use Object.equals() which compares object identity, not contents. Are
> there any plans to implement methods equals() and hashCode() in messages
> and segments?
>
>
>
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> Hl7api-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/hl7api-devel
>
>
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Hl7api-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/hl7api-devel