This project has retired. For details please refer to its Attic page.
SetAndDeleteContentTest 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.tck.tests.crud;
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.UNEXPECTED_EXCEPTION;
24  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.WARNING;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.IOException;
28  import java.math.BigInteger;
29  import java.util.List;
30  import java.util.Map;
31  
32  import org.apache.chemistry.opencmis.client.api.Document;
33  import org.apache.chemistry.opencmis.client.api.Folder;
34  import org.apache.chemistry.opencmis.client.api.ObjectId;
35  import org.apache.chemistry.opencmis.client.api.Session;
36  import org.apache.chemistry.opencmis.commons.data.ContentStream;
37  import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
38  import org.apache.chemistry.opencmis.commons.enums.Action;
39  import org.apache.chemistry.opencmis.commons.enums.BindingType;
40  import org.apache.chemistry.opencmis.commons.enums.CapabilityContentStreamUpdates;
41  import org.apache.chemistry.opencmis.commons.enums.ContentStreamAllowed;
42  import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
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  
47  /**
48   * Copy test.
49   */
50  public class SetAndDeleteContentTest extends AbstractSessionTest {
51  
52      private static final String CONTENT1 = "one";
53      private static final String CONTENT2 = "two";
54  
55      @Override
56      public void init(Map<String, String> parameters) {
57          super.init(parameters);
58          setName("Set and Delete content Test");
59          setDescription("Creates a new document and tries to set and delete its content.");
60      }
61  
62      @Override
63      public void run(Session session) {
64          CmisTestResult f;
65  
66          if (getContentStreamUpdatesCapbility(session) == CapabilityContentStreamUpdates.NONE) {
67              addResult(createInfoResult("Stream updates are not supported. Test skipped!"));
68              return;
69          }
70  
71          try {
72              // create folder and document
73              Folder testFolder = createTestFolder(session);
74              Document doc = createDocument(session, testFolder, "contenttest.txt", CONTENT1);
75              Document workDoc = doc;
76              DocumentTypeDefinition docType = (DocumentTypeDefinition) doc.getType();
77  
78              // test if check out is required and possible
79              boolean checkedout = false;
80              if (!doc.getAllowableActions().getAllowableActions().contains(Action.CAN_SET_CONTENT_STREAM)) {
81                  if (!docType.isVersionable()) {
82                      addResult(createResult(SKIPPED,
83                              "The test document does not accept a new content stream. Test skipped!"));
84                      doc.delete(true);
85                      return;
86                  } else {
87                      workDoc = (Document) session.getObject(doc.checkOut(), SELECT_ALL_NO_CACHE_OC);
88                      checkedout = true;
89  
90                      if (!workDoc.getAllowableActions().getAllowableActions().contains(Action.CAN_SET_CONTENT_STREAM)) {
91                          addResult(createResult(SKIPPED,
92                                  "The test PWC does not accept a new content stream. Test skipped!"));
93                          workDoc.cancelCheckOut();
94                          doc.delete(true);
95                          return;
96                      }
97                  }
98              }
99  
100             // test if the content stream can be deleted
101             if (docType.getContentStreamAllowed() == ContentStreamAllowed.REQUIRED) {
102                 addResult(createResult(SKIPPED,
103                         "A content stream is required for this docuemnt type. deleteContentStream() test skipped!"));
104             } else {
105                 // delete content stream
106                 try {
107                     ObjectId newObjectId = workDoc.deleteContentStream(true);
108 
109                     // deleteContentStream may have created a new version
110                     Document contentDoc = getNewVersion(session, workDoc, checkedout, newObjectId,
111                             "deleteContentStream()");
112 
113                     f = createResult(FAILURE, "Document still has content after deleteContentStream() has been called!");
114                     addResult(assertNull(contentDoc.getContentStream(), null, f));
115 
116                     workDoc = contentDoc;
117                 } catch (CmisNotSupportedException e) {
118                     addResult(createResult(WARNING, "deleteContentStream() is not supported!"));
119                 }
120             }
121 
122             // set a new content stream
123             byte[] contentBytes = new byte[0];
124             try {
125                 contentBytes = CONTENT2.getBytes("UTF-8");
126             } catch (Exception e) {
127             }
128 
129             ContentStream contentStream = new ContentStreamImpl(workDoc.getName(),
130                     BigInteger.valueOf(contentBytes.length), "text/plain", new ByteArrayInputStream(contentBytes));
131 
132             ObjectId newObjectId = workDoc.setContentStream(contentStream, true, true);
133 
134             // setContentStream may have created a new version
135             Document contentDoc = getNewVersion(session, workDoc, checkedout, newObjectId, "setContentStream()");
136 
137             // test new content
138             try {
139                 String content = getStringFromContentStream(contentDoc.getContentStream());
140                 f = createResult(FAILURE, "Document content doesn't match the content set by setContentStream()!");
141                 addResult(assertEquals(CONTENT2, content, null, f));
142             } catch (IOException e) {
143                 addResult(createResult(UNEXPECTED_EXCEPTION,
144                         "Document content couldn't be read! Exception: " + e.getMessage(), e, true));
145             }
146 
147             // cancel a possible check out
148             if (checkedout) {
149                 workDoc.cancelCheckOut();
150             }
151 
152             // remove the document
153             deleteObject(doc);
154         } finally {
155             deleteTestFolder();
156         }
157     }
158 
159     private CapabilityContentStreamUpdates getContentStreamUpdatesCapbility(Session session) {
160         if (session.getRepositoryInfo().getCapabilities() == null) {
161             return null;
162         }
163 
164         return session.getRepositoryInfo().getCapabilities().getContentStreamUpdatesCapability();
165     }
166 
167     private Document getNewVersion(Session session, Document orgDoc, boolean checkedout, ObjectId newObjectId,
168             String operation) {
169         Document result = orgDoc;
170 
171         if (newObjectId != null) {
172             // -> Non AtomPub binding
173             if (!orgDoc.getId().equals(newObjectId.getId())) {
174                 if (checkedout) {
175                     addResult(createResult(FAILURE, operation + " created a new version from a PWC!"));
176                 } else {
177                     result = (Document) session.getObject(newObjectId, SELECT_ALL_NO_CACHE_OC);
178                     addResult(checkObject(session, result, getAllProperties(result), "Version created by " + operation
179                             + "  compliance"));
180                 }
181             }
182         } else {
183             if (getBinding() != BindingType.ATOMPUB) {
184                 addResult(createResult(FAILURE, operation + " did not return an object id!"));
185             }
186 
187             // -> AtomPub binding or incompliant other binding
188             if (checkedout) {
189                 // we cannot check if the repository does the right thing,
190                 // but if there is a problem the versioning tests should
191                 // catch it
192             } else if (Boolean.TRUE.equals(((DocumentTypeDefinition) orgDoc.getType()).isVersionable())) {
193                 List<Document> versions = orgDoc.getAllVersions();
194                 if (versions == null || versions.isEmpty()) {
195                     addResult(createResult(FAILURE, operation
196                             + " created a new version but the version history is empty!"));
197                 } else if (!orgDoc.getId().equals(versions.get(0).getId())) {
198                     result = (Document) session.getObject(versions.get(0), SELECT_ALL_NO_CACHE_OC);
199                     addResult(checkObject(session, result, getAllProperties(result), "Version created by " + operation
200                             + " compliance"));
201                 }
202             }
203         }
204 
205         return result;
206     }
207 }