View Javadoc
1   /*
2    * Copyright 2011 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.hl7v2ws.pcd01;
17  
18  import ca.uhn.hl7v2.HL7Exception;
19  import ca.uhn.hl7v2.model.v26.message.ACK;
20  import ca.uhn.hl7v2.model.v26.message.ORU_R01;
21  import org.junit.Ignore;
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  
26  /**
27   *
28   */
29  public class Pcd01ValidatorTest extends AbstractPCD01ValidatorTest {
30  
31      static String MSH = "MSH|^~\\&|AcmeInc^ACDE48234567ABCD^EUI-64||||20090713090030+0500||ORU^R01^ORU_R01|MSGID1234|P|2.6|||NE|AL|||||IHE PCD ORU-R01 2006^HL7^1.3.6.1.4.1.19376.1.6.1.1.1^ISO\r";
32      static String PID = "PID|||789567^^^Imaginary Hospital^PI||Doe^John^Joseph^^^^L^A|||M\r";
33      static String OBR = "OBR|1|AB12345^AcmeAHDInc^ACDE48234567ABCD^EUI-64|CD12345^AcmeAHDInc^ACDE48234567ABCD^EUI-64|528391^MDC_DEV_SPEC_PROFILE_BP^MDC|||20090813095715+0500\r";
34      static String OBX1 = "OBX|1||528391^MDC_DEV_SPEC_PROFILE_BP^MDC|1|||||||R|||||||0123456789ABCDEF^EUI-64\r";
35      static String OBX2 = "OBX|2||150020^MDC_PRESS_BLD_NONINV^MDC|1.0.1|||||||R|||20090813095715+0500\r";
36      static String OBX3 = "OBX|3|NM|150021^MDC_PRESS_BLD_NONINV_SYS^MDC|1.0.1.1|120|266016^MDC_DIM_MMHG^MDC|||||R\r";
37      static String OBX4 = "OBX|4|NM|150022^MDC_PRESS_BLD_NONINV_DIA^MDC|1.0.1.2|80|266016^MDC_DIM_MMHG^MDC|||||R\r";
38      static String VALID = MSH + PID + OBR + OBX1 + OBX2 + OBX3 + OBX4;
39      
40      static String ACK_MSH = "MSH|^~\\&|Stepstone||AcmeInc^ACDE48234567ABCD^EUI64||20090726095731+0500||ACK^R01^ACK|AMSGID1234|P|2.6|||NE|AL|||||IHE PCD ORU-R01 2006^HL7^2.16.840.1.113883.9.n.m^HL7\r";
41      static String MSA = "MSA|CE|20070701132554000008\r";
42      static String ERR = "ERR|||100|E|||Missing required OBR segment\r";
43      static String VALID_RESPONSE =  ACK_MSH + MSA + ERR;
44      
45     
46      @Test
47      public void testMessageSectionJ2() throws HL7Exception {
48          ORU_R01 msg1 = load(getParser(), "pcd01/valid-pcd01-request.hl7v2");
49          validate(msg1);
50      }
51  
52      @Test
53      public void testMessageSectionE11() throws HL7Exception {
54          ORU_R01 msg2 = load(getParser(), "pcd01/valid-pcd01-request2.hl7v2");
55          validate(msg2);
56      }
57  
58      @Test
59      public void testSyntheticMessage() throws HL7Exception{
60          ORU_R01 msg3 = (ORU_R01)getParser().parse(VALID);
61          validate(msg3);
62          assertObservationCount(4, msg3);
63      }
64      
65      @Test
66      public void testSyntheticMessageTrimmed() throws HL7Exception{
67          ORU_R01 msg4 = (ORU_R01)getParser().parse(VALID.trim());
68          validate(msg4);
69          assertObservationCount(4, msg4);
70      }
71      
72      @Test
73      public void testResponseMessage()  throws HL7Exception {
74          ACK rsp = load(getParser(), "pcd01/valid-pcd01-response.hl7v2");
75          validate(rsp);
76      }
77      
78      @Test
79      public void testResponseMessage2() throws HL7Exception  {
80          ACK rsp2 = load(getParser(), "pcd01/valid-pcd01-response2.hl7v2");
81          validate(rsp2);
82      }
83      
84      @Test
85      public void testSyntheticResponseMessage() throws HL7Exception {
86          validate(getParser().parse(VALID_RESPONSE));
87      }
88  
89      
90      @Test
91      public void testNoPID() throws HL7Exception {
92          //no PID is allowed (see the definition of ORU^R01) 
93          validate(getParser().parse(VALID.replace(PID, "")));
94      }
95      
96      @Test
97      public void testOnlyFamilyName() throws HL7Exception {
98          validate(getParser().parse(VALID.replace("Doe^John^Joseph", "Doe^^")));
99      }
100     
101     @Test
102     public void testNotPresentPatientName() throws HL7Exception {
103         validate(getParser().parse(VALID.replace("Doe^John^Joseph", "^^")));
104     }
105     
106 
107     // ///////// Negative tests
108     @Test(expected = HL7Exception.class)
109     public void testNoOBR() throws HL7Exception {
110         validate(getParser().parse(VALID.replace(OBR, "")));
111     }
112 
113     @Ignore
114     @Test(expected = HL7Exception.class)
115     public void testSyntheticResponseUnsupportedCODE() throws HL7Exception {
116     	//code SN is not supported
117         validate(getParser().parse(VALID_RESPONSE.replace("MSA|CE|", "MSA|SN|")));
118     }
119     
120 
121     /////////////////// Field cheks
122     @Test(expected = HL7Exception.class)
123     public void testIncompletePatientId() throws HL7Exception {
124         validate(getParser().parse(VALID.replace("789567", "")));
125     }
126 
127     @Test(expected = HL7Exception.class)
128     public void testObservationIdentifierNotPresent() throws HL7Exception {
129         validate(getParser().parse(VALID.replace("528391^MDC_DEV_SPEC_PROFILE_BP^MDC", "")));
130     }
131     
132     @Test(expected = HL7Exception.class)
133     public void testObservationWithNoSubId() throws HL7Exception {
134         validate(getParser().parse(VALID.replace("PROFILE_BP^MDC|1|", "PROFILE_BP^MDC||")));
135     }
136     
137     @Test(expected = HL7Exception.class)
138     public void testObservationWithNoSubId2() throws HL7Exception {
139         validate(getParser().parse(VALID.replace("|1.0.1|", "||")));
140     }
141     
142     private void assertObservationCount(int expected, ORU_R01 msg){
143         int observationsInMsg = msg.getPATIENT_RESULT().getORDER_OBSERVATION().getOBSERVATIONReps();
144         assertEquals(expected, observationsInMsg);
145     }
146 
147 }