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.UNEXPECTED_EXCEPTION;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.concurrent.Future;
29  
30  import org.apache.chemistry.opencmis.client.api.AsyncSession;
31  import org.apache.chemistry.opencmis.client.api.CmisObject;
32  import org.apache.chemistry.opencmis.client.api.Folder;
33  import org.apache.chemistry.opencmis.client.api.ItemIterable;
34  import org.apache.chemistry.opencmis.client.api.ObjectId;
35  import org.apache.chemistry.opencmis.client.api.Session;
36  import org.apache.chemistry.opencmis.client.runtime.async.AbstractExecutorServiceAsyncSession;
37  import org.apache.chemistry.opencmis.client.runtime.async.AsyncSessionFactoryImpl;
38  import org.apache.chemistry.opencmis.commons.PropertyIds;
39  import org.apache.chemistry.opencmis.commons.enums.UnfileObject;
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 AsyncCreateAndDeleteFolderTest extends AbstractSessionTest {
47  
48      @Override
49      public void init(Map<String, String> parameters) {
50          super.init(parameters);
51          setName("Asynchronous Create and Delete Folder Test");
52          setDescription("Creates folders in parallel and deletes the created folders in parallel.");
53      }
54  
55      @Override
56      public void run(Session session) {
57          CmisTestResult f;
58  
59          int numOfFolders = 100;
60  
61          // create an async session
62          AsyncSession asyncSession = AsyncSessionFactoryImpl.newInstance().createAsyncSession(session, 10);
63  
64          // create a test folder
65          Folder testFolder = createTestFolder(session);
66  
67          try {
68              // create folders
69              List<Future<ObjectId>> folderFutures = new ArrayList<Future<ObjectId>>();
70              for (int i = 0; i < numOfFolders; i++) {
71                  String name = "asyncfolder" + i;
72  
73                  Map<String, Object> properties = new HashMap<String, Object>();
74                  properties.put(PropertyIds.NAME, name);
75                  properties.put(PropertyIds.OBJECT_TYPE_ID, getFolderTestTypeId());
76  
77                  Future<ObjectId> newFolder = asyncSession.createFolder(properties, testFolder);
78  
79                  folderFutures.add(newFolder);
80              }
81  
82              // wait for all folders being created
83              List<ObjectId> folderIds = new ArrayList<ObjectId>();
84              try {
85                  for (Future<ObjectId> folderFuture : folderFutures) {
86                      ObjectId id = folderFuture.get();
87                      folderIds.add(id);
88                  }
89              } catch (Exception e) {
90                  addResult(createResult(UNEXPECTED_EXCEPTION,
91                          "Folder could not been created! Exception: " + e.getMessage(), e, true));
92              }
93  
94              // check children of test folder
95              int count = countChildren(testFolder);
96              f = createResult(FAILURE, "Test folder should have " + numOfFolders + " children but has " + count + "!");
97              addResult(assertEquals(count, numOfFolders, null, f));
98  
99              // get folders
100             Map<String, Future<CmisObject>> getObjectFutures = new HashMap<String, Future<CmisObject>>();
101 
102             for (ObjectId folderId : folderIds) {
103                 Future<CmisObject> getObjectFuture = asyncSession.getObject(folderId, SELECT_ALL_NO_CACHE_OC);
104                 getObjectFutures.put(folderId.getId(), getObjectFuture);
105             }
106 
107             // wait for all folders being fetched
108             List<String> paths = new ArrayList<String>();
109             try {
110                 for (Map.Entry<String, Future<CmisObject>> getObjectFuture : getObjectFutures.entrySet()) {
111                     CmisObject object = getObjectFuture.getValue().get();
112 
113                     f = createResult(FAILURE, "Fetching folder failed!");
114                     addResult(assertIsTrue(object instanceof Folder, null, f));
115 
116                     if (object != null) {
117                         f = createResult(FAILURE, "Fetched wrong folder!");
118                         addResult(assertEquals(getObjectFuture.getKey(), object.getId(), null, f));
119 
120                         paths.add(((Folder) object).getPath());
121                     }
122                 }
123             } catch (Exception e) {
124                 addResult(createResult(UNEXPECTED_EXCEPTION,
125                         "Folders could not been fetched! Exception: " + e.getMessage(), e, true));
126             }
127 
128             // get folders by path
129             Map<String, Future<CmisObject>> getObjectByPathFutures = new HashMap<String, Future<CmisObject>>();
130 
131             for (String path : paths) {
132                 Future<CmisObject> getObjectByPathFuture = asyncSession.getObjectByPath(path, SELECT_ALL_NO_CACHE_OC);
133                 getObjectByPathFutures.put(path, getObjectByPathFuture);
134             }
135 
136             // wait for all folders being fetched
137             try {
138                 for (Map.Entry<String, Future<CmisObject>> getObjectByPathFuture : getObjectByPathFutures.entrySet()) {
139                     CmisObject object = getObjectByPathFuture.getValue().get();
140 
141                     f = createResult(FAILURE, "Fetching folder failed!");
142                     addResult(assertIsTrue(object instanceof Folder, null, f));
143 
144                     if (object != null) {
145                         f = createResult(FAILURE, "Fetched wrong folder!");
146                         addResult(assertEquals(getObjectByPathFuture.getKey(), ((Folder) object).getPath(), null, f));
147                     }
148                 }
149             } catch (Exception e) {
150                 addResult(createResult(UNEXPECTED_EXCEPTION,
151                         "Folders could not been fetched! Exception: " + e.getMessage(), e, true));
152             }
153 
154             // delete folders
155             List<Future<?>> delFutures = new ArrayList<Future<?>>();
156             for (ObjectId folderId : folderIds) {
157                 Future<?> delFuture = asyncSession.deleteTree(folderId, true, UnfileObject.DELETE, true);
158                 delFutures.add(delFuture);
159             }
160 
161             // wait for all folders being deleted
162             try {
163                 for (Future<?> delFuture : delFutures) {
164                     delFuture.get();
165                 }
166             } catch (Exception e) {
167                 addResult(createResult(UNEXPECTED_EXCEPTION,
168                         "Folder could not been deleted! Exception: " + e.getMessage(), e, true));
169             }
170 
171             // check children of test folder
172             count = countChildren(testFolder);
173             f = createResult(FAILURE, "Test folder should be empty but has " + count + " children!");
174             addResult(assertEquals(count, 0, null, f));
175         } finally {
176             // delete the test folder
177             deleteTestFolder();
178 
179             if (asyncSession instanceof AbstractExecutorServiceAsyncSession<?>) {
180                 ((AbstractExecutorServiceAsyncSession<?>) asyncSession).shutdown();
181             }
182         }
183 
184         addResult(createInfoResult("Tested the parallel creation and deletion of " + numOfFolders + " folders."));
185     }
186 
187     private int countChildren(Folder folder) {
188         int count = 0;
189         ItemIterable<CmisObject> children = folder.getChildren(SELECT_ALL_NO_CACHE_OC);
190         for (CmisObject child : children) {
191             if (child instanceof Folder) {
192                 count++;
193             }
194         }
195 
196         return count;
197     }
198 }