This project has retired. For details please refer to its Attic page.
AbstractSimpleReadWriteTests 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.Ace;
23  import org.apache.chemistry.opencmis.commons.data.Acl;
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.ObjectInFolderData;
27  import org.apache.chemistry.opencmis.commons.data.Properties;
28  import org.apache.chemistry.opencmis.commons.data.PropertyData;
29  import org.apache.chemistry.opencmis.commons.enums.CapabilityContentStreamUpdates;
30  import org.apache.chemistry.opencmis.commons.enums.UnfileObject;
31  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
32  import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
33  import org.apache.chemistry.opencmis.commons.spi.Holder;
34  
35  import java.util.ArrayList;
36  import java.util.Collections;
37  import java.util.List;
38  
39  /**
40   * Simple read-write test.
41   */
42  public abstract class AbstractSimpleReadWriteTests extends AbstractCmisTestCase {
43  
44      public static final String TEST_CREATE_FOLDER = "createFolder";
45      public static final String TEST_CREATE_DOCUMENT = "createDocument";
46      public static final String TEST_CREATE_FROM_SOURCE = "createDocumentFromSource";
47      public static final String TEST_SET_AND_DELETE_CONTENT = "setAndDeleteContent";
48      public static final String TEST_UPDATE_PROPERTIES = "updateProperties";
49      public static final String TEST_DELETE_TREE = "deleteTree";
50      public static final String TEST_MOVE_OBJECT = "moveObject";
51      public static final String TEST_COPY_OBJECT = "copyObject";
52      public static final String TEST_VERSIONING = "versioning";
53  
54      private static final byte[] CONTENT = "My document test content!".getBytes();
55      private static final byte[] CONTENT2 = "Another test content!".getBytes();
56      private static final String CONTENT_TYPE = "text/plain";
57  
58      /**
59       * Tests folder creation.
60       */
61      public void testCreateFolder() {
62          if (!isEnabled(TEST_CREATE_FOLDER)) {
63              return;
64          }
65  
66          // create folder
67          List<PropertyData<?>> propList = new ArrayList<PropertyData<?>>();
68          propList.add(getObjectFactory().createPropertyStringData(PropertyIds.NAME, "testfolder"));
69          propList.add(getObjectFactory().createPropertyIdData(PropertyIds.OBJECT_TYPE_ID, getDefaultFolderType()));
70  
71          Properties properties = getObjectFactory().createPropertiesData(propList);
72  
73          String folderId = createFolder(properties, getTestRootFolder(), null, null, null);
74  
75          // delete folder
76          delete(folderId, true);
77      }
78  
79      /**
80       * Tests document creation.
81       */
82      public void testCreateDocument() throws Exception {
83          if (!isEnabled(TEST_CREATE_DOCUMENT)) {
84              return;
85          }
86  
87          VersioningState vs = (isVersionable(getDefaultDocumentType()) ? VersioningState.MAJOR : VersioningState.NONE);
88  
89          // create document
90          List<PropertyData<?>> propList = new ArrayList<PropertyData<?>>();
91          propList.add(getObjectFactory().createPropertyStringData(PropertyIds.NAME, "testdoc.txt"));
92          propList.add(getObjectFactory().createPropertyIdData(PropertyIds.OBJECT_TYPE_ID, getDefaultDocumentType()));
93  
94          Properties properties = getObjectFactory().createPropertiesData(propList);
95  
96          ContentStream contentStream = createContentStreamData(CONTENT_TYPE, CONTENT);
97  
98          String docId = createDocument(properties, getTestRootFolder(), contentStream, vs, null, null, null);
99  
100         // read and assert content
101         ContentStream contentStream2 = getContent(docId, null);
102         assertMimeType(CONTENT_TYPE, contentStream2.getMimeType());
103         if (contentStream2.getBigLength() != null) {
104             assertEquals(CONTENT.length, contentStream2.getBigLength().intValue());
105         }
106 
107         byte[] content = readContent(contentStream2);
108         assertContent(CONTENT, content);
109 
110         // apply an ACL
111         if (supportsManageACLs()) {
112             Ace ace = getObjectFactory()
113                     .createAccessControlEntry(getUsername(), Collections.singletonList("cmis:read"));
114             Acl acl = getObjectFactory().createAccessControlList(Collections.singletonList(ace));
115 
116             Acl newAcl = getBinding().getAclService().applyAcl(getTestRepositoryId(), docId, acl, null,
117                     getAclPropagation(), null);
118             assertNotNull(newAcl);
119 
120             Acl readAcl = getBinding().getAclService().getAcl(getTestRepositoryId(), docId, Boolean.FALSE, null);
121             assertNotNull(readAcl);
122 
123             assertEquals(newAcl, readAcl);
124         } else {
125             warning("ACLs management not supported!");
126         }
127 
128         // delete document
129         delete(docId, true);
130     }
131 
132     /**
133      * Tests document creation from source.
134      */
135     public void testCreateDocumentFromSource() throws Exception {
136         if (!isEnabled(TEST_CREATE_FROM_SOURCE)) {
137             return;
138         }
139 
140         VersioningState vs = (isVersionable(getDefaultDocumentType()) ? VersioningState.MAJOR : VersioningState.NONE);
141 
142         String docId = createDefaultDocument(getTestRootFolder(), "testdoc.org.txt", CONTENT_TYPE, CONTENT);
143 
144         // create a copy
145         List<PropertyData<?>> propList2 = new ArrayList<PropertyData<?>>();
146         propList2.add(getObjectFactory().createPropertyStringData(PropertyIds.NAME, "testdoc.copy.txt"));
147 
148         Properties properties2 = getObjectFactory().createPropertiesData(propList2);
149 
150         String docId2 = createDocumentFromSource(docId, properties2, getTestRootFolder(), vs, null, null, null);
151 
152         // get objects
153         getObject(docId);
154         getObject(docId2);
155 
156         // read and assert content
157         ContentStream contentStream2 = getContent(docId, null);
158         ContentStream contentStream3 = getContent(docId2, null);
159 
160         assertEquals(contentStream2.getMimeType(), contentStream3.getMimeType());
161         assertEquals(contentStream2.getBigLength(), contentStream3.getBigLength());
162 
163         byte[] content2 = readContent(contentStream2);
164         byte[] content3 = readContent(contentStream3);
165         assertContent(content2, content3);
166 
167         // delete documents
168         delete(docId, true);
169         delete(docId2, true);
170     }
171 
172     /**
173      * Tests setting and deleting content stream.
174      */
175     public void testSetAndDeleteContent() throws Exception {
176         if (!isEnabled(TEST_SET_AND_DELETE_CONTENT)) {
177             return;
178         }
179 
180         boolean requiresCheckOut = getRepositoryInfo().getCapabilities().getContentStreamUpdatesCapability() == CapabilityContentStreamUpdates.PWCONLY;
181 
182         boolean isVersionable = isVersionable(getDefaultDocumentType());
183 
184         String docId = createDefaultDocument(getTestRootFolder(), "testcontent.txt", CONTENT_TYPE, CONTENT);
185 
186         // if a check out is required, do it
187         Holder<String> docIdHolder = new Holder<String>(docId);
188         if (requiresCheckOut) {
189             if (isVersionable) {
190                 getBinding().getVersioningService().checkOut(getTestRepositoryId(), docIdHolder, null, null);
191             } else {
192                 warning("Default document type is not versionable!");
193                 delete(docId, true);
194                 return;
195             }
196         }
197 
198         String docIdWorkingCopy = docIdHolder.getValue();
199 
200         // delete content
201         try {
202             getBinding().getObjectService().deleteContentStream(getTestRepositoryId(), docIdHolder, null, null);
203         } catch (CmisNotSupportedException e) {
204             warning("deleteContentStream not supported!");
205         }
206 
207         // set content
208         ContentStream contentStream2 = createContentStreamData(CONTENT_TYPE, CONTENT2);
209 
210         docIdHolder = new Holder<String>(docIdWorkingCopy);
211         getBinding().getObjectService().setContentStream(getTestRepositoryId(), docIdHolder, true, null,
212                 contentStream2, null);
213 
214         // read and assert content
215         String newVersionDocId = (docIdHolder.getValue() == null ? docIdWorkingCopy : docIdHolder.getValue());
216         ContentStream contentStream3 = getContent(newVersionDocId, null);
217         assertMimeType(CONTENT_TYPE, contentStream3.getMimeType());
218         if (contentStream3.getBigLength() != null) {
219             assertEquals(CONTENT2.length, contentStream3.getBigLength().intValue());
220         }
221 
222         byte[] content = readContent(contentStream3);
223         assertContent(CONTENT2, content);
224 
225         // if it has been checked out, cancel that
226         if (requiresCheckOut) {
227             getBinding().getVersioningService().cancelCheckOut(getTestRepositoryId(), docIdWorkingCopy, null);
228         }
229 
230         // delete document
231         delete(docId, true);
232     }
233 
234     /**
235      * Tests property updates.
236      */
237     public void testUpdateProperties() {
238         if (!isEnabled(TEST_UPDATE_PROPERTIES)) {
239             return;
240         }
241 
242         String name1 = "updateTest1.txt";
243         String name2 = "updateTest2.txt";
244 
245         // create document
246         String docId = createDefaultDocument(getTestRootFolder(), name1, CONTENT_TYPE, CONTENT);
247 
248         // update
249         List<PropertyData<?>> updatePropList = new ArrayList<PropertyData<?>>();
250         updatePropList.add(getObjectFactory().createPropertyStringData(PropertyIds.NAME, name2));
251 
252         Properties updateProperties = getObjectFactory().createPropertiesData(updatePropList);
253 
254         Holder<String> docIdHolder = new Holder<String>(docId);
255         getBinding().getObjectService().updateProperties(getTestRepositoryId(), docIdHolder, null, updateProperties,
256                 null);
257 
258         // get new id and check name property
259         docId = docIdHolder.getValue();
260 
261         ObjectData updatedObject = getObject(docId);
262         String updatedName = (String) updatedObject.getProperties().getProperties().get(PropertyIds.NAME)
263                 .getFirstValue();
264         assertNotNull(updatedName);
265         assertEquals(name2, updatedName);
266 
267         // delete document
268         delete(docId, true);
269     }
270 
271     /**
272      * Tests delete tree.
273      */
274     public void testDeleteTree() {
275         if (!isEnabled(TEST_DELETE_TREE)) {
276             return;
277         }
278 
279         // create a folder tree
280         String folder1 = createDefaultFolder(getTestRootFolder(), "folder1");
281         String folder11 = createDefaultFolder(folder1, "folder11");
282         String folder12 = createDefaultFolder(folder1, "folder12");
283         String folder121 = createDefaultFolder(folder12, "folder121");
284         String folder122 = createDefaultFolder(folder12, "folder122");
285 
286         // create a few documents
287         String doc111 = createDefaultDocument(folder11, "doc111.txt", CONTENT_TYPE, CONTENT);
288         String doc1221 = createDefaultDocument(folder122, "doc1221.txt", CONTENT_TYPE, CONTENT2);
289 
290         // delete the tree
291         getBinding().getObjectService().deleteTree(getTestRepositoryId(), folder1, Boolean.TRUE, UnfileObject.DELETE,
292                 Boolean.TRUE, null);
293 
294         assertFalse(existsObject(folder1));
295         assertFalse(existsObject(folder11));
296         assertFalse(existsObject(folder12));
297         assertFalse(existsObject(folder121));
298         assertFalse(existsObject(folder122));
299         assertFalse(existsObject(doc111));
300         assertFalse(existsObject(doc1221));
301     }
302 
303     /**
304      * Tests move object.
305      */
306     public void testMoveObject() {
307         if (!isEnabled(TEST_MOVE_OBJECT)) {
308             return;
309         }
310 
311         // create folders
312         String folder1 = createDefaultFolder(getTestRootFolder(), "folder1");
313         String folder2 = createDefaultFolder(getTestRootFolder(), "folder2");
314 
315         // create document
316         String docId = createDefaultDocument(folder1, "testdoc.txt", CONTENT_TYPE, CONTENT);
317 
318         // move it
319         Holder<String> docIdHolder = new Holder<String>(docId);
320         getBinding().getObjectService().moveObject(getTestRepositoryId(), docIdHolder, folder2, folder1, null);
321         assertNotNull(docIdHolder.getValue());
322 
323         assertTrue(existsObject(docIdHolder.getValue()));
324         getChild(folder2, docIdHolder.getValue());
325 
326         deleteTree(folder1);
327         deleteTree(folder2);
328     }
329 
330     /**
331      * Tests copy object.
332      */
333     public void testCopyObject() {
334         if (!isEnabled(TEST_COPY_OBJECT)) {
335             return;
336         }
337 
338         // create folders
339         String folder1 = createDefaultFolder(getTestRootFolder(), "folder1");
340         String folder2 = createDefaultFolder(getTestRootFolder(), "folder2");
341 
342         // create document
343         String docId = createDefaultDocument(folder1, "testdoc.txt", CONTENT_TYPE, CONTENT);
344 
345         // copy it with new properties
346         List<PropertyData<?>> updatePropList = new ArrayList<PropertyData<?>>();
347         updatePropList.add(getObjectFactory().createPropertyStringData(PropertyIds.NAME, "newdocname"));
348         Properties updateProperties = getObjectFactory().createPropertiesData(updatePropList);
349 
350         String copyId = getBinding().getObjectService().createDocumentFromSource(getTestRepositoryId(), docId,
351                 updateProperties, folder2, null, null, null, null, null);
352         assertNotNull(copyId);
353 
354         assertTrue(existsObject(copyId));
355         ObjectInFolderData copy = getChild(folder2, copyId);
356         String updatedName = (String) copy.getObject().getProperties().getProperties().get(PropertyIds.NAME)
357                 .getFirstValue();
358         assertEquals("newdocname", updatedName);
359 
360         deleteTree(folder1);
361         deleteTree(folder2);
362     }
363 
364     /**
365      * Test check-in/check-out.
366      */
367     public void testVersioning() {
368         if (!isEnabled(TEST_VERSIONING)) {
369             return;
370         }
371 
372         if (!isVersionable(getDefaultDocumentType())) {
373             warning("Default document type is not versionable!");
374             return;
375         }
376 
377         // create document
378         String docId = createDefaultDocument(getTestRootFolder(), "versionTest.txt", CONTENT_TYPE, CONTENT);
379 
380         // there must be only one version in the version series
381         List<ObjectData> allVersions = getBinding().getVersioningService().getAllVersions(getTestRepositoryId(), docId,
382                 getVersionSeriesId(docId), "*", Boolean.FALSE, null);
383         assertNotNull(allVersions);
384         assertEquals(1, allVersions.size());
385 
386         assertEquals(docId, allVersions.get(0).getId());
387 
388         // check out
389         Holder<String> versionIdHolder = new Holder<String>(docId);
390         getBinding().getVersioningService().checkOut(getTestRepositoryId(), versionIdHolder, null, null);
391         String versionId = versionIdHolder.getValue();
392 
393         // object must be marked as checked out
394         assertTrue(isCheckedOut(docId));
395 
396         // cancel check out
397         getBinding().getVersioningService().cancelCheckOut(getTestRepositoryId(), versionId, null);
398 
399         // object must NOT be marked as checked out
400         assertFalse(isCheckedOut(docId));
401 
402         // check out again
403         versionIdHolder.setValue(docId);
404         getBinding().getVersioningService().checkOut(getTestRepositoryId(), versionIdHolder, null, null);
405         versionId = versionIdHolder.getValue();
406 
407         // object must be marked as checked out
408         assertTrue(isCheckedOut(docId));
409 
410         versionIdHolder.setValue(versionId);
411         getBinding().getVersioningService().checkIn(getTestRepositoryId(), versionIdHolder, Boolean.TRUE, null, null,
412                 "Test Version 2", null, null, null, null);
413         docId = versionIdHolder.getValue();
414 
415         // object must NOT be marked as checked out
416         assertFalse(isCheckedOut(docId));
417 
418         // there must be exactly two versions in the version series
419         allVersions = getBinding().getVersioningService().getAllVersions(getTestRepositoryId(), docId,
420                 getVersionSeriesId(docId), "*", Boolean.FALSE, null);
421         assertNotNull(allVersions);
422         assertEquals(2, allVersions.size());
423 
424         // delete document
425         delete(docId, true);
426     }
427 
428     private boolean isCheckedOut(String docId) {
429         ObjectData object = getObject(docId);
430         PropertyData<?> isCheckedOut = object.getProperties().getProperties()
431                 .get(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT);
432         assertNotNull(isCheckedOut);
433         assertTrue(isCheckedOut.getFirstValue() instanceof Boolean);
434 
435         return ((Boolean) isCheckedOut.getFirstValue()).booleanValue();
436     }
437 }