This project has retired. For details please refer to its Attic page.
AclServiceTest 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.assertNull;
25  import static org.junit.Assert.assertTrue;
26  
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.Collections;
30  import java.util.List;
31  
32  import org.apache.chemistry.opencmis.commons.data.Ace;
33  import org.apache.chemistry.opencmis.commons.data.Acl;
34  import org.apache.chemistry.opencmis.commons.data.ObjectData;
35  import org.apache.chemistry.opencmis.commons.enums.AclPropagation;
36  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
37  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
38  import org.apache.chemistry.opencmis.commons.impl.jaxb.EnumBasicPermissions;
39  import org.apache.chemistry.opencmis.inmemory.ObjectServiceTest.ObjectTestTypeSystemCreator;
40  import org.apache.chemistry.opencmis.inmemory.storedobj.impl.InMemoryAce;
41  import org.apache.chemistry.opencmis.inmemory.types.InMemoryDocumentTypeDefinition;
42  import org.apache.chemistry.opencmis.inmemory.types.InMemoryFolderTypeDefinition;
43  import org.apache.commons.logging.Log;
44  import org.apache.commons.logging.LogFactory;
45  import org.junit.After;
46  import org.junit.Before;
47  import org.junit.Test;
48  
49  public class AclServiceTest extends AbstractServiceTest {
50  
51      private static final Log LOG = LogFactory.getLog(AclServiceTest.class);
52      private ObjectCreator fCreator;
53      private static final String DOCUMENT_NAME = "DocumentWithAcl";
54      private static final String FOLDER_NAME = "FolderWithAcl";
55      private static final String DOCUMENT_TYPE_ID = InMemoryDocumentTypeDefinition.getRootDocumentType().getId();
56      private static final String FOLDER_TYPE_ID = InMemoryFolderTypeDefinition.getRootFolderType().getId();
57      private static final String USER = "user";
58      private static final String ALICE = "alice";
59      private static final String BOB = "bob";
60      private static final String CHRIS = "chris";
61      private static final String DAN = "dan";        
62      private Acl defaultAcl = null;
63      
64      @Override
65      @Before
66      public void setUp() {
67          super.setTypeCreatorClass(ObjectTestTypeSystemCreator.class.getName());
68          super.setUp();
69          fCreator = new ObjectCreator(fFactory, fObjSvc, fRepositoryId);
70          
71          List<Ace> defaultACEs = new ArrayList<Ace>(1);
72          defaultACEs.add(fFactory.createAccessControlEntry(InMemoryAce.getAnyoneUser(), Collections.singletonList( EnumBasicPermissions.CMIS_ALL.value())));
73          defaultAcl = fFactory.createAccessControlList(defaultACEs); 
74  
75      }
76  
77      @Override
78      @After
79      public void tearDown() {
80          super.tearDown();
81      }
82  
83      @Test
84      public void testCreateDocumentWithAcl() {
85          LOG.info("starting testCreateDocumentWithAcl() ...");
86          Acl removeAces = defaultAcl;
87          Acl acl = createSimpleTestAcl();
88  
89          String id = createDocument(fRootFolderId, acl, removeAces);
90          LOG.debug("created document with id: " + id);
91          
92          // get ACL using AclService
93          Acl aclReturn = fAclSvc.getAcl(fRepositoryId, id, true, null);        
94          checkSimpleTestAcl(acl, aclReturn);
95          
96          // get ACL using ObjectService getObject
97          ObjectData objData = fObjSvc.getObject(fRepositoryId, id, "*", false, IncludeRelationships.NONE, null, false, true, null);
98          checkSimpleTestAcl(acl, aclReturn);
99         
100         // get ACL using ObjectService getObjectByPath
101         objData = fObjSvc.getObjectByPath(fRepositoryId, "/" + DOCUMENT_NAME, "*", false, IncludeRelationships.NONE, null, false,
102                 true, null);
103         assertNotNull(objData);
104         aclReturn = objData.getAcl();
105         checkSimpleTestAcl(acl, aclReturn);
106 
107         LOG.info("... testCreateDocumentWithAcl() finished.");
108     }
109 
110     @Test
111     public void testCreateFolderWithAcl() {
112         LOG.info("starting testCreateFolderWithAcl() ...");
113         Acl removeAces = defaultAcl;
114         Acl acl = createSimpleTestAcl();
115         
116         String id = createFolder(fRootFolderId, acl, removeAces);
117         LOG.debug("created folder with id: " + id);
118         
119         // get ACL using AclService
120         Acl aclReturn = fAclSvc.getAcl(fRepositoryId, id, true, null);        
121         checkSimpleTestAcl(acl, aclReturn);
122 
123         // get ACL using ObjectService getObject
124         ObjectData objData = fObjSvc.getObject(fRepositoryId, id, "*", false, IncludeRelationships.NONE, null, false, true, null);
125         checkSimpleTestAcl(acl, aclReturn);
126        
127         // get ACL using ObjectService getObjectByPath
128         objData = fObjSvc.getObjectByPath(fRepositoryId, "/" + FOLDER_NAME, "*", false, IncludeRelationships.NONE, null, false,
129                 true, null);
130         assertNotNull(objData);
131         aclReturn = objData.getAcl();
132         checkSimpleTestAcl(acl, aclReturn);        
133         LOG.info("... testCreateFolderWithAcl() finished.");
134    }
135         
136     @Test
137     public void testApplyAcl() {
138         LOG.info("starting testApplyAcl() ...");
139         Acl acl = createSimpleTestAcl();
140         
141         String id = createDocument(fRootFolderId, null, null);
142         LOG.debug("created document with id: " + id);
143         
144         // apply absolute ACL using AclService
145         Acl acl1 = fAclSvc.applyAcl(fRepositoryId, id, acl, defaultAcl, AclPropagation.OBJECTONLY, null);        
146         checkSimpleTestAcl(acl, acl1);
147 
148         // get ACL using AclService
149         Acl aclReturn = fAclSvc.getAcl(fRepositoryId, id, true, null);        
150         checkSimpleTestAcl(acl, aclReturn);
151 
152         LOG.info("... testApplyAcl() finished.");
153     }
154     
155     @Test
156     public void testAddRemoveAcl() {
157         
158         LOG.info("starting testAddRemoveAcl() ...");
159         Acl acl = createAdvancedTestAcl();        
160         
161         String id = createDocument(fRootFolderId, acl, defaultAcl);
162         LOG.debug("created document with id: " + id);
163         
164         Acl aclAdd = createAclAdd();        
165         Acl aclRemove = createAclRemove();
166         // apply absolute ACL using AclService
167         Acl aclReturn = fAclSvc.applyAcl(fRepositoryId, id, aclAdd, aclRemove, AclPropagation.OBJECTONLY, null);        
168 
169         checkAclAfterAddRemove(aclReturn);
170                 
171         LOG.info("... testAddRemoveAcl() finished.");
172     }
173     
174     @Test
175     public void testAddRemoveDuplicatedAcl() {
176         final String DOCUMENT_NAME_1 = "DocumentWithAcl-1";
177         final String DOCUMENT_NAME_2 = "DocumentWithAcl-2";
178 
179         LOG.info("starting testAddRemoveDuplicatedAcl() ...");
180         Acl acl = createAdvancedTestAcl();  
181         String id1 = createDocument(DOCUMENT_NAME_1, fRootFolderId, acl, defaultAcl);
182         String id2 = createDocument(DOCUMENT_NAME_2, fRootFolderId, acl, defaultAcl);
183         
184 //        // modify ACL of first doc
185 //        List<Ace> acesRemove = Arrays.asList(new Ace[] { 
186 //                createAce(BOB, EnumBasicPermissions.CMIS_WRITE.value()),
187 //                });
188 //        Acl aclRemove = new AccessControlListImpl(acesRemove);        
189 //        List<Ace> acesAdd = Arrays.asList(new Ace[] { 
190 //                createAce(DAN, EnumBasicPermissions.CMIS_WRITE.value()),
191 //                });
192 //        Acl aclAdd = new AccessControlListImpl(acesAdd);        
193         Acl aclAdd = createAclAdd();        
194         Acl aclRemove = createAclRemove();
195         Acl aclReturn = fAclSvc.applyAcl(fRepositoryId, id1, aclAdd, aclRemove, AclPropagation.OBJECTONLY, null);        
196 
197         checkAclAfterAddRemove(aclReturn);
198   
199         // Ensure that ACL of second doc is unchanged
200         aclReturn = fAclSvc.getAcl(fRepositoryId, id2, true, null);        
201         checkAdvancedTestAcl(acl, aclReturn);
202         LOG.info("... testAddRemoveDuplicatedAcl() finished.");
203     }
204  
205     @Test
206     public void testApplyAclRecursiveSimple () {
207         LOG.info("starting testApplyAclRecursiveSimple() ...");
208         Acl acl = createSimpleTestAcl();
209         String[] ids = createHierarchy(acl, defaultAcl);
210         fAclSvc.applyAcl(fRepositoryId, ids[0], acl, null, AclPropagation.PROPAGATE, null);        
211         checkAclRecursiveSimple(ids, acl);                
212         LOG.info("... testApplyAclRecursiveSimple() finished.");
213     }    
214 
215     @Test
216     public void testApplyAclRecursiveIncremental() {
217         LOG.info("starting testApplyAclRecursiveIncremental() ...");
218         Acl acl = createAdvancedTestAcl();        
219         String[] ids = createHierarchy(acl, defaultAcl);
220                
221         Acl aclRemove = createAclRemove();        
222         Acl aclAdd = createAclAdd();        
223 
224         Acl aclReturn = fAclSvc.applyAcl(fRepositoryId, ids[0], aclAdd, aclRemove, AclPropagation.PROPAGATE, null);        
225         checkAclAfterAddRemove(aclReturn);
226         for (String id : ids) {
227             aclReturn = fAclSvc.getAcl(fRepositoryId, id, true, null);        
228             checkAclAfterAddRemove(aclReturn);
229         }
230         LOG.info("... testApplyAclRecursiveIncremental() finished.");
231     }    
232 
233     @Test
234     public void testRemoveAllAcls() {
235         LOG.info("starting testRemoveAllAcls() ...");
236 
237         Acl acl = createAdvancedTestAcl();                
238         String id = createDocument(fRootFolderId, acl, defaultAcl);
239         LOG.debug("created document with id: " + id);
240         
241         Acl aclReturn = fAclSvc.applyAcl(fRepositoryId, id, null, acl, AclPropagation.OBJECTONLY, null);
242         assertNotNull(aclReturn);
243         assertEquals(1, aclReturn.getAces().size());
244         assertTrue(aclHasPermission(aclReturn, "anyone", EnumBasicPermissions.CMIS_ALL.value()));        
245 
246         LOG.info("... testRemoveAllAcls() finished.");
247     }    
248 
249     private String createDocument(String name, String folderId, Acl addAces, Acl removeAces) {
250         return createDocumentNoCatch(name, folderId, DOCUMENT_TYPE_ID, VersioningState.NONE, false, addAces,
251                 removeAces);        
252     }
253     
254     private String createDocument(String folderId, Acl addAces, Acl removeAces) {
255         return createDocumentNoCatch(DOCUMENT_NAME, folderId, DOCUMENT_TYPE_ID, VersioningState.NONE, false, addAces,
256                 removeAces);        
257     }
258 
259     private String createFolder(String folderId, Acl addAces, Acl removeAces) {
260         return createFolderNoCatch(FOLDER_NAME, folderId, FOLDER_TYPE_ID, addAces, removeAces);        
261     }
262     
263     private String[] createHierarchy(Acl addAces, Acl removeAces) {
264         String result[] = new String [6];
265         String rootFolderId = createFolderNoCatch(FOLDER_NAME, fRootFolderId, FOLDER_TYPE_ID, addAces, removeAces);
266         result[0] = rootFolderId;
267         result[1] = createDocument(DOCUMENT_NAME + "-1", rootFolderId, addAces, removeAces);
268         result[2] = createDocument(DOCUMENT_NAME + "-2", rootFolderId, addAces, removeAces);
269         String subFolderId = createFolderNoCatch(FOLDER_NAME, rootFolderId, FOLDER_TYPE_ID, addAces, removeAces);
270         result[3] = subFolderId;
271         result[4] = createDocument(DOCUMENT_NAME + "-1", subFolderId, addAces, removeAces);
272         result[5] = createDocument(DOCUMENT_NAME + "-2", subFolderId, addAces, removeAces);
273         return result;
274     }
275     
276     private void checkAclRecursiveSimple(String[] ids, Acl acl) {
277         // get ACL using ObjectService getObject
278         for (String id : ids) {
279             ObjectData objData = fObjSvc.getObject(fRepositoryId, id, "*", false, IncludeRelationships.NONE, null, false, true, null);
280             checkSimpleTestAcl(acl, objData.getAcl());
281         }
282     }
283 
284     private Acl createSimpleTestAcl() {
285         List<Ace> aces = Arrays.asList(new Ace[] { createAce(USER, EnumBasicPermissions.CMIS_READ.value()) });
286         return fFactory.createAccessControlList(aces);        
287     }
288     
289     private void checkSimpleTestAcl(Acl acl, Acl aclReturn) {
290         assertNotNull(aclReturn);
291         assertEquals(acl.getAces().size(), aclReturn.getAces().size());
292         assertTrue(aclHasPermission(aclReturn, USER, EnumBasicPermissions.CMIS_READ.value()));        
293     }
294     
295     private Acl createAdvancedTestAcl() {
296         List<Ace> aces = Arrays.asList(new Ace[] { 
297                 createAce(ALICE, EnumBasicPermissions.CMIS_READ.value()),
298                 createAce(BOB, EnumBasicPermissions.CMIS_WRITE.value()),
299                 createAce(CHRIS, EnumBasicPermissions.CMIS_ALL.value()),
300                 });
301         return fFactory.createAccessControlList(aces);        
302     }
303     
304     private Acl createAclAdd() {
305         List<Ace> acesAdd = Arrays.asList(new Ace[] { 
306                 createAce(DAN, EnumBasicPermissions.CMIS_WRITE.value()),
307                 });
308        return fFactory.createAccessControlList(acesAdd);                
309     }
310     
311     private Acl createAclRemove() {
312         List<Ace> acesRemove = Arrays.asList(new Ace[] { 
313                 createAce(BOB, EnumBasicPermissions.CMIS_WRITE.value()),
314                 createAce(CHRIS, EnumBasicPermissions.CMIS_ALL.value())
315                 });
316         return fFactory.createAccessControlList(acesRemove);               
317     }
318     
319     private void checkAclAfterAddRemove(Acl aclReturn) {
320         assertNotNull(aclReturn);
321         assertEquals(2, aclReturn.getAces().size());
322         assertTrue(aclHasPermission(aclReturn, ALICE, EnumBasicPermissions.CMIS_READ.value()));        
323         assertTrue(aclHasPermission(aclReturn, DAN, EnumBasicPermissions.CMIS_WRITE.value()));        
324         assertFalse(aclHasPermission(aclReturn, BOB, EnumBasicPermissions.CMIS_WRITE.value()));        
325         assertFalse(aclHasPermission(aclReturn, CHRIS, EnumBasicPermissions.CMIS_ALL.value()));        
326         assertTrue(aclHasNoPermission(aclReturn, BOB));        
327         assertTrue(aclHasNoPermission(aclReturn, CHRIS));        
328     }
329     
330    private void checkAdvancedTestAcl(Acl acl, Acl aclReturn) {
331         assertNotNull(aclReturn);
332         assertEquals(acl.getAces().size(), aclReturn.getAces().size());
333         assertTrue(aclHasPermission(aclReturn, ALICE, EnumBasicPermissions.CMIS_READ.value()));        
334         assertTrue(aclHasPermission(aclReturn, BOB, EnumBasicPermissions.CMIS_WRITE.value()));        
335         assertTrue(aclHasPermission(aclReturn, CHRIS, EnumBasicPermissions.CMIS_ALL.value()));        
336     }
337 
338     private Ace createAce(String principalId, String permission) {
339         return  fFactory.createAccessControlEntry(principalId, Collections.singletonList( permission ));
340     }
341     
342     private boolean aclHasPermission(Acl acl, String principalId, String permission) {
343         for (Ace ace : acl.getAces()) {
344             if (ace.getPrincipalId().equals(principalId) && aceContainsPermission(ace, permission))
345                 return true;            
346         }
347         return false;
348     }
349     
350     private boolean aclHasNoPermission(Acl acl, String principalId) {
351         if (null == acl)
352             return false;
353         
354         for (Ace ace : acl.getAces()) {
355             if (ace.getPrincipalId().equals(principalId))
356                 return false;
357         }
358         return true;
359     }
360     
361     private boolean aceContainsPermission(Ace ace, String permission) {
362         for (String acePerm : ace.getPermissions())
363             if (permission.equals(acePerm))
364                 return true;
365         return false;
366     }
367 }