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.account.client.test;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Hashtable;
28 import java.util.List;
29 import javax.ws.rs.core.Response;
31 //import org.collectionspace.services.authorization.AccountRolesList;
32 import org.collectionspace.services.account.AccountsCommon;
33 import org.collectionspace.services.authorization.AccountRole;
34 import org.collectionspace.services.authorization.AccountValue;
35 import org.collectionspace.services.authorization.Role;
36 import org.collectionspace.services.authorization.RoleValue;
37 import org.collectionspace.services.client.AccountClient;
38 import org.collectionspace.services.client.AccountFactory;
39 import org.collectionspace.services.client.AccountRoleClient;
40 import org.collectionspace.services.client.AccountRoleFactory;
41 import org.collectionspace.services.client.CollectionSpaceClient;
42 import org.collectionspace.services.client.RoleClient;
43 import org.collectionspace.services.client.RoleFactory;
44 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
45 import org.collectionspace.services.client.test.ServiceRequestType;
46 import org.collectionspace.services.jaxb.AbstractCommonList;
47 import org.jboss.resteasy.client.ClientResponse;
50 import org.testng.Assert;
51 import org.testng.annotations.Test;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.testng.annotations.AfterClass;
56 import org.testng.annotations.BeforeClass;
59 * AccountServiceTest, carries out tests against a
60 * deployed and running Account, Role and AccountRole Services.
62 * $LastChangedRevision: 917 $
63 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
65 public class AccountRoleServiceTest extends AbstractServiceTestImpl {
67 /** The Constant logger. */
68 private final static String CLASS_NAME = AccountRoleServiceTest.class.getName();
69 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
70 // Instance variables specific to this test.
71 /** The known resource id. */
72 private String knownResourceId = null;
73 /** The all resource ids created. */
74 private List<String> allResourceIdsCreated = new ArrayList<String>();
75 /** The acc values. */
76 private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
77 /** The role values. */
78 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
80 * This method is called only by the parent class, AbstractServiceTestImpl
84 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
87 protected String getServicePathComponent() {
88 return new AccountRoleClient().getServicePathComponent();
94 @BeforeClass(alwaysRun = true)
95 public void seedData() {
96 String userId = "acc-role-user1";
97 String accId = createAccount(userId, "acc-role-user1-test@cspace.org");
98 AccountValue ava = new AccountValue();
99 ava.setScreenName(userId);
100 ava.setUserId(userId);
101 ava.setAccountId(accId);
102 accValues.put(ava.getScreenName(), ava);
104 String userId2 = "acc-role-user2";
105 String coAccId = createAccount(userId2, "acc-role-user2-test@cspace.org");
106 AccountValue avc = new AccountValue();
107 avc.setScreenName(userId2);
108 avc.setUserId(userId2);
109 avc.setAccountId(coAccId);
110 accValues.put(avc.getScreenName(), avc);
112 String rn1 = "ROLE_CO1";
113 String r1RoleId = createRole(rn1);
114 RoleValue rv1 = new RoleValue();
115 rv1.setRoleId(r1RoleId);
116 rv1.setRoleName(rn1);
117 roleValues.put(rv1.getRoleName(), rv1);
119 String rn2 = "ROLE_CO2";
120 String r2RoleId = createRole(rn2);
121 RoleValue rv2 = new RoleValue();
122 rv2.setRoleId(r2RoleId);
123 rv2.setRoleName(rn2);
124 roleValues.put(rv2.getRoleName(), rv2);
128 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
131 protected CollectionSpaceClient getClientInstance() {
132 return new AccountRoleClient();
136 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
139 protected AbstractCommonList getAbstractCommonList(
140 ClientResponse<AbstractCommonList> response) {
141 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
142 throw new UnsupportedOperationException();
146 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
148 @Test(dataProvider = "testName")
150 public void readPaginatedList(String testName) throws Exception {
151 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
154 // ---------------------------------------------------------------
155 // CRUD tests : CREATE tests
156 // ---------------------------------------------------------------
159 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
162 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
163 public void create(String testName) throws Exception {
165 if (logger.isDebugEnabled()) {
166 testBanner(testName, CLASS_NAME);
168 // Perform setup, such as initializing the type of service request
169 // (e.g. CREATE, DELETE), its valid and expected status codes, and
170 // its associated HTTP method name (e.g. POST, DELETE).
173 // Submit the request to the service and store the response.
174 AccountValue av = accValues.get("acc-role-user1");
175 AccountRole accRole = createAccountRoleInstance(av,
176 roleValues.values(), true, true);
177 AccountRoleClient client = new AccountRoleClient();
178 ClientResponse<Response> res = client.create(av.getAccountId(), accRole);
180 int statusCode = res.getStatus();
182 if (logger.isDebugEnabled()) {
183 logger.debug(testName + ": status = " + statusCode);
185 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
186 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
187 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
189 // Store the ID returned from this create operation
190 // for additional tests below.
191 //this is is not important in case of this relationship
192 knownResourceId = extractId(res);
193 if (logger.isDebugEnabled()) {
194 logger.debug(testName + ": knownResourceId=" + knownResourceId);
197 res.releaseConnection();
201 //to not cause uniqueness violation for accRole, createList is removed
203 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
205 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
206 dependsOnMethods = {"create"})
208 public void createList(String testName) throws Exception {
209 //FIXME: Should this test really be empty? If so, please comment accordingly.
213 // Placeholders until the three tests below can be uncommented.
214 // See Issue CSPACE-401.
216 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
219 public void createWithEmptyEntityBody(String testName) throws Exception {
220 //FIXME: Should this test really be empty? If so, please comment accordingly.
224 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
227 public void createWithMalformedXml(String testName) throws Exception {
228 //FIXME: Should this test really be empty? If so, please comment accordingly.
232 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
235 public void createWithWrongXmlSchema(String testName) throws Exception {
236 //FIXME: Should this test really be empty? If so, please comment accordingly.
239 // ---------------------------------------------------------------
240 // CRUD tests : READ tests
241 // ---------------------------------------------------------------
244 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
247 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
248 dependsOnMethods = {"create"})
249 public void read(String testName) throws Exception {
251 if (logger.isDebugEnabled()) {
252 testBanner(testName, CLASS_NAME);
258 // Submit the request to the service and store the response.
259 AccountRoleClient client = new AccountRoleClient();
260 ClientResponse<AccountRole> res = client.read(
261 accValues.get("acc-role-user1").getAccountId());
262 int statusCode = res.getStatus();
264 // Check the status code of the response: does it match
265 // the expected response(s)?
266 if (logger.isDebugEnabled()) {
267 logger.debug(testName + ": status = " + statusCode);
269 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
270 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
271 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
273 AccountRole output = res.getEntity();
274 Assert.assertNotNull(output);
276 res.releaseConnection();
282 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
285 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
286 public void readNonExistent(String testName) throws Exception {
288 if (logger.isDebugEnabled()) {
289 testBanner(testName, CLASS_NAME);
293 setupReadNonExistent();
295 // Submit the request to the service and store the response.
296 AccountRoleClient client = new AccountRoleClient();
297 ClientResponse<AccountRole> res = client.read(this.NON_EXISTENT_ID);
298 int statusCode = res.getStatus();
300 // Check the status code of the response: does it match
301 // the expected response(s)?
302 if (logger.isDebugEnabled()) {
303 logger.debug(testName + ": status = " + statusCode);
305 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
306 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
307 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
309 res.releaseConnection();
313 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
314 dependsOnMethods = {"create"})
315 public void readNoRelationship(String testName) throws Exception {
317 if (logger.isDebugEnabled()) {
318 testBanner(testName, CLASS_NAME);
324 // Submit the request to the service and store the response.
325 AccountRoleClient client = new AccountRoleClient();
326 ClientResponse<AccountRole> res = client.read(
327 accValues.get("acc-role-user2").getAccountId());
328 int statusCode = res.getStatus();
330 // Check the status code of the response: does it match
331 // the expected response(s)?
332 if (logger.isDebugEnabled()) {
333 logger.debug(testName + ": status = " + statusCode);
335 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
336 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
337 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
338 AccountRole output = res.getEntity();
340 String sOutput = objectAsXmlString(output, AccountRole.class);
341 if(logger.isDebugEnabled()) {
342 logger.debug(testName + " received " + sOutput);
345 res.releaseConnection();
349 // ---------------------------------------------------------------
350 // CRUD tests : READ_LIST tests
351 // ---------------------------------------------------------------
354 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
357 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
358 dependsOnMethods = {"createList", "read"})
359 public void readList(String testName) throws Exception {
360 //FIXME: Should this test really be empty? If so, please comment accordingly.
365 // ---------------------------------------------------------------
366 // CRUD tests : UPDATE tests
367 // ---------------------------------------------------------------
370 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
373 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
374 dependsOnMethods = {"read", "readList", "readNonExistent"})
375 public void update(String testName) throws Exception {
376 //FIXME: Should this test really be empty? If so, please comment accordingly.
380 // Placeholders until the three tests below can be uncommented.
381 // See Issue CSPACE-401.
383 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
386 public void updateWithEmptyEntityBody(String testName) throws Exception {
387 //FIXME: Should this test really be empty? If so, please comment accordingly.
391 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
394 public void updateWithMalformedXml(String testName) throws Exception {
395 //FIXME: Should this test really be empty? If so, please comment accordingly.
399 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
402 public void updateWithWrongXmlSchema(String testName) throws Exception {
403 //FIXME: Should this test really be empty? If so, please comment accordingly.
407 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
410 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
411 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
412 public void updateNonExistent(String testName) throws Exception {
413 //FIXME: Should this test really be empty? If so, please comment accordingly.
416 // ---------------------------------------------------------------
417 // CRUD tests : DELETE tests
418 // ---------------------------------------------------------------
421 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
424 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
425 dependsOnMethods = {"read"})
426 public void delete(String testName) throws Exception {
428 if (logger.isDebugEnabled()) {
429 testBanner(testName, CLASS_NAME);
436 // Lookup a know account, and delete all of its role relationships
438 AccountRoleClient client = new AccountRoleClient();
439 ClientResponse<AccountRole> readResponse = client.read(
440 accValues.get("acc-role-user1").getAccountId());
441 AccountRole toDelete = null;
443 toDelete = readResponse.getEntity();
445 readResponse.releaseConnection();
448 ClientResponse<Response> res = client.delete(
449 toDelete.getAccounts().get(0).getAccountId(), toDelete);
451 int statusCode = res.getStatus();
452 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
453 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
454 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
456 res.releaseConnection();
460 // recreate 'acc-role-user1' account and roles
466 // Lookup a know account, and delete all of its role relationships
468 readResponse = client.read(
469 accValues.get("acc-role-user1").getAccountId());
472 toDelete = readResponse.getEntity();
474 readResponse.releaseConnection();
477 res = client.delete(toDelete.getAccounts().get(0).getAccountId());
479 int statusCode = res.getStatus();
480 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
481 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
482 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
484 res.releaseConnection();
491 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
494 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
495 public void deleteNonExistent(String testName) throws Exception {
496 //ignoring this test as the service side returns 200 now even if it does
497 //not find a record in the db
500 // ---------------------------------------------------------------
502 // ---------------------------------------------------------------
505 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
506 public void searchWorkflowDeleted(String testName) throws Exception {
507 // Fixme: null test for now, overriding test in base class
510 // ---------------------------------------------------------------
511 // Utility tests : tests of code used in tests above
512 // ---------------------------------------------------------------
514 * Tests the code for manually submitting data that is used by several
515 * of the methods above.
518 @Test(dependsOnMethods = {"create"})
519 public void testSubmitRequest() throws Exception { //FIXME: REM - This is not testing a submit /accounts/*/accountroles, but instead just to /accounts
521 // Expected status code: 200 OK
522 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
524 // Submit the request to the service and store the response.
525 String method = ServiceRequestType.READ.httpMethodName();
526 String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
527 int statusCode = submitRequest(method, url);
529 // Check the status code of the response: does it match
530 // the expected response(s)?
531 if (logger.isDebugEnabled()) {
532 logger.debug("testSubmitRequest: url=" + url
533 + " status=" + statusCode);
535 Assert.assertEquals(statusCode, EXPECTED_STATUS);
539 // ---------------------------------------------------------------
540 // Utility methods used by tests above
541 // ---------------------------------------------------------------
543 * Creates the account role instance.
547 * @param usePermId the use perm id
548 * @param useRoleId the use role id
549 * @return the account role
551 static public AccountRole createAccountRoleInstance(AccountValue pv,
552 Collection<RoleValue> rvs,
556 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
557 pv, rvs, usePermId, useRoleId);
559 if (logger.isDebugEnabled()) {
560 logger.debug("to be created, accRole common");
561 logger.debug(objectAsXmlString(accRole, AccountRole.class));
569 @AfterClass(alwaysRun = true)
571 public void cleanUp() {
575 String noTest = System.getProperty("noTestCleanup");
576 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
577 if (logger.isDebugEnabled()) {
578 logger.debug("Skipping Cleanup phase ...");
582 if (logger.isDebugEnabled()) {
583 logger.debug("Cleaning up temporary resources created for testing ...");
587 for (AccountValue pv : accValues.values()) {
588 deleteAccount(pv.getAccountId());
591 for (RoleValue rv : roleValues.values()) {
592 deleteRole(rv.getRoleId());
597 * Creates the account.
599 * @param userName the user name
600 * @param email the email
603 private String createAccount(String userName, String email) {
605 if (logger.isDebugEnabled()) {
606 testBanner("createAccount");
611 AccountClient accClient = new AccountClient();
612 AccountsCommon account = AccountFactory.createAccountInstance(
613 userName, userName, userName, email, accClient.getTenantId(),
614 true, false, true, true);
615 ClientResponse<Response> res = accClient.create(account);
616 int statusCode = res.getStatus();
617 if (logger.isDebugEnabled()) {
618 logger.debug("createAccount: userName=" + userName
619 + " status = " + statusCode);
621 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
622 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
623 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
624 res.releaseConnection();
625 return extractId(res);
631 * @param accId the acc id
633 private void deleteAccount(String accId) {
635 if (logger.isDebugEnabled()) {
636 testBanner("deleteAccount");
641 AccountClient accClient = new AccountClient();
642 ClientResponse<Response> res = accClient.delete(accId);
643 int statusCode = res.getStatus();
645 if (logger.isDebugEnabled()) {
646 logger.debug("deleteAccount: delete account id="
647 + accId + " status=" + statusCode);
649 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
650 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
651 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
653 res.releaseConnection();
660 * @param roleName the role name
663 private String createRole(String roleName) {
664 if (logger.isDebugEnabled()) {
665 testBanner("createRole");
668 RoleClient roleClient = new RoleClient();
670 Role role = RoleFactory.createRoleInstance(roleName,
671 roleName, //the display name
672 "role for " + roleName, true);
673 ClientResponse<Response> res = roleClient.create(role);
674 int statusCode = res.getStatus();
675 if (logger.isDebugEnabled()) {
676 logger.debug("createRole: name=" + roleName
677 + " status = " + statusCode);
679 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
680 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
681 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
682 res.releaseConnection();
683 return extractId(res);
689 * @param roleId the role id
691 private void deleteRole(String roleId) {
692 if (logger.isDebugEnabled()) {
693 testBanner("deleteRole");
696 RoleClient roleClient = new RoleClient();
697 ClientResponse<Response> res = roleClient.delete(roleId);
698 int statusCode = res.getStatus();
700 if (logger.isDebugEnabled()) {
701 logger.debug("deleteRole: delete role id=" + roleId
702 + " status=" + statusCode);
704 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
705 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
706 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
708 res.releaseConnection();
713 protected String getServiceName() {
714 // AccountRoles service is a sub-service of the Account service, so we return Account's service name
715 return AccountClient.SERVICE_NAME;