This project has retired. For details please refer to its Attic page.
AbstractSimpleReadOnlyTests 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.client.bindings.framework;
20  
21  import org.apache.chemistry.opencmis.commons.PropertyIds;
22  import org.apache.chemistry.opencmis.commons.data.Acl;
23  import org.apache.chemistry.opencmis.commons.data.AllowableActions;
24  import org.apache.chemistry.opencmis.commons.data.ContentStream;
25  import org.apache.chemistry.opencmis.commons.data.ObjectData;
26  import org.apache.chemistry.opencmis.commons.data.ObjectInFolderContainer;
27  import org.apache.chemistry.opencmis.commons.data.ObjectInFolderData;
28  import org.apache.chemistry.opencmis.commons.data.ObjectInFolderList;
29  import org.apache.chemistry.opencmis.commons.data.ObjectList;
30  import org.apache.chemistry.opencmis.commons.data.Properties;
31  import org.apache.chemistry.opencmis.commons.data.RenditionData;
32  import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
33  import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
34  import org.apache.chemistry.opencmis.commons.definitions.FolderTypeDefinition;
35  import org.apache.chemistry.opencmis.commons.definitions.PolicyTypeDefinition;
36  import org.apache.chemistry.opencmis.commons.definitions.RelationshipTypeDefinition;
37  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
38  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionContainer;
39  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionList;
40  import org.apache.chemistry.opencmis.commons.enums.Action;
41  import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
42  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
43  import org.apache.chemistry.opencmis.commons.enums.RelationshipDirection;
44  
45  import java.math.BigInteger;
46  import java.util.ArrayList;
47  import java.util.List;
48  
49  /**
50   * Simple read-only tests.
51   * 
52   * @author <a href="mailto:fmueller@opentext.com">Florian M&uuml;ller</a>
53   * 
54   */
55  public abstract class AbstractSimpleReadOnlyTests extends AbstractCmisTestCase {
56  
57      public static final String TEST_REPOSITORY_INFO = "repositoryInfo";
58      public static final String TEST_TYPES = "types";
59      public static final String TEST_CONTENT_STREAM = "contentStream";
60      public static final String TEST_NAVIGATION = "navigation";
61      public static final String TEST_QUERY = "query";
62      public static final String TEST_CHECKEDOUT = "checkedout";
63      public static final String TEST_CONTENT_CHANGES = "contentChanges";
64  
65      /**
66       * Tests repository info.
67       */
68      public void testRepositoryInfo() {
69          if (!isEnabled(TEST_REPOSITORY_INFO)) {
70              return;
71          }
72  
73          RepositoryInfo repInfo = getRepositoryInfo();
74  
75          Tools.print(repInfo);
76  
77          assertNotNull(repInfo.getId());
78          assertNotNull(repInfo.getCmisVersionSupported());
79          assertNotNull(repInfo.getRootFolderId());
80          assertNotNull(repInfo.getCapabilities());
81      }
82  
83      /**
84       * Some type related tests.
85       */
86      public void testTypes() {
87          if (!isEnabled(TEST_TYPES)) {
88              return;
89          }
90  
91          String repId = getTestRepositoryId();
92  
93          // get standard type
94          TypeDefinition docType = getTypeDefinition("cmis:document");
95          assertTrue(docType instanceof DocumentTypeDefinition);
96          assertEquals("cmis:document", docType.getId());
97          assertEquals(BaseTypeId.CMIS_DOCUMENT, docType.getBaseTypeId());
98  
99          TypeDefinition folderType = getTypeDefinition("cmis:folder");
100         assertTrue(folderType instanceof FolderTypeDefinition);
101         assertEquals("cmis:folder", folderType.getId());
102         assertEquals(BaseTypeId.CMIS_FOLDER, folderType.getBaseTypeId());
103 
104         try {
105             TypeDefinition relationshipType = getTypeDefinition("cmis:relationship");
106             assertTrue(relationshipType instanceof RelationshipTypeDefinition);
107             assertEquals("cmis:relationship", relationshipType.getId());
108             assertEquals(BaseTypeId.CMIS_RELATIONSHIP, relationshipType.getBaseTypeId());
109         } catch (Exception e) {
110             warning("Relationships type: " + e);
111         }
112 
113         try {
114             TypeDefinition policyType = getTypeDefinition("cmis:policy");
115             assertTrue(policyType instanceof PolicyTypeDefinition);
116             assertEquals("cmis:policy", policyType.getId());
117             assertEquals(BaseTypeId.CMIS_POLICY, policyType.getBaseTypeId());
118         } catch (Exception e) {
119             warning("Policy type: " + e);
120         }
121 
122         // getTypeChildren
123         TypeDefinitionList types = getBinding().getRepositoryService().getTypeChildren(repId, null, Boolean.TRUE, null,
124                 null, null);
125         assertNotNull(types);
126         assertNotNull(types.hasMoreItems());
127         assertNotNull(types.getList());
128         assertFalse(types.getList().isEmpty());
129         assertTrue(types.getList().size() >= 2);
130         assertTrue(types.getList().size() <= 4);
131 
132         getBinding().clearAllCaches();
133 
134         for (TypeDefinition type : types.getList()) {
135             TypeDefinition type2 = getTypeDefinition(type.getId());
136             assertEquals(type, type2, true);
137         }
138 
139         // getTypeDescendants
140         List<TypeDefinitionContainer> typesContainers = getBinding().getRepositoryService().getTypeDescendants(repId,
141                 null, null, Boolean.TRUE, null);
142         assertNotNull(typesContainers);
143         assertFalse(typesContainers.isEmpty());
144 
145         for (TypeDefinitionContainer typeContainer : typesContainers) {
146             assertNotNull(typeContainer.getTypeDefinition());
147             assertNotNull(typeContainer.getTypeDefinition().getId());
148             TypeDefinition type2 = getTypeDefinition(typeContainer.getTypeDefinition().getId());
149             assertEquals(typeContainer.getTypeDefinition(), type2, true);
150         }
151 
152         Tools.printTypes("Type Descendants", typesContainers);
153 
154         getBinding().clearAllCaches();
155 
156         assertTypeContainers(repId, typesContainers);
157     }
158 
159     private void assertTypeContainers(String repId, List<TypeDefinitionContainer> typesContainers) {
160         if (typesContainers == null) {
161             return;
162         }
163 
164         for (TypeDefinitionContainer container : typesContainers) {
165             assertNotNull(container.getTypeDefinition());
166 
167             TypeDefinition type = container.getTypeDefinition();
168             TypeDefinition type2 = getTypeDefinition(type.getId());
169 
170             assertEquals(type, type2, true);
171 
172             assertTypeContainers(repId, container.getChildren());
173         }
174     }
175 
176     /**
177      * Navigation smoke test.
178      */
179     public void testNavigation() {
180         if (!isEnabled(TEST_NAVIGATION)) {
181             return;
182         }
183 
184         String repId = getTestRepositoryId();
185         String rootFolder = getRootFolderId();
186         String testRootFolder = getTestRootFolder();
187 
188         ObjectData rootFolderObject = getObject(rootFolder);
189         String rootPath = getPath(rootFolderObject);
190         assertEquals("Root path is not \"/\"!", "/", rootPath);
191         assertAllowableAction(rootFolderObject.getAllowableActions(), Action.CAN_GET_OBJECT_PARENTS, false);
192 
193         ObjectData folderObject = getObject(testRootFolder);
194         String path = getPath(folderObject);
195 
196         ObjectInFolderList children = getBinding().getNavigationService().getChildren(repId, testRootFolder, "*", null,
197                 Boolean.TRUE, IncludeRelationships.BOTH, null, Boolean.TRUE, null, null, null);
198         assertNotNull(children);
199         assertNotNull(children.hasMoreItems());
200 
201         if (supportsDescendants()) {
202             List<ObjectInFolderContainer> desc = getBinding().getNavigationService().getDescendants(repId,
203                     testRootFolder, BigInteger.valueOf(2), "*", Boolean.TRUE, IncludeRelationships.BOTH, null,
204                     Boolean.TRUE, null);
205             assertNotNull(desc);
206             Tools.print("Descendants", desc);
207 
208             assertContainer(desc, 5);
209         } else {
210             warning("Descendants not supported!");
211         }
212 
213         if (supportsFolderTree()) {
214             List<ObjectInFolderContainer> tree = getBinding().getNavigationService().getFolderTree(repId,
215                     testRootFolder, BigInteger.valueOf(2), "*", Boolean.TRUE, IncludeRelationships.BOTH, null,
216                     Boolean.TRUE, null);
217             assertNotNull(tree);
218             Tools.print("Tree", tree);
219 
220             assertContainer(tree, 5);
221         } else {
222             warning("Folder Tree not supported!");
223         }
224 
225         for (ObjectInFolderData object : children.getObjects()) {
226             assertNotNull(object.getObject());
227             assertNotNull(object.getObject().getId());
228             assertNotNull(object.getObject().getBaseTypeId());
229 
230             ObjectData object2 = getObject(object.getObject().getId());
231             assertNotNull(object2.getId());
232             assertEquals(object.getObject().getId(), object2.getId());
233             assertEquals(object.getObject().getProperties(), object2.getProperties());
234 
235             ObjectData object3 = getObjectByPath((path.equals("/") ? "/" : path + "/") + object.getPathSegment());
236             assertNotNull(object3);
237             assertNotNull(object3.getId());
238             assertEquals(object.getObject().getId(), object3.getId());
239             assertEquals(object.getObject().getProperties(), object3.getProperties());
240 
241             checkObject(object.getObject().getId());
242 
243             if (object.getObject().getBaseTypeId() == BaseTypeId.CMIS_FOLDER) {
244                 ObjectInFolderList children2 = getBinding().getNavigationService().getChildren(repId,
245                         object.getObject().getId(), null, null, Boolean.TRUE, IncludeRelationships.BOTH, null,
246                         Boolean.TRUE, null, null, null);
247                 assertNotNull(children2);
248             } else if (object.getObject().getBaseTypeId() == BaseTypeId.CMIS_DOCUMENT) {
249                 checkObjectVersions(object.getObject().getId());
250             }
251         }
252     }
253 
254     private void assertContainer(List<ObjectInFolderContainer> containers, int maxDepth) {
255         if (containers == null) {
256             return;
257         }
258 
259         if (maxDepth < 1) {
260             return;
261         }
262 
263         for (ObjectInFolderContainer container : containers) {
264             assertNotNull(container);
265             assertNotNull(container.getObject());
266             assertNotNull(container.getObject().getObject());
267             assertNotNull(container.getObject().getObject().getId());
268             assertNotNull(container.getObject().getPathSegment());
269 
270             ObjectData object = getObject(container.getObject().getObject().getId());
271 
272             assertEquals(container.getObject().getObject().getProperties(), object.getProperties());
273             assertEquals(container.getObject().getObject().getAllowableActions(), object.getAllowableActions());
274 
275             assertContainer(container.getChildren(), maxDepth - 1);
276         }
277     }
278 
279     /**
280      * Content stream smoke test.
281      */
282     public void testContentStream() throws Exception {
283         if (!isEnabled(TEST_CONTENT_STREAM)) {
284             return;
285         }
286 
287         String repId = getTestRepositoryId();
288         String rootFolder = getTestRootFolder();
289 
290         ObjectInFolderList children = getBinding().getNavigationService().getChildren(repId, rootFolder, null, null,
291                 Boolean.FALSE, IncludeRelationships.BOTH, null, Boolean.FALSE, null, null, null);
292         assertNotNull(children);
293         assertNotNull(children.getObjects());
294 
295         for (ObjectInFolderData object : children.getObjects()) {
296             assertNotNull(object.getObject().getId());
297             assertNotNull(object.getObject().getBaseTypeId());
298 
299             if (object.getObject().getBaseTypeId() == BaseTypeId.CMIS_DOCUMENT) {
300                 ContentStream contentStream = getContent(object.getObject().getId(), null);
301                 readContent(contentStream);
302 
303                 return;
304             }
305         }
306 
307         fail("No document in test folder!");
308     }
309 
310     /**
311      * Query smoke test.
312      */
313     public void testQuery() {
314         if (!isEnabled(TEST_QUERY)) {
315             return;
316         }
317 
318         if (supportsQuery()) {
319             String repId = getTestRepositoryId();
320 
321             ObjectList rs = getBinding().getDiscoveryService().query(repId, "SELECT * FROM cmis:document", null, null,
322                     null, null, null, null, null);
323             assertNotNull(rs);
324 
325             if (rs.getObjects() != null) {
326                 for (ObjectData object : rs.getObjects()) {
327                     assertNotNull(object);
328                     assertNotNull(object.getProperties());
329                     assertNotNull(object.getProperties().getProperties());
330                 }
331             }
332 
333         } else {
334             warning("Query not supported!");
335         }
336     }
337 
338     /**
339      * Checked out smoke test.
340      */
341     public void testCheckedout() {
342         if (!isEnabled(TEST_CHECKEDOUT)) {
343             return;
344         }
345 
346         String repId = getTestRepositoryId();
347 
348         ObjectList co = getBinding().getNavigationService().getCheckedOutDocs(repId, getTestRootFolder(), null, null,
349                 Boolean.TRUE, IncludeRelationships.BOTH, null, BigInteger.valueOf(100), null, null);
350         assertNotNull(co);
351 
352         if (co.getObjects() != null) {
353             assertTrue(co.getObjects().size() <= 100);
354 
355             for (ObjectData object : co.getObjects()) {
356                 assertNotNull(object);
357                 assertNotNull(object.getId());
358                 assertEquals(BaseTypeId.CMIS_DOCUMENT, object.getBaseTypeId());
359             }
360         }
361     }
362 
363     /**
364      * Content changes smoke test.
365      */
366     public void testContentChanges() {
367         if (!isEnabled(TEST_CONTENT_CHANGES)) {
368             return;
369         }
370 
371         if (supportsContentChanges()) {
372             String repId = getTestRepositoryId();
373 
374             ObjectList cc = getBinding().getDiscoveryService().getContentChanges(repId, null, Boolean.TRUE, "*",
375                     Boolean.TRUE, Boolean.TRUE, BigInteger.valueOf(100), null);
376             assertNotNull(cc);
377 
378             if (cc.getObjects() != null) {
379                 assertTrue(cc.getObjects().size() <= 100);
380 
381                 for (ObjectData object : cc.getObjects()) {
382                     assertNotNull(object);
383                     assertNotNull(object.getId());
384                     assertNotNull(object.getChangeEventInfo());
385                     assertNotNull(object.getChangeEventInfo().getChangeType());
386                     assertNotNull(object.getChangeEventInfo().getChangeTime());
387                 }
388             }
389         } else {
390             warning("Content changes not supported!");
391         }
392     }
393 
394     /**
395      * Tests some of the read-only methods of the Object Service.
396      */
397     private void checkObject(String objectId) {
398         System.out.println("Checking object " + objectId + "...");
399 
400         ObjectData object = getObject(objectId);
401 
402         // check properties
403         Properties properties = getBinding().getObjectService().getProperties(getTestRepositoryId(), objectId, "*",
404                 null);
405 
406         assertEquals(object.getProperties(), properties);
407 
408         // check allowable actions
409         AllowableActions allowableActions = getBinding().getObjectService().getAllowableActions(getTestRepositoryId(),
410                 objectId, null);
411 
412         assertEquals(object.getAllowableActions(), allowableActions);
413 
414         // check ACLS
415         if (supportsDiscoverACLs()) {
416             Acl acl = getBinding().getAclService().getAcl(getTestRepositoryId(), objectId, Boolean.FALSE, null);
417 
418             assertEquals(object.getAcl(), acl);
419         } else {
420             warning("ACLs not supported!");
421         }
422 
423         // check policies
424         if (supportsPolicies()) {
425             List<ObjectData> policies = getBinding().getPolicyService().getAppliedPolicies(getTestRepositoryId(),
426                     objectId, null, null);
427 
428             if (policies == null) {
429                 assertNull(object.getPolicyIds().getPolicyIds());
430             } else {
431                 assertNotNull(object.getPolicyIds().getPolicyIds());
432 
433                 List<String> policyIds = new ArrayList<String>();
434 
435                 for (ObjectData policy : policies) {
436                     assertNotNull(policy);
437                     assertNotNull(policy.getId());
438 
439                     policyIds.add(policy.getId());
440                 }
441 
442                 assertEqualLists(object.getPolicyIds().getPolicyIds(), policyIds);
443             }
444         } else {
445             warning("Policies not supported!");
446         }
447 
448         // check renditions
449         if (supportsRenditions()) {
450             List<RenditionData> renditions = getBinding().getObjectService().getRenditions(getTestRepositoryId(),
451                     objectId, null, null, null, null);
452 
453             assertEqualLists(object.getRenditions(), renditions);
454         } else {
455             warning("Renditions not supported!");
456         }
457 
458         // check relationships
459         if (supportsRelationships()) {
460             ObjectList relationships = getBinding().getRelationshipService().getObjectRelationships(
461                     getTestRepositoryId(), objectId, Boolean.TRUE, RelationshipDirection.EITHER, null, "*",
462                     Boolean.TRUE, null, null, null);
463             assertNotNull(relationships);
464 
465             if ((object.getRelationships() != null) && (relationships.getObjects() != null)) {
466                 assertEquals(object.getRelationships().size(), relationships.getObjects().size());
467                 for (ObjectData rel1 : relationships.getObjects()) {
468                     assertBasicProperties(rel1.getProperties());
469                     boolean found = false;
470 
471                     for (ObjectData rel2 : object.getRelationships()) {
472                         if (rel2.getId().equals(rel1.getId())) {
473                             found = true;
474                             assertEquals(rel2.getProperties(), rel1.getProperties());
475                             break;
476                         }
477                     }
478 
479                     assertTrue(found);
480                 }
481             }
482         } else {
483             warning("Relationships not supported!");
484         }
485     }
486 
487     /**
488      * Tests some of the read-only methods of the Versioning Service.
489      */
490     private void checkObjectVersions(String objectId) {
491         System.out.println("Checking versions of object " + objectId + "...");
492 
493         String versionSeriesId = getVersionSeriesId(objectId);
494         assertNotNull(versionSeriesId);
495 
496         // check latest version
497         ObjectData latestVersionObject = getBinding().getVersioningService().getObjectOfLatestVersion(
498                 getTestRepositoryId(), objectId, versionSeriesId, Boolean.FALSE, "*", Boolean.TRUE,
499                 IncludeRelationships.BOTH, null, Boolean.TRUE, Boolean.TRUE, null);
500         assertNotNull(latestVersionObject);
501 
502         Properties latestVersionProperties = getBinding().getVersioningService().getPropertiesOfLatestVersion(
503                 getTestRepositoryId(), objectId, versionSeriesId, Boolean.FALSE, "*", null);
504         assertNotNull(latestVersionProperties);
505 
506         assertEquals(latestVersionObject.getProperties(), latestVersionProperties);
507 
508         String typeName = (String) latestVersionObject.getProperties().getProperties().get(PropertyIds.BASE_TYPE_ID)
509                 .getFirstValue();
510         if (isVersionable(typeName)) {
511             List<ObjectData> allVersions = getBinding().getVersioningService().getAllVersions(getTestRepositoryId(),
512                     objectId, versionSeriesId, "*", Boolean.FALSE, null);
513             assertNotNull(allVersions);
514             assertTrue(allVersions.size() > 0);
515 
516             boolean foundObject = false;
517             boolean foundLatestObject = false;
518             for (ObjectData object : allVersions) {
519                 assertNotNull(object);
520                 assertNotNull(object.getId());
521 
522                 if (objectId.equals(object.getId())) {
523                     foundObject = true;
524                 }
525 
526                 if (latestVersionObject.getId().equals(object.getId())) {
527                     foundLatestObject = true;
528                     assertEquals(latestVersionObject.getProperties(), object.getProperties());
529                 }
530             }
531 
532             if (!foundObject) {
533                 fail("Object " + objectId + " not found in it's version history!");
534             }
535 
536             if (!foundLatestObject) {
537                 fail("Object " + latestVersionObject.getId() + " not found in it's version history!");
538             }
539         }
540     }
541 }