View Javadoc
1   /*
2    * Copyright 2013 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.Test;
19  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSubmitObjectsRequest;
20  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLSubmitObjectsRequest30;
21  import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
22  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage;
23  import org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationProfile;
24  import org.openehealth.ipf.commons.ihe.xds.core.validate.XDSMetaDataException;
25  
26  import javax.xml.bind.JAXBContext;
27  import javax.xml.bind.Unmarshaller;
28  import java.io.File;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.fail;
32  import static org.openehealth.ipf.commons.ihe.xds.XDS.Interactions.ITI_57;
33  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.LOGICAL_ID_EQUALS_ENTRY_UUID;
34  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.LOGICAL_ID_MISSING;
35  import static org.openehealth.ipf.commons.ihe.xds.core.validate.ValidationMessage.LOGICAL_ID_SAME;
36  
37  /**
38   * Test for {@link org.openehealth.ipf.commons.ihe.xds.core.validate.requests.SubmitObjectsRequestValidator}.
39   * @author Boris Stanojevic
40   */
41  public class SubmitObjectsRequestForUpdateValidatorTest {
42      private SubmitObjectsRequestValidator validator = new SubmitObjectsRequestValidator();
43  
44      @Test
45      public void testOKFromRealEbXML() throws Exception {
46          EbXMLSubmitObjectsRequest30 request = getRequest("SubmitObjectsRequest_ebrs30_update.xml");
47          validator.validate(request, ITI_57);
48      }
49  
50      @Test
51      public void testLid() throws Exception {
52          EbXMLSubmitObjectsRequest30 request = getRequest("SubmitObjectsRequest_ebrs30_update_sameLid.xml");
53  
54          expectFailure(LOGICAL_ID_SAME, request, ITI_57);
55  
56          request.getExtrinsicObjects().get(0).setLid(null);
57          expectFailure(LOGICAL_ID_MISSING, request, ITI_57);
58  
59          request.getExtrinsicObjects().get(0).setLid(request.getExtrinsicObjects().get(0).getId());
60          expectFailure(LOGICAL_ID_EQUALS_ENTRY_UUID, request, ITI_57);
61      }
62  
63      private void expectFailure(ValidationMessage expectedMessage, EbXMLSubmitObjectsRequest ebXML, ValidationProfile profile) {
64          try {
65              validator.validate(ebXML, profile);
66              fail("Expected exception: " + XDSMetaDataException.class);
67          }
68          catch (XDSMetaDataException e) {
69              assertEquals(expectedMessage, e.getValidationMessage());
70          }
71      }
72  
73      private EbXMLSubmitObjectsRequest30 getRequest(String resourcePath) throws Exception {
74          File file = new File(getClass().getClassLoader().getResource(resourcePath).toURI());
75  
76          JAXBContext context = JAXBContext.newInstance("org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs");
77          Unmarshaller unmarshaller = context.createUnmarshaller();
78  
79          Object unmarshalled = unmarshaller.unmarshal(file);
80          SubmitObjectsRequest original = (SubmitObjectsRequest) unmarshalled;
81          return  new EbXMLSubmitObjectsRequest30(original);
82      }
83  }