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.WARNING;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.chemistry.opencmis.client.api.Document;
28  import org.apache.chemistry.opencmis.client.api.Folder;
29  import org.apache.chemistry.opencmis.client.api.ObjectId;
30  import org.apache.chemistry.opencmis.client.api.Session;
31  import org.apache.chemistry.opencmis.commons.PropertyIds;
32  import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
33  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
34  import org.apache.chemistry.opencmis.commons.enums.Action;
35  import org.apache.chemistry.opencmis.commons.enums.Updatability;
36  import org.apache.chemistry.opencmis.tck.CmisTestResult;
37  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
38  
39  /**
40   * Update smoke test.
41   */
42  public class UpdateSmokeTest extends AbstractSessionTest {
43  
44      private static final String DOC_NAME1 = "updatetest1.txt";
45      private static final String DOC_NAME2 = "updatetest2.txt";
46      private static final String FOLDER_NAME1 = "updatetest1";
47      private static final String FOLDER_NAME2 = "updatetest2";
48  
49      @Override
50      public void init(Map<String, String> parameters) {
51          super.init(parameters);
52          setName("Update Smoke Test");
53          setDescription("Creates a document, updates its name and finally deletes it.");
54      }
55  
56      @Override
57      public void run(Session session) {
58          try {
59              // create test folder
60              Folder testFolder = createTestFolder(session);
61  
62              // document test
63              updateDocument(session, testFolder);
64  
65              // folder test
66              updateFolder(session, testFolder);
67          } finally {
68              // clean up
69              deleteTestFolder();
70          }
71      }
72  
73      private void updateDocument(Session session, Folder testFolder) {
74          CmisTestResult f;
75  
76          // create document
77          Document doc1 = createDocument(session, testFolder, DOC_NAME1, "rename me!");
78          Document workDoc = doc1;
79  
80          f = createResult(FAILURE, "Document name doesn't match the given name!");
81          addResult(assertEquals(DOC_NAME1, doc1.getName(), null, f));
82  
83          // test if check out is required
84          boolean checkedout = false;
85          DocumentTypeDefinition type = (DocumentTypeDefinition) doc1.getType();
86          PropertyDefinition<?> namePropDef = type.getPropertyDefinitions().get(PropertyIds.NAME);
87          if (namePropDef.getUpdatability() == Updatability.WHENCHECKEDOUT
88                  || (!doc1.getAllowableActions().getAllowableActions().contains(Action.CAN_UPDATE_PROPERTIES) && Boolean.TRUE
89                          .equals(type.isVersionable()))) {
90              workDoc = (Document) session.getObject(doc1.checkOut(), SELECT_ALL_NO_CACHE_OC);
91              checkedout = true;
92          }
93  
94          // update
95          Map<String, Object> properties = new HashMap<String, Object>();
96          properties.put(PropertyIds.NAME, DOC_NAME2);
97  
98          ObjectId newId = workDoc.updateProperties(properties, false);
99          Document doc2 = (Document) session.getObject(newId, SELECT_ALL_NO_CACHE_OC);
100 
101         addResult(checkObject(session, doc2, getAllProperties(doc2), "Updated document compliance"));
102 
103         f = createResult(FAILURE, "Document name doesn't match updated value!");
104         addResult(assertEquals(DOC_NAME2, doc2.getName(), null, f));
105 
106         // delete
107         if (!workDoc.getId().equals(doc2.getId())) {
108             deleteObject(doc2);
109         }
110 
111         // cancel a possible check out
112         if (checkedout) {
113             workDoc.cancelCheckOut();
114         }
115 
116         if (!doc1.getId().equals(doc2.getId())) {
117             if (exists(doc1)) {
118                 deleteObject(doc1);
119             }
120         }
121     }
122 
123     private void updateFolder(Session session, Folder testFolder) {
124         CmisTestResult f;
125 
126         Folder folder = createFolder(session, testFolder, FOLDER_NAME1);
127 
128         f = createResult(FAILURE, "Folder name doesn't match the given name!");
129         addResult(assertEquals(FOLDER_NAME1, folder.getName(), null, f));
130 
131         // update
132         Map<String, Object> properties = new HashMap<String, Object>();
133         properties.put(PropertyIds.NAME, FOLDER_NAME2);
134 
135         ObjectId newId = folder.updateProperties(properties, false);
136 
137         f = createResult(WARNING, "Folder id changed after name update! The folder id should never change!");
138         addResult(assertEquals(folder.getId(), newId.getId(), null, f));
139 
140         // get the new folder object and check the new name
141         folder.refresh();
142 
143         f = createResult(FAILURE, "Folder name doesn't match updated value!");
144         addResult(assertEquals(FOLDER_NAME2, folder.getName(), null, f));
145 
146         // update again with the same name
147         folder.rename(FOLDER_NAME2, true);
148         
149         f = createResult(FAILURE, "Folder name doesn't match updated value!");
150         addResult(assertEquals(FOLDER_NAME2, folder.getName(), null, f));
151         
152         deleteObject(folder);
153     }
154 }