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.SKIPPED;
24  
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.chemistry.opencmis.client.api.CmisObject;
31  import org.apache.chemistry.opencmis.client.api.Document;
32  import org.apache.chemistry.opencmis.client.api.Folder;
33  import org.apache.chemistry.opencmis.client.api.Session;
34  import org.apache.chemistry.opencmis.commons.PropertyIds;
35  import org.apache.chemistry.opencmis.commons.data.BulkUpdateObjectIdAndChangeToken;
36  import org.apache.chemistry.opencmis.commons.enums.BindingType;
37  import org.apache.chemistry.opencmis.commons.enums.CmisVersion;
38  import org.apache.chemistry.opencmis.tck.CmisTestResult;
39  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
40  
41  public class BulkUpdatePropertiesTest extends AbstractSessionTest {
42  
43      private static final String CONTENT = "Bluk update test content.";
44      private static final String NEW_NAME = "bunewname.txt";
45  
46      @Override
47      public void init(Map<String, String> parameters) {
48          super.init(parameters);
49          setName("Bulk Update Properties Test");
50          setDescription("Creates a few folders and documents, renames all documents at once, and deletes all created objects.");
51      }
52  
53      @Override
54      public void run(Session session) {
55          if (session.getRepositoryInfo().getCmisVersion() == CmisVersion.CMIS_1_0) {
56              addResult(createResult(SKIPPED, "Bulk Update Properties is not supported by CMIS 1.0. Test skipped!"));
57              return;
58          }
59  
60          CmisTestResult failure = null;
61          int numOfObjects = 20;
62  
63          // create a test folder
64          Folder testFolder = createTestFolder(session);
65  
66          try {
67              Map<String, Folder> folders = new HashMap<String, Folder>();
68              Map<String, Document> documents = new HashMap<String, Document>();
69  
70              // create folders and documents
71              for (int i = 0; i < numOfObjects; i++) {
72                  Folder newFolder = createFolder(session, testFolder, "bufolder" + i);
73                  folders.put(newFolder.getId(), newFolder);
74                  Document newDocument = createDocument(session, newFolder, "budoc" + i + ".txt", CONTENT);
75                  documents.put(newDocument.getId(), newDocument);
76              }
77  
78              // update cmis:name of all the documents
79              List<CmisObject> objects = new ArrayList<CmisObject>(documents.values());
80              Map<String, Object> properties = new HashMap<String, Object>();
81              properties.put(PropertyIds.NAME, NEW_NAME);
82  
83              List<BulkUpdateObjectIdAndChangeToken> updatedIds = session.bulkUpdateProperties(objects, properties, null,
84                      null);
85  
86              // check the result
87              if (getBinding() == BindingType.WEBSERVICES) {
88                  // TODO: review after TC clarification
89                  addResult(createResult(INFO, "The Web Services binding does not return the updated ids."
90                          + " This issue has to be clarified by the CMIS TC and the test to adopted later."));
91              } else {
92                  if (updatedIds == null || updatedIds.isEmpty()) {
93                      addResult(createResult(FAILURE, "Bulk Update Properties did not update any documents!"));
94                  } else {
95                      failure = createResult(FAILURE, "Bulk Update Properties did not update all test documents!");
96                      addResult(assertEquals(documents.size(), updatedIds.size(), null, failure));
97                  }
98              }
99  
100             // check all documents
101             for (Folder folder : folders.values()) {
102                 List<CmisObject> children = new ArrayList<CmisObject>();
103                 for (CmisObject child : folder.getChildren(SELECT_ALL_NO_CACHE_OC)) {
104                     children.add(child);
105                 }
106 
107                 if (children.size() != 1) {
108                     addResult(createResult(FAILURE,
109                             "Test folder should have exactly one child, but it has " + children.size() + "!"));
110                 } else {
111                     failure = createResult(FAILURE, "Document does not have the new name! Id: "
112                             + children.get(0).getId());
113                     addResult(assertEquals(NEW_NAME, children.get(0).getName(), null, failure));
114                 }
115             }
116 
117             // delete folders and documents
118             for (Folder folder : folders.values()) {
119                 folder.deleteTree(true, null, true);
120             }
121         } finally {
122             // delete the test folder
123             deleteTestFolder();
124         }
125 
126     }
127 }