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 java.math.BigInteger;
28 import javax.ws.rs.core.Response;
29 import javax.xml.datatype.DatatypeConfigurationException;
30 import javax.xml.datatype.DatatypeFactory;
31 import javax.xml.datatype.XMLGregorianCalendar;
33 import org.collectionspace.services.client.AbstractCommonListUtils;
34 import org.collectionspace.services.client.PublicItemClient;
35 import org.collectionspace.services.client.PayloadInputPart;
36 import org.collectionspace.services.client.PayloadOutputPart;
37 import org.collectionspace.services.client.PoxPayloadIn;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.jaxb.AbstractCommonList;
40 import org.collectionspace.services.publicitem.PublicitemsCommon;
42 import org.jboss.resteasy.client.ClientResponse;
43 import org.testng.Assert;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * PublicItemServiceTest, carries out tests against a deployed and running PublicItem
52 * $LastChangedRevision$ $LastChangedDate$
54 public class PublicItemServiceTest extends
55 AbstractPoxServiceTestImpl<AbstractCommonList, PublicitemsCommon> {
58 private final String CLASS_NAME = PublicItemServiceTest.class.getName();
59 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
60 // Instance variables specific to this test.
61 /** The service path component. */
62 final String SERVICE_NAME = PublicItemClient.SERVICE_NAME;
63 final String SERVICE_PATH_COMPONENT = PublicItemClient.SERVICE_PATH_COMPONENT;
69 * org.collectionspace.services.client.test.BaseServiceTest#getClientInstance
73 protected PublicItemClient getClientInstance() {
74 return new PublicItemClient();
80 * @see org.collectionspace.services.client.test.BaseServiceTest#
81 * getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
84 protected AbstractCommonList getCommonList(Response response) {
85 return response.readEntity(AbstractCommonList.class);
88 // ---------------------------------------------------------------
89 // CRUD tests : CREATE tests
90 // ---------------------------------------------------------------
98 * org.collectionspace.services.client.test.ServiceTest#create(java.lang
102 // @Test(dataProvider = "testName", dataProviderClass =
103 // AbstractServiceTestImpl.class)
104 public void create(String testName) throws Exception {
105 // Perform setup, such as initializing the type of service request
106 // (e.g. CREATE, DELETE), its valid and expected status codes, and
107 // its associated HTTP method name (e.g. POST, DELETE).
110 // Submit the request to the service and store the response.
111 PublicItemClient client = new PublicItemClient();
112 String identifier = createIdentifier();
113 PoxPayloadOut multipart = createPublicItemInstance(identifier);
115 Response res = client.create(multipart);
117 int statusCode = res.getStatus();
119 // Check the status code of the response: does it match
120 // the expected response(s)?
123 // Does it fall within the set of valid status codes?
124 // Does it exactly match the expected status code?
125 if (logger.isDebugEnabled()) {
126 logger.debug(testName + ": status = " + statusCode);
128 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
129 invalidStatusCodeMessage(testRequestType, statusCode));
130 Assert.assertEquals(statusCode, testExpectedStatusCode);
132 newID = extractId(res);
139 // Store the ID returned from the first resource created
140 // for additional tests below.
141 if (knownResourceId == null) {
142 knownResourceId = newID;
143 if (logger.isDebugEnabled()) {
144 logger.debug(testName + ": knownResourceId=" + knownResourceId);
148 // Store the IDs from every resource created by tests,
149 // so they can be deleted after tests have been run.
150 allResourceIdsCreated.add(newID);
157 * org.collectionspace.services.client.test.AbstractServiceTestImpl#createList
161 // @Test(dataProvider = "testName", dataProviderClass =
162 // AbstractServiceTestImpl.class,
163 // dependsOnMethods = {"create"})
164 public void createList(String testName) throws Exception {
165 for (int i = 0; i < 3; i++) {
170 // ---------------------------------------------------------------
171 // CRUD tests : READ tests
172 // ---------------------------------------------------------------
178 * org.collectionspace.services.client.test.AbstractServiceTestImpl#read
182 // @Test(dataProvider = "testName", dataProviderClass =
183 // AbstractServiceTestImpl.class,
184 // dependsOnMethods = {"create"})
185 public void read(String testName) throws Exception {
189 // Submit the request to the service and store the response.
190 PublicItemClient client = new PublicItemClient();
191 Response res = client.read(knownResourceId);
192 PoxPayloadIn input = null;
194 assertStatusCode(res, testName);
195 input = new PoxPayloadIn((String)res.getEntity());
202 // Get the common part of the response and verify that it is not null.
203 PayloadInputPart payloadInputPart = input.getPart(client
204 .getCommonPartName());
205 PublicitemsCommon publicItemsCommon = null;
206 if (payloadInputPart != null) {
207 publicItemsCommon = (PublicitemsCommon) payloadInputPart.getBody();
209 Assert.assertNotNull(publicItemsCommon);
218 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#
219 * readNonExistent(java.lang.String)
222 // @Test(dataProvider = "testName", dataProviderClass =
223 // AbstractServiceTestImpl.class,
224 // dependsOnMethods = {"read"})
225 public void readNonExistent(String testName) throws Exception {
227 setupReadNonExistent();
229 // Submit the request to the service and store the response.
230 PublicItemClient client = new PublicItemClient();
231 Response res = client.read(NON_EXISTENT_ID);
233 int statusCode = res.getStatus();
235 // Check the status code of the response: does it match
236 // the expected response(s)?
237 if (logger.isDebugEnabled()) {
238 logger.debug(testName + ": status = " + statusCode);
240 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
241 invalidStatusCodeMessage(testRequestType, statusCode));
242 Assert.assertEquals(statusCode, testExpectedStatusCode);
250 // ---------------------------------------------------------------
251 // CRUD tests : READ_LIST tests
252 // ---------------------------------------------------------------
260 * org.collectionspace.services.client.test.AbstractServiceTestImpl#readList
264 // @Test(dataProvider = "testName", dataProviderClass =
265 // AbstractServiceTestImpl.class,
266 // dependsOnMethods = {"createList", "read"})
267 public void readList(String testName) throws Exception {
271 // Submit the request to the service and store the response.
272 AbstractCommonList list = null;
273 PublicItemClient client = new PublicItemClient();
274 Response res = client.readList();
275 assertStatusCode(res, testName);
277 int statusCode = res.getStatus();
279 // Check the status code of the response: does it match
280 // the expected response(s)?
281 if (logger.isDebugEnabled()) {
282 logger.debug(testName + ": status = " + statusCode);
284 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
285 invalidStatusCodeMessage(testRequestType, statusCode));
286 Assert.assertEquals(statusCode, testExpectedStatusCode);
288 list = res.readEntity(getCommonListType());
295 // Optionally output additional data about list members for debugging.
296 boolean iterateThroughList = true;
297 if (iterateThroughList && logger.isDebugEnabled()) {
298 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
307 // ---------------------------------------------------------------
308 // CRUD tests : UPDATE tests
309 // ---------------------------------------------------------------
317 * org.collectionspace.services.client.test.AbstractServiceTestImpl#update
321 // @Test(dataProvider = "testName", dataProviderClass =
322 // AbstractServiceTestImpl.class,
323 // dependsOnMethods = {"read"})
324 public void update(String testName) throws Exception {
328 // Retrieve the contents of a resource to update.
329 PublicItemClient client = new PublicItemClient();
330 Response res = client.read(knownResourceId);
331 PoxPayloadIn input = null;
333 assertStatusCode(res, testName);
334 input = new PoxPayloadIn((String)res.getEntity());
335 if (logger.isDebugEnabled()) {
336 logger.debug("got object to update with ID: " + knownResourceId);
344 // Extract the common part from the response.
345 PayloadInputPart payloadInputPart = input.getPart(client
346 .getCommonPartName());
347 PublicitemsCommon publicItemsCommon = null;
348 if (payloadInputPart != null) {
349 publicItemsCommon = (PublicitemsCommon) payloadInputPart.getBody();
351 Assert.assertNotNull(publicItemsCommon);
353 // Update the content of this resource.
354 publicItemsCommon.setItemNumber("updated-"
355 + publicItemsCommon.getItemNumber());
356 publicItemsCommon.setContentCreationJobId("updated-" + publicItemsCommon.getContentCreationJobId());
357 if (logger.isDebugEnabled()) {
358 logger.debug("to be updated object");
359 logger.debug(objectAsXmlString(publicItemsCommon, PublicitemsCommon.class));
364 // Submit the updated common part in an update request to the service
365 // and store the response.
366 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
367 PayloadOutputPart commonPart = output.addPart(
368 client.getCommonPartName(), publicItemsCommon);
369 res = client.update(knownResourceId, output);
371 assertStatusCode(res, testName);
372 int statusCode = res.getStatus();
373 // Check the status code of the response: does it match the expected
375 if (logger.isDebugEnabled()) {
376 logger.debug(testName + ": status = " + statusCode);
378 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
379 invalidStatusCodeMessage(testRequestType, statusCode));
380 Assert.assertEquals(statusCode, testExpectedStatusCode);
381 input = new PoxPayloadIn((String)res.getEntity());
388 // Extract the updated common part from the response.
389 payloadInputPart = input.getPart(client.getCommonPartName());
390 PublicitemsCommon updatedPublicitemsCommon = null;
391 if (payloadInputPart != null) {
392 updatedPublicitemsCommon = (PublicitemsCommon) payloadInputPart.getBody();
394 Assert.assertNotNull(updatedPublicitemsCommon);
396 // Check selected fields in the updated common part.
397 Assert.assertEquals(updatedPublicitemsCommon.getItemNumber(),
398 publicItemsCommon.getItemNumber(),
399 "Data in updated object did not match submitted data.");
401 if (logger.isDebugEnabled()) {
402 logger.debug("UTF-8 data sent=" + publicItemsCommon.getContentCreationJobId()
403 + "\n" + "UTF-8 data received="
404 + updatedPublicitemsCommon.getContentCreationJobId());
406 Assert.assertTrue(updatedPublicitemsCommon.getContentSource().contains(
407 getUTF8DataFragment()), "UTF-8 data retrieved '"
408 + updatedPublicitemsCommon.getContentSource()
409 + "' does not contain expected data '"
410 + getUTF8DataFragment());
411 Assert.assertEquals(updatedPublicitemsCommon.getContentCreationJobId(),
412 publicItemsCommon.getContentCreationJobId(),
413 "Data in updated object did not match submitted data.");
417 // @Test(dataProvider = "testName", dataProviderClass =
418 // AbstractServiceTestImpl.class,
419 // dependsOnMethods = {"update", "testSubmitRequest"})
420 public void updateNonExistent(String testName) throws Exception {
422 setupUpdateNonExistent();
424 // Submit the request to the service and store the response.
425 // Note: The ID used in this 'create' call may be arbitrary.
426 // The only relevant ID may be the one used in update(), below.
427 PublicItemClient client = new PublicItemClient();
428 PoxPayloadOut multipart = createPublicItemInstance(NON_EXISTENT_ID);
429 Response res = client.update(NON_EXISTENT_ID, multipart);
431 int statusCode = res.getStatus();
433 // Check the status code of the response: does it match
434 // the expected response(s)?
435 if (logger.isDebugEnabled()) {
436 logger.debug(testName + ": status = " + statusCode);
438 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
439 invalidStatusCodeMessage(testRequestType, statusCode));
440 Assert.assertEquals(statusCode, testExpectedStatusCode);
448 // ---------------------------------------------------------------
449 // CRUD tests : DELETE tests
450 // ---------------------------------------------------------------
458 * org.collectionspace.services.client.test.AbstractServiceTestImpl#delete
462 // @Test(dataProvider = "testName", dataProviderClass =
463 // AbstractServiceTestImpl.class,
464 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
465 public void delete(String testName) throws Exception {
469 // Submit the request to the service and store the response.
470 PublicItemClient client = new PublicItemClient();
471 Response res = client.delete(knownResourceId);
473 int statusCode = res.getStatus();
475 // Check the status code of the response: does it match
476 // the expected response(s)?
477 if (logger.isDebugEnabled()) {
478 logger.debug(testName + ": status = " + statusCode);
480 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
481 invalidStatusCodeMessage(testRequestType, statusCode));
482 Assert.assertEquals(statusCode, testExpectedStatusCode);
495 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#
496 * deleteNonExistent(java.lang.String)
499 // @Test(dataProvider = "testName", dataProviderClass =
500 // AbstractServiceTestImpl.class,
501 // dependsOnMethods = {"delete"})
502 public void deleteNonExistent(String testName) throws Exception {
504 setupDeleteNonExistent();
506 // Submit the request to the service and store the response.
507 PublicItemClient client = new PublicItemClient();
508 Response res = client.delete(NON_EXISTENT_ID);
510 int statusCode = res.getStatus();
512 // Check the status code of the response: does it match
513 // the expected response(s)?
514 if (logger.isDebugEnabled()) {
515 logger.debug(testName + ": status = " + statusCode);
517 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
518 invalidStatusCodeMessage(testRequestType, statusCode));
519 Assert.assertEquals(statusCode, testExpectedStatusCode);
527 // ---------------------------------------------------------------
528 // Utility tests : tests of code used in tests above
529 // ---------------------------------------------------------------
532 * Tests the code for manually submitting data that is used by several of
535 // @Test(dependsOnMethods = {"create", "read"})
536 public void testSubmitRequest() {
538 // Expected status code: 200 OK
539 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
541 // Submit the request to the service and store the response.
542 String method = ServiceRequestType.READ.httpMethodName();
543 String url = getResourceURL(knownResourceId);
544 int statusCode = submitRequest(method, url);
546 // Check the status code of the response: does it match
547 // the expected response(s)?
548 if (logger.isDebugEnabled()) {
549 logger.debug("testSubmitRequest: url=" + url + " status="
552 Assert.assertEquals(statusCode, EXPECTED_STATUS);
556 // ---------------------------------------------------------------
557 // Utility methods used by tests above
558 // ---------------------------------------------------------------
561 public String getServiceName() {
568 * @see org.collectionspace.services.client.test.BaseServiceTest#
569 * getServicePathComponent()
572 public String getServicePathComponent() {
573 return SERVICE_PATH_COMPONENT;
577 protected PoxPayloadOut createInstance(String identifier) {
578 return createPublicItemInstance(identifier);
582 * Creates the publicitem instance.
586 * @return the multipart output
588 private PoxPayloadOut createPublicItemInstance(String identifier) {
589 return createPublicItemInstance("itemNumber-" + identifier,
590 "contentCreationJobId-" + identifier);
594 * Creates the PublicItem instance.
597 * the publicitem number
598 * @param contentCreationJobId
599 * the publicitem asynch job ID
600 * @return the multipart output
602 private PoxPayloadOut createPublicItemInstance(String itemNumber,
605 PublicitemsCommon publicItemsCommon = new PublicitemsCommon();
606 publicItemsCommon.setItemNumber(itemNumber);
607 publicItemsCommon.setContentName("Inventory report-" + itemNumber);
608 publicItemsCommon.setContentId("42640780-82eb-4650-8a70");
609 publicItemsCommon.setContentUri("publicitems/1/7eaf0780-9eeb-af50-9d76/content");
610 publicItemsCommon.setContentCreationJobId(itemJobId);
611 publicItemsCommon.setContentSource(getUTF8DataFragment());
613 XMLGregorianCalendar expirationDate =
614 DatatypeFactory.newInstance().newXMLGregorianCalendarDate(2013, 12, 31, 0);
615 publicItemsCommon.setContentExpirationDate(expirationDate);
616 } catch (DatatypeConfigurationException e) {
617 // TODO Auto-generated catch block
620 publicItemsCommon.setContentAccessedCount(new BigInteger("3"));
621 publicItemsCommon.setContentAccessCountLimit(new BigInteger("5"));
623 PoxPayloadOut multipart = new PoxPayloadOut(
624 this.getServicePathComponent());
625 PayloadOutputPart commonPart = multipart.addPart(
626 new PublicItemClient().getCommonPartName(), publicItemsCommon);
628 if (logger.isDebugEnabled()) {
629 logger.debug("To be created, publicitem common:");
630 logger.debug(objectAsXmlString(publicItemsCommon, PublicitemsCommon.class));
637 public void CRUDTests(String testName) {
638 // TODO Auto-generated method stub
643 protected PoxPayloadOut createInstance(String commonPartName,
645 PoxPayloadOut result = createPublicItemInstance(identifier);
650 protected PublicitemsCommon updateInstance(PublicitemsCommon commonPartObject) {
651 // TODO Auto-generated method stub
656 protected void compareUpdatedInstances(PublicitemsCommon original,
657 PublicitemsCommon updated) throws Exception {
658 // TODO Auto-generated method stub