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