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.jboss.resteasy.plugins.providers.multipart.MultipartInput;
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("sanjay", "hello");
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);
105 * @see org.collectionspace.services.client.test.ServiceTest#createList()
108 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
109 dependsOnMethods = {"create"})
110 public void createList(String testName) throws Exception {
111 for (int i = 0; i < 3; i++) {
117 // Placeholders until the three tests below can be uncommented.
118 // See Issue CSPACE-401.
120 public void createWithEmptyEntityBody(String testName) throws Exception {
124 public void createWithMalformedXml(String testName) throws Exception {
128 public void createWithWrongXmlSchema(String testName) throws Exception {
131 // ---------------------------------------------------------------
132 // CRUD tests : READ tests
133 // ---------------------------------------------------------------
136 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
137 dependsOnMethods = {"create"})
138 public void read(String testName) throws Exception {
143 // Submit the request to the service and store the response.
144 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
145 int statusCode = res.getStatus();
147 // Check the status code of the response: does it match
148 // the expected response(s)?
149 if (logger.isDebugEnabled()) {
150 logger.debug(testName + ": status = " + statusCode);
152 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
153 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
154 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
156 AccountsCommon output = (AccountsCommon) res.getEntity();
157 Assert.assertNotNull(output);
162 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
163 dependsOnMethods = {"read"})
164 public void readNonExistent(String testName) throws Exception {
167 setupReadNonExistent(testName);
169 // Submit the request to the service and store the response.
170 ClientResponse<AccountsCommon> res = client.read(NON_EXISTENT_ID);
171 int statusCode = res.getStatus();
173 // Check the status code of the response: does it match
174 // the expected response(s)?
175 if (logger.isDebugEnabled()) {
176 logger.debug(testName + ": status = " + statusCode);
178 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
183 // ---------------------------------------------------------------
184 // CRUD tests : READ_LIST tests
185 // ---------------------------------------------------------------
188 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
189 dependsOnMethods = {"createList", "read"})
190 public void readList(String testName) throws Exception {
193 setupReadList(testName);
195 // Submit the request to the service and store the response.
196 ClientResponse<AccountsCommonList> res = client.readList();
197 AccountsCommonList list = res.getEntity();
198 int statusCode = res.getStatus();
200 // Check the status code of the response: does it match
201 // the expected response(s)?
202 if (logger.isDebugEnabled()) {
203 logger.debug(testName + ": status = " + statusCode);
205 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
206 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
207 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
209 // Optionally output additional data about list members for debugging.
210 boolean iterateThroughList = false;
211 if (iterateThroughList && logger.isDebugEnabled()) {
212 List<AccountsCommonList.AccountListItem> items =
213 list.getAccountListItem();
216 for (AccountsCommonList.AccountListItem item : items) {
217 logger.debug(testName + ": list-item[" + i + "] csid=" +
219 logger.debug(testName + ": list-item[" + i + "] anchorName=" +
220 item.getAnchorName());
221 logger.debug(testName + ": list-item[" + i + "] URI=" +
231 // ---------------------------------------------------------------
232 // CRUD tests : UPDATE tests
233 // ---------------------------------------------------------------
236 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
237 dependsOnMethods = {"read"})
238 public void update(String testName) throws Exception {
241 setupUpdate(testName);
243 ClientResponse<AccountsCommon> res =
244 client.read(knownResourceId);
245 if (logger.isDebugEnabled()) {
246 logger.debug(testName + ": read status = " + res.getStatus());
248 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
250 if (logger.isDebugEnabled()) {
251 logger.debug("got object to update with ID: " + knownResourceId);
253 MultipartInput input = (MultipartInput) res.getEntity();
254 AccountsCommon toUpdateAccount =
255 (AccountsCommon) extractPart(input,
256 client.getCommonPartName(), AccountsCommon.class);
257 Assert.assertNotNull(toUpdateAccount);
259 // Update the content of this resource.
260 toUpdateAccount.setEmail("updated-" + toUpdateAccount.getEmail());
261 toUpdateAccount.setPhone("updated-" + toUpdateAccount.getPhone());
262 if (logger.isDebugEnabled()) {
263 logger.debug("updated object");
264 logger.debug(objectAsXmlString(toUpdateAccount,
265 AccountsCommon.class));
268 // Submit the request to the service and store the response.
269 res = client.update(knownResourceId, toUpdateAccount);
270 int statusCode = res.getStatus();
271 // Check the status code of the response: does it match the expected response(s)?
272 if (logger.isDebugEnabled()) {
273 logger.debug(testName + ": status = " + statusCode);
275 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
276 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
277 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
280 AccountsCommon updatedAccount = (AccountsCommon)res.getEntity();
281 Assert.assertNotNull(updatedAccount);
283 Assert.assertEquals(updatedAccount.getEmail(),
284 toUpdateAccount.getPhone(),
285 "Data in updated object did not match submitted data.");
290 // Placeholders until the three tests below can be uncommented.
291 // See Issue CSPACE-401.
293 public void updateWithEmptyEntityBody(String testName) throws Exception {
297 public void updateWithMalformedXml(String testName) throws Exception {
301 public void updateWithWrongXmlSchema(String testName) throws Exception {
305 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
306 dependsOnMethods = {"update", "testSubmitRequest"})
307 public void updateNonExistent(String testName) throws Exception {
310 setupUpdateNonExistent(testName);
312 // Submit the request to the service and store the response.
314 // Note: The ID used in this 'create' call may be arbitrary.
315 // The only relevant ID may be the one used in updateAccount(), below.
316 AccountsCommon account =
317 createAccountInstance("dalal", "junk");
318 ClientResponse<AccountsCommon> res =
319 client.update(NON_EXISTENT_ID, account);
320 int statusCode = res.getStatus();
322 // Check the status code of the response: does it match
323 // the expected response(s)?
324 if (logger.isDebugEnabled()) {
325 logger.debug(testName + ": status = " + statusCode);
327 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
328 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
329 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
332 // ---------------------------------------------------------------
333 // CRUD tests : DELETE tests
334 // ---------------------------------------------------------------
337 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
338 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
339 public void delete(String testName) throws Exception {
342 setupDelete(testName);
344 // Submit the request to the service and store the response.
345 ClientResponse<Response> res = client.delete(knownResourceId);
346 int statusCode = res.getStatus();
348 // Check the status code of the response: does it match
349 // the expected response(s)?
350 if (logger.isDebugEnabled()) {
351 logger.debug(testName + ": status = " + statusCode);
353 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
354 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
355 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
360 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
361 dependsOnMethods = {"delete"})
362 public void deleteNonExistent(String testName) throws Exception {
365 setupDeleteNonExistent(testName);
367 // Submit the request to the service and store the response.
368 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
369 int statusCode = res.getStatus();
371 // Check the status code of the response: does it match
372 // the expected response(s)?
373 if (logger.isDebugEnabled()) {
374 logger.debug(testName + ": status = " + statusCode);
376 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
377 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
378 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
381 // ---------------------------------------------------------------
382 // Utility tests : tests of code used in tests above
383 // ---------------------------------------------------------------
385 * Tests the code for manually submitting data that is used by several
386 * of the methods above.
388 @Test(dependsOnMethods = {"create", "read"})
389 public void testSubmitRequest() throws Exception {
391 // Expected status code: 200 OK
392 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
394 // Submit the request to the service and store the response.
395 String method = ServiceRequestType.READ.httpMethodName();
396 String url = getResourceURL(knownResourceId);
397 int statusCode = submitRequest(method, url);
399 // Check the status code of the response: does it match
400 // the expected response(s)?
401 if (logger.isDebugEnabled()) {
402 logger.debug("testSubmitRequest: url=" + url +
403 " status=" + statusCode);
405 Assert.assertEquals(statusCode, EXPECTED_STATUS);
409 // ---------------------------------------------------------------
410 // Utility methods used by tests above
411 // ---------------------------------------------------------------
412 private AccountsCommon createAccountInstance(String anchorName,
415 AccountsCommon account = new AccountsCommon();
416 account.setAnchorName(anchorName);
417 account.setUserName(anchorName);
418 byte[] b64passwd = Base64.encodeBase64(passwd.getBytes());
419 account.setPassword(b64passwd);
421 if (logger.isDebugEnabled()) {
422 logger.debug("to be created, account common");
423 logger.debug(objectAsXmlString(account,
424 AccountsCommon.class));