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