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 static private final Logger logger =
67 LoggerFactory.getLogger(AccountRoleServiceTest.class);
68 // Instance variables specific to this test.
69 private String knownResourceId = null;
70 private List<String> allResourceIdsCreated = new ArrayList<String>();
71 private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
72 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
74 * This method is called only by the parent class, AbstractServiceTestImpl
78 protected String getServicePathComponent() {
79 return new AccountRoleClient().getServicePathComponent();
82 @BeforeClass(alwaysRun = true)
83 public void seedData() {
84 String userId = "acc-role-user1";
85 String accId = createAccount(userId, "acc-role-test@cspace.org");
86 AccountValue ava = new AccountValue();
87 ava.setScreenName(userId);
88 ava.setUserId(userId);
89 ava.setAccountId(accId);
90 accValues.put(ava.getScreenName(), ava);
92 String userId2 = "acc-role-user2";
93 String coAccId = createAccount(userId2, "acc-role-test@cspace.org");
94 AccountValue avc = new AccountValue();
95 avc.setScreenName(userId2);
96 avc.setUserId(userId2);
97 avc.setAccountId(coAccId);
98 accValues.put(avc.getScreenName(), avc);
100 String ri = "acc-role-user3";
101 String iAccId = createAccount(ri, "acc-role-test@cspace.org");
102 AccountValue avi = new AccountValue();
103 avi.setScreenName(ri);
105 avi.setAccountId(iAccId);
106 accValues.put(avi.getScreenName(), avi);
108 String rn1 = "ROLE_CO1";
109 String r1RoleId = createRole(rn1);
110 RoleValue rv1 = new RoleValue();
111 rv1.setRoleId(r1RoleId);
112 rv1.setRoleName(rn1);
113 roleValues.put(rv1.getRoleName(), rv1);
115 String rn2 = "ROLE_CO2";
116 String r2RoleId = createRole(rn2);
117 RoleValue rv2 = new RoleValue();
118 rv2.setRoleId(r2RoleId);
119 rv2.setRoleName(rn2);
120 roleValues.put(rv2.getRoleName(), rv2);
124 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
127 protected CollectionSpaceClient getClientInstance() {
128 return new AccountRoleClient();
132 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
135 protected AbstractCommonList getAbstractCommonList(
136 ClientResponse<AbstractCommonList> response) {
137 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
138 throw new UnsupportedOperationException();
141 @Test(dataProvider = "testName")
143 public void readPaginatedList(String testName) throws Exception {
144 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
147 // ---------------------------------------------------------------
148 // CRUD tests : CREATE tests
149 // ---------------------------------------------------------------
152 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
153 public void create(String testName) throws Exception {
155 // Perform setup, such as initializing the type of service request
156 // (e.g. CREATE, DELETE), its valid and expected status codes, and
157 // its associated HTTP method name (e.g. POST, DELETE).
158 setupCreate(testName);
160 // Submit the request to the service and store the response.
161 AccountValue pv = accValues.get("acc-role-user1");
162 AccountRole accRole = createAccountRoleInstance(pv,
163 roleValues.values(), true, true);
164 AccountRoleClient client = new AccountRoleClient();
165 ClientResponse<Response> res = client.create(pv.getAccountId(), accRole);
166 int statusCode = res.getStatus();
168 if (logger.isDebugEnabled()) {
169 logger.debug(testName + ": status = " + statusCode);
171 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
172 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
173 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
175 // Store the ID returned from this create operation
176 // for additional tests below.
177 //this is is not important in case of this relationship
178 knownResourceId = extractId(res);
179 if (logger.isDebugEnabled()) {
180 logger.debug(testName + ": knownResourceId=" + knownResourceId);
182 res.releaseConnection();
185 //to not cause uniqueness violation for accRole, createList is removed
186 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
187 dependsOnMethods = {"create"})
189 public void createList(String testName) throws Exception {
190 //FIXME: Should this test really be empty? If so, please comment accordingly.
194 // Placeholders until the three tests below can be uncommented.
195 // See Issue CSPACE-401.
197 public void createWithEmptyEntityBody(String testName) throws Exception {
198 //FIXME: Should this test really be empty? If so, please comment accordingly.
202 public void createWithMalformedXml(String testName) throws Exception {
203 //FIXME: Should this test really be empty? If so, please comment accordingly.
207 public void createWithWrongXmlSchema(String testName) throws Exception {
208 //FIXME: Should this test really be empty? If so, please comment accordingly.
211 // ---------------------------------------------------------------
212 // CRUD tests : READ tests
213 // ---------------------------------------------------------------
216 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
217 dependsOnMethods = {"create"})
218 public void read(String testName) throws Exception {
223 // Submit the request to the service and store the response.
224 AccountRoleClient client = new AccountRoleClient();
225 ClientResponse<AccountRole> res = client.read(
226 accValues.get("acc-role-user1").getAccountId(), "123");
227 int statusCode = res.getStatus();
229 // Check the status code of the response: does it match
230 // the expected response(s)?
231 if (logger.isDebugEnabled()) {
232 logger.debug(testName + ": status = " + statusCode);
234 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
235 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
236 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
238 AccountRole output = (AccountRole) res.getEntity();
239 Assert.assertNotNull(output);
240 res.releaseConnection();
245 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
246 public void readNonExistent(String testName) throws Exception {
249 setupReadNonExistent(testName);
251 // Submit the request to the service and store the response.
252 AccountRoleClient client = new AccountRoleClient();
253 ClientResponse<AccountRole> res = client.read(this.NON_EXISTENT_ID, "123");
254 int statusCode = res.getStatus();
256 // Check the status code of the response: does it match
257 // the expected response(s)?
258 if (logger.isDebugEnabled()) {
259 logger.debug(testName + ": status = " + statusCode);
261 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
262 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
263 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
264 res.releaseConnection();
267 // ---------------------------------------------------------------
268 // CRUD tests : READ_LIST tests
269 // ---------------------------------------------------------------
272 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
273 dependsOnMethods = {"createList", "read"})
274 public void readList(String testName) throws Exception {
275 //FIXME: Should this test really be empty? If so, please comment accordingly.
280 // ---------------------------------------------------------------
281 // CRUD tests : UPDATE tests
282 // ---------------------------------------------------------------
285 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
286 dependsOnMethods = {"read", "readList", "readNonExistent"})
287 public void update(String testName) throws Exception {
288 //FIXME: Should this test really be empty? If so, please comment accordingly.
292 // Placeholders until the three tests below can be uncommented.
293 // See Issue CSPACE-401.
295 public void updateWithEmptyEntityBody(String testName) throws Exception {
296 //FIXME: Should this test really be empty? If so, please comment accordingly.
300 public void updateWithMalformedXml(String testName) throws Exception {
301 //FIXME: Should this test really be empty? If so, please comment accordingly.
305 public void updateWithWrongXmlSchema(String testName) throws Exception {
306 //FIXME: Should this test really be empty? If so, please comment accordingly.
310 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
311 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
312 public void updateNonExistent(String testName) throws Exception {
313 //FIXME: Should this test really be empty? If so, please comment accordingly.
316 // ---------------------------------------------------------------
317 // CRUD tests : DELETE tests
318 // ---------------------------------------------------------------
321 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
322 dependsOnMethods = {"read"})
323 public void delete(String testName) throws Exception {
326 setupDelete(testName);
328 // Submit the request to the service and store the response.
329 AccountRoleClient client = new AccountRoleClient();
330 ClientResponse<Response> res = client.delete(
331 accValues.get("acc-role-user1").getAccountId(), "123");
332 int statusCode = res.getStatus();
334 // Check the status code of the response: does it match
335 // the expected response(s)?
336 if (logger.isDebugEnabled()) {
337 logger.debug(testName + ": status = " + statusCode);
339 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
340 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
341 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
342 res.releaseConnection();
347 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
350 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
351 public void deleteNonExistent(String testName) throws Exception {
352 //ignoring this test as the service side returns 200 now even if it does
353 //not find a record in the db
356 // ---------------------------------------------------------------
357 // Utility tests : tests of code used in tests above
358 // ---------------------------------------------------------------
360 * Tests the code for manually submitting data that is used by several
361 * of the methods above.
363 @Test(dependsOnMethods = {"create"})
364 public void testSubmitRequest() throws Exception {
366 // Expected status code: 200 OK
367 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
369 // Submit the request to the service and store the response.
370 String method = ServiceRequestType.READ.httpMethodName();
371 String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
372 int statusCode = submitRequest(method, url);
374 // Check the status code of the response: does it match
375 // the expected response(s)?
376 if (logger.isDebugEnabled()) {
377 logger.debug("testSubmitRequest: url=" + url
378 + " status=" + statusCode);
380 Assert.assertEquals(statusCode, EXPECTED_STATUS);
384 // ---------------------------------------------------------------
385 // Utility methods used by tests above
386 // ---------------------------------------------------------------
388 * Creates the account role instance.
392 * @param usePermId the use perm id
393 * @param useRoleId the use role id
394 * @return the account role
396 static public AccountRole createAccountRoleInstance(AccountValue pv,
397 Collection<RoleValue> rvs,
401 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
402 pv, rvs, usePermId, useRoleId);
404 if (logger.isDebugEnabled()) {
405 logger.debug("to be created, accRole common");
406 logger.debug(objectAsXmlString(accRole, AccountRole.class));
411 @AfterClass(alwaysRun = true)
412 public void cleanUp() {
413 setupDelete("delete");
414 String noTest = System.getProperty("noTestCleanup");
415 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
416 if (logger.isDebugEnabled()) {
417 logger.debug("Skipping Cleanup phase ...");
421 if (logger.isDebugEnabled()) {
422 logger.debug("Cleaning up temporary resources created for testing ...");
424 AccountRoleClient client = new AccountRoleClient();
425 for (String resourceId : allResourceIdsCreated) {
427 ClientResponse<Response> res = client.delete(resourceId, "123");
428 int statusCode = res.getStatus();
429 if (logger.isDebugEnabled()) {
430 logger.debug("clenaup: delete relationships for accission id="
431 + resourceId + " status=" + statusCode);
433 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
434 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
435 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
436 res.releaseConnection();
439 for (AccountValue pv : accValues.values()) {
440 deleteAccount(pv.getAccountId());
443 for (RoleValue rv : roleValues.values()) {
444 deleteRole(rv.getRoleId());
448 private String createAccount(String userName, String email) {
450 AccountClient accClient = new AccountClient();
451 AccountsCommon account = AccountFactory.createAccountInstance(
452 userName, userName, userName, email,
453 true, true, false, true, true);
454 ClientResponse<Response> res = accClient.create(account);
455 int statusCode = res.getStatus();
456 if (logger.isDebugEnabled()) {
457 logger.debug("createAccount: userName=" + userName
458 + " status = " + statusCode);
460 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
461 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
462 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
463 res.releaseConnection();
464 return extractId(res);
467 private void deleteAccount(String accId) {
469 AccountClient accClient = new AccountClient();
470 ClientResponse<Response> res = accClient.delete(accId);
471 int statusCode = res.getStatus();
472 if (logger.isDebugEnabled()) {
473 logger.debug("deleteAccount: delete account id="
474 + accId + " status=" + statusCode);
476 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
477 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
478 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
479 res.releaseConnection();
482 private String createRole(String roleName) {
484 RoleClient roleClient = new RoleClient();
486 Role role = RoleFactory.createRoleInstance(roleName,
487 "role for " + roleName, true);
488 ClientResponse<Response> res = roleClient.create(role);
489 int statusCode = res.getStatus();
490 if (logger.isDebugEnabled()) {
491 logger.debug("createRole: name=" + roleName
492 + " status = " + statusCode);
494 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
495 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
496 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
497 res.releaseConnection();
498 return extractId(res);
501 private void deleteRole(String roleId) {
503 RoleClient roleClient = new RoleClient();
504 ClientResponse<Response> res = roleClient.delete(roleId);
505 int statusCode = res.getStatus();
506 if (logger.isDebugEnabled()) {
507 logger.debug("deleteRole: delete role id=" + roleId
508 + " status=" + statusCode);
510 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
511 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
512 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
513 res.releaseConnection();