This project has retired. For details please refer to its Attic page.
AclMergeTest 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.client.bindings.atompub;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.AbstractAtomPubService;
29  import org.apache.chemistry.opencmis.commons.data.Ace;
30  import org.apache.chemistry.opencmis.commons.data.Acl;
31  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AccessControlEntryImpl;
32  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AccessControlListImpl;
33  import org.apache.chemistry.opencmis.commons.impl.dataobjects.AccessControlPrincipalDataImpl;
34  
35  /**
36   * Test for the ACL merging that is necessary in the AtomPub binding
37   * implementation.
38   */
39  public class AclMergeTest extends TestCase {
40  
41      public void testIsACLMergeRequired() {
42          AtomPubService service = new AtomPubService();
43  
44          assertFalse(service.publicIsACLMergeRequired(null, null));
45          assertFalse(service.publicIsACLMergeRequired(new AccessControlListImpl(), null));
46          assertFalse(service.publicIsACLMergeRequired(null, new AccessControlListImpl()));
47          assertFalse(service.publicIsACLMergeRequired(new AccessControlListImpl(), new AccessControlListImpl()));
48      }
49  
50      public void testAclMerge() {
51          AtomPubService service = new AtomPubService();
52  
53          // original
54          Map<String, String[]> originalAceData = new HashMap<String, String[]>();
55  
56          originalAceData.put("p1", new String[] { "perm:read", "perm:write", "perm:delete" });
57          originalAceData.put("p2", new String[] { "perm:read" });
58          originalAceData.put("p3", new String[] { "perm:all" });
59  
60          Acl originalACEs = createACL(originalAceData);
61  
62          // add
63          Map<String, String[]> addAceData = new HashMap<String, String[]>();
64  
65          addAceData.put("p2", new String[] { "perm:write" });
66          addAceData.put("p4", new String[] { "perm:all" });
67  
68          Acl addACEs = createACL(addAceData);
69  
70          // remove
71          Map<String, String[]> removeAceData = new HashMap<String, String[]>();
72  
73          removeAceData.put("p1", new String[] { "perm:write" });
74          removeAceData.put("p3", new String[] { "perm:all" });
75  
76          Acl removeACEs = createACL(removeAceData);
77  
78          Acl newACL = service.publicMergeACLs(originalACEs, addACEs, removeACEs);
79  
80          assertEquals(3, newACL.getAces().size());
81  
82          for (Ace ace : newACL.getAces()) {
83              String principal = ace.getPrincipal().getId();
84              assertNotNull(principal);
85  
86              if (principal.equals("p1")) {
87                  assertEquals(2, ace.getPermissions().size());
88                  assertTrue(ace.getPermissions().contains("perm:read"));
89                  assertTrue(ace.getPermissions().contains("perm:delete"));
90                  assertFalse(ace.getPermissions().contains("perm:write"));
91              } else if (principal.equals("p2")) {
92                  assertEquals(2, ace.getPermissions().size());
93                  assertTrue(ace.getPermissions().contains("perm:read"));
94                  assertTrue(ace.getPermissions().contains("perm:write"));
95              } else if (principal.equals("p3")) {
96                  fail("Principal should be deleted!");
97              } else if (principal.equals("p4")) {
98                  assertEquals(1, ace.getPermissions().size());
99                  assertTrue(ace.getPermissions().contains("perm:all"));
100             }
101         }
102     }
103 
104     /**
105      * Creates an ACL structure from a Map.
106      */
107     private static Acl createACL(Map<String, String[]> aceData) {
108         AccessControlListImpl result = new AccessControlListImpl();
109 
110         List<Ace> aces = new ArrayList<Ace>();
111 
112         for (Map.Entry<String, String[]> e : aceData.entrySet()) {
113             ArrayList<String> permissions = new ArrayList<String>();
114 
115             for (String s : e.getValue()) {
116                 permissions.add(s);
117             }
118 
119             AccessControlEntryImpl ace = new AccessControlEntryImpl(new AccessControlPrincipalDataImpl(e.getKey()),
120                     permissions);
121 
122             aces.add(ace);
123         }
124 
125         result.setAces(aces);
126 
127         return result;
128     }
129 
130     /**
131      * A class to make a few protected methods publicly available.
132      */
133     private static class AtomPubService extends AbstractAtomPubService {
134         public Acl publicMergeACLs(Acl originalACEs, Acl addACEs, Acl removeACEs) {
135             return mergeAcls(originalACEs, addACEs, removeACEs);
136         }
137 
138         public boolean publicIsACLMergeRequired(Acl addACEs, Acl removeACEs) {
139             return isAclMergeRequired(addACEs, removeACEs);
140         }
141     }
142 }