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.validate.requests;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  import org.openehealth.ipf.commons.ihe.xds.core.SampleData;
21  import org.openehealth.ipf.commons.ihe.xds.core.XdsRuntimeException;
22  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAdhocQueryRequest;
23  import org.openehealth.ipf.commons.ihe.xds.core.metadata.AssigningAuthority;
24  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Identifiable;
25  import org.openehealth.ipf.commons.ihe.xds.core.requests.QueryRegistry;
26  import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindDocumentsForMultiplePatientsQuery;
27  import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindDocumentsQuery;
28  import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindFoldersForMultiplePatientsQuery;
29  import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindFoldersQuery;
30  import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetDocumentsQuery;
31  import org.openehealth.ipf.commons.ihe.xds.core.responses.ErrorCode;
32  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryParameter;
33  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.QueryRegistryTransformer;
34  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage;
35  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationProfile;
36  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
37  
38  import java.util.Collections;
39  import java.util.List;
40  
41  import static org.junit.Assert.*;
42  import static org.openehealth.ipf.commons.ihe.xds.XDS.Interactions.ITI_18;
43  import static org.openehealth.ipf.commons.ihe.xds.XDS.Interactions.ITI_51;
44  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*;
45  
46  /**
47   * Tests for {@link AdhocQueryRequestValidator}.
48   * @author Jens Riemschneider
49   * @author Michael Ottati
50   * @author Joerg Rueckert
51   */
52  public class AdhocQueryRequestValidatorTest {
53      private AdhocQueryRequestValidator validator;
54      private QueryRegistry request, requestMpq, folderRequest, folderRequestMpq;
55      private QueryRegistryTransformer transformer;
56  
57      @Before
58      public void setUp() {
59          validator = new AdhocQueryRequestValidator();
60          transformer = new QueryRegistryTransformer();
61          request = SampleData.createFindDocumentsQuery();
62          requestMpq = SampleData.createFindDocumentsForMultiplePatientsQuery();
63          folderRequest = SampleData.createFindFoldersQuery();
64          folderRequestMpq = SampleData.createFindFoldersForMultiplePatientsQuery();
65      }
66      
67      @Test
68      public void testGoodCase() throws XDSMetaDataException {
69          validator.validate(transformer.toEbXML(request), ITI_18);
70      }
71      
72      @Test
73      public void testUnknownReturnType() {        
74          EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(request);
75          ebXML.setReturnType("lol");
76          expectFailure(UNKNOWN_RETURN_TYPE, ebXML, ITI_18);
77      }
78  
79      @Test
80      public void testMissingRequiredQueryParameter() {
81          ((FindDocumentsQuery)request.getQuery()).setPatientId(null);
82          expectFailure(MISSING_REQUIRED_QUERY_PARAMETER, ITI_18);
83      }
84  
85      @Test
86      public void testTooManyQueryParameterValues() {
87          EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(request);
88          ebXML.getSlots(QueryParameter.DOC_ENTRY_PATIENT_ID.getSlotName()).get(0).getValueList().add("'lol'");
89          expectFailure(TOO_MANY_VALUES_FOR_QUERY_PARAMETER, ebXML, ITI_18);
90      }
91  
92      @Test
93      public void testParameterValueNotString() {
94          EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(request);
95          ebXML.getSlots(QueryParameter.DOC_ENTRY_PATIENT_ID.getSlotName()).get(0).getValueList().set(0, "lol");
96          expectFailure(PARAMETER_VALUE_NOT_STRING, ebXML, ITI_18);
97      }
98  
99      @Test
100     public void testParameterValueNotStringList() {
101         EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(request);
102         ebXML.getSlots(QueryParameter.DOC_ENTRY_CLASS_CODE.getSlotName()).get(0).getValueList().add("lol");
103         expectFailure(PARAMETER_VALUE_NOT_STRING_LIST, ebXML, ITI_18);
104     }
105 
106     @Test
107     public void testCodeListNotEnoughSchemes() {
108         EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(request);
109         ebXML.getSlots(QueryParameter.DOC_ENTRY_CLASS_CODE.getSlotName()).get(0).getValueList().add("('code^^')");
110         expectFailure(INVALID_QUERY_PARAMETER_VALUE, ebXML, ITI_18);
111     }
112 
113     @Test
114     public void testCodeListOldStyleNotEnoughSchemes() {
115         EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(request);
116         ebXML.getSlots(QueryParameter.DOC_ENTRY_CLASS_CODE.getSlotName()).get(0).getValueList().set(0, "('code1')");
117         ebXML.getSlots(QueryParameter.DOC_ENTRY_CLASS_CODE.getSlotName()).get(0).getValueList().set(1, "('code2')");
118         ebXML.addSlot(QueryParameter.DOC_ENTRY_CLASS_CODE_SCHEME.getSlotName(), "('scheme1')");
119         expectFailure(INVALID_QUERY_PARAMETER_VALUE, ebXML, ITI_18);
120     }
121 
122     @Test
123     public void testUnknownStatusCodes() {
124         EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(request);
125         List<String> valueList = ebXML.getSlots(QueryParameter.DOC_ENTRY_STATUS.getSlotName()).get(0).getValueList();
126 
127         // no codes at all -- should fail
128         valueList.clear();
129         expectFailure(MISSING_REQUIRED_QUERY_PARAMETER, ebXML, ITI_18);
130 
131         // only unknown codes -- should fail
132         valueList.clear();
133         valueList.add("('lol')");
134         valueList.add("('foo')");
135         expectFailure(INVALID_QUERY_PARAMETER_VALUE, ebXML, ITI_18);
136 
137         // at least one code -- should pass
138         valueList.set(0, "('bar')");
139         valueList.set(1, "('Approved')");
140         validator.validate(ebXML, ITI_18);
141     }
142     
143     @Test public void testUnknownFormatCode() {
144         EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(request);
145         List<String> valueList = ebXML.getSlots(QueryParameter.DOC_ENTRY_FORMAT_CODE.getSlotName()).get(0).getValueList();
146 
147         // invalid code without code -- should fail
148         valueList.clear();
149         valueList.add("('^^gablorg')");
150         expectFailure(INVALID_QUERY_PARAMETER_VALUE, ebXML, ITI_18);
151 
152         // invalid code without scheme -- should fail
153         valueList.clear();
154         valueList.add("('x^')");
155         expectFailure(INVALID_QUERY_PARAMETER_VALUE, ebXML, ITI_18);
156 
157         // empty code -- should fail
158         valueList.clear();
159         valueList.add("('')");
160         expectFailure(INVALID_QUERY_PARAMETER_VALUE, ebXML, ITI_18);
161     }
162     
163     @Test
164     public void testQueryParametersCannotBeSetTogether() {
165         request = SampleData.createGetDocumentsQuery();        
166         ((GetDocumentsQuery)request.getQuery()).setUniqueIds(Collections.singletonList("1.2.3"));
167         boolean exceptionOccurred = false;
168         try {
169             validator.validate(transformer.toEbXML(request), ITI_18);
170         } catch (XdsRuntimeException e) {
171             exceptionOccurred = true;
172             assertEquals(e.getErrorCode(), ErrorCode.STORED_QUERY_PARAM_NUMBER);
173             assertTrue(e.getMessage().contains("[$XDSDocumentEntryEntryUUID, $XDSDocumentEntryUniqueId, $XDSDocumentEntryLogicalID]"));
174         }
175         assertTrue(exceptionOccurred);
176     }
177 
178     @Test
179     public void testQueryParametersEitherOrChoiceMissing() {
180         request = SampleData.createGetDocumentsQuery();
181         ((GetDocumentsQuery)request.getQuery()).setUuids(null);
182         boolean exceptionOccurred = false;
183         try {
184             validator.validate(transformer.toEbXML(request), ITI_18);
185         } catch (XDSMetaDataException e) {
186             exceptionOccurred = true;
187             assertEquals(ValidationMessage.MISSING_REQUIRED_QUERY_PARAMETER, e.getValidationMessage());
188             assertTrue(e.getMessage().contains("one of [$XDSDocumentEntryEntryUUID, $XDSDocumentEntryUniqueId, $XDSDocumentEntryLogicalID]"));
189         }
190         assertTrue(exceptionOccurred);
191     }
192 
193     /*
194         The validation profiles for ITI-18 and ITI-51 are identical except for how they handle
195         PatientId data. The ITI-18 queries MUST contain exactly 1 PatientId. The ITI-51 Mutli
196         Patient Queries MAY contain a (possibly empty) list of patient IDs. The Following set
197         of tests suffixed with "MPQ" test the MPQ PatientId validation code.
198     */
199 
200     @Test
201     public void testGoodCaseMPQ() throws XDSMetaDataException {
202         validator.validate(transformer.toEbXML(requestMpq), ITI_51);
203     }
204 
205     @Test
206     public void testMissingPatientIdsMPQ() {
207         ((FindDocumentsForMultiplePatientsQuery) requestMpq.getQuery()).setPatientIds(null);
208         validator.validate(transformer.toEbXML(requestMpq), ITI_51);
209     }
210 
211     @Test
212     public void testPatientIdMustBeISO_MPQ() {
213         EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(requestMpq);
214         ebXML.getSlots(QueryParameter.DOC_ENTRY_PATIENT_ID.getSlotName()).get(0).getValueList().add("('Invalid ISO Patient ID')");
215         expectFailure(UNIVERSAL_ID_TYPE_MUST_BE_ISO, ebXML, ITI_51);
216     }
217 
218     // Folder and FolderMPQ test cases
219     @Test
220     public void testGoodCaseFolder() throws XDSMetaDataException {
221         validator.validate(transformer.toEbXML(folderRequest), ITI_18);
222     }
223 
224     @Test
225     public void testMissingPatientIdFolder() throws XDSMetaDataException {
226         ((FindFoldersQuery)folderRequest.getQuery()).setPatientId(null);
227         expectFailure(MISSING_REQUIRED_QUERY_PARAMETER, transformer.toEbXML(folderRequest), ITI_18);
228     }
229 
230     @Test
231     public void testMissingPatientIdFolderMPQ() {
232         ((FindFoldersForMultiplePatientsQuery) folderRequestMpq.getQuery()).setPatientIds(null);
233         validator.validate(transformer.toEbXML(folderRequestMpq), ITI_51);
234     }
235 
236     @Test
237     public void testGoodCaseFolderMPQ() throws XDSMetaDataException {
238         validator.validate(transformer.toEbXML(folderRequestMpq), ITI_51);
239     }
240 
241     @Test
242     public void testPatientIdMustBeISOFolder_MPQ() {
243         EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(folderRequestMpq);
244         ebXML.getSlots(QueryParameter.FOLDER_PATIENT_ID.getSlotName()).get(0).getValueList().add("('Invalid ISO Patient ID')");
245         expectFailure(UNIVERSAL_ID_TYPE_MUST_BE_ISO, ebXML, ITI_51);
246     }
247     
248     @Test
249     public void testUnknownQueryType() {
250         EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(request);
251         ebXML.setId("lol");
252         expectFailure(UNKNOWN_QUERY_TYPE, ebXML, ITI_18);
253     }
254 
255     @Test
256     public void testHomeCommunityIdAttributeValidation() {
257         request = SampleData.createGetDocumentsQuery();
258         // without prefix
259         ((GetDocumentsQuery)request.getQuery()).setHomeCommunityId("1.2.3");
260         expectFailure(INVALID_OID, ITI_18);
261         // wrong suffix
262         ((GetDocumentsQuery)request.getQuery()).setHomeCommunityId("urn:oid:foo");
263         expectFailure(INVALID_OID, ITI_18);
264     }
265 
266     private void expectFailure(ValidationMessage expectedMessage, ValidationProfile profile) {
267         expectFailure(expectedMessage, transformer.toEbXML(request), profile);
268     }
269 
270     @Test
271     public void testDuplicateSlotForFindDocumentsQueryValidationWithFailure() {
272         FindDocumentsQuery query = new FindDocumentsQuery();
273         EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(new QueryRegistry(query));
274         ebXML.addSlot(QueryParameter.DOC_ENTRY_CLASS_CODE.getSlotName(), "('class-code-1^^class-code-scheme-1')");
275         ebXML.addSlot(QueryParameter.DOC_ENTRY_CLASS_CODE.getSlotName(), "('class-code-2^^class-code-scheme-2')");
276         expectFailure(DUPLICATE_SLOT_NAME, ebXML, ITI_18);
277     }
278 
279     @Test
280     public void testDuplicateSlotForFindDocumentsQueryValidationWithSuccess() {
281         FindDocumentsQuery query = new FindDocumentsQuery();
282         query.setPatientId(new Identifiable("id3", new AssigningAuthority("1.3")));
283         EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(request);
284         ebXML.addSlot(QueryParameter.DOC_ENTRY_EVENT_CODE.getSlotName(), "('event-code-1^^event-code-scheme-1')");
285         ebXML.addSlot(QueryParameter.DOC_ENTRY_EVENT_CODE.getSlotName(), "('event-code-2^^event-code-scheme-2')");
286 
287         try {
288             validator.validate(ebXML, ITI_18);
289         }
290         catch (XDSMetaDataException e) {
291             fail("Test should succeed, but failed with exception: " + XDSMetaDataException.class);
292         }
293     }
294 
295     @Test
296     public void testDuplicateSlotForGetDocumentsQueryValidationWithFailure() {
297         GetDocumentsQuery query = new GetDocumentsQuery();
298         EbXMLAdhocQueryRequest ebXML = transformer.toEbXML(new QueryRegistry(query));
299         ebXML.addSlot(QueryParameter.DOC_ENTRY_UUID.getSlotName(), "('urn:uuid:5678-9012-3456-7890-1234-1')");
300         ebXML.addSlot(QueryParameter.DOC_ENTRY_UUID.getSlotName(), "('urn:uuid:6543-2109-8765-4321-0987-2')");
301         expectFailure(DUPLICATE_SLOT_NAME, ebXML, ITI_18);
302     }
303 
304     private void expectFailure(ValidationMessage expectedMessage, EbXMLAdhocQueryRequest request, ValidationProfile profile) {
305         try {
306             validator.validate(request, profile);
307             fail("Expected exception: " + XDSMetaDataException.class);
308         }
309         catch (XDSMetaDataException e) {
310             assertEquals(expectedMessage, e.getValidationMessage());
311         }
312     }
313 }