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 static private final Logger logger =
68 LoggerFactory.getLogger(AccountRoleServiceTest.class);
69 // Instance variables specific to this test.
70 /** The known resource id. */
71 private String knownResourceId = null;
73 /** The all resource ids created. */
74 private List<String> allResourceIdsCreated = new ArrayList<String>();
76 /** The acc values. */
77 private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
79 /** The role values. */
80 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
82 * This method is called only by the parent class, AbstractServiceTestImpl
86 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
89 protected String getServicePathComponent() {
90 return new AccountRoleClient().getServicePathComponent();
96 @BeforeClass(alwaysRun = true)
97 public void seedData() {
98 String userId = "acc-role-user1";
99 String accId = createAccount(userId, "acc-role-test@cspace.org");
100 AccountValue ava = new AccountValue();
101 ava.setScreenName(userId);
102 ava.setUserId(userId);
103 ava.setAccountId(accId);
104 accValues.put(ava.getScreenName(), ava);
106 String userId2 = "acc-role-user2";
107 String coAccId = createAccount(userId2, "acc-role-test@cspace.org");
108 AccountValue avc = new AccountValue();
109 avc.setScreenName(userId2);
110 avc.setUserId(userId2);
111 avc.setAccountId(coAccId);
112 accValues.put(avc.getScreenName(), avc);
114 String ri = "acc-role-user3";
115 String iAccId = createAccount(ri, "acc-role-test@cspace.org");
116 AccountValue avi = new AccountValue();
117 avi.setScreenName(ri);
119 avi.setAccountId(iAccId);
120 accValues.put(avi.getScreenName(), avi);
122 String rn1 = "ROLE_CO1";
123 String r1RoleId = createRole(rn1);
124 RoleValue rv1 = new RoleValue();
125 rv1.setRoleId(r1RoleId);
126 rv1.setRoleName(rn1);
127 roleValues.put(rv1.getRoleName(), rv1);
129 String rn2 = "ROLE_CO2";
130 String r2RoleId = createRole(rn2);
131 RoleValue rv2 = new RoleValue();
132 rv2.setRoleId(r2RoleId);
133 rv2.setRoleName(rn2);
134 roleValues.put(rv2.getRoleName(), rv2);
138 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
141 protected CollectionSpaceClient getClientInstance() {
142 return new AccountRoleClient();
146 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
149 protected AbstractCommonList getAbstractCommonList(
150 ClientResponse<AbstractCommonList> response) {
151 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
152 throw new UnsupportedOperationException();
156 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
158 @Test(dataProvider = "testName")
160 public void readPaginatedList(String testName) throws Exception {
161 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
164 // ---------------------------------------------------------------
165 // CRUD tests : CREATE tests
166 // ---------------------------------------------------------------
169 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
172 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
173 public void create(String testName) throws Exception {
175 // Perform setup, such as initializing the type of service request
176 // (e.g. CREATE, DELETE), its valid and expected status codes, and
177 // its associated HTTP method name (e.g. POST, DELETE).
178 setupCreate(testName);
180 // Submit the request to the service and store the response.
181 AccountValue pv = accValues.get("acc-role-user1");
182 AccountRole accRole = createAccountRoleInstance(pv,
183 roleValues.values(), true, true);
184 AccountRoleClient client = new AccountRoleClient();
185 ClientResponse<Response> res = client.create(pv.getAccountId(), accRole);
187 int statusCode = res.getStatus();
189 if (logger.isDebugEnabled()) {
190 logger.debug(testName + ": status = " + statusCode);
192 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
193 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
194 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
196 // Store the ID returned from this create operation
197 // for additional tests below.
198 //this is is not important in case of this relationship
199 knownResourceId = extractId(res);
200 if (logger.isDebugEnabled()) {
201 logger.debug(testName + ": knownResourceId=" + knownResourceId);
204 res.releaseConnection();
208 //to not cause uniqueness violation for accRole, createList is removed
210 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
212 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
213 dependsOnMethods = {"create"})
215 public void createList(String testName) throws Exception {
216 //FIXME: Should this test really be empty? If so, please comment accordingly.
220 // Placeholders until the three tests below can be uncommented.
221 // See Issue CSPACE-401.
223 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
226 public void createWithEmptyEntityBody(String testName) throws Exception {
227 //FIXME: Should this test really be empty? If so, please comment accordingly.
231 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
234 public void createWithMalformedXml(String testName) throws Exception {
235 //FIXME: Should this test really be empty? If so, please comment accordingly.
239 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
242 public void createWithWrongXmlSchema(String testName) throws Exception {
243 //FIXME: Should this test really be empty? If so, please comment accordingly.
246 // ---------------------------------------------------------------
247 // CRUD tests : READ tests
248 // ---------------------------------------------------------------
251 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
254 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
255 dependsOnMethods = {"create"})
256 public void read(String testName) throws Exception {
261 // Submit the request to the service and store the response.
262 AccountRoleClient client = new AccountRoleClient();
263 ClientResponse<AccountRole> res = client.read(
264 accValues.get("acc-role-user1").getAccountId(), "123");
265 int statusCode = res.getStatus();
267 // Check the status code of the response: does it match
268 // the expected response(s)?
269 if (logger.isDebugEnabled()) {
270 logger.debug(testName + ": status = " + statusCode);
272 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
273 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
274 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
276 AccountRole output = (AccountRole) res.getEntity();
277 Assert.assertNotNull(output);
279 res.releaseConnection();
285 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
288 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
289 public void readNonExistent(String testName) throws Exception {
292 setupReadNonExistent(testName);
294 // Submit the request to the service and store the response.
295 AccountRoleClient client = new AccountRoleClient();
296 ClientResponse<AccountRole> res = client.read(this.NON_EXISTENT_ID, "123");
297 int statusCode = res.getStatus();
299 // Check the status code of the response: does it match
300 // the expected response(s)?
301 if (logger.isDebugEnabled()) {
302 logger.debug(testName + ": status = " + statusCode);
304 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
305 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
306 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
308 res.releaseConnection();
312 // ---------------------------------------------------------------
313 // CRUD tests : READ_LIST tests
314 // ---------------------------------------------------------------
317 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
320 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
321 dependsOnMethods = {"createList", "read"})
322 public void readList(String testName) throws Exception {
323 //FIXME: Should this test really be empty? If so, please comment accordingly.
328 // ---------------------------------------------------------------
329 // CRUD tests : UPDATE tests
330 // ---------------------------------------------------------------
333 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
336 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
337 dependsOnMethods = {"read", "readList", "readNonExistent"})
338 public void update(String testName) throws Exception {
339 //FIXME: Should this test really be empty? If so, please comment accordingly.
343 // Placeholders until the three tests below can be uncommented.
344 // See Issue CSPACE-401.
346 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
349 public void updateWithEmptyEntityBody(String testName) throws Exception {
350 //FIXME: Should this test really be empty? If so, please comment accordingly.
354 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
357 public void updateWithMalformedXml(String testName) throws Exception {
358 //FIXME: Should this test really be empty? If so, please comment accordingly.
362 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
365 public void updateWithWrongXmlSchema(String testName) throws Exception {
366 //FIXME: Should this test really be empty? If so, please comment accordingly.
370 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
373 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
374 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
375 public void updateNonExistent(String testName) throws Exception {
376 //FIXME: Should this test really be empty? If so, please comment accordingly.
379 // ---------------------------------------------------------------
380 // CRUD tests : DELETE tests
381 // ---------------------------------------------------------------
384 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
387 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
388 dependsOnMethods = {"read"})
389 public void delete(String testName) throws Exception {
391 setupDelete(testName);
393 // Submit the request to the service and store the response.
394 AccountRoleClient client = new AccountRoleClient();
395 ClientResponse<Response> res = client.delete(
396 accValues.get("acc-role-user1").getAccountId(), "123");
397 int statusCode = res.getStatus();
399 // Check the status code of the response: does it match
400 // the expected response(s)?
401 if (logger.isDebugEnabled()) {
402 logger.debug(testName + ": status = " + statusCode);
404 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
405 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
406 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
408 res.releaseConnection();
414 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
417 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
418 public void deleteNonExistent(String testName) throws Exception {
419 //ignoring this test as the service side returns 200 now even if it does
420 //not find a record in the db
423 // ---------------------------------------------------------------
424 // Utility tests : tests of code used in tests above
425 // ---------------------------------------------------------------
427 * Tests the code for manually submitting data that is used by several
428 * of the methods above.
431 @Test(dependsOnMethods = {"create"})
432 public void testSubmitRequest() throws Exception {
434 // Expected status code: 200 OK
435 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
437 // Submit the request to the service and store the response.
438 String method = ServiceRequestType.READ.httpMethodName();
439 String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
440 int statusCode = submitRequest(method, url);
442 // Check the status code of the response: does it match
443 // the expected response(s)?
444 if (logger.isDebugEnabled()) {
445 logger.debug("testSubmitRequest: url=" + url
446 + " status=" + statusCode);
448 Assert.assertEquals(statusCode, EXPECTED_STATUS);
452 // ---------------------------------------------------------------
453 // Utility methods used by tests above
454 // ---------------------------------------------------------------
456 * Creates the account role instance.
460 * @param usePermId the use perm id
461 * @param useRoleId the use role id
462 * @return the account role
464 static public AccountRole createAccountRoleInstance(AccountValue pv,
465 Collection<RoleValue> rvs,
469 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
470 pv, rvs, usePermId, useRoleId);
472 if (logger.isDebugEnabled()) {
473 logger.debug("to be created, accRole common");
474 logger.debug(objectAsXmlString(accRole, AccountRole.class));
482 @AfterClass(alwaysRun = true)
484 public void cleanUp() {
485 setupDelete("delete");
487 String noTest = System.getProperty("noTestCleanup");
488 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
489 if (logger.isDebugEnabled()) {
490 logger.debug("Skipping Cleanup phase ...");
494 if (logger.isDebugEnabled()) {
495 logger.debug("Cleaning up temporary resources created for testing ...");
498 AccountRoleClient client = new AccountRoleClient();
499 for (String resourceId : allResourceIdsCreated) {
500 ClientResponse<Response> res = client.delete(resourceId, "123");
502 int statusCode = res.getStatus();
503 if (logger.isDebugEnabled()) {
504 logger.debug("clenaup: delete relationships for accission id="
505 + resourceId + " status=" + statusCode);
507 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
508 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
509 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
511 res.releaseConnection();
515 for (AccountValue pv : accValues.values()) {
516 deleteAccount(pv.getAccountId());
519 for (RoleValue rv : roleValues.values()) {
520 deleteRole(rv.getRoleId());
525 * Creates the account.
527 * @param userName the user name
528 * @param email the email
531 private String createAccount(String userName, String email) {
533 AccountClient accClient = new AccountClient();
534 AccountsCommon account = AccountFactory.createAccountInstance(
535 userName, userName, userName, email,
536 true, true, false, true, true);
537 ClientResponse<Response> res = accClient.create(account);
538 int statusCode = res.getStatus();
539 if (logger.isDebugEnabled()) {
540 logger.debug("createAccount: userName=" + userName
541 + " status = " + statusCode);
543 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
544 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
545 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
546 res.releaseConnection();
547 return extractId(res);
553 * @param accId the acc id
555 private void deleteAccount(String accId) {
557 AccountClient accClient = new AccountClient();
558 ClientResponse<Response> res = accClient.delete(accId);
559 int statusCode = res.getStatus();
561 if (logger.isDebugEnabled()) {
562 logger.debug("deleteAccount: delete account id="
563 + accId + " status=" + statusCode);
565 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
566 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
567 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
569 res.releaseConnection();
576 * @param roleName the role name
579 private String createRole(String roleName) {
581 RoleClient roleClient = new RoleClient();
583 Role role = RoleFactory.createRoleInstance(roleName,
584 "role for " + roleName, true);
585 ClientResponse<Response> res = roleClient.create(role);
586 int statusCode = res.getStatus();
587 if (logger.isDebugEnabled()) {
588 logger.debug("createRole: name=" + roleName
589 + " status = " + statusCode);
591 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
592 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
593 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
594 res.releaseConnection();
595 return extractId(res);
601 * @param roleId the role id
603 private void deleteRole(String roleId) {
605 RoleClient roleClient = new RoleClient();
606 ClientResponse<Response> res = roleClient.delete(roleId);
607 int statusCode = res.getStatus();
609 if (logger.isDebugEnabled()) {
610 logger.debug("deleteRole: delete role id=" + roleId
611 + " status=" + statusCode);
613 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
614 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
615 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
617 res.releaseConnection();