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.SKIPPED;
23  
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.Folder;
31  import org.apache.chemistry.opencmis.client.api.Item;
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.enums.CmisVersion;
35  import org.apache.chemistry.opencmis.tck.CmisTestResult;
36  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
37  
38  /**
39   * Simple folder test.
40   */
41  public class CreateAndDeleteItemTest extends AbstractSessionTest {
42  
43      @Override
44      public void init(Map<String, String> parameters) {
45          super.init(parameters);
46          setName("Create and Delete Item Test");
47          setDescription("Creates a few items, checks the newly created items and their parent and finally deletes the created items.");
48      }
49  
50      @Override
51      public void run(Session session) {
52          if (session.getRepositoryInfo().getCmisVersion() == CmisVersion.CMIS_1_0) {
53              addResult(createResult(SKIPPED, "Items are not supported by CMIS 1.0. Test skipped!"));
54              return;
55          }
56  
57          if (hasItems(session)) {
58              CmisTestResult f;
59  
60              int numOfItems = 20;
61  
62              // create a test folder
63              Folder testFolder = createTestFolder(session);
64  
65              try {
66                  Map<String, Item> items = new HashMap<String, Item>();
67  
68                  // create items
69                  for (int i = 0; i < numOfItems; i++) {
70                      Item newItem = createItem(session, testFolder, "item" + i);
71                      items.put(newItem.getId(), newItem);
72                  }
73  
74                  // check if all items are there
75                  ItemIterable<CmisObject> children = testFolder.getChildren(SELECT_ALL_NO_CACHE_OC);
76                  List<String> childrenIds = new ArrayList<String>();
77                  for (CmisObject child : children) {
78                      if (child != null) {
79                          childrenIds.add(child.getId());
80                          Item item = items.get(child.getId());
81  
82                          f = createResult(FAILURE, "Item and test folder child don't match! Id: " + child.getId());
83                          addResult(assertShallowEquals(item, child, null, f));
84                      }
85                  }
86  
87                  f = createResult(FAILURE, "Number of created items does not match the number of existing items!");
88                  addResult(assertEquals(numOfItems, childrenIds.size(), null, f));
89  
90                  for (Item item : items.values()) {
91                      if (!childrenIds.contains(item.getId())) {
92                          addResult(createResult(FAILURE,
93                                  "Created item not found in test folder children! Id: " + item.getId()));
94                      }
95                  }
96  
97                  // delete all item
98                  for (Item item : items.values()) {
99                      item.delete(true);
100 
101                     f = createResult(FAILURE,
102                             "Item should not exist anymore but it is still there! Id: " + item.getId());
103                     addResult(assertIsFalse(exists(item), null, f));
104                 }
105             } finally {
106                 // delete the test folder
107                 deleteTestFolder();
108             }
109 
110             addResult(createInfoResult("Tested the creation and deletion of " + numOfItems + " items."));
111         } else {
112             addResult(createResult(SKIPPED, "Items not supported. Test skipped!"));
113         }
114     }
115 }