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;
46 * RoleServiceTest, carries out tests against a
47 * deployed and running Role Service.
49 * $LastChangedRevision: 917 $
50 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
52 public class RoleServiceTest extends AbstractServiceTestImpl {
55 private final Logger logger =
56 LoggerFactory.getLogger(RoleServiceTest.class);
57 // Instance variables specific to this test.
58 /** The known resource id. */
59 private String knownResourceId = null;
60 // private List<String> allResourceIdsCreated = new ArrayList<String>();
61 /** The add tenant. */
62 boolean addTenant = true;
64 * This method is called only by the parent class, AbstractServiceTestImpl
68 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
71 protected String getServicePathComponent() {
72 return new RoleClient().getServicePathComponent();
76 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
79 protected CollectionSpaceClient getClientInstance() {
80 return new RoleClient();
84 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
87 protected AbstractCommonList getAbstractCommonList(
88 ClientResponse<AbstractCommonList> response) {
89 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
90 throw new UnsupportedOperationException();
94 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
96 @Test(dataProvider = "testName")
98 public void readPaginatedList(String testName) throws Exception {
99 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
102 // ---------------------------------------------------------------
103 // CRUD tests : CREATE tests
104 // ---------------------------------------------------------------
107 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
110 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
111 public void create(String testName) throws Exception {
113 // Perform setup, such as initializing the type of service request
114 // (e.g. CREATE, DELETE), its valid and expected status codes, and
115 // its associated HTTP method name (e.g. POST, DELETE).
116 setupCreate(testName);
118 // Submit the request to the service and store the response.
119 RoleClient client = new RoleClient();
120 Role role = createRoleInstance("ROLE_USERS_TEST",
121 "all users are required to be in this role",
123 ClientResponse<Response> res = client.create(role);
124 int statusCode = res.getStatus();
126 // Check the status code of the response: does it match
127 // the expected response(s)?
130 // Does it fall within the set of valid status codes?
131 // Does it exactly match the expected status code?
132 if (logger.isDebugEnabled()) {
133 logger.debug(testName + ": status = " + statusCode);
135 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
136 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
137 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
139 // Store the ID returned from this create operation
140 // for additional tests below.
141 knownResourceId = extractId(res);
142 if (logger.isDebugEnabled()) {
143 logger.debug(testName + ": knownResourceId=" + knownResourceId);
148 * Creates the for unique role.
150 * @param testName the test name
151 * @throws Exception the exception
153 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
154 dependsOnMethods = {"create"})
155 public void createForUniqueRole(String testName) throws Exception {
157 setupCreate(testName);
159 // Submit the request to the service and store the response.
160 RoleClient client = new RoleClient();
161 Role role = createRoleInstance("ROLE_USERS",
164 ClientResponse<Response> res = client.create(role);
165 int statusCode = res.getStatus();
167 if (logger.isDebugEnabled()) {
168 logger.debug(testName + ": status = " + statusCode);
170 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
171 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
172 Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
176 * Creates the without role name.
178 * @param testName the test name
179 * @throws Exception the exception
181 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
182 dependsOnMethods = {"create"})
183 public void createWithoutRoleName(String testName) throws Exception {
185 setupCreate(testName);
187 // Submit the request to the service and store the response.
188 RoleClient client = new RoleClient();
189 Role role = createRoleInstance("ROLE_USERS",
192 ClientResponse<Response> res = client.create(role);
193 int statusCode = res.getStatus();
194 // Does it exactly match the expected status code?
195 if (logger.isDebugEnabled()) {
196 logger.debug(testName + ": status = " + statusCode);
198 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
199 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
200 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
203 //to not cause uniqueness violation for role, createList is removed
205 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
208 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
209 dependsOnMethods = {"create"})
210 public void createList(String testName) throws Exception {
212 setupCreate(testName);
214 // Submit the request to the service and store the response.
215 RoleClient client = new RoleClient();
216 Role role1 = createRoleInstance("ROLE_COLLECTIONS_MANGER_TEST",
217 "collection manager",
219 ClientResponse<Response> res = client.create(role1);
220 int statusCode = res.getStatus();
221 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
222 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
223 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
224 allResourceIdsCreated.add(extractId(res));
226 Role role2 = createRoleInstance("ROLE_COLLECTIONS_CURATOR_TEST",
227 "collections curator",
229 res = client.create(role2);
230 statusCode = res.getStatus();
231 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
232 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
233 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
234 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
235 allResourceIdsCreated.add(extractId(res));
237 Role role3 = createRoleInstance("ROLE_MOVINGIMAGE_ADMIN_TEST",
238 "moving image admin",
240 res = client.create(role3);
241 statusCode = res.getStatus();
242 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
243 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
244 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
245 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
246 allResourceIdsCreated.add(extractId(res));
250 // Placeholders until the three tests below can be uncommented.
251 // See Issue CSPACE-401.
253 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
256 public void createWithEmptyEntityBody(String testName) throws Exception {
257 //FIXME: Should this test really be empty? If so, please comment accordingly.
261 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
264 public void createWithMalformedXml(String testName) throws Exception {
265 //FIXME: Should this test really be empty? If so, please comment accordingly.
269 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
272 public void createWithWrongXmlSchema(String testName) throws Exception {
273 //FIXME: Should this test really be empty? If so, please comment accordingly.
276 // ---------------------------------------------------------------
277 // CRUD tests : READ tests
278 // ---------------------------------------------------------------
281 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
284 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
285 dependsOnMethods = {"create"})
286 public void read(String testName) throws Exception {
291 // Submit the request to the service and store the response.
292 RoleClient client = new RoleClient();
293 ClientResponse<Role> res = client.read(knownResourceId);
294 int statusCode = res.getStatus();
296 // Check the status code of the response: does it match
297 // the expected response(s)?
298 if (logger.isDebugEnabled()) {
299 logger.debug(testName + ": status = " + statusCode);
301 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
302 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
303 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
305 Role output = (Role) res.getEntity();
306 Assert.assertNotNull(output);
311 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
314 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
315 dependsOnMethods = {"read"})
316 public void readNonExistent(String testName) throws Exception {
319 setupReadNonExistent(testName);
321 // Submit the request to the service and store the response.
322 RoleClient client = new RoleClient();
323 ClientResponse<Role> res = client.read(NON_EXISTENT_ID);
324 int statusCode = res.getStatus();
326 // Check the status code of the response: does it match
327 // the expected response(s)?
328 if (logger.isDebugEnabled()) {
329 logger.debug(testName + ": status = " + statusCode);
331 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
332 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
333 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
336 // ---------------------------------------------------------------
337 // CRUD tests : READ_LIST tests
338 // ---------------------------------------------------------------
341 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
344 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
345 dependsOnMethods = {"createList", "read"})
346 public void readList(String testName) throws Exception {
349 setupReadList(testName);
351 // Submit the request to the service and store the response.
352 RoleClient client = new RoleClient();
353 ClientResponse<RolesList> res = client.readList();
354 RolesList list = res.getEntity();
355 int statusCode = res.getStatus();
357 // Check the status code of the response: does it match
358 // the expected response(s)?
359 if (logger.isDebugEnabled()) {
360 logger.debug(testName + ": status = " + statusCode);
362 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
363 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
364 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
366 // Optionally output additional data about list members for debugging.
367 boolean iterateThroughList = true;
368 if (iterateThroughList && logger.isDebugEnabled()) {
369 printList(testName, list);
376 * @param testName the test name
377 * @throws Exception the exception
379 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
380 dependsOnMethods = {"createList", "read"})
381 public void searchRoleName(String testName) throws Exception {
384 setupReadList(testName);
386 // Submit the request to the service and store the response.
387 RoleClient client = new RoleClient();
388 ClientResponse<RolesList> res = client.readSearchList("movingImage");
389 RolesList list = res.getEntity();
390 int statusCode = res.getStatus();
391 // Check the status code of the response: does it match
392 // the expected response(s)?
393 if (logger.isDebugEnabled()) {
394 logger.debug(testName + ": status = " + statusCode);
396 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
397 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
398 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
399 int EXPECTED_ITEMS = 1;
400 if (logger.isDebugEnabled()) {
401 logger.debug(testName + ": received = " + list.getRoles().size()
402 + " expected=" + EXPECTED_ITEMS);
404 Assert.assertEquals(EXPECTED_ITEMS, list.getRoles().size());
405 // Optionally output additional data about list members for debugging.
406 boolean iterateThroughList = true;
407 if (iterateThroughList && logger.isDebugEnabled()) {
408 printList(testName, list);
414 // ---------------------------------------------------------------
415 // CRUD tests : UPDATE tests
416 // ---------------------------------------------------------------
419 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
422 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
423 dependsOnMethods = {"read", "readList", "readNonExistent"})
424 public void update(String testName) throws Exception {
427 setupUpdate(testName);
429 Role roleToUpdate = new Role();
430 roleToUpdate.setCsid(knownResourceId);
432 // Update the content of this resource.
433 roleToUpdate.setRoleName("updated-role");
434 if (logger.isDebugEnabled()) {
435 logger.debug("updated object");
436 logger.debug(objectAsXmlString(roleToUpdate,
439 RoleClient client = new RoleClient();
440 // Submit the request to the service and store the response.
441 ClientResponse<Role> res = client.update(knownResourceId, roleToUpdate);
442 int statusCode = res.getStatus();
443 // Check the status code of the response: does it match the expected response(s)?
444 if (logger.isDebugEnabled()) {
445 logger.debug(testName + ": status = " + statusCode);
447 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
448 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
449 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
452 Role roleUpdated = (Role) res.getEntity();
453 Assert.assertNotNull(roleUpdated);
455 Assert.assertEquals(roleUpdated.getRoleName(),
456 roleToUpdate.getRoleName(),
457 "Data in updated object did not match submitted data.");
461 // Placeholders until the three tests below can be uncommented.
462 // See Issue CSPACE-401.
464 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
467 public void updateWithEmptyEntityBody(String testName) throws Exception {
468 //FIXME: Should this test really be empty? If so, please comment accordingly.
472 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
475 public void updateWithMalformedXml(String testName) throws Exception {
476 //FIXME: Should this test really be empty? If so, please comment accordingly.
480 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
483 public void updateWithWrongXmlSchema(String testName) throws Exception {
484 //FIXME: Should this test really be empty? If so, please comment accordingly.
488 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
491 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
492 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
493 public void updateNonExistent(String testName) throws Exception {
496 setupUpdateNonExistent(testName);
498 // Submit the request to the service and store the response.
500 // Note: The ID used in this 'create' call may be arbitrary.
501 // The only relevant ID may be the one used in updateRole(), below.
502 RoleClient client = new RoleClient();
503 Role role = createRoleInstance("ROLE_XXX",
506 ClientResponse<Role> res =
507 client.update(NON_EXISTENT_ID, role);
508 int statusCode = res.getStatus();
510 // Check the status code of the response: does it match
511 // the expected response(s)?
512 if (logger.isDebugEnabled()) {
513 logger.debug(testName + ": status = " + statusCode);
515 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
516 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
517 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
520 // ---------------------------------------------------------------
521 // CRUD tests : DELETE tests
522 // ---------------------------------------------------------------
525 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
528 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
529 dependsOnMethods = {"update"})
530 public void delete(String testName) throws Exception {
533 setupDelete(testName);
535 // Submit the request to the service and store the response.
536 RoleClient client = new RoleClient();
537 ClientResponse<Response> res = client.delete(knownResourceId);
538 int statusCode = res.getStatus();
540 // Check the status code of the response: does it match
541 // the expected response(s)?
542 if (logger.isDebugEnabled()) {
543 logger.debug(testName + ": status = " + statusCode);
545 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
546 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
547 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
552 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
555 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
556 dependsOnMethods = {"delete"})
557 public void deleteNonExistent(String testName) throws Exception {
560 setupDeleteNonExistent(testName);
562 // Submit the request to the service and store the response.
563 RoleClient client = new RoleClient();
564 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
565 int statusCode = res.getStatus();
567 // Check the status code of the response: does it match
568 // the expected response(s)?
569 if (logger.isDebugEnabled()) {
570 logger.debug(testName + ": status = " + statusCode);
572 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
573 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
574 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
577 // ---------------------------------------------------------------
578 // Utility tests : tests of code used in tests above
579 // ---------------------------------------------------------------
581 * Tests the code for manually submitting data that is used by several
582 * of the methods above.
585 @Test(dependsOnMethods = {"create", "read"})
586 public void testSubmitRequest() throws Exception {
588 // Expected status code: 200 OK
589 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
591 // Submit the request to the service and store the response.
592 String method = ServiceRequestType.READ.httpMethodName();
593 String url = getResourceURL(knownResourceId);
594 int statusCode = submitRequest(method, url);
596 // Check the status code of the response: does it match
597 // the expected response(s)?
598 if (logger.isDebugEnabled()) {
599 logger.debug("testSubmitRequest: url=" + url
600 + " status=" + statusCode);
602 Assert.assertEquals(statusCode, EXPECTED_STATUS);
606 // ---------------------------------------------------------------
607 // Utility methods used by tests above
608 // ---------------------------------------------------------------
610 * create role instance
616 public Role createRoleInstance(String roleName,
618 boolean useRoleName) {
620 Role role = RoleFactory.createRoleInstance(roleName, description,
622 if (logger.isDebugEnabled()) {
623 logger.debug("to be created, role");
624 logger.debug(objectAsXmlString(role, Role.class));
633 * @param testName the test name
634 * @param list the list
637 private int printList(String testName, RolesList list) {
641 for (Role role : list.getRoles()) {
642 logger.debug(testName + " role csid=" + role.getCsid()
643 + " name=" + role.getRoleName()
644 + " desc=" + role.getDescription());