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.client.test;
25 import java.util.List;
26 import javax.ws.rs.core.Response;
28 import org.apache.commons.codec.binary.Base64;
29 import org.collectionspace.services.client.AccountClient;
30 import org.collectionspace.services.account.AccountsCommon;
31 import org.collectionspace.services.account.AccountsCommonList;
32 import org.collectionspace.services.account.Status;
33 import org.jboss.resteasy.client.ClientResponse;
35 import org.testng.Assert;
36 import org.testng.annotations.Test;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * AccountServiceTest, carries out tests against a
43 * deployed and running Account Service.
45 * $LastChangedRevision: 917 $
46 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
48 public class AccountServiceTest extends AbstractServiceTest {
50 private final Logger logger =
51 LoggerFactory.getLogger(AccountServiceTest.class);
52 // Instance variables specific to this test.
53 private AccountClient client = new AccountClient();
54 private String knownResourceId = null;
57 * This method is called only by the parent class, AbstractServiceTest
60 protected String getServicePathComponent() {
61 return client.getServicePathComponent();
64 // ---------------------------------------------------------------
65 // CRUD tests : CREATE tests
66 // ---------------------------------------------------------------
69 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class)
70 public void create(String testName) throws Exception {
72 // Perform setup, such as initializing the type of service request
73 // (e.g. CREATE, DELETE), its valid and expected status codes, and
74 // its associated HTTP method name (e.g. POST, DELETE).
75 setupCreate(testName);
77 // Submit the request to the service and store the response.
78 AccountsCommon account =
79 createAccountInstance("barney", "dino", "barney", "hithere08", "barney@dinoland.com");
80 ClientResponse<Response> res = client.create(account);
81 int statusCode = res.getStatus();
83 // Check the status code of the response: does it match
84 // the expected response(s)?
87 // Does it fall within the set of valid status codes?
88 // Does it exactly match the expected status code?
89 if (logger.isDebugEnabled()) {
90 logger.debug(testName + ": status = " + statusCode);
92 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
93 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
94 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
96 // Store the ID returned from this create operation
97 // for additional tests below.
98 knownResourceId = extractId(res);
99 if (logger.isDebugEnabled()) {
100 logger.debug(testName + ": knownResourceId=" + knownResourceId);
104 //to not cause uniqueness violation for account, createList is removed
106 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
107 dependsOnMethods = {"create"})
108 public void createList(String testName) throws Exception {
112 // Placeholders until the three tests below can be uncommented.
113 // See Issue CSPACE-401.
115 public void createWithEmptyEntityBody(String testName) throws Exception {
119 public void createWithMalformedXml(String testName) throws Exception {
123 public void createWithWrongXmlSchema(String testName) throws Exception {
126 // ---------------------------------------------------------------
127 // CRUD tests : READ tests
128 // ---------------------------------------------------------------
131 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
132 dependsOnMethods = {"create"})
133 public void read(String testName) throws Exception {
138 // Submit the request to the service and store the response.
139 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
140 int statusCode = res.getStatus();
142 // Check the status code of the response: does it match
143 // the expected response(s)?
144 if (logger.isDebugEnabled()) {
145 logger.debug(testName + ": status = " + statusCode);
147 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
148 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
149 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
151 AccountsCommon output = (AccountsCommon) res.getEntity();
152 Assert.assertNotNull(output);
157 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
158 dependsOnMethods = {"read"})
159 public void readNonExistent(String testName) throws Exception {
162 setupReadNonExistent(testName);
164 // Submit the request to the service and store the response.
165 ClientResponse<AccountsCommon> res = client.read(NON_EXISTENT_ID);
166 int statusCode = res.getStatus();
168 // Check the status code of the response: does it match
169 // the expected response(s)?
170 if (logger.isDebugEnabled()) {
171 logger.debug(testName + ": status = " + statusCode);
173 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
174 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
175 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
178 // ---------------------------------------------------------------
179 // CRUD tests : READ_LIST tests
180 // ---------------------------------------------------------------
183 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
184 dependsOnMethods = {"createList", "read"})
185 public void readList(String testName) throws Exception {
188 setupReadList(testName);
190 // Submit the request to the service and store the response.
191 ClientResponse<AccountsCommonList> res = client.readList();
192 AccountsCommonList list = res.getEntity();
193 int statusCode = res.getStatus();
195 // Check the status code of the response: does it match
196 // the expected response(s)?
197 if (logger.isDebugEnabled()) {
198 logger.debug(testName + ": status = " + statusCode);
200 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
201 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
202 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
204 // Optionally output additional data about list members for debugging.
205 boolean iterateThroughList = true;
206 if (iterateThroughList && logger.isDebugEnabled()) {
207 List<AccountsCommonList.AccountListItem> items =
208 list.getAccountListItem();
211 for (AccountsCommonList.AccountListItem item : items) {
212 logger.debug(testName + ": list-item[" + i + "] csid=" +
214 logger.debug(testName + ": list-item[" + i + "] screenName=" +
215 item.getScreenName());
216 logger.debug(testName + ": list-item[" + i + "] URI=" +
226 // ---------------------------------------------------------------
227 // CRUD tests : UPDATE tests
228 // ---------------------------------------------------------------
231 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
232 dependsOnMethods = {"read", "readList", "readNonExistent"})
233 public void update(String testName) throws Exception {
236 setupUpdate(testName);
239 ClientResponse<AccountsCommon> res =
240 client.read(knownResourceId);
241 if (logger.isDebugEnabled()) {
242 logger.debug(testName + ": read status = " + res.getStatus());
244 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
246 if (logger.isDebugEnabled()) {
247 logger.debug("got object to update with ID: " + knownResourceId);
249 AccountsCommon toUpdateAccount =
250 (AccountsCommon) res.getEntity();
251 Assert.assertNotNull(toUpdateAccount);
253 // Update the content of this resource.
254 toUpdateAccount.setEmail("updated-" + toUpdateAccount.getEmail());
255 if (logger.isDebugEnabled()) {
256 logger.debug("updated object");
257 logger.debug(objectAsXmlString(toUpdateAccount,
258 AccountsCommon.class));
261 // Submit the request to the service and store the response.
262 res = client.update(knownResourceId, toUpdateAccount);
263 int statusCode = res.getStatus();
264 // Check the status code of the response: does it match 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);
273 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
274 Assert.assertNotNull(updatedAccount);
276 Assert.assertEquals(updatedAccount.getEmail(),
277 toUpdateAccount.getEmail(),
278 "Data in updated object did not match submitted data.");
281 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
282 dependsOnMethods = {"update"})
283 public void updatePassword(String testName) throws Exception {
286 setupUpdate(testName);
288 ClientResponse<AccountsCommon> res =
289 client.read(knownResourceId);
290 if (logger.isDebugEnabled()) {
291 logger.debug(testName + ": read status = " + res.getStatus());
293 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
295 if (logger.isDebugEnabled()) {
296 logger.debug("got object to update with ID: " + knownResourceId);
298 AccountsCommon toUpdateAccount =
299 (AccountsCommon) res.getEntity();
300 Assert.assertNotNull(toUpdateAccount);
303 toUpdateAccount.setPassword(Base64.encodeBase64("imagination".getBytes()));
304 if (logger.isDebugEnabled()) {
305 logger.debug("updated object");
306 logger.debug(objectAsXmlString(toUpdateAccount,
307 AccountsCommon.class));
310 // Submit the request to the service and store the response.
311 res = client.update(knownResourceId, toUpdateAccount);
312 int statusCode = res.getStatus();
313 // Check the status code of the response: does it match the expected response(s)?
314 if (logger.isDebugEnabled()) {
315 logger.debug(testName + ": status = " + statusCode);
317 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
318 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
319 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
322 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
323 Assert.assertNotNull(updatedAccount);
325 // Assert.assertEquals(updatedAccount.getPassword(),
326 // toUpdateAccount.getPassword(),
327 // "Data in updated object did not match submitted data.");
330 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
331 dependsOnMethods = {"updatePassword"})
332 public void deactivate(String testName) throws Exception {
335 setupUpdate(testName);
337 ClientResponse<AccountsCommon> res =
338 client.read(knownResourceId);
339 if (logger.isDebugEnabled()) {
340 logger.debug(testName + ": read status = " + res.getStatus());
342 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
344 if (logger.isDebugEnabled()) {
345 logger.debug("got object to update with ID: " + knownResourceId);
347 AccountsCommon toUpdateAccount =
348 (AccountsCommon) res.getEntity();
349 Assert.assertNotNull(toUpdateAccount);
351 // Update the content of this resource.
352 toUpdateAccount.setStatus(Status.INACTIVE);
353 if (logger.isDebugEnabled()) {
354 logger.debug("updated object");
355 logger.debug(objectAsXmlString(toUpdateAccount,
356 AccountsCommon.class));
359 // Submit the request to the service and store the response.
360 res = client.update(knownResourceId, toUpdateAccount);
361 int statusCode = res.getStatus();
362 // Check the status code of the response: does it match the expected response(s)?
363 if (logger.isDebugEnabled()) {
364 logger.debug(testName + ": status = " + statusCode);
366 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
367 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
368 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
371 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
372 Assert.assertNotNull(updatedAccount);
374 Assert.assertEquals(updatedAccount.getStatus(),
375 toUpdateAccount.getStatus(),
376 "Data in updated object did not match submitted data.");
381 // Placeholders until the three tests below can be uncommented.
382 // See Issue CSPACE-401.
384 public void updateWithEmptyEntityBody(String testName) throws Exception {
388 public void updateWithMalformedXml(String testName) throws Exception {
392 public void updateWithWrongXmlSchema(String testName) throws Exception {
396 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
397 dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
398 public void updateNonExistent(String testName) throws Exception {
401 setupUpdateNonExistent(testName);
403 // Submit the request to the service and store the response.
405 // Note: The ID used in this 'create' call may be arbitrary.
406 // The only relevant ID may be the one used in updateAccount(), below.
407 AccountsCommon account =
408 createAccountInstance("simba", "mufasa", "simba", "tiger", "simba@lionking.com");
409 ClientResponse<AccountsCommon> res =
410 client.update(NON_EXISTENT_ID, account);
411 int statusCode = res.getStatus();
413 // Check the status code of the response: does it match
414 // the expected response(s)?
415 if (logger.isDebugEnabled()) {
416 logger.debug(testName + ": status = " + statusCode);
418 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
419 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
420 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
423 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
424 dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
425 public void updateWrongUser(String testName) throws Exception {
428 // Submit the request to the service and store the response.
430 // Note: The ID used in this 'create' call may be arbitrary.
431 // The only relevant ID may be the one used in updateAccount(), below.
432 ClientResponse<AccountsCommon> res =
433 client.read(knownResourceId);
434 if (logger.isDebugEnabled()) {
435 logger.debug(testName + ": read status = " + res.getStatus());
437 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
439 if (logger.isDebugEnabled()) {
440 logger.debug("got object to update with ID: " + knownResourceId);
442 AccountsCommon toUpdateAccount =
443 (AccountsCommon) res.getEntity();
444 Assert.assertNotNull(toUpdateAccount);
446 toUpdateAccount.setUserId("barneyFake");
447 if (logger.isDebugEnabled()) {
448 logger.debug("updated object with wrongUser");
449 logger.debug(objectAsXmlString(toUpdateAccount,
450 AccountsCommon.class));
452 EXPECTED_STATUS_CODE = Response.Status.BAD_REQUEST.getStatusCode();
453 res = client.update(knownResourceId, toUpdateAccount);
454 int statusCode = res.getStatus();
456 // Check the status code of the response: does it match
457 // the expected response(s)?
458 if (logger.isDebugEnabled()) {
459 logger.debug(testName + ": status = " + statusCode);
461 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
462 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
463 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
466 // ---------------------------------------------------------------
467 // CRUD tests : DELETE tests
468 // ---------------------------------------------------------------
471 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
472 dependsOnMethods = {"testSubmitRequest", "updateNonExistent"})
473 public void delete(String testName) throws Exception {
476 setupDelete(testName);
478 // Submit the request to the service and store the response.
479 ClientResponse<Response> res = client.delete(knownResourceId);
480 int statusCode = res.getStatus();
482 // Check the status code of the response: does it match
483 // the expected response(s)?
484 if (logger.isDebugEnabled()) {
485 logger.debug(testName + ": status = " + statusCode);
487 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
488 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
489 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
494 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
495 dependsOnMethods = {"delete"})
496 public void deleteNonExistent(String testName) throws Exception {
499 setupDeleteNonExistent(testName);
501 // Submit the request to the service and store the response.
502 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
503 int statusCode = res.getStatus();
505 // Check the status code of the response: does it match
506 // the expected response(s)?
507 if (logger.isDebugEnabled()) {
508 logger.debug(testName + ": status = " + statusCode);
510 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
511 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
512 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
515 // ---------------------------------------------------------------
516 // Utility tests : tests of code used in tests above
517 // ---------------------------------------------------------------
519 * Tests the code for manually submitting data that is used by several
520 * of the methods above.
522 @Test(dependsOnMethods = {"create", "read"})
523 public void testSubmitRequest() throws Exception {
525 // Expected status code: 200 OK
526 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
528 // Submit the request to the service and store the response.
529 String method = ServiceRequestType.READ.httpMethodName();
530 String url = getResourceURL(knownResourceId);
531 int statusCode = submitRequest(method, url);
533 // Check the status code of the response: does it match
534 // the expected response(s)?
535 if (logger.isDebugEnabled()) {
536 logger.debug("testSubmitRequest: url=" + url +
537 " status=" + statusCode);
539 Assert.assertEquals(statusCode, EXPECTED_STATUS);
543 // ---------------------------------------------------------------
544 // Utility methods used by tests above
545 // ---------------------------------------------------------------
546 private AccountsCommon createAccountInstance(String firstName, String lastName, String screenName,
547 String passwd, String email) {
549 AccountsCommon account = new AccountsCommon();
550 account.setFirstName(firstName);
551 account.setLastName(lastName);
552 account.setScreenName(screenName);
553 account.setUserId(screenName);
554 account.setPassword(Base64.encodeBase64(passwd.getBytes()));
555 account.setEmail(email);
556 account.setPhone("1234567890");
557 if (logger.isDebugEnabled()) {
558 logger.debug("to be created, account common");
559 logger.debug(objectAsXmlString(account,
560 AccountsCommon.class));