... or there's ca.uhn.hl7v2.util.EncodedMessageComparator, which provides a
couple of methods that help standardizing and comparing messages.

cheers
Christian



rahul somasunderam-2 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
> Hl7api-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/hl7api-devel
> 
> 

-- 
View this message in context: 
http://old.nabble.com/Comparing-messages-tp34272587p34273533.html
Sent from the hl7api-devel mailing list archive at Nabble.com.


------------------------------------------------------------------------------
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
Hl7api-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hl7api-devel

Reply via email to