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.RoleClient;
40 import org.collectionspace.services.client.RoleFactory;
41 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
42 import org.collectionspace.services.client.test.ServiceRequestType;
43 import org.jboss.resteasy.client.ClientResponse;
46 import org.testng.Assert;
47 import org.testng.annotations.Test;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.testng.annotations.AfterClass;
52 import org.testng.annotations.BeforeClass;
55 * AccountServiceTest, carries out tests against a
56 * deployed and running Account, Role and AccountRole Services.
58 * $LastChangedRevision: 917 $
59 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
61 public class AccountRoleServiceTest extends AbstractServiceTestImpl {
63 static private final Logger logger =
64 LoggerFactory.getLogger(AccountRoleServiceTest.class);
65 // Instance variables specific to this test.
66 private String knownResourceId = null;
67 private List<String> allResourceIdsCreated = new ArrayList();
68 private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
69 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
71 * This method is called only by the parent class, AbstractServiceTestImpl
75 protected String getServicePathComponent() {
76 return new AccountRoleClient().getServicePathComponent();
79 @BeforeClass(alwaysRun = true)
80 public void seedData() {
81 String ra = "acc-role-user1";
82 String accId = createAccount(ra, "acc-role-test@cspace.org");
83 AccountValue ava = new AccountValue();
84 ava.setScreenName(ra);
86 ava.setAccountId(accId);
87 accValues.put(ava.getScreenName(), ava);
89 String rc = "acc-role-user2";
90 String coAccId = createAccount(rc, "acc-role-test@cspace.org");
91 AccountValue avc = new AccountValue();
92 avc.setScreenName(rc);
94 avc.setAccountId(coAccId);
95 accValues.put(avc.getScreenName(), avc);
97 String ri = "acc-role-user3";
98 String iAccId = createAccount(ri, "acc-role-test@cspace.org");
99 AccountValue avi = new AccountValue();
100 avi.setScreenName(ri);
102 avi.setAccountId(iAccId);
103 accValues.put(avi.getScreenName(), avi);
105 String rn1 = "ROLE_CO1";
106 String r1RoleId = createRole(rn1);
107 RoleValue rv1 = new RoleValue();
108 rv1.setRoleId(r1RoleId);
109 rv1.setRoleName(rn1);
110 roleValues.put(rv1.getRoleName(), rv1);
112 String rn2 = "ROLE_CO2";
113 String r2RoleId = createRole(rn2);
114 RoleValue rv2 = new RoleValue();
115 rv2.setRoleId(r2RoleId);
116 rv2.setRoleName(rn2);
117 roleValues.put(rv2.getRoleName(), rv2);
120 // ---------------------------------------------------------------
121 // CRUD tests : CREATE tests
122 // ---------------------------------------------------------------
125 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
126 public void create(String testName) throws Exception {
128 // Perform setup, such as initializing the type of service request
129 // (e.g. CREATE, DELETE), its valid and expected status codes, and
130 // its associated HTTP method name (e.g. POST, DELETE).
131 setupCreate(testName);
133 // Submit the request to the service and store the response.
134 AccountValue pv = accValues.get("acc-role-user1");
135 AccountRole accRole = createAccountRoleInstance(pv,
136 roleValues.values(), true, true);
137 AccountRoleClient client = new AccountRoleClient();
138 ClientResponse<Response> res = client.create(pv.getAccountId(), accRole);
139 int statusCode = res.getStatus();
141 if (logger.isDebugEnabled()) {
142 logger.debug(testName + ": status = " + statusCode);
144 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
145 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
146 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
148 // Store the ID returned from this create operation
149 // for additional tests below.
150 //this is is not important in case of this relationship
151 knownResourceId = extractId(res);
152 if (logger.isDebugEnabled()) {
153 logger.debug(testName + ": knownResourceId=" + knownResourceId);
157 //to not cause uniqueness violation for accRole, createList is removed
159 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
160 dependsOnMethods = {"create"})
161 public void createList(String testName) throws Exception {
165 // Placeholders until the three tests below can be uncommented.
166 // See Issue CSPACE-401.
168 public void createWithEmptyEntityBody(String testName) throws Exception {
172 public void createWithMalformedXml(String testName) throws Exception {
176 public void createWithWrongXmlSchema(String testName) throws Exception {
179 // ---------------------------------------------------------------
180 // CRUD tests : READ tests
181 // ---------------------------------------------------------------
184 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
185 dependsOnMethods = {"create"})
186 public void read(String testName) throws Exception {
191 // Submit the request to the service and store the response.
192 AccountRoleClient client = new AccountRoleClient();
193 ClientResponse<AccountRole> res = client.read(
194 accValues.get("acc-role-user1").getAccountId(), "123");
195 int statusCode = res.getStatus();
197 // Check the status code of the response: does it match
198 // the expected response(s)?
199 if (logger.isDebugEnabled()) {
200 logger.debug(testName + ": status = " + statusCode);
202 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
203 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
204 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
206 AccountRole output = (AccountRole) res.getEntity();
207 Assert.assertNotNull(output);
212 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
213 public void readNonExistent(String testName) throws Exception {
216 setupReadNonExistent(testName);
218 // Submit the request to the service and store the response.
219 AccountRoleClient client = new AccountRoleClient();
220 ClientResponse<AccountRole> res = client.read(NON_EXISTENT_ID, "123");
221 int statusCode = res.getStatus();
223 // Check the status code of the response: does it match
224 // the expected response(s)?
225 if (logger.isDebugEnabled()) {
226 logger.debug(testName + ": status = " + statusCode);
228 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
229 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
230 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
233 // ---------------------------------------------------------------
234 // CRUD tests : READ_LIST tests
235 // ---------------------------------------------------------------
238 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
239 dependsOnMethods = {"createList", "read"})
240 public void readList(String testName) throws Exception {
245 // ---------------------------------------------------------------
246 // CRUD tests : UPDATE tests
247 // ---------------------------------------------------------------
250 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
251 dependsOnMethods = {"read", "readList", "readNonExistent"})
252 public void update(String testName) throws Exception {
256 // Placeholders until the three tests below can be uncommented.
257 // See Issue CSPACE-401.
259 public void updateWithEmptyEntityBody(String testName) throws Exception {
263 public void updateWithMalformedXml(String testName) throws Exception {
267 public void updateWithWrongXmlSchema(String testName) throws Exception {
271 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
272 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
273 public void updateNonExistent(String testName) throws Exception {
276 // ---------------------------------------------------------------
277 // CRUD tests : DELETE tests
278 // ---------------------------------------------------------------
281 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
282 dependsOnMethods = {"read"})
283 public void delete(String testName) throws Exception {
286 setupDelete(testName);
288 // Submit the request to the service and store the response.
289 AccountRoleClient client = new AccountRoleClient();
290 ClientResponse<Response> res = client.delete(
291 accValues.get("acc-role-user1").getAccountId(), "123");
292 int statusCode = res.getStatus();
294 // Check the status code of the response: does it match
295 // the expected response(s)?
296 if (logger.isDebugEnabled()) {
297 logger.debug(testName + ": status = " + statusCode);
299 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
300 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
301 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
306 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
307 public void deleteNonExistent(String testName) throws Exception {
310 setupDeleteNonExistent(testName);
312 // Submit the request to the service and store the response.
313 AccountRoleClient client = new AccountRoleClient();
314 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID, "123");
315 int statusCode = res.getStatus();
317 // Check the status code of the response: does it match
318 // the expected response(s)?
319 if (logger.isDebugEnabled()) {
320 logger.debug(testName + ": status = " + statusCode);
322 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
323 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
324 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
327 // ---------------------------------------------------------------
328 // Utility tests : tests of code used in tests above
329 // ---------------------------------------------------------------
331 * Tests the code for manually submitting data that is used by several
332 * of the methods above.
334 @Test(dependsOnMethods = {"create"})
335 public void testSubmitRequest() throws Exception {
337 // Expected status code: 200 OK
338 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
340 // Submit the request to the service and store the response.
341 String method = ServiceRequestType.READ.httpMethodName();
342 String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
343 int statusCode = submitRequest(method, url);
345 // Check the status code of the response: does it match
346 // the expected response(s)?
347 if (logger.isDebugEnabled()) {
348 logger.debug("testSubmitRequest: url=" + url
349 + " status=" + statusCode);
351 Assert.assertEquals(statusCode, EXPECTED_STATUS);
355 // ---------------------------------------------------------------
356 // Utility methods used by tests above
357 // ---------------------------------------------------------------
359 * create accRolerole instance
361 * @param roleValues array of role ids
366 static public AccountRole createAccountRoleInstance(AccountValue pv,
367 Collection<RoleValue> rvs,
371 AccountRole accRole = new AccountRole();
372 //service consume is not required to provide subject as it is determined
374 // accRole.setSubject(SubjectType.ROLE);
376 ArrayList<AccountValue> pvs = new ArrayList<AccountValue>();
378 accRole.setAccounts(pvs);
381 //FIXME is there a better way?
382 ArrayList<RoleValue> rvas = new ArrayList<RoleValue>();
383 for (RoleValue rv : rvs) {
386 accRole.setRoles(rvas);
389 if (logger.isDebugEnabled()) {
390 logger.debug("to be created, accRole common");
391 logger.debug(objectAsXmlString(accRole, AccountRole.class));
396 @AfterClass(alwaysRun = true)
397 public void cleanUp() {
398 setupDelete("delete");
399 if (logger.isDebugEnabled()) {
400 logger.debug("clenaup: Cleaning up temporary resources created for testing ...");
402 AccountRoleClient client = new AccountRoleClient();
403 for (String resourceId : allResourceIdsCreated) {
405 // Note: Any non-success responses are ignored and not reported.
406 ClientResponse<Response> res = client.delete(resourceId, "123");
407 int statusCode = res.getStatus();
408 if (logger.isDebugEnabled()) {
409 logger.debug("clenaup: delete relationships for accission id="
410 + resourceId + " status=" + statusCode);
412 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
413 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
414 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
417 for (AccountValue pv : accValues.values()) {
418 deleteAccount(pv.getAccountId());
421 for (RoleValue rv : roleValues.values()) {
422 deleteRole(rv.getRoleId());
426 private String createAccount(String userName, String email) {
428 AccountClient accClient = new AccountClient();
429 AccountsCommon account = AccountFactory.createAccountInstance(
430 userName, userName, userName, email,
431 true, true, false, true, true);
432 ClientResponse<Response> res = accClient.create(account);
433 int statusCode = res.getStatus();
434 if (logger.isDebugEnabled()) {
435 logger.debug("createAccount: userName=" + userName
436 + " status = " + statusCode);
438 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
439 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
440 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
441 return extractId(res);
444 private void deleteAccount(String accId) {
446 AccountClient accClient = new AccountClient();
447 ClientResponse<Response> res = accClient.delete(accId);
448 int statusCode = res.getStatus();
449 if (logger.isDebugEnabled()) {
450 logger.debug("deleteAccount: delete account id="
451 + accId + " status=" + statusCode);
453 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
454 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
455 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
458 private String createRole(String roleName) {
460 RoleClient roleClient = new RoleClient();
462 Role role = RoleFactory.createRoleInstance(roleName,
463 "role for " + roleName, true);
464 ClientResponse<Response> res = roleClient.create(role);
465 int statusCode = res.getStatus();
466 if (logger.isDebugEnabled()) {
467 logger.debug("createRole: name=" + roleName
468 + " status = " + statusCode);
470 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
471 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
472 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
473 return extractId(res);
476 private void deleteRole(String roleId) {
478 RoleClient roleClient = new RoleClient();
479 ClientResponse<Response> res = roleClient.delete(roleId);
480 int statusCode = res.getStatus();
481 if (logger.isDebugEnabled()) {
482 logger.debug("deleteRole: delete role id=" + roleId
483 + " status=" + statusCode);
485 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
486 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
487 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);