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.OK;
23  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.SKIPPED;
24  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.WARNING;
25  
26  import java.io.ByteArrayInputStream;
27  import java.math.BigInteger;
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import org.apache.chemistry.opencmis.client.api.Document;
34  import org.apache.chemistry.opencmis.client.api.Folder;
35  import org.apache.chemistry.opencmis.client.api.ObjectId;
36  import org.apache.chemistry.opencmis.client.api.Property;
37  import org.apache.chemistry.opencmis.client.api.Session;
38  import org.apache.chemistry.opencmis.commons.data.ContentStream;
39  import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
40  import org.apache.chemistry.opencmis.commons.enums.Updatability;
41  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
42  import org.apache.chemistry.opencmis.commons.impl.IOUtils;
43  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
44  import org.apache.chemistry.opencmis.tck.CmisTestResult;
45  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
46  import org.apache.chemistry.opencmis.tck.impl.CmisTestResultImpl;
47  
48  /**
49   * Checked out test.
50   */
51  public class VersioningSmokeTest extends AbstractSessionTest {
52  
53      @Override
54      public void init(Map<String, String> parameters) {
55          super.init(parameters);
56          setName("Versioning Smoke Test");
57          setDescription("Creates a document, checks it out, cancels the check out, checks it out again and finally checks it in.");
58      }
59  
60      @Override
61      public void run(Session session) {
62          CmisTestResult f;
63  
64          try {
65              // create folder and document
66              Folder testFolder = createTestFolder(session);
67              Document doc = createDocument(session, testFolder, "versioningtest.txt", "versioning");
68              DocumentTypeDefinition docType = (DocumentTypeDefinition) doc.getType();
69  
70              if (!docType.isVersionable()) {
71                  addResult(createResult(SKIPPED, "Test type is not versionable. Test skipped!"));
72                  doc.delete(true);
73                  return;
74              }
75  
76              // gather properties for later
77              String[] propertiesToCheck = new String[doc.getType().getPropertyDefinitions().size()];
78  
79              int i = 0;
80              for (String propId : doc.getType().getPropertyDefinitions().keySet()) {
81                  propertiesToCheck[i++] = propId;
82              }
83  
84              Map<String, Object> writableProperties = new HashMap<String, Object>();
85              for (Property<?> property : doc.getProperties()) {
86                  if (property.getDefinition().getUpdatability() == Updatability.READWRITE) {
87                      writableProperties.put(property.getId(), property.getValue());
88                  }
89              }
90  
91              // check out
92              ObjectId pwcId = doc.checkOut();
93              Document pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
94  
95              addResult(checkObject(session, pwc, getAllProperties(pwc), "PWC spec compliance - test 1"));
96  
97              checkCheckedOut(pwc);
98  
99              // check version series
100             addResult(checkVersionSeries(session, pwc.getAllVersions(SELECT_ALL_NO_CACHE_OC), propertiesToCheck,
101                     "Test version series after check out"));
102 
103             // cancel checkout
104             pwc.cancelCheckOut();
105 
106             doc.refresh();
107             checkCheckedIn(doc);
108 
109             // check out again
110             pwcId = doc.checkOut();
111             pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
112 
113             addResult(checkObject(session, pwc, getAllProperties(pwc), "PWC spec compliance - test 2"));
114 
115             checkCheckedOut(pwc);
116 
117             // check in
118             ObjectId newVersionId = pwc.checkIn(true, null, null, "Test Version 2");
119             Document newVersion = (Document) session.getObject(newVersionId, SELECT_ALL_NO_CACHE_OC);
120 
121             addResult(checkObject(session, newVersion, getAllProperties(newVersion), "New version compliance"));
122 
123             checkCheckedIn(newVersion);
124 
125             // check version history
126             List<Document> versions = newVersion.getAllVersions(SELECT_ALL_NO_CACHE_OC);
127             f = createResult(FAILURE, "Version series should have 2 versions but has " + versions.size() + "!");
128             addResult(assertEquals(2, versions.size(), null, f));
129 
130             if (!versions.isEmpty()) {
131                 f = createResult(FAILURE,
132                         "Version history order is incorrect! The first version should be the new version.");
133                 addResult(assertEquals(newVersion.getId(), versions.get(0).getId(), null, f));
134 
135                 f = createResult(FAILURE,
136                         "The new version should be the latest version, but cmis:isLatestVersion is not TRUE.");
137                 addResult(assertEquals(true, versions.get(0).isLatestVersion(), null, f));
138 
139                 f = createResult(FAILURE,
140                         "The new version should be the latest major version, but cmis:isLatestMajorVersion is not TRUE.");
141                 addResult(assertEquals(true, versions.get(0).isLatestMajorVersion(), null, f));
142             }
143 
144             if (versions.size() > 1) {
145                 f = createResult(FAILURE,
146                         "Version history order is incorrect! The second version should be the origin document.");
147                 addResult(assertEquals(doc.getId(), versions.get(1).getId(), null, f));
148             }
149 
150             // check version series
151             addResult(checkVersionSeries(session, versions, propertiesToCheck, "Test version series after check in"));
152 
153             // check out again
154             pwcId = newVersion.checkOut();
155             pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
156 
157             addResult(checkObject(session, pwc, getAllProperties(pwc), "PWC spec compliance - test 3"));
158 
159             checkCheckedOut(pwc);
160 
161             // check in giving back all updateable properties
162             ObjectId thirdVersionId = pwc.checkIn(true, writableProperties, null, "Test Version 3");
163             Document thirdVersion = (Document) session.getObject(thirdVersionId, SELECT_ALL_NO_CACHE_OC);
164 
165             addResult(checkObject(session, thirdVersion, getAllProperties(thirdVersion), "New version compliance"));
166 
167             // check out again
168             pwcId = thirdVersion.checkOut();
169             pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
170 
171             addResult(checkObject(session, pwc, getAllProperties(pwc), "PWC spec compliance - test 4"));
172 
173             checkCheckedOut(pwc);
174 
175             // check in giving a new content stream
176             String fourthContent = "new content";
177             byte[] fourthContentBytes = IOUtils.toUTF8Bytes(fourthContent);
178             ContentStream fourthContentStream = new ContentStreamImpl("version4",
179                     BigInteger.valueOf(fourthContentBytes.length), "text/plain", new ByteArrayInputStream(
180                             fourthContentBytes));
181 
182             ObjectId fourthVersionId = pwc.checkIn(true, null, fourthContentStream, "Test Version 5");
183             Document fourthVersion = (Document) session.getObject(fourthVersionId, SELECT_ALL_NO_CACHE_OC);
184 
185             addResult(checkObject(session, fourthVersion, getAllProperties(fourthVersion), "New version compliance"));
186 
187             checkCheckedIn(fourthVersion);
188 
189             // check out again
190             pwcId = fourthVersion.checkOut();
191             pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
192 
193             addResult(checkObject(session, pwc, getAllProperties(pwc), "PWC spec compliance - test 5"));
194 
195             checkCheckedOut(pwc);
196 
197             // check in giving properties and a new content stream
198             String fifthContent = "brand-new content";
199             byte[] fifthContentBytes = IOUtils.toUTF8Bytes(fifthContent);
200             ContentStream fifthContentStream = new ContentStreamImpl("version5",
201                     BigInteger.valueOf(fifthContentBytes.length), "text/plain", new ByteArrayInputStream(
202                             fifthContentBytes));
203 
204             ObjectId fifthVersionId = pwc.checkIn(true, writableProperties, fifthContentStream, "Test Version 5");
205             Document fifthVersion = (Document) session.getObject(fifthVersionId, SELECT_ALL_NO_CACHE_OC);
206 
207             addResult(checkObject(session, fifthVersion, getAllProperties(fifthVersion), "New version compliance"));
208 
209             checkCheckedIn(fifthVersion);
210 
211             // test the latest version
212             Document latest = session.getLatestDocumentVersion(doc, SELECT_ALL_NO_CACHE_OC);
213 
214             f = createResult(FAILURE, "getObjectOfLatestVersion() did not return the expected version!");
215             addResult(assertEquals(fifthVersion.getId(), latest.getId(), null, f));
216 
217             // test if checking out a non-latest version works for this
218             // repository
219             try {
220                 pwcId = doc.checkOut();
221                 pwc = (Document) session.getObject(pwcId, SELECT_ALL_NO_CACHE_OC);
222                 pwc.cancelCheckOut();
223 
224                 addResult(createInfoResult("Repository allows check out on a version that is not the latest version."));
225             } catch (CmisBaseException e) {
226                 addResult(createInfoResult("Repository only support check out on the latest version."));
227             }
228 
229             // remove the document
230             deleteObject(doc);
231 
232             // test if all versions have been deleted
233             f = createResult(FAILURE, "Version 2 has not been deleted!");
234             addResult(assertIsFalse(session.exists(newVersion), null, f));
235 
236             f = createResult(FAILURE, "Version 3 has not been deleted!");
237             addResult(assertIsFalse(session.exists(thirdVersion), null, f));
238 
239             f = createResult(FAILURE, "Version 4 has not been deleted!");
240             addResult(assertIsFalse(session.exists(fourthVersion), null, f));
241 
242             f = createResult(FAILURE, "Version 5 has not been deleted!");
243             addResult(assertIsFalse(session.exists(fifthVersion), null, f));
244         } finally {
245             deleteTestFolder();
246         }
247     }
248 
249     private void checkCheckedOut(Document pwc) {
250         CmisTestResult f;
251 
252         f = createResult(FAILURE, "Version series has a PWC but cmis:isVersionSeriesCheckedOut is not TRUE!");
253         addResult(assertIsTrue(pwc.isVersionSeriesCheckedOut(), null, f));
254 
255         if (pwc.getVersionSeriesCheckedOutId() == null) {
256             addResult(createResult(WARNING, "cmis:versionSeriesCheckedOutId is not set!"));
257         } else {
258             f = createResult(FAILURE, "PWC id and cmis:versionSeriesCheckedOutId don't match!");
259             addResult(assertEquals(pwc.getId(), pwc.getVersionSeriesCheckedOutId(), null, f));
260         }
261 
262         f = createResult(WARNING, "PWC does not have a value for cmis:versionSeriesCheckedOutBy!");
263         addResult(assertStringNotEmpty(pwc.getVersionSeriesCheckedOutBy(), null, f));
264     }
265 
266     private void checkCheckedIn(Document doc) {
267         CmisTestResult f;
268 
269         f = createResult(FAILURE, "Version series is not checked out but cmis:isVersionSeriesCheckedOut is not FALSE!");
270         addResult(assertIsFalse(doc.isVersionSeriesCheckedOut(), null, f));
271 
272         f = createResult(FAILURE, "Version series is not checked out but cmis:versionSeriesCheckedOutId has a value!");
273         addResult(assertNull(doc.getVersionSeriesCheckedOutId(), null, f));
274 
275         f = createResult(FAILURE, "Version series is not checked out but cmis:versionSeriesCheckedOutBy has a value!");
276         addResult(assertNull(doc.getVersionSeriesCheckedOutBy(), null, f));
277     }
278 
279     private CmisTestResult checkVersionSeries(Session session, List<Document> versions, String[] properties,
280             String message) {
281         List<CmisTestResult> results = new ArrayList<CmisTestResult>();
282         CmisTestResult f;
283 
284         // make sure there is only one latest version
285         // and zero or one latest major version
286         int countLatest = 0;
287         int countLatestMajor = 0;
288         String latestId = null;
289         for (Document version : versions) {
290             addResult(results, checkObject(session, version, properties, "Version object check: " + version.getId()));
291 
292             if (Boolean.TRUE.equals(version.isLatestVersion())) {
293                 countLatest++;
294                 latestId = version.getId();
295             }
296 
297             if (Boolean.TRUE.equals(version.isLatestMajorVersion())) {
298                 countLatestMajor++;
299             }
300         }
301 
302         f = createResult(FAILURE, "The version series must have exactly one latest version, but it has " + countLatest
303                 + "!");
304         addResult(results, assertEquals(1, countLatest, null, f));
305 
306         f = createResult(FAILURE, "The version series must have zero or one latest major version, but it has "
307                 + countLatestMajor + "!");
308         addResult(results, assertIsTrue(countLatestMajor < 2, null, f));
309 
310         // check getObjectOfLatestVersion()
311         if (countLatest == 1) {
312             Document latestVersion = versions.get(0).getObjectOfLatestVersion(false, SELECT_ALL_NO_CACHE_OC);
313             addResult(
314                     results,
315                     checkObject(session, latestVersion, properties,
316                             "Latest version object check: " + latestVersion.getId()));
317 
318             f = createResult(FAILURE,
319                     "The version that is flagged as latest version is not returned by getObjectOfLatestVersion()!");
320             addResult(results, assertEquals(latestId, latestVersion.getId(), null, f));
321 
322             // check with session.getLatestDocumentVersion()
323             Document latestVersion2 = session.getLatestDocumentVersion(versions.get(versions.size() - 1).getId(),
324                     SELECT_ALL_NO_CACHE_OC);
325 
326             addResult(
327                     results,
328                     checkObject(session, latestVersion2, properties, "Latest version object check (2): "
329                             + latestVersion2.getId()));
330 
331             f = createResult(FAILURE,
332                     "The version that is flagged as latest version is not returned by getObjectOfLatestVersion()!");
333             addResult(results, assertEquals(latestId, latestVersion2.getId(), null, f));
334         }
335 
336         CmisTestResultImpl result = createResult(getWorst(results), message);
337         result.getChildren().addAll(results);
338 
339         return result.getStatus().getLevel() <= OK.getLevel() ? null : result;
340     }
341 }