This project has retired. For details please refer to its Attic page.
ConverterTest xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.commons.impl.misc;
20  
21  import java.lang.reflect.Method;
22  import java.math.BigDecimal;
23  import java.math.BigInteger;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.LinkedHashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  import junit.framework.TestCase;
33  
34  import org.apache.chemistry.opencmis.commons.data.ObjectData;
35  import org.apache.chemistry.opencmis.commons.data.PermissionMapping;
36  import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
37  import org.apache.chemistry.opencmis.commons.definitions.PermissionDefinition;
38  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
39  import org.apache.chemistry.opencmis.commons.enums.AclPropagation;
40  import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
41  import org.apache.chemistry.opencmis.commons.enums.CapabilityAcl;
42  import org.apache.chemistry.opencmis.commons.enums.CapabilityChanges;
43  import org.apache.chemistry.opencmis.commons.enums.CapabilityContentStreamUpdates;
44  import org.apache.chemistry.opencmis.commons.enums.CapabilityJoin;
45  import org.apache.chemistry.opencmis.commons.enums.CapabilityQuery;
46  import org.apache.chemistry.opencmis.commons.enums.CapabilityRenditions;
47  import org.apache.chemistry.opencmis.commons.enums.ContentStreamAllowed;
48  import org.apache.chemistry.opencmis.commons.impl.Converter;
49  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AclCapabilitiesDataImpl;
50  import org.apache.chemistry.opencmis.commons.impl.dataobjects.DocumentTypeDefinitionImpl;
51  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ObjectDataImpl;
52  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PermissionDefinitionDataImpl;
53  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PermissionMappingDataImpl;
54  import org.apache.chemistry.opencmis.commons.impl.dataobjects.RepositoryCapabilitiesImpl;
55  import org.apache.chemistry.opencmis.commons.impl.dataobjects.RepositoryInfoImpl;
56  
57  /**
58   * Tests converter methods.
59   *
60   * @author <a href="mailto:fmueller@opentext.com">Florian M&uuml;ller</a>
61   *
62   */
63  public class ConverterTest extends TestCase {
64  
65      public void testRepositoryInfo() throws Exception {
66          // dry run
67          RepositoryInfoImpl obj1 = new RepositoryInfoImpl();
68          RepositoryInfo obj2 = Converter.convert(Converter.convert(obj1));
69  
70          assertDataObjectsEquals("RepositoryInfo", obj1, obj2);
71  
72          // values
73          obj1 = new RepositoryInfoImpl();
74          obj1.setChangesIncomplete(Boolean.TRUE);
75          obj1.setChangesOnType(Collections.singletonList(BaseTypeId.CMIS_DOCUMENT));
76          obj1.setCmisVersionSupported("1.0");
77          obj1.setLatestChangeLogToken("changeLogToken");
78          obj1.setPrincipalAnonymous("principalAnonymous");
79          obj1.setPrincipalAnyone("principalAnyone");
80          obj1.setProductName("productName");
81          obj1.setProductVersion("productVersion");
82          obj1.setDescription("description");
83          obj1.setId("id");
84          obj1.setName("name");
85          obj1.setRootFolder("rootFolderId");
86          obj1.setThinClientUri("thinClientUri");
87          obj1.setVendorName("vendorName");
88  
89          RepositoryCapabilitiesImpl cap1 = new RepositoryCapabilitiesImpl();
90          cap1.setAllVersionsSearchable(Boolean.TRUE);
91          cap1.setCapabilityAcl(CapabilityAcl.DISCOVER);
92          cap1.setCapabilityChanges(CapabilityChanges.ALL);
93          cap1.setCapabilityContentStreamUpdates(CapabilityContentStreamUpdates.ANYTIME);
94          cap1.setCapabilityJoin(CapabilityJoin.INNERANDOUTER);
95          cap1.setCapabilityQuery(CapabilityQuery.BOTHCOMBINED);
96          cap1.setCapabilityRendition(CapabilityRenditions.READ);
97          cap1.setIsPwcSearchable(Boolean.TRUE);
98          cap1.setIsPwcUpdatable(Boolean.TRUE);
99          cap1.setSupportsGetDescendants(Boolean.TRUE);
100         cap1.setSupportsGetFolderTree(Boolean.TRUE);
101         cap1.setSupportsMultifiling(Boolean.TRUE);
102         cap1.setSupportsUnfiling(Boolean.TRUE);
103         cap1.setSupportsVersionSpecificFiling(Boolean.TRUE);
104         obj1.setCapabilities(cap1);
105 
106         AclCapabilitiesDataImpl acl1 = new AclCapabilitiesDataImpl();
107         acl1.setAclPropagation(AclPropagation.PROPAGATE);
108         List<PermissionDefinition> pddList = new ArrayList<PermissionDefinition>();
109         PermissionDefinitionDataImpl pdd1 = new PermissionDefinitionDataImpl();
110         pdd1.setPermission("test:perm1");
111         pdd1.setDescription("Permission1");
112         pddList.add(pdd1);
113         PermissionDefinitionDataImpl pdd2 = new PermissionDefinitionDataImpl();
114         pdd2.setPermission("test:perm2");
115         pdd2.setDescription("Permission2");
116         pddList.add(pdd2);
117         acl1.setPermissionDefinitionData(pddList);
118         Map<String, PermissionMapping> pmd = new LinkedHashMap<String, PermissionMapping>();
119         PermissionMappingDataImpl pmd1 = new PermissionMappingDataImpl();
120         pmd1.setKey(PermissionMapping.CAN_CREATE_DOCUMENT_FOLDER);
121         pmd1.setPermissions(Arrays.asList(new String[] { "p1", "p2" }));
122         pmd.put(pmd1.getKey(), pmd1);
123         PermissionMappingDataImpl pmd2 = new PermissionMappingDataImpl();
124         pmd2.setKey(PermissionMapping.CAN_DELETE_OBJECT);
125         pmd2.setPermissions(Arrays.asList(new String[] { "p3", "p4" }));
126         pmd.put(pmd2.getKey(), pmd2);
127         acl1.setPermissionMappingData(pmd);
128         obj1.setAclCapabilities(acl1);
129 
130         obj2 = Converter.convert(Converter.convert(obj1));
131 
132         assertDataObjectsEquals("RepositoryInfo", obj1, obj2);
133     }
134 
135     public void testTypeDefinition() throws Exception {
136         // dry run
137         DocumentTypeDefinitionImpl obj1 = new DocumentTypeDefinitionImpl();
138         TypeDefinition obj2 = Converter.convert(Converter.convert(obj1));
139 
140         assertDataObjectsEquals("TypeDefinition", obj1, obj2);
141 
142         // simple values
143         obj1 = new DocumentTypeDefinitionImpl();
144         obj1.setBaseTypeId(BaseTypeId.CMIS_DOCUMENT);
145         obj1.setContentStreamAllowed(ContentStreamAllowed.ALLOWED);
146         obj1.setDescription("description");
147         obj1.setDisplayName("displayName");
148         obj1.setId("id");
149         obj1.setIsControllableAcl(Boolean.TRUE);
150         obj1.setIsControllablePolicy(Boolean.TRUE);
151         obj1.setIsCreatable(Boolean.TRUE);
152         obj1.setIsFileable(Boolean.TRUE);
153         obj1.setIsIncludedInSupertypeQuery(Boolean.TRUE);
154         obj1.setIsQueryable(Boolean.TRUE);
155         obj1.setIsVersionable(Boolean.TRUE);
156         obj1.setLocalName("localName");
157         obj1.setLocalNamespace("localNamespace");
158         obj1.setParentTypeId("parentId");
159         obj1.setQueryName("queryName");
160 
161         obj2 = Converter.convert(Converter.convert(obj1));
162 
163         assertDataObjectsEquals("TypeDefinition", obj1, obj2);
164     }
165 
166     public void testObject() throws Exception {
167         // dry run
168         ObjectDataImpl obj1 = new ObjectDataImpl();
169         ObjectData obj2 = Converter.convert(Converter.convert(obj1));
170 
171         assertDataObjectsEquals("Object", obj1, obj2);
172     }
173 
174     /**
175      * Asserts OpenCMIS data objects.
176      */
177     protected void assertDataObjectsEquals(String name, Object expected, Object actual) throws Exception {
178 
179         if ((expected == null) && (actual == null)) {
180             return;
181         }
182 
183         if ((expected == null) && (actual instanceof Collection<?>)) {
184             assertTrue(((Collection<?>) actual).isEmpty());
185             return;
186         } else if ((expected instanceof Collection<?>) && (actual == null)) {
187             assertTrue(((Collection<?>) expected).isEmpty());
188             return;
189         } else if ((expected == null) || (actual == null)) {
190             fail("Data object is null! name: " + name + " / expected: " + expected + " / actual: " + actual);
191         }
192 
193         // handle simple types
194         if ((expected instanceof String) || (expected instanceof Boolean) || (expected instanceof BigInteger)
195                 || (expected instanceof BigDecimal) || (expected instanceof Enum<?>)) {
196             assertEquals(expected, actual);
197 
198             return;
199         } else if (expected instanceof List<?>) {
200             List<?> expectedList = (List<?>) expected;
201             List<?> actualList = (List<?>) actual;
202 
203             assertEquals(expectedList.size(), actualList.size());
204 
205             for (int i = 0; i < expectedList.size(); i++) {
206                 assertDataObjectsEquals(name + "[" + i + "]", expectedList.get(i), actualList.get(i));
207             }
208 
209             return;
210         } else if (expected instanceof Map<?, ?>) {
211             Map<?, ?> expectedMap = (Map<?, ?>) expected;
212             Map<?, ?> actualMap = (Map<?, ?>) actual;
213 
214             assertEquals(expectedMap.size(), actualMap.size());
215 
216             for (Map.Entry<?, ?> entry : expectedMap.entrySet()) {
217                 assertTrue(actualMap.containsKey(entry.getKey()));
218                 assertDataObjectsEquals(name + "[" + entry.getKey() + "]", entry.getValue(), actualMap.get(entry
219                         .getKey()));
220             }
221 
222             return;
223         }
224 
225         for (Method m : expected.getClass().getMethods()) {
226             if (!m.getName().startsWith("get") && !m.getName().startsWith("supports")) {
227                 continue;
228             }
229 
230             if (m.getName().equals("getClass")) {
231                 continue;
232             }
233 
234             if (m.getParameterTypes().length != 0) {
235                 continue;
236             }
237 
238             Object expectedValue = m.invoke(expected, new Object[0]);
239             Object actualValue = m.invoke(actual, new Object[0]);
240 
241             assertDataObjectsEquals(name + "." + m.getName(), expectedValue, actualValue);
242         }
243     }
244 }