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 permissions and
21 * limitations under the License.
23 package org.collectionspace.services.authorization.client.test;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Random;
28 import javax.ws.rs.core.Response;
30 import org.collectionspace.services.client.CollectionSpaceClient;
31 import org.collectionspace.services.client.PermissionClient;
32 import org.collectionspace.services.client.RoleClient;
33 import org.collectionspace.services.authorization.PermissionValue;
34 import org.collectionspace.services.authorization.Role;
35 import org.collectionspace.services.authorization.RolesList;
36 import org.collectionspace.services.authorization.perms.Permission;
37 import org.collectionspace.services.client.RoleFactory;
38 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
40 import org.testng.Assert;
41 import org.testng.annotations.AfterClass;
42 import org.testng.annotations.BeforeClass;
43 import org.testng.annotations.Test;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * RoleServiceTest, carries out tests against a
50 * deployed and running Role Service.
52 * $LastChangedRevision: 917 $
53 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
55 public class RoleServiceTest extends AbstractServiceTestImpl<RolesList, Role, Role, Role> {
58 private final static String CLASS_NAME = RoleServiceTest.class.getName();
59 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
61 // Used to create unique identifiers
62 static private final Random random = new Random(System.currentTimeMillis());
64 private static final String PERM_1_RL_RESOURCE = "xROLE_TEST_PERMVALUE_RESOURCE_1";
65 private static final String PERM_1_RL_ACTIONGROUP = "RL";
66 private static final String PERM_2_RL_RESOURCE = "xROLE_TEST_PERMVALUE_RESOURCE_2";
67 private static final String PERM_2_RL_ACTIONGROUP = "CRUL";
68 private static final String PERM_3_RL_RESOURCE = "xROLE_TEST_PERMVALUE_RESOURCE_3";
69 private static final String PERM_3_RL_ACTIONGROUP = "CRUDL";
72 // Instance variables specific to this test.
73 /** The known resource id. */
74 private String knownRoleName = "xROLE_USERS_MOCK-1";
75 private String knownRoleDisplayName = "xROLE_DISPLAYNAME_USERS_MOCK-1";
76 private String verifyResourceId = null;
77 private String verifyRoleName = "collections_manager_mock-1";
81 private List<PermissionValue> permissionValues = RoleFactory.EMPTY_PERMVALUE_LIST;
84 public String getServiceName() {
85 return RoleClient.SERVICE_NAME;
89 protected String getServicePathComponent() throws Exception {
90 return new RoleClient().getServicePathComponent();
94 * The entity type expected from the JAX-RS Response object
96 public Class<Role> getEntityResponseType() {
101 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
104 protected CollectionSpaceClient getClientInstance() throws Exception {
105 return new RoleClient();
109 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
110 return new RoleClient(clientPropertiesFilename);
117 @BeforeClass(alwaysRun = true)
118 public void seedData() throws Exception {
119 permissionValues = new ArrayList<PermissionValue>();
120 permissionValues.add(createPermissionValueInstance(PERM_1_RL_RESOURCE, PERM_1_RL_ACTIONGROUP));
121 permissionValues.add(createPermissionValueInstance(PERM_2_RL_RESOURCE, PERM_2_RL_ACTIONGROUP));
122 permissionValues.add(createPermissionValueInstance(PERM_3_RL_RESOURCE, PERM_3_RL_ACTIONGROUP));
125 private PermissionValue createPermissionValueInstance(String resource, String actionGroup) {
126 PermissionValue permValue = new PermissionValue();
127 permValue.setResourceName(resource);
128 permValue.setActionGroup(actionGroup);
136 @AfterClass(alwaysRun = true)
138 public void cleanUp() throws Exception {
139 String noTest = System.getProperty("noTestCleanup");
140 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
141 if (logger.isDebugEnabled()) {
142 logger.debug("Skipping Cleanup phase ...");
146 if (logger.isDebugEnabled()) {
147 logger.debug("Cleaning up temporary resources created for testing ...");
150 // Delete the permissions we indirectly created with when we created via the "createRoleWithPerms()" test
152 for (PermissionValue pv : permissionValues) {
153 deletePermission(pv);
157 // Call our parent cleanup method.
164 * Use a resource name and action group value to find and delete a permission record/resource.
166 private void deletePermission(PermissionValue permissionValue) throws Exception {
167 int statusCode = Response.Status.OK.getStatusCode();
168 PermissionClient client = new PermissionClient();
169 Permission permission = client.read(permissionValue.getResourceName(), permissionValue.getActionGroup());
170 if (permission != null) {
171 Response res = client.delete(permission.getCsid());
173 statusCode = res.getStatus();
179 // Something bad happened.
181 statusCode = Response.Status.BAD_REQUEST.getStatusCode();
184 if (statusCode != Response.Status.OK.getStatusCode()) {
185 String msg = String.format("Could not delete test Permission record: resource name='%s', actionGroup='%s'.",
186 permissionValue.getResourceName(), permissionValue.getActionGroup());
192 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
195 public void readPaginatedList(String testName) throws Exception {
196 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
199 // ---------------------------------------------------------------
200 // CRUD tests : CREATE tests
201 // ---------------------------------------------------------------
205 public void create(String testName) throws Exception {
206 // Perform setup, such as initializing the type of service request
207 // (e.g. CREATE, DELETE), its valid and expected status codes, and
208 // its associated HTTP method name (e.g. POST, DELETE).
211 // Submit the request to the service and store the response.
212 RoleClient client = new RoleClient();
213 Role role = createRoleInstance(knownRoleName, "All users are required to be in this role",
214 true, RoleFactory.EMPTY_PERMVALUE_LIST);
215 Response res = client.create(role);
217 int statusCode = res.getStatus();
218 // Check the status code of the response: does it match
219 // the expected response(s)?
222 // Does it fall within the set of valid status codes?
223 // Does it exactly match the expected status code?
224 if (logger.isDebugEnabled()) {
225 logger.debug(testName + ": status = " + statusCode);
227 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
228 invalidStatusCodeMessage(testRequestType, statusCode));
229 Assert.assertEquals(statusCode, testExpectedStatusCode);
231 // Store the ID returned from this create operation
232 // for additional tests below.
233 knownResourceId = extractId(res);
234 //allResourceIdsCreated.add(knownResourceId); //FIXME:
235 if (logger.isDebugEnabled()) {
236 logger.debug(testName + ": knownResourceId=" + knownResourceId);
243 @Test(dataProvider = "testName",
244 dependsOnMethods = {"CRUDTests"})
245 public void createRoleWithPerms(String testName) throws Exception {
246 // Perform setup, such as initializing the type of service request
247 // (e.g. CREATE, DELETE), its valid and expected status codes, and
248 // its associated HTTP method name (e.g. POST, DELETE).
251 // Submit the request to the service and store the response.
252 RoleClient client = new RoleClient();
253 Role role = createRoleInstance("CREATE_ROLE_WITH_PERMS" + System.currentTimeMillis(), "A role created with perms.",
254 true, permissionValues);
255 Response res = client.create(role);
257 int statusCode = res.getStatus();
258 // Check the status code of the response: does it match
259 // the expected response(s)?
262 // Does it fall within the set of valid status codes?
263 // Does it exactly match the expected status code?
264 if (logger.isDebugEnabled()) {
265 logger.debug(testName + ": status = " + statusCode);
267 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
268 invalidStatusCodeMessage(testRequestType, statusCode));
269 Assert.assertEquals(statusCode, testExpectedStatusCode);
271 // Store the ID returned from this create operation
272 // for additional tests below.
273 String csid = extractId(res);
274 allResourceIdsCreated.add(csid);
275 if (logger.isDebugEnabled()) {
276 logger.debug(testName + ": role with perms ID=" + csid);
283 @Test(dataProvider = "testName",
284 dependsOnMethods = {"CRUDTests"})
285 public void createDupliateRole(String testName) throws Exception {
286 // Perform setup, such as initializing the type of service request
287 // (e.g. CREATE, DELETE), its valid and expected status codes, and
288 // its associated HTTP method name (e.g. POST, DELETE).
291 // Submit the request to the service and store the response.
292 RoleClient client = new RoleClient();
293 Role role = createRoleInstance(knownRoleName, "This is a duplicate of an existing role.",
294 true, RoleFactory.EMPTY_PERMVALUE_LIST);
295 Response res = client.create(role);
297 int statusCode = res.getStatus();
298 if (statusCode == 201) {
299 // We expected NOT to create a new role, so if we did then we need to keep track of it and eventually delete it.
300 String duplicateRole = extractId(res);
301 allResourceIdsCreated.add(duplicateRole);
303 // Check the status code of the response: does it match
304 // the expected response(s)?
307 // Does it exactly match the expected status code?
308 if (logger.isDebugEnabled()) {
309 logger.debug(testName + ": status = " + statusCode);
311 Assert.assertEquals(statusCode, testExpectedStatusCode);
318 @Test(dataProvider = "testName",
319 dependsOnMethods = {"CRUDTests"})
320 public void createWithDisplayname(String testName) throws Exception {
321 // Perform setup, such as initializing the type of service request
322 // (e.g. CREATE, DELETE), its valid and expected status codes, and
323 // its associated HTTP method name (e.g. POST, DELETE).
326 // Submit the request to the service and store the response.
327 RoleClient client = new RoleClient();
328 Role role = createRoleInstance(knownRoleName + "_" + knownRoleDisplayName,
329 "all users are required to be in this role", true);
330 role.setDisplayName(knownRoleDisplayName);
331 Response res = client.create(role);
333 int statusCode = res.getStatus();
334 // Check the status code of the response: does it match
335 // the expected response(s)?
338 // Does it fall within the set of valid status codes?
339 // Does it exactly match the expected status code?
340 if (logger.isDebugEnabled()) {
341 logger.debug(testName + ": status = " + statusCode);
343 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
344 invalidStatusCodeMessage(testRequestType, statusCode));
345 Assert.assertEquals(statusCode, testExpectedStatusCode);
347 // Store the ID returned from this create operation
348 // for additional tests below.
349 String csid = extractId(res);
350 allResourceIdsCreated.add(csid);
351 if (logger.isDebugEnabled()) {
352 logger.debug(testName + ": csid=" + csid);
361 * Creates the for unique role.
363 * @param testName the test name
364 * @throws Exception the exception
366 @Test(dataProvider = "testName",
367 dependsOnMethods = {"CRUDTests"})
368 public void createForUniqueRole(String testName) throws Exception {
371 // Submit the request to the service and store the response.
372 RoleClient client = new RoleClient();
373 Role role = createRoleInstance(knownRoleName,
375 Response res = client.create(role);
377 int statusCode = res.getStatus();
379 if (logger.isDebugEnabled()) {
380 logger.debug(testName + ": Role with name \"" +
381 knownRoleName + "\" should already exist, so this request should fail.");
382 logger.debug(testName + ": status = " + statusCode);
383 logger.debug(testName + ": " + res);
385 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
386 invalidStatusCodeMessage(testRequestType, statusCode));
387 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
394 * Creates the for unique display name of role.
396 * @param testName the test name
397 * @throws Exception the exception
399 @Test(dataProvider = "testName",
400 dependsOnMethods = {"createWithDisplayname"})
401 public void createForUniqueDisplayRole(String testName) throws Exception {
404 // Submit the request to the service and store the response.
405 RoleClient client = new RoleClient();
406 Role role = createRoleInstance(knownRoleName + createIdentifier(),
407 "role users with non-unique display name",
409 role.setDisplayName(knownRoleDisplayName);
410 Response res = client.create(role);
412 int statusCode = res.getStatus();
414 if (logger.isDebugEnabled()) {
415 logger.debug(testName + ": Role with name \"" +
416 knownRoleName + "\" should already exist, so this request should fail.");
417 logger.debug(testName + ": status = " + statusCode);
418 logger.debug(testName + ": " + res);
420 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
421 invalidStatusCodeMessage(testRequestType, statusCode));
422 if (statusCode != Response.Status.BAD_REQUEST.getStatusCode()) {
423 // If the test fails then we've just created a Role that we need to delete, so
424 // store the ID returned from this create operation.
425 String csid = extractId(res);
426 allResourceIdsCreated.add(csid);
427 if (logger.isDebugEnabled()) {
428 logger.debug(testName + ": csid=" + csid);
430 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
437 protected String createIdentifier() {
438 long identifier = System.currentTimeMillis() + random.nextInt();
439 return Long.toString(identifier);
443 * Creates the without role name.
445 * @param testName the test name
446 * @throws Exception the exception
448 @Test(dataProvider = "testName",
449 dependsOnMethods = {"CRUDTests"})
450 public void createWithoutRoleName(String testName) throws Exception {
453 // Submit the request to the service and store the response.
454 RoleClient client = new RoleClient();
455 Role role = createRoleInstance("",
458 Response res = client.create(role);
460 int statusCode = res.getStatus();
461 // Does it exactly match the expected status code?
462 if (logger.isDebugEnabled()) {
463 logger.debug(testName + ": status = " + statusCode);
465 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
466 invalidStatusCodeMessage(testRequestType, statusCode));
467 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
473 //to not cause uniqueness violation for role, createList is removed
475 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
478 // @Test(dataProvider = "testName",
479 // dependsOnMethods = {"createWithDisplayname"})
480 public void createList(String testName) throws Exception {
483 // Submit the request to the service and store the response.
484 RoleClient client = new RoleClient();
485 //create a role with lowercase role name without role prefix
486 //the service should make it upper case and add the role prefix
487 Role role1 = createRoleInstance(verifyRoleName,
488 "collection manager", true);
489 Response res = client.create(role1);
491 int statusCode = res.getStatus();
492 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
493 invalidStatusCodeMessage(testRequestType, statusCode));
494 Assert.assertEquals(statusCode, testExpectedStatusCode);
495 verifyResourceId = extractId(res);
496 allResourceIdsCreated.add(verifyResourceId);
501 Role role2 = createRoleInstance("xROLE_COLLECTIONS_CURATOR_TEST",
502 "collections curator", true);
503 res = client.create(role2);
505 int statusCode = res.getStatus();
506 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
507 invalidStatusCodeMessage(testRequestType, statusCode));
508 Assert.assertEquals(statusCode, testExpectedStatusCode);
509 Assert.assertEquals(statusCode, testExpectedStatusCode);
510 allResourceIdsCreated.add(extractId(res));
515 Role role3 = createRoleInstance("xROLE_MOVINGIMAGE_ADMIN_TEST",
516 "moving image admin", true);
517 res = client.create(role3);
519 int statusCode = res.getStatus();
520 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
521 invalidStatusCodeMessage(testRequestType, statusCode));
522 Assert.assertEquals(statusCode, testExpectedStatusCode);
523 Assert.assertEquals(statusCode, testExpectedStatusCode);
524 allResourceIdsCreated.add(extractId(res));
531 // Placeholders until the three tests below can be uncommented.
532 // See Issue CSPACE-401.
534 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
537 public void createWithEmptyEntityBody(String testName) throws Exception {
538 //FIXME: Should this test really be empty? If so, please comment accordingly.
542 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
545 public void createWithMalformedXml(String testName) throws Exception {
546 //FIXME: Should this test really be empty? If so, please comment accordingly.
550 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
553 public void createWithWrongXmlSchema(String testName) throws Exception {
554 //FIXME: Should this test really be empty? If so, please comment accordingly.
557 // ---------------------------------------------------------------
558 // CRUD tests : READ tests
559 // ---------------------------------------------------------------
562 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
565 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
566 // dependsOnMethods = {"createForUniqueRole"})
567 public void read(String testName) throws Exception {
571 // Submit the request to the service and store the response.
572 RoleClient client = new RoleClient();
573 Response res = client.read(knownResourceId);
575 int statusCode = res.getStatus();
577 // Check the status code of the response: does it match
578 // the expected response(s)?
579 if (logger.isDebugEnabled()) {
580 logger.debug(testName + ": status = " + statusCode);
582 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
583 invalidStatusCodeMessage(testRequestType, statusCode));
584 Assert.assertEquals(statusCode, testExpectedStatusCode);
586 Role output = res.readEntity(Role.class);
587 Assert.assertNotNull(output);
593 @Test(dataProvider = "testName",
594 dependsOnMethods = {"CRUDTests"})
595 public void readToVerify(String testName) throws Exception {
599 // Submit the request to the service and store the response.
600 RoleClient client = new RoleClient();
601 Response res = client.read(verifyResourceId);
603 int statusCode = res.getStatus();
605 // Check the status code of the response: does it match
606 // the expected response(s)?
607 if (logger.isDebugEnabled()) {
608 logger.debug(testName + ": status = " + statusCode);
610 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
611 invalidStatusCodeMessage(testRequestType, statusCode));
612 Assert.assertEquals(statusCode, testExpectedStatusCode);
614 Role output = res.readEntity(Role.class);
615 Assert.assertNotNull(output);
617 //FIXME: Tenant ID of "1" should not be hard coded
618 String roleNameToVerify = RoleClient.BACKEND_ROLE_PREFIX +
620 verifyRoleName.toUpperCase();
621 Assert.assertEquals(output.getRoleName(), roleNameToVerify,
622 "RoleName fix did not work!");
629 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
633 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
634 // dependsOnMethods = {"read"})
635 public void readNonExistent(String testName) throws Exception {
637 setupReadNonExistent();
639 // Submit the request to the service and store the response.
640 RoleClient client = new RoleClient();
641 Response res = client.read(NON_EXISTENT_ID);
643 int statusCode = res.getStatus();
644 // Check the status code of the response: does it match
645 // the expected response(s)?
646 if (logger.isDebugEnabled()) {
647 logger.debug(testName + ": status = " + statusCode);
649 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
650 invalidStatusCodeMessage(testRequestType, statusCode));
651 Assert.assertEquals(statusCode, testExpectedStatusCode);
657 // ---------------------------------------------------------------
658 // CRUD tests : READ_LIST tests
659 // ---------------------------------------------------------------
662 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
665 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
666 // dependsOnMethods = {"createList", "read"})
667 public void readList(String testName) throws Exception {
671 // Submit the request to the service and store the response.
672 RoleClient client = new RoleClient();
673 Response res = client.readList();
675 // Check the status code of the response: does it match
676 // the expected response(s)?
677 assertStatusCode(res, testName);
678 RolesList list = res.readEntity(RolesList.class);
679 // Optionally output additional data about list members for debugging.
680 boolean iterateThroughList = true;
681 if (iterateThroughList && logger.isDebugEnabled()) {
682 printList(testName, list);
694 * @param testName the test name
695 * @throws Exception the exception
697 @Test(dataProvider = "testName",
698 dependsOnMethods = {"CRUDTests"})
699 public void searchRoleName(String testName) throws Exception {
703 // Submit the request to the service and store the response.
704 RoleClient client = new RoleClient();
705 Response res = client.readSearchList("movingImage");
707 assertStatusCode(res, testName);
708 RolesList list = res.readEntity(RolesList.class);
709 int EXPECTED_ITEMS = 1;
710 if (logger.isDebugEnabled()) {
711 logger.debug(testName + ": received = " + list.getRole().size()
712 + " expected=" + EXPECTED_ITEMS);
714 Assert.assertEquals(EXPECTED_ITEMS, list.getRole().size());
715 // Optionally output additional data about list members for debugging.
716 boolean iterateThroughList = true;
717 if (iterateThroughList && logger.isDebugEnabled()) {
718 printList(testName, list);
729 // ---------------------------------------------------------------
730 // CRUD tests : UPDATE tests
731 // ---------------------------------------------------------------
734 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
737 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
738 // dependsOnMethods = {"read", "readList", "readNonExistent"})
739 public void update(String testName) throws Exception {
743 Role roleToUpdate = new Role();
744 roleToUpdate.setCsid(knownResourceId);
745 roleToUpdate.setRoleName(knownRoleName);
746 roleToUpdate.setDisplayName(knownRoleName);
748 // Update the content of this resource.
749 roleToUpdate.setDescription("updated role description");
750 if (logger.isDebugEnabled()) {
751 logger.debug("updated object");
752 org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();
753 logger.debug(objectAsXmlString(objectFactory.createRole(roleToUpdate),
756 RoleClient client = new RoleClient();
757 // Submit the request to the service and store the response.
758 Response res = client.update(knownResourceId, roleToUpdate);
760 // Check the status code of the response: does it match the expected response(s)?
761 int statusCode = res.getStatus();
762 if (logger.isDebugEnabled()) {
763 logger.debug(testName + ": status = " + statusCode);
765 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
766 invalidStatusCodeMessage(testRequestType, statusCode));
767 Assert.assertEquals(statusCode, testExpectedStatusCode);
770 Role roleUpdated = res.readEntity(Role.class);
771 Assert.assertNotNull(roleUpdated);
773 Assert.assertEquals(roleUpdated.getDescription(),
774 roleToUpdate.getDescription(),
775 "Data in updated object did not match submitted data.");
781 @Test(dataProvider = "testName",
782 dependsOnMethods = {"CRUDTests"})
783 public void verifyProtectionReadOnly(String testName) throws Exception {
784 // Setup to create a new role
786 Role role = createRoleInstance(knownRoleName+"_PT", "Just a temp", true);
787 role.setMetadataProtection(RoleClient.IMMUTABLE);
788 role.setPermsProtection(RoleClient.IMMUTABLE);
790 // Submit the create request to the service and store the response.
792 RoleClient client = new RoleClient();
793 Response res = client.create(role);
794 String testResourceId = null;
796 int statusCode = res.getStatus();
797 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
798 invalidStatusCodeMessage(testRequestType, statusCode));
799 Assert.assertEquals(statusCode, testExpectedStatusCode);
800 // Store the ID returned from this create operation
801 // for additional tests below.
802 testResourceId = extractId(res);
803 allResourceIdsCreated.add(testResourceId);
804 if (logger.isDebugEnabled()) {
805 logger.debug(testName + ": testResourceId=" + testResourceId);
811 // Next, read back the role we just created.
813 // Submit the request to the service and store the response.
814 Response roleRes = client.read(testResourceId);
816 int statusCode = roleRes.getStatus();
817 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
818 invalidStatusCodeMessage(testRequestType, statusCode));
819 Assert.assertEquals(statusCode, testExpectedStatusCode);
821 Role roleRead = roleRes.readEntity(Role.class);
822 Assert.assertNotNull(roleRead);
823 String mdProtection = roleRead.getMetadataProtection();
824 String permsProtection = roleRead.getPermsProtection();
825 if (logger.isTraceEnabled()) {
826 logger.trace(testName + ": metadataProtection=" + mdProtection);
827 logger.trace(testName + ": permsProtection=" + permsProtection);
829 Assert.assertFalse(role.getMetadataProtection().equals(mdProtection),
830 "Role allowed create to set the metadata protection flag.");
831 Assert.assertFalse(role.getPermsProtection().equals(permsProtection),
832 "Role allowed create to set the perms protection flag.");
836 // Finally, update the role with changes and verify
839 Role roleToUpdate = createRoleInstance(knownRoleName+"_PT", "Just a temp", true);
840 roleToUpdate.setMetadataProtection(RoleClient.IMMUTABLE);
841 roleToUpdate.setPermsProtection(RoleClient.IMMUTABLE);
843 // Submit the request to the service and store the response.
844 roleRes = client.update(testResourceId, roleToUpdate);
846 // Check the status code of the response: does it match the expected response(s)?
847 int statusCode = roleRes.getStatus();
848 if (logger.isDebugEnabled()) {
849 logger.debug(testName + ": status = " + statusCode);
851 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
852 invalidStatusCodeMessage(testRequestType, statusCode));
853 Assert.assertEquals(statusCode, testExpectedStatusCode);
856 Role roleUpdated = roleRes.readEntity(Role.class);
857 Assert.assertNotNull(roleUpdated);
858 if (logger.isDebugEnabled()) {
859 logger.debug(testName + "Updated role: ");
860 org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();
861 logger.debug(objectAsXmlString(objectFactory.createRole(roleUpdated),
865 Assert.assertFalse(RoleClient.IMMUTABLE.equalsIgnoreCase(roleUpdated.getMetadataProtection()),
866 "Role allowed update of the metadata protection flag.");
867 Assert.assertFalse(RoleClient.IMMUTABLE.equalsIgnoreCase(roleUpdated.getPermsProtection()),
868 "Role allowed update of the perms protection flag.");
874 @Test(dataProvider = "testName",
875 dependsOnMethods = {"CRUDTests"})
876 public void updateNotAllowed(String testName) throws Exception {
881 Role roleToUpdate = new Role();
882 roleToUpdate.setCsid(knownResourceId);
883 // Update the content of this resource.
884 roleToUpdate.setRoleName("UPDATED-ROLE_USERS_TEST");
885 roleToUpdate.setDisplayName("UPDATED-ROLE_USERS_TEST");
886 if (logger.isDebugEnabled()) {
887 logger.debug("updated object");
888 org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();
889 logger.debug(objectAsXmlString(objectFactory.createRole(roleToUpdate),
892 RoleClient client = new RoleClient();
893 // Submit the request to the service and store the response.
894 Response res = client.update(knownResourceId, roleToUpdate);
896 int statusCode = res.getStatus();
897 // Check the status code of the response: does it match the expected response(s)?
898 if (logger.isDebugEnabled()) {
899 logger.debug(testName + ": status = " + statusCode);
901 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
902 invalidStatusCodeMessage(testRequestType, statusCode));
903 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
910 // Placeholders until the three tests below can be uncommented.
911 // See Issue CSPACE-401.
913 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
916 public void updateWithEmptyEntityBody(String testName) throws Exception {
917 //FIXME: Should this test really be empty? If so, please comment accordingly.
921 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
924 public void updateWithMalformedXml(String testName) throws Exception {
925 //FIXME: Should this test really be empty? If so, please comment accordingly.
929 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
932 public void updateWithWrongXmlSchema(String testName) throws Exception {
933 //FIXME: Should this test really be empty? If so, please comment accordingly.
937 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
941 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
942 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
943 public void updateNonExistent(String testName) throws Exception {
945 setupUpdateNonExistent();
947 // Submit the request to the service and store the response.
949 // Note: The ID used in this 'create' call may be arbitrary.
950 // The only relevant ID may be the one used in updateRole(), below.
951 RoleClient client = new RoleClient();
952 Role role = createRoleInstance("xROLE_XXX",
955 Response res = client.update(NON_EXISTENT_ID, role);
957 int statusCode = res.getStatus();
959 // Check the status code of the response: does it match
960 // the expected response(s)?
961 if (logger.isDebugEnabled()) {
962 logger.debug(testName + ": status = " + statusCode);
964 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
965 invalidStatusCodeMessage(testRequestType, statusCode));
966 Assert.assertEquals(statusCode, testExpectedStatusCode);
972 // ---------------------------------------------------------------
973 // CRUD tests : DELETE tests
974 // ---------------------------------------------------------------
977 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
980 public void delete(String testName) throws Exception {
981 // Do nothing since other tests like "updateNotAllowed" need the "known resource" that this test
982 // deletes. Once all those tests get run, the "localDelete" method will call the base class' delete
983 // method that will delete the "known resource".
986 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
987 dependsOnMethods = {"updateNotAllowed", "createForUniqueRole", "createForUniqueDisplayRole"})
988 public void localDelete(String testName) throws Exception {
989 super.delete(testName);
994 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
997 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
998 // dependsOnMethods = {"delete"})
999 public void deleteNonExistent(String testName) throws Exception {
1001 setupDeleteNonExistent();
1003 // Submit the request to the service and store the response.
1004 RoleClient client = new RoleClient();
1005 Response res = client.delete(NON_EXISTENT_ID);
1007 int statusCode = res.getStatus();
1009 // Check the status code of the response: does it match
1010 // the expected response(s)?
1011 if (logger.isDebugEnabled()) {
1012 logger.debug(testName + ": status = " + statusCode);
1014 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
1015 invalidStatusCodeMessage(testRequestType, statusCode));
1016 Assert.assertEquals(statusCode, testExpectedStatusCode);
1022 // ---------------------------------------------------------------
1024 // ---------------------------------------------------------------
1027 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
1028 public void searchWorkflowDeleted(String testName) throws Exception {
1029 // Fixme: null test for now, overriding test in base class
1032 // ---------------------------------------------------------------
1033 // Utility tests : tests of code used in tests above
1034 // ---------------------------------------------------------------
1036 * Tests the code for manually submitting data that is used by several
1037 * of the methods above.
1041 // ---------------------------------------------------------------
1042 // Utility methods used by tests above
1043 // ---------------------------------------------------------------
1045 * create role instance
1047 * @param description
1048 * @param useRoleName
1051 public Role createRoleInstance(String roleName,
1053 boolean useRoleName,
1054 List<PermissionValue> permValueList) {
1056 Role role = RoleFactory.createRoleInstance(roleName,
1057 roleName, //the display name
1062 if (logger.isDebugEnabled()) {
1063 logger.debug("to be created, role");
1064 org.collectionspace.services.authorization.ObjectFactory objectFactory = new org.collectionspace.services.authorization.ObjectFactory();
1065 logger.debug(objectAsXmlString(objectFactory.createRole(role),
1072 public Role createRoleInstance(String roleName,
1074 boolean useRoleName) {
1075 return this.createRoleInstance(roleName, description, useRoleName, RoleFactory.EMPTY_PERMVALUE_LIST);
1081 * @param testName the test name
1082 * @param list the list
1085 protected void printList(String testName, RolesList list) {
1086 for (Role role : list.getRole()) {
1087 logger.debug(testName + " role csid=" + role.getCsid()
1088 + " name=" + role.getRoleName()
1089 + " desc=" + role.getDescription());
1094 protected Role createInstance(String commonPartName, String identifier) {
1095 // TODO Auto-generated method stub
1100 protected Role updateInstance(Role commonPartObject) {
1101 // TODO Auto-generated method stub
1106 protected void compareUpdatedInstances(Role original, Role updated)
1108 // TODO Auto-generated method stub
1113 protected Class<RolesList> getCommonListType() {
1114 // TODO Auto-generated method stub
1119 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
1120 * refer to this method in their @Test annotation declarations.
1123 @Test(dataProvider = "testName",
1124 dependsOnMethods = {
1125 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
1126 public void CRUDTests(String testName) {
1127 // Do nothing. Simply here to for a TestNG execution order for our tests
1131 protected long getSizeOfList(RolesList list) {
1132 return list.getRole().size();