One of the key testing elements of this approach to testing HL7 integration scenarios is how we compare HL7 messages. We might have an instance where the stub of an HL7 application has received a message and we want to check that the message will match a template or expected message. The normal approach I would take to do this is to get the stub to save a copy of the message at a known location so that the Specflow test can at the next stage compare the saved message against the expected message.

Note that I tend not to explicitly do this comparison in the stub because it will be hidden away from the BDD specification some what and we lose a little of the documentation capabilities provided by the test and it is not obvious where this validation takes place.

When I come to do the validation however there are some framework features to help.

Comparable HL7 String

HL7 messages have some data elements that change as the message flows through the system and some elements which are difficult to assume what the value will be. An example of this could be a date/time field. This value may change depending on when the test is ran.

In the framework you can create an instance of the HL7Message object providing your HL7 string and then use the .ToComparableString() method. This method will change parts of the message which are known to fall into this problem. At present this is really just the message date and time field but in the future this may be extended to include other fields. An example getting the comparable HL7 string is below.

var myMessage = new HL7Message(actualMessage);
var s = myMessage.ToComparableString();

Compare HL7 Strings

If you had 2 strings containing HL7 data and wanted to compare them using this comparable string approach then you could use an example like in the below sample:

        public static void CompareHL7Strings(string expectedMessage, string actualMessage)
            var expectedHL7Message = new HL7Message(expectedMessage);            
            var actualHL7Message = new HL7Message(actualMessage);

            Assert.AreEqual(expectedHL7Message.ToComparableString(), actualHL7Message.ToComparableString(), "The comparable HL7 messages do not match");

Compare HL7 saved to files

Sometimes you have have saved some HL7 data to a file and you may wish to compare 2 files. The below example (combined with the above compare HL7 strings) shows how you can do this.

        public static void CompareHL7Files(string expectedPath, string actualPath)
            var expectedMessage = FileHelper.ReadContentFromFile(expectedPath);            
            var actualMessage = FileHelper.ReadContentFromFile(actualPath);

            CompareHL7Strings(expectedMessage, actualMessage);

Overriding Compare Approach

If you wanted to change the comparison approach you can of course do something completely different but what might work well is if you load your HL7 data into an HL7Message object first which will give you a way to interact with some of the fields in some segments.

Last edited Apr 21, 2014 at 1:24 AM by michaelstephenson, version 2