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.ebxml.EbXMLFactory;
22  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLProvideAndRegisterDocumentSetRequest;
23  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLFactory30;
24  import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntry;
25  import org.openehealth.ipf.commons.ihe.xds.core.metadata.Organization;
26  import org.openehealth.ipf.commons.ihe.xds.core.requests.ProvideAndRegisterDocumentSet;
27  import org.openehealth.ipf.commons.ihe.xds.core.transform.requests.ProvideAndRegisterDocumentSetTransformer;
28  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage;
29  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationProfile;
30  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.fail;
34  import static org.openehealth.ipf.commons.ihe.xds.XDS.Interactions.ITI_41;
35  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.*;
36  
37  /**
38   * Test for {@link ProvideAndRegisterDocumentSetRequestValidator}.
39   * @author Jens Riemschneider
40   */
41  public class ProvideAndRegisterDocumentSetRequestValidatorTest {
42      private ProvideAndRegisterDocumentSetRequestValidator validator;
43      private ProvideAndRegisterDocumentSet request;
44      private ProvideAndRegisterDocumentSetTransformer transformer;
45  
46      private DocumentEntry docEntry;
47  
48      @Before
49      public void setUp() {
50          validator = new ProvideAndRegisterDocumentSetRequestValidator();
51          EbXMLFactory factory = new EbXMLFactory30();
52          
53          request = SampleData.createProvideAndRegisterDocumentSet();
54          transformer = new ProvideAndRegisterDocumentSetTransformer(factory);
55  
56          docEntry = request.getDocuments().get(0).getDocumentEntry();
57      }
58      
59      @Test
60      public void testValidateGoodCase() {
61          validator.validate(transformer.toEbXML(request), ITI_41);
62      }
63      
64      @Test
65      public void testValidateDelegatesToSubmitObjectsRequestValidator() {
66          // Try a failure that is produced by the SubmitObjectsRequestValidator
67          docEntry.getAuthors().get(0).getAuthorInstitution().add(new Organization(null, "LOL", null));
68          expectFailure(ORGANIZATION_NAME_MISSING);            
69      }
70      
71      @Test
72      public void testValidateMissingDocEntryForDocument() {
73          EbXMLProvideAndRegisterDocumentSetRequest ebXML = transformer.toEbXML(request);
74          ebXML.addDocument("lol", SampleData.createDataHandler());
75          expectFailure(MISSING_DOC_ENTRY_FOR_DOCUMENT, ebXML, ITI_41);
76      }
77      
78      @Test
79      public void testValidateMissingDocumentForDocEntry() {
80          EbXMLProvideAndRegisterDocumentSetRequest ebXML = transformer.toEbXML(request);
81          ebXML.removeDocument("document01");
82          expectFailure(MISSING_DOCUMENT_FOR_DOC_ENTRY, ebXML, ITI_41);
83      }
84      
85      @Test
86      public void testRepositoryUniqueIdIsNotNecessary() {
87          docEntry.setRepositoryUniqueId(null);
88          validator.validate(transformer.toEbXML(request), ITI_41);
89      }
90  
91      @Test
92      public void testMandatorySubmissionSetStatus() {
93          request.getAssociations().get(0).setLabel(null);
94          expectFailure(SUBMISSION_SET_STATUS_MANDATORY);
95      }
96  
97      @Test
98      public void testWrongTargetHomeCommunityId() {
99          request.setTargetHomeCommunityId("urn:oid:1.2.3.foobar");
100         expectFailure(INVALID_OID);
101     }
102 
103     private void expectFailure(ValidationMessage expectedMessage) {
104         expectFailure(expectedMessage, transformer.toEbXML(request), ITI_41);
105     }
106 
107     private void expectFailure(ValidationMessage expectedMessage, EbXMLProvideAndRegisterDocumentSetRequest ebXML, ValidationProfile profile) {
108         try {
109             validator.validate(ebXML, profile);
110             fail("Expected exception: " + XDSMetaDataException.class);
111         }
112         catch (XDSMetaDataException e) {
113             assertEquals(expectedMessage, e.getValidationMessage());
114         }
115     }
116 }