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.account.AccountsCommon;
32 import org.collectionspace.services.authorization.AccountRole;
33 import org.collectionspace.services.authorization.AccountValue;
34 import org.collectionspace.services.authorization.Role;
35 import org.collectionspace.services.authorization.RoleValue;
36 import org.collectionspace.services.client.AccountClient;
37 import org.collectionspace.services.client.AccountFactory;
38 import org.collectionspace.services.client.AccountRoleClient;
39 import org.collectionspace.services.client.AccountRoleFactory;
40 import org.collectionspace.services.client.CollectionSpaceClient;
41 import org.collectionspace.services.client.RoleClient;
42 import org.collectionspace.services.client.RoleFactory;
43 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
44 import org.collectionspace.services.client.test.ServiceRequestType;
45 import org.collectionspace.services.jaxb.AbstractCommonList;
46 import org.jboss.resteasy.client.ClientResponse;
49 import org.testng.Assert;
50 import org.testng.annotations.Test;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.testng.annotations.AfterClass;
55 import org.testng.annotations.BeforeClass;
58 * AccountServiceTest, carries out tests against a
59 * deployed and running Account, Role and AccountRole Services.
61 * $LastChangedRevision: 917 $
62 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
64 public class AccountRoleServiceTest extends AbstractServiceTestImpl {
66 /** The Constant logger. */
67 private final static String CLASS_NAME = AccountRoleServiceTest.class.getName();
68 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;
74 /** The all resource ids created. */
75 private List<String> allResourceIdsCreated = new ArrayList<String>();
77 /** The acc values. */
78 private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
80 /** The role values. */
81 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
83 * This method is called only by the parent class, AbstractServiceTestImpl
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
90 protected String getServicePathComponent() {
91 return new AccountRoleClient().getServicePathComponent();
97 @BeforeClass(alwaysRun = true)
98 public void seedData() {
99 String userId = "acc-role-user1";
100 String accId = createAccount(userId, "acc-role-test@cspace.org");
101 AccountValue ava = new AccountValue();
102 ava.setScreenName(userId);
103 ava.setUserId(userId);
104 ava.setAccountId(accId);
105 accValues.put(ava.getScreenName(), ava);
107 String userId2 = "acc-role-user2";
108 String coAccId = createAccount(userId2, "acc-role-test@cspace.org");
109 AccountValue avc = new AccountValue();
110 avc.setScreenName(userId2);
111 avc.setUserId(userId2);
112 avc.setAccountId(coAccId);
113 accValues.put(avc.getScreenName(), avc);
115 String ri = "acc-role-user3";
116 String iAccId = createAccount(ri, "acc-role-test@cspace.org");
117 AccountValue avi = new AccountValue();
118 avi.setScreenName(ri);
120 avi.setAccountId(iAccId);
121 accValues.put(avi.getScreenName(), avi);
123 String rn1 = "ROLE_CO1";
124 String r1RoleId = createRole(rn1);
125 RoleValue rv1 = new RoleValue();
126 rv1.setRoleId(r1RoleId);
127 rv1.setRoleName(rn1);
128 roleValues.put(rv1.getRoleName(), rv1);
130 String rn2 = "ROLE_CO2";
131 String r2RoleId = createRole(rn2);
132 RoleValue rv2 = new RoleValue();
133 rv2.setRoleId(r2RoleId);
134 rv2.setRoleName(rn2);
135 roleValues.put(rv2.getRoleName(), rv2);
139 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
142 protected CollectionSpaceClient getClientInstance() {
143 return new AccountRoleClient();
147 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
150 protected AbstractCommonList getAbstractCommonList(
151 ClientResponse<AbstractCommonList> response) {
152 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
153 throw new UnsupportedOperationException();
157 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
159 @Test(dataProvider = "testName")
161 public void readPaginatedList(String testName) throws Exception {
162 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
165 // ---------------------------------------------------------------
166 // CRUD tests : CREATE tests
167 // ---------------------------------------------------------------
170 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
173 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
174 public void create(String testName) throws Exception {
176 if (logger.isDebugEnabled()) {
177 testBanner(testName, CLASS_NAME);
179 // Perform setup, such as initializing the type of service request
180 // (e.g. CREATE, DELETE), its valid and expected status codes, and
181 // its associated HTTP method name (e.g. POST, DELETE).
184 // Submit the request to the service and store the response.
185 AccountValue pv = accValues.get("acc-role-user1");
186 AccountRole accRole = createAccountRoleInstance(pv,
187 roleValues.values(), true, true);
188 AccountRoleClient client = new AccountRoleClient();
189 ClientResponse<Response> res = client.create(pv.getAccountId(), accRole);
191 int statusCode = res.getStatus();
193 if (logger.isDebugEnabled()) {
194 logger.debug(testName + ": status = " + statusCode);
196 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
197 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
198 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
200 // Store the ID returned from this create operation
201 // for additional tests below.
202 //this is is not important in case of this relationship
203 knownResourceId = extractId(res);
204 if (logger.isDebugEnabled()) {
205 logger.debug(testName + ": knownResourceId=" + knownResourceId);
208 res.releaseConnection();
212 //to not cause uniqueness violation for accRole, createList is removed
214 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
216 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
217 dependsOnMethods = {"create"})
219 public void createList(String testName) throws Exception {
220 //FIXME: Should this test really be empty? If so, please comment accordingly.
224 // Placeholders until the three tests below can be uncommented.
225 // See Issue CSPACE-401.
227 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
230 public void createWithEmptyEntityBody(String testName) throws Exception {
231 //FIXME: Should this test really be empty? If so, please comment accordingly.
235 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
238 public void createWithMalformedXml(String testName) throws Exception {
239 //FIXME: Should this test really be empty? If so, please comment accordingly.
243 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
246 public void createWithWrongXmlSchema(String testName) throws Exception {
247 //FIXME: Should this test really be empty? If so, please comment accordingly.
250 // ---------------------------------------------------------------
251 // CRUD tests : READ tests
252 // ---------------------------------------------------------------
255 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
258 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
259 dependsOnMethods = {"create"})
260 public void read(String testName) throws Exception {
262 if (logger.isDebugEnabled()) {
263 testBanner(testName, CLASS_NAME);
269 // Submit the request to the service and store the response.
270 AccountRoleClient client = new AccountRoleClient();
271 ClientResponse<AccountRole> res = client.read(
272 accValues.get("acc-role-user1").getAccountId(), "123");
273 int statusCode = res.getStatus();
275 // Check the status code of the response: does it match
276 // the expected response(s)?
277 if (logger.isDebugEnabled()) {
278 logger.debug(testName + ": status = " + statusCode);
280 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
281 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
282 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
284 AccountRole output = (AccountRole) res.getEntity();
285 Assert.assertNotNull(output);
287 res.releaseConnection();
293 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
296 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
297 public void readNonExistent(String testName) throws Exception {
299 if (logger.isDebugEnabled()) {
300 testBanner(testName, CLASS_NAME);
304 setupReadNonExistent();
306 // Submit the request to the service and store the response.
307 AccountRoleClient client = new AccountRoleClient();
308 ClientResponse<AccountRole> res = client.read(this.NON_EXISTENT_ID, "123");
309 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);
320 res.releaseConnection();
324 // ---------------------------------------------------------------
325 // CRUD tests : READ_LIST tests
326 // ---------------------------------------------------------------
329 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
332 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
333 dependsOnMethods = {"createList", "read"})
334 public void readList(String testName) throws Exception {
335 //FIXME: Should this test really be empty? If so, please comment accordingly.
340 // ---------------------------------------------------------------
341 // CRUD tests : UPDATE tests
342 // ---------------------------------------------------------------
345 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
348 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
349 dependsOnMethods = {"read", "readList", "readNonExistent"})
350 public void update(String testName) throws Exception {
351 //FIXME: Should this test really be empty? If so, please comment accordingly.
355 // Placeholders until the three tests below can be uncommented.
356 // See Issue CSPACE-401.
358 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
361 public void updateWithEmptyEntityBody(String testName) throws Exception {
362 //FIXME: Should this test really be empty? If so, please comment accordingly.
366 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
369 public void updateWithMalformedXml(String testName) throws Exception {
370 //FIXME: Should this test really be empty? If so, please comment accordingly.
374 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
377 public void updateWithWrongXmlSchema(String testName) throws Exception {
378 //FIXME: Should this test really be empty? If so, please comment accordingly.
382 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
385 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
386 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
387 public void updateNonExistent(String testName) throws Exception {
388 //FIXME: Should this test really be empty? If so, please comment accordingly.
391 // ---------------------------------------------------------------
392 // CRUD tests : DELETE tests
393 // ---------------------------------------------------------------
396 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
399 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
400 dependsOnMethods = {"read"})
401 public void delete(String testName) throws Exception {
403 if (logger.isDebugEnabled()) {
404 testBanner(testName, CLASS_NAME);
410 // Submit the request to the service and store the response.
411 AccountRoleClient client = new AccountRoleClient();
412 ClientResponse<Response> res = client.delete(
413 accValues.get("acc-role-user1").getAccountId(), "123");
414 int statusCode = res.getStatus();
416 // Check the status code of the response: does it match
417 // the expected response(s)?
418 if (logger.isDebugEnabled()) {
419 logger.debug(testName + ": status = " + statusCode);
421 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
422 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
423 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
425 res.releaseConnection();
431 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
434 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
435 public void deleteNonExistent(String testName) throws Exception {
436 //ignoring this test as the service side returns 200 now even if it does
437 //not find a record in the db
440 // ---------------------------------------------------------------
441 // Utility tests : tests of code used in tests above
442 // ---------------------------------------------------------------
444 * Tests the code for manually submitting data that is used by several
445 * of the methods above.
448 @Test(dependsOnMethods = {"create"})
449 public void testSubmitRequest() throws Exception {
451 // Expected status code: 200 OK
452 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
454 // Submit the request to the service and store the response.
455 String method = ServiceRequestType.READ.httpMethodName();
456 String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
457 int statusCode = submitRequest(method, url);
459 // Check the status code of the response: does it match
460 // the expected response(s)?
461 if (logger.isDebugEnabled()) {
462 logger.debug("testSubmitRequest: url=" + url
463 + " status=" + statusCode);
465 Assert.assertEquals(statusCode, EXPECTED_STATUS);
469 // ---------------------------------------------------------------
470 // Utility methods used by tests above
471 // ---------------------------------------------------------------
473 * Creates the account role instance.
477 * @param usePermId the use perm id
478 * @param useRoleId the use role id
479 * @return the account role
481 static public AccountRole createAccountRoleInstance(AccountValue pv,
482 Collection<RoleValue> rvs,
486 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
487 pv, rvs, usePermId, useRoleId);
489 if (logger.isDebugEnabled()) {
490 logger.debug("to be created, accRole common");
491 logger.debug(objectAsXmlString(accRole, AccountRole.class));
499 @AfterClass(alwaysRun = true)
501 public void cleanUp() {
505 String noTest = System.getProperty("noTestCleanup");
506 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
507 if (logger.isDebugEnabled()) {
508 logger.debug("Skipping Cleanup phase ...");
512 if (logger.isDebugEnabled()) {
513 logger.debug("Cleaning up temporary resources created for testing ...");
516 AccountRoleClient client = new AccountRoleClient();
517 for (String resourceId : allResourceIdsCreated) {
518 ClientResponse<Response> res = client.delete(resourceId, "123");
520 int statusCode = res.getStatus();
521 if (logger.isDebugEnabled()) {
522 logger.debug("clenaup: delete relationships for accission id="
523 + resourceId + " status=" + statusCode);
525 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
526 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
527 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
529 res.releaseConnection();
533 for (AccountValue pv : accValues.values()) {
534 deleteAccount(pv.getAccountId());
537 for (RoleValue rv : roleValues.values()) {
538 deleteRole(rv.getRoleId());
543 * Creates the account.
545 * @param userName the user name
546 * @param email the email
549 private String createAccount(String userName, String email) {
551 if (logger.isDebugEnabled()) {
552 testBanner("createAccount");
557 AccountClient accClient = new AccountClient();
558 AccountsCommon account = AccountFactory.createAccountInstance(
559 userName, userName, userName, email,
560 true, true, false, true, true);
561 ClientResponse<Response> res = accClient.create(account);
562 int statusCode = res.getStatus();
563 if (logger.isDebugEnabled()) {
564 logger.debug("createAccount: userName=" + userName
565 + " status = " + statusCode);
567 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
568 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
569 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
570 res.releaseConnection();
571 return extractId(res);
577 * @param accId the acc id
579 private void deleteAccount(String accId) {
581 if (logger.isDebugEnabled()) {
582 testBanner("deleteAccount");
587 AccountClient accClient = new AccountClient();
588 ClientResponse<Response> res = accClient.delete(accId);
589 int statusCode = res.getStatus();
591 if (logger.isDebugEnabled()) {
592 logger.debug("deleteAccount: delete account id="
593 + accId + " status=" + statusCode);
595 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
596 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
597 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
599 res.releaseConnection();
606 * @param roleName the role name
609 private String createRole(String roleName) {
610 if (logger.isDebugEnabled()) {
611 testBanner("createRole");
614 RoleClient roleClient = new RoleClient();
616 Role role = RoleFactory.createRoleInstance(roleName,
617 "role for " + roleName, true);
618 ClientResponse<Response> res = roleClient.create(role);
619 int statusCode = res.getStatus();
620 if (logger.isDebugEnabled()) {
621 logger.debug("createRole: name=" + roleName
622 + " status = " + statusCode);
624 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
625 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
626 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
627 res.releaseConnection();
628 return extractId(res);
634 * @param roleId the role id
636 private void deleteRole(String roleId) {
637 if (logger.isDebugEnabled()) {
638 testBanner("deleteRole");
641 RoleClient roleClient = new RoleClient();
642 ClientResponse<Response> res = roleClient.delete(roleId);
643 int statusCode = res.getStatus();
645 if (logger.isDebugEnabled()) {
646 logger.debug("deleteRole: delete role id=" + roleId
647 + " status=" + statusCode);
649 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
650 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
651 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
653 res.releaseConnection();