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.CollectionSpaceClient;
40 import org.collectionspace.services.client.RoleClient;
41 import org.collectionspace.services.client.RoleFactory;
42 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
43 import org.collectionspace.services.client.test.ServiceRequestType;
44 import org.collectionspace.services.jaxb.AbstractCommonList;
45 import org.jboss.resteasy.client.ClientResponse;
48 import org.testng.Assert;
49 import org.testng.annotations.Test;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.testng.annotations.AfterClass;
54 import org.testng.annotations.BeforeClass;
57 * AccountServiceTest, carries out tests against a
58 * deployed and running Account, Role and AccountRole Services.
60 * $LastChangedRevision: 917 $
61 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
63 public class AccountRoleServiceTest extends AbstractServiceTestImpl {
65 static private final Logger logger =
66 LoggerFactory.getLogger(AccountRoleServiceTest.class);
67 // Instance variables specific to this test.
68 private String knownResourceId = null;
69 private List<String> allResourceIdsCreated = new ArrayList<String>();
70 private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
71 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
73 * This method is called only by the parent class, AbstractServiceTestImpl
77 protected String getServicePathComponent() {
78 return new AccountRoleClient().getServicePathComponent();
81 @BeforeClass(alwaysRun = true)
82 public void seedData() {
83 String ra = "acc-role-user1";
84 String accId = createAccount(ra, "acc-role-test@cspace.org");
85 AccountValue ava = new AccountValue();
86 ava.setScreenName(ra);
88 ava.setAccountId(accId);
89 accValues.put(ava.getScreenName(), ava);
91 String rc = "acc-role-user2";
92 String coAccId = createAccount(rc, "acc-role-test@cspace.org");
93 AccountValue avc = new AccountValue();
94 avc.setScreenName(rc);
96 avc.setAccountId(coAccId);
97 accValues.put(avc.getScreenName(), avc);
99 String ri = "acc-role-user3";
100 String iAccId = createAccount(ri, "acc-role-test@cspace.org");
101 AccountValue avi = new AccountValue();
102 avi.setScreenName(ri);
104 avi.setAccountId(iAccId);
105 accValues.put(avi.getScreenName(), avi);
107 String rn1 = "ROLE_CO1";
108 String r1RoleId = createRole(rn1);
109 RoleValue rv1 = new RoleValue();
110 rv1.setRoleId(r1RoleId);
111 rv1.setRoleName(rn1);
112 roleValues.put(rv1.getRoleName(), rv1);
114 String rn2 = "ROLE_CO2";
115 String r2RoleId = createRole(rn2);
116 RoleValue rv2 = new RoleValue();
117 rv2.setRoleId(r2RoleId);
118 rv2.setRoleName(rn2);
119 roleValues.put(rv2.getRoleName(), rv2);
123 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
126 protected CollectionSpaceClient getClientInstance() {
127 return new AccountRoleClient();
131 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
134 protected AbstractCommonList getAbstractCommonList(
135 ClientResponse<AbstractCommonList> response) {
136 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
137 throw new UnsupportedOperationException();
140 @Test(dataProvider = "testName")
142 public void readPaginatedList(String testName) throws Exception {
143 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
146 // ---------------------------------------------------------------
147 // CRUD tests : CREATE tests
148 // ---------------------------------------------------------------
151 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
152 public void create(String testName) throws Exception {
154 // Perform setup, such as initializing the type of service request
155 // (e.g. CREATE, DELETE), its valid and expected status codes, and
156 // its associated HTTP method name (e.g. POST, DELETE).
157 setupCreate(testName);
159 // Submit the request to the service and store the response.
160 AccountValue pv = accValues.get("acc-role-user1");
161 AccountRole accRole = createAccountRoleInstance(pv,
162 roleValues.values(), true, true);
163 AccountRoleClient client = new AccountRoleClient();
164 ClientResponse<Response> res = client.create(pv.getAccountId(), accRole);
165 int statusCode = res.getStatus();
167 if (logger.isDebugEnabled()) {
168 logger.debug(testName + ": status = " + statusCode);
170 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
171 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
172 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
174 // Store the ID returned from this create operation
175 // for additional tests below.
176 //this is is not important in case of this relationship
177 knownResourceId = extractId(res);
178 if (logger.isDebugEnabled()) {
179 logger.debug(testName + ": knownResourceId=" + knownResourceId);
183 //to not cause uniqueness violation for accRole, createList is removed
185 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
186 dependsOnMethods = {"create"})
187 public void createList(String testName) throws Exception {
188 //FIXME: Should this test really be empty? If so, please comment accordingly.
192 // Placeholders until the three tests below can be uncommented.
193 // See Issue CSPACE-401.
195 public void createWithEmptyEntityBody(String testName) throws Exception {
196 //FIXME: Should this test really be empty? If so, please comment accordingly.
200 public void createWithMalformedXml(String testName) throws Exception {
201 //FIXME: Should this test really be empty? If so, please comment accordingly.
205 public void createWithWrongXmlSchema(String testName) throws Exception {
206 //FIXME: Should this test really be empty? If so, please comment accordingly.
209 // ---------------------------------------------------------------
210 // CRUD tests : READ tests
211 // ---------------------------------------------------------------
214 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
215 dependsOnMethods = {"create"})
216 public void read(String testName) throws Exception {
221 // Submit the request to the service and store the response.
222 AccountRoleClient client = new AccountRoleClient();
223 ClientResponse<AccountRole> res = client.read(
224 accValues.get("acc-role-user1").getAccountId(), "123");
225 int statusCode = res.getStatus();
227 // Check the status code of the response: does it match
228 // the expected response(s)?
229 if (logger.isDebugEnabled()) {
230 logger.debug(testName + ": status = " + statusCode);
232 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
233 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
234 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
236 AccountRole output = (AccountRole) res.getEntity();
237 Assert.assertNotNull(output);
242 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
243 public void readNonExistent(String testName) throws Exception {
246 setupReadNonExistent(testName);
248 // Submit the request to the service and store the response.
249 AccountRoleClient client = new AccountRoleClient();
250 ClientResponse<AccountRole> res = client.read(this.NON_EXISTENT_ID, "123");
251 int statusCode = res.getStatus();
253 // Check the status code of the response: does it match
254 // the expected response(s)?
255 if (logger.isDebugEnabled()) {
256 logger.debug(testName + ": status = " + statusCode);
258 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
259 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
260 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
263 // ---------------------------------------------------------------
264 // CRUD tests : READ_LIST tests
265 // ---------------------------------------------------------------
268 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
269 dependsOnMethods = {"createList", "read"})
270 public void readList(String testName) throws Exception {
271 //FIXME: Should this test really be empty? If so, please comment accordingly.
276 // ---------------------------------------------------------------
277 // CRUD tests : UPDATE tests
278 // ---------------------------------------------------------------
281 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
282 dependsOnMethods = {"read", "readList", "readNonExistent"})
283 public void update(String testName) throws Exception {
284 //FIXME: Should this test really be empty? If so, please comment accordingly.
288 // Placeholders until the three tests below can be uncommented.
289 // See Issue CSPACE-401.
291 public void updateWithEmptyEntityBody(String testName) throws Exception {
292 //FIXME: Should this test really be empty? If so, please comment accordingly.
296 public void updateWithMalformedXml(String testName) throws Exception {
297 //FIXME: Should this test really be empty? If so, please comment accordingly.
301 public void updateWithWrongXmlSchema(String testName) throws Exception {
302 //FIXME: Should this test really be empty? If so, please comment accordingly.
306 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
307 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
308 public void updateNonExistent(String testName) throws Exception {
309 //FIXME: Should this test really be empty? If so, please comment accordingly.
312 // ---------------------------------------------------------------
313 // CRUD tests : DELETE tests
314 // ---------------------------------------------------------------
317 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
318 dependsOnMethods = {"read"})
319 public void delete(String testName) throws Exception {
322 setupDelete(testName);
324 // Submit the request to the service and store the response.
325 AccountRoleClient client = new AccountRoleClient();
326 ClientResponse<Response> res = client.delete(
327 accValues.get("acc-role-user1").getAccountId(), "123");
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, EXPECTED_STATUS_CODE);
342 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
345 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
346 public void deleteNonExistent(String testName) throws Exception {
347 //ignoring this test as the service side returns 200 now even if it does
348 //not find a record in the db
351 // ---------------------------------------------------------------
352 // Utility tests : tests of code used in tests above
353 // ---------------------------------------------------------------
355 * Tests the code for manually submitting data that is used by several
356 * of the methods above.
358 @Test(dependsOnMethods = {"create"})
359 public void testSubmitRequest() throws Exception {
361 // Expected status code: 200 OK
362 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
364 // Submit the request to the service and store the response.
365 String method = ServiceRequestType.READ.httpMethodName();
366 String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
367 int statusCode = submitRequest(method, url);
369 // Check the status code of the response: does it match
370 // the expected response(s)?
371 if (logger.isDebugEnabled()) {
372 logger.debug("testSubmitRequest: url=" + url
373 + " status=" + statusCode);
375 Assert.assertEquals(statusCode, EXPECTED_STATUS);
379 // ---------------------------------------------------------------
380 // Utility methods used by tests above
381 // ---------------------------------------------------------------
383 * Creates the account role instance.
387 * @param usePermId the use perm id
388 * @param useRoleId the use role id
389 * @return the account role
391 static public AccountRole createAccountRoleInstance(AccountValue pv,
392 Collection<RoleValue> rvs,
396 AccountRole accRole = new AccountRole();
397 //service consume is not required to provide subject as it is determined
399 // accRole.setSubject(SubjectType.ROLE);
401 ArrayList<AccountValue> pvs = new ArrayList<AccountValue>();
403 accRole.setAccounts(pvs);
406 //FIXME is there a better way?
407 ArrayList<RoleValue> rvas = new ArrayList<RoleValue>();
408 for (RoleValue rv : rvs) {
411 accRole.setRoles(rvas);
414 if (logger.isDebugEnabled()) {
415 logger.debug("to be created, accRole common");
416 logger.debug(objectAsXmlString(accRole, AccountRole.class));
421 @AfterClass(alwaysRun = true)
422 public void cleanUp() {
423 setupDelete("delete");
424 String noTest = System.getProperty("noTestCleanup");
425 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
426 if (logger.isDebugEnabled()) {
427 logger.debug("Skipping Cleanup phase ...");
431 if (logger.isDebugEnabled()) {
432 logger.debug("Cleaning up temporary resources created for testing ...");
434 AccountRoleClient client = new AccountRoleClient();
435 for (String resourceId : allResourceIdsCreated) {
437 ClientResponse<Response> res = client.delete(resourceId, "123");
438 int statusCode = res.getStatus();
439 if (logger.isDebugEnabled()) {
440 logger.debug("clenaup: delete relationships for accission id="
441 + resourceId + " status=" + statusCode);
443 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
444 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
445 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
448 for (AccountValue pv : accValues.values()) {
449 deleteAccount(pv.getAccountId());
452 for (RoleValue rv : roleValues.values()) {
453 deleteRole(rv.getRoleId());
457 private String createAccount(String userName, String email) {
459 AccountClient accClient = new AccountClient();
460 AccountsCommon account = AccountFactory.createAccountInstance(
461 userName, userName, userName, email,
462 true, true, false, true, true);
463 ClientResponse<Response> res = accClient.create(account);
464 int statusCode = res.getStatus();
465 if (logger.isDebugEnabled()) {
466 logger.debug("createAccount: userName=" + userName
467 + " status = " + statusCode);
469 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
470 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
471 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
472 return extractId(res);
475 private void deleteAccount(String accId) {
477 AccountClient accClient = new AccountClient();
478 ClientResponse<Response> res = accClient.delete(accId);
479 int statusCode = res.getStatus();
480 if (logger.isDebugEnabled()) {
481 logger.debug("deleteAccount: delete account id="
482 + accId + " status=" + statusCode);
484 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
485 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
486 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
489 private String createRole(String roleName) {
491 RoleClient roleClient = new RoleClient();
493 Role role = RoleFactory.createRoleInstance(roleName,
494 "role for " + roleName, true);
495 ClientResponse<Response> res = roleClient.create(role);
496 int statusCode = res.getStatus();
497 if (logger.isDebugEnabled()) {
498 logger.debug("createRole: name=" + roleName
499 + " status = " + statusCode);
501 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
502 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
503 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
504 return extractId(res);
507 private void deleteRole(String roleId) {
509 RoleClient roleClient = new RoleClient();
510 ClientResponse<Response> res = roleClient.delete(roleId);
511 int statusCode = res.getStatus();
512 if (logger.isDebugEnabled()) {
513 logger.debug("deleteRole: delete role id=" + roleId
514 + " status=" + statusCode);
516 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
517 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
518 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);