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.jboss.resteasy.client.ClientResponse;
34 import org.testng.Assert;
35 import org.testng.annotations.Test;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * AccountServiceTest, carries out tests against a
42 * deployed and running Account Service.
44 * $LastChangedRevision: 917 $
45 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
47 public class AccountServiceTest extends AbstractServiceTest {
49 private final Logger logger =
50 LoggerFactory.getLogger(AccountServiceTest.class);
51 // Instance variables specific to this test.
52 private AccountClient client = new AccountClient();
53 private String knownResourceId = null;
56 * This method is called only by the parent class, AbstractServiceTest
59 protected String getServicePathComponent() {
60 return client.getServicePathComponent();
63 // ---------------------------------------------------------------
64 // CRUD tests : CREATE tests
65 // ---------------------------------------------------------------
68 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class)
69 public void create(String testName) throws Exception {
71 // Perform setup, such as initializing the type of service request
72 // (e.g. CREATE, DELETE), its valid and expected status codes, and
73 // its associated HTTP method name (e.g. POST, DELETE).
74 setupCreate(testName);
76 // Submit the request to the service and store the response.
77 AccountsCommon account =
78 createAccountInstance("barney", "dino", "barney", "hello", "barney@dinoland.com");
79 ClientResponse<Response> res = client.create(account);
80 int statusCode = res.getStatus();
82 // Check the status code of the response: does it match
83 // the expected response(s)?
86 // Does it fall within the set of valid status codes?
87 // Does it exactly match the expected status code?
88 if (logger.isDebugEnabled()) {
89 logger.debug(testName + ": status = " + statusCode);
91 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
92 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
93 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
95 // Store the ID returned from this create operation
96 // for additional tests below.
97 knownResourceId = extractId(res);
98 if (logger.isDebugEnabled()) {
99 logger.debug(testName + ": knownResourceId=" + knownResourceId);
104 * @see org.collectionspace.services.client.test.ServiceTest#createList()
107 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
108 dependsOnMethods = {"create"})
109 public void createList(String testName) throws Exception {
110 for (int i = 0; i < 3; i++) {
116 // Placeholders until the three tests below can be uncommented.
117 // See Issue CSPACE-401.
119 public void createWithEmptyEntityBody(String testName) throws Exception {
123 public void createWithMalformedXml(String testName) throws Exception {
127 public void createWithWrongXmlSchema(String testName) throws Exception {
130 // ---------------------------------------------------------------
131 // CRUD tests : READ tests
132 // ---------------------------------------------------------------
135 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
136 dependsOnMethods = {"create"})
137 public void read(String testName) throws Exception {
142 // Submit the request to the service and store the response.
143 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
144 int statusCode = res.getStatus();
146 // Check the status code of the response: does it match
147 // the expected response(s)?
148 if (logger.isDebugEnabled()) {
149 logger.debug(testName + ": status = " + statusCode);
151 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
152 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
153 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
155 AccountsCommon output = (AccountsCommon) res.getEntity();
156 Assert.assertNotNull(output);
161 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
162 dependsOnMethods = {"read"})
163 public void readNonExistent(String testName) throws Exception {
166 setupReadNonExistent(testName);
168 // Submit the request to the service and store the response.
169 ClientResponse<AccountsCommon> res = client.read(NON_EXISTENT_ID);
170 int statusCode = res.getStatus();
172 // Check the status code of the response: does it match
173 // the expected response(s)?
174 if (logger.isDebugEnabled()) {
175 logger.debug(testName + ": status = " + statusCode);
177 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
178 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
179 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
182 // ---------------------------------------------------------------
183 // CRUD tests : READ_LIST tests
184 // ---------------------------------------------------------------
187 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
188 dependsOnMethods = {"createList", "read"})
189 public void readList(String testName) throws Exception {
192 setupReadList(testName);
194 // Submit the request to the service and store the response.
195 ClientResponse<AccountsCommonList> res = client.readList();
196 AccountsCommonList list = res.getEntity();
197 int statusCode = res.getStatus();
199 // Check the status code of the response: does it match
200 // the expected response(s)?
201 if (logger.isDebugEnabled()) {
202 logger.debug(testName + ": status = " + statusCode);
204 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
205 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
206 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
208 // Optionally output additional data about list members for debugging.
209 boolean iterateThroughList = true;
210 if (iterateThroughList && logger.isDebugEnabled()) {
211 List<AccountsCommonList.AccountListItem> items =
212 list.getAccountListItem();
215 for (AccountsCommonList.AccountListItem item : items) {
216 logger.debug(testName + ": list-item[" + i + "] csid=" +
218 logger.debug(testName + ": list-item[" + i + "] anchorName=" +
219 item.getAnchorName());
220 logger.debug(testName + ": list-item[" + i + "] URI=" +
230 // ---------------------------------------------------------------
231 // CRUD tests : UPDATE tests
232 // ---------------------------------------------------------------
235 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
236 dependsOnMethods = {"read", "readNonExistent"})
237 public void update(String testName) throws Exception {
240 setupUpdate(testName);
242 ClientResponse<AccountsCommon> res =
243 client.read(knownResourceId);
244 if (logger.isDebugEnabled()) {
245 logger.debug(testName + ": read status = " + res.getStatus());
247 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
249 if (logger.isDebugEnabled()) {
250 logger.debug("got object to update with ID: " + knownResourceId);
252 AccountsCommon toUpdateAccount =
253 (AccountsCommon) res.getEntity();
254 Assert.assertNotNull(toUpdateAccount);
256 // Update the content of this resource.
257 toUpdateAccount.setEmail("updated-" + toUpdateAccount.getEmail());
258 toUpdateAccount.setPhone("updated-" + toUpdateAccount.getPhone());
259 if (logger.isDebugEnabled()) {
260 logger.debug("updated object");
261 logger.debug(objectAsXmlString(toUpdateAccount,
262 AccountsCommon.class));
265 // Submit the request to the service and store the response.
266 res = client.update(knownResourceId, toUpdateAccount);
267 int statusCode = res.getStatus();
268 // Check the status code of the response: does it match the expected response(s)?
269 if (logger.isDebugEnabled()) {
270 logger.debug(testName + ": status = " + statusCode);
272 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
273 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
274 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
277 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
278 Assert.assertNotNull(updatedAccount);
280 Assert.assertEquals(updatedAccount.getEmail(),
281 toUpdateAccount.getEmail(),
282 "Data in updated object did not match submitted data.");
287 // Placeholders until the three tests below can be uncommented.
288 // See Issue CSPACE-401.
290 public void updateWithEmptyEntityBody(String testName) throws Exception {
294 public void updateWithMalformedXml(String testName) throws Exception {
298 public void updateWithWrongXmlSchema(String testName) throws Exception {
302 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
303 dependsOnMethods = {"update", "readNonExistent", "testSubmitRequest"})
304 public void updateNonExistent(String testName) throws Exception {
307 setupUpdateNonExistent(testName);
309 // Submit the request to the service and store the response.
311 // Note: The ID used in this 'create' call may be arbitrary.
312 // The only relevant ID may be the one used in updateAccount(), below.
313 AccountsCommon account =
314 createAccountInstance("simba", "mufasa", "simba", "tiger", "simba@lionking.com");
315 ClientResponse<AccountsCommon> res =
316 client.update(NON_EXISTENT_ID, account);
317 int statusCode = res.getStatus();
319 // Check the status code of the response: does it match
320 // the expected response(s)?
321 if (logger.isDebugEnabled()) {
322 logger.debug(testName + ": status = " + statusCode);
324 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
325 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
326 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
329 // ---------------------------------------------------------------
330 // CRUD tests : DELETE tests
331 // ---------------------------------------------------------------
334 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
335 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update", "updateNonExistent"})
336 public void delete(String testName) throws Exception {
339 setupDelete(testName);
341 // Submit the request to the service and store the response.
342 ClientResponse<Response> res = client.delete(knownResourceId);
343 int statusCode = res.getStatus();
345 // Check the status code of the response: does it match
346 // the expected response(s)?
347 if (logger.isDebugEnabled()) {
348 logger.debug(testName + ": status = " + statusCode);
350 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
351 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
352 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
357 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
358 dependsOnMethods = {"delete"})
359 public void deleteNonExistent(String testName) throws Exception {
362 setupDeleteNonExistent(testName);
364 // Submit the request to the service and store the response.
365 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
366 int statusCode = res.getStatus();
368 // Check the status code of the response: does it match
369 // the expected response(s)?
370 if (logger.isDebugEnabled()) {
371 logger.debug(testName + ": status = " + statusCode);
373 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
374 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
375 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
378 // ---------------------------------------------------------------
379 // Utility tests : tests of code used in tests above
380 // ---------------------------------------------------------------
382 * Tests the code for manually submitting data that is used by several
383 * of the methods above.
385 @Test(dependsOnMethods = {"create", "read"})
386 public void testSubmitRequest() throws Exception {
388 // Expected status code: 200 OK
389 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
391 // Submit the request to the service and store the response.
392 String method = ServiceRequestType.READ.httpMethodName();
393 String url = getResourceURL(knownResourceId);
394 int statusCode = submitRequest(method, url);
396 // Check the status code of the response: does it match
397 // the expected response(s)?
398 if (logger.isDebugEnabled()) {
399 logger.debug("testSubmitRequest: url=" + url +
400 " status=" + statusCode);
402 Assert.assertEquals(statusCode, EXPECTED_STATUS);
406 // ---------------------------------------------------------------
407 // Utility methods used by tests above
408 // ---------------------------------------------------------------
409 private AccountsCommon createAccountInstance(String firstName, String lastName, String anchorName,
410 String passwd, String email) {
412 AccountsCommon account = new AccountsCommon();
413 account.setFirstName(firstName);
414 account.setLastName(lastName);
415 account.setAnchorName(anchorName);
416 account.setUserName(anchorName);
417 byte[] b64passwd = Base64.encodeBase64(passwd.getBytes());
418 account.setPassword(b64passwd);
419 account.setEmail(email);
420 if (logger.isDebugEnabled()) {
421 logger.debug("to be created, account common");
422 logger.debug(objectAsXmlString(account,
423 AccountsCommon.class));