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.Date;
28 import java.util.Hashtable;
29 import java.util.List;
30 import javax.ws.rs.core.Response;
31 import org.collectionspace.services.authorization.EffectType;
33 import org.collectionspace.services.authorization.Permission;
34 import org.collectionspace.services.authorization.PermissionAction;
35 import org.collectionspace.services.authorization.PermissionRole;
36 import org.collectionspace.services.authorization.PermissionValue;
37 import org.collectionspace.services.authorization.Role;
38 import org.collectionspace.services.authorization.RoleValue;
39 import org.collectionspace.services.client.CollectionSpaceClient;
40 import org.collectionspace.services.client.PermissionClient;
41 import org.collectionspace.services.client.PermissionFactory;
42 import org.collectionspace.services.client.RolePermissionClient;
43 import org.collectionspace.services.client.PermissionRoleFactory;
44 import org.collectionspace.services.client.RoleClient;
45 import org.collectionspace.services.client.RoleFactory;
46 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
47 import org.collectionspace.services.client.test.ServiceRequestType;
48 import org.collectionspace.services.jaxb.AbstractCommonList;
49 import org.jboss.resteasy.client.ClientResponse;
51 import org.testng.Assert;
52 import org.testng.annotations.Test;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.testng.annotations.AfterClass;
57 import org.testng.annotations.BeforeClass;
60 * PermissionServiceTest, carries out tests against a
61 * deployed and running Permission, Role and PermissionRole Services.
63 * $LastChangedRevision: 917 $
64 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
66 public class RolePermissionServiceTest extends AbstractServiceTestImpl {
68 /** The Constant logger. */
69 private final static String CLASS_NAME = RolePermissionServiceTest.class.getName();
70 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
71 // Instance variables specific to this test.
72 /** The known resource id. */
73 private String knownResourceId = null;
74 /** The all resource ids created. */
75 private List<String> allResourceIdsCreated = new ArrayList<String>();
76 final private static String TEST_MARKER = "_RolePermissionServiceTest";
77 final private static String TEST_ROLE_NAME = "ROLE";
78 final private static String NO_REL_SUFFIX = "-no-rel";
79 /** The perm values. */
80 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
81 /** The role values. */
82 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
83 private Date now = new Date();
85 * This method is called only by the parent class, AbstractServiceTestImpl
89 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
92 protected String getServicePathComponent() {
93 return new RolePermissionClient().getServicePathComponent();
96 private String getRoleName() {
97 return TEST_ROLE_NAME + TEST_MARKER + now.toString();
103 @BeforeClass(alwaysRun = true)
104 public void seedData() {
106 String rn1 = getRoleName();
107 String r1RoleId = createRole(rn1);
108 RoleValue rv1 = new RoleValue();
109 rv1.setRoleId(r1RoleId);
110 rv1.setRoleName(rn1);
111 roleValues.put(rv1.getRoleName(), rv1);
113 String rn2 = getRoleName() + NO_REL_SUFFIX;
114 String r2RoleId = createRole(rn2);
115 RoleValue rv2 = new RoleValue();
116 rv2.setRoleId(r2RoleId);
117 rv2.setRoleName(rn2);
118 roleValues.put(rv2.getRoleName(), rv2);
120 String ra1 = "fooService" + TEST_MARKER;
121 String permId1 = createPermission(ra1, EffectType.PERMIT);
122 PermissionValue pva1 = new PermissionValue();
123 pva1.setResourceName(ra1);
124 pva1.setPermissionId(permId1);
125 permValues.put(pva1.getResourceName(), pva1);
127 String ra2 = "barService" + TEST_MARKER;
128 String permId2 = createPermission(ra1, EffectType.PERMIT);
129 PermissionValue pva2 = new PermissionValue();
130 pva2.setResourceName(ra2);
131 pva2.setPermissionId(permId2);
132 permValues.put(pva2.getResourceName(), pva2);
136 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
139 protected CollectionSpaceClient getClientInstance() {
140 return new RolePermissionClient();
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)
170 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
171 public void create(String testName) throws Exception {
173 if (logger.isDebugEnabled()) {
174 logger.debug(testBanner(testName, CLASS_NAME));
177 // Perform setup, such as initializing the type of service request
178 // (e.g. CREATE, DELETE), its valid and expected status codes, and
179 // its associated HTTP method name (e.g. POST, DELETE).
182 // Submit the request to the service and store the response.
183 RoleValue rv = roleValues.get(getRoleName());
184 PermissionRole permRole = createPermissionRoleInstance(rv,
185 permValues.values(), true, true);
186 RolePermissionClient client = new RolePermissionClient();
187 ClientResponse<Response> res = null;
189 res = client.create(rv.getRoleId(), permRole);
190 int statusCode = res.getStatus();
191 if (logger.isDebugEnabled()) {
192 logger.debug(testName + ": status = " + statusCode);
194 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
195 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
196 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
197 // Store the ID returned from this create operation
198 // for additional tests below.
199 //this is is not important in case of this relationship
200 knownResourceId = extractId(res);
201 if (logger.isDebugEnabled()) {
202 logger.debug(testName + ": knownResourceId=" + knownResourceId);
206 res.releaseConnection();
211 //to not cause uniqueness violation for permRole, createList is removed
213 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
216 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
217 dependsOnMethods = {"create"})
218 public void createList(String testName) throws Exception {
219 //Should this really be empty?
223 // Placeholders until the three tests below can be uncommented.
224 // See Issue CSPACE-401.
226 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
229 public void createWithEmptyEntityBody(String testName) throws Exception {
230 //Should this really be empty?
234 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
237 public void createWithMalformedXml(String testName) throws Exception {
238 //Should this really be empty?
242 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
245 public void createWithWrongXmlSchema(String testName) throws Exception {
246 //Should this really be empty?
249 // ---------------------------------------------------------------
250 // CRUD tests : READ tests
251 // ---------------------------------------------------------------
254 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
257 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
258 dependsOnMethods = {"create"})
259 public void read(String testName) throws Exception {
261 if (logger.isDebugEnabled()) {
262 logger.debug(testBanner(testName, CLASS_NAME));
268 // Submit the request to the service and store the response.
269 RolePermissionClient client = new RolePermissionClient();
270 ClientResponse<PermissionRole> res = null;
272 res = client.read(roleValues.get(getRoleName()).getRoleId());
273 int statusCode = res.getStatus();
275 // Check the status code of the response: does it match
276 // the expected response(s)?
277 if (logger.isDebugEnabled()) {
278 logger.debug(testName + ": status = " + statusCode);
280 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
281 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
282 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
284 PermissionRole output = (PermissionRole) res.getEntity();
285 Assert.assertNotNull(output);
288 res.releaseConnection();
295 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
298 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
299 public void readNonExistent(String testName) throws Exception {
301 if (logger.isDebugEnabled()) {
302 logger.debug(testBanner(testName, CLASS_NAME));
306 setupReadNonExistent();
308 // Submit the request to the service and store the response.
309 RolePermissionClient client = new RolePermissionClient();
310 ClientResponse<PermissionRole> res = null;
313 res = client.read(NON_EXISTENT_ID);
314 int statusCode = res.getStatus();
316 // Check the status code of the response: does it match
317 // the expected response(s)?
318 if (logger.isDebugEnabled()) {
319 logger.debug(testName + ": status = " + statusCode);
321 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
322 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
323 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
326 res.releaseConnection();
331 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
332 public void readNoRelationship(String testName) throws Exception {
334 if (logger.isDebugEnabled()) {
335 logger.debug(testBanner(testName, CLASS_NAME));
339 // Submit the request to the service and store the response.
340 RolePermissionClient client = new RolePermissionClient();
341 ClientResponse<PermissionRole> res = null;
344 res = client.read(roleValues.get(getRoleName() + NO_REL_SUFFIX).getRoleId());
345 int statusCode = res.getStatus();
347 // Check the status code of the response: does it match
348 // the expected response(s)?
349 if (logger.isDebugEnabled()) {
350 logger.debug(testName + ": status = " + statusCode);
352 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
353 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
354 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
355 PermissionRole output = (PermissionRole) res.getEntity();
357 String sOutput = objectAsXmlString(output, PermissionRole.class);
358 if (logger.isDebugEnabled()) {
359 logger.debug(testName + " received " + sOutput);
363 res.releaseConnection();
368 // ---------------------------------------------------------------
369 // CRUD tests : READ_LIST tests
370 // ---------------------------------------------------------------
373 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
376 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
377 dependsOnMethods = {"createList", "read"})
378 public void readList(String testName) throws Exception {
379 //Should this really be empty?
384 // ---------------------------------------------------------------
385 // CRUD tests : UPDATE tests
386 // ---------------------------------------------------------------
389 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
392 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
393 dependsOnMethods = {"read", "readList", "readNonExistent"})
394 public void update(String testName) throws Exception {
395 //Should this really be empty?
399 // Placeholders until the three tests below can be uncommented.
400 // See Issue CSPACE-401.
402 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
405 public void updateWithEmptyEntityBody(String testName) throws Exception {
406 //Should this really be empty?
410 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
413 public void updateWithMalformedXml(String testName) throws Exception {
414 //Should this really be empty?
418 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
421 public void updateWithWrongXmlSchema(String testName) throws Exception {
422 //Should this really be empty?
426 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
429 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
430 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
431 public void updateNonExistent(String testName) throws Exception {
432 //Should this really be empty?
435 // ---------------------------------------------------------------
436 // CRUD tests : DELETE tests
437 // ---------------------------------------------------------------
440 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
443 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
444 dependsOnMethods = {"read"})
445 public void delete(String testName) throws Exception {
447 if (logger.isDebugEnabled()) {
448 logger.debug(testBanner(testName, CLASS_NAME));
454 // Submit the request to the service and store the response.
455 RolePermissionClient client = new RolePermissionClient();
456 RoleValue rv = roleValues.get(getRoleName());
457 ClientResponse<Response> delRes = null;
459 delRes = client.delete(rv.getRoleId());
460 int statusCode = delRes.getStatus();
461 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
462 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
463 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
465 if (delRes != null) {
466 delRes.releaseConnection();
470 // reset for next delete
474 rv = roleValues.get(getRoleName());
475 ClientResponse<PermissionRole> readResponse = client.read(rv.getRoleId());
476 PermissionRole toDelete = readResponse.getEntity();
477 readResponse.releaseConnection();
479 rv = toDelete.getRoles().get(0);
480 ClientResponse<Response> res = null;
483 rv.getRoleId(), toDelete);
484 int statusCode = res.getStatus();
485 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
486 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
487 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
490 res.releaseConnection();
497 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
500 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
501 public void deleteNonExistent(String testName) throws Exception {
502 //ignoring this test as the service side returns 200 now even if it does
503 //not find a record in the db
506 // ---------------------------------------------------------------
507 // Utility tests : tests of code used in tests above
508 // ---------------------------------------------------------------
510 * Tests the code for manually submitting data that is used by several
511 * of the methods above.
514 @Test(dependsOnMethods = {"create"})
515 public void testSubmitRequest() throws Exception {
517 // Expected status code: 200 OK
518 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
520 // Submit the request to the service and store the response.
521 String method = ServiceRequestType.READ.httpMethodName();
522 String url = getResourceURL(roleValues.get(getRoleName()).getRoleId());
523 int statusCode = submitRequest(method, url);
525 // Check the status code of the response: does it match
526 // the expected response(s)?
527 if (logger.isDebugEnabled()) {
528 logger.debug("testSubmitRequest: url=" + url
529 + " status=" + statusCode);
531 Assert.assertEquals(statusCode, EXPECTED_STATUS);
536 // ---------------------------------------------------------------
537 // Utility methods used by tests above
538 // ---------------------------------------------------------------
540 * create PermissionRole instance
541 * @param rv rolevalue
542 * @param pvs permission value array
545 * @return PermissionRole
547 public static PermissionRole createPermissionRoleInstance(RoleValue rv,
548 Collection<PermissionValue> pvs,
551 List<PermissionValue> pvls = new ArrayList<PermissionValue>();
553 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
554 rv, pvls, usePermId, useRoleId);
555 if (logger.isDebugEnabled()) {
558 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
566 @AfterClass(alwaysRun = true)
568 public void cleanUp() {
570 String noTest = System.getProperty("noTestCleanup");
571 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
572 if (logger.isDebugEnabled()) {
573 logger.debug("Skipping Cleanup phase ...");
577 if (logger.isDebugEnabled()) {
578 logger.debug("Cleaning up temporary resources created for testing ...");
580 for (PermissionValue pv : permValues.values()) {
581 deletePermission(pv.getPermissionId());
583 for (RoleValue rv : roleValues.values()) {
584 deleteRole(rv.getRoleId());
589 * Creates the permission.
591 * @param resName the res name
592 * @param effect the effect
595 private String createPermission(String resName, EffectType effect) {
597 PermissionClient permClient = new PermissionClient();
598 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
599 Permission permission = PermissionFactory.createPermissionInstance(resName,
600 "default permissions for " + resName,
601 actions, effect, true, true, true);
602 ClientResponse<Response> res = null;
605 res = permClient.create(permission);
606 int statusCode = res.getStatus();
607 if (logger.isDebugEnabled()) {
608 logger.debug("createPermission: resName=" + resName
609 + " status = " + statusCode);
611 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
612 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
613 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
617 res.releaseConnection();
626 * @param permId the perm id
628 private void deletePermission(String permId) {
630 PermissionClient permClient = new PermissionClient();
632 ClientResponse<Response> res = null;
634 res = permClient.delete(permId);
635 int statusCode = res.getStatus();
636 if (logger.isDebugEnabled()) {
637 logger.debug("deletePermission: delete permission id="
638 + permId + " status=" + statusCode);
640 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
641 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
642 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
646 res.releaseConnection();
654 * @param roleName the role name
657 private String createRole(String roleName) {
659 RoleClient roleClient = new RoleClient();
661 Role role = RoleFactory.createRoleInstance(roleName,
662 "role for " + roleName, true);
663 role.setRoleGroup("something");
664 ClientResponse<Response> res = null;
667 res = roleClient.create(role);
668 int statusCode = res.getStatus();
669 if (logger.isDebugEnabled()) {
670 logger.debug("createRole: name=" + roleName
671 + " status = " + statusCode);
673 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
674 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
675 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
679 res.releaseConnection();
688 * @param roleId the role id
690 private void deleteRole(String roleId) {
692 RoleClient roleClient = new RoleClient();
693 ClientResponse<Response> res = null;
695 res = roleClient.delete(roleId);
696 int statusCode = res.getStatus();
697 if (logger.isDebugEnabled()) {
698 logger.debug("deleteRole: delete role id=" + roleId
699 + " status=" + statusCode);
701 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
702 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
703 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
705 res.releaseConnection();