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.xds.core.validate;
17  
18  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.DOC_ENTRY_EVENT_CODE_CLASS_SCHEME;
19  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.DOC_ENTRY_HEALTHCARE_FACILITY_TYPE_CODE_CLASS_SCHEME;
20  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.DisplayNameUsage.OPTIONAL;
21  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.DisplayNameUsage.REQUIRED;
22  import static org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.SLOT_NAME_CODING_SCHEME;
23  
24  import java.io.ByteArrayOutputStream;
25  
26  import javax.xml.bind.JAXBContext;
27  import javax.xml.bind.JAXBException;
28  import javax.xml.bind.Marshaller;
29  
30  import org.junit.Before;
31  import org.junit.Test;
32  import org.openehealth.ipf.commons.ihe.xds.core.SampleData;
33  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
34  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLFactory;
35  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLProvideAndRegisterDocumentSetRequest;
36  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLFactory30;
37  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.ProvideAndRegisterDocumentSetRequestType;
38  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary;
39  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Vocabulary.DisplayNameUsage;
40  import org.openehealth.ipf.commons.ihe.xds.core.requests.ProvideAndRegisterDocumentSet;
41  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.ProvideAndRegisterDocumentSetTransformer;
42  
43  /**
44   * Tests the display name validation for XDS.b. 
45   * 
46   * @author Mitko Kolev
47   */
48  public class ClassificationDisplayNameValidatorTest {
49  
50      private static final SlotValueValidation[] NO_SLOT_VALUE_VALIDATION = new SlotValueValidation[] {};
51  
52      private EbXMLExtrinsicObject extrinsicObject;
53  
54      @Before
55      public void setUp() throws Exception {
56          EbXMLProvideAndRegisterDocumentSetRequest ebXMLObject = createProvideAndRegisterDocumentSetRequest();
57          extrinsicObject = ebXMLObject.getExtrinsicObjects().get(0);
58      }
59  
60      @Test
61      public void testValidateClassificationHappyCase() throws XDSMetaDataException {
62          doValidation(DOC_ENTRY_HEALTHCARE_FACILITY_TYPE_CODE_CLASS_SCHEME, REQUIRED);
63      }
64      
65      @Test
66      public void testValidateClassificationNamePresentAndOptional() throws XDSMetaDataException {
67          doValidation(DOC_ENTRY_HEALTHCARE_FACILITY_TYPE_CODE_CLASS_SCHEME, OPTIONAL);
68      }
69      
70      @Test
71      public void testValidateClassificationNoNameRequiredButNamePresent() throws XDSMetaDataException {
72          clearNameInFirstClassification(DOC_ENTRY_HEALTHCARE_FACILITY_TYPE_CODE_CLASS_SCHEME);
73          doValidation(DOC_ENTRY_HEALTHCARE_FACILITY_TYPE_CODE_CLASS_SCHEME, OPTIONAL);
74      }
75  
76      @Test(expected = XDSMetaDataException.class)
77      public void testValidateClassificationNameRequiredButNotAvailable() throws XDSMetaDataException {
78          clearNameInFirstClassification(DOC_ENTRY_HEALTHCARE_FACILITY_TYPE_CODE_CLASS_SCHEME);
79          doValidation(DOC_ENTRY_HEALTHCARE_FACILITY_TYPE_CODE_CLASS_SCHEME, REQUIRED);
80      }
81      
82      
83      @Test
84      public void testEventCodeClassificationNameExists() throws XDSMetaDataException {
85          ClassificationValidation validator = buildEventListValidator();
86          validator.validate(extrinsicObject);
87      }
88      
89      
90      private void doValidation(String classScheme, DisplayNameUsage displayNameUsage){
91          ClassificationValidation validator = new ClassificationValidation(classScheme, 1, 1, displayNameUsage,
92                  Vocabulary.NodeRepresentationUsage.REQUIRED, NO_SLOT_VALUE_VALIDATION);
93          validator.validate(extrinsicObject);
94      }
95      
96      private void clearNameInFirstClassification(String classScheme){
97          extrinsicObject.getClassifications(classScheme).get(0).setName(null);
98      }
99      
100     private ClassificationValidation buildEventListValidator(){
101         SlotValueValidation [] eventCodeListValidator =
102                 new SlotValueValidation [] {new SlotValueValidation(SLOT_NAME_CODING_SCHEME, new NopValidator())};
103         return new ClassificationValidation(DOC_ENTRY_EVENT_CODE_CLASS_SCHEME, 1, 1,
104                 OPTIONAL, Vocabulary.NodeRepresentationUsage.REQUIRED, eventCodeListValidator);
105     }
106     
107     
108     private EbXMLProvideAndRegisterDocumentSetRequest createProvideAndRegisterDocumentSetRequest() {
109         EbXMLFactory factory = new EbXMLFactory30();
110         ProvideAndRegisterDocumentSet request = SampleData.createProvideAndRegisterDocumentSet();
111         ProvideAndRegisterDocumentSetTransformer transformer = new ProvideAndRegisterDocumentSetTransformer(factory);
112         return transformer.toEbXML(request);
113     }
114     /**
115      * Helper method to print the content of a EbXMLProvideAndRegisterDocumentSetRequest. Can be used to understand the test.
116      * @param ebXML ebXML object
117      */
118     @SuppressWarnings("unused")
119     private void marshalEbXML(EbXMLProvideAndRegisterDocumentSetRequest ebXML) {
120         try {
121             JAXBContext context = JAXBContext.newInstance("org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs");
122             Marshaller marshaller = context.createMarshaller();
123             ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
124             ProvideAndRegisterDocumentSetRequestType request = (ProvideAndRegisterDocumentSetRequestType) ebXML.getInternal();
125             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
126             marshaller.marshal(request.getSubmitObjectsRequest(), outputStream);
127             // System.out.println(new String(outputStream.toByteArray()));
128         } catch (JAXBException e) {
129             throw new RuntimeException(e);
130         }
131     }
132 }