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.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.collectionspace.services.client.CollectionSpaceClient;
30 import org.collectionspace.services.client.ContactClient;
31 import org.collectionspace.services.client.ContactClientUtils;
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.contact.ContactsCommon;
36 import org.collectionspace.services.contact.ContactsCommonList;
37 import org.collectionspace.services.jaxb.AbstractCommonList;
39 import org.jboss.resteasy.client.ClientResponse;
40 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
42 import org.testng.Assert;
43 import org.testng.annotations.AfterClass;
44 import org.testng.annotations.Test;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * ContactServiceTest, carries out tests against a
51 * deployed and running Contact Service.
53 * $LastChangedRevision: 917 $
54 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
56 public class ContactServiceTest extends AbstractServiceTestImpl {
57 private final String CLASS_NAME = ContactServiceTest.class.getName();
58 private final Logger logger = LoggerFactory.getLogger(ContactServiceTest.class);
60 // Instance variables specific to this test.
61 // final String SERVICE_PATH_COMPONENT = "contacts";
63 private String knownResourceId = null;
66 public String getServicePathComponent() {
67 return ContactClient.SERVICE_PATH_COMPONENT;
71 protected String getServiceName() {
72 return ContactClient.SERVICE_NAME;
76 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
79 protected CollectionSpaceClient getClientInstance() {
80 return new ContactClient();
84 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
87 protected AbstractCommonList getAbstractCommonList(
88 ClientResponse<AbstractCommonList> response) {
89 return response.getEntity(ContactsCommonList.class);
93 // protected PoxPayloadOut createInstance(String identifier) {
94 // ContactClient client = new ContactClient();
95 // PoxPayloadOut multipart =
96 // ContactClientUtils.createContactInstance(identifier, client.getCommonPartName());
100 // ---------------------------------------------------------------
101 // CRUD tests : CREATE tests
102 // ---------------------------------------------------------------
105 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
106 public void create(String testName) throws Exception {
108 if (logger.isDebugEnabled()) {
109 logger.debug(testBanner(testName, CLASS_NAME));
111 // Perform setup, such as initializing the type of service request
112 // (e.g. CREATE, DELETE), its valid and expected status codes, and
113 // its associated HTTP method name (e.g. POST, DELETE).
116 // Submit the request to the service and store the response.
117 ContactClient client = new ContactClient();
118 String identifier = createIdentifier();
119 PoxPayloadOut multipart =
120 ContactClientUtils.createContactInstance(identifier, client.getCommonPartName());
121 ClientResponse<Response> res = client.create(multipart);
123 int statusCode = res.getStatus();
125 // Check the status code of the response: does it match
126 // the expected response(s)?
129 // Does it fall within the set of valid status codes?
130 // Does it exactly match the expected status code?
131 if(logger.isDebugEnabled()){
132 logger.debug(testName + ": status = " + statusCode);
134 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
135 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
136 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
138 // Store the ID returned from the first resource created
139 // for additional tests below.
140 if (knownResourceId == null){
141 knownResourceId = extractId(res);
142 if (logger.isDebugEnabled()) {
143 logger.debug(testName + ": knownResourceId=" + knownResourceId);
147 // Store the IDs from every resource created by tests,
148 // so they can be deleted after tests have been run.
149 allResourceIdsCreated.add(extractId(res));
153 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
154 dependsOnMethods = {"create"})
155 public void createList(String testName) throws Exception {
156 for(int i = 0; i < 3; i++){
162 // Placeholders until the three tests below can be uncommented.
163 // See Issue CSPACE-401.
165 public void createWithEmptyEntityBody(String testName) throws Exception {
166 //Should this really be empty?
170 public void createWithMalformedXml(String testName) throws Exception {
171 //Should this really be empty??
175 public void createWithWrongXmlSchema(String testName) throws Exception {
176 //Should this really be empty??
181 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
182 dependsOnMethods = {"create", "testSubmitRequest"})
183 public void createWithEmptyEntityBody(String testName) throws Exception {
185 if (logger.isDebugEnabled()) {
186 logger.debug(testBanner(testName, CLASS_NAME));
189 setupCreateWithEmptyEntityBody(testName, logger);
191 // Submit the request to the service and store the response.
192 String method = REQUEST_TYPE.httpMethodName();
193 String url = getServiceRootURL();
194 String mediaType = MediaType.APPLICATION_XML;
195 final String entity = "";
196 int statusCode = submitRequest(method, url, mediaType, entity);
198 // Check the status code of the response: does it match
199 // the expected response(s)?
200 if(logger.isDebugEnabled()){
201 logger.debug("createWithEmptyEntityBody url=" + url +
202 " status=" + statusCode);
204 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
205 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
206 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
210 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
211 dependsOnMethods = {"create", "testSubmitRequest"})
212 public void createWithMalformedXml(String testName) throws Exception {
214 if (logger.isDebugEnabled()) {
215 logger.debug(testBanner(testName, CLASS_NAME));
218 setupCreateWithMalformedXml();
220 // Submit the request to the service and store the response.
221 String method = REQUEST_TYPE.httpMethodName();
222 String url = getServiceRootURL();
223 String mediaType = MediaType.APPLICATION_XML;
224 final String entity = MALFORMED_XML_DATA; // Constant from base class.
225 int statusCode = submitRequest(method, url, mediaType, entity);
227 // Check the status code of the response: does it match
228 // the expected response(s)?
229 if(logger.isDebugEnabled()){
230 logger.debug(testName + ": url=" + url +
231 " status=" + statusCode);
233 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
234 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
235 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
239 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
240 dependsOnMethods = {"create", "testSubmitRequest"})
241 public void createWithWrongXmlSchema(String testName) throws Exception {
243 if (logger.isDebugEnabled()) {
244 logger.debug(testBanner(testName, CLASS_NAME));
247 setupCreateWithWrongXmlSchema(testName, logger);
249 // Submit the request to the service and store the response.
250 String method = REQUEST_TYPE.httpMethodName();
251 String url = getServiceRootURL();
252 String mediaType = MediaType.APPLICATION_XML;
253 final String entity = WRONG_XML_SCHEMA_DATA;
254 int statusCode = submitRequest(method, url, mediaType, entity);
256 // Check the status code of the response: does it match
257 // the expected response(s)?
258 if(logger.isDebugEnabled()){
259 logger.debug(testName + ": url=" + url +
260 " status=" + statusCode);
262 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
263 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
264 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
268 // ---------------------------------------------------------------
269 // CRUD tests : READ tests
270 // ---------------------------------------------------------------
273 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
274 dependsOnMethods = {"create"})
275 public void read(String testName) throws Exception {
277 if (logger.isDebugEnabled()) {
278 logger.debug(testBanner(testName, CLASS_NAME));
283 // Submit the request to the service and store the response.
284 ContactClient client = new ContactClient();
285 ClientResponse<String> res = client.read(knownResourceId);
286 int statusCode = res.getStatus();
288 // Check the status code of the response: does it match
289 // the expected response(s)?
290 if(logger.isDebugEnabled()){
291 logger.debug(testName + ": status = " + statusCode);
293 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
294 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
295 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
297 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
298 ContactsCommon contact = (ContactsCommon) extractPart(input,
299 client.getCommonPartName(), ContactsCommon.class);
300 Assert.assertNotNull(contact);
305 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
306 dependsOnMethods = {"read"})
307 public void readNonExistent(String testName) throws Exception {
309 if (logger.isDebugEnabled()) {
310 logger.debug(testBanner(testName, CLASS_NAME));
313 setupReadNonExistent();
315 // Submit the request to the service and store the response.
316 ContactClient client = new ContactClient();
317 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
318 int statusCode = res.getStatus();
320 // Check the status code of the response: does it match
321 // the expected response(s)?
322 if(logger.isDebugEnabled()){
323 logger.debug(testName + ": status = " + statusCode);
325 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
326 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
327 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
330 // ---------------------------------------------------------------
331 // CRUD tests : READ_LIST tests
332 // ---------------------------------------------------------------
335 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
336 dependsOnMethods = {"read"})
337 public void readList(String testName) throws Exception {
339 if (logger.isDebugEnabled()) {
340 logger.debug(testBanner(testName, CLASS_NAME));
345 // Submit the request to the service and store the response.
346 ContactClient client = new ContactClient();
347 ClientResponse<ContactsCommonList> res = client.readList();
348 ContactsCommonList list = res.getEntity();
349 int statusCode = res.getStatus();
351 // Check the status code of the response: does it match
352 // the expected response(s)?
353 if(logger.isDebugEnabled()){
354 logger.debug(testName + ": status = " + statusCode);
356 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
357 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
358 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
360 // Optionally output additional data about list members for debugging.
361 boolean iterateThroughList = false;
362 if(iterateThroughList && logger.isDebugEnabled()){
363 List<ContactsCommonList.ContactListItem> items =
364 list.getContactListItem();
366 for(ContactsCommonList.ContactListItem item : items){
367 logger.debug(testName + ": list-item[" + i + "] csid=" +
369 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
370 item.getAddressPlace());
371 logger.debug(testName + ": list-item[" + i + "] URI=" +
381 // ---------------------------------------------------------------
382 // CRUD tests : UPDATE tests
383 // ---------------------------------------------------------------
386 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
387 dependsOnMethods = {"read"})
388 public void update(String testName) throws Exception {
390 if (logger.isDebugEnabled()) {
391 logger.debug(testBanner(testName, CLASS_NAME));
396 // Submit the request to the service and store the response.
397 ContactClient client = new ContactClient();
398 ClientResponse<String> res = client.read(knownResourceId);
399 if(logger.isDebugEnabled()){
400 logger.debug(testName + ": read status = " + res.getStatus());
402 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
404 if(logger.isDebugEnabled()){
405 logger.debug("got object to update with ID: " + knownResourceId);
407 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
408 ContactsCommon contact = (ContactsCommon) extractPart(input,
409 client.getCommonPartName(), ContactsCommon.class);
410 Assert.assertNotNull(contact);
412 // Update the content of this resource.
413 contact.setAddressType("updated-" + contact.getAddressType());
414 contact.setAddressPlace("updated-" + contact.getAddressPlace());
415 contact.setEmail("updated-" + contact.getEmail());
416 if(logger.isDebugEnabled()){
417 logger.debug("to be updated object");
418 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
420 // Submit the request to the service and store the response.
421 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
422 PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
423 commonPart.setLabel(client.getCommonPartName());
425 res = client.update(knownResourceId, output);
426 int statusCode = res.getStatus();
427 // Check the status code of the response: does it match the expected response(s)?
428 if(logger.isDebugEnabled()){
429 logger.debug(testName + ": status = " + statusCode);
431 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
432 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
433 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
436 input = new PoxPayloadIn(res.getEntity());
437 ContactsCommon updatedContact =
438 (ContactsCommon) extractPart(input,
439 client.getCommonPartName(), ContactsCommon.class);
440 Assert.assertNotNull(updatedContact);
442 Assert.assertEquals(updatedContact.getEmail(),
444 "Data in updated object did not match submitted data.");
449 // Placeholders until the three tests below can be uncommented.
450 // See Issue CSPACE-401.
452 public void updateWithEmptyEntityBody(String testName) throws Exception {
453 //Should this really be empty??
457 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
460 public void updateWithMalformedXml(String testName) throws Exception {
461 //Should this really be empty??
465 public void updateWithWrongXmlSchema(String testName) throws Exception {
466 //Should this really be empty??
471 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
472 dependsOnMethods = {"create", "update", "testSubmitRequest"})
473 public void updateWithEmptyEntityBody(String testName) throws Exception {
475 if (logger.isDebugEnabled()) {
476 logger.debug(testBanner(testName, CLASS_NAME));
479 setupUpdateWithEmptyEntityBody();
481 // Submit the request to the service and store the response.
482 String method = REQUEST_TYPE.httpMethodName();
483 String url = getResourceURL(knownResourceId);
484 String mediaType = MediaType.APPLICATION_XML;
485 final String entity = "";
486 int statusCode = submitRequest(method, url, mediaType, entity);
488 // Check the status code of the response: does it match
489 // the expected response(s)?
490 if(logger.isDebugEnabled()){
491 logger.debug(testName + ": url=" + url +
492 " status=" + statusCode);
494 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
495 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
496 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
500 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
501 dependsOnMethods = {"create", "update", "testSubmitRequest"})
502 public void updateWithMalformedXml(String testName) throws Exception {
504 if (logger.isDebugEnabled()) {
505 logger.debug(testBanner(testName, CLASS_NAME));
508 setupUpdateWithMalformedXml();
510 // Submit the request to the service and store the response.
511 String method = REQUEST_TYPE.httpMethodName();
512 String url = getResourceURL(knownResourceId);
513 String mediaType = MediaType.APPLICATION_XML;
514 final String entity = MALFORMED_XML_DATA;
515 int statusCode = submitRequest(method, url, mediaType, entity);
517 // Check the status code of the response: does it match
518 // the expected response(s)?
519 if(logger.isDebugEnabled()){
520 logger.debug(testName + ": url=" + url +
521 " status=" + statusCode);
523 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
524 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
525 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
529 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
530 dependsOnMethods = {"create", "update", "testSubmitRequest"})
531 public void updateWithWrongXmlSchema(String testName) throws Exception {
533 if (logger.isDebugEnabled()) {
534 logger.debug(testBanner(testName, CLASS_NAME));
537 setupUpdateWithWrongXmlSchema(testName, logger);
539 // Submit the request to the service and store the response.
540 String method = REQUEST_TYPE.httpMethodName();
541 String url = getResourceURL(knownResourceId);
542 String mediaType = MediaType.APPLICATION_XML;
543 final String entity = WRONG_XML_SCHEMA_DATA;
544 int statusCode = submitRequest(method, url, mediaType, entity);
546 // Check the status code of the response: does it match
547 // the expected response(s)?
548 if(logger.isDebugEnabled()){
549 logger.debug(testName + ": url=" + url +
550 " status=" + statusCode);
552 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
553 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
554 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
559 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
560 dependsOnMethods = {"update", "testSubmitRequest"})
561 public void updateNonExistent(String testName) throws Exception {
563 if (logger.isDebugEnabled()) {
564 logger.debug(testBanner(testName, CLASS_NAME));
567 setupUpdateNonExistent();
569 // Submit the request to the service and store the response.
570 // Note: The ID used in this 'create' call may be arbitrary.
571 // The only relevant ID may be the one used in update(), below.
572 ContactClient client = new ContactClient();
573 PoxPayloadOut multipart =
574 ContactClientUtils.createContactInstance(NON_EXISTENT_ID, client.getCommonPartName());
575 ClientResponse<String> res =
576 client.update(NON_EXISTENT_ID, multipart);
577 int statusCode = res.getStatus();
579 // Check the status code of the response: does it match
580 // the expected response(s)?
581 if(logger.isDebugEnabled()){
582 logger.debug(testName + ": status = " + statusCode);
584 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
585 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
586 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
589 // ---------------------------------------------------------------
590 // CRUD tests : DELETE tests
591 // ---------------------------------------------------------------
594 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
595 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
596 public void delete(String testName) throws Exception {
598 if (logger.isDebugEnabled()) {
599 logger.debug(testBanner(testName, CLASS_NAME));
604 // Submit the request to the service and store the response.
605 ContactClient client = new ContactClient();
606 ClientResponse<Response> res = client.delete(knownResourceId);
607 int statusCode = res.getStatus();
609 // Check the status code of the response: does it match
610 // the expected response(s)?
611 if(logger.isDebugEnabled()){
612 logger.debug(testName + ": status = " + statusCode);
614 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
615 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
616 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
621 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
622 dependsOnMethods = {"delete"})
623 public void deleteNonExistent(String testName) throws Exception {
625 if (logger.isDebugEnabled()) {
626 logger.debug(testBanner(testName, CLASS_NAME));
629 setupDeleteNonExistent();
631 // Submit the request to the service and store the response.
632 ContactClient client = new ContactClient();
633 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
634 int statusCode = res.getStatus();
636 // Check the status code of the response: does it match
637 // the expected response(s)?
638 if(logger.isDebugEnabled()){
639 logger.debug(testName + ": status = " + statusCode);
641 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
642 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
643 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
646 // ---------------------------------------------------------------
647 // Utility tests : tests of code used in tests above
648 // ---------------------------------------------------------------
650 * Tests the code for manually submitting data that is used by several
651 * of the methods above.
653 @Test(dependsOnMethods = {"create", "read"})
654 public void testSubmitRequest() {
656 // Expected status code: 200 OK
657 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
659 // Submit the request to the service and store the response.
660 String method = ServiceRequestType.READ.httpMethodName();
661 String url = getResourceURL(knownResourceId);
662 int statusCode = submitRequest(method, url);
664 // Check the status code of the response: does it match
665 // the expected response(s)?
666 if(logger.isDebugEnabled()){
667 logger.debug("testSubmitRequest: url=" + url +
668 " status=" + statusCode);
670 Assert.assertEquals(statusCode, EXPECTED_STATUS);