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  
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.chemistry.opencmis.client.api.CmisObject;
29  import org.apache.chemistry.opencmis.client.api.Folder;
30  import org.apache.chemistry.opencmis.client.api.ItemIterable;
31  import org.apache.chemistry.opencmis.client.api.Session;
32  import org.apache.chemistry.opencmis.tck.CmisTestResult;
33  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
34  
35  /**
36   * Simple folder test.
37   */
38  public class CreateAndDeleteFolderTest extends AbstractSessionTest {
39  
40      @Override
41      public void init(Map<String, String> parameters) {
42          super.init(parameters);
43          setName("Create and Delete Folder Test");
44          setDescription(
45                  "Creates a few folders, checks the newly created folders and their parent and finally deletes the created folders.");
46      }
47  
48      @Override
49      public void run(Session session) {
50          // create a test folder
51          Folder testFolder = createTestFolder(session);
52  
53          try {
54              testFolderList(session, testFolder, 20);
55              testDeepHierarchy(session, testFolder, 20);
56          } finally {
57              // delete the test folder
58              deleteTestFolder();
59          }
60      }
61  
62      private void testFolderList(Session session, Folder testFolder, int numOfFolders) {
63          CmisTestResult f;
64  
65          Map<String, Folder> folders = new HashMap<String, Folder>();
66  
67          // create folders
68          for (int i = 0; i < numOfFolders; i++) {
69              Folder newFolder = createFolder(session, testFolder, "folder" + i);
70              folders.put(newFolder.getId(), newFolder);
71          }
72  
73          // simple children test
74          addResult(checkChildren(session, testFolder, "Test folder children check"));
75  
76          // check if all folders are there
77          ItemIterable<CmisObject> children = testFolder.getChildren(SELECT_ALL_NO_CACHE_OC);
78          List<String> childrenIds = new ArrayList<String>();
79          for (CmisObject child : children) {
80              if (child != null) {
81                  childrenIds.add(child.getId());
82                  Folder folder = folders.get(child.getId());
83  
84                  f = createResult(FAILURE, "Folder and test folder child don't match! Id: " + child.getId());
85                  addResult(assertShallowEquals(folder, child, null, f));
86              }
87          }
88  
89          f = createResult(FAILURE, "Number of created folders does not match the number of existing folders!");
90          addResult(assertEquals(numOfFolders, childrenIds.size(), null, f));
91  
92          for (Folder folder : folders.values()) {
93              if (!childrenIds.contains(folder.getId())) {
94                  addResult(createResult(FAILURE,
95                          "Created folder not found in test folder children! Id: " + folder.getId()));
96              }
97          }
98  
99          // delete all folders
100         for (Folder folder : folders.values()) {
101             // empty folders should be deleteable like this
102             folder.delete(true);
103 
104             f = createResult(FAILURE, "Folder should not exist anymore but it is still there! Id: " + folder.getId());
105             addResult(assertIsFalse(exists(folder), null, f));
106         }
107 
108         addResult(createInfoResult("Tested the creation and deletion of " + numOfFolders + " folders."));
109     }
110 
111     private void testDeepHierarchy(Session session, Folder testFolder, int depth) {
112         String folderName = "depth";
113 
114         StringBuilder path = new StringBuilder(testFolder.getPath());
115         for (int i = 0; i < depth; i++) {
116             path.append('/');
117             path.append(folderName + i);
118         }
119 
120         session.createPath(testFolder, path.toString(), getFolderTestTypeId());
121 
122         for (int i = 0; i < depth; i++) {
123             CmisObject folderObj = session.getObjectByPath(path.toString(), SELECT_ALL_NO_CACHE_OC);
124 
125             if (folderObj instanceof Folder) {
126                 session.deleteByPath(((Folder) folderObj).getPath());
127             } else {
128                 addResult(createResult(FAILURE, "Just created folder is not a folder! Id: " + folderObj.getId()));
129             }
130 
131             path.delete(path.lastIndexOf("/"), path.length());
132         }
133 
134         addResult(createInfoResult("Tested the creation of a folder hierarchy with a depth of " + (depth + 1)
135                 + " below the test folder."));
136     }
137 }