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 javax.ws.rs.core.Response;
29 import org.collectionspace.services.client.CollectionSpaceClient;
30 import org.collectionspace.services.client.RoleClient;
31 import org.collectionspace.services.authorization.Role;
32 import org.collectionspace.services.authorization.RolesList;
33 import org.collectionspace.services.client.RoleFactory;
34 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
35 import org.collectionspace.services.client.test.ServiceRequestType;
36 import org.collectionspace.services.jaxb.AbstractCommonList;
37 import org.jboss.resteasy.client.ClientResponse;
39 import org.testng.Assert;
40 import org.testng.annotations.Test;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * RoleServiceTest, carries out tests against a
47 * deployed and running Role Service.
49 * $LastChangedRevision: 917 $
50 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
52 public class RoleServiceTest extends AbstractServiceTestImpl {
55 private final Logger logger =
56 LoggerFactory.getLogger(RoleServiceTest.class);
57 // Instance variables specific to this test.
58 /** The known resource id. */
59 private String knownResourceId = null;
60 private String knownRoleName = "ROLE_USERS_TEST";
61 private String verifyResourceId = null;
62 private String verifyRoleName = "collections_manager_test";
63 // private List<String> allResourceIdsCreated = new ArrayList<String>();
64 /** The add tenant. */
65 boolean addTenant = true;
67 * This method is called only by the parent class, AbstractServiceTestImpl
71 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
74 protected String getServicePathComponent() {
75 return new RoleClient().getServicePathComponent();
79 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
82 protected CollectionSpaceClient getClientInstance() {
83 return new RoleClient();
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
90 protected AbstractCommonList getAbstractCommonList(
91 ClientResponse<AbstractCommonList> response) {
92 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
93 throw new UnsupportedOperationException();
97 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
99 @Test(dataProvider = "testName")
101 public void readPaginatedList(String testName) throws Exception {
102 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
105 // ---------------------------------------------------------------
106 // CRUD tests : CREATE tests
107 // ---------------------------------------------------------------
110 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
113 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
114 public void create(String testName) throws Exception {
116 // Perform setup, such as initializing the type of service request
117 // (e.g. CREATE, DELETE), its valid and expected status codes, and
118 // its associated HTTP method name (e.g. POST, DELETE).
119 setupCreate(testName);
121 // Submit the request to the service and store the response.
122 RoleClient client = new RoleClient();
123 Role role = createRoleInstance(knownRoleName,
124 "all users are required to be in this role",
126 ClientResponse<Response> res = client.create(role);
127 int statusCode = res.getStatus();
129 // Check the status code of the response: does it match
130 // the expected response(s)?
133 // Does it fall within the set of valid status codes?
134 // Does it exactly match the expected status code?
135 if (logger.isDebugEnabled()) {
136 logger.debug(testName + ": status = " + statusCode);
138 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
139 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
140 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
142 // Store the ID returned from this create operation
143 // for additional tests below.
144 knownResourceId = extractId(res);
145 if (logger.isDebugEnabled()) {
146 logger.debug(testName + ": knownResourceId=" + knownResourceId);
151 * Creates the for unique role.
153 * @param testName the test name
154 * @throws Exception the exception
156 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
157 dependsOnMethods = {"create"})
158 public void createForUniqueRole(String testName) throws Exception {
160 setupCreate(testName);
162 // Submit the request to the service and store the response.
163 RoleClient client = new RoleClient();
164 Role role = createRoleInstance("ROLE_USERS",
167 ClientResponse<Response> res = client.create(role);
168 int statusCode = res.getStatus();
170 if (logger.isDebugEnabled()) {
171 logger.debug(testName + ": status = " + statusCode);
173 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
174 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
175 Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
179 * Creates the without role name.
181 * @param testName the test name
182 * @throws Exception the exception
184 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
185 dependsOnMethods = {"create"})
186 public void createWithoutRoleName(String testName) throws Exception {
188 setupCreate(testName);
190 // Submit the request to the service and store the response.
191 RoleClient client = new RoleClient();
192 Role role = createRoleInstance("ROLE_USERS",
195 ClientResponse<Response> res = client.create(role);
196 int statusCode = res.getStatus();
197 // Does it exactly match the expected status code?
198 if (logger.isDebugEnabled()) {
199 logger.debug(testName + ": status = " + statusCode);
201 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
202 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
203 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
206 //to not cause uniqueness violation for role, createList is removed
208 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
211 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
212 dependsOnMethods = {"create"})
213 public void createList(String testName) throws Exception {
215 setupCreate(testName);
217 // Submit the request to the service and store the response.
218 RoleClient client = new RoleClient();
219 //create a role with lowercase role name without role prefix
220 //the service should make it upper case and add the role prefix
221 Role role1 = createRoleInstance(verifyRoleName,
222 "collection manager",
224 ClientResponse<Response> res = client.create(role1);
225 int statusCode = res.getStatus();
226 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
227 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
228 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
229 verifyResourceId = extractId(res);
230 allResourceIdsCreated.add(verifyResourceId);
232 Role role2 = createRoleInstance("ROLE_COLLECTIONS_CURATOR_TEST",
233 "collections curator",
235 res = client.create(role2);
236 statusCode = res.getStatus();
237 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
238 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
239 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
240 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
241 allResourceIdsCreated.add(extractId(res));
243 Role role3 = createRoleInstance("ROLE_MOVINGIMAGE_ADMIN_TEST",
244 "moving image admin",
246 res = client.create(role3);
247 statusCode = res.getStatus();
248 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
249 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
250 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
251 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
252 allResourceIdsCreated.add(extractId(res));
256 // Placeholders until the three tests below can be uncommented.
257 // See Issue CSPACE-401.
259 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
262 public void createWithEmptyEntityBody(String testName) throws Exception {
263 //FIXME: Should this test really be empty? If so, please comment accordingly.
267 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
270 public void createWithMalformedXml(String testName) throws Exception {
271 //FIXME: Should this test really be empty? If so, please comment accordingly.
275 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
278 public void createWithWrongXmlSchema(String testName) throws Exception {
279 //FIXME: Should this test really be empty? If so, please comment accordingly.
282 // ---------------------------------------------------------------
283 // CRUD tests : READ tests
284 // ---------------------------------------------------------------
287 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
290 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
291 dependsOnMethods = {"create"})
292 public void read(String testName) throws Exception {
297 // Submit the request to the service and store the response.
298 RoleClient client = new RoleClient();
299 ClientResponse<Role> res = client.read(knownResourceId);
300 int statusCode = res.getStatus();
302 // Check the status code of the response: does it match
303 // the expected response(s)?
304 if (logger.isDebugEnabled()) {
305 logger.debug(testName + ": status = " + statusCode);
307 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
308 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
309 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
311 Role output = (Role) res.getEntity();
312 Assert.assertNotNull(output);
315 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
316 dependsOnMethods = {"createList"})
317 public void readToVerify(String testName) throws Exception {
322 // Submit the request to the service and store the response.
323 RoleClient client = new RoleClient();
324 ClientResponse<Role> res = client.read(verifyResourceId);
325 int statusCode = res.getStatus();
327 // Check the status code of the response: does it match
328 // the expected response(s)?
329 if (logger.isDebugEnabled()) {
330 logger.debug(testName + ": status = " + statusCode);
332 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
333 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
334 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
336 Role output = (Role) res.getEntity();
337 Assert.assertNotNull(output);
339 String roleNameToVerify = "ROLE_" + verifyRoleName.toUpperCase();
340 Assert.assertEquals(output.getRoleName(), roleNameToVerify,
341 "RoleName fix did not work!");
345 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
349 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
350 dependsOnMethods = {"read"})
351 public void readNonExistent(String testName) throws Exception {
354 setupReadNonExistent(testName);
356 // Submit the request to the service and store the response.
357 RoleClient client = new RoleClient();
358 ClientResponse<Role> res = client.read(NON_EXISTENT_ID);
359 int statusCode = res.getStatus();
361 // Check the status code of the response: does it match
362 // the expected response(s)?
363 if (logger.isDebugEnabled()) {
364 logger.debug(testName + ": status = " + statusCode);
366 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
367 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
368 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
371 // ---------------------------------------------------------------
372 // CRUD tests : READ_LIST tests
373 // ---------------------------------------------------------------
376 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
379 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
380 dependsOnMethods = {"createList", "read"})
381 public void readList(String testName) throws Exception {
384 setupReadList(testName);
386 // Submit the request to the service and store the response.
387 RoleClient client = new RoleClient();
388 ClientResponse<RolesList> res = client.readList();
389 RolesList list = res.getEntity();
390 int statusCode = res.getStatus();
392 // Check the status code of the response: does it match
393 // the expected response(s)?
394 if (logger.isDebugEnabled()) {
395 logger.debug(testName + ": status = " + statusCode);
397 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
398 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
399 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
401 // Optionally output additional data about list members for debugging.
402 boolean iterateThroughList = true;
403 if (iterateThroughList && logger.isDebugEnabled()) {
404 printList(testName, list);
411 * @param testName the test name
412 * @throws Exception the exception
414 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
415 dependsOnMethods = {"createList", "read"})
416 public void searchRoleName(String testName) throws Exception {
419 setupReadList(testName);
421 // Submit the request to the service and store the response.
422 RoleClient client = new RoleClient();
423 ClientResponse<RolesList> res = client.readSearchList("movingImage");
424 RolesList list = res.getEntity();
425 int statusCode = res.getStatus();
426 // Check the status code of the response: does it match
427 // the expected response(s)?
428 if (logger.isDebugEnabled()) {
429 logger.debug(testName + ": status = " + statusCode);
431 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
432 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
433 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
434 int EXPECTED_ITEMS = 1;
435 if (logger.isDebugEnabled()) {
436 logger.debug(testName + ": received = " + list.getRoles().size()
437 + " expected=" + EXPECTED_ITEMS);
439 Assert.assertEquals(EXPECTED_ITEMS, list.getRoles().size());
440 // Optionally output additional data about list members for debugging.
441 boolean iterateThroughList = true;
442 if (iterateThroughList && logger.isDebugEnabled()) {
443 printList(testName, list);
449 // ---------------------------------------------------------------
450 // CRUD tests : UPDATE tests
451 // ---------------------------------------------------------------
454 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
457 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
458 dependsOnMethods = {"read", "readList", "readNonExistent"})
459 public void update(String testName) throws Exception {
462 setupUpdate(testName);
464 Role roleToUpdate = new Role();
465 roleToUpdate.setCsid(knownResourceId);
466 roleToUpdate.setRoleName(knownRoleName);
468 // Update the content of this resource.
469 roleToUpdate.setDescription("updated role description");
470 if (logger.isDebugEnabled()) {
471 logger.debug("updated object");
472 logger.debug(objectAsXmlString(roleToUpdate,
475 RoleClient client = new RoleClient();
476 // Submit the request to the service and store the response.
477 ClientResponse<Role> res = client.update(knownResourceId, roleToUpdate);
478 int statusCode = res.getStatus();
479 // Check the status code of the response: does it match the expected response(s)?
480 if (logger.isDebugEnabled()) {
481 logger.debug(testName + ": status = " + statusCode);
483 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
484 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
485 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
488 Role roleUpdated = (Role) res.getEntity();
489 Assert.assertNotNull(roleUpdated);
491 Assert.assertEquals(roleUpdated.getDescription(),
492 roleToUpdate.getDescription(),
493 "Data in updated object did not match submitted data.");
496 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
497 dependsOnMethods = {"read", "readList", "readNonExistent"})
498 public void updateNotAllowed(String testName) throws Exception {
501 setupUpdate(testName);
503 Role roleToUpdate = new Role();
504 roleToUpdate.setCsid(knownResourceId);
505 // Update the content of this resource.
506 roleToUpdate.setRoleName("UPDATED-ROLE_USERS_TEST");
507 if (logger.isDebugEnabled()) {
508 logger.debug("updated object");
509 logger.debug(objectAsXmlString(roleToUpdate,
512 RoleClient client = new RoleClient();
513 // Submit the request to the service and store the response.
514 ClientResponse<Role> res = client.update(knownResourceId, roleToUpdate);
515 int statusCode = res.getStatus();
516 // Check the status code of the response: does it match the expected response(s)?
517 if (logger.isDebugEnabled()) {
518 logger.debug(testName + ": status = " + statusCode);
520 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
521 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
522 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
527 // Placeholders until the three tests below can be uncommented.
528 // See Issue CSPACE-401.
530 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
533 public void updateWithEmptyEntityBody(String testName) throws Exception {
534 //FIXME: Should this test really be empty? If so, please comment accordingly.
538 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
541 public void updateWithMalformedXml(String testName) throws Exception {
542 //FIXME: Should this test really be empty? If so, please comment accordingly.
546 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
549 public void updateWithWrongXmlSchema(String testName) throws Exception {
550 //FIXME: Should this test really be empty? If so, please comment accordingly.
554 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
557 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
558 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
559 public void updateNonExistent(String testName) throws Exception {
562 setupUpdateNonExistent(testName);
564 // Submit the request to the service and store the response.
566 // Note: The ID used in this 'create' call may be arbitrary.
567 // The only relevant ID may be the one used in updateRole(), below.
568 RoleClient client = new RoleClient();
569 Role role = createRoleInstance("ROLE_XXX",
572 ClientResponse<Role> res =
573 client.update(NON_EXISTENT_ID, role);
574 int statusCode = res.getStatus();
576 // Check the status code of the response: does it match
577 // the expected response(s)?
578 if (logger.isDebugEnabled()) {
579 logger.debug(testName + ": status = " + statusCode);
581 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
582 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
583 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
586 // ---------------------------------------------------------------
587 // CRUD tests : DELETE tests
588 // ---------------------------------------------------------------
591 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
594 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
595 dependsOnMethods = {"updateNotAllowed", "testSubmitRequest"})
596 public void delete(String testName) throws Exception {
599 setupDelete(testName);
601 // Submit the request to the service and store the response.
602 RoleClient client = new RoleClient();
603 ClientResponse<Response> res = client.delete(knownResourceId);
604 int statusCode = res.getStatus();
606 // Check the status code of the response: does it match
607 // the expected response(s)?
608 if (logger.isDebugEnabled()) {
609 logger.debug(testName + ": status = " + statusCode);
611 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
612 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
613 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
618 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
621 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
622 dependsOnMethods = {"delete"})
623 public void deleteNonExistent(String testName) throws Exception {
626 setupDeleteNonExistent(testName);
628 // Submit the request to the service and store the response.
629 RoleClient client = new RoleClient();
630 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
631 int statusCode = res.getStatus();
633 // Check the status code of the response: does it match
634 // the expected response(s)?
635 if (logger.isDebugEnabled()) {
636 logger.debug(testName + ": status = " + statusCode);
638 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
639 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
640 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
643 // ---------------------------------------------------------------
644 // Utility tests : tests of code used in tests above
645 // ---------------------------------------------------------------
647 * Tests the code for manually submitting data that is used by several
648 * of the methods above.
652 @Test(dependsOnMethods = {"create"})
653 public void testSubmitRequest() throws Exception {
655 // Expected status code: 200 OK
656 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
658 // Submit the request to the service and store the response.
659 String method = ServiceRequestType.READ.httpMethodName();
660 String url = getResourceURL(knownResourceId);
661 int statusCode = submitRequest(method, url);
663 // Check the status code of the response: does it match
664 // the expected response(s)?
665 if (logger.isDebugEnabled()) {
666 logger.debug("testSubmitRequest: url=" + url
667 + " status=" + statusCode);
669 Assert.assertEquals(statusCode, EXPECTED_STATUS);
673 // ---------------------------------------------------------------
674 // Utility methods used by tests above
675 // ---------------------------------------------------------------
677 * create role instance
683 public Role createRoleInstance(String roleName,
685 boolean useRoleName) {
687 Role role = RoleFactory.createRoleInstance(roleName, description,
689 if (logger.isDebugEnabled()) {
690 logger.debug("to be created, role");
691 logger.debug(objectAsXmlString(role, Role.class));
700 * @param testName the test name
701 * @param list the list
704 private int printList(String testName, RolesList list) {
708 for (Role role : list.getRoles()) {
709 logger.debug(testName + " role csid=" + role.getCsid()
710 + " name=" + role.getRoleName()
711 + " desc=" + role.getDescription());