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;
17  
18  import org.junit.Test;
19  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.*;
20  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLFactory30;
21  
22  import static org.junit.Assert.fail;
23  
24  /**
25   * Validates slot lengths.
26   *
27   * @author Jens Riemschneider
28   */
29  public class SlotLengthValidatorTest {
30      private static final EbXMLFactory30 factory30 = new EbXMLFactory30();
31  
32      private static final String SLOT_VALUE_30 = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456";
33  
34  
35      @Test
36      public void testValidateGoodCase30() throws XDSMetaDataException {
37          new SlotLengthAndNameUniquenessValidator().validateContainer(createContainer(factory30, SLOT_VALUE_30, -1));
38      }
39  
40      @Test
41      public void testValidateTooLong30() throws XDSMetaDataException {
42          for (int idx = 0; idx < 7; ++idx) {
43              try {
44                  new SlotLengthAndNameUniquenessValidator().validateContainer(createContainer(factory30, SLOT_VALUE_30, idx));
45                  fail("Expected exception: " + XDSMetaDataException.class + ", index=" + idx);
46              } catch (XDSMetaDataException e) {
47                  // expected
48              }
49          }
50      }
51  
52      private EbXMLObjectContainer createContainer(EbXMLFactory factory, String slotValue, int incorrectIdx) {
53          String[] values = new String[7];
54          for (int idx = 0; idx < 7; ++idx) {
55              values[idx] = idx == incorrectIdx ? slotValue + "!" : slotValue;
56          }
57  
58          EbXMLObjectContainer container = factory.createSubmitObjectsRequest();
59          EbXMLObjectLibrary objectLibrary = container.getObjectLibrary();
60  
61          EbXMLClassification classification1 = factory.createClassification(objectLibrary);
62          classification1.addSlot("slot", values[0]);
63  
64          EbXMLClassification classification2 = factory.createClassification(objectLibrary);
65          classification2.addSlot("slot", values[1]);
66  
67          EbXMLClassification classification3 = factory.createClassification(objectLibrary);
68          classification3.addSlot("slot1", values[2]);
69  
70          EbXMLClassification classification4 = factory.createClassification(objectLibrary);
71          classification3.addSlot("slot2", values[3]);
72  
73          EbXMLAssociation association = factory.createAssociation("assoc", objectLibrary);
74          association.addSlot("slot", values[4]);
75          association.addClassification(classification1, "scheme");
76  
77          EbXMLExtrinsicObject extrinsic = factory.createExtrinsic("ex", objectLibrary);
78          extrinsic.addSlot("slot", values[5]);
79          extrinsic.addClassification(classification2, "scheme");
80  
81          EbXMLRegistryPackage regPackage = factory.createRegistryPackage("reg", objectLibrary);
82          regPackage.addSlot("slot", values[6]);
83          regPackage.addClassification(classification3, "scheme");
84  
85          container.addAssociation(association);
86          container.addClassification(classification4);
87          container.addExtrinsicObject(extrinsic);
88          container.addRegistryPackage(regPackage);
89          return container;
90      }
91  }