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.responses;
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.ebxml.EbXMLFactory;
22  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLQueryResponse;
23  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLFactory30;
24  import org.openehealth.ipf.commons.ihe.xds.core.metadata.*;
25  import org.openehealth.ipf.commons.ihe.xds.core.responses.QueryResponse;
26  import org.openehealth.ipf.commons.ihe.xds.core.transform.responses.QueryResponseTransformer;
27  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage;
28  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
29  
30  import static org.junit.Assert.*;
31  import static org.openehealth.ipf.commons.ihe.xds.XDS.Interactions.ITI_18;
32  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*;
33  
34  /**
35   * Test for {@link QueryResponseValidator}.
36   * @author Jens Riemschneider
37   */
38  public class QueryResponseValidatorTest {
39      private QueryResponseValidator validator;
40      private QueryResponse response;
41      private QueryResponseTransformer transformer;
42      private DocumentEntry docEntry;
43  
44      @Before
45      public void setUp() {
46          validator = new QueryResponseValidator();
47          EbXMLFactory factory = new EbXMLFactory30();
48          
49          response = SampleData.createQueryResponseWithLeafClass();
50          transformer = new QueryResponseTransformer(factory);
51  
52          docEntry = response.getDocumentEntries().get(0);
53      }
54      
55      @Test
56      public void testValidateGoodCase() {
57          validator.validate(transformer.toEbXML(response), ITI_18);
58      }
59  
60      @Test
61      public void testQueryResponseDoesNotHaveSubmissionSetLimitations() {
62          response.getSubmissionSets().clear();
63          validator.validate(transformer.toEbXML(response), ITI_18);
64      }
65      
66      @Test
67      public void testQueryResponseMultiplePatientIdsDueToDocEntry() {
68          Identifiable otherId = new Identifiable("idbla", new AssigningAuthority("1.6"));
69          DocumentEntry docEntryOtherPatientId = SampleData.createDocumentEntry(otherId);
70          response.getDocumentEntries().add(docEntryOtherPatientId);
71          expectFailure(RESULT_NOT_SINGLE_PATIENT);
72      }
73  
74      @Test
75      public void testQueryResponseMultiplePatientIdsDueToFolder() {
76          Identifiable otherId = new Identifiable("idbla", new AssigningAuthority("1.6"));
77          Folder folderOtherPatientId = SampleData.createFolder(otherId);
78          response.getFolders().add(folderOtherPatientId);
79          expectFailure(RESULT_NOT_SINGLE_PATIENT);
80      }
81  
82      @Test
83      public void testQueryResponseMultiplePatientIdsDueToSubmissionSet() {
84          Identifiable otherId = new Identifiable("idbla", new AssigningAuthority("1.6"));
85          SubmissionSet submissionSetOtherPatientId = SampleData.createSubmissionSet(otherId);
86          response.getSubmissionSets().add(submissionSetOtherPatientId);
87          expectFailure(RESULT_NOT_SINGLE_PATIENT);
88      }
89  
90      @Test
91      public void testValidateDelegatesToSubmitObjectsRequestValidator() {
92          // Try a failure that is produced by the SubmitObjectsRequestValidator
93          docEntry.getAuthors().get(0).getAuthorInstitution().add(new Organization(null, "LOL", null));
94          expectFailure(ORGANIZATION_NAME_MISSING);            
95      }
96      
97      @Test
98      public void testValidateDelegatesToRegistryResponseValidator() {
99          // Try a failure that is produced by the RegistryResponseValidator
100         response.setStatus(null);
101         expectFailure(INVALID_STATUS_IN_RESPONSE);
102     }
103     
104     @Test
105     public void testMissingObjRef() {
106         response.getReferences().add(new ObjectReference());        
107         expectFailure(MISSING_OBJ_REF);
108     }
109 
110     @Test
111     public void testValidateDocumentEntryHasInvalidAvailabilityStatus() {
112         docEntry.setAvailabilityStatus(AvailabilityStatus.SUBMITTED);
113         expectFailure(DOC_ENTRY_INVALID_AVAILABILITY_STATUS);
114     }
115 
116     /**
117      * Test the case when a Query returned an On-Demand Document Entry.
118      */
119     @Test
120     public void testOnDemandDocumentEntryValidation() {
121         assertNotNull(docEntry.getSize());
122         assertNotNull(docEntry.getHash());
123         docEntry.setType(DocumentEntryType.ON_DEMAND);
124         expectFailure(WRONG_NUMBER_OF_SLOT_VALUES);
125 
126         docEntry.setSize(null);
127         docEntry.setHash(null);
128         expectFailure(WRONG_NUMBER_OF_SLOT_VALUES);
129 
130         docEntry.setCreationTime((Timestamp) null);
131         docEntry.setLegalAuthenticator(null);
132         validator.validate(transformer.toEbXML(response), ITI_18);
133     }
134 
135     private void expectFailure(ValidationMessage expectedMessage) {
136         expectFailure(expectedMessage, transformer.toEbXML(response));
137     }
138 
139     private void expectFailure(ValidationMessage expectedMessage, EbXMLQueryResponse ebXMLQueryResponse) {
140         try {
141             validator.validate(ebXMLQueryResponse, ITI_18);
142             fail("Expected exception: " + XDSMetaDataException.class);
143         }
144         catch (XDSMetaDataException e) {
145             assertEquals(expectedMessage, e.getValidationMessage());
146         }
147     }
148 }