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.util.List;
27 import javax.ws.rs.core.Response;
29 import org.collectionspace.services.client.AbstractCommonListUtils;
30 import org.collectionspace.services.client.CollectionSpaceClient;
31 import org.collectionspace.services.client.ExhibitionClient;
32 import org.collectionspace.services.client.PayloadInputPart;
33 import org.collectionspace.services.client.PayloadOutputPart;
34 import org.collectionspace.services.client.PoxPayloadIn;
35 import org.collectionspace.services.client.PoxPayloadOut;
36 import org.collectionspace.services.jaxb.AbstractCommonList;
37 import org.collectionspace.services.exhibition.ExhibitionsCommon;
39 import org.jboss.resteasy.client.ClientResponse;
40 import org.testng.Assert;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * ExhibitionServiceTest, carries out tests against a
47 * deployed and running Exhibition Service.
49 public class ExhibitionServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, ExhibitionsCommon> {
52 private final String CLASS_NAME = ExhibitionServiceTest.class.getName();
53 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
54 // Instance variables specific to this test.
55 /** The service path component. */
56 final String SERVICE_NAME = "exhibitions";
57 final String SERVICE_PATH_COMPONENT = "exhibitions";
60 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
63 protected CollectionSpaceClient getClientInstance() {
64 return new ExhibitionClient();
68 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
71 protected AbstractCommonList getCommonList(Response response) {
72 return response.readEntity(AbstractCommonList.class);
75 // ---------------------------------------------------------------
76 // CRUD tests : CREATE tests
77 // ---------------------------------------------------------------
82 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
85 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
86 public void create(String testName) throws Exception {
87 // Perform setup, such as initializing the type of service request
88 // (e.g. CREATE, DELETE), its valid and expected status codes, and
89 // its associated HTTP method name (e.g. POST, DELETE).
92 // Submit the request to the service and store the response.
93 ExhibitionClient client = new ExhibitionClient();
94 String identifier = createIdentifier();
95 PoxPayloadOut multipart = createExhibitionInstance(identifier);
97 Response res = client.create(multipart);
99 int statusCode = res.getStatus();
101 // Check the status code of the response: does it match
102 // the expected response(s)?
105 // Does it fall within the set of valid status codes?
106 // Does it exactly match the expected status code?
107 if (logger.isDebugEnabled()) {
108 logger.debug(testName + ": status = " + statusCode);
110 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
111 invalidStatusCodeMessage(testRequestType, statusCode));
112 Assert.assertEquals(statusCode, testExpectedStatusCode);
114 newID = extractId(res);
121 // Store the ID returned from the first resource created
122 // for additional tests below.
123 if (knownResourceId == null) {
124 knownResourceId = newID;
125 if (logger.isDebugEnabled()) {
126 logger.debug(testName + ": knownResourceId=" + knownResourceId);
130 // Store the IDs from every resource created by tests,
131 // so they can be deleted after tests have been run.
132 allResourceIdsCreated.add(newID);
136 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
139 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
140 // dependsOnMethods = {"create"})
141 public void createList(String testName) throws Exception {
142 for (int i = 0; i < 3; i++) {
150 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
151 dependsOnMethods = {"create", "testSubmitRequest"})
152 public void createWithEmptyEntityBody(String testName) throws Exception {
154 if (logger.isDebugEnabled()) {
155 logger.debug(testBanner(testName, CLASS_NAME));
158 setupCreateWithEmptyEntityBody();
160 // Submit the request to the service and store the response.
161 String method = REQUEST_TYPE.httpMethodName();
162 String url = getServiceRootURL();
163 String mediaType = MediaType.APPLICATION_XML;
164 final String entity = "";
165 int statusCode = submitRequest(method, url, mediaType, entity);
167 // Check the status code of the response: does it match
168 // the expected response(s)?
169 if(logger.isDebugEnabled()){
170 logger.debug("createWithEmptyEntityBody url=" + url +
171 " status=" + statusCode);
173 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
174 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
175 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
179 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
180 dependsOnMethods = {"create", "testSubmitRequest"})
181 public void createWithMalformedXml(String testName) throws Exception {
183 if (logger.isDebugEnabled()) {
184 logger.debug(testBanner(testName, CLASS_NAME));
187 setupCreateWithMalformedXml(testName, logger);
189 // Submit the request to the service and store the response.
190 String method = REQUEST_TYPE.httpMethodName();
191 String url = getServiceRootURL();
192 String mediaType = MediaType.APPLICATION_XML;
193 final String entity = MALFORMED_XML_DATA; // Constant from base class.
194 int statusCode = submitRequest(method, url, mediaType, entity);
196 // Check the status code of the response: does it match
197 // the expected response(s)?
198 if(logger.isDebugEnabled()){
199 logger.debug(testName + ": url=" + url +
200 " status=" + statusCode);
202 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
203 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
204 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
208 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
209 dependsOnMethods = {"create", "testSubmitRequest"})
210 public void createWithWrongXmlSchema(String testName) throws Exception {
212 if (logger.isDebugEnabled()) {
213 logger.debug(testBanner(testName, CLASS_NAME));
216 setupCreateWithWrongXmlSchema(testName, logger);
218 // Submit the request to the service and store the response.
219 String method = REQUEST_TYPE.httpMethodName();
220 String url = getServiceRootURL();
221 String mediaType = MediaType.APPLICATION_XML;
222 final String entity = WRONG_XML_SCHEMA_DATA;
223 int statusCode = submitRequest(method, url, mediaType, entity);
225 // Check the status code of the response: does it match
226 // the expected response(s)?
227 if(logger.isDebugEnabled()){
228 logger.debug(testName + ": url=" + url +
229 " status=" + statusCode);
231 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
232 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
233 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
237 // ---------------------------------------------------------------
238 // CRUD tests : READ tests
239 // ---------------------------------------------------------------
244 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
247 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
248 // dependsOnMethods = {"create"})
249 public void read(String testName) throws Exception {
253 // Submit the request to the service and store the response.
254 ExhibitionClient client = new ExhibitionClient();
255 Response res = client.read(knownResourceId);
256 PoxPayloadIn input = null;
258 assertStatusCode(res, testName);
259 input = new PoxPayloadIn((String)res.getEntity());
266 // Get the common part of the response and verify that it is not null.
267 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
268 ExhibitionsCommon exhibitionCommon = null;
269 if (payloadInputPart != null) {
270 exhibitionCommon = (ExhibitionsCommon) payloadInputPart.getBody();
272 Assert.assertNotNull(exhibitionCommon);
274 // Check selected fields.
276 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
277 String generalNote = exhibitionCommon.getGeneralNote();
279 if (logger.isDebugEnabled()) {
280 logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
281 + "UTF-8 data received=" + generalNote);
284 Assert.assertEquals(generalNote, getUTF8DataFragment(),
285 "UTF-8 data retrieved '" + generalNote
286 + "' does not match expected data '" + getUTF8DataFragment());
292 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
295 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
296 // dependsOnMethods = {"read"})
297 public void readNonExistent(String testName) throws Exception {
299 setupReadNonExistent();
301 // Submit the request to the service and store the response.
302 ExhibitionClient client = new ExhibitionClient();
303 Response res = client.read(NON_EXISTENT_ID);
305 int statusCode = res.getStatus();
307 // Check the status code of the response: does it match
308 // the expected response(s)?
309 if (logger.isDebugEnabled()) {
310 logger.debug(testName + ": status = " + statusCode);
312 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
313 invalidStatusCodeMessage(testRequestType, statusCode));
314 Assert.assertEquals(statusCode, testExpectedStatusCode);
322 // ---------------------------------------------------------------
323 // CRUD tests : READ_LIST tests
324 // ---------------------------------------------------------------
329 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
332 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
333 // dependsOnMethods = {"createList", "read"})
334 public void readList(String testName) throws Exception {
338 // Submit the request to the service and store the response.
339 AbstractCommonList list = null;
340 ExhibitionClient client = new ExhibitionClient();
341 Response res = client.readList();
342 assertStatusCode(res, testName);
344 int statusCode = res.getStatus();
346 // Check the status code of the response: does it match
347 // the expected response(s)?
348 if (logger.isDebugEnabled()) {
349 logger.debug(testName + ": status = " + statusCode);
351 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
352 invalidStatusCodeMessage(testRequestType, statusCode));
353 Assert.assertEquals(statusCode, testExpectedStatusCode);
355 list = res.readEntity(getCommonListType());
362 // Optionally output additional data about list members for debugging.
363 boolean iterateThroughList = true;
364 if(iterateThroughList && logger.isDebugEnabled()){
365 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
373 // ---------------------------------------------------------------
374 // CRUD tests : UPDATE tests
375 // ---------------------------------------------------------------
380 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
383 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
384 // dependsOnMethods = {"read"})
385 public void update(String testName) throws Exception {
389 // Retrieve the contents of a resource to update.
390 ExhibitionClient client = new ExhibitionClient();
391 Response res = client.read(knownResourceId);
392 PoxPayloadIn input = null;
394 assertStatusCode(res, testName);
395 input = new PoxPayloadIn((String)res.getEntity());
396 if (logger.isDebugEnabled()) {
397 logger.debug("got object to update with ID: " + knownResourceId);
405 // Extract the common part from the response.
406 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
407 ExhibitionsCommon exhibitionCommon = null;
408 if (payloadInputPart != null) {
409 exhibitionCommon = (ExhibitionsCommon) payloadInputPart.getBody();
411 Assert.assertNotNull(exhibitionCommon);
413 // Update the content of this resource.
414 exhibitionCommon.setExhibitionNumber("updated-" + exhibitionCommon.getExhibitionNumber());
416 String generalNote = exhibitionCommon.getGeneralNote();
417 exhibitionCommon.setGeneralNote("updated-generalNote-" + generalNote);
419 if (logger.isDebugEnabled()) {
420 logger.debug("to be updated object");
421 logger.debug(objectAsXmlString(exhibitionCommon, ExhibitionsCommon.class));
426 // Submit the updated common part in an update request to the service
427 // and store the response.
428 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
429 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), exhibitionCommon);
430 res = client.update(knownResourceId, output);
432 assertStatusCode(res, testName);
433 int statusCode = res.getStatus();
434 // Check the status code of the response: does it match 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);
441 input = new PoxPayloadIn((String)res.getEntity());
448 // Extract the updated common part from the response.
449 payloadInputPart = input.getPart(client.getCommonPartName());
450 ExhibitionsCommon updatedExhibitionCommon = null;
451 if (payloadInputPart != null) {
452 updatedExhibitionCommon = (ExhibitionsCommon) payloadInputPart.getBody();
454 Assert.assertNotNull(updatedExhibitionCommon);
456 // Check selected fields in the updated common part.
457 Assert.assertEquals(updatedExhibitionCommon.getExhibitionNumber(),
458 exhibitionCommon.getExhibitionNumber(),
459 "Data in updated object did not match submitted data.");
461 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
462 String originalGeneralNote = exhibitionCommon.getGeneralNote();
463 String updatedGeneralNote = updatedExhibitionCommon.getGeneralNote();
465 Assert.assertEquals(updatedGeneralNote, originalGeneralNote,
466 "Data in updated object did not match submitted data.");
468 if(logger.isDebugEnabled()){
469 logger.debug("UTF-8 data sent=" + originalGeneralNote + "\n"
470 + "UTF-8 data received=" + updatedGeneralNote);
472 Assert.assertTrue(updatedGeneralNote.contains(getUTF8DataFragment()),
473 "UTF-8 data retrieved '" + updatedGeneralNote
474 + "' does not match expected data '" + getUTF8DataFragment());
478 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
479 // dependsOnMethods = {"update", "testSubmitRequest"})
480 public void updateNonExistent(String testName) throws Exception {
482 setupUpdateNonExistent();
484 // Submit the request to the service and store the response.
485 // Note: The ID used in this 'create' call may be arbitrary.
486 // The only relevant ID may be the one used in update(), below.
487 ExhibitionClient client = new ExhibitionClient();
488 PoxPayloadOut multipart = createExhibitionInstance(NON_EXISTENT_ID);
489 Response res = client.update(NON_EXISTENT_ID, multipart);
491 int statusCode = res.getStatus();
493 // Check the status code of the response: does it match
494 // the expected response(s)?
495 if (logger.isDebugEnabled()) {
496 logger.debug(testName + ": status = " + statusCode);
498 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
499 invalidStatusCodeMessage(testRequestType, statusCode));
500 Assert.assertEquals(statusCode, testExpectedStatusCode);
508 // ---------------------------------------------------------------
509 // CRUD tests : DELETE tests
510 // ---------------------------------------------------------------
515 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
518 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
519 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
520 public void delete(String testName) throws Exception {
524 // Submit the request to the service and store the response.
525 ExhibitionClient client = new ExhibitionClient();
526 Response res = client.delete(knownResourceId);
528 int statusCode = res.getStatus();
530 // Check the status code of the response: does it match
531 // the expected response(s)?
532 if (logger.isDebugEnabled()) {
533 logger.debug(testName + ": status = " + statusCode);
535 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
536 invalidStatusCodeMessage(testRequestType, statusCode));
537 Assert.assertEquals(statusCode, testExpectedStatusCode);
548 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
551 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
552 // dependsOnMethods = {"delete"})
553 public void deleteNonExistent(String testName) throws Exception {
555 setupDeleteNonExistent();
557 // Submit the request to the service and store the response.
558 ExhibitionClient client = new ExhibitionClient();
559 Response res = client.delete(NON_EXISTENT_ID);
561 int statusCode = res.getStatus();
563 // Check the status code of the response: does it match
564 // the expected response(s)?
565 if (logger.isDebugEnabled()) {
566 logger.debug(testName + ": status = " + statusCode);
568 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
569 invalidStatusCodeMessage(testRequestType, statusCode));
570 Assert.assertEquals(statusCode, testExpectedStatusCode);
578 // ---------------------------------------------------------------
579 // Utility tests : tests of code used in tests above
580 // ---------------------------------------------------------------
583 * Tests the code for manually submitting data that is used by several
584 * of the methods above.
586 // @Test(dependsOnMethods = {"create", "read"})
587 public void testSubmitRequest() {
589 // Expected status code: 200 OK
590 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
592 // Submit the request to the service and store the response.
593 String method = ServiceRequestType.READ.httpMethodName();
594 String url = getResourceURL(knownResourceId);
595 int statusCode = submitRequest(method, url);
597 // Check the status code of the response: does it match
598 // the expected response(s)?
599 if (logger.isDebugEnabled()) {
600 logger.debug("testSubmitRequest: url=" + url
601 + " status=" + statusCode);
603 Assert.assertEquals(statusCode, EXPECTED_STATUS);
607 // ---------------------------------------------------------------
608 // Utility methods used by tests above
609 // ---------------------------------------------------------------
612 public String getServiceName() {
617 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
620 public String getServicePathComponent() {
621 return SERVICE_PATH_COMPONENT;
625 protected PoxPayloadOut createInstance(String identifier) {
626 return createExhibitionInstance(identifier);
630 * Creates the exhibition instance.
632 * @param identifier the identifier
633 * @return the multipart output
635 //private PoxPayloadOut createExhibitionInstance(String identifier) {
636 // return createExhibitionInstance("exhibitionNumber-" + identifier);
640 * Creates the exhibition instance.
642 * @param exhibitionNumber the exhibition number
643 * @return the multipart output
645 private PoxPayloadOut createExhibitionInstance(String exhibitionNumber) {
647 ExhibitionsCommon exhibitionCommon = new ExhibitionsCommon();
648 exhibitionCommon.setExhibitionNumber(exhibitionNumber);
649 exhibitionCommon.setGeneralNote(getUTF8DataFragment());
651 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
652 PayloadOutputPart commonPart =
653 multipart.addPart(new ExhibitionClient().getCommonPartName(), exhibitionCommon);
655 if (logger.isDebugEnabled()) {
656 logger.debug("to be created, exhibition common");
657 logger.debug(objectAsXmlString(exhibitionCommon, ExhibitionsCommon.class));
664 public void CRUDTests(String testName) {
665 // TODO Auto-generated method stub
670 protected PoxPayloadOut createInstance(String commonPartName,
672 PoxPayloadOut result = createExhibitionInstance(identifier);
677 protected ExhibitionsCommon updateInstance(ExhibitionsCommon commonPartObject) {
678 // TODO Auto-generated method stub
683 protected void compareUpdatedInstances(ExhibitionsCommon original,
684 ExhibitionsCommon updated) throws Exception {
685 // TODO Auto-generated method stub