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;
31 import org.collectionspace.services.authorization.perms.EffectType;
32 import org.collectionspace.services.authorization.perms.Permission;
33 import org.collectionspace.services.authorization.perms.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;
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.PermissionRoleClient;
43 import org.collectionspace.services.client.PermissionRoleFactory;
44 import org.collectionspace.services.client.RoleClient;
45 import org.collectionspace.services.client.RoleFactory;
47 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
48 import org.collectionspace.services.client.test.ServiceRequestType;
50 import org.jboss.resteasy.client.ClientResponse;
52 import org.testng.Assert;
53 import org.testng.annotations.Test;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.testng.annotations.AfterClass;
58 import org.testng.annotations.BeforeClass;
61 * PermissionServiceTest, carries out tests against a
62 * deployed and running Permission, Role and PermissionRole Services.
64 * $LastChangedRevision: 917 $
65 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
67 public class PermissionRoleServiceTest extends AbstractServiceTestImpl<PermissionRole, PermissionRole,
68 PermissionRole, PermissionRole> {
70 /** The Constant logger. */
71 private final static String CLASS_NAME = PermissionRoleServiceTest.class.getName();
72 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
74 // Instance variables specific to this test.
75 final private static String TEST_MARKER = "_PermissionRoleServiceTest";
76 final private static String TEST_SERVICE_NAME = "fakeservice";
77 final private static String NO_REL_SUFFIX = "-no-rel";
78 /** The perm values. */
79 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();
95 protected String getServiceName() {
96 return PermissionClient.SERVICE_NAME; //Since we're a sub-resource of permission service return its name?
100 * The entity type expected from the JAX-RS Response object
102 public Class<PermissionRole> getEntityResponseType() {
103 return PermissionRole.class;
109 @BeforeClass(alwaysRun = true)
110 public void seedData() {
111 String ra = TEST_SERVICE_NAME + TEST_MARKER;
112 String accPermId = createPermission(ra, EffectType.PERMIT);
113 PermissionValue pva = new PermissionValue();
114 pva.setResourceName(ra);
115 pva.setPermissionId(accPermId);
116 permValues.put(pva.getResourceName(), pva);
118 String rc = TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX;
119 String coPermId = createPermission(rc, EffectType.DENY);
120 PermissionValue pvc = new PermissionValue();
121 pvc.setResourceName(rc);
122 pvc.setPermissionId(coPermId);
123 permValues.put(pvc.getResourceName(), pvc);
125 // String ri = "intakes";
126 // String iPermId = createPermission(ri, EffectType.DENY);
127 // PermissionValue pvi = new PermissionValue();
128 // pvi.setResourceName(ri);
129 // pvi.setPermissionId(iPermId);
130 // permValues.put(pvi.getResourceName(), pvi);
132 String rn1 = "ROLE_CO1" + TEST_MARKER;
133 String r1RoleId = createRole(rn1);
134 RoleValue rv1 = new RoleValue();
135 rv1.setRoleId(r1RoleId);
136 rv1.setRoleName(rn1);
137 roleValues.put(rv1.getRoleName(), rv1);
139 String rn2 = "ROLE_CO2" + TEST_MARKER;
140 String r2RoleId = createRole(rn2);
141 RoleValue rv2 = new RoleValue();
142 rv2.setRoleId(r2RoleId);
143 rv2.setRoleName(rn2);
144 roleValues.put(rv2.getRoleName(), rv2);
148 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
151 protected CollectionSpaceClient getClientInstance() {
152 return new PermissionRoleClient();
156 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
159 protected PermissionRole getCommonList(Response response) {
160 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
161 throw new UnsupportedOperationException();
165 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
168 public void readPaginatedList(String testName) throws Exception {
169 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
171 // ---------------------------------------------------------------
172 // CRUD tests : CREATE tests
173 // ---------------------------------------------------------------
176 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
180 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
181 public void create(String testName) throws Exception {
182 // Perform setup, such as initializing the type of service request
183 // (e.g. CREATE, DELETE), its valid and expected status codes, and
184 // its associated HTTP method name (e.g. POST, DELETE).
187 // Submit the request to the service and store the response.
188 PermissionValue pv = permValues.get(TEST_SERVICE_NAME + TEST_MARKER);
189 PermissionRole permRole = createPermissionRoleInstance(pv,
190 roleValues.values(), true, true);
191 PermissionRoleClient client = new PermissionRoleClient();
192 ClientResponse<Response> res = null;
194 res = client.create(pv.getPermissionId(), permRole);
195 int statusCode = res.getStatus();
197 if (logger.isDebugEnabled()) {
198 logger.debug(testName + ": status = " + statusCode);
200 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
201 invalidStatusCodeMessage(testRequestType, statusCode));
202 Assert.assertEquals(statusCode, testExpectedStatusCode);
204 knownResourceId = extractId(res); //This is meaningless in this test, see getKnowResourceId() method for details
205 if (logger.isDebugEnabled()) {
206 logger.debug(testName + ": knownResourceId=" + knownResourceId);
215 //to not cause uniqueness violation for permRole, createList is removed
217 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
220 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
221 // dependsOnMethods = {"create"})
222 public void createList(String testName) throws Exception {
223 //Should this really be empty?
227 // Placeholders until the three tests below can be uncommented.
228 // See Issue CSPACE-401.
230 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
233 public void createWithEmptyEntityBody(String testName) throws Exception {
234 //Should this really be empty?
238 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
241 public void createWithMalformedXml(String testName) throws Exception {
242 //Should this really be empty?
246 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
249 public void createWithWrongXmlSchema(String testName) throws Exception {
250 //Should this really be empty?
253 // ---------------------------------------------------------------
254 // CRUD tests : READ tests
255 // ---------------------------------------------------------------
258 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
261 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
262 // dependsOnMethods = {"create"})
263 public void read(String testName) throws Exception {
267 // Submit the request to the service and store the response.
268 PermissionRoleClient client = new PermissionRoleClient();
269 ClientResponse<PermissionRole> res = null;
272 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
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(testRequestType.isValidStatusCode(statusCode),
281 invalidStatusCodeMessage(testRequestType, statusCode));
282 Assert.assertEquals(statusCode, testExpectedStatusCode);
284 PermissionRole output = (PermissionRole) res.getEntity();
285 Assert.assertNotNull(output);
288 res.releaseConnection();
296 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
299 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
300 public void readNonExistent(String testName) throws Exception {
302 setupReadNonExistent();
304 // Submit the request to the service and store the response.
305 PermissionRoleClient client = new PermissionRoleClient();
306 ClientResponse<PermissionRole> res = null;
308 res = client.read(NON_EXISTENT_ID);
309 int statusCode = res.getStatus();
311 // Check the status code of the response: does it match
312 // the expected response(s)?
313 if (logger.isDebugEnabled()) {
314 logger.debug(testName + ": status = " + statusCode);
316 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
317 invalidStatusCodeMessage(testRequestType, statusCode));
318 Assert.assertEquals(statusCode, testExpectedStatusCode);
321 res.releaseConnection();
326 @Test(dataProvider = "testName",
327 dependsOnMethods = {"CRUDTests"})
328 public void readNoRelationship(String testName) throws Exception {
332 // Submit the request to the service and store the response.
333 PermissionRoleClient client = new PermissionRoleClient();
334 ClientResponse<PermissionRole> res = null;
337 permValues.get(TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX).getPermissionId());
338 // Check the status code of the response: does it match
339 // the expected response(s)?
340 assertStatusCode(res, testName);
341 PermissionRole output = (PermissionRole) res.getEntity();
343 String sOutput = objectAsXmlString(output, PermissionRole.class);
344 if (logger.isDebugEnabled()) {
345 logger.debug(testName + " received " + sOutput);
349 res.releaseConnection();
354 // ---------------------------------------------------------------
355 // CRUD tests : READ_LIST tests
356 // ---------------------------------------------------------------
359 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
363 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
364 // dependsOnMethods = {"createList", "read"})
365 public void readList(String testName) throws Exception {
366 //Should this really be empty?
371 // ---------------------------------------------------------------
372 // CRUD tests : UPDATE tests
373 // ---------------------------------------------------------------
376 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
379 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
380 // dependsOnMethods = {"read", "readList", "readNonExistent"})
381 public void update(String testName) throws Exception {
382 //Should this really be empty?
386 // Placeholders until the three tests below can be uncommented.
387 // See Issue CSPACE-401.
389 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
392 public void updateWithEmptyEntityBody(String testName) throws Exception {
393 //Should this really be empty?
397 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
400 public void updateWithMalformedXml(String testName) throws Exception {
401 //Should this really be empty?
405 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
408 public void updateWithWrongXmlSchema(String testName) throws Exception {
409 //Should this really be empty?
413 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
416 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
417 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
418 public void updateNonExistent(String testName) throws Exception {
419 //Should this really be empty?
422 // ---------------------------------------------------------------
423 // CRUD tests : DELETE tests
424 // ---------------------------------------------------------------
427 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
430 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
431 // dependsOnMethods = {"read"})
432 public void delete(String testName) throws Exception {
439 PermissionRoleClient client = new PermissionRoleClient();
440 ClientResponse<PermissionRole> readResponse = client.read(
441 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
442 PermissionRole toDelete = null;
444 toDelete = readResponse.getEntity();
446 readResponse.releaseConnection();
449 ClientResponse<Response> res = client.delete(
450 toDelete.getPermission().get(0).getPermissionId(), toDelete);
452 int statusCode = res.getStatus();
453 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
454 invalidStatusCodeMessage(testRequestType, statusCode));
455 Assert.assertEquals(statusCode, testExpectedStatusCode);
458 res.releaseConnection();
463 // recreate 'TEST_SERVICE_NAME + TEST_MARKER' account and roles
469 // Lookup a know permission, and delete all of its role relationships
471 readResponse = client.read(
472 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
475 toDelete = readResponse.getEntity();
477 readResponse.releaseConnection();
480 Response deleteRes = client.delete(toDelete.getPermission().get(0).getPermissionId());
482 int statusCode = deleteRes.getStatus();
483 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
484 invalidStatusCodeMessage(testRequestType, statusCode));
485 Assert.assertEquals(statusCode, testExpectedStatusCode);
494 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
497 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
498 public void deleteNonExistent(String testName) throws Exception {
499 //ignoring this test as the service side returns 200 now even if it does
500 //not find a record in the db
503 // ---------------------------------------------------------------
505 // ---------------------------------------------------------------
508 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
509 public void searchWorkflowDeleted(String testName) throws Exception {
510 // Fixme: null test for now, overriding test in base class
514 protected String getKnowResourceId() {
515 return permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId();
518 // ---------------------------------------------------------------
519 // Utility methods used by tests above
520 // ---------------------------------------------------------------
522 * create permRolerole instance
523 * @param pv permissionvalue
524 * @param rvs rolevalue array
527 * @return PermissionRole
529 public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
530 Collection<RoleValue> rvs,
534 List<RoleValue> rvls = new ArrayList<RoleValue>();
536 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
537 pv, rvls, usePermId, useRoleId);
538 if (logger.isDebugEnabled()) {
539 logger.debug("to be created, permRole");
540 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
548 @AfterClass(alwaysRun = true)
550 public void cleanUp() {
552 String noTest = System.getProperty("noTestCleanup");
553 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
554 if (logger.isDebugEnabled()) {
555 logger.debug("Skipping Cleanup phase ...");
559 if (logger.isDebugEnabled()) {
560 logger.debug("Cleaning up temporary resources created for testing ...");
563 for (PermissionValue pv : permValues.values()) {
564 deletePermission(pv.getPermissionId());
566 for (RoleValue rv : roleValues.values()) {
567 deleteRole(rv.getRoleId());
572 * Creates the permission.
574 * @param resName the res name
575 * @param effect the effect
578 private String createPermission(String resName, EffectType effect) {
579 if (logger.isDebugEnabled()) {
580 logger.debug(getTestBanner("createPermission"));
583 PermissionClient permClient = new PermissionClient();
584 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
585 Permission permission = PermissionFactory.createPermissionInstance(resName,
586 "default permissions for " + resName,
587 actions, effect, true, true, true);
589 ClientResponse<Response> res = null;
591 res = permClient.create(permission);
593 int statusCode = res.getStatus();
594 if (logger.isDebugEnabled()) {
595 logger.debug("createPermission: resName=" + resName
596 + " status = " + statusCode);
598 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
599 invalidStatusCodeMessage(testRequestType, statusCode));
600 Assert.assertEquals(statusCode, testExpectedStatusCode);
604 res.releaseConnection();
613 * @param permId the perm id
615 private void deletePermission(String permId) {
616 if (logger.isDebugEnabled()) {
617 logger.debug(getTestBanner("deletePermission"));
620 PermissionClient permClient = new PermissionClient();
621 Response res = permClient.delete(permId);
623 int statusCode = res.getStatus();
624 if (logger.isDebugEnabled()) {
625 logger.debug("deletePermission: delete permission id="
626 + permId + " status=" + statusCode);
628 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
629 invalidStatusCodeMessage(testRequestType, statusCode));
630 Assert.assertEquals(statusCode, testExpectedStatusCode);
640 * @param roleName the role name
643 private String createRole(String roleName) {
644 if (logger.isDebugEnabled()) {
645 logger.debug(getTestBanner("createRole"));
648 RoleClient roleClient = new RoleClient();
650 Role role = RoleFactory.createRoleInstance(roleName,
651 roleName, //the display name
652 "role for " + roleName, true);
653 ClientResponse<Response> res = null;
656 res = roleClient.create(role);
657 int statusCode = res.getStatus();
658 if (logger.isDebugEnabled()) {
659 logger.debug("createRole: name=" + roleName
660 + " status = " + statusCode);
662 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
663 invalidStatusCodeMessage(testRequestType, statusCode));
664 Assert.assertEquals(statusCode, testExpectedStatusCode);
668 res.releaseConnection();
676 * @param roleId the role id
678 private void deleteRole(String roleId) {
679 if (logger.isDebugEnabled()) {
680 logger.debug(getTestBanner("deleteRole"));
683 RoleClient roleClient = new RoleClient();
684 Response res = roleClient.delete(roleId);
686 int statusCode = res.getStatus();
687 if (logger.isDebugEnabled()) {
688 logger.debug("deleteRole: delete role id=" + roleId
689 + " status=" + statusCode);
691 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
692 invalidStatusCodeMessage(testRequestType, statusCode));
693 Assert.assertEquals(statusCode, testExpectedStatusCode);
700 protected PermissionRole createInstance(String commonPartName,
702 // TODO Auto-generated method stub
707 protected PermissionRole updateInstance(PermissionRole commonPartObject) {
708 // TODO Auto-generated method stub
713 protected void compareUpdatedInstances(PermissionRole original,
714 PermissionRole updated) throws Exception {
715 // TODO Auto-generated method stub
720 protected Class<PermissionRole> getCommonListType() {
721 return PermissionRole.class;
725 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
726 * refer to this method in their @Test annotation declarations.
729 @Test(dataProvider = "testName",
731 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
732 public void CRUDTests(String testName) {
733 // Do nothing. Simply here to for a TestNG execution order for our tests
737 protected long getSizeOfList(PermissionRole list) {
738 // TODO Auto-generated method stub
739 throw new UnsupportedOperationException("Method getSizeOfList() is not implemented because this service does not support lists.");