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.control;
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.Collections;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.chemistry.opencmis.client.api.Document;
31  import org.apache.chemistry.opencmis.client.api.Folder;
32  import org.apache.chemistry.opencmis.client.api.Session;
33  import org.apache.chemistry.opencmis.commons.data.Ace;
34  import org.apache.chemistry.opencmis.commons.data.Acl;
35  import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
36  import org.apache.chemistry.opencmis.commons.enums.AclPropagation;
37  import org.apache.chemistry.opencmis.commons.enums.CapabilityAcl;
38  import org.apache.chemistry.opencmis.tck.CmisTestResult;
39  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
40  import org.apache.chemistry.opencmis.tck.impl.TestParameters;
41  
42  /**
43   * ACL smoke test.
44   */
45  public class ACLSmokeTest extends AbstractSessionTest {
46  
47      @Override
48      public void init(Map<String, String> parameters) {
49          super.init(parameters);
50          setName("ACL Test");
51          setDescription("Creates a document and checks its ACL.");
52      }
53  
54      @Override
55      public void run(Session session) {
56          CmisTestResult f;
57  
58          if (supportsACLs(session)) {
59              try {
60                  // create folder and document
61                  Folder testFolder = createTestFolder(session);
62                  Document doc = createDocument(session, testFolder, "acltest.txt", "ACL test");
63  
64                  // check if there is an ACL
65                  Acl acl = doc.getAcl();
66  
67                  f = createResult(FAILURE, "ACLs are supported but newly created document has no ACL!");
68                  addResult(assertNotNull(acl, null, f));
69  
70                  // check basic permissions
71                  Acl basicAcl = session.getAcl(doc, true);
72  
73                  f = createResult(FAILURE,
74                          "ACLs are supported but repository does not return a basic ACL for the newly created document!");
75                  addResult(assertNotNull(basicAcl, null, f));
76  
77                  if (basicAcl != null) {
78                      addResult(checkACL(session, basicAcl, false, "Basic ACL"));
79  
80                      if (basicAcl.getAces() != null) {
81                          for (Ace ace : basicAcl.getAces()) {
82                              if (ace.getPermissions() != null) {
83                                  for (String permission : ace.getPermissions()) {
84                                      if (!"cmis:read".equals(permission) && !"cmis:write".equals(permission)
85                                              && !"cmis:all".equals(permission)) {
86                                          addResult(createResult(FAILURE, "ACE contains a non-basic permission: "
87                                                  + permission));
88                                      }
89                                  }
90                              }
91                          }
92                      }
93                  }
94  
95                  if (getAclCapability(session) == CapabilityAcl.MANAGE
96                          && !Boolean.FALSE.equals(doc.getType().isControllableAcl())) {
97                      String principal = getParameters().get(TestParameters.DEFAULT_ACL_PRINCIPAL);
98                      if (principal == null) {
99                          principal = TestParameters.DEFAULT_ACL_PRINCIPAL_VALUE;
100                     }
101 
102                     // apply permission "cmis:write"
103                     List<Ace> aces = new ArrayList<Ace>();
104                     aces.add(session.getObjectFactory().createAce(principal, Collections.singletonList("cmis:write")));
105 
106                     session.applyAcl(doc, aces, null, null);
107 
108                     if (session.getRepositoryInfo().getAclCapabilities().getAclPropagation() != AclPropagation.REPOSITORYDETERMINED) {
109                         // set permission "cmis:all"
110                         aces = new ArrayList<Ace>();
111                         aces.add(session.getObjectFactory().createAce(principal, Collections.singletonList("cmis:all")));
112 
113                         session.setAcl(doc, aces);
114                     }
115                 } else {
116                     addResult(createResult(INFO, "The repository or the type '" + doc.getType().getId()
117                             + "' don't support managing ACLs."));
118                 }
119 
120                 deleteObject(doc);
121             } finally {
122                 deleteTestFolder();
123             }
124         } else {
125             addResult(createResult(SKIPPED, "ACLs are not supported. Test Skipped!"));
126         }
127     }
128 
129     protected boolean supportsACLs(Session session) {
130         CapabilityAcl aclCap = getAclCapability(session);
131         return (aclCap != null) && (aclCap != CapabilityAcl.NONE);
132     }
133 
134     protected CapabilityAcl getAclCapability(Session session) {
135         RepositoryInfo repository = session.getRepositoryInfo();
136 
137         if (repository.getCapabilities().getAclCapability() == null) {
138             return null;
139         }
140 
141         return repository.getCapabilities().getAclCapability();
142     }
143 }