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 // ---------------------------------------------------------------
501 // Utility tests : tests of code used in tests above
502 // ---------------------------------------------------------------
504 * Tests the code for manually submitting data that is used by several
505 * of the methods above.
508 @Test(dependsOnMethods = {"create"})
509 public void testSubmitRequest() throws Exception { //FIXME: REM - This is not testing a submit /accounts/*/accountroles, but instead just to /accounts
511 // Expected status code: 200 OK
512 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
514 // Submit the request to the service and store the response.
515 String method = ServiceRequestType.READ.httpMethodName();
516 String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
517 int statusCode = submitRequest(method, url);
519 // Check the status code of the response: does it match
520 // the expected response(s)?
521 if (logger.isDebugEnabled()) {
522 logger.debug("testSubmitRequest: url=" + url
523 + " status=" + statusCode);
525 Assert.assertEquals(statusCode, EXPECTED_STATUS);
529 // ---------------------------------------------------------------
530 // Utility methods used by tests above
531 // ---------------------------------------------------------------
533 * Creates the account role instance.
537 * @param usePermId the use perm id
538 * @param useRoleId the use role id
539 * @return the account role
541 static public AccountRole createAccountRoleInstance(AccountValue pv,
542 Collection<RoleValue> rvs,
546 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
547 pv, rvs, usePermId, useRoleId);
549 if (logger.isDebugEnabled()) {
550 logger.debug("to be created, accRole common");
551 logger.debug(objectAsXmlString(accRole, AccountRole.class));
559 @AfterClass(alwaysRun = true)
561 public void cleanUp() {
565 String noTest = System.getProperty("noTestCleanup");
566 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
567 if (logger.isDebugEnabled()) {
568 logger.debug("Skipping Cleanup phase ...");
572 if (logger.isDebugEnabled()) {
573 logger.debug("Cleaning up temporary resources created for testing ...");
577 for (AccountValue pv : accValues.values()) {
578 deleteAccount(pv.getAccountId());
581 for (RoleValue rv : roleValues.values()) {
582 deleteRole(rv.getRoleId());
587 * Creates the account.
589 * @param userName the user name
590 * @param email the email
593 private String createAccount(String userName, String email) {
595 if (logger.isDebugEnabled()) {
596 testBanner("createAccount");
601 AccountClient accClient = new AccountClient();
602 AccountsCommon account = AccountFactory.createAccountInstance(
603 userName, userName, userName, email, accClient.getTenantId(),
604 true, false, true, true);
605 ClientResponse<Response> res = accClient.create(account);
606 int statusCode = res.getStatus();
607 if (logger.isDebugEnabled()) {
608 logger.debug("createAccount: userName=" + userName
609 + " status = " + statusCode);
611 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
612 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
613 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
614 res.releaseConnection();
615 return extractId(res);
621 * @param accId the acc id
623 private void deleteAccount(String accId) {
625 if (logger.isDebugEnabled()) {
626 testBanner("deleteAccount");
631 AccountClient accClient = new AccountClient();
632 ClientResponse<Response> res = accClient.delete(accId);
633 int statusCode = res.getStatus();
635 if (logger.isDebugEnabled()) {
636 logger.debug("deleteAccount: delete account id="
637 + accId + " status=" + statusCode);
639 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
640 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
641 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
643 res.releaseConnection();
650 * @param roleName the role name
653 private String createRole(String roleName) {
654 if (logger.isDebugEnabled()) {
655 testBanner("createRole");
658 RoleClient roleClient = new RoleClient();
660 Role role = RoleFactory.createRoleInstance(roleName,
661 roleName, //the display name
662 "role for " + roleName, true);
663 ClientResponse<Response> res = roleClient.create(role);
664 int statusCode = res.getStatus();
665 if (logger.isDebugEnabled()) {
666 logger.debug("createRole: name=" + roleName
667 + " status = " + statusCode);
669 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
670 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
671 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
672 res.releaseConnection();
673 return extractId(res);
679 * @param roleId the role id
681 private void deleteRole(String roleId) {
682 if (logger.isDebugEnabled()) {
683 testBanner("deleteRole");
686 RoleClient roleClient = new RoleClient();
687 ClientResponse<Response> res = roleClient.delete(roleId);
688 int statusCode = res.getStatus();
690 if (logger.isDebugEnabled()) {
691 logger.debug("deleteRole: delete role id=" + roleId
692 + " status=" + statusCode);
694 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
695 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
696 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
698 res.releaseConnection();
703 protected String getServiceName() {
704 // AccountRoles service is a sub-service of the Account service, so we return Account's service name
705 return AccountClient.SERVICE_NAME;