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.crud;
20  
21  import java.io.IOException;
22  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.FAILURE;
23  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.INFO;
24  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.WARNING;
25  
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.HashSet;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Set;
32  
33  import org.apache.chemistry.opencmis.client.api.CmisObject;
34  import org.apache.chemistry.opencmis.client.api.Document;
35  import org.apache.chemistry.opencmis.client.api.DocumentType;
36  import org.apache.chemistry.opencmis.client.api.Folder;
37  import org.apache.chemistry.opencmis.client.api.ItemIterable;
38  import org.apache.chemistry.opencmis.client.api.OperationContext;
39  import org.apache.chemistry.opencmis.client.api.Session;
40  import org.apache.chemistry.opencmis.commons.data.ContentStream;
41  import org.apache.chemistry.opencmis.commons.impl.IOUtils;
42  import org.apache.chemistry.opencmis.tck.CmisTestResult;
43  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
44  
45  /**
46   * Simple document test.
47   */
48  public class CreateAndDeleteDocumentTest extends AbstractSessionTest {
49  
50      private static final String CONTENT = "TCK test content.";
51  
52      @Override
53      public void init(Map<String, String> parameters) {
54          super.init(parameters);
55          setName("Create and Delete Document Test");
56          setDescription("Creates a few documents, checks the newly created documents and their parent and finally deletes the created documents.");
57      }
58  
59      @Override
60      public void run(Session session) {
61          CmisTestResult f;
62  
63          int numOfDocuments = 20;
64  
65          OperationContext orderContext = isOrderByNameSupported(session) ? SELECT_ALL_NO_CACHE_OC_ORDER_BY_NAME
66                  : SELECT_ALL_NO_CACHE_OC;
67  
68          // create a test folder
69          Folder testFolder = createTestFolder(session);
70  
71          try {
72              Map<String, Document> documents = new HashMap<String, Document>();
73              Set<String> versionSeriesIds = new HashSet<String>();
74  
75              // create documents
76              for (int i = 0; i < numOfDocuments; i++) {
77                  Document newDocument = createDocument(session, testFolder, "doc" + i, CONTENT);
78                  documents.put(newDocument.getId(), newDocument);
79                  versionSeriesIds.add(newDocument.getVersionSeriesId());
80              }
81  
82              // simple children test
83              addResult(checkChildren(session, testFolder, "Test folder children check"));
84  
85              // check if all documents are there
86              ItemIterable<CmisObject> children = testFolder.getChildren(SELECT_ALL_NO_CACHE_OC);
87              List<String> childrenIds = new ArrayList<String>();
88              for (CmisObject child : children) {
89                  if (child != null) {
90                      childrenIds.add(child.getId());
91                      Document document = documents.get(child.getId());
92  
93                      f = createResult(FAILURE, "Document and test folder child don't match! Id: " + child.getId());
94                      addResult(assertShallowEquals(document, child, null, f));
95                  }
96              }
97  
98              f = createResult(FAILURE, "Number of created documents does not match the number of existing documents!");
99              addResult(assertEquals(numOfDocuments, childrenIds.size(), null, f));
100 
101             for (Document document : documents.values()) {
102                 if (!childrenIds.contains(document.getId())) {
103                     addResult(createResult(FAILURE, "Created document not found in test folder children! Id: "
104                             + document.getId()));
105                 }
106             }
107 
108             // check version series ids
109             if (Boolean.TRUE.equals(((DocumentType) documents.values().iterator().next().getType()).isVersionable())) {
110                 f = createResult(FAILURE,
111                         "Although the created documents are independent, some documents share a Version Series Id!");
112             } else {
113                 f = createResult(INFO, "Some documents share the same Version Series Id.");
114             }
115 
116             addResult(assertEquals(numOfDocuments, versionSeriesIds.size(), null, f));
117 
118             // check paging
119             int pageSize = 5;
120             CmisObject lastObject = null;
121 
122             int count = 0;
123             ItemIterable<CmisObject> page1 = testFolder.getChildren(orderContext).getPage(pageSize);
124             for (CmisObject child : page1) {
125                 count++;
126                 lastObject = child;
127             }
128 
129             f = createResult(FAILURE, "Returned number of children doesn't match the page size!");
130             addResult(assertEquals(pageSize, count, null, f));
131 
132             if (page1.getTotalNumItems() == -1) {
133                 addResult(createResult(WARNING, "Repository did not return numItems for the first test page."));
134             } else {
135                 f = createResult(FAILURE, "Returned numItems doesn't match the number of documents!");
136                 addResult(assertEquals((long) numOfDocuments, page1.getTotalNumItems(), null, f));
137             }
138 
139             f = createResult(FAILURE, "hasMoreItems of the first test page must be TRUE!");
140             addResult(assertEquals(true, page1.getHasMoreItems(), null, f));
141 
142             // check second page
143             count = 0;
144             ItemIterable<CmisObject> page2 = testFolder.getChildren(orderContext).skipTo(pageSize - 1)
145                     .getPage(pageSize);
146             for (CmisObject child : page2) {
147                 count++;
148 
149                 if (count == 1 && lastObject != null) {
150                     f = createResult(FAILURE,
151                             "Last object of the first page doesn't match the first object of the second page.");
152                     addResult(assertEquals(lastObject.getId(), child.getId(), null, f));
153                 }
154             }
155 
156             f = createResult(FAILURE, "Returned number of children doesn't match the page size!");
157             addResult(assertEquals(pageSize, count, null, f));
158 
159             if (page2.getTotalNumItems() == -1) {
160                 addResult(createResult(WARNING, "Repository did not return numItems for the second test page."));
161             } else {
162                 f = createResult(FAILURE, "Returned numItems doesn't match the number of documents!");
163                 addResult(assertEquals((long) numOfDocuments, page2.getTotalNumItems(), null, f));
164             }
165 
166             f = createResult(FAILURE, "hasMoreItems of the second test page must be TRUE!");
167             addResult(assertEquals(true, page2.getHasMoreItems(), null, f));
168 
169             // check third page
170             count = 0;
171             ItemIterable<CmisObject> page3 = testFolder.getChildren(orderContext).skipTo(numOfDocuments - 5)
172                     .getPage(10);
173             for (@SuppressWarnings("unused")
174             CmisObject child : page3) {
175                 count++;
176             }
177 
178             f = createResult(FAILURE,
179                     "Returned number of children should be 5 because page startetd at (numOfDocuments - 5).");
180             addResult(assertEquals(5, count, null, f));
181 
182             if (page3.getTotalNumItems() == -1) {
183                 addResult(createResult(WARNING, "Repository did not return numItems for the third test page."));
184             } else {
185                 f = createResult(FAILURE, "Returned numItems doesn't match the number of documents!");
186                 addResult(assertEquals((long) numOfDocuments, page3.getTotalNumItems(), null, f));
187             }
188 
189             f = createResult(FAILURE, "hasMoreItems of the third test page must be FALSE!");
190             addResult(assertEquals(false, page3.getHasMoreItems(), null, f));
191 
192             // check non-existing page
193             count = 0;
194             ItemIterable<CmisObject> pageNotExisting = testFolder.getChildren(orderContext).skipTo(100000)
195                     .getPage(pageSize);
196             for (@SuppressWarnings("unused")
197             CmisObject child : pageNotExisting) {
198                 count++;
199             }
200 
201             f = createResult(FAILURE, "The page size of a non-existing page must be 0!");
202             addResult(assertEquals(0, count, null, f));
203 
204             if (pageNotExisting.getTotalNumItems() == -1) {
205                 addResult(createResult(WARNING, "Repository did not return numItems for a non-existing page."));
206             } else {
207                 f = createResult(FAILURE, "Returned numItems doesn't match the number of documents!");
208                 addResult(assertEquals((long) numOfDocuments, pageNotExisting.getTotalNumItems(), null, f));
209             }
210 
211             f = createResult(FAILURE, "hasMoreItems of a non-existing page must be FALSE!");
212             addResult(assertEquals(false, pageNotExisting.getHasMoreItems(), null, f));
213 
214             // check content
215             for (Document document : documents.values()) {
216                 ContentStream contentStream = document.getContentStream();
217                 if (contentStream == null || contentStream.getStream() == null) {
218                     addResult(createResult(FAILURE, "Document has no content! Id: " + document.getId()));
219                     continue;
220                 }
221 
222                 try {
223                     // first stream from document
224                     String contentStr = getStringFromContentStream(contentStream);
225 
226                     f = createResult(FAILURE, "Unexpected document content! Id: " + document.getId());
227                     addResult(assertEquals(CONTENT, contentStr, null, f));
228 
229                     // second stream from session
230                     String contentStr2 = getStringFromContentStream(session.getContentStream(document));
231 
232                     f = createResult(FAILURE, "Unexpected document content! Id: " + document.getId());
233                     addResult(assertEquals(CONTENT, contentStr2, null, f));
234 
235                     // third stream by path
236                     List<String> paths = document.getPaths();
237 
238                     f = createResult(FAILURE,
239                             "The document must have at least one path because it was created in a folder! Id: "
240                                     + document.getId());
241                     addResult(assertIsTrue(paths != null && paths.size() > 0, null, f));
242 
243                     String contentStr3 = getStringFromContentStream(session.getContentStreamByPath(paths.get(0)));
244 
245                     f = createResult(FAILURE, "Unexpected document content! Id: " + document.getId());
246                     addResult(assertEquals(CONTENT, contentStr3, null, f));
247                 } catch (IOException ioe) {
248                     addResult(createResult(FAILURE, "Could not read content of document! Id: " + document.getId(), ioe,
249                             false));
250                 } finally {
251                     IOUtils.closeQuietly(contentStream);
252                 }
253             }
254 
255             // delete all documents
256             for (Document document : documents.values()) {
257                 document.delete(true);
258 
259                 f = createResult(FAILURE,
260                         "Document should not exist anymore but it is still there! Id: " + document.getId());
261                 addResult(assertIsFalse(exists(document), null, f));
262             }
263         } finally {
264             // delete the test folder
265             deleteTestFolder();
266         }
267 
268         addResult(createInfoResult("Tested the creation and deletion of " + numOfDocuments + " documents."));
269     }
270 }