2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permRoles and
21 * limitations under the License.
23 package org.collectionspace.services.authorization.client.test;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Hashtable;
28 import java.util.List;
29 import javax.ws.rs.core.Response;
30 import org.collectionspace.services.authorization.EffectType;
32 import org.collectionspace.services.authorization.Permission;
33 import org.collectionspace.services.authorization.PermissionAction;
34 import org.collectionspace.services.authorization.PermissionRole;
35 import org.collectionspace.services.authorization.PermissionValue;
36 import org.collectionspace.services.authorization.Role;
37 import org.collectionspace.services.authorization.RoleValue;
38 import org.collectionspace.services.client.CollectionSpaceClient;
39 import org.collectionspace.services.client.PermissionClient;
40 import org.collectionspace.services.client.PermissionFactory;
41 import org.collectionspace.services.client.PermissionRoleClient;
42 import org.collectionspace.services.client.PermissionRoleFactory;
43 import org.collectionspace.services.client.RoleClient;
44 import org.collectionspace.services.client.RoleFactory;
45 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
46 import org.collectionspace.services.client.test.ServiceRequestType;
47 import org.collectionspace.services.jaxb.AbstractCommonList;
48 import org.jboss.resteasy.client.ClientResponse;
50 import org.testng.Assert;
51 import org.testng.annotations.Test;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.testng.annotations.AfterClass;
56 import org.testng.annotations.BeforeClass;
59 * PermissionServiceTest, carries out tests against a
60 * deployed and running Permission, Role and PermissionRole Services.
62 * $LastChangedRevision: 917 $
63 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
65 public class PermissionRoleServiceTest extends AbstractServiceTestImpl {
67 /** The Constant logger. */
68 static private final Logger logger =
69 LoggerFactory.getLogger(PermissionRoleServiceTest.class);
70 // Instance variables specific to this test.
71 /** The known resource id. */
72 private String knownResourceId = null;
74 /** The all resource ids created. */
75 private List<String> allResourceIdsCreated = new ArrayList<String>();
77 /** The perm values. */
78 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
80 /** The role values. */
81 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
83 * This method is called only by the parent class, AbstractServiceTestImpl
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
90 protected String getServicePathComponent() {
91 return new PermissionRoleClient().getServicePathComponent();
97 @BeforeClass(alwaysRun = true)
98 public void seedData() {
99 String ra = "accounts";
100 String accPermId = createPermission(ra, EffectType.PERMIT);
101 PermissionValue pva = new PermissionValue();
102 pva.setResourceName(ra);
103 pva.setPermissionId(accPermId);
104 permValues.put(pva.getResourceName(), pva);
106 // String rc = "collectionobjects";
107 // String coPermId = createPermission(rc, EffectType.DENY);
108 // PermissionValue pvc = new PermissionValue();
109 // pvc.setResourceName(rc);
110 // pvc.setPermissionId(coPermId);
111 // permValues.put(pvc.getResourceName(), pvc);
113 // String ri = "intakes";
114 // String iPermId = createPermission(ri, EffectType.DENY);
115 // PermissionValue pvi = new PermissionValue();
116 // pvi.setResourceName(ri);
117 // pvi.setPermissionId(iPermId);
118 // permValues.put(pvi.getResourceName(), pvi);
120 String rn1 = "ROLE_CO1";
121 String r1RoleId = createRole(rn1);
122 RoleValue rv1 = new RoleValue();
123 rv1.setRoleId(r1RoleId);
124 rv1.setRoleName(rn1);
125 roleValues.put(rv1.getRoleName(), rv1);
127 String rn2 = "ROLE_CO2";
128 String r2RoleId = createRole(rn2);
129 RoleValue rv2 = new RoleValue();
130 rv2.setRoleId(r2RoleId);
131 rv2.setRoleName(rn2);
132 roleValues.put(rv2.getRoleName(), rv2);
136 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
139 protected CollectionSpaceClient getClientInstance() {
140 return new PermissionRoleClient();
144 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
147 protected AbstractCommonList getAbstractCommonList(
148 ClientResponse<AbstractCommonList> response) {
149 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
150 throw new UnsupportedOperationException();
154 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
156 @Test(dataProvider = "testName")
158 public void readPaginatedList(String testName) throws Exception {
159 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
161 // ---------------------------------------------------------------
162 // CRUD tests : CREATE tests
163 // ---------------------------------------------------------------
166 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
169 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
170 public void create(String testName) throws Exception {
172 // Perform setup, such as initializing the type of service request
173 // (e.g. CREATE, DELETE), its valid and expected status codes, and
174 // its associated HTTP method name (e.g. POST, DELETE).
175 setupCreate(testName);
177 // Submit the request to the service and store the response.
178 PermissionValue pv = permValues.get("accounts");
179 PermissionRole permRole = createPermissionRoleInstance(pv,
180 roleValues.values(), true, true);
181 PermissionRoleClient client = new PermissionRoleClient();
182 ClientResponse<Response> res = client.create(pv.getPermissionId(), permRole);
183 int statusCode = res.getStatus();
185 if (logger.isDebugEnabled()) {
186 logger.debug(testName + ": status = " + statusCode);
188 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
189 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
190 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
191 res.releaseConnection();
192 // Store the ID returned from this create operation
193 // for additional tests below.
194 //this is is not important in case of this relationship
195 knownResourceId = extractId(res);
196 if (logger.isDebugEnabled()) {
197 logger.debug(testName + ": knownResourceId=" + knownResourceId);
201 //to not cause uniqueness violation for permRole, createList is removed
203 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
206 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
207 dependsOnMethods = {"create"})
208 public void createList(String testName) throws Exception {
209 //Should this really be empty?
213 // Placeholders until the three tests below can be uncommented.
214 // See Issue CSPACE-401.
216 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
219 public void createWithEmptyEntityBody(String testName) throws Exception {
220 //Should this really be empty?
224 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
227 public void createWithMalformedXml(String testName) throws Exception {
228 //Should this really be empty?
232 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
235 public void createWithWrongXmlSchema(String testName) throws Exception {
236 //Should this really be empty?
239 // ---------------------------------------------------------------
240 // CRUD tests : READ tests
241 // ---------------------------------------------------------------
244 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
247 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
248 dependsOnMethods = {"create"})
249 public void read(String testName) throws Exception {
254 // Submit the request to the service and store the response.
255 PermissionRoleClient client = new PermissionRoleClient();
256 ClientResponse<PermissionRole> res = client.read(
257 permValues.get("accounts").getPermissionId(), "123");
258 int statusCode = res.getStatus();
260 // Check the status code of the response: does it match
261 // the expected response(s)?
262 if (logger.isDebugEnabled()) {
263 logger.debug(testName + ": status = " + statusCode);
265 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
266 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
267 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
269 PermissionRole output = (PermissionRole) res.getEntity();
270 Assert.assertNotNull(output);
271 res.releaseConnection();
276 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
279 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
280 public void readNonExistent(String testName) throws Exception {
283 setupReadNonExistent(testName);
285 // Submit the request to the service and store the response.
286 PermissionRoleClient client = new PermissionRoleClient();
287 ClientResponse<PermissionRole> res = client.read(NON_EXISTENT_ID, "123");
288 int statusCode = res.getStatus();
290 // Check the status code of the response: does it match
291 // the expected response(s)?
292 if (logger.isDebugEnabled()) {
293 logger.debug(testName + ": status = " + statusCode);
295 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
296 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
297 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
298 res.releaseConnection();
301 // ---------------------------------------------------------------
302 // CRUD tests : READ_LIST tests
303 // ---------------------------------------------------------------
306 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
309 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
310 dependsOnMethods = {"createList", "read"})
311 public void readList(String testName) throws Exception {
312 //Should this really be empty?
317 // ---------------------------------------------------------------
318 // CRUD tests : UPDATE tests
319 // ---------------------------------------------------------------
322 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
325 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
326 dependsOnMethods = {"read", "readList", "readNonExistent"})
327 public void update(String testName) throws Exception {
328 //Should this really be empty?
332 // Placeholders until the three tests below can be uncommented.
333 // See Issue CSPACE-401.
335 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
338 public void updateWithEmptyEntityBody(String testName) throws Exception {
339 //Should this really be empty?
343 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
346 public void updateWithMalformedXml(String testName) throws Exception {
347 //Should this really be empty?
351 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
354 public void updateWithWrongXmlSchema(String testName) throws Exception {
355 //Should this really be empty?
359 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
362 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
363 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
364 public void updateNonExistent(String testName) throws Exception {
365 //Should this really be empty?
368 // ---------------------------------------------------------------
369 // CRUD tests : DELETE tests
370 // ---------------------------------------------------------------
373 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
376 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
377 dependsOnMethods = {"read"})
378 public void delete(String testName) throws Exception {
381 setupDelete(testName);
383 // Submit the request to the service and store the response.
384 PermissionRoleClient client = new PermissionRoleClient();
385 ClientResponse<Response> res = client.delete(
386 permValues.get("accounts").getPermissionId(), "123");
387 int statusCode = res.getStatus();
389 // Check the status code of the response: does it match
390 // the expected response(s)?
391 if (logger.isDebugEnabled()) {
392 logger.debug(testName + ": status = " + statusCode);
394 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
395 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
396 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
397 res.releaseConnection();
402 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
405 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
406 public void deleteNonExistent(String testName) throws Exception {
407 //ignoring this test as the service side returns 200 now even if it does
408 //not find a record in the db
411 // ---------------------------------------------------------------
412 // Utility tests : tests of code used in tests above
413 // ---------------------------------------------------------------
415 * Tests the code for manually submitting data that is used by several
416 * of the methods above.
419 @Test(dependsOnMethods = {"create"})
420 public void testSubmitRequest() throws Exception {
422 // Expected status code: 200 OK
423 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
425 // Submit the request to the service and store the response.
426 String method = ServiceRequestType.READ.httpMethodName();
427 String url = getResourceURL(permValues.get("accounts").getPermissionId());
428 int statusCode = submitRequest(method, url);
430 // Check the status code of the response: does it match
431 // the expected response(s)?
432 if (logger.isDebugEnabled()) {
433 logger.debug("testSubmitRequest: url=" + url
434 + " status=" + statusCode);
436 Assert.assertEquals(statusCode, EXPECTED_STATUS);
441 // ---------------------------------------------------------------
442 // Utility methods used by tests above
443 // ---------------------------------------------------------------
445 * create permRolerole instance
450 * @param roleValues array of role ids
453 * @return PermissionRole
455 public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
456 Collection<RoleValue> rvs,
460 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
461 pv, rvs, usePermId, useRoleId);
462 if (logger.isDebugEnabled()) {
463 logger.debug("to be created, permRole");
464 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
472 @AfterClass(alwaysRun = true)
474 public void cleanUp() {
475 setupDelete("cleanup");
476 String noTest = System.getProperty("noTestCleanup");
477 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
478 if (logger.isDebugEnabled()) {
479 logger.debug("Skipping Cleanup phase ...");
483 if (logger.isDebugEnabled()) {
484 logger.debug("Cleaning up temporary resources created for testing ...");
487 PermissionRoleClient client = new PermissionRoleClient();
488 for (String resourceId : allResourceIdsCreated) {
490 ClientResponse<Response> res = client.delete(resourceId, "123");
491 int statusCode = res.getStatus();
493 if (logger.isDebugEnabled()) {
494 logger.debug("cleanup: delete relationships for permission id="
495 + resourceId + " status=" + statusCode);
497 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
498 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
499 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
501 res.releaseConnection();
505 for (PermissionValue pv : permValues.values()) {
506 deletePermission(pv.getPermissionId());
508 for (RoleValue rv : roleValues.values()) {
509 deleteRole(rv.getRoleId());
514 * Creates the permission.
516 * @param resName the res name
517 * @param effect the effect
520 private String createPermission(String resName, EffectType effect) {
522 PermissionClient permClient = new PermissionClient();
523 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
524 Permission permission = PermissionFactory.createPermissionInstance(resName,
525 "default permissions for " + resName,
526 actions, effect, true, true, true);
527 ClientResponse<Response> res = permClient.create(permission);
528 int statusCode = res.getStatus();
529 if (logger.isDebugEnabled()) {
530 logger.debug("createPermission: resName=" + resName
531 + " status = " + statusCode);
533 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
534 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
535 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
536 res.releaseConnection();
537 return extractId(res);
543 * @param permId the perm id
545 private void deletePermission(String permId) {
547 PermissionClient permClient = new PermissionClient();
548 ClientResponse<Response> res = permClient.delete(permId);
549 int statusCode = res.getStatus();
551 if (logger.isDebugEnabled()) {
552 logger.debug("deletePermission: delete permission id="
553 + permId + " status=" + statusCode);
555 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
556 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
557 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
559 res.releaseConnection();
561 res.releaseConnection();
567 * @param roleName the role name
570 private String createRole(String roleName) {
572 RoleClient roleClient = new RoleClient();
574 Role role = RoleFactory.createRoleInstance(roleName,
575 "role for " + roleName, true);
576 ClientResponse<Response> res = roleClient.create(role);
577 int statusCode = res.getStatus();
578 if (logger.isDebugEnabled()) {
579 logger.debug("createRole: name=" + roleName
580 + " status = " + statusCode);
582 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
583 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
584 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
585 res.releaseConnection();
586 return extractId(res);
592 * @param roleId the role id
594 private void deleteRole(String roleId) {
596 RoleClient roleClient = new RoleClient();
597 ClientResponse<Response> res = roleClient.delete(roleId);
598 int statusCode = res.getStatus();
600 if (logger.isDebugEnabled()) {
601 logger.debug("deleteRole: delete role id=" + roleId
602 + " status=" + statusCode);
604 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
605 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
606 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
608 res.releaseConnection();
610 res.releaseConnection();