View Javadoc

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