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.RoleFactory;
33 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
34 import org.collectionspace.services.client.test.ServiceRequestType;
35 import org.jboss.resteasy.client.ClientResponse;
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.testng.annotations.AfterClass;
45 * RoleServiceTest, carries out tests against a
46 * deployed and running Role Service.
48 * $LastChangedRevision: 917 $
49 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
51 public class RoleServiceTest extends AbstractServiceTestImpl {
53 private final Logger logger =
54 LoggerFactory.getLogger(RoleServiceTest.class);
55 // Instance variables specific to this test.
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 new RoleClient().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 RoleClient client = new RoleClient();
83 Role role = createRoleInstance("ROLE_USERS_TEST",
84 "all users are required to be in this role",
86 ClientResponse<Response> res = client.create(role);
87 int statusCode = res.getStatus();
89 // Check the status code of the response: does it match
90 // the expected response(s)?
93 // Does it fall within the set of valid status codes?
94 // Does it exactly match the expected status code?
95 if (logger.isDebugEnabled()) {
96 logger.debug(testName + ": status = " + statusCode);
98 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
99 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
100 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
102 // Store the ID returned from this create operation
103 // for additional tests below.
104 knownResourceId = extractId(res);
105 if (logger.isDebugEnabled()) {
106 logger.debug(testName + ": knownResourceId=" + knownResourceId);
110 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
111 dependsOnMethods = {"create"})
112 public void createForUniqueRole(String testName) throws Exception {
114 setupCreate(testName);
116 // Submit the request to the service and store the response.
117 RoleClient client = new RoleClient();
118 Role role = createRoleInstance("ROLE_USERS",
121 ClientResponse<Response> res = client.create(role);
122 int statusCode = res.getStatus();
124 if (logger.isDebugEnabled()) {
125 logger.debug(testName + ": status = " + statusCode);
127 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
128 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
129 Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
132 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
133 dependsOnMethods = {"create"})
134 public void createWithoutRoleName(String testName) throws Exception {
136 setupCreate(testName);
138 // Submit the request to the service and store the response.
139 RoleClient client = new RoleClient();
140 Role role = createRoleInstance("ROLE_USERS",
143 ClientResponse<Response> res = client.create(role);
144 int statusCode = res.getStatus();
145 // Does it exactly match the expected status code?
146 if (logger.isDebugEnabled()) {
147 logger.debug(testName + ": status = " + statusCode);
149 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
150 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
151 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
154 //to not cause uniqueness violation for role, createList is removed
156 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
157 dependsOnMethods = {"create"})
158 public void createList(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 role1 = createRoleInstance("ROLE_COLLECTIONS_MANGER_TEST",
165 "collection manager",
167 ClientResponse<Response> res = client.create(role1);
168 int statusCode = res.getStatus();
169 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
170 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
171 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
172 allResourceIdsCreated.add(extractId(res));
174 Role role2 = createRoleInstance("ROLE_COLLECTIONS_CURATOR_TEST",
175 "collections curator",
177 res = client.create(role2);
178 statusCode = res.getStatus();
179 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
180 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
181 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
182 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
183 allResourceIdsCreated.add(extractId(res));
185 Role role3 = createRoleInstance("ROLE_MOVINGIMAGE_ADMIN_TEST",
186 "moving image admin",
188 res = client.create(role3);
189 statusCode = res.getStatus();
190 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
191 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
192 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
193 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
194 allResourceIdsCreated.add(extractId(res));
198 // Placeholders until the three tests below can be uncommented.
199 // See Issue CSPACE-401.
201 public void createWithEmptyEntityBody(String testName) throws Exception {
205 public void createWithMalformedXml(String testName) throws Exception {
209 public void createWithWrongXmlSchema(String testName) throws Exception {
212 // ---------------------------------------------------------------
213 // CRUD tests : READ tests
214 // ---------------------------------------------------------------
217 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
218 dependsOnMethods = {"create"})
219 public void read(String testName) throws Exception {
224 // Submit the request to the service and store the response.
225 RoleClient client = new RoleClient();
226 ClientResponse<Role> res = client.read(knownResourceId);
227 int statusCode = res.getStatus();
229 // Check the status code of the response: does it match
230 // the expected response(s)?
231 if (logger.isDebugEnabled()) {
232 logger.debug(testName + ": status = " + statusCode);
234 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
235 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
236 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
238 Role output = (Role) res.getEntity();
239 Assert.assertNotNull(output);
244 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
245 dependsOnMethods = {"read"})
246 public void readNonExistent(String testName) throws Exception {
249 setupReadNonExistent(testName);
251 // Submit the request to the service and store the response.
252 RoleClient client = new RoleClient();
253 ClientResponse<Role> res = client.read(NON_EXISTENT_ID);
254 int statusCode = res.getStatus();
256 // Check the status code of the response: does it match
257 // the expected response(s)?
258 if (logger.isDebugEnabled()) {
259 logger.debug(testName + ": status = " + statusCode);
261 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
262 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
263 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
266 // ---------------------------------------------------------------
267 // CRUD tests : READ_LIST tests
268 // ---------------------------------------------------------------
271 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
272 dependsOnMethods = {"createList", "read"})
273 public void readList(String testName) throws Exception {
276 setupReadList(testName);
278 // Submit the request to the service and store the response.
279 RoleClient client = new RoleClient();
280 ClientResponse<RolesList> res = client.readList();
281 RolesList list = res.getEntity();
282 int statusCode = res.getStatus();
284 // Check the status code of the response: does it match
285 // the expected response(s)?
286 if (logger.isDebugEnabled()) {
287 logger.debug(testName + ": status = " + statusCode);
289 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
290 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
291 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
293 // Optionally output additional data about list members for debugging.
294 boolean iterateThroughList = true;
295 if (iterateThroughList && logger.isDebugEnabled()) {
296 printList(testName, list);
300 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
301 dependsOnMethods = {"createList", "read"})
302 public void searchRoleName(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.readSearchList("movingImage");
310 RolesList list = res.getEntity();
311 int statusCode = res.getStatus();
312 // Check the status code of the response: does it match
313 // the expected response(s)?
314 if (logger.isDebugEnabled()) {
315 logger.debug(testName + ": status = " + statusCode);
317 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
318 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
319 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
320 int EXPECTED_ITEMS = 1;
321 if (logger.isDebugEnabled()) {
322 logger.debug(testName + ": received = " + list.getRoles().size()
323 + " expected=" + EXPECTED_ITEMS);
325 Assert.assertEquals(EXPECTED_ITEMS, list.getRoles().size());
326 // Optionally output additional data about list members for debugging.
327 boolean iterateThroughList = true;
328 if (iterateThroughList && logger.isDebugEnabled()) {
329 printList(testName, list);
335 // ---------------------------------------------------------------
336 // CRUD tests : UPDATE tests
337 // ---------------------------------------------------------------
340 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
341 dependsOnMethods = {"read", "readList", "readNonExistent"})
342 public void update(String testName) throws Exception {
345 setupUpdate(testName);
347 RoleClient client = new RoleClient();
348 ClientResponse<Role> res =
349 client.read(knownResourceId);
350 if (logger.isDebugEnabled()) {
351 logger.debug(testName + ": read status = " + res.getStatus());
353 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
355 if (logger.isDebugEnabled()) {
356 logger.debug("got object to update with ID: " + knownResourceId);
359 (Role) res.getEntity();
360 Assert.assertNotNull(toUpdateRole);
362 // Update the content of this resource.
363 toUpdateRole.setRoleName("updated-" + toUpdateRole.getRoleName());
364 if (logger.isDebugEnabled()) {
365 logger.debug("updated object");
366 logger.debug(objectAsXmlString(toUpdateRole,
370 // Submit the request to the service and store the response.
371 res = client.update(knownResourceId, toUpdateRole);
372 int statusCode = res.getStatus();
373 // Check the status code of the response: does it match the expected response(s)?
374 if (logger.isDebugEnabled()) {
375 logger.debug(testName + ": status = " + statusCode);
377 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
378 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
379 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
382 Role updatedRole = (Role) res.getEntity();
383 Assert.assertNotNull(updatedRole);
385 Assert.assertEquals(updatedRole.getRoleName(),
386 toUpdateRole.getRoleName(),
387 "Data in updated object did not match submitted data.");
391 // Placeholders until the three tests below can be uncommented.
392 // See Issue CSPACE-401.
394 public void updateWithEmptyEntityBody(String testName) throws Exception {
398 public void updateWithMalformedXml(String testName) throws Exception {
402 public void updateWithWrongXmlSchema(String testName) throws Exception {
406 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
407 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
408 public void updateNonExistent(String testName) throws Exception {
411 setupUpdateNonExistent(testName);
413 // Submit the request to the service and store the response.
415 // Note: The ID used in this 'create' call may be arbitrary.
416 // The only relevant ID may be the one used in updateRole(), below.
417 RoleClient client = new RoleClient();
418 Role role = createRoleInstance("ROLE_XXX",
421 ClientResponse<Role> res =
422 client.update(NON_EXISTENT_ID, role);
423 int statusCode = res.getStatus();
425 // Check the status code of the response: does it match
426 // the expected response(s)?
427 if (logger.isDebugEnabled()) {
428 logger.debug(testName + ": status = " + statusCode);
430 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
431 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
432 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
435 // ---------------------------------------------------------------
436 // CRUD tests : DELETE tests
437 // ---------------------------------------------------------------
440 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
441 dependsOnMethods = {"update"})
442 public void delete(String testName) throws Exception {
445 setupDelete(testName);
447 // Submit the request to the service and store the response.
448 RoleClient client = new RoleClient();
449 ClientResponse<Response> res = client.delete(knownResourceId);
450 int statusCode = res.getStatus();
452 // Check the status code of the response: does it match
453 // the expected response(s)?
454 if (logger.isDebugEnabled()) {
455 logger.debug(testName + ": status = " + statusCode);
457 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
458 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
459 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
464 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
465 dependsOnMethods = {"delete"})
466 public void deleteNonExistent(String testName) throws Exception {
469 setupDeleteNonExistent(testName);
471 // Submit the request to the service and store the response.
472 RoleClient client = new RoleClient();
473 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
474 int statusCode = res.getStatus();
476 // Check the status code of the response: does it match
477 // the expected response(s)?
478 if (logger.isDebugEnabled()) {
479 logger.debug(testName + ": status = " + statusCode);
481 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
482 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
483 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
486 // ---------------------------------------------------------------
487 // Utility tests : tests of code used in tests above
488 // ---------------------------------------------------------------
490 * Tests the code for manually submitting data that is used by several
491 * of the methods above.
493 @Test(dependsOnMethods = {"create", "read"})
494 public void testSubmitRequest() throws Exception {
496 // Expected status code: 200 OK
497 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
499 // Submit the request to the service and store the response.
500 String method = ServiceRequestType.READ.httpMethodName();
501 String url = getResourceURL(knownResourceId);
502 int statusCode = submitRequest(method, url);
504 // Check the status code of the response: does it match
505 // the expected response(s)?
506 if (logger.isDebugEnabled()) {
507 logger.debug("testSubmitRequest: url=" + url
508 + " status=" + statusCode);
510 Assert.assertEquals(statusCode, EXPECTED_STATUS);
514 // ---------------------------------------------------------------
515 // Utility methods used by tests above
516 // ---------------------------------------------------------------
518 * create role instance
524 public Role createRoleInstance(String roleName,
526 boolean useRoleName) {
528 Role role = RoleFactory.createRoleInstance(roleName, description,
530 if (logger.isDebugEnabled()) {
531 logger.debug("to be created, role common");
532 logger.debug(objectAsXmlString(role, Role.class));
538 @AfterClass(alwaysRun = true)
539 public void cleanUp() {
540 setupDelete("delete");
541 if (logger.isDebugEnabled()) {
542 logger.debug("Cleaning up temporary resources created for testing ...");
544 RoleClient client = new RoleClient();
545 for (String resourceId : allResourceIdsCreated) {
546 // Note: Any non-success responses are ignored and not reported.
547 ClientResponse<Response> res = client.delete(resourceId);
548 int statusCode = res.getStatus();
549 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
550 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
551 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
555 private int printList(String testName, RolesList list) {
559 for (Role role : list.getRoles()) {
560 logger.debug(testName + " role csid=" + role.getCsid()
561 + " name=" + role.getRoleName()
562 + " desc=" + role.getDescription());