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;
28 //import org.collectionspace.services.authorization.ActionType;
29 import org.collectionspace.services.authorization.EffectType;
31 import org.collectionspace.services.client.CollectionSpaceClient;
32 import org.collectionspace.services.client.PermissionClient;
33 import org.collectionspace.services.authorization.Permission;
34 import org.collectionspace.services.authorization.PermissionAction;
35 import org.collectionspace.services.authorization.PermissionsList;
36 import org.collectionspace.services.client.PermissionFactory;
37 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
38 import org.collectionspace.services.client.test.ServiceRequestType;
39 import org.collectionspace.services.jaxb.AbstractCommonList;
40 import org.jboss.resteasy.client.ClientResponse;
42 import org.testng.Assert;
43 import org.testng.annotations.Test;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * PermissionServiceTest, carries out tests against a
50 * deployed and running Permission Service.
52 * $LastChangedRevision: 917 $
53 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
55 public class PermissionServiceTest extends AbstractServiceTestImpl {
57 /** The Constant logger. */
58 static private final Logger logger =
59 LoggerFactory.getLogger(PermissionServiceTest.class);
60 // Instance variables specific to this test.
61 /** The known resource id. */
62 private String knownResourceId = null;
64 /** The add tenant. */
65 boolean addTenant = true;
67 * This method is called only by the parent class, AbstractServiceTestImpl
71 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
74 protected String getServicePathComponent() {
75 return new PermissionClient().getServicePathComponent();
79 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
82 protected CollectionSpaceClient getClientInstance() {
83 return new PermissionClient();
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
90 protected AbstractCommonList getAbstractCommonList(
91 ClientResponse<AbstractCommonList> response) {
92 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
93 throw new UnsupportedOperationException();
97 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
99 @Test(dataProvider = "testName")
101 public void readPaginatedList(String testName) throws Exception {
102 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
105 // ---------------------------------------------------------------
106 // CRUD tests : CREATE tests
107 // ---------------------------------------------------------------
110 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
113 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
114 public void create(String testName) throws Exception {
116 // Perform setup, such as initializing the type of service request
117 // (e.g. CREATE, DELETE), its valid and expected status codes, and
118 // its associated HTTP method name (e.g. POST, DELETE).
119 setupCreate(testName);
121 // Submit the request to the service and store the response.
122 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
123 Permission permission = createPermissionInstance("accounts",
124 "default permissions for account",
130 PermissionClient client = new PermissionClient();
131 ClientResponse<Response> res = client.create(permission);
132 int statusCode = res.getStatus();
134 // Check the status code of the response: does it match
135 // the expected response(s)?
138 // Does it fall within the set of valid status codes?
139 // Does it exactly match the expected status code?
140 if (logger.isDebugEnabled()) {
141 logger.debug(testName + ": status = " + statusCode);
143 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
144 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
145 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
147 // Store the ID returned from this create operation
148 // for additional tests below.
149 knownResourceId = extractId(res);
150 if (logger.isDebugEnabled()) {
151 logger.debug(testName + ": knownResourceId=" + knownResourceId);
156 * Creates the without resource name.
158 * @param testName the test name
159 * @throws Exception the exception
161 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
162 dependsOnMethods = {"create"})
163 public void createWithoutResourceName(String testName) throws Exception {
165 setupCreate(testName);
167 // Submit the request to the service and store the response.
168 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
169 Permission permission = createPermissionInstance(null,
170 "default permissions for account",
176 PermissionClient client = new PermissionClient();
177 ClientResponse<Response> res = client.create(permission);
178 int statusCode = res.getStatus();
179 // Does it exactly match the expected status code?
180 if (logger.isDebugEnabled()) {
181 logger.debug(testName + ": status = " + statusCode);
183 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
184 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
185 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
188 //to not cause uniqueness violation for permission, createList is removed
190 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
193 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
194 dependsOnMethods = {"create"})
195 public void createList(String testName) throws Exception {
197 setupCreate(testName);
198 // Submit the request to the service and store the response.
199 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
200 Permission permission1 = createPermissionInstance("collectionobjects",
201 "default permissions for collectionobjects",
207 PermissionClient client = new PermissionClient();
208 ClientResponse<Response> res = client.create(permission1);
209 int statusCode = res.getStatus();
210 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
211 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
212 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
213 allResourceIdsCreated.add(extractId(res));
215 Permission permission2 = createPermissionInstance("acquisitions",
216 "default permissions for acquisitions",
222 res = client.create(permission2);
223 statusCode = res.getStatus();
224 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
225 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
226 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
227 allResourceIdsCreated.add(extractId(res));
229 Permission permission3 = createPermissionInstance("ids",
230 "default permissions for id service",
236 res = client.create(permission3);
237 statusCode = res.getStatus();
238 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
239 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
240 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
241 allResourceIdsCreated.add(extractId(res));
245 // Placeholders until the three tests below can be uncommented.
246 // See Issue CSPACE-401.
248 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
251 public void createWithEmptyEntityBody(String testName) throws Exception {
252 //FIXME: Should this test really be empty? If so, please comment accordingly.
256 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
259 public void createWithMalformedXml(String testName) throws Exception {
260 //FIXME: Should this test really be empty? If so, please comment accordingly.
264 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
267 public void createWithWrongXmlSchema(String testName) throws Exception {
268 //FIXME: Should this test really be empty? If so, please comment accordingly.
271 // ---------------------------------------------------------------
272 // CRUD tests : READ tests
273 // ---------------------------------------------------------------
276 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
279 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
280 dependsOnMethods = {"create"})
281 public void read(String testName) throws Exception {
286 // Submit the request to the service and store the response.
287 PermissionClient client = new PermissionClient();
288 ClientResponse<Permission> res = client.read(knownResourceId);
289 int statusCode = res.getStatus();
291 // Check the status code of the response: does it match
292 // the expected response(s)?
293 if (logger.isDebugEnabled()) {
294 logger.debug(testName + ": status = " + statusCode);
296 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
297 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
298 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
300 Permission output = (Permission) res.getEntity();
301 Assert.assertNotNull(output);
306 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
309 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
310 dependsOnMethods = {"read"})
311 public void readNonExistent(String testName) throws Exception {
314 setupReadNonExistent(testName);
316 // Submit the request to the service and store the response.
317 PermissionClient client = new PermissionClient();
318 ClientResponse<Permission> res = client.read(NON_EXISTENT_ID);
319 int statusCode = res.getStatus();
321 // Check the status code of the response: does it match
322 // the expected response(s)?
323 if (logger.isDebugEnabled()) {
324 logger.debug(testName + ": status = " + statusCode);
326 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
327 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
328 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
331 // ---------------------------------------------------------------
332 // CRUD tests : READ_LIST tests
333 // ---------------------------------------------------------------
336 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
339 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
340 dependsOnMethods = {"createList", "read"})
341 public void readList(String testName) throws Exception {
344 setupReadList(testName);
346 // Submit the request to the service and store the response.
347 PermissionClient client = new PermissionClient();
348 ClientResponse<PermissionsList> res = client.readList();
349 PermissionsList list = res.getEntity();
350 int statusCode = res.getStatus();
352 // Check the status code of the response: does it match
353 // the expected response(s)?
354 if (logger.isDebugEnabled()) {
355 logger.debug(testName + ": status = " + statusCode);
357 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
358 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
359 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
361 // Optionally output additional data about list members for debugging.
362 boolean iterateThroughList = true;
363 if (iterateThroughList && logger.isDebugEnabled()) {
364 printList(testName, list);
369 * Search resource name.
371 * @param testName the test name
372 * @throws Exception the exception
374 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
375 dependsOnMethods = {"createList", "read"})
376 public void searchResourceName(String testName) throws Exception {
379 setupReadList(testName);
381 // Submit the request to the service and store the response.
382 PermissionClient client = new PermissionClient();
383 ClientResponse<PermissionsList> res = client.readSearchList("acquisition");
384 PermissionsList list = res.getEntity();
385 int statusCode = res.getStatus();
386 // Check the status code of the response: does it match
387 // the expected response(s)?
388 if (logger.isDebugEnabled()) {
389 logger.debug(testName + ": status = " + statusCode);
391 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
392 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
393 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
394 int EXPECTED_ITEMS = 1;
395 if (logger.isDebugEnabled()) {
396 logger.debug(testName + ": received = " + list.getPermissions().size()
397 + " expected=" + EXPECTED_ITEMS);
399 Assert.assertEquals(EXPECTED_ITEMS, list.getPermissions().size());
400 // Optionally output additional data about list members for debugging.
401 boolean iterateThroughList = true;
402 if (iterateThroughList && logger.isDebugEnabled()) {
403 printList(testName, list);
409 // ---------------------------------------------------------------
410 // CRUD tests : UPDATE tests
411 // ---------------------------------------------------------------
414 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
417 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
418 dependsOnMethods = {"read", "readList", "readNonExistent"})
419 public void update(String testName) throws Exception {
422 setupUpdate(testName);
424 Permission permToUpdate = new Permission();
425 permToUpdate.setCsid(knownResourceId);
426 // Update the content of this resource.
427 permToUpdate.setResourceName("updated-resource");
428 if (logger.isDebugEnabled()) {
429 logger.debug("updated object");
430 logger.debug(objectAsXmlString(permToUpdate,
433 PermissionClient client = new PermissionClient();
434 // Submit the request to the service and store the response.
435 ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
436 int statusCode = res.getStatus();
437 // Check the status code of the response: does it match the expected response(s)?
438 if (logger.isDebugEnabled()) {
439 logger.debug(testName + ": status = " + statusCode);
441 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
442 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
443 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
446 Permission permUpdated = (Permission) res.getEntity();
447 Assert.assertNotNull(permUpdated);
449 Assert.assertEquals(permUpdated.getResourceName(),
450 permToUpdate.getResourceName(),
451 "Data in updated object did not match submitted data.");
457 * @param testName the test name
458 * @throws Exception the exception
460 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
461 dependsOnMethods = {"update"})
462 public void updateActions(String testName) throws Exception {
465 setupUpdate(testName);
467 Permission permToUpdate = new Permission();
468 permToUpdate.setCsid(knownResourceId);
469 // Update the content of this resource.
470 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
471 int default_actions = actions.size();
474 int toUpdate_actions = actions.size();
475 if (logger.isDebugEnabled()) {
476 logger.debug(testName + " no. of actions default=" + default_actions
477 + " to update =" + toUpdate_actions);
479 permToUpdate.setActions(actions);
480 if (logger.isDebugEnabled()) {
481 logger.debug(testName + " updated object\n"
482 + objectAsXmlString(permToUpdate, Permission.class));
484 PermissionClient client = new PermissionClient();
485 // Submit the request to the service and store the response.
486 ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
487 int statusCode = res.getStatus();
488 // Check the status code of the response: does it match the expected response(s)?
489 if (logger.isDebugEnabled()) {
490 logger.debug(testName + ": status = " + statusCode);
492 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
493 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
494 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
496 Permission permUpdated = (Permission) res.getEntity();
497 Assert.assertNotNull(permUpdated);
498 int updated_actions = permToUpdate.getActions().size();
499 if (logger.isDebugEnabled()) {
500 logger.debug(testName + " no. of actions to update=" + toUpdate_actions
501 + " updated =" + updated_actions);
503 Assert.assertEquals(toUpdate_actions,
505 "Data in updated object did not match submitted data.");
508 // Placeholders until the three tests below can be uncommented.
509 // See Issue CSPACE-401.
512 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
515 public void updateWithEmptyEntityBody(String testName) throws Exception {
516 //FIXME: Should this test really be empty? If so, please comment accordingly.
520 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
523 public void updateWithMalformedXml(String testName) throws Exception {
524 //FIXME: Should this test really be empty? If so, please comment accordingly.
528 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
531 public void updateWithWrongXmlSchema(String testName) throws Exception {
532 //FIXME: Should this test really be empty? If so, please comment accordingly.
536 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
539 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
540 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
541 public void updateNonExistent(String testName) throws Exception {
544 setupUpdateNonExistent(testName);
546 // Submit the request to the service and store the response.
548 // Note: The ID used in this 'create' call may be arbitrary.
549 // The only relevant ID may be the one used in updatePermission(), below.
550 PermissionClient client = new PermissionClient();
551 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
552 Permission permission = createPermissionInstance("acquisitions",
553 "default permissions for acquisitions",
559 ClientResponse<Permission> res =
560 client.update(NON_EXISTENT_ID, permission);
561 int statusCode = res.getStatus();
563 // Check the status code of the response: does it match
564 // the expected response(s)?
565 if (logger.isDebugEnabled()) {
566 logger.debug(testName + ": status = " + statusCode);
568 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
569 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
570 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
573 // ---------------------------------------------------------------
574 // CRUD tests : DELETE tests
575 // ---------------------------------------------------------------
578 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
581 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
582 dependsOnMethods = {"updateActions"})
583 public void delete(String testName) throws Exception {
586 setupDelete(testName);
588 // Submit the request to the service and store the response.
589 PermissionClient client = new PermissionClient();
590 ClientResponse<Response> res = client.delete(knownResourceId);
591 int statusCode = res.getStatus();
593 // Check the status code of the response: does it match
594 // the expected response(s)?
595 if (logger.isDebugEnabled()) {
596 logger.debug(testName + ": status = " + statusCode);
598 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
599 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
600 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
605 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
608 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
609 dependsOnMethods = {"delete"})
610 public void deleteNonExistent(String testName) throws Exception {
613 setupDeleteNonExistent(testName);
615 // Submit the request to the service and store the response.
616 PermissionClient client = new PermissionClient();
617 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
618 int statusCode = res.getStatus();
620 // Check the status code of the response: does it match
621 // the expected response(s)?
622 if (logger.isDebugEnabled()) {
623 logger.debug(testName + ": status = " + statusCode);
625 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
626 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
627 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
630 // ---------------------------------------------------------------
631 // Utility tests : tests of code used in tests above
632 // ---------------------------------------------------------------
634 * Tests the code for manually submitting data that is used by several
635 * of the methods above.
638 @Test(dependsOnMethods = {"create", "read"})
639 public void testSubmitRequest() throws Exception {
641 // Expected status code: 200 OK
642 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
644 // Submit the request to the service and store the response.
645 String method = ServiceRequestType.READ.httpMethodName();
646 String url = getResourceURL(knownResourceId);
647 int statusCode = submitRequest(method, url);
649 // Check the status code of the response: does it match
650 // the expected response(s)?
651 if (logger.isDebugEnabled()) {
652 logger.debug("testSubmitRequest: url=" + url
653 + " status=" + statusCode);
655 Assert.assertEquals(statusCode, EXPECTED_STATUS);
659 // ---------------------------------------------------------------
660 // Utility methods used by tests above
661 // ---------------------------------------------------------------
663 * create permission instance
664 * @param resourceName
666 * @param actionList list of actions for this permission
667 * @param effect effect of the permission
668 * @param useResourceName
673 public static Permission createPermissionInstance(String resourceName,
675 List<PermissionAction> actionList,
677 boolean useResourceName,
681 Permission permission = PermissionFactory.createPermissionInstance(resourceName,
682 description, actionList, effect,
683 useResourceName, useAction, useEffect);
685 if (logger.isDebugEnabled()) {
686 logger.debug("to be created, permission");
687 logger.debug(objectAsXmlString(permission, Permission.class));
695 * @param testName the test name
696 * @param list the list
699 private int printList(String testName, PermissionsList list) {
703 for (Permission permission : list.getPermissions()) {
704 logger.debug(testName + " permission csid=" + permission.getCsid()
705 + " name=" + permission.getResourceName()
706 + " desc=" + permission.getDescription());