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.RoleClient;
30 import org.collectionspace.services.authorization.Role;
31 import org.collectionspace.services.authorization.RolesList;
32 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
33 import org.collectionspace.services.client.test.ServiceRequestType;
34 import org.jboss.resteasy.client.ClientResponse;
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.testng.annotations.AfterClass;
44 * RoleServiceTest, carries out tests against a
45 * deployed and running Role Service.
47 * $LastChangedRevision: 917 $
48 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
50 public class RoleServiceTest extends AbstractServiceTestImpl {
52 static private final Logger logger =
53 LoggerFactory.getLogger(RoleServiceTest.class);
54 // Instance variables specific to this test.
55 private RoleClient client = new RoleClient();
56 private String knownResourceId = null;
57 private List<String> allResourceIdsCreated = new ArrayList();
58 boolean addTenant = true;
60 * This method is called only by the parent class, AbstractServiceTestImpl
64 protected String getServicePathComponent() {
65 return client.getServicePathComponent();
68 // ---------------------------------------------------------------
69 // CRUD tests : CREATE tests
70 // ---------------------------------------------------------------
73 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
74 public void create(String testName) throws Exception {
76 // Perform setup, such as initializing the type of service request
77 // (e.g. CREATE, DELETE), its valid and expected status codes, and
78 // its associated HTTP method name (e.g. POST, DELETE).
79 setupCreate(testName);
81 // Submit the request to the service and store the response.
82 Role role = createRoleInstance("ROLE_USERS_TEST",
83 "all users are required to be in this role",
85 ClientResponse<Response> res = client.create(role);
86 int statusCode = res.getStatus();
88 // Check the status code of the response: does it match
89 // the expected response(s)?
92 // Does it fall within the set of valid status codes?
93 // Does it exactly match the expected status code?
94 if (logger.isDebugEnabled()) {
95 logger.debug(testName + ": status = " + statusCode);
97 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
98 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
99 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
101 // Store the ID returned from this create operation
102 // for additional tests below.
103 knownResourceId = extractId(res);
104 if (logger.isDebugEnabled()) {
105 logger.debug(testName + ": knownResourceId=" + knownResourceId);
109 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
110 dependsOnMethods = {"create"})
111 public void createForUniqueRole(String testName) throws Exception {
113 setupCreate(testName);
115 // Submit the request to the service and store the response.
116 Role role = createRoleInstance("ROLE_USERS",
119 ClientResponse<Response> res = client.create(role);
120 int statusCode = res.getStatus();
122 if (logger.isDebugEnabled()) {
123 logger.debug(testName + ": status = " + statusCode);
125 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
126 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
127 Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
130 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
131 dependsOnMethods = {"create"})
132 public void createWithoutRoleName(String testName) throws Exception {
134 setupCreate(testName);
136 // Submit the request to the service and store the response.
137 Role role = createRoleInstance("ROLE_USERS",
140 ClientResponse<Response> res = client.create(role);
141 int statusCode = res.getStatus();
142 // Does it exactly match the expected status code?
143 if (logger.isDebugEnabled()) {
144 logger.debug(testName + ": status = " + statusCode);
146 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
147 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
148 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
151 //to not cause uniqueness violation for role, createList is removed
153 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
154 dependsOnMethods = {"create"})
155 public void createList(String testName) throws Exception {
157 setupCreate(testName);
158 // Submit the request to the service and store the response.
159 Role role1 = createRoleInstance("ROLE_COLLECTIONS_MANGER_TEST",
160 "collection manager",
162 ClientResponse<Response> res = client.create(role1);
163 int statusCode = res.getStatus();
164 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
165 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
166 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
167 allResourceIdsCreated.add(extractId(res));
169 Role role2 = createRoleInstance("ROLE_COLLECTIONS_CURATOR_TEST",
170 "collections curator",
172 res = client.create(role2);
173 statusCode = res.getStatus();
174 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
175 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
176 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
177 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
178 allResourceIdsCreated.add(extractId(res));
180 Role role3 = createRoleInstance("ROLE_MOVINGIMAGE_ADMIN_TEST",
181 "moving image admin",
183 res = client.create(role3);
184 statusCode = res.getStatus();
185 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
186 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
187 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
188 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
189 allResourceIdsCreated.add(extractId(res));
193 // Placeholders until the three tests below can be uncommented.
194 // See Issue CSPACE-401.
196 public void createWithEmptyEntityBody(String testName) throws Exception {
200 public void createWithMalformedXml(String testName) throws Exception {
204 public void createWithWrongXmlSchema(String testName) throws Exception {
207 // ---------------------------------------------------------------
208 // CRUD tests : READ tests
209 // ---------------------------------------------------------------
212 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
213 dependsOnMethods = {"create"})
214 public void read(String testName) throws Exception {
219 // Submit the request to the service and store the response.
220 ClientResponse<Role> res = client.read(knownResourceId);
221 int statusCode = res.getStatus();
223 // Check the status code of the response: does it match
224 // the expected response(s)?
225 if (logger.isDebugEnabled()) {
226 logger.debug(testName + ": status = " + statusCode);
228 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
229 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
230 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
232 Role output = (Role) res.getEntity();
233 Assert.assertNotNull(output);
238 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
239 dependsOnMethods = {"read"})
240 public void readNonExistent(String testName) throws Exception {
243 setupReadNonExistent(testName);
245 // Submit the request to the service and store the response.
246 ClientResponse<Role> res = client.read(NON_EXISTENT_ID);
247 int statusCode = res.getStatus();
249 // Check the status code of the response: does it match
250 // the expected response(s)?
251 if (logger.isDebugEnabled()) {
252 logger.debug(testName + ": status = " + statusCode);
254 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
255 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
256 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
259 // ---------------------------------------------------------------
260 // CRUD tests : READ_LIST tests
261 // ---------------------------------------------------------------
264 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
265 dependsOnMethods = {"createList", "read"})
266 public void readList(String testName) throws Exception {
269 setupReadList(testName);
271 // Submit the request to the service and store the response.
272 ClientResponse<RolesList> res = client.readList();
273 RolesList list = res.getEntity();
274 int statusCode = res.getStatus();
276 // Check the status code of the response: does it match
277 // the expected response(s)?
278 if (logger.isDebugEnabled()) {
279 logger.debug(testName + ": status = " + statusCode);
281 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
282 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
283 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
285 // Optionally output additional data about list members for debugging.
286 boolean iterateThroughList = true;
287 if (iterateThroughList && logger.isDebugEnabled()) {
288 printList(testName, list);
292 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
293 dependsOnMethods = {"createList", "read"})
294 public void searchRoleName(String testName) throws Exception {
297 setupReadList(testName);
299 // Submit the request to the service and store the response.
300 ClientResponse<RolesList> res = client.readSearchList("movingImage");
301 RolesList list = res.getEntity();
302 int statusCode = res.getStatus();
303 // Check the status code of the response: does it match
304 // the expected response(s)?
305 if (logger.isDebugEnabled()) {
306 logger.debug(testName + ": status = " + statusCode);
308 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
309 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
310 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
311 int EXPECTED_ITEMS = 1;
312 if (logger.isDebugEnabled()) {
313 logger.debug(testName + ": received = " + list.getRoles().size()
314 + " expected=" + EXPECTED_ITEMS);
316 Assert.assertEquals(EXPECTED_ITEMS, list.getRoles().size());
317 // Optionally output additional data about list members for debugging.
318 boolean iterateThroughList = true;
319 if (iterateThroughList && logger.isDebugEnabled()) {
320 printList(testName, list);
326 // ---------------------------------------------------------------
327 // CRUD tests : UPDATE tests
328 // ---------------------------------------------------------------
331 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
332 dependsOnMethods = {"read", "readList", "readNonExistent"})
333 public void update(String testName) throws Exception {
336 setupUpdate(testName);
339 ClientResponse<Role> res =
340 client.read(knownResourceId);
341 if (logger.isDebugEnabled()) {
342 logger.debug(testName + ": read status = " + res.getStatus());
344 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
346 if (logger.isDebugEnabled()) {
347 logger.debug("got object to update with ID: " + knownResourceId);
350 (Role) res.getEntity();
351 Assert.assertNotNull(toUpdateRole);
353 // Update the content of this resource.
354 toUpdateRole.setRoleName("updated-" + toUpdateRole.getRoleName());
355 if (logger.isDebugEnabled()) {
356 logger.debug("updated object");
357 logger.debug(objectAsXmlString(toUpdateRole,
361 // Submit the request to the service and store the response.
362 res = client.update(knownResourceId, toUpdateRole);
363 int statusCode = res.getStatus();
364 // Check the status code of the response: does it match the expected response(s)?
365 if (logger.isDebugEnabled()) {
366 logger.debug(testName + ": status = " + statusCode);
368 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
369 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
370 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
373 Role updatedRole = (Role) res.getEntity();
374 Assert.assertNotNull(updatedRole);
376 Assert.assertEquals(updatedRole.getRoleName(),
377 toUpdateRole.getRoleName(),
378 "Data in updated object did not match submitted data.");
382 // Placeholders until the three tests below can be uncommented.
383 // See Issue CSPACE-401.
385 public void updateWithEmptyEntityBody(String testName) throws Exception {
389 public void updateWithMalformedXml(String testName) throws Exception {
393 public void updateWithWrongXmlSchema(String testName) throws Exception {
397 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
398 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
399 public void updateNonExistent(String testName) throws Exception {
402 setupUpdateNonExistent(testName);
404 // Submit the request to the service and store the response.
406 // Note: The ID used in this 'create' call may be arbitrary.
407 // The only relevant ID may be the one used in updateRole(), below.
408 Role role = createRoleInstance("ROLE_XXX",
411 ClientResponse<Role> res =
412 client.update(NON_EXISTENT_ID, role);
413 int statusCode = res.getStatus();
415 // Check the status code of the response: does it match
416 // the expected response(s)?
417 if (logger.isDebugEnabled()) {
418 logger.debug(testName + ": status = " + statusCode);
420 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
421 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
422 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
425 // ---------------------------------------------------------------
426 // CRUD tests : DELETE tests
427 // ---------------------------------------------------------------
430 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
431 dependsOnMethods = {"update"})
432 public void delete(String testName) throws Exception {
435 setupDelete(testName);
437 // Submit the request to the service and store the response.
438 ClientResponse<Response> res = client.delete(knownResourceId);
439 int statusCode = res.getStatus();
441 // Check the status code of the response: does it match
442 // the expected response(s)?
443 if (logger.isDebugEnabled()) {
444 logger.debug(testName + ": status = " + statusCode);
446 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
447 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
448 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
453 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
454 dependsOnMethods = {"delete"})
455 public void deleteNonExistent(String testName) throws Exception {
458 setupDeleteNonExistent(testName);
460 // Submit the request to the service and store the response.
461 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
462 int statusCode = res.getStatus();
464 // Check the status code of the response: does it match
465 // the expected response(s)?
466 if (logger.isDebugEnabled()) {
467 logger.debug(testName + ": status = " + statusCode);
469 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
470 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
471 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
474 // ---------------------------------------------------------------
475 // Utility tests : tests of code used in tests above
476 // ---------------------------------------------------------------
478 * Tests the code for manually submitting data that is used by several
479 * of the methods above.
481 @Test(dependsOnMethods = {"create", "read"})
482 public void testSubmitRequest() throws Exception {
484 // Expected status code: 200 OK
485 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
487 // Submit the request to the service and store the response.
488 String method = ServiceRequestType.READ.httpMethodName();
489 String url = getResourceURL(knownResourceId);
490 int statusCode = submitRequest(method, url);
492 // Check the status code of the response: does it match
493 // the expected response(s)?
494 if (logger.isDebugEnabled()) {
495 logger.debug("testSubmitRequest: url=" + url
496 + " status=" + statusCode);
498 Assert.assertEquals(statusCode, EXPECTED_STATUS);
502 // ---------------------------------------------------------------
503 // Utility methods used by tests above
504 // ---------------------------------------------------------------
506 * create role instance
512 static Role createRoleInstance(String roleName,
514 boolean useRoleName) {
516 Role role = new Role();
518 role.setRoleName(roleName);
520 role.setDescription(description);
521 if (logger.isDebugEnabled()) {
522 logger.debug("to be created, role common");
523 logger.debug(objectAsXmlString(role, Role.class));
529 @AfterClass(alwaysRun = true)
530 public void cleanUp() {
531 setupDelete("delete");
532 if (logger.isDebugEnabled()) {
533 logger.debug("Cleaning up temporary resources created for testing ...");
535 for (String resourceId : allResourceIdsCreated) {
536 // Note: Any non-success responses are ignored and not reported.
537 ClientResponse<Response> res = client.delete(resourceId);
538 int statusCode = res.getStatus();
539 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
540 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
541 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
545 private int printList(String testName, RolesList list) {
549 for (Role role : list.getRoles()) {
550 logger.debug(testName + " role csid=" + role.getCsid()
551 + " name=" + role.getRoleName()
552 + " desc=" + role.getDescription());