/*
* XMLParserTest.java
*
* Created on March 15, 2002, 12:53 PM
*/
package ca.uhn.hl7v2.parser.tests;
import static org.ops4j.pax.exam.CoreOptions.equinox;
import static org.ops4j.pax.exam.CoreOptions.felix;
import static org.ops4j.pax.exam.CoreOptions.frameworks;
import static org.ops4j.pax.exam.CoreOptions.knopflerfish;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.systemProperty;
import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.logProfile;
import static org.junit.Assert.*;
import java.io.IOException;
import java.io.InputStream;
import ca.uhn.hl7v2.HL7Exception;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Inject;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.Configuration;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.osgi.framework.BundleContext;
import org.w3c.dom.Document;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.Segment;
import ca.uhn.hl7v2.model.Type;
import ca.uhn.hl7v2.parser.DefaultXMLParser;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.PipeParser;
import ca.uhn.hl7v2.parser.XMLParser;
/**
* JUnit test harness for XMLParser
* @author Bryan Tripp
* @author Niranjan Sharma niranjan.sharma@med.ge.com This testcase has been
* extended for OSGI environment using Junit4 and PAX-Exam.
*/
@RunWith(JUnit4TestRunner.class)
public class XMLParserTest {
// you get that because you "installed" a log profile in configuration.
public Log logger = LogFactory.getLog(XMLParserTest.class);
@Inject
BundleContext bundleContext;
@Configuration
public static Option[] configure() {
return options(frameworks(equinox(), felix(), knopflerfish())
, logProfile()
, systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("INFO")
, mavenBundle().groupId("org.ops4j.pax.url").artifactId("pax-url-mvn").version("0.4.0")
, wrappedBundle(mavenBundle().groupId("org.ops4j.base").artifactId("ops4j-base-util").version("0.5.3"))
, mavenBundle().groupId("ca.uhn.hapi").artifactId("hapi-osgi-base").version("1.0-beta1")
// , vmOption("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5006" )
);
}
XMLParser parser;
/** Creates a new instance of XMLParserTest
* @throws HL7Exception */
@Before
public void BeforeTheTest() throws HL7Exception {
parser = new DummyXMLParser();
}
/**
* Tests a fix to bug 2164291
*
* XML parsing of segments which appear twice in a message structure definition (e.g. the
* duplicate PID segments in a swap patient message) should be handled correctly.
*/
@Test
public void testParseDuplicateSegment() throws IOException, EncodingNotSupportedException, HL7Exception {
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("ca/uhn/hl7v2/parser/tests/adt_a17.xml");
byte[] bytes = new byte[10000];
StringBuffer buffer = new StringBuffer();
int count;
while ((count = stream.read(bytes)) > 0) {
buffer.append(new String(bytes), 0, count);
}
String xmlMessage = buffer.toString();
Message message = new DefaultXMLParser().parse(xmlMessage);
String er7Message = new PipeParser().encode(message).replaceAll("\\r", "\r\n");
System.out.println("Re-encoded:\r\n" + er7Message);
// We should have only two reps of PID
int firstIndex = er7Message.indexOf("PID");
int secondIndex = er7Message.indexOf("PID", firstIndex + 1);
int thirdIndex = er7Message.indexOf("PID", secondIndex + 1);
assertTrue(firstIndex > 0);
assertTrue(secondIndex > firstIndex);
assertTrue("Found third PID " + firstIndex + " " + secondIndex + " " + thirdIndex + ":\r\n" + er7Message, thirdIndex == -1);
}
@Test
public void testGetAckID() throws HL7Exception {
assertEquals(parser.getAckID("12"), "12");
assertEquals(parser.getAckID(" help >>> *** x");
assertEquals(null, ackID);
ackID = parser.getAckID("x");
assertEquals(null, ackID);
ackID = parser.getAckID(" 12 \r");
assertEquals("12", ackID);
ackID = parser.getAckID("|^~/&MPIISOHealthLinkISOUHN VistaISOUHNISO200204292049RSPK22RSP_K22200204292049100799P2.4Q22AA876OKQ22");
assertEquals("876", ackID);
}
@Test
public void testGetEncoding() throws Exception {
String test1 = "\r|\r^~\\&\r";
String test2 = "\r|\r^~\\&\r"; //bad: no
assertEquals("XML", parser.getEncoding(test1));
assertEquals(null, parser.getEncoding(test2));
}
/*public void testParse() throws Exception {
String test1 = "\r|\r^~\\&\r";
Message m = parser.parse(test1);
assertEquals(null, m);
}*/
@Test
public void testGetVersion() throws Exception {
String message = " | ^~/& UHN Vista ISO UHN ISO MPI ISO HealthLink ISO 20020429132718.734-0400 QBP Q22 QBP_Q21 855 P 2.4 Q22 Q22 Find Candidates HL7nnnn @PID.3.19583518684@PID.3.4.1CANON@PID.5.1.1ECG-Acharya@PID.5.2Nf@PID.5.7L@PID.7197104010000@PID.8M 100 TTH 13831ULTIuser2234564R&H Med I 100RD R ";
String ver = parser.getVersion(message);
assertEquals("2.4", ver);
}
// public void testRemoveWhitespace() throws Exception {
// assertEquals("hello", parser.removeWhitespace("\t\r\nhello "));
// assertEquals("hello there", parser.removeWhitespace(" hello \t \rthere\r\n"));
// }
/**
* -
* @throws HL7Exception -
*/
@Test
public void testGetCriticalResponseData() throws HL7Exception {
String message = "\r\n" +
" \r\n" +
" |\r\n" +
" ^~\\&\r\n" +
" LABMI1\r\n" +
" DMCRES\r\n" +
" \r\n" +
" 19951010134000\r\n" +
" \r\n" +
" \r\n" +
" ORU\r\n" +
" R01\r\n" +
" \r\n" +
" LABMI1199510101340007\r\n" +
" D\r\n" +
" 2.2\r\n" +
" AL\r\n" +
" ";
Segment data = parser.getCriticalResponseData(message);
Type actual = data.getField(2, 0);
String expected = "^~\\&";
assertEquals(expected, actual.toString()); // Encoding
}
public class DummyXMLParser extends XMLParser {
public DummyXMLParser() throws HL7Exception {
super();
}
public Message parseDocument(Document XMLMessage, String version) throws HL7Exception {
return null;
}
public Document encodeDocument(Message source) throws HL7Exception {
return null;
}
}
}