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 Role roleToUpdate = new Role();
348 roleToUpdate.setCsid(knownResourceId);
350 // Update the content of this resource.
351 roleToUpdate.setRoleName("updated-role");
352 if (logger.isDebugEnabled()) {
353 logger.debug("updated object");
354 logger.debug(objectAsXmlString(roleToUpdate,
357 RoleClient client = new RoleClient();
358 // Submit the request to the service and store the response.
359 ClientResponse<Role> res = client.update(knownResourceId, roleToUpdate);
360 int statusCode = res.getStatus();
361 // Check the status code of the response: does it match the expected response(s)?
362 if (logger.isDebugEnabled()) {
363 logger.debug(testName + ": status = " + statusCode);
365 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
366 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
367 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
370 Role roleUpdated = (Role) res.getEntity();
371 Assert.assertNotNull(roleUpdated);
373 Assert.assertEquals(roleUpdated.getRoleName(),
374 roleToUpdate.getRoleName(),
375 "Data in updated object did not match submitted data.");
379 // Placeholders until the three tests below can be uncommented.
380 // See Issue CSPACE-401.
382 public void updateWithEmptyEntityBody(String testName) throws Exception {
386 public void updateWithMalformedXml(String testName) throws Exception {
390 public void updateWithWrongXmlSchema(String testName) throws Exception {
394 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
395 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
396 public void updateNonExistent(String testName) throws Exception {
399 setupUpdateNonExistent(testName);
401 // Submit the request to the service and store the response.
403 // Note: The ID used in this 'create' call may be arbitrary.
404 // The only relevant ID may be the one used in updateRole(), below.
405 RoleClient client = new RoleClient();
406 Role role = createRoleInstance("ROLE_XXX",
409 ClientResponse<Role> res =
410 client.update(NON_EXISTENT_ID, role);
411 int statusCode = res.getStatus();
413 // Check the status code of the response: does it match
414 // the expected response(s)?
415 if (logger.isDebugEnabled()) {
416 logger.debug(testName + ": status = " + statusCode);
418 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
419 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
420 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
423 // ---------------------------------------------------------------
424 // CRUD tests : DELETE tests
425 // ---------------------------------------------------------------
428 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
429 dependsOnMethods = {"update"})
430 public void delete(String testName) throws Exception {
433 setupDelete(testName);
435 // Submit the request to the service and store the response.
436 RoleClient client = new RoleClient();
437 ClientResponse<Response> res = client.delete(knownResourceId);
438 int statusCode = res.getStatus();
440 // Check the status code of the response: does it match
441 // the expected response(s)?
442 if (logger.isDebugEnabled()) {
443 logger.debug(testName + ": status = " + statusCode);
445 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
446 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
447 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
452 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
453 dependsOnMethods = {"delete"})
454 public void deleteNonExistent(String testName) throws Exception {
457 setupDeleteNonExistent(testName);
459 // Submit the request to the service and store the response.
460 RoleClient client = new RoleClient();
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 public Role createRoleInstance(String roleName,
514 boolean useRoleName) {
516 Role role = RoleFactory.createRoleInstance(roleName, description,
518 if (logger.isDebugEnabled()) {
519 logger.debug("to be created, role common");
520 logger.debug(objectAsXmlString(role, Role.class));
526 @AfterClass(alwaysRun = true)
527 public void cleanUp() {
528 setupDelete("delete");
529 String noTest = System.getProperty("noTestCleanup");
530 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
531 if (logger.isDebugEnabled()) {
532 logger.debug("Skipping Cleanup phase ...");
536 if (logger.isDebugEnabled()) {
537 logger.debug("Cleaning up temporary resources created for testing ...");
539 RoleClient client = new RoleClient();
540 for (String resourceId : allResourceIdsCreated) {
541 ClientResponse<Response> res = client.delete(resourceId);
542 int statusCode = res.getStatus();
543 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
544 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
545 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
549 private int printList(String testName, RolesList list) {
553 for (Role role : list.getRoles()) {
554 logger.debug(testName + " role csid=" + role.getCsid()
555 + " name=" + role.getRoleName()
556 + " desc=" + role.getDescription());