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.transform.ebxml;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertSame;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.util.List;
23  
24  import ca.uhn.hl7v2.model.v25.datatype.XPN;
25  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
26  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExternalIdentifier;
27  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLInternationalString;
28  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryObject;
29  import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLSlot;
30  import org.openehealth.ipf.commons.ihe.xds.core.metadata.*;
31  
32  /**
33   * Test framework methods for ebXML related classes.
34   * @author Jens Riemschneider
35   */
36  public abstract class EbrsTestUtils {
37      private EbrsTestUtils() {
38          throw new UnsupportedOperationException("Utility class");
39      }
40      
41      /**
42       * Asserts that a single external identifier matching the scheme is contained in the list. 
43       * @param scheme
44       *          the scheme to look for.
45       * @param regEntry
46       *          the registry entry containing the list of identifiers to check..
47       * @param expectedValue
48       *          the expected value of the identifier.
49       * @param expectedName
50       *          the expected name of the identifier.
51       */
52      public static void assertExternalIdentifier(
53              String scheme, 
54              EbXMLRegistryObject regEntry, 
55              String expectedValue, 
56              String expectedName) {
57          
58          int found = 0; 
59          for (EbXMLExternalIdentifier identifier : regEntry.getExternalIdentifiers()) {
60              if (identifier.getIdentificationScheme().equals(scheme)) {
61                  ++found;
62                  assertEquals(expectedValue, identifier.getValue());
63                  assertEquals(expectedName, toLocal(identifier.getName()).getValue());
64              }
65          }
66          
67          assertEquals(1, found);
68      }
69  
70      /**
71       * Asserts that a classification matching the given scheme is in the list. 
72       * @param scheme
73       *          the scheme to look for.
74       * @param regEntry
75       *          registry entry whose classifications are checked.
76       * @param occurrence
77       *          the index of the classification to check. The scheme can be matched
78       *          multiple times. The occurrence is the index of the classification
79       *          in the list of classification that match the scheme (not in the 
80       *          list given by the parameter classification).
81       * @param expectedNodeRepresentation
82       *          the expected node representation.
83       * @param expectedLocalizedIdxName
84       *          the expected localized index of the name.
85       * @return the classification that matched the assertion.
86       */
87      public static EbXMLClassification assertClassification(String scheme, EbXMLRegistryObject regEntry, int occurrence, String expectedNodeRepresentation, int expectedLocalizedIdxName) {
88          List<EbXMLClassification> filtered = regEntry.getClassifications(scheme);
89          assertTrue("Not enough classification matching the scheme: " + scheme, filtered.size() > occurrence);
90  
91          EbXMLClassification classification = filtered.get(occurrence);
92          assertSame(regEntry.getId(), classification.getClassifiedObject());
93          assertEquals(expectedNodeRepresentation, classification.getNodeRepresentation());
94          if (expectedLocalizedIdxName > 0) {
95              assertEquals(
96                      createLocal(expectedLocalizedIdxName), 
97                      toLocal(classification.getNameAsInternationalString()));
98          }
99          
100         return classification;
101     }
102 
103     /**
104      * Asserts that a single slot matching the slot name has the expected values.
105      * @param slotName
106      *          the name of the slot to check.
107      * @param slots
108      *          the slot list to check.
109      * @param expectedSlotValues
110      *          the expected values of the slot.
111      */
112     public static void assertSlot(String slotName, List<EbXMLSlot> slots, String... expectedSlotValues) {
113         int found = 0;
114         for (EbXMLSlot slot : slots) {
115             if (slot.getName().equals(slotName)) {
116                 List<String> values = slot.getValueList();
117                 for (String expectedValue : expectedSlotValues) {
118                     assertTrue("Not found: " + expectedValue + ", was: " + values, values.contains(expectedValue));
119                 }
120                 assertEquals(values.size(), expectedSlotValues.length);
121                 ++found;
122             }
123         }
124         
125         assertEquals(1, found);
126     }
127 
128     /**
129      * Retrieves the localized string from an international string and asserts 
130      * that this string is available.
131      * @param international
132      *          the international string.
133      * @return the localized string.
134      */
135     public static LocalizedString toLocal(EbXMLInternationalString international) {
136         assertEquals(1, international.getLocalizedStrings().size());
137         return international.getSingleLocalizedString();
138     }
139     
140     /**
141      * Creates a standard person.
142      * @param idx
143      *          the index of the person. This index is used in all attributes of the
144      *          person and any sub structures.
145      * @return the new person instance.
146      */
147     public static Person createPerson(int idx) {
148         Person person = new Person();
149         person.setId(createIdentifiable(idx));
150         person.setName(createName(idx));
151         return person;
152     }
153     
154     /**
155      * Creates a standard organization.
156      * @param idx
157      *          the index of the organization. This index is used in all attributes of the
158      *          organization and any sub structures.
159      * @return the new organization instance.
160      */
161     public static Organization createOrganization(int idx) {
162         Organization organization = new Organization();
163         organization.setAssigningAuthority(createAssigningAuthority(idx));
164         organization.setIdNumber("id " + idx);
165         organization.setOrganizationName("orgName " + idx);
166         return organization;        
167     }
168 
169     /**
170      * Creates a standard name.
171      * @param idx
172      *          the index of the name. This index is used in all attributes of the
173      *          name.
174      * @return the new name instance.
175      */
176     public static Name<XPN> createName(int idx) {
177         return new XpnName("familyName " + idx, "givenName " + idx, "prefix " + idx, "second " + idx, "suffix " + idx, "degree " + idx);
178     }
179 
180     /**
181      * Creates a standard identifiable.
182      * @param idx
183      *          the index of the identifiable. This index is used in all attributes
184      *          of the identifiable and its sub structures.
185      * @return the new identifiable.
186      */
187     public static Identifiable createIdentifiable(int idx) {
188         return new Identifiable("id " + idx, createAssigningAuthority(idx));
189     }
190 
191     /**
192      * Creates a standard assigning authority.
193      * @param idx
194      *          the index of the assigning authority. This index is used in all
195      *          attributes of the assigning authority.
196      * @return the new assigning authority instance.
197      */
198     public static AssigningAuthority createAssigningAuthority(int idx) {
199         return new AssigningAuthority("uni " + idx, "uniType " + idx);
200     }
201 
202     /**
203      * Creates a standard code.
204      * @param idx
205      *          the index of the code. This index is used in all attributes of the code
206      *          and its sub structure.
207      * @return the new code instance.
208      */
209     public static Code createCode(int idx) {
210         return new Code("code " + idx, createLocal(idx), "scheme " + idx);
211     }
212 
213     /**
214      * Creates a standard localized string. 
215      * @param idx
216      *          the index of the localized string. This index is used in all attributes 
217      *          of the localized string.
218      * @return the new localized string instance.
219      */
220     public static LocalizedString createLocal(int idx) {
221         return new LocalizedString("value " + idx, "lang " + idx, "charset " + idx);
222     }    
223 }