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.ArrayList;
26 import javax.ws.rs.core.Response;
28 import org.collectionspace.services.client.AbstractCommonListUtils;
29 import org.collectionspace.services.client.CollectionSpaceClient;
30 import org.collectionspace.services.client.ArticleClient;
31 import org.collectionspace.services.client.PayloadInputPart;
32 import org.collectionspace.services.client.PayloadOutputPart;
33 import org.collectionspace.services.client.PoxPayloadIn;
34 import org.collectionspace.services.client.PoxPayloadOut;
35 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
36 import org.collectionspace.services.jaxb.AbstractCommonList;
37 import org.collectionspace.services.article.ArticlesCommon;
39 import org.jboss.resteasy.client.ClientResponse;
40 import org.testng.Assert;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * ArticleServiceTest, carries out tests against a deployed and running Articles
49 * $LastChangedRevision$ $LastChangedDate$
51 public class ArticleServiceTest extends
52 AbstractPoxServiceTestImpl<AbstractCommonList, ArticlesCommon> {
55 private final String CLASS_NAME = ArticleServiceTest.class.getName();
56 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
57 // Instance variables specific to this test.
58 /** The service path component. */
59 final String SERVICE_NAME = "articles";
60 final String SERVICE_PATH_COMPONENT = "articles";
66 * org.collectionspace.services.client.test.BaseServiceTest#getClientInstance
70 protected ArticleClient getClientInstance() {
71 return new ArticleClient();
77 * @see org.collectionspace.services.client.test.BaseServiceTest#
78 * getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
81 protected AbstractCommonList getCommonList(
82 ClientResponse<AbstractCommonList> response) {
83 return response.getEntity(AbstractCommonList.class);
86 // ---------------------------------------------------------------
87 // CRUD tests : CREATE tests
88 // ---------------------------------------------------------------
96 * org.collectionspace.services.client.test.ServiceTest#create(java.lang
100 // @Test(dataProvider = "testName", dataProviderClass =
101 // AbstractServiceTestImpl.class)
102 public void create(String testName) throws Exception {
103 // Perform setup, such as initializing the type of service request
104 // (e.g. CREATE, DELETE), its valid and expected status codes, and
105 // its associated HTTP method name (e.g. POST, DELETE).
108 // Submit the request to the service and store the response.
109 ArticleClient client = new ArticleClient();
110 String identifier = createIdentifier();
111 PoxPayloadOut multipart = createArticleInstance(identifier);
113 ClientResponse<Response> res = client.create(multipart);
115 int statusCode = res.getStatus();
117 // Check the status code of the response: does it match
118 // the expected response(s)?
121 // Does it fall within the set of valid status codes?
122 // Does it exactly match the expected status code?
123 if (logger.isDebugEnabled()) {
124 logger.debug(testName + ": status = " + statusCode);
126 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
127 invalidStatusCodeMessage(testRequestType, statusCode));
128 Assert.assertEquals(statusCode, testExpectedStatusCode);
130 newID = extractId(res);
133 res.releaseConnection();
137 // Store the ID returned from the first resource created
138 // for additional tests below.
139 if (knownResourceId == null) {
140 knownResourceId = newID;
141 if (logger.isDebugEnabled()) {
142 logger.debug(testName + ": knownResourceId=" + knownResourceId);
146 // Store the IDs from every resource created by tests,
147 // so they can be deleted after tests have been run.
148 allResourceIdsCreated.add(newID);
155 * org.collectionspace.services.client.test.AbstractServiceTestImpl#createList
159 // @Test(dataProvider = "testName", dataProviderClass =
160 // AbstractServiceTestImpl.class,
161 // dependsOnMethods = {"create"})
162 public void createList(String testName) throws Exception {
163 for (int i = 0; i < 3; i++) {
168 // ---------------------------------------------------------------
169 // CRUD tests : READ tests
170 // ---------------------------------------------------------------
176 * org.collectionspace.services.client.test.AbstractServiceTestImpl#read
180 // @Test(dataProvider = "testName", dataProviderClass =
181 // AbstractServiceTestImpl.class,
182 // dependsOnMethods = {"create"})
183 public void read(String testName) throws Exception {
187 // Submit the request to the service and store the response.
188 ArticleClient client = new ArticleClient();
189 ClientResponse<String> res = client.read(knownResourceId);
190 PoxPayloadIn input = null;
192 assertStatusCode(res, testName);
193 input = new PoxPayloadIn(res.getEntity());
196 res.releaseConnection();
200 // Get the common part of the response and verify that it is not null.
201 PayloadInputPart payloadInputPart = input.getPart(client
202 .getCommonPartName());
203 ArticlesCommon articlesCommon = null;
204 if (payloadInputPart != null) {
205 articlesCommon = (ArticlesCommon) payloadInputPart.getBody();
207 Assert.assertNotNull(articlesCommon);
216 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#
217 * readNonExistent(java.lang.String)
220 // @Test(dataProvider = "testName", dataProviderClass =
221 // AbstractServiceTestImpl.class,
222 // dependsOnMethods = {"read"})
223 public void readNonExistent(String testName) throws Exception {
225 setupReadNonExistent();
227 // Submit the request to the service and store the response.
228 ArticleClient client = new ArticleClient();
229 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
231 int statusCode = res.getStatus();
233 // Check the status code of the response: does it match
234 // the expected response(s)?
235 if (logger.isDebugEnabled()) {
236 logger.debug(testName + ": status = " + statusCode);
238 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
239 invalidStatusCodeMessage(testRequestType, statusCode));
240 Assert.assertEquals(statusCode, testExpectedStatusCode);
243 res.releaseConnection();
248 // ---------------------------------------------------------------
249 // CRUD tests : READ_LIST tests
250 // ---------------------------------------------------------------
258 * org.collectionspace.services.client.test.AbstractServiceTestImpl#readList
262 // @Test(dataProvider = "testName", dataProviderClass =
263 // AbstractServiceTestImpl.class,
264 // dependsOnMethods = {"createList", "read"})
265 public void readList(String testName) throws Exception {
269 // Submit the request to the service and store the response.
270 AbstractCommonList list = null;
271 ArticleClient client = new ArticleClient();
272 ClientResponse<AbstractCommonList> res = client.readList();
273 assertStatusCode(res, testName);
275 int statusCode = res.getStatus();
277 // Check the status code of the response: does it match
278 // the expected response(s)?
279 if (logger.isDebugEnabled()) {
280 logger.debug(testName + ": status = " + statusCode);
282 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
283 invalidStatusCodeMessage(testRequestType, statusCode));
284 Assert.assertEquals(statusCode, testExpectedStatusCode);
286 list = res.getEntity();
289 res.releaseConnection();
293 // Optionally output additional data about list members for debugging.
294 boolean iterateThroughList = true;
295 if (iterateThroughList && logger.isDebugEnabled()) {
296 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
305 // ---------------------------------------------------------------
306 // CRUD tests : UPDATE tests
307 // ---------------------------------------------------------------
315 * org.collectionspace.services.client.test.AbstractServiceTestImpl#update
319 // @Test(dataProvider = "testName", dataProviderClass =
320 // AbstractServiceTestImpl.class,
321 // dependsOnMethods = {"read"})
322 public void update(String testName) throws Exception {
326 // Retrieve the contents of a resource to update.
327 ArticleClient client = new ArticleClient();
328 ClientResponse<String> res = client.read(knownResourceId);
329 PoxPayloadIn input = null;
331 assertStatusCode(res, testName);
332 input = new PoxPayloadIn(res.getEntity());
333 if (logger.isDebugEnabled()) {
334 logger.debug("got object to update with ID: " + knownResourceId);
338 res.releaseConnection();
342 // Extract the common part from the response.
343 PayloadInputPart payloadInputPart = input.getPart(client
344 .getCommonPartName());
345 ArticlesCommon articlesCommon = null;
346 if (payloadInputPart != null) {
347 articlesCommon = (ArticlesCommon) payloadInputPart.getBody();
349 Assert.assertNotNull(articlesCommon);
351 // Update the content of this resource.
352 articlesCommon.setArticleNumber("updated-"
353 + articlesCommon.getArticleNumber());
354 articlesCommon.setArticleJobId("updated-" + articlesCommon.getArticleJobId());
355 if (logger.isDebugEnabled()) {
356 logger.debug("to be updated object");
357 logger.debug(objectAsXmlString(articlesCommon, ArticlesCommon.class));
362 // Submit the updated common part in an update request to the service
363 // and store the response.
364 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
365 PayloadOutputPart commonPart = output.addPart(
366 client.getCommonPartName(), articlesCommon);
367 res = client.update(knownResourceId, output);
369 assertStatusCode(res, testName);
370 int statusCode = res.getStatus();
371 // Check the status code of the response: does it match the expected
373 if (logger.isDebugEnabled()) {
374 logger.debug(testName + ": status = " + statusCode);
376 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
377 invalidStatusCodeMessage(testRequestType, statusCode));
378 Assert.assertEquals(statusCode, testExpectedStatusCode);
379 input = new PoxPayloadIn(res.getEntity());
382 res.releaseConnection();
386 // Extract the updated common part from the response.
387 payloadInputPart = input.getPart(client.getCommonPartName());
388 ArticlesCommon updatedArticleCommon = null;
389 if (payloadInputPart != null) {
390 updatedArticleCommon = (ArticlesCommon) payloadInputPart.getBody();
392 Assert.assertNotNull(updatedArticleCommon);
394 // Check selected fields in the updated common part.
395 Assert.assertEquals(updatedArticleCommon.getArticleNumber(),
396 articlesCommon.getArticleNumber(),
397 "Data in updated object did not match submitted data.");
399 if (logger.isDebugEnabled()) {
400 logger.debug("UTF-8 data sent=" + articlesCommon.getArticleJobId()
401 + "\n" + "UTF-8 data received="
402 + updatedArticleCommon.getArticleJobId());
405 updatedArticleCommon.getArticleJobId().contains(
406 getUTF8DataFragment()), "UTF-8 data retrieved '"
407 + updatedArticleCommon.getArticleJobId()
408 + "' does not contain expected data '"
409 + getUTF8DataFragment());
410 Assert.assertEquals(updatedArticleCommon.getArticleJobId(),
411 articlesCommon.getArticleJobId(),
412 "Data in updated object did not match submitted data.");
416 // @Test(dataProvider = "testName", dataProviderClass =
417 // AbstractServiceTestImpl.class,
418 // dependsOnMethods = {"update", "testSubmitRequest"})
419 public void updateNonExistent(String testName) throws Exception {
421 setupUpdateNonExistent();
423 // Submit the request to the service and store the response.
424 // Note: The ID used in this 'create' call may be arbitrary.
425 // The only relevant ID may be the one used in update(), below.
426 ArticleClient client = new ArticleClient();
427 PoxPayloadOut multipart = createArticleInstance(NON_EXISTENT_ID);
428 ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
430 int statusCode = res.getStatus();
432 // Check the status code of the response: does it match
433 // the expected response(s)?
434 if (logger.isDebugEnabled()) {
435 logger.debug(testName + ": status = " + statusCode);
437 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
438 invalidStatusCodeMessage(testRequestType, statusCode));
439 Assert.assertEquals(statusCode, testExpectedStatusCode);
442 res.releaseConnection();
447 // ---------------------------------------------------------------
448 // CRUD tests : DELETE tests
449 // ---------------------------------------------------------------
457 * org.collectionspace.services.client.test.AbstractServiceTestImpl#delete
461 // @Test(dataProvider = "testName", dataProviderClass =
462 // AbstractServiceTestImpl.class,
463 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
464 public void delete(String testName) throws Exception {
468 // Submit the request to the service and store the response.
469 ArticleClient client = new ArticleClient();
470 ClientResponse<Response> res = client.delete(knownResourceId);
472 int statusCode = res.getStatus();
474 // Check the status code of the response: does it match
475 // the expected response(s)?
476 if (logger.isDebugEnabled()) {
477 logger.debug(testName + ": status = " + statusCode);
479 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
480 invalidStatusCodeMessage(testRequestType, statusCode));
481 Assert.assertEquals(statusCode, testExpectedStatusCode);
484 res.releaseConnection();
494 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#
495 * deleteNonExistent(java.lang.String)
498 // @Test(dataProvider = "testName", dataProviderClass =
499 // AbstractServiceTestImpl.class,
500 // dependsOnMethods = {"delete"})
501 public void deleteNonExistent(String testName) throws Exception {
503 setupDeleteNonExistent();
505 // Submit the request to the service and store the response.
506 ArticleClient client = new ArticleClient();
507 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
509 int statusCode = res.getStatus();
511 // Check the status code of the response: does it match
512 // the expected response(s)?
513 if (logger.isDebugEnabled()) {
514 logger.debug(testName + ": status = " + statusCode);
516 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
517 invalidStatusCodeMessage(testRequestType, statusCode));
518 Assert.assertEquals(statusCode, testExpectedStatusCode);
521 res.releaseConnection();
526 // ---------------------------------------------------------------
527 // Utility tests : tests of code used in tests above
528 // ---------------------------------------------------------------
531 * Tests the code for manually submitting data that is used by several of
534 // @Test(dependsOnMethods = {"create", "read"})
535 public void testSubmitRequest() {
537 // Expected status code: 200 OK
538 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
540 // Submit the request to the service and store the response.
541 String method = ServiceRequestType.READ.httpMethodName();
542 String url = getResourceURL(knownResourceId);
543 int statusCode = submitRequest(method, url);
545 // Check the status code of the response: does it match
546 // the expected response(s)?
547 if (logger.isDebugEnabled()) {
548 logger.debug("testSubmitRequest: url=" + url + " status="
551 Assert.assertEquals(statusCode, EXPECTED_STATUS);
555 // ---------------------------------------------------------------
556 // Utility methods used by tests above
557 // ---------------------------------------------------------------
560 public String getServiceName() {
567 * @see org.collectionspace.services.client.test.BaseServiceTest#
568 * getServicePathComponent()
571 public String getServicePathComponent() {
572 return SERVICE_PATH_COMPONENT;
576 protected PoxPayloadOut createInstance(String identifier) {
577 return createArticleInstance(identifier);
581 * Creates the article instance.
585 * @return the multipart output
587 private PoxPayloadOut createArticleInstance(String identifier) {
588 return createArticleInstance("loaninNumber-" + identifier,
589 "returnDate-" + identifier);
593 * Creates the article instance.
595 * @param loaninNumber
599 * @return the multipart output
601 private PoxPayloadOut createArticleInstance(String articleNumber,
602 String articleJobId) {
604 ArticlesCommon articlesCommon = new ArticlesCommon();
605 articlesCommon.setArticleNumber(articleNumber);
606 articlesCommon.setArticleJobId(articleJobId);
608 PoxPayloadOut multipart = new PoxPayloadOut(
609 this.getServicePathComponent());
610 PayloadOutputPart commonPart = multipart.addPart(
611 new ArticleClient().getCommonPartName(), articlesCommon);
613 if (logger.isDebugEnabled()) {
614 logger.debug("To be created, article common:");
615 logger.debug(objectAsXmlString(articlesCommon, ArticlesCommon.class));
622 public void CRUDTests(String testName) {
623 // TODO Auto-generated method stub
628 protected PoxPayloadOut createInstance(String commonPartName,
630 PoxPayloadOut result = createArticleInstance(identifier);
635 protected ArticlesCommon updateInstance(ArticlesCommon commonPartObject) {
636 // TODO Auto-generated method stub
641 protected void compareUpdatedInstances(ArticlesCommon original,
642 ArticlesCommon updated) throws Exception {
643 // TODO Auto-generated method stub