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(
85 ClientResponse<AbstractCommonList> response) {
86 return response.getEntity(AbstractCommonList.class);
89 // ---------------------------------------------------------------
90 // CRUD tests : CREATE tests
91 // ---------------------------------------------------------------
99 * org.collectionspace.services.client.test.ServiceTest#create(java.lang
103 // @Test(dataProvider = "testName", dataProviderClass =
104 // AbstractServiceTestImpl.class)
105 public void create(String testName) throws Exception {
106 // Perform setup, such as initializing the type of service request
107 // (e.g. CREATE, DELETE), its valid and expected status codes, and
108 // its associated HTTP method name (e.g. POST, DELETE).
111 // Submit the request to the service and store the response.
112 PublicItemClient client = new PublicItemClient();
113 String identifier = createIdentifier();
114 PoxPayloadOut multipart = createPublicItemInstance(identifier);
116 Response res = client.create(multipart);
118 int statusCode = res.getStatus();
120 // Check the status code of the response: does it match
121 // the expected response(s)?
124 // Does it fall within the set of valid status codes?
125 // Does it exactly match the expected status code?
126 if (logger.isDebugEnabled()) {
127 logger.debug(testName + ": status = " + statusCode);
129 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
130 invalidStatusCodeMessage(testRequestType, statusCode));
131 Assert.assertEquals(statusCode, testExpectedStatusCode);
133 newID = extractId(res);
140 // Store the ID returned from the first resource created
141 // for additional tests below.
142 if (knownResourceId == null) {
143 knownResourceId = newID;
144 if (logger.isDebugEnabled()) {
145 logger.debug(testName + ": knownResourceId=" + knownResourceId);
149 // Store the IDs from every resource created by tests,
150 // so they can be deleted after tests have been run.
151 allResourceIdsCreated.add(newID);
158 * org.collectionspace.services.client.test.AbstractServiceTestImpl#createList
162 // @Test(dataProvider = "testName", dataProviderClass =
163 // AbstractServiceTestImpl.class,
164 // dependsOnMethods = {"create"})
165 public void createList(String testName) throws Exception {
166 for (int i = 0; i < 3; i++) {
171 // ---------------------------------------------------------------
172 // CRUD tests : READ tests
173 // ---------------------------------------------------------------
179 * org.collectionspace.services.client.test.AbstractServiceTestImpl#read
183 // @Test(dataProvider = "testName", dataProviderClass =
184 // AbstractServiceTestImpl.class,
185 // dependsOnMethods = {"create"})
186 public void read(String testName) throws Exception {
190 // Submit the request to the service and store the response.
191 PublicItemClient client = new PublicItemClient();
192 ClientResponse<String> res = client.read(knownResourceId);
193 PoxPayloadIn input = null;
195 assertStatusCode(res, testName);
196 input = new PoxPayloadIn(res.getEntity());
199 res.releaseConnection();
203 // Get the common part of the response and verify that it is not null.
204 PayloadInputPart payloadInputPart = input.getPart(client
205 .getCommonPartName());
206 PublicitemsCommon publicItemsCommon = null;
207 if (payloadInputPart != null) {
208 publicItemsCommon = (PublicitemsCommon) payloadInputPart.getBody();
210 Assert.assertNotNull(publicItemsCommon);
219 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#
220 * readNonExistent(java.lang.String)
223 // @Test(dataProvider = "testName", dataProviderClass =
224 // AbstractServiceTestImpl.class,
225 // dependsOnMethods = {"read"})
226 public void readNonExistent(String testName) throws Exception {
228 setupReadNonExistent();
230 // Submit the request to the service and store the response.
231 PublicItemClient client = new PublicItemClient();
232 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
234 int statusCode = res.getStatus();
236 // Check the status code of the response: does it match
237 // the expected response(s)?
238 if (logger.isDebugEnabled()) {
239 logger.debug(testName + ": status = " + statusCode);
241 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
242 invalidStatusCodeMessage(testRequestType, statusCode));
243 Assert.assertEquals(statusCode, testExpectedStatusCode);
246 res.releaseConnection();
251 // ---------------------------------------------------------------
252 // CRUD tests : READ_LIST tests
253 // ---------------------------------------------------------------
261 * org.collectionspace.services.client.test.AbstractServiceTestImpl#readList
265 // @Test(dataProvider = "testName", dataProviderClass =
266 // AbstractServiceTestImpl.class,
267 // dependsOnMethods = {"createList", "read"})
268 public void readList(String testName) throws Exception {
272 // Submit the request to the service and store the response.
273 AbstractCommonList list = null;
274 PublicItemClient client = new PublicItemClient();
275 ClientResponse<AbstractCommonList> res = client.readList();
276 assertStatusCode(res, testName);
278 int statusCode = res.getStatus();
280 // Check the status code of the response: does it match
281 // the expected response(s)?
282 if (logger.isDebugEnabled()) {
283 logger.debug(testName + ": status = " + statusCode);
285 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
286 invalidStatusCodeMessage(testRequestType, statusCode));
287 Assert.assertEquals(statusCode, testExpectedStatusCode);
289 list = res.getEntity();
292 res.releaseConnection();
296 // Optionally output additional data about list members for debugging.
297 boolean iterateThroughList = true;
298 if (iterateThroughList && logger.isDebugEnabled()) {
299 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
308 // ---------------------------------------------------------------
309 // CRUD tests : UPDATE tests
310 // ---------------------------------------------------------------
318 * org.collectionspace.services.client.test.AbstractServiceTestImpl#update
322 // @Test(dataProvider = "testName", dataProviderClass =
323 // AbstractServiceTestImpl.class,
324 // dependsOnMethods = {"read"})
325 public void update(String testName) throws Exception {
329 // Retrieve the contents of a resource to update.
330 PublicItemClient client = new PublicItemClient();
331 ClientResponse<String> res = client.read(knownResourceId);
332 PoxPayloadIn input = null;
334 assertStatusCode(res, testName);
335 input = new PoxPayloadIn(res.getEntity());
336 if (logger.isDebugEnabled()) {
337 logger.debug("got object to update with ID: " + knownResourceId);
341 res.releaseConnection();
345 // Extract the common part from the response.
346 PayloadInputPart payloadInputPart = input.getPart(client
347 .getCommonPartName());
348 PublicitemsCommon publicItemsCommon = null;
349 if (payloadInputPart != null) {
350 publicItemsCommon = (PublicitemsCommon) payloadInputPart.getBody();
352 Assert.assertNotNull(publicItemsCommon);
354 // Update the content of this resource.
355 publicItemsCommon.setItemNumber("updated-"
356 + publicItemsCommon.getItemNumber());
357 publicItemsCommon.setContentCreationJobId("updated-" + publicItemsCommon.getContentCreationJobId());
358 if (logger.isDebugEnabled()) {
359 logger.debug("to be updated object");
360 logger.debug(objectAsXmlString(publicItemsCommon, PublicitemsCommon.class));
365 // Submit the updated common part in an update request to the service
366 // and store the response.
367 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
368 PayloadOutputPart commonPart = output.addPart(
369 client.getCommonPartName(), publicItemsCommon);
370 res = client.update(knownResourceId, output);
372 assertStatusCode(res, testName);
373 int statusCode = res.getStatus();
374 // Check the status code of the response: does it match the expected
376 if (logger.isDebugEnabled()) {
377 logger.debug(testName + ": status = " + statusCode);
379 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
380 invalidStatusCodeMessage(testRequestType, statusCode));
381 Assert.assertEquals(statusCode, testExpectedStatusCode);
382 input = new PoxPayloadIn(res.getEntity());
385 res.releaseConnection();
389 // Extract the updated common part from the response.
390 payloadInputPart = input.getPart(client.getCommonPartName());
391 PublicitemsCommon updatedPublicitemsCommon = null;
392 if (payloadInputPart != null) {
393 updatedPublicitemsCommon = (PublicitemsCommon) payloadInputPart.getBody();
395 Assert.assertNotNull(updatedPublicitemsCommon);
397 // Check selected fields in the updated common part.
398 Assert.assertEquals(updatedPublicitemsCommon.getItemNumber(),
399 publicItemsCommon.getItemNumber(),
400 "Data in updated object did not match submitted data.");
402 if (logger.isDebugEnabled()) {
403 logger.debug("UTF-8 data sent=" + publicItemsCommon.getContentCreationJobId()
404 + "\n" + "UTF-8 data received="
405 + updatedPublicitemsCommon.getContentCreationJobId());
407 Assert.assertTrue(updatedPublicitemsCommon.getContentSource().contains(
408 getUTF8DataFragment()), "UTF-8 data retrieved '"
409 + updatedPublicitemsCommon.getContentSource()
410 + "' does not contain expected data '"
411 + getUTF8DataFragment());
412 Assert.assertEquals(updatedPublicitemsCommon.getContentCreationJobId(),
413 publicItemsCommon.getContentCreationJobId(),
414 "Data in updated object did not match submitted data.");
418 // @Test(dataProvider = "testName", dataProviderClass =
419 // AbstractServiceTestImpl.class,
420 // dependsOnMethods = {"update", "testSubmitRequest"})
421 public void updateNonExistent(String testName) throws Exception {
423 setupUpdateNonExistent();
425 // Submit the request to the service and store the response.
426 // Note: The ID used in this 'create' call may be arbitrary.
427 // The only relevant ID may be the one used in update(), below.
428 PublicItemClient client = new PublicItemClient();
429 PoxPayloadOut multipart = createPublicItemInstance(NON_EXISTENT_ID);
430 ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
432 int statusCode = res.getStatus();
434 // Check the status code of the response: does it match
435 // the expected response(s)?
436 if (logger.isDebugEnabled()) {
437 logger.debug(testName + ": status = " + statusCode);
439 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
440 invalidStatusCodeMessage(testRequestType, statusCode));
441 Assert.assertEquals(statusCode, testExpectedStatusCode);
444 res.releaseConnection();
449 // ---------------------------------------------------------------
450 // CRUD tests : DELETE tests
451 // ---------------------------------------------------------------
459 * org.collectionspace.services.client.test.AbstractServiceTestImpl#delete
463 // @Test(dataProvider = "testName", dataProviderClass =
464 // AbstractServiceTestImpl.class,
465 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
466 public void delete(String testName) throws Exception {
470 // Submit the request to the service and store the response.
471 PublicItemClient client = new PublicItemClient();
472 ClientResponse<Response> res = client.delete(knownResourceId);
474 int statusCode = res.getStatus();
476 // Check the status code of the response: does it match
477 // the expected response(s)?
478 if (logger.isDebugEnabled()) {
479 logger.debug(testName + ": status = " + statusCode);
481 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
482 invalidStatusCodeMessage(testRequestType, statusCode));
483 Assert.assertEquals(statusCode, testExpectedStatusCode);
486 res.releaseConnection();
496 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#
497 * deleteNonExistent(java.lang.String)
500 // @Test(dataProvider = "testName", dataProviderClass =
501 // AbstractServiceTestImpl.class,
502 // dependsOnMethods = {"delete"})
503 public void deleteNonExistent(String testName) throws Exception {
505 setupDeleteNonExistent();
507 // Submit the request to the service and store the response.
508 PublicItemClient client = new PublicItemClient();
509 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
511 int statusCode = res.getStatus();
513 // Check the status code of the response: does it match
514 // the expected response(s)?
515 if (logger.isDebugEnabled()) {
516 logger.debug(testName + ": status = " + statusCode);
518 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
519 invalidStatusCodeMessage(testRequestType, statusCode));
520 Assert.assertEquals(statusCode, testExpectedStatusCode);
523 res.releaseConnection();
528 // ---------------------------------------------------------------
529 // Utility tests : tests of code used in tests above
530 // ---------------------------------------------------------------
533 * Tests the code for manually submitting data that is used by several of
536 // @Test(dependsOnMethods = {"create", "read"})
537 public void testSubmitRequest() {
539 // Expected status code: 200 OK
540 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
542 // Submit the request to the service and store the response.
543 String method = ServiceRequestType.READ.httpMethodName();
544 String url = getResourceURL(knownResourceId);
545 int statusCode = submitRequest(method, url);
547 // Check the status code of the response: does it match
548 // the expected response(s)?
549 if (logger.isDebugEnabled()) {
550 logger.debug("testSubmitRequest: url=" + url + " status="
553 Assert.assertEquals(statusCode, EXPECTED_STATUS);
557 // ---------------------------------------------------------------
558 // Utility methods used by tests above
559 // ---------------------------------------------------------------
562 public String getServiceName() {
569 * @see org.collectionspace.services.client.test.BaseServiceTest#
570 * getServicePathComponent()
573 public String getServicePathComponent() {
574 return SERVICE_PATH_COMPONENT;
578 protected PoxPayloadOut createInstance(String identifier) {
579 return createPublicItemInstance(identifier);
583 * Creates the publicitem instance.
587 * @return the multipart output
589 private PoxPayloadOut createPublicItemInstance(String identifier) {
590 return createPublicItemInstance("itemNumber-" + identifier,
591 "contentCreationJobId-" + identifier);
595 * Creates the PublicItem instance.
598 * the publicitem number
599 * @param contentCreationJobId
600 * the publicitem asynch job ID
601 * @return the multipart output
603 private PoxPayloadOut createPublicItemInstance(String itemNumber,
606 PublicitemsCommon publicItemsCommon = new PublicitemsCommon();
607 publicItemsCommon.setItemNumber(itemNumber);
608 publicItemsCommon.setContentName("Inventory report-" + itemNumber);
609 publicItemsCommon.setContentId("42640780-82eb-4650-8a70");
610 publicItemsCommon.setContentUri("publicitems/1/7eaf0780-9eeb-af50-9d76/content");
611 publicItemsCommon.setContentCreationJobId(itemJobId);
612 publicItemsCommon.setContentSource(getUTF8DataFragment());
614 XMLGregorianCalendar expirationDate =
615 DatatypeFactory.newInstance().newXMLGregorianCalendarDate(2013, 12, 31, 0);
616 publicItemsCommon.setContentExpirationDate(expirationDate);
617 } catch (DatatypeConfigurationException e) {
618 // TODO Auto-generated catch block
621 publicItemsCommon.setContentAccessedCount(new BigInteger("3"));
622 publicItemsCommon.setContentAccessCountLimit(new BigInteger("5"));
624 PoxPayloadOut multipart = new PoxPayloadOut(
625 this.getServicePathComponent());
626 PayloadOutputPart commonPart = multipart.addPart(
627 new PublicItemClient().getCommonPartName(), publicItemsCommon);
629 if (logger.isDebugEnabled()) {
630 logger.debug("To be created, publicitem common:");
631 logger.debug(objectAsXmlString(publicItemsCommon, PublicitemsCommon.class));
638 public void CRUDTests(String testName) {
639 // TODO Auto-generated method stub
644 protected PoxPayloadOut createInstance(String commonPartName,
646 PoxPayloadOut result = createPublicItemInstance(identifier);
651 protected PublicitemsCommon updateInstance(PublicitemsCommon commonPartObject) {
652 // TODO Auto-generated method stub
657 protected void compareUpdatedInstances(PublicitemsCommon original,
658 PublicitemsCommon updated) throws Exception {
659 // TODO Auto-generated method stub