1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
57 Folder testFolder = createTestFolder(session);
58
59 try {
60
61 Policy policy = createPolicy(session, testFolder, "testPolicy", "TCK Test Policy");
62
63
64 Document doc = createDocument(session, testFolder, "testDocument", "Policy Test");
65
66 if (Boolean.TRUE.equals(doc.getType().isControllablePolicy())) {
67 doc.applyPolicy(policy);
68
69
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
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
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
106 doc.removePolicy(policy);
107
108
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
129 deleteObject(doc);
130
131
132 deleteObject(policy);
133
134 } finally {
135
136 deleteTestFolder();
137 }
138 } else {
139 addResult(createResult(SKIPPED, "Policies not supported. Test skipped!"));
140 }
141 }
142 }