View Javadoc
1   /*
2    * Copyright 2009 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.transform.ebxml;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
21  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
22  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
23  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSlot;
24  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLAdhocQueryRequest30;
25  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLExtrinsicObject30;
26  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLFactory30;
27  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLSubmitObjectsRequest30;
28  import org.openehealth.ipf.commons.ihe.xds.core.metadata.*;
29  import org.openehealth.ipf.commons.ihe.xds.core.requests.RegisterDocumentSet;
30  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
31  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryRequest;
32  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.ExtrinsicObjectType;
33  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.IdentifiableType;
34  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.RegistryObjectListType;
35  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryParameter;
36  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.RegisterDocumentSetTransformer;
37  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.query.QuerySlotHelper;
38  
39  import javax.xml.bind.*;
40  import javax.xml.namespace.QName;
41  import java.io.ByteArrayInputStream;
42  import java.io.ByteArrayOutputStream;
43  import java.io.File;
44  import java.util.List;
45  
46  import static org.junit.Assert.*;
47  
48  /**
49   * Tests for marshaling objects created with our ebxml 2.1 classes.
50   * @author Jens Riemschneider
51   */
52  public class Ebrs30MarshalingTest {
53      private SubmitObjectsRequest request;
54      private EbXMLExtrinsicObject docEntry;
55      private EbXMLFactory30 factory;
56      private EbXMLObjectLibrary objectLibrary;
57  
58      private static final QName EXTRINSIC_OBJECT_QNAME = 
59          new QName("urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0", "ExtrinsicObject", "rim"); 
60  
61      @Before
62      public void setUp() {
63          factory = new EbXMLFactory30();
64          objectLibrary = factory.createObjectLibrary();
65          
66          request = new SubmitObjectsRequest();
67          RegistryObjectListType objListElement = new RegistryObjectListType();
68          request.setRegistryObjectList(objListElement);
69          List<JAXBElement<? extends IdentifiableType>> objList = objListElement.getIdentifiable();
70  
71          docEntry = factory.createExtrinsic("Document01", objectLibrary);
72          docEntry.setObjectType(DocumentEntryType.STABLE.getUuid());
73          objList.add(getJaxbElement(EXTRINSIC_OBJECT_QNAME, ((EbXMLExtrinsicObject30)docEntry).getInternal()));
74      }
75      
76      @SuppressWarnings("unchecked")
77      private static JAXBElement<IdentifiableType> getJaxbElement(QName qname, IdentifiableType object) {
78          return new JAXBElement<>(qname, (Class)object.getClass(), object);
79      }
80      
81      @Test
82      public void testCreateClassification() throws Exception {        
83          EbXMLClassification classification = factory.createClassification(objectLibrary);
84          classification.setClassifiedObject(docEntry.getId());
85          docEntry.addClassification(classification, Vocabulary.DOC_ENTRY_AUTHOR_CLASS_SCHEME);
86          
87          SubmitObjectsRequest received = send();
88          
89          EbXMLExtrinsicObject docEntryResult = getDocumentEntry(received);
90          assertEquals(1, docEntryResult.getClassifications().size());
91          EbXMLClassification classificationResult = docEntryResult.getClassifications().get(0);
92          assertNotNull(classificationResult);
93          
94          assertEquals(Vocabulary.DOC_ENTRY_AUTHOR_CLASS_SCHEME, classificationResult.getClassificationScheme());
95          assertNull(classificationResult.getNodeRepresentation());
96          assertEquals(docEntryResult.getId(), classificationResult.getClassifiedObject());
97      }
98      
99      private EbXMLExtrinsicObject getDocumentEntry(SubmitObjectsRequest received) {
100         for(JAXBElement<? extends IdentifiableType> obj : received.getRegistryObjectList().getIdentifiable()) {
101             if(obj.getDeclaredType() == ExtrinsicObjectType.class) {
102                 return new EbXMLExtrinsicObject30((ExtrinsicObjectType)obj.getValue(), objectLibrary);
103             }
104         }
105         fail("Document entry not found");
106         return null;
107     }
108 
109     @Test
110     public void testAddSlot() throws Exception {
111         EbXMLClassification classification = factory.createClassification(objectLibrary);
112         docEntry.addClassification(classification, Vocabulary.DOC_ENTRY_AUTHOR_CLASS_SCHEME);
113         
114         classification.addSlot("something", "a", "b", "c");
115 
116         SubmitObjectsRequest received = send();        
117         EbXMLExtrinsicObject docEntryResult = getDocumentEntry(received);
118         List<EbXMLSlot> slots = docEntryResult.getClassifications().get(0).getSlots();        
119         assertEquals(1, slots.size());        
120         
121         EbXMLSlot slot = slots.get(0);
122         assertEquals("something", slot.getName());
123         
124         List<String> values = slot.getValueList();
125         assertEquals(3, values.size());
126         assertTrue(values.contains("a"));
127         assertTrue(values.contains("b"));
128         assertTrue(values.contains("c"));
129     }
130 
131     @Test
132     public void testFromRealEbXML() throws Exception {
133         File file = new File(getClass().getClassLoader().getResource("SubmitObjectsRequest_ebrs30.xml").toURI());
134 
135         JAXBContext context = JAXBContext.newInstance("org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs");
136         Unmarshaller unmarshaller = context.createUnmarshaller();
137 
138         Object unmarshalled = unmarshaller.unmarshal(file);
139         SubmitObjectsRequest original = (SubmitObjectsRequest) unmarshalled;
140 
141         RegisterDocumentSetTransformer transformer = new RegisterDocumentSetTransformer(factory);
142         RegisterDocumentSet result = transformer.fromEbXML(new EbXMLSubmitObjectsRequest30(original));
143 
144         DocumentEntry documentEntry = result.getDocumentEntries().get(0);
145         assertEquals("Document01", documentEntry.getEntryUuid());
146         assertEquals(DocumentEntryType.STABLE, documentEntry.getType());
147         assertEquals("Gerald Smitty", documentEntry.getAuthor().getAuthorPerson().getId().getId());
148 
149         SubmissionSet submissionSet = result.getSubmissionSet();
150         assertEquals("SubmissionSet01", submissionSet.getEntryUuid());
151         assertEquals(1, submissionSet.getAuthors().size());
152         assertEquals("Sherry Dopplemeyer", submissionSet.getAuthors().get(0).getAuthorPerson().getId().getId());
153 
154         assertEquals(result.getAssociations().get(0).getAssociationType(), AssociationType.HAS_MEMBER);
155         assertEquals(result.getAssociations().get(1).getAssociationType(), AssociationType.IS_SNAPSHOT_OF);
156     }
157 
158     @Test
159     public void testPatientIdSlotRegexp() throws Exception {
160         File file = new File(getClass().getClassLoader().getResource("iti18request.xml").toURI());
161 
162         JAXBContext context = JAXBContext.newInstance("org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs");
163         Unmarshaller unmarshaller = context.createUnmarshaller();
164 
165         Object unmarshalled = unmarshaller.unmarshal(file);
166         AdhocQueryRequest original = (AdhocQueryRequest) unmarshalled;
167 
168         QuerySlotHelper slotHelper = new QuerySlotHelper(new EbXMLAdhocQueryRequest30(original));
169         List<String> patientIdList = slotHelper.toStringList(QueryParameter.DOC_ENTRY_PATIENT_ID);
170         assertEquals(1, patientIdList.size());
171     }
172 
173     @Test
174     public void testPatientIdMPQSlotRegexp() throws Exception {
175         File file = new File(getClass().getClassLoader().getResource("iti51request.xml").toURI());
176 
177         JAXBContext context = JAXBContext.newInstance("org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs");
178         Unmarshaller unmarshaller = context.createUnmarshaller();
179 
180         Object unmarshalled = unmarshaller.unmarshal(file);
181         AdhocQueryRequest original = (AdhocQueryRequest) unmarshalled;
182 
183         QuerySlotHelper slotHelper = new QuerySlotHelper(new EbXMLAdhocQueryRequest30(original));
184         List<String> patientIdList = slotHelper.toStringList(QueryParameter.DOC_ENTRY_PATIENT_ID);
185         assertEquals(2, patientIdList.size());
186     }
187 
188     private SubmitObjectsRequest send() throws JAXBException {
189         JAXBContext context = JAXBContext.newInstance("org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs");
190         Marshaller marshaller = context.createMarshaller();
191         Unmarshaller unmarshaller = context.createUnmarshaller();
192         ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
193         marshaller.marshal(request, outputStream);
194         
195         ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
196         return (SubmitObjectsRequest) unmarshaller.unmarshal(inputStream);
197     }
198 }