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 String knownResourceId = null;
56 private List<String> allResourceIdsCreated = new ArrayList();
57 boolean addTenant = true;
59 * This method is called only by the parent class, AbstractServiceTestImpl
63 protected String getServicePathComponent() {
64 return new RoleClient().getServicePathComponent();
67 // ---------------------------------------------------------------
68 // CRUD tests : CREATE tests
69 // ---------------------------------------------------------------
72 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
73 public void create(String testName) throws Exception {
75 // Perform setup, such as initializing the type of service request
76 // (e.g. CREATE, DELETE), its valid and expected status codes, and
77 // its associated HTTP method name (e.g. POST, DELETE).
78 setupCreate(testName);
80 // Submit the request to the service and store the response.
81 RoleClient client = new RoleClient();
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 RoleClient client = new RoleClient();
117 Role role = createRoleInstance("ROLE_USERS",
120 ClientResponse<Response> res = client.create(role);
121 int statusCode = res.getStatus();
123 if (logger.isDebugEnabled()) {
124 logger.debug(testName + ": status = " + statusCode);
126 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
127 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
128 Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
131 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
132 dependsOnMethods = {"create"})
133 public void createWithoutRoleName(String testName) throws Exception {
135 setupCreate(testName);
137 // Submit the request to the service and store the response.
138 RoleClient client = new RoleClient();
139 Role role = createRoleInstance("ROLE_USERS",
142 ClientResponse<Response> res = client.create(role);
143 int statusCode = res.getStatus();
144 // Does it exactly match the expected status code?
145 if (logger.isDebugEnabled()) {
146 logger.debug(testName + ": status = " + statusCode);
148 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
149 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
150 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
153 //to not cause uniqueness violation for role, createList is removed
155 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
156 dependsOnMethods = {"create"})
157 public void createList(String testName) throws Exception {
159 setupCreate(testName);
161 // Submit the request to the service and store the response.
162 RoleClient client = new RoleClient();
163 Role role1 = createRoleInstance("ROLE_COLLECTIONS_MANGER_TEST",
164 "collection manager",
166 ClientResponse<Response> res = client.create(role1);
167 int statusCode = res.getStatus();
168 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
169 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
170 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
171 allResourceIdsCreated.add(extractId(res));
173 Role role2 = createRoleInstance("ROLE_COLLECTIONS_CURATOR_TEST",
174 "collections curator",
176 res = client.create(role2);
177 statusCode = res.getStatus();
178 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
181 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
182 allResourceIdsCreated.add(extractId(res));
184 Role role3 = createRoleInstance("ROLE_MOVINGIMAGE_ADMIN_TEST",
185 "moving image admin",
187 res = client.create(role3);
188 statusCode = res.getStatus();
189 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
190 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
191 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
192 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
193 allResourceIdsCreated.add(extractId(res));
197 // Placeholders until the three tests below can be uncommented.
198 // See Issue CSPACE-401.
200 public void createWithEmptyEntityBody(String testName) throws Exception {
204 public void createWithMalformedXml(String testName) throws Exception {
208 public void createWithWrongXmlSchema(String testName) throws Exception {
211 // ---------------------------------------------------------------
212 // CRUD tests : READ tests
213 // ---------------------------------------------------------------
216 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
217 dependsOnMethods = {"create"})
218 public void read(String testName) throws Exception {
223 // Submit the request to the service and store the response.
224 RoleClient client = new RoleClient();
225 ClientResponse<Role> res = client.read(knownResourceId);
226 int statusCode = res.getStatus();
228 // Check the status code of the response: does it match
229 // the expected response(s)?
230 if (logger.isDebugEnabled()) {
231 logger.debug(testName + ": status = " + statusCode);
233 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
234 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
235 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
237 Role output = (Role) res.getEntity();
238 Assert.assertNotNull(output);
243 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
244 dependsOnMethods = {"read"})
245 public void readNonExistent(String testName) throws Exception {
248 setupReadNonExistent(testName);
250 // Submit the request to the service and store the response.
251 RoleClient client = new RoleClient();
252 ClientResponse<Role> res = client.read(NON_EXISTENT_ID);
253 int statusCode = res.getStatus();
255 // Check the status code of the response: does it match
256 // the expected response(s)?
257 if (logger.isDebugEnabled()) {
258 logger.debug(testName + ": status = " + statusCode);
260 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
261 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
262 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
265 // ---------------------------------------------------------------
266 // CRUD tests : READ_LIST tests
267 // ---------------------------------------------------------------
270 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
271 dependsOnMethods = {"createList", "read"})
272 public void readList(String testName) throws Exception {
275 setupReadList(testName);
277 // Submit the request to the service and store the response.
278 RoleClient client = new RoleClient();
279 ClientResponse<RolesList> res = client.readList();
280 RolesList list = res.getEntity();
281 int statusCode = res.getStatus();
283 // Check the status code of the response: does it match
284 // the expected response(s)?
285 if (logger.isDebugEnabled()) {
286 logger.debug(testName + ": status = " + statusCode);
288 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
289 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
290 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
292 // Optionally output additional data about list members for debugging.
293 boolean iterateThroughList = true;
294 if (iterateThroughList && logger.isDebugEnabled()) {
295 printList(testName, list);
299 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
300 dependsOnMethods = {"createList", "read"})
301 public void searchRoleName(String testName) throws Exception {
304 setupReadList(testName);
306 // Submit the request to the service and store the response.
307 RoleClient client = new RoleClient();
308 ClientResponse<RolesList> res = client.readSearchList("movingImage");
309 RolesList list = res.getEntity();
310 int statusCode = res.getStatus();
311 // Check the status code of the response: does it match
312 // the expected response(s)?
313 if (logger.isDebugEnabled()) {
314 logger.debug(testName + ": status = " + statusCode);
316 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
317 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
318 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
319 int EXPECTED_ITEMS = 1;
320 if (logger.isDebugEnabled()) {
321 logger.debug(testName + ": received = " + list.getRoles().size()
322 + " expected=" + EXPECTED_ITEMS);
324 Assert.assertEquals(EXPECTED_ITEMS, list.getRoles().size());
325 // Optionally output additional data about list members for debugging.
326 boolean iterateThroughList = true;
327 if (iterateThroughList && logger.isDebugEnabled()) {
328 printList(testName, list);
334 // ---------------------------------------------------------------
335 // CRUD tests : UPDATE tests
336 // ---------------------------------------------------------------
339 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
340 dependsOnMethods = {"read", "readList", "readNonExistent"})
341 public void update(String testName) throws Exception {
344 setupUpdate(testName);
346 RoleClient client = new RoleClient();
347 ClientResponse<Role> res =
348 client.read(knownResourceId);
349 if (logger.isDebugEnabled()) {
350 logger.debug(testName + ": read status = " + res.getStatus());
352 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
354 if (logger.isDebugEnabled()) {
355 logger.debug("got object to update with ID: " + knownResourceId);
358 (Role) res.getEntity();
359 Assert.assertNotNull(toUpdateRole);
361 // Update the content of this resource.
362 toUpdateRole.setRoleName("updated-" + toUpdateRole.getRoleName());
363 if (logger.isDebugEnabled()) {
364 logger.debug("updated object");
365 logger.debug(objectAsXmlString(toUpdateRole,
369 // Submit the request to the service and store the response.
370 res = client.update(knownResourceId, toUpdateRole);
371 int statusCode = res.getStatus();
372 // Check the status code of the response: does it match the expected response(s)?
373 if (logger.isDebugEnabled()) {
374 logger.debug(testName + ": status = " + statusCode);
376 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
377 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
378 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
381 Role updatedRole = (Role) res.getEntity();
382 Assert.assertNotNull(updatedRole);
384 Assert.assertEquals(updatedRole.getRoleName(),
385 toUpdateRole.getRoleName(),
386 "Data in updated object did not match submitted data.");
390 // Placeholders until the three tests below can be uncommented.
391 // See Issue CSPACE-401.
393 public void updateWithEmptyEntityBody(String testName) throws Exception {
397 public void updateWithMalformedXml(String testName) throws Exception {
401 public void updateWithWrongXmlSchema(String testName) throws Exception {
405 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
406 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
407 public void updateNonExistent(String testName) throws Exception {
410 setupUpdateNonExistent(testName);
412 // Submit the request to the service and store the response.
414 // Note: The ID used in this 'create' call may be arbitrary.
415 // The only relevant ID may be the one used in updateRole(), below.
416 RoleClient client = new RoleClient();
417 Role role = createRoleInstance("ROLE_XXX",
420 ClientResponse<Role> res =
421 client.update(NON_EXISTENT_ID, role);
422 int statusCode = res.getStatus();
424 // Check the status code of the response: does it match
425 // the expected response(s)?
426 if (logger.isDebugEnabled()) {
427 logger.debug(testName + ": status = " + statusCode);
429 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
430 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
431 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
434 // ---------------------------------------------------------------
435 // CRUD tests : DELETE tests
436 // ---------------------------------------------------------------
439 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
440 dependsOnMethods = {"update"})
441 public void delete(String testName) throws Exception {
444 setupDelete(testName);
446 // Submit the request to the service and store the response.
447 RoleClient client = new RoleClient();
448 ClientResponse<Response> res = client.delete(knownResourceId);
449 int statusCode = res.getStatus();
451 // Check the status code of the response: does it match
452 // the expected response(s)?
453 if (logger.isDebugEnabled()) {
454 logger.debug(testName + ": status = " + statusCode);
456 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
457 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
458 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
463 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
464 dependsOnMethods = {"delete"})
465 public void deleteNonExistent(String testName) throws Exception {
468 setupDeleteNonExistent(testName);
470 // Submit the request to the service and store the response.
471 RoleClient client = new RoleClient();
472 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
473 int statusCode = res.getStatus();
475 // Check the status code of the response: does it match
476 // the expected response(s)?
477 if (logger.isDebugEnabled()) {
478 logger.debug(testName + ": status = " + statusCode);
480 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
481 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
482 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
485 // ---------------------------------------------------------------
486 // Utility tests : tests of code used in tests above
487 // ---------------------------------------------------------------
489 * Tests the code for manually submitting data that is used by several
490 * of the methods above.
492 @Test(dependsOnMethods = {"create", "read"})
493 public void testSubmitRequest() throws Exception {
495 // Expected status code: 200 OK
496 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
498 // Submit the request to the service and store the response.
499 String method = ServiceRequestType.READ.httpMethodName();
500 String url = getResourceURL(knownResourceId);
501 int statusCode = submitRequest(method, url);
503 // Check the status code of the response: does it match
504 // the expected response(s)?
505 if (logger.isDebugEnabled()) {
506 logger.debug("testSubmitRequest: url=" + url
507 + " status=" + statusCode);
509 Assert.assertEquals(statusCode, EXPECTED_STATUS);
513 // ---------------------------------------------------------------
514 // Utility methods used by tests above
515 // ---------------------------------------------------------------
517 * create role instance
523 static Role createRoleInstance(String roleName,
525 boolean useRoleName) {
527 Role role = new Role();
529 role.setRoleName(roleName);
531 role.setDescription(description);
532 if (logger.isDebugEnabled()) {
533 logger.debug("to be created, role common");
534 logger.debug(objectAsXmlString(role, Role.class));
540 @AfterClass(alwaysRun = true)
541 public void cleanUp() {
542 setupDelete("delete");
543 if (logger.isDebugEnabled()) {
544 logger.debug("Cleaning up temporary resources created for testing ...");
546 RoleClient client = new RoleClient();
547 for (String resourceId : allResourceIdsCreated) {
548 // Note: Any non-success responses are ignored and not reported.
549 ClientResponse<Response> res = client.delete(resourceId);
550 int statusCode = res.getStatus();
551 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
552 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
553 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
557 private int printList(String testName, RolesList list) {
561 for (Role role : list.getRoles()) {
562 logger.debug(testName + " role csid=" + role.getCsid()
563 + " name=" + role.getRoleName()
564 + " desc=" + role.getDescription());