This project has retired. For details please refer to its Attic page.
CreateAndDeleteDocumentTest 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  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.chemistry.opencmis.client.api.CmisObject;
30  import org.apache.chemistry.opencmis.client.api.Document;
31  import org.apache.chemistry.opencmis.client.api.Folder;
32  import org.apache.chemistry.opencmis.client.api.ItemIterable;
33  import org.apache.chemistry.opencmis.client.api.Session;
34  import org.apache.chemistry.opencmis.commons.data.ContentStream;
35  import org.apache.chemistry.opencmis.tck.CmisTestResult;
36  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
37  
38  /**
39   * Simple document test.
40   */
41  public class CreateAndDeleteDocumentTest extends AbstractSessionTest {
42  
43      private static final String CONTENT = "TCK test content.";
44  
45      @Override
46      public void init(Map<String, String> parameters) {
47          super.init(parameters);
48          setName("Create and Delete Document Test");
49          setDescription("Creates a few documents, checks the newly created documents and their parent and finally deletes the created documents.");
50      }
51  
52      @Override
53      public void run(Session session) {
54          CmisTestResult f;
55  
56          int numOfDocuments = 20;
57  
58          // create a test folder
59          Folder testFolder = createTestFolder(session);
60  
61          try {
62              Map<String, Document> documents = new HashMap<String, Document>();
63  
64              // create documents
65              for (int i = 0; i < numOfDocuments; i++) {
66                  Document newDocument = createDocument(session, testFolder, "doc" + i, CONTENT);
67                  documents.put(newDocument.getId(), newDocument);
68              }
69  
70              // simple children test
71              addResult(checkChildren(session, testFolder, "Test folder children check"));
72  
73              // check if all documents are there
74              ItemIterable<CmisObject> children = testFolder.getChildren(SELECT_ALL_NO_CACHE_OC);
75              List<String> childrenIds = new ArrayList<String>();
76              for (CmisObject child : children) {
77                  if (child != null) {
78                      childrenIds.add(child.getId());
79                      Document document = documents.get(child.getId());
80  
81                      f = createResult(FAILURE, "Document and test folder child don't match! Id: " + child.getId());
82                      addResult(assertShallowEquals(document, child, null, f));
83                  }
84              }
85  
86              f = createResult(FAILURE, "Number of created folders does not match the number of existing folders!");
87              addResult(assertEquals(numOfDocuments, childrenIds.size(), null, f));
88  
89              for (Document document : documents.values()) {
90                  if (!childrenIds.contains(document.getId())) {
91                      addResult(createResult(FAILURE, "Created document not found in test folder children! Id: "
92                              + document.getId()));
93                  }
94              }
95  
96              // check paging
97              int pageSize = 5;
98              CmisObject lastObject = null;
99  
100             int count = 0;
101             ItemIterable<CmisObject> page1 = testFolder.getChildren(SELECT_ALL_NO_CACHE_OC_ORDER_BY_NAME).getPage(
102                     pageSize);
103             for (CmisObject child : page1) {
104                 count++;
105                 lastObject = child;
106             }
107 
108             f = createResult(FAILURE, "Returned number of children doesn't match the page size!");
109             addResult(assertEquals(pageSize, count, null, f));
110 
111             count = 0;
112             ItemIterable<CmisObject> page2 = testFolder.getChildren(SELECT_ALL_NO_CACHE_OC_ORDER_BY_NAME)
113                     .skipTo(pageSize - 1).getPage(pageSize);
114             for (CmisObject child : page2) {
115                 count++;
116 
117                 if (count == 1) {
118                     f = createResult(FAILURE,
119                             "Last object of the first page doesn't match the first object of the second page.");
120                     addResult(assertEquals(lastObject.getId(), child.getId(), null, f));
121                 }
122             }
123 
124             f = createResult(FAILURE, "Returned number of children doesn't match the page size!");
125             addResult(assertEquals(pageSize, count, null, f));
126 
127             // check content
128             for (Document document : documents.values()) {
129                 ContentStream contentStream = document.getContentStream();
130                 if (contentStream == null || contentStream.getStream() == null) {
131                     addResult(createResult(FAILURE, "Document has no content! Id: " + document.getId()));
132                     continue;
133                 } else {
134                     try {
135                         contentStream.getStream().close();
136                     } catch (IOException e) {
137                     }
138                 }
139 
140                 // TODO: content checks
141             }
142 
143             // delete all documents
144             for (Document document : documents.values()) {
145                 document.delete(true);
146 
147                 f = createResult(FAILURE,
148                         "Document should not exist anymore but it is still there! Id: " + document.getId());
149                 addResult(assertIsFalse(exists(document), null, f));
150             }
151         } finally {
152             // delete the test folder
153             deleteTestFolder();
154         }
155 
156         addResult(createInfoResult("Tested the creation and deletion of " + numOfDocuments + " documents."));
157     }
158 }