1:/*
   2: * TestHl7Util.java
   3: *
   4: * Created on March 27, 2006, 1:58 PM
   5: *
   6: * Copyright (C) 2004-2007 M Litherland
   7: */
   8:
   9:package org.nule.lighthl7lib.tests;
  10:
  11:import java.util.*;
  12:
  13:import junit.framework.TestCase;
  14:
  15:import org.nule.lighthl7lib.hl7.*;
  16:import org.nule.lighthl7lib.util.*;
  17:
  18:/**
  19: *
  20: * @author litherm
  21: */
  22:public class TestHl7Util extends TestCase {
  23:    private static final String testRec1 = "MSH|^~\\&|NULEORG|HL7BROWSER_TNG|||" + 
  24:            "20040825120000||ACK||P|2.3||||NE|\rZZZ|this^is^not^valid|hl7|" +
  25:            "but~I^don't~care|even^if&I&did&I^wouldn't^tell|you\r";
  26:    private static final String testRec2 = "MSH|^~\\&|\rSCH|\rNTE|\rNTE|\r" +
  27:            "PID|\rAIL|\rAIP|\rNTE|\rNTE|\rNTE|\r";
  28:    private static final String testRec3 = "MSH|^~\\&|\rSCH|\rNTE|\rNTE|\r" +
  29:            "PID|\rAIL\rAIP|\rNTE|\rNTE|\rNTE|\r";
  30:    private static final String testRec4 = "MSH|^~\\&|\rSCH|\rNTE|\rNTE|\r" +
  31:            "PID|\rAIL\rAIP|\rNTE|\rNTE|\rNTE\r";
  32:    
  33:    private static final String testFields1 = "";
  34:    
  35:    private Hl7Record h7u;
  36:    
  37:    public void setUp() {
  38:        h7u = new Hl7Record(testRec1);
  39:        //System.out.println(testRec1);
  40:    }
  41:    
  42:    public void testSeparators() {
  43:        String[] expected = { "[|]", "[\\^]", "[~]", "[&]" };
  44:        String[] got = h7u.getSeparators();
  45:        int i = 0;
  46:        while (i < expected.length) {
  47:            assertEquals(expected[i], got[i]);
  48:            i++;
  49:        }
  50:    }
  51:    
  52:    public void testExtract() {
  53:        String dirty = "2004-08-26 10:51:00 Randomstuff, here's a message: <"
  54:                + testRec1 + ">\r\n";
  55:        String clean = Hl7Record.cleanString(dirty);
  56:        assertEquals(testRec1, clean);
  57:        dirty = testRec1 + "\n";
  58:        clean = Hl7Record.cleanString(dirty);
  59:        assertEquals(testRec1, clean);
  60:        dirty = "ABC annoying test case\r" + testRec1;
  61:        clean = Hl7Record.cleanString(dirty);
  62:        assertEquals(testRec1, clean);
  63:    }
  64:    
  65:    public void testSegments() {
  66:        String[] expectSegments = testRec1.split("\r");
  67:        
  68:        // test using getAll
  69:        Hl7Segment[] gotSegments = h7u.getAll();
  70:        assertEquals(expectSegments[0], gotSegments[0].toString());
  71:        assertEquals(expectSegments[1], gotSegments[1].toString());
  72:        
  73:        // test using get(int)
  74:        assertEquals(expectSegments[0], h7u.get(1).toString());
  75:        assertEquals(expectSegments[1], h7u.get(2).toString());
  76:        
  77:        // test using get(string)
  78:        assertEquals(expectSegments[0], h7u.get("MSH").toString());
  79:        assertEquals(expectSegments[1], h7u.get("ZZZ").toString());
  80:    }
  81:    
  82:    public void testSegmentsExt() {
  83:        String testRec2 = testRec1 + "MSH|test|test|\rZZZ|Test again|\r";
  84:        String[] expectSegments = testRec2.split("\r");
  85:        
  86:        // test using get(string, count)
  87:        Hl7Record h7u2 = new Hl7Record(testRec2);
  88:        assertEquals(expectSegments[0], h7u2.get("MSH", 1).toString());
  89:        assertEquals(expectSegments[1], h7u2.get("ZZZ", 1).toString());
  90:        assertEquals(expectSegments[2], h7u2.get("MSH", 2).toString());
  91:        assertEquals(expectSegments[3], h7u2.get("ZZZ", 2).toString());
  92:        
  93:        // Make sure the segment count is right.
  94:        assertEquals(4, h7u2.size());
  95:    }
  96:    
  97:    public void testFields() {
  98:        String segment1 = testRec1.split("\r")[0];
  99:        String segment2 = testRec1.split("\r")[1];
 100:        // Fourth field of MSH
 101:        String expectField1 = segment1.split("[|]")[3];
 102:        // Second field of ZZZ
 103:        String expectField2 = segment2.split("[|]")[2];
 104:        
 105:        assertEquals(expectField1, h7u.get("MSH").field(4).toString());
 106:        assertEquals(expectField2, h7u.get("ZZZ").field(2).toString());
 107:    }
 108:    
 109:    public void testComps() {
 110:        String segment1 = testRec1.split("\r")[1];
 111:        // First field of ZZZ
 112:        String field1 = segment1.split("[|]")[1];
 113:        // Third component of field1
 114:        String expectComp1 = field1.split("[\\^]")[2];
 115:        
 116:        assertEquals(expectComp1, h7u.get(2).field(1).getComp(3).toString());
 117:    }
 118:    
 119:    public void testReps() {
 120:        String segment1 = testRec1.split("\r")[1];
 121:        // Third field of ZZZ
 122:        String field1 = segment1.split("[|]")[3];
 123:        // Third repetition of field1
 124:        String expectComp1 = field1.split("[~]")[2];
 125:        
 126:        assertEquals(expectComp1, h7u.get(2).field(3).getRep(3).toString());
 127:    }
 128:    
 129:    public void testSubcomps() {
 130:        String segment1 = testRec1.split("\r")[1];
 131:        // Fourth field of ZZZ
 132:        String field1 = segment1.split("[|]")[4];
 133:        // Second comp of field1
 134:        String comp2 = field1.split("[\\^]")[1];
 135:        // Second subcom of rep2
 136:        String expectSub1 = comp2.split("[&]")[2];
 137:        
 138:        assertEquals(expectSub1, h7u.get(2).field(4).getComp(2).getSubcomp(3)
 139:        .toString());
 140:    }
 141:    
 142:    /**
 143:     * Make sure that our various get methods don't throw exceptions if we give
 144:     * them out of bounds values - they should return an Hl7Field object
 145:     * containing an empty string.
 146:     *
 147:     */
 148:    public void testOutOfBounds() {
 149:        assertEquals(null, h7u.get("ABC"));
 150:        assertEquals(null, h7u.get("ZZZ", 2));
 151:        assertEquals("NULEORG", h7u.get("MSH").field(3).getComp(1).toString());
 152:        assertEquals("", h7u.get("MSH").field(3).getComp(2).toString());
 153:        assertEquals("care", h7u.get("ZZZ").field(3).getRep(3).toString());
 154:        assertEquals("", h7u.get("ZZZ").field(3).getRep(4).toString());
 155:        assertEquals("did", h7u.get("ZZZ").field(4).getComp(2).getSubcomp(3)
 156:        .toString());
 157:        assertEquals("", h7u.get("ZZZ").field(4).getComp(2).getSubcomp(5)
 158:        .toString());
 159:    }
 160:    
 161:    /**
 162:     * Test new functionality to allow for the alteration of messages.
 163:     *
 164:     */
 165:    public void testChangeRecord() {
 166:        String testRec2 = "MSH|^~\\&|NULEORG|HL7BROWSER_TNG|||20040825120000||" +
 167:                "ACK||P|2.3||||NE|\rZZZ|this^is^^valid|hl7|but~I^don't~care|" +
 168:                "even^if&I&did&I^wouldn't^tell|you\r";
 169:        String testSeg1 = "ZZZ|this^is^not^valid|hl7|but~I^don't~care|" +
 170:                "even^if&I&did&I^wouldn't^tell|you";
 171:        String testSeg2 = "ZZZ|this^is^^valid|hl7|but~I^don't~care|" +
 172:                "even^if&I&did&I^wouldn't^tell|you";
 173:        Hl7Record h7u1 = new Hl7Record(testRec1);
 174:        assertEquals(testSeg1, h7u1.get("ZZZ").toString());
 175:        h7u1.changeRecord(testRec2);
 176:        assertEquals(testSeg2, h7u1.get("ZZZ").toString());
 177:        h7u1.get("ZZZ").changeSegment(testSeg1);
 178:        h7u1.rebuild();
 179:        assertEquals(testSeg1, h7u1.get("ZZZ").toString());
 180:        h7u1.get("ZZZ").changeSegment(testSeg2);
 181:        h7u1.rebuild();
 182:        assertEquals(testSeg2, h7u1.get("ZZZ").toString());
 183:        h7u1.get("ZZZ").field(1).changeField("this^is^not^valid");
 184:        h7u1.rebuild();
 185:        assertEquals(testSeg1, h7u1.get("ZZZ").toString());
 186:        assertEquals("not", h7u1.get("ZZZ").field(1).getComp(3).toString());
 187:        h7u1.get("ZZZ").field(1).getRep(1).getComp(3).changeField("");
 188:        h7u1.rebuild();
 189:        assertEquals("", h7u1.get("ZZZ").field(1).getRep(1).getComp(3).toString());
 190:        h7u1.get("ZZZ").field(1).getRep(1).getComp(3).changeField("not");
 191:        h7u1.rebuild();
 192:        assertEquals("not", h7u1.get("ZZZ").field(1).getRep(1).getComp(3).toString());
 193:    }
 194:    
 195:    /**
 196:     * Test segment list functionality.
 197:     *
 198:     */
 199:    public void testSegmentLists() {
 200:        List l = new ArrayList();
 201:        l.add("MSH");
 202:        l.add("SCH");
 203:        l.add("NTE");
 204:        l.add("NTE");
 205:        l.add("PID");
 206:        l.add("AIL");
 207:        l.add("AIP");
 208:        l.add("NTE");
 209:        l.add("NTE");
 210:        l.add("NTE");
 211:        Hl7Record hl7 = new Hl7Record(testRec2);
 212:        assertEquals(l, hl7.listSegments());
 213:        hl7 = new Hl7Record(testRec3);
 214:        assertEquals(l, hl7.listSegments());
 215:        hl7 = new Hl7Record(testRec4);
 216:        assertEquals(l, hl7.listSegments());
 217:    }
 218:    
 219:    /**
 220:     * Test record for field, rep and component append
 221:     */
 222:    public void testAppendField() {
 223:        Hl7Record hl7 = new Hl7Record(testRec2);
 224:        hl7.get("SCH").field(10).changeField("blah");
 225:        String expected = "MSH|^~\\&|\rSCH||||||||||blah\rNTE|\rNTE|\r" +
 226:            "PID|\rAIL|\rAIP|\rNTE|\rNTE|\rNTE|\r";
 227:        hl7.rebuild();
 228:        assertEquals(expected, hl7.toString());
 229:        expected = "MSH|^~\\&|\rSCH||||||||||blah|blah\rNTE|\rNTE|\r" +
 230:            "PID|\rAIL|\rAIP|\rNTE|\rNTE|\rNTE|\r";
 231:        hl7.get("SCH").field(11).changeField("blah");
 232:        hl7.rebuild();
 233:        assertEquals(expected, hl7.toString());
 234:        expected = "MSH|^~\\&|\rSCH||||||||blah||blah|\rNTE|\rNTE|\r" +
 235:            "PID|\rAIL|\rAIP|\rNTE|\rNTE|\rNTE|\r";
 236:        hl7.get("SCH").field(11).changeField("");
 237:        hl7.get("SCH").field(8).changeField("blah");
 238:        hl7.rebuild();
 239:        assertEquals(expected, hl7.toString());
 240:        expected = "MSH|^~\\&|\rSCH||||||||blah^^^huh?||blah\rNTE|\rNTE|\r" +
 241:            "PID|\rAIL|\rAIP|\rNTE|\rNTE|\rNTE|\r";
 242:        hl7.get("SCH").field(8).getComp(4).changeField("huh?");
 243:        hl7.rebuild();
 244:        assertEquals(expected, hl7.toString());
 245:        expected = "MSH|^~\\&|\rSCH||||||||blah^^^huh?~^^^blargh||blah\rNTE|\rNTE|\r" +
 246:            "PID|\rAIL|\rAIP|\rNTE|\rNTE|\rNTE|\r";
 247:        hl7.get("SCH").field(8).getRep(2).getComp(4).changeField("blargh");
 248:        hl7.rebuild();
 249:        assertEquals(expected, hl7.toString());
 250:        expected = "MSH|^~\\&|\rSCH||||||||blah^^^what?~^^^blargh||blah\rNTE|\rNTE|\r" +
 251:            "PID|\rAIL|\rAIP|\rNTE|\rNTE|\rNTE|\r";
 252:        hl7.get("SCH").field(8).getRep(1).getComp(4).changeField("what?");
 253:        hl7.rebuild();
 254:        assertEquals(expected, hl7.toString());
 255:        int count = hl7.get("SCH").field(8).getRepCount();
 256:        assertEquals(count, 2);
 257:    }
 258:    
 259:    /**
 260:     * Test for segment append and insert.
 261:     */
 262:    public void testAppendSeg() {
 263:        Hl7Record hl7 = new Hl7Record(testRec2);
 264:        hl7.append("XYZ");
 265:        List l = new ArrayList();
 266:        l.add("MSH");
 267:        l.add("SCH");
 268:        l.add("NTE");
 269:        l.add("NTE");
 270:        l.add("PID");
 271:        l.add("AIL");
 272:        l.add("AIP");
 273:        l.add("NTE");
 274:        l.add("NTE");
 275:        l.add("NTE");
 276:        l.add("XYZ"); // new append segment
 277:        assertEquals(l, hl7.listSegments());
 278:        hl7.add(5, "ABC");
 279:        l = new ArrayList();
 280:        l.add("MSH");
 281:        l.add("SCH");
 282:        l.add("NTE");
 283:        l.add("NTE");
 284:        l.add("PID");
 285:        l.add("ABC"); // new add segment
 286:        l.add("AIL");
 287:        l.add("AIP");
 288:        l.add("NTE");
 289:        l.add("NTE");
 290:        l.add("NTE");
 291:        l.add("XYZ");
 292:        assertEquals(l, hl7.listSegments());
 293:    }
 294:    
 295:    /**
 296:     * Test for record creation
 297:     */
 298:    public void testNewRecord() {
 299:        String[] segs = {"MSH", "EVN", "PID", "ORC", "OBR", "NTE", "NTE", "OBR", "NTE"};
 300:        Hl7Record hl7 = new Hl7Record(segs);
 301:        String expected = "MSH|^~\\&|\rEVN|\rPID|\rORC|\rOBR|\rNTE|\rNTE|\rOBR|\rNTE|\r";
 302:        assertEquals(expected, hl7.toString());
 303:        List l = new ArrayList();
 304:        l.add("MSH");
 305:        l.add("EVN");
 306:        l.add("PID");
 307:        l.add("ORC");
 308:        l.add("OBR");
 309:        l.add("NTE");
 310:        l.add("NTE");
 311:        l.add("OBR");
 312:        l.add("NTE");
 313:        assertEquals(l, hl7.listSegments());
 314:        hl7 = new Hl7Record(segs, ":^~\\(");
 315:        expected = "MSH:^~\\(\rEVN:\rPID:\rORC:\rOBR:\rNTE:\rNTE:\rOBR:\rNTE:\r";
 316:        assertEquals(expected, hl7.toString());
 317:        hl7.get("PID").field(5).getComp(1).changeField("PATIENT");
 318:        hl7.get("PID").field(5).getComp(2).changeField("TEST");
 319:        hl7.get("PID").field(5).getComp(3).changeField("A");
 320:        hl7.rebuild();
 321:        expected = "MSH:^~\\(\rEVN:\rPID:::::PATIENT^TEST^A\rORC:\rOBR:\rNTE:\rNTE:\rOBR:\rNTE:\r";
 322:        assertEquals(expected, hl7.toString());
 323:    }
 324:    
 325:    /**
 326:     * Tests for the field match object. Note that the getFieldObj method returns
 327:     * an Hl7Field object and getField returns the string from the toString
 328:     * method of that object.  Therefore the both don't need to be tested except
 329:     * where the getFieldObj(hl7).changeField(); hl7.rebuild() is applied.
 330:     */
 331:    public void testFieldMatch() {
 332:        Hl7Record hl7 = new Hl7Record(testRec1);
 333:        FieldMatch fm = FieldMatch.verifyFields("ZZZ:3");
 334:        String expected = "but~I^don't~care";
 335:        assertEquals(expected, fm.getField(hl7));
 336:        fm = FieldMatch.verifyFields("ZZZ:6");
 337:        fm.getFieldObj(hl7).changeField("not");
 338:        hl7.rebuild();
 339:        expected = "MSH|^~\\&|NULEORG|HL7BROWSER_TNG|||" + 
 340:            "20040825120000||ACK||P|2.3||||NE|\rZZZ|this^is^not^valid|hl7|" +
 341:            "but~I^don't~care|even^if&I&did&I^wouldn't^tell|you|not\r";
 342:        assertEquals(expected, hl7.toString());
 343:        expected = "did";
 344:        assertEquals(expected, hl7.getField("ZZZ:4-0-2-3"));
 345:        hl7.setField("ZZZ:6", "");
 346:        hl7.setField("ZZZ:3-2-2", "do");
 347:        hl7.setField("ZZZ:4-0-3", "would");
 348:        hl7.rebuild();
 349:        expected = "MSH|^~\\&|NULEORG|HL7BROWSER_TNG|||" + 
 350:            "20040825120000||ACK||P|2.3||||NE|\rZZZ|this^is^not^valid|hl7|" +
 351:            "but~I^do~care|even^if&I&did&I^would^tell|you|\r";
 352:        assertEquals(expected, hl7.toString());
 353:    }
 354:}