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);
69 // Instance variables specific to this test.
70 /** The known resource id. */
71 private String knownResourceId = null;
72 /** The all resource ids created. */
73 private List<String> allResourceIdsCreated = new ArrayList<String>();
74 /** The acc values. */
75 private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
76 /** The role values. */
77 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
79 * This method is called only by the parent class, AbstractServiceTestImpl
83 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
86 protected String getServicePathComponent() {
87 return new AccountRoleClient().getServicePathComponent();
93 @BeforeClass(alwaysRun = true)
94 public void seedData() {
95 String userId = "acc-role-user1";
96 String accId = createAccount(userId, "acc-role-user1-test@cspace.org");
97 AccountValue ava = new AccountValue();
98 ava.setScreenName(userId);
99 ava.setUserId(userId);
100 ava.setAccountId(accId);
101 accValues.put(ava.getScreenName(), ava);
103 String userId2 = "acc-role-user2";
104 String coAccId = createAccount(userId2, "acc-role-user2-test@cspace.org");
105 AccountValue avc = new AccountValue();
106 avc.setScreenName(userId2);
107 avc.setUserId(userId2);
108 avc.setAccountId(coAccId);
109 accValues.put(avc.getScreenName(), avc);
111 String rn1 = "ROLE_CO1";
112 String r1RoleId = createRole(rn1);
113 RoleValue rv1 = new RoleValue();
114 rv1.setRoleId(r1RoleId);
115 rv1.setRoleName(rn1);
116 roleValues.put(rv1.getRoleName(), rv1);
118 String rn2 = "ROLE_CO2";
119 String r2RoleId = createRole(rn2);
120 RoleValue rv2 = new RoleValue();
121 rv2.setRoleId(r2RoleId);
122 rv2.setRoleName(rn2);
123 roleValues.put(rv2.getRoleName(), rv2);
127 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
130 protected CollectionSpaceClient getClientInstance() {
131 return new AccountRoleClient();
135 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
138 protected AbstractCommonList getAbstractCommonList(
139 ClientResponse<AbstractCommonList> response) {
140 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
141 throw new UnsupportedOperationException();
145 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
147 @Test(dataProvider = "testName")
149 public void readPaginatedList(String testName) throws Exception {
150 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
153 // ---------------------------------------------------------------
154 // CRUD tests : CREATE tests
155 // ---------------------------------------------------------------
158 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
161 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
162 public void create(String testName) throws Exception {
164 if (logger.isDebugEnabled()) {
165 testBanner(testName, CLASS_NAME);
167 // Perform setup, such as initializing the type of service request
168 // (e.g. CREATE, DELETE), its valid and expected status codes, and
169 // its associated HTTP method name (e.g. POST, DELETE).
172 // Submit the request to the service and store the response.
173 AccountValue av = accValues.get("acc-role-user1");
174 AccountRole accRole = createAccountRoleInstance(av,
175 roleValues.values(), true, true);
176 AccountRoleClient client = new AccountRoleClient();
177 ClientResponse<Response> res = client.create(av.getAccountId(), accRole);
179 int statusCode = res.getStatus();
181 if (logger.isDebugEnabled()) {
182 logger.debug(testName + ": status = " + statusCode);
184 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
185 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
186 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
188 // Store the ID returned from this create operation
189 // for additional tests below.
190 //this is is not important in case of this relationship
191 knownResourceId = extractId(res);
192 if (logger.isDebugEnabled()) {
193 logger.debug(testName + ": knownResourceId=" + knownResourceId);
196 res.releaseConnection();
200 //to not cause uniqueness violation for accRole, createList is removed
202 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
204 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
205 dependsOnMethods = {"create"})
207 public void createList(String testName) throws Exception {
208 //FIXME: Should this test really be empty? If so, please comment accordingly.
212 // Placeholders until the three tests below can be uncommented.
213 // See Issue CSPACE-401.
215 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
218 public void createWithEmptyEntityBody(String testName) throws Exception {
219 //FIXME: Should this test really be empty? If so, please comment accordingly.
223 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
226 public void createWithMalformedXml(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#createWithWrongXmlSchema(java.lang.String)
234 public void createWithWrongXmlSchema(String testName) throws Exception {
235 //FIXME: Should this test really be empty? If so, please comment accordingly.
238 // ---------------------------------------------------------------
239 // CRUD tests : READ tests
240 // ---------------------------------------------------------------
243 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
246 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
247 dependsOnMethods = {"create"})
248 public void read(String testName) throws Exception {
250 if (logger.isDebugEnabled()) {
251 testBanner(testName, CLASS_NAME);
257 // Submit the request to the service and store the response.
258 AccountRoleClient client = new AccountRoleClient();
259 ClientResponse<AccountRole> res = client.read(
260 accValues.get("acc-role-user1").getAccountId(), "123");
261 int statusCode = res.getStatus();
263 // Check the status code of the response: does it match
264 // the expected response(s)?
265 if (logger.isDebugEnabled()) {
266 logger.debug(testName + ": status = " + statusCode);
268 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
269 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
270 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
272 AccountRole output = (AccountRole) res.getEntity();
273 Assert.assertNotNull(output);
275 res.releaseConnection();
281 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
284 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
285 public void readNonExistent(String testName) throws Exception {
287 if (logger.isDebugEnabled()) {
288 testBanner(testName, CLASS_NAME);
292 setupReadNonExistent();
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 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
313 dependsOnMethods = {"create"})
314 public void readNoRelationship(String testName) throws Exception {
316 if (logger.isDebugEnabled()) {
317 testBanner(testName, CLASS_NAME);
323 // Submit the request to the service and store the response.
324 AccountRoleClient client = new AccountRoleClient();
325 ClientResponse<AccountRole> res = client.read(
326 accValues.get("acc-role-user2").getAccountId(), "123");
327 int statusCode = res.getStatus();
329 // Check the status code of the response: does it match
330 // the expected response(s)?
331 if (logger.isDebugEnabled()) {
332 logger.debug(testName + ": status = " + statusCode);
334 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
335 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
336 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
337 AccountRole output = (AccountRole) res.getEntity();
339 String sOutput = objectAsXmlString(output, AccountRole.class);
340 if(logger.isDebugEnabled()) {
341 logger.debug(testName + " received " + sOutput);
344 res.releaseConnection();
348 // ---------------------------------------------------------------
349 // CRUD tests : READ_LIST tests
350 // ---------------------------------------------------------------
353 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
356 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
357 dependsOnMethods = {"createList", "read"})
358 public void readList(String testName) throws Exception {
359 //FIXME: Should this test really be empty? If so, please comment accordingly.
364 // ---------------------------------------------------------------
365 // CRUD tests : UPDATE tests
366 // ---------------------------------------------------------------
369 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
372 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
373 dependsOnMethods = {"read", "readList", "readNonExistent"})
374 public void update(String testName) throws Exception {
375 //FIXME: Should this test really be empty? If so, please comment accordingly.
379 // Placeholders until the three tests below can be uncommented.
380 // See Issue CSPACE-401.
382 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
385 public void updateWithEmptyEntityBody(String testName) throws Exception {
386 //FIXME: Should this test really be empty? If so, please comment accordingly.
390 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
393 public void updateWithMalformedXml(String testName) throws Exception {
394 //FIXME: Should this test really be empty? If so, please comment accordingly.
398 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
401 public void updateWithWrongXmlSchema(String testName) throws Exception {
402 //FIXME: Should this test really be empty? If so, please comment accordingly.
406 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
409 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
410 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
411 public void updateNonExistent(String testName) throws Exception {
412 //FIXME: Should this test really be empty? If so, please comment accordingly.
415 // ---------------------------------------------------------------
416 // CRUD tests : DELETE tests
417 // ---------------------------------------------------------------
420 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
423 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
424 dependsOnMethods = {"read"})
425 public void delete(String testName) throws Exception {
427 if (logger.isDebugEnabled()) {
428 testBanner(testName, CLASS_NAME);
434 // Submit the request to the service and store the response.
435 AccountRoleClient client = new AccountRoleClient();
436 AccountValue av = accValues.get("acc-role-user1");
437 AccountRole accRole = createAccountRoleInstance(av,
438 roleValues.values(), true, true);
439 ClientResponse<Response> res = client.delete(
440 accValues.get("acc-role-user1").getAccountId(), accRole);
441 int statusCode = res.getStatus();
443 // Check the status code of the response: does it match
444 // the expected response(s)?
445 if (logger.isDebugEnabled()) {
446 logger.debug(testName + ": status = " + statusCode);
448 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
449 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
450 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
452 res.releaseConnection();
458 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
461 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
462 public void deleteNonExistent(String testName) throws Exception {
463 //ignoring this test as the service side returns 200 now even if it does
464 //not find a record in the db
467 // ---------------------------------------------------------------
468 // Utility tests : tests of code used in tests above
469 // ---------------------------------------------------------------
471 * Tests the code for manually submitting data that is used by several
472 * of the methods above.
475 @Test(dependsOnMethods = {"create"})
476 public void testSubmitRequest() throws Exception {
478 // Expected status code: 200 OK
479 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
481 // Submit the request to the service and store the response.
482 String method = ServiceRequestType.READ.httpMethodName();
483 String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
484 int statusCode = submitRequest(method, url);
486 // Check the status code of the response: does it match
487 // the expected response(s)?
488 if (logger.isDebugEnabled()) {
489 logger.debug("testSubmitRequest: url=" + url
490 + " status=" + statusCode);
492 Assert.assertEquals(statusCode, EXPECTED_STATUS);
496 // ---------------------------------------------------------------
497 // Utility methods used by tests above
498 // ---------------------------------------------------------------
500 * Creates the account role instance.
504 * @param usePermId the use perm id
505 * @param useRoleId the use role id
506 * @return the account role
508 static public AccountRole createAccountRoleInstance(AccountValue pv,
509 Collection<RoleValue> rvs,
513 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
514 pv, rvs, usePermId, useRoleId);
516 if (logger.isDebugEnabled()) {
517 logger.debug("to be created, accRole common");
518 logger.debug(objectAsXmlString(accRole, AccountRole.class));
526 @AfterClass(alwaysRun = true)
528 public void cleanUp() {
532 String noTest = System.getProperty("noTestCleanup");
533 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
534 if (logger.isDebugEnabled()) {
535 logger.debug("Skipping Cleanup phase ...");
539 if (logger.isDebugEnabled()) {
540 logger.debug("Cleaning up temporary resources created for testing ...");
544 for (AccountValue pv : accValues.values()) {
545 deleteAccount(pv.getAccountId());
548 for (RoleValue rv : roleValues.values()) {
549 deleteRole(rv.getRoleId());
554 * Creates the account.
556 * @param userName the user name
557 * @param email the email
560 private String createAccount(String userName, String email) {
562 if (logger.isDebugEnabled()) {
563 testBanner("createAccount");
568 AccountClient accClient = new AccountClient();
569 AccountsCommon account = AccountFactory.createAccountInstance(
570 userName, userName, userName, email, accClient.getTenantId(),
571 true, false, true, true);
572 ClientResponse<Response> res = accClient.create(account);
573 int statusCode = res.getStatus();
574 if (logger.isDebugEnabled()) {
575 logger.debug("createAccount: userName=" + userName
576 + " status = " + statusCode);
578 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
579 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
580 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
581 res.releaseConnection();
582 return extractId(res);
588 * @param accId the acc id
590 private void deleteAccount(String accId) {
592 if (logger.isDebugEnabled()) {
593 testBanner("deleteAccount");
598 AccountClient accClient = new AccountClient();
599 ClientResponse<Response> res = accClient.delete(accId);
600 int statusCode = res.getStatus();
602 if (logger.isDebugEnabled()) {
603 logger.debug("deleteAccount: delete account id="
604 + accId + " status=" + statusCode);
606 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
607 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
608 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
610 res.releaseConnection();
617 * @param roleName the role name
620 private String createRole(String roleName) {
621 if (logger.isDebugEnabled()) {
622 testBanner("createRole");
625 RoleClient roleClient = new RoleClient();
627 Role role = RoleFactory.createRoleInstance(roleName,
628 "role for " + roleName, true);
629 ClientResponse<Response> res = roleClient.create(role);
630 int statusCode = res.getStatus();
631 if (logger.isDebugEnabled()) {
632 logger.debug("createRole: name=" + roleName
633 + " status = " + statusCode);
635 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
636 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
637 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
638 res.releaseConnection();
639 return extractId(res);
645 * @param roleId the role id
647 private void deleteRole(String roleId) {
648 if (logger.isDebugEnabled()) {
649 testBanner("deleteRole");
652 RoleClient roleClient = new RoleClient();
653 ClientResponse<Response> res = roleClient.delete(roleId);
654 int statusCode = res.getStatus();
656 if (logger.isDebugEnabled()) {
657 logger.debug("deleteRole: delete role id=" + roleId
658 + " status=" + statusCode);
660 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
661 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
662 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
664 res.releaseConnection();