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.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  }