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;
44 import org.testng.annotations.AfterClass;
47 * RoleServiceTest, carries out tests against a
48 * deployed and running Role Service.
50 * $LastChangedRevision: 917 $
51 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
53 public class RoleServiceTest extends AbstractServiceTestImpl {
55 private final Logger logger =
56 LoggerFactory.getLogger(RoleServiceTest.class);
57 // Instance variables specific to this test.
58 private String knownResourceId = null;
59 private List<String> allResourceIdsCreated = new ArrayList<String>();
60 boolean addTenant = true;
62 * This method is called only by the parent class, AbstractServiceTestImpl
66 protected String getServicePathComponent() {
67 return new RoleClient().getServicePathComponent();
71 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
74 protected CollectionSpaceClient getClientInstance() {
75 return new RoleClient();
79 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
82 protected AbstractCommonList getAbstractCommonList(
83 ClientResponse<AbstractCommonList> response) {
84 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
85 throw new UnsupportedOperationException();
88 @Test(dataProvider = "testName")
90 public void readPaginatedList(String testName) throws Exception {
91 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
94 // ---------------------------------------------------------------
95 // CRUD tests : CREATE tests
96 // ---------------------------------------------------------------
99 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
100 public void create(String testName) throws Exception {
102 // Perform setup, such as initializing the type of service request
103 // (e.g. CREATE, DELETE), its valid and expected status codes, and
104 // its associated HTTP method name (e.g. POST, DELETE).
105 setupCreate(testName);
107 // Submit the request to the service and store the response.
108 RoleClient client = new RoleClient();
109 Role role = createRoleInstance("ROLE_USERS_TEST",
110 "all users are required to be in this role",
112 ClientResponse<Response> res = client.create(role);
113 int statusCode = res.getStatus();
115 // Check the status code of the response: does it match
116 // the expected response(s)?
119 // Does it fall within the set of valid status codes?
120 // Does it exactly match the expected status code?
121 if (logger.isDebugEnabled()) {
122 logger.debug(testName + ": status = " + statusCode);
124 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
125 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
126 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
128 // Store the ID returned from this create operation
129 // for additional tests below.
130 knownResourceId = extractId(res);
131 if (logger.isDebugEnabled()) {
132 logger.debug(testName + ": knownResourceId=" + knownResourceId);
136 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
137 dependsOnMethods = {"create"})
138 public void createForUniqueRole(String testName) throws Exception {
140 setupCreate(testName);
142 // Submit the request to the service and store the response.
143 RoleClient client = new RoleClient();
144 Role role = createRoleInstance("ROLE_USERS",
147 ClientResponse<Response> res = client.create(role);
148 int statusCode = res.getStatus();
150 if (logger.isDebugEnabled()) {
151 logger.debug(testName + ": status = " + statusCode);
153 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
154 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
155 Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
158 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
159 dependsOnMethods = {"create"})
160 public void createWithoutRoleName(String testName) throws Exception {
162 setupCreate(testName);
164 // Submit the request to the service and store the response.
165 RoleClient client = new RoleClient();
166 Role role = createRoleInstance("ROLE_USERS",
169 ClientResponse<Response> res = client.create(role);
170 int statusCode = res.getStatus();
171 // Does it exactly match the expected status code?
172 if (logger.isDebugEnabled()) {
173 logger.debug(testName + ": status = " + statusCode);
175 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
176 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
177 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
180 //to not cause uniqueness violation for role, createList is removed
182 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
183 dependsOnMethods = {"create"})
184 public void createList(String testName) throws Exception {
186 setupCreate(testName);
188 // Submit the request to the service and store the response.
189 RoleClient client = new RoleClient();
190 Role role1 = createRoleInstance("ROLE_COLLECTIONS_MANGER_TEST",
191 "collection manager",
193 ClientResponse<Response> res = client.create(role1);
194 int statusCode = res.getStatus();
195 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
198 allResourceIdsCreated.add(extractId(res));
200 Role role2 = createRoleInstance("ROLE_COLLECTIONS_CURATOR_TEST",
201 "collections curator",
203 res = client.create(role2);
204 statusCode = res.getStatus();
205 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
206 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
207 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
208 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
209 allResourceIdsCreated.add(extractId(res));
211 Role role3 = createRoleInstance("ROLE_MOVINGIMAGE_ADMIN_TEST",
212 "moving image admin",
214 res = client.create(role3);
215 statusCode = res.getStatus();
216 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
217 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
218 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
219 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
220 allResourceIdsCreated.add(extractId(res));
224 // Placeholders until the three tests below can be uncommented.
225 // See Issue CSPACE-401.
227 public void createWithEmptyEntityBody(String testName) throws Exception {
228 //FIXME: Should this test really be empty? If so, please comment accordingly.
232 public void createWithMalformedXml(String testName) throws Exception {
233 //FIXME: Should this test really be empty? If so, please comment accordingly.
237 public void createWithWrongXmlSchema(String testName) throws Exception {
238 //FIXME: Should this test really be empty? If so, please comment accordingly.
241 // ---------------------------------------------------------------
242 // CRUD tests : READ tests
243 // ---------------------------------------------------------------
246 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
247 dependsOnMethods = {"create"})
248 public void read(String testName) throws Exception {
253 // Submit the request to the service and store the response.
254 RoleClient client = new RoleClient();
255 ClientResponse<Role> res = client.read(knownResourceId);
256 int statusCode = res.getStatus();
258 // Check the status code of the response: does it match
259 // the expected response(s)?
260 if (logger.isDebugEnabled()) {
261 logger.debug(testName + ": status = " + statusCode);
263 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
264 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
265 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
267 Role output = (Role) res.getEntity();
268 Assert.assertNotNull(output);
273 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
274 dependsOnMethods = {"read"})
275 public void readNonExistent(String testName) throws Exception {
278 setupReadNonExistent(testName);
280 // Submit the request to the service and store the response.
281 RoleClient client = new RoleClient();
282 ClientResponse<Role> res = client.read(NON_EXISTENT_ID);
283 int statusCode = res.getStatus();
285 // Check the status code of the response: does it match
286 // the expected response(s)?
287 if (logger.isDebugEnabled()) {
288 logger.debug(testName + ": status = " + statusCode);
290 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
291 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
292 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
295 // ---------------------------------------------------------------
296 // CRUD tests : READ_LIST tests
297 // ---------------------------------------------------------------
300 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
301 dependsOnMethods = {"createList", "read"})
302 public void readList(String testName) throws Exception {
305 setupReadList(testName);
307 // Submit the request to the service and store the response.
308 RoleClient client = new RoleClient();
309 ClientResponse<RolesList> res = client.readList();
310 RolesList list = res.getEntity();
311 int statusCode = res.getStatus();
313 // Check the status code of the response: does it match
314 // the expected response(s)?
315 if (logger.isDebugEnabled()) {
316 logger.debug(testName + ": status = " + statusCode);
318 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
319 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
320 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
322 // Optionally output additional data about list members for debugging.
323 boolean iterateThroughList = true;
324 if (iterateThroughList && logger.isDebugEnabled()) {
325 printList(testName, list);
329 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
330 dependsOnMethods = {"createList", "read"})
331 public void searchRoleName(String testName) throws Exception {
334 setupReadList(testName);
336 // Submit the request to the service and store the response.
337 RoleClient client = new RoleClient();
338 ClientResponse<RolesList> res = client.readSearchList("movingImage");
339 RolesList list = res.getEntity();
340 int statusCode = res.getStatus();
341 // Check the status code of the response: does it match
342 // the expected response(s)?
343 if (logger.isDebugEnabled()) {
344 logger.debug(testName + ": status = " + statusCode);
346 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
347 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
348 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
349 int EXPECTED_ITEMS = 1;
350 if (logger.isDebugEnabled()) {
351 logger.debug(testName + ": received = " + list.getRoles().size()
352 + " expected=" + EXPECTED_ITEMS);
354 Assert.assertEquals(EXPECTED_ITEMS, list.getRoles().size());
355 // Optionally output additional data about list members for debugging.
356 boolean iterateThroughList = true;
357 if (iterateThroughList && logger.isDebugEnabled()) {
358 printList(testName, list);
364 // ---------------------------------------------------------------
365 // CRUD tests : UPDATE tests
366 // ---------------------------------------------------------------
369 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
370 dependsOnMethods = {"read", "readList", "readNonExistent"})
371 public void update(String testName) throws Exception {
374 setupUpdate(testName);
376 Role roleToUpdate = new Role();
377 roleToUpdate.setCsid(knownResourceId);
379 // Update the content of this resource.
380 roleToUpdate.setRoleName("updated-role");
381 if (logger.isDebugEnabled()) {
382 logger.debug("updated object");
383 logger.debug(objectAsXmlString(roleToUpdate,
386 RoleClient client = new RoleClient();
387 // Submit the request to the service and store the response.
388 ClientResponse<Role> res = client.update(knownResourceId, roleToUpdate);
389 int statusCode = res.getStatus();
390 // Check the status code of the response: does it match the expected response(s)?
391 if (logger.isDebugEnabled()) {
392 logger.debug(testName + ": status = " + statusCode);
394 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
395 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
396 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
399 Role roleUpdated = (Role) res.getEntity();
400 Assert.assertNotNull(roleUpdated);
402 Assert.assertEquals(roleUpdated.getRoleName(),
403 roleToUpdate.getRoleName(),
404 "Data in updated object did not match submitted data.");
408 // Placeholders until the three tests below can be uncommented.
409 // See Issue CSPACE-401.
411 public void updateWithEmptyEntityBody(String testName) throws Exception {
412 //FIXME: Should this test really be empty? If so, please comment accordingly.
416 public void updateWithMalformedXml(String testName) throws Exception {
417 //FIXME: Should this test really be empty? If so, please comment accordingly.
421 public void updateWithWrongXmlSchema(String testName) throws Exception {
422 //FIXME: Should this test really be empty? If so, please comment accordingly.
426 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
427 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
428 public void updateNonExistent(String testName) throws Exception {
431 setupUpdateNonExistent(testName);
433 // Submit the request to the service and store the response.
435 // Note: The ID used in this 'create' call may be arbitrary.
436 // The only relevant ID may be the one used in updateRole(), below.
437 RoleClient client = new RoleClient();
438 Role role = createRoleInstance("ROLE_XXX",
441 ClientResponse<Role> res =
442 client.update(NON_EXISTENT_ID, role);
443 int statusCode = res.getStatus();
445 // Check the status code of the response: does it match
446 // the expected response(s)?
447 if (logger.isDebugEnabled()) {
448 logger.debug(testName + ": status = " + statusCode);
450 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
451 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
452 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
455 // ---------------------------------------------------------------
456 // CRUD tests : DELETE tests
457 // ---------------------------------------------------------------
460 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
461 dependsOnMethods = {"update"})
462 public void delete(String testName) throws Exception {
465 setupDelete(testName);
467 // Submit the request to the service and store the response.
468 RoleClient client = new RoleClient();
469 ClientResponse<Response> res = client.delete(knownResourceId);
470 int statusCode = res.getStatus();
472 // Check the status code of the response: does it match
473 // the expected response(s)?
474 if (logger.isDebugEnabled()) {
475 logger.debug(testName + ": status = " + statusCode);
477 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
478 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
479 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
484 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
485 dependsOnMethods = {"delete"})
486 public void deleteNonExistent(String testName) throws Exception {
489 setupDeleteNonExistent(testName);
491 // Submit the request to the service and store the response.
492 RoleClient client = new RoleClient();
493 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
494 int statusCode = res.getStatus();
496 // Check the status code of the response: does it match
497 // the expected response(s)?
498 if (logger.isDebugEnabled()) {
499 logger.debug(testName + ": status = " + statusCode);
501 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
502 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
503 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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.
513 @Test(dependsOnMethods = {"create", "read"})
514 public void testSubmitRequest() throws Exception {
516 // Expected status code: 200 OK
517 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
519 // Submit the request to the service and store the response.
520 String method = ServiceRequestType.READ.httpMethodName();
521 String url = getResourceURL(knownResourceId);
522 int statusCode = submitRequest(method, url);
524 // Check the status code of the response: does it match
525 // the expected response(s)?
526 if (logger.isDebugEnabled()) {
527 logger.debug("testSubmitRequest: url=" + url
528 + " status=" + statusCode);
530 Assert.assertEquals(statusCode, EXPECTED_STATUS);
534 // ---------------------------------------------------------------
535 // Utility methods used by tests above
536 // ---------------------------------------------------------------
538 * create role instance
544 public Role createRoleInstance(String roleName,
546 boolean useRoleName) {
548 Role role = RoleFactory.createRoleInstance(roleName, description,
550 if (logger.isDebugEnabled()) {
551 logger.debug("to be created, role");
552 logger.debug(objectAsXmlString(role, Role.class));
558 @AfterClass(alwaysRun = true)
559 public void cleanUp() {
560 setupDelete("delete");
561 String noTest = System.getProperty("noTestCleanup");
562 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
563 if (logger.isDebugEnabled()) {
564 logger.debug("Skipping Cleanup phase ...");
568 if (logger.isDebugEnabled()) {
569 logger.debug("Cleaning up temporary resources created for testing ...");
571 RoleClient client = new RoleClient();
572 for (String resourceId : allResourceIdsCreated) {
573 ClientResponse<Response> res = client.delete(resourceId);
574 int statusCode = res.getStatus();
575 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
576 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
577 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
581 private int printList(String testName, RolesList list) {
585 for (Role role : list.getRoles()) {
586 logger.debug(testName + " role csid=" + role.getCsid()
587 + " name=" + role.getRoleName()
588 + " desc=" + role.getDescription());