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.EbXMLRetrieveDocumentSetResponse;
23  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLFactory30;
24  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRetrieveDocumentSetResponse30;
25  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.RetrieveDocumentSetResponseType;
26  import org.openehealth.ipf.commons.ihe.xds.core.requests.DocumentReference;
27  import org.openehealth.ipf.commons.ihe.xds.core.responses.RetrievedDocument;
28  import org.openehealth.ipf.commons.ihe.xds.core.responses.RetrievedDocumentSet;
29  import org.openehealth.ipf.commons.ihe.xds.core.transform.responses.RetrieveDocumentSetResponseTransformer;
30  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage;
31  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
32  
33  import static org.junit.Assert.assertEquals;
34  import static org.junit.Assert.fail;
35  import static org.openehealth.ipf.commons.ihe.xds.XDS.Interactions.ITI_43;
36  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.DOC_ID_MUST_BE_SPECIFIED;
37  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.INVALID_STATUS_IN_RESPONSE;
38  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.MIME_TYPE_MUST_BE_SPECIFIED;
39  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.MISSING_DOCUMENT_FOR_DOC_ENTRY;
40  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.REPO_ID_MUST_BE_SPECIFIED;
41  
42  /**
43   * Tests for {@link RetrieveDocumentSetResponseValidator}.
44   * @author Jens Riemschneider
45   */
46  public class RetrieveDocumentResponseValidatorTest {
47      private RetrieveDocumentSetResponseValidator validator;
48      private RetrievedDocumentSet response;
49      private RetrieveDocumentSetResponseTransformer transformer;
50  
51      @Before
52      public void setUp() {
53          validator = new RetrieveDocumentSetResponseValidator();
54          EbXMLFactory factory = new EbXMLFactory30();
55          transformer = new RetrieveDocumentSetResponseTransformer(factory);
56          response = SampleData.createRetrievedDocumentSet();
57      }
58  
59      @Test
60      public void testGoodCase() throws XDSMetaDataException {
61          validator.validate(transformer.toEbXML(response), ITI_43);
62      }
63      
64      @Test
65      public void testDelegatesToRegistryResponseValidator() {
66          // Test a failure that is detected by the RegistryResponseValidator
67          response.setStatus(null);
68          expectFailure(INVALID_STATUS_IN_RESPONSE);
69      }
70  
71      @Test
72      public void testDelegatesToRegistryResponseValidatorEmptyInternalRegistryResponse() {
73          // Test a failure that is detected by the RegistryResponseValidator
74          RetrieveDocumentSetResponseType responseType = new RetrieveDocumentSetResponseType();
75          responseType.setRegistryResponse(null);
76          expectFailure(INVALID_STATUS_IN_RESPONSE, new EbXMLRetrieveDocumentSetResponse30(responseType));
77      }
78      
79      @Test
80      public void testRepoIdMustBeSpecified() {
81          DocumentReference requestData = new DocumentReference(null, "doc3", "home3");
82          RetrievedDocument doc = new RetrievedDocument();
83          doc.setRequestData(requestData);
84          doc.setDataHandler(SampleData.createDataHandler());
85          doc.setMimeType("text/plain");
86          response.getDocuments().add(doc);
87          EbXMLRetrieveDocumentSetResponse ebXML = transformer.toEbXML(response);
88          expectFailure(REPO_ID_MUST_BE_SPECIFIED, ebXML);
89      }
90      
91      @Test
92      public void testDocIdMustBeSpecified() {
93          DocumentReference requestData = new DocumentReference("repo3", "", "home3");
94          RetrievedDocument doc = new RetrievedDocument();
95          doc.setRequestData(requestData);
96          doc.setDataHandler(SampleData.createDataHandler());
97          doc.setMimeType("text/plain");
98          response.getDocuments().add(doc);
99          EbXMLRetrieveDocumentSetResponse ebXML = transformer.toEbXML(response);
100         expectFailure(DOC_ID_MUST_BE_SPECIFIED, ebXML);
101     }
102     
103     @Test
104     public void testMimeTypeMustBeSpecified() {
105         DocumentReference requestData = new DocumentReference("repo3", "doc3", "home3");
106         RetrievedDocument doc = new RetrievedDocument();
107         doc.setRequestData(requestData);
108         doc.setDataHandler(SampleData.createDataHandler());
109         doc.setMimeType("");
110         response.getDocuments().add(doc);
111         EbXMLRetrieveDocumentSetResponse ebXML = transformer.toEbXML(response);
112         expectFailure(MIME_TYPE_MUST_BE_SPECIFIED, ebXML);
113     }
114 
115     @Test
116     public void testDocumentMustBeSpecified() {
117         DocumentReference requestData = new DocumentReference("repo3", "doc3", "urn:oid:1.2.5");
118         RetrievedDocument doc = new RetrievedDocument();
119         doc.setRequestData(requestData);
120         doc.setDataHandler(null);
121         doc.setMimeType("text/plain");
122         response.getDocuments().add(doc);
123         EbXMLRetrieveDocumentSetResponse ebXML = transformer.toEbXML(response);
124         expectFailure(MISSING_DOCUMENT_FOR_DOC_ENTRY, ebXML);
125     }
126         
127     private void expectFailure(ValidationMessage expectedMessage) {
128         expectFailure(expectedMessage, transformer.toEbXML(response));
129     }
130 
131     private void expectFailure(ValidationMessage expectedMessage, EbXMLRetrieveDocumentSetResponse ebXMLRegistryResponse) {
132         try {
133             validator.validate(ebXMLRegistryResponse, ITI_43);
134             fail("Expected exception: " + XDSMetaDataException.class);
135         }
136         catch (XDSMetaDataException e) {
137             assertEquals(expectedMessage, e.getValidationMessage());
138         }
139     }
140 }