This project has retired. For details please refer to its Attic page.
MultiFilingTest xref

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.inmemory;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.chemistry.opencmis.commons.PropertyIds;
32  import org.apache.chemistry.opencmis.commons.data.ObjectData;
33  import org.apache.chemistry.opencmis.commons.data.ObjectParentData;
34  import org.apache.chemistry.opencmis.commons.data.Properties;
35  import org.apache.chemistry.opencmis.commons.data.PropertyData;
36  import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
37  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
38  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
39  import org.apache.chemistry.opencmis.commons.exceptions.CmisNameConstraintViolationException;
40  import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
41  import org.apache.chemistry.opencmis.commons.server.CallContext;
42  import org.apache.chemistry.opencmis.commons.spi.Holder;
43  import org.apache.chemistry.opencmis.inmemory.types.InMemoryFolderTypeDefinition;
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  import org.junit.After;
47  import org.junit.Before;
48  import org.junit.Test;
49  
50  public class MultiFilingTest extends AbstractServiceTest {
51  
52      private static final Log LOG = LogFactory.getLog(MultiFilingTest.class);
53      private static final String DOCUMENT_TYPE_ID = UnitTestTypeSystemCreator.COMPLEX_TYPE;
54      private static final String FOLDER_TYPE_ID = InMemoryFolderTypeDefinition.getRootFolderType().getId();
55      private static final String UNFILED_DOC_NAME = "Unfiled document";
56      private static final String RENAMED_DOC_NAME = "My Renamed Document";
57  
58      private String fId1;
59      private String fId2;
60      private String fId11;
61  
62      @Override
63      @Before
64      public void setUp() {
65          super.setUp();
66      }
67  
68      @Override
69      @After
70      public void tearDown() {
71          super.tearDown();
72      }
73  
74      @Test
75      public void testCreateUnfiledDocument() {
76          LOG.debug("Begin testCreatUnfiledDocument()");
77          String docId = createUnfiledDocument();
78          String docId2 = getDocument(docId);
79          assertEquals(docId, docId2);
80  
81          // get object parents, must be empty
82          List<ObjectParentData> res = fNavSvc.getObjectParents(fRepositoryId, docId, "*", false,
83                  IncludeRelationships.NONE, null, true, null);
84  
85          assertNotNull(res);
86          assertEquals(res.size(), 0);
87          
88          // test with a different user than Admin:
89          switchCallContext("Alice");
90          docId = createDocument(UNFILED_DOC_NAME + "_2", null, DOCUMENT_TYPE_ID, true);
91          docId2 = getDocument(docId);
92          assertEquals(docId, docId2);
93  
94          LOG.debug("End testCreatUnfiledDocument()");
95      }
96  
97      @Test
98      public void testMakeFiledDocumentUnfiled() {
99          LOG.debug("Begin testMakeFiledDocumentUnfiled()");
100 
101         String docId = createDocument("Filed document", fRootFolderId, DOCUMENT_TYPE_ID, true);
102 
103         fMultiSvc.removeObjectFromFolder(fRepositoryId, docId, fRootFolderId, null);
104         List<ObjectParentData> parents = fNavSvc.getObjectParents(fRepositoryId, docId, "*", false,
105                 IncludeRelationships.NONE, null, true, null);
106         assertEquals(0, parents.size());
107 
108         LOG.debug("End testMakeFiledDocumentUnfiled()");
109     }
110 
111     @Test
112     public void testAddDocumentToFolder() {
113         LOG.debug("Begin testAddDocumentToFolder()");
114         String docId = createUnfiledDocument();
115         addDocumentToFolder(docId);
116         LOG.debug("End testAddDocumentToFolder()");
117     }
118 
119     @Test
120     public void testRemoveDocumentFromFolder() {
121         LOG.debug("Begin testRemoveDocumentFromFolder()");
122 
123         String docId = createUnfiledDocument();
124         removeDocumentFromFolder(docId);
125         LOG.debug("End testRemoveDocumentFromFolder()");
126     }
127 
128     @Test
129     public void testMoveMultiFiledDocument() {
130         LOG.debug("begin testMoveMultiFiledDocument()");
131         String docId = createUnfiledDocument();
132         prepareMultiFiledDocument(docId);
133         String newFolderId = createFolder("folder2.1", fId2, FOLDER_TYPE_ID);
134 
135         Holder<String> idHolder = new Holder<String>(docId);
136         fObjSvc.moveObject(fRepositoryId, idHolder, newFolderId, fId11, null);
137         List<ObjectParentData> parents = fNavSvc.getObjectParents(fRepositoryId, docId, "*", false,
138                 IncludeRelationships.NONE, null, true, null);
139         assertEquals(3, parents.size());
140         boolean foundNewParent = false;
141         boolean foundOldParent = false;
142         for (ObjectParentData parentData : parents) {
143             if (parentData.getObject().getId().equals(newFolderId)) {
144                 foundNewParent = true;
145             }
146             if (parentData.getObject().getId().equals(fId11)) {
147                 foundOldParent = true;
148             }
149         }
150         assertTrue("After move new target should be a parent", foundNewParent);
151         assertFalse("After move old source should no longer be a parent", foundOldParent);
152         LOG.debug("End testMoveMultiFiledDocument()");
153     }
154 
155     @Test
156     public void testRenameMultiFiledDocument() {
157         LOG.debug("begin testRenameMultiFiledDocument()");
158         String docId = createUnfiledDocument();
159         prepareMultiFiledDocument(docId);
160         renameDocumentAndCheckResult(docId);
161         LOG.debug("End testRenameMultiFiledDocument()");
162     }
163 
164     @Test
165     public void testRenameMultiFiledDocumentWithNameConflict() {
166         LOG.debug("begin testRenameMultiFiledDocument()");
167         String docId = createUnfiledDocument();
168         prepareMultiFiledDocument(docId);
169         // create a document with the new name in one of the folders
170         createDocument(RENAMED_DOC_NAME, fId11, DOCUMENT_TYPE_ID, true);
171         // try to rename which should fail now
172         try {
173             renameDocumentAndCheckResult(docId);
174             fail("A rename to an existing name in one of the filed folders should fail");
175         } catch (Exception e) {
176             assertTrue(e instanceof CmisNameConstraintViolationException);
177         }
178         LOG.debug("End testRenameMultiFiledDocument()");
179     }
180 
181     @Test
182     public void testAddVersionedDocumentToFolder() {
183         LOG.debug("Begin testAddVersionedDocumentToFolder()");
184         String docId = createVersionedDocument();
185         addDocumentToFolder(docId);
186         LOG.debug("End testAddVersionedDocumentToFolder()");
187     }
188 
189     @Test
190     public void testRemoveVersionedDocumentFromFolder() {
191         LOG.debug("Begin testRemoveVersionedDocumentFromFolder()");
192 
193         String docId = createVersionedDocument();
194         removeDocumentFromFolder(docId);
195         LOG.debug("End testRemoveVersionedDocumentFromFolder()");
196     }
197 
198     private void createFolders() {
199         fId1 = createFolder("folder1", fRootFolderId, FOLDER_TYPE_ID);
200         fId2 = createFolder("folder2", fRootFolderId, FOLDER_TYPE_ID);
201         fId11 = createFolder("folder1.1", fId1, FOLDER_TYPE_ID);
202     }
203 
204     private void addDocumentToFolder(String docId) {
205 
206         List<String> folderIds = prepareMultiFiledDocument(docId);
207 
208         // get object parents, must contain all folders
209         List<ObjectParentData> res = fNavSvc.getObjectParents(fRepositoryId, docId, "*", false,
210                 IncludeRelationships.NONE, null, true, null);
211         assertEquals(3, res.size());
212         for (ObjectParentData opd : res) {
213             assertTrue(folderIds.contains(opd.getObject().getId()));
214             assertEquals(BaseTypeId.CMIS_FOLDER, opd.getObject().getBaseTypeId());
215             assertEquals(UNFILED_DOC_NAME, opd.getRelativePathSegment());
216         }
217 
218         // try version specific filing, should fail
219         try {
220             fMultiSvc.addObjectToFolder(fRepositoryId, docId, fId1, false, null);
221             fail("Adding not all versions to a folder should fail.");
222         } catch (Exception e) {
223             assertTrue(e instanceof CmisNotSupportedException);
224         }
225     }
226 
227     private void removeDocumentFromFolder(String docId) {
228         prepareMultiFiledDocument(docId);
229 
230         fMultiSvc.removeObjectFromFolder(fRepositoryId, docId, fId1, null);
231         List<ObjectParentData> parents = fNavSvc.getObjectParents(fRepositoryId, docId, "*", false,
232                 IncludeRelationships.NONE, null, true, null);
233         assertEquals(2, parents.size());
234         for (ObjectParentData opd : parents) {
235             assertFalse(fId1.equals(opd.getObject().getId()));
236         }
237 
238         fMultiSvc.removeObjectFromFolder(fRepositoryId, docId, fId2, null);
239         parents = fNavSvc.getObjectParents(fRepositoryId, docId, "*", false, IncludeRelationships.NONE, null, true,
240                 null);
241         assertEquals(1, parents.size());
242         for (ObjectParentData opd : parents) {
243             assertFalse(fId1.equals(opd.getObject().getId()));
244         }
245 
246         fMultiSvc.removeObjectFromFolder(fRepositoryId, docId, fId11, null);
247         parents = fNavSvc.getObjectParents(fRepositoryId, docId, "*", false, IncludeRelationships.NONE, null, true,
248                 null);
249         assertEquals(0, parents.size());
250     }
251 
252     private String createUnfiledDocument() {
253         return createDocument(UNFILED_DOC_NAME, null, DOCUMENT_TYPE_ID, true);
254     }
255 
256     private List<String> prepareMultiFiledDocument(String docId) {
257         createFolders();
258 
259         // add the document to three folders
260         fMultiSvc.addObjectToFolder(fRepositoryId, docId, fId1, true, null);
261         fMultiSvc.addObjectToFolder(fRepositoryId, docId, fId2, true, null);
262         fMultiSvc.addObjectToFolder(fRepositoryId, docId, fId11, true, null);
263 
264         List<String> folderIds = new ArrayList<String>();
265         folderIds.add(fId1);
266         folderIds.add(fId2);
267         folderIds.add(fId11);
268 
269         return folderIds;
270     }
271 
272     private void renameDocumentAndCheckResult(String docId) {
273         Holder<String> idHolder = new Holder<String>(docId);
274         List<PropertyData<?>> properties = new ArrayList<PropertyData<?>>();
275         properties.add(fFactory.createPropertyIdData(PropertyIds.NAME, RENAMED_DOC_NAME));
276         Properties newProps = fFactory.createPropertiesData(properties);
277         Holder<String> changeTokenHolder = new Holder<String>();
278         fObjSvc.updateProperties(fRepositoryId, idHolder, changeTokenHolder, newProps, null);
279         docId = idHolder.getValue();
280         ObjectData res = fObjSvc.getObject(fRepositoryId, docId, "*", false, IncludeRelationships.NONE, null, false,
281                 false, null);
282         assertNotNull(res);
283         Map<String, PropertyData<?>> propMap = res.getProperties().getProperties();
284         PropertyData<?> pd = propMap.get(PropertyIds.NAME);
285         assertNotNull(pd);
286         assertEquals(RENAMED_DOC_NAME, pd.getFirstValue());
287     }
288 
289     private String createVersionedDocument() {
290 
291         return createDocument(UNFILED_DOC_NAME, null, UnitTestTypeSystemCreator.VERSIONED_TYPE,
292                 VersioningState.MAJOR, true);
293 
294     }
295 
296     private void switchCallContext(String user) {
297         ((DummyCallContext) fTestCallContext).put(CallContext.USERNAME, user);
298     }
299 
300 }