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.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.chemistry.opencmis.client.api.Document;
32  import org.apache.chemistry.opencmis.client.api.Folder;
33  import org.apache.chemistry.opencmis.client.api.Session;
34  import org.apache.chemistry.opencmis.commons.PropertyIds;
35  import org.apache.chemistry.opencmis.commons.data.ContentStream;
36  import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
37  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
38  import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
39  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
40  import org.apache.chemistry.opencmis.commons.impl.IOUtils;
41  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
42  import org.apache.chemistry.opencmis.tck.CmisTestResult;
43  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
44  
45  public class VersioningStateCreateTest extends AbstractSessionTest {
46  
47      @Override
48      public void init(Map<String, String> parameters) {
49          super.init(parameters);
50          setName("Versioning State Create Test");
51          setDescription("Creates documents in different versioning states.");
52      }
53  
54      @Override
55      public void run(Session session) {
56          CmisTestResult f;
57  
58          try {
59              // create folder and document
60              Folder testFolder = createTestFolder(session);
61  
62              DocumentTypeDefinition docType = (DocumentTypeDefinition) session
63                      .getTypeDefinition(getDocumentTestTypeId());
64  
65              if (!docType.isVersionable()) {
66                  addResult(createResult(SKIPPED, "Test type is not versionable. Test skipped!"));
67                  return;
68              }
69  
70              // major version
71              Document docMajor = testFolder.createDocument(getProperties("major.txt"), getContentStream(),
72                      VersioningState.MAJOR, null, null, null, SELECT_ALL_NO_CACHE_OC);
73              addResult(checkObject(session, docMajor, getAllProperties(docMajor), "Major version compliance"));
74  
75              f = createResult(FAILURE, "Document should be major version.");
76              addResult(assertIsTrue(docMajor.isMajorVersion(), null, f));
77  
78              List<Document> versions = docMajor.getAllVersions();
79  
80              f = createResult(FAILURE, "Version series should have one version but has " + versions.size() + ".");
81              addResult(assertEquals(1, versions.size(), null, f));
82  
83              deleteObject(docMajor);
84  
85              // minor version
86              try {
87                  Document docMinor = testFolder.createDocument(getProperties("minor.txt"), getContentStream(),
88                          VersioningState.MINOR, null, null, null, SELECT_ALL_NO_CACHE_OC);
89                  addResult(checkObject(session, docMinor, getAllProperties(docMinor), "Minor version compliance"));
90  
91                  f = createResult(FAILURE, "Document should be minor version.");
92                  addResult(assertIsFalse(docMinor.isMajorVersion(), null, f));
93  
94                  versions = docMinor.getAllVersions();
95  
96                  f = createResult(FAILURE, "Version series should have one version but has " + versions.size() + ".");
97                  addResult(assertEquals(1, versions.size(), null, f));
98  
99                  deleteObject(docMinor);
100             } catch (CmisConstraintException ce) {
101                 addResult(createResult(WARNING, "Creating a minor version failed! "
102                         + "The repository might not support minor versions. Exception: " + ce, ce, false));
103             } catch (CmisInvalidArgumentException iae) {
104                 addResult(createResult(WARNING, "Creating a minor version failed! "
105                         + "The repository might not support minor versions.  Exception: " + iae, iae, false));
106             }
107 
108             // checked out version
109             try {
110                 Document docCheckedOut = testFolder.createDocument(getProperties("checkout.txt"), getContentStream(),
111                         VersioningState.CHECKEDOUT, null, null, null, SELECT_ALL_NO_CACHE_OC);
112                 addResult(checkObject(session, docCheckedOut, getAllProperties(docCheckedOut),
113                         "Checked out version compliance"));
114 
115                 f = createResult(FAILURE, "Version series should be checked out.");
116                 addResult(assertIsTrue(docCheckedOut.isVersionSeriesCheckedOut(), null, f));
117 
118                 versions = docCheckedOut.getAllVersions();
119 
120                 f = createResult(FAILURE, "Version series should have one version but has " + versions.size() + ".");
121                 addResult(assertEquals(1, versions.size(), null, f));
122 
123                 docCheckedOut.cancelCheckOut();
124             } catch (CmisConstraintException ce) {
125                 addResult(createResult(WARNING, "Creating a checked out version failed! "
126                         + "The repository might not support creating checked out versions. Exception: " + ce, ce, false));
127             } catch (CmisInvalidArgumentException iae) {
128                 addResult(createResult(WARNING, "Creating a checked out version failed! "
129                         + "The repository might not  support creating checked out versions.  Exception: " + iae, iae,
130                         false));
131             }
132 
133         } finally {
134             deleteTestFolder();
135         }
136     }
137 
138     private Map<String, Object> getProperties(String name) {
139         Map<String, Object> properties = new HashMap<String, Object>();
140         properties.put(PropertyIds.NAME, name);
141         properties.put(PropertyIds.OBJECT_TYPE_ID, getDocumentTestTypeId());
142 
143         return properties;
144     }
145 
146     private ContentStream getContentStream() {
147         byte[] contentBytes = IOUtils.toUTF8Bytes("some content");
148 
149         return new ContentStreamImpl("content.txt", BigInteger.valueOf(contentBytes.length), "text/plain",
150                 new ByteArrayInputStream(contentBytes));
151     }
152 }