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.tck.tests.versioning;
20  
21  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.FAILURE;
22  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.SKIPPED;
23  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.WARNING;
24  
25  import java.io.ByteArrayInputStream;
26  import java.math.BigInteger;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.chemistry.opencmis.client.api.Document;
31  import org.apache.chemistry.opencmis.client.api.Folder;
32  import org.apache.chemistry.opencmis.client.api.ObjectId;
33  import org.apache.chemistry.opencmis.client.api.Session;
34  import org.apache.chemistry.opencmis.commons.data.ContentStream;
35  import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
36  import org.apache.chemistry.opencmis.commons.enums.Action;
37  import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
38  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
39  import org.apache.chemistry.opencmis.commons.impl.IOUtils;
40  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
41  import org.apache.chemistry.opencmis.tck.CmisTestResult;
42  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
43  
44  public class VersionDeleteTest extends AbstractSessionTest {
45  
46      @Override
47      public void init(Map<String, String> parameters) {
48          super.init(parameters);
49          setName("Versioning Delete Test");
50          setDescription("Creates a document, adds three versions and deletes the current version until the document is gone.");
51      }
52  
53      @Override
54      public void run(Session session) {
55          try {
56              // create folder and document
57              Folder testFolder = createTestFolder(session);
58              Document doc = createDocument(session, testFolder, "versiondeletetest.txt", "v1");
59              DocumentTypeDefinition docType = (DocumentTypeDefinition) doc.getType();
60  
61              if (!docType.isVersionable()) {
62                  addResult(createResult(SKIPPED, "Test type is not versionable. Test skipped!"));
63                  doc.delete(true);
64                  return;
65              }
66  
67              // add versions
68              Document doc2 = createVersion(session, doc, "v2", 2);
69              Document doc3 = createVersion(session, doc2, "v3", 3);
70              Document doc4 = createVersion(session, doc3, "v4", 4);
71  
72              // delete versions
73              deleteVersion(doc4, doc3, 4);
74              deleteVersion(doc3, doc2, 3);
75              deleteVersion(doc2, doc, 2);
76              deleteVersion(doc, null, 1);
77  
78          } finally {
79              deleteTestFolder();
80          }
81      }
82  
83      private Document createVersion(Session session, Document doc, String content, int version) {
84          CmisTestResult f;
85  
86          // check out
87          ObjectId pwcId = doc.checkOut();
88          Document pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
89  
90          addResult(checkObject(session, pwc, getAllProperties(pwc), "PWC " + version + " compliance"));
91  
92          // check in
93          byte[] contentBytes = IOUtils.toUTF8Bytes(content);
94  
95          ContentStream contentStream = new ContentStreamImpl(doc.getName(), BigInteger.valueOf(contentBytes.length),
96                  "text/plain", new ByteArrayInputStream(contentBytes));
97  
98          ObjectId newVersionId = pwc.checkIn(true, null, contentStream, "test version " + version);
99  
100         IOUtils.closeQuietly(contentStream);
101 
102         Document newVersion = (Document) session.getObject(newVersionId, SELECT_ALL_NO_CACHE_OC);
103 
104         addResult(checkObject(session, newVersion, getAllProperties(newVersion), "Version " + version + " compliance"));
105 
106         // check version history
107         List<Document> versions = doc.getAllVersions();
108 
109         f = createResult(FAILURE, "Version series should have " + version + " versions but has " + versions.size()
110                 + "!");
111         addResult(assertEquals(version, versions.size(), null, f));
112 
113         if (!versions.isEmpty()) {
114             f = createResult(FAILURE, "Newly created version " + version + " is not the latest version!");
115             addResult(assertEquals(newVersion.getId(), versions.get(0).getId(), null, f));
116 
117             if (versions.size() > 1) {
118                 f = createResult(FAILURE, "The previous version of version " + version
119                         + " is not the document it has been created from!");
120                 addResult(assertEquals(doc.getId(), versions.get(1).getId(), null, f));
121             }
122         }
123 
124         return newVersion;
125     }
126 
127     private void deleteVersion(Document versionDoc, Document previousDoc, int version) {
128         CmisTestResult f;
129 
130         // check Allowable Action
131         if (!versionDoc.hasAllowableAction(Action.CAN_DELETE_OBJECT)) {
132             addResult(createResult(WARNING, "Version " + version
133                     + " does not have the Allowable Action 'canDeleteObject'."));
134             return;
135         }
136 
137         // get version history before delete
138         List<Document> versionsBefore = versionDoc.getAllVersions();
139 
140         // delete and check
141         try {
142             versionDoc.delete(false);
143         } catch (CmisInvalidArgumentException iae) {
144             addResult(createResult(WARNING, "Deletion of version " + version
145                     + " failed with an invalidArgument exception. "
146                     + "Removing just one version doesn't seem to be supported."));
147             return;
148         } catch (CmisConstraintException ce) {
149             addResult(createResult(WARNING, "Deletion of version " + version + " failed with an constraint exception. "
150                     + "Removing just one version doesn't seem to be supported."));
151             return;
152         }
153 
154         f = createResult(FAILURE, "Deleted version " + version + " still exists!");
155         addResult(assertIsFalse(exists(versionDoc), null, f));
156 
157         // check version history after delete
158         if (previousDoc != null) {
159             List<Document> versionsAfter = previousDoc.getAllVersions();
160 
161             f = createResult(FAILURE, "After version " + version
162                     + " has been deleted, the version history should consist of " + (versionsBefore.size() - 1)
163                     + "  documents but is has " + versionsAfter.size() + " !");
164             addResult(assertEquals(versionsBefore.size() - 1, versionsAfter.size(), null, f));
165         }
166     }
167 }