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.crud;
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  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.WARNING;
25  
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.Policy;
32  import org.apache.chemistry.opencmis.client.api.Session;
33  import org.apache.chemistry.opencmis.commons.data.ObjectData;
34  import org.apache.chemistry.opencmis.tck.CmisTestResult;
35  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
36  
37  /**
38   * Simple folder test.
39   */
40  public class CreateAndDeletePolicyTest extends AbstractSessionTest {
41  
42      @Override
43      public void init(Map<String, String> parameters) {
44          super.init(parameters);
45          setName("Create and Delete Policy Test");
46          setDescription(
47                  "Creates a policy object, checks the newly created policy object, applys and removes it from a document, and finally deletes the created policy object.");
48      }
49  
50      @Override
51      public void run(Session session) {
52  
53          if (hasPolicies(session)) {
54              CmisTestResult f;
55  
56              // create a test folder
57              Folder testFolder = createTestFolder(session);
58  
59              try {
60                  // create policy object
61                  Policy policy = createPolicy(session, testFolder, "testPolicy", "TCK Test Policy");
62  
63                  // create document and apply policy
64                  Document doc = createDocument(session, testFolder, "testDocument", "Policy Test");
65  
66                  if (Boolean.TRUE.equals(doc.getType().isControllablePolicy())) {
67                      doc.applyPolicy(policy);
68  
69                      // check if policy has been applied
70                      List<Policy> policies1 = doc.getPolicies();
71                      boolean found1 = false;
72                      for (Policy p : policies1) {
73                          if (p.getId().equals(policy.getId())) {
74                              found1 = true;
75                              break;
76                          }
77                      }
78  
79                      f = createResult(FAILURE, "Policy has not been applied to document! Policy Id: " + policy.getId()
80                              + ", Doc Id: " + doc.getId());
81                      addResult(assertIsTrue(found1, null, f));
82  
83                      // check if policy IDs and policy object match
84                      f = createResult(WARNING, "Not all policy IDs can be resolved to policy objects.");
85                      addResult(assertEquals(doc.getPolicyIds().size(), doc.getPolicies().size(), null, f));
86  
87                      // get the policies
88                      List<ObjectData> policiesData2 = session.getBinding().getPolicyService()
89                              .getAppliedPolicies(session.getRepositoryInfo().getId(), doc.getId(), "*", null);
90  
91                      boolean found2 = false;
92                      if (policiesData2 != null && !policiesData2.isEmpty()) {
93                          for (ObjectData p : policiesData2) {
94                              if (p.getId().equals(policy.getId())) {
95                                  found2 = true;
96                                  break;
97                              }
98                          }
99                      }
100 
101                     f = createResult(FAILURE, "Applied policy is not returned by the repository! Policy Id: "
102                             + policy.getId() + ", Doc Id: " + doc.getId());
103                     addResult(assertIsTrue(found2, null, f));
104 
105                     // remove policy
106                     doc.removePolicy(policy);
107 
108                     // check if policy has been applied
109                     List<Policy> policies3 = doc.getPolicies();
110                     if (policies3 != null) {
111                         boolean found3 = false;
112                         for (Policy p : policies3) {
113                             if (p.getId().equals(policy.getId())) {
114                                 found3 = true;
115                                 break;
116                             }
117                         }
118 
119                         f = createResult(FAILURE, "Policy has not been removed from document! Policy Id: "
120                                 + policy.getId() + ", Doc Id: " + doc.getId());
121                         addResult(assertIsFalse(found3, null, f));
122                     }
123                 } else {
124                     addResult(createResult(INFO, "Document type " + doc.getType().getId()
125                             + " does not allow applying and removing policies. Choose a different document type for this test."));
126                 }
127 
128                 // delete document
129                 deleteObject(doc);
130 
131                 // delete policy object
132                 deleteObject(policy);
133 
134             } finally {
135                 // delete the test folder
136                 deleteTestFolder();
137             }
138         } else {
139             addResult(createResult(SKIPPED, "Policies not supported. Test skipped!"));
140         }
141     }
142 }