1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.commons.ihe.hl7v2ws.pcd01;
17
18 import ca.uhn.hl7v2.HL7Exception;
19 import ca.uhn.hl7v2.HapiContext;
20 import ca.uhn.hl7v2.model.AbstractMessage;
21 import ca.uhn.hl7v2.model.Message;
22 import ca.uhn.hl7v2.model.v26.message.ORU_R01;
23 import ca.uhn.hl7v2.parser.Parser;
24 import ca.uhn.hl7v2.validation.Validator;
25 import ca.uhn.hl7v2.validation.impl.SimpleValidationExceptionHandler;
26 import org.junit.Before;
27 import org.openehealth.ipf.commons.ihe.hl7v2.definitions.HapiContextFactory;
28 import org.openehealth.ipf.gazelle.validation.profile.pcd.PcdTransactions;
29
30 import java.util.Scanner;
31
32 public abstract class AbstractPCD01ValidatorTest {
33
34 private HapiContext hapiContext = HapiContextFactory.createHapiContext(PcdTransactions.PCD1);
35
36 protected ORU_R01 maximumMessage;
37
38
39 public Validator getValidator() {
40 return hapiContext.getMessageValidator();
41 }
42
43 public Parser getParser() {
44 return hapiContext.getPipeParser();
45 }
46
47 @Before
48 public void setUp() throws HL7Exception {
49 maximumMessage = load(getParser(), "pcd01/valid-pcd01-MaximumRequest2.hl7");
50 }
51
52 protected <T extends AbstractMessage> T maxMsgReplace(String whatToReplace, String replacement) throws HL7Exception {
53 return (T) getParser().parse(maximumMessage.toString().replace(whatToReplace, replacement));
54 }
55
56 protected <T extends AbstractMessage> void validate(Message message) throws HL7Exception {
57 SimpleValidationExceptionHandler handler = new SimpleValidationExceptionHandler(hapiContext);
58 getValidator().validate(message, handler);
59 if (handler.hasFailed()) throw new HL7Exception("Validation has failed", handler.getExceptions().get(0));
60 }
61
62 protected static <T extends Message> T load(Parser parser, String fileName) throws HL7Exception {
63 return (T) parser.parse(
64 new Scanner(AbstractPCD01ValidatorTest.class.getResourceAsStream("/" + fileName)).useDelimiter("\\A").next());
65 }
66 }