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.common.AbstractCommonListUtils;
36 import org.collectionspace.services.contact.AddressGroup;
37 import org.collectionspace.services.contact.AddressGroupList;
38 import org.collectionspace.services.contact.ContactsCommon;
39 import org.collectionspace.services.contact.EmailGroup;
40 import org.collectionspace.services.contact.EmailGroupList;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
43 import org.jboss.resteasy.client.ClientResponse;
45 import org.testng.Assert;
46 import org.testng.annotations.Test;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * ContactServiceTest, carries out tests against a
53 * deployed and running Contact Service.
55 * $LastChangedRevision: 917 $
56 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
58 public class ContactServiceTest extends AbstractServiceTestImpl {
60 private final String CLASS_NAME = ContactServiceTest.class.getName();
61 private final Logger logger = LoggerFactory.getLogger(ContactServiceTest.class);
62 // Instance variables specific to this test.
63 // final String SERVICE_PATH_COMPONENT = "contacts";
64 private String knownResourceId = null;
67 public String getServicePathComponent() {
68 return ContactClient.SERVICE_PATH_COMPONENT;
72 protected String getServiceName() {
73 return ContactClient.SERVICE_NAME;
77 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
80 protected CollectionSpaceClient getClientInstance() {
81 return new ContactClient();
85 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
88 protected AbstractCommonList getAbstractCommonList(
89 ClientResponse<AbstractCommonList> response) {
90 return response.getEntity(AbstractCommonList.class);
94 // protected PoxPayloadOut createInstance(String identifier) {
95 // ContactClient client = new ContactClient();
96 // PoxPayloadOut multipart =
97 // 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);
267 // ---------------------------------------------------------------
268 // CRUD tests : READ tests
269 // ---------------------------------------------------------------
272 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
273 dependsOnMethods = {"create"})
274 public void read(String testName) throws Exception {
276 if (logger.isDebugEnabled()) {
277 logger.debug(testBanner(testName, CLASS_NAME));
282 // Submit the request to the service and store the response.
283 ContactClient client = new ContactClient();
284 ClientResponse<String> res = client.read(knownResourceId);
285 assertStatusCode(res, testName);
287 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
288 ContactsCommon contact = (ContactsCommon) extractPart(input,
289 client.getCommonPartName(), ContactsCommon.class);
290 Assert.assertNotNull(contact);
295 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
296 dependsOnMethods = {"read"})
297 public void readNonExistent(String testName) throws Exception {
299 if (logger.isDebugEnabled()) {
300 logger.debug(testBanner(testName, CLASS_NAME));
303 setupReadNonExistent();
305 // Submit the request to the service and store the response.
306 ContactClient client = new ContactClient();
307 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
308 int statusCode = res.getStatus();
310 // Check the status code of the response: does it match
311 // the expected response(s)?
312 if (logger.isDebugEnabled()) {
313 logger.debug(testName + ": status = " + statusCode);
315 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
316 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
317 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
320 // ---------------------------------------------------------------
321 // CRUD tests : READ_LIST tests
322 // ---------------------------------------------------------------
325 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
326 dependsOnMethods = {"read"})
327 public void readList(String testName) throws Exception {
329 if (logger.isDebugEnabled()) {
330 logger.debug(testBanner(testName, CLASS_NAME));
335 // Submit the request to the service and store the response.
336 ContactClient client = new ContactClient();
337 ClientResponse<AbstractCommonList> res = client.readList();
338 assertStatusCode(res, testName);
339 AbstractCommonList list = res.getEntity();
341 // Optionally output additional data about list members for debugging.
342 boolean iterateThroughList = false;
343 if (iterateThroughList && logger.isDebugEnabled()) {
344 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
351 // ---------------------------------------------------------------
352 // CRUD tests : UPDATE tests
353 // ---------------------------------------------------------------
356 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
357 dependsOnMethods = {"read"})
358 public void update(String testName) throws Exception {
360 if (logger.isDebugEnabled()) {
361 logger.debug(testBanner(testName, CLASS_NAME));
366 // Submit the request to the service and store the response.
367 ContactClient client = new ContactClient();
368 ClientResponse<String> res = client.read(knownResourceId);
369 assertStatusCode(res, testName);
371 if (logger.isDebugEnabled()) {
372 logger.debug("got object to update with ID: " + knownResourceId);
374 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
375 ContactsCommon contact = (ContactsCommon) extractPart(input,
376 client.getCommonPartName(), ContactsCommon.class);
377 Assert.assertNotNull(contact);
379 if(logger.isDebugEnabled()){
380 logger.debug("contact common before updating");
381 logger.debug(BaseServiceTest.objectAsXmlString(contact, ContactsCommon.class));
384 // Verify the contents of this resource
385 EmailGroupList emailGroupList = contact.getEmailGroupList();
386 Assert.assertNotNull(emailGroupList);
387 List<EmailGroup> emailGroups = emailGroupList.getEmailGroup();
388 Assert.assertNotNull(emailGroups);
389 Assert.assertTrue(emailGroups.size() > 0);
390 String email = emailGroups.get(0).getEmail();
391 Assert.assertNotNull(email);
393 AddressGroupList addressGroupList = contact.getAddressGroupList();
394 Assert.assertNotNull(addressGroupList);
395 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
396 Assert.assertNotNull(addressGroups);
397 Assert.assertTrue(addressGroups.size() > 0);
398 String addressType = addressGroups.get(0).getAddressType();
399 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
400 Assert.assertNotNull(addressType);
401 Assert.assertNotNull(addressPlace1);
403 // Update the contents of this resource.
404 emailGroups.get(0).setEmail("updated-" + email);
405 contact.setEmailGroupList(emailGroupList);
407 addressGroups.get(0).setAddressType("updated-" + addressType);
408 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
409 contact.setAddressGroupList(addressGroupList);
411 if (logger.isDebugEnabled()) {
412 logger.debug("to be updated object");
413 logger.debug(BaseServiceTest.objectAsXmlString(contact, ContactsCommon.class));
416 // Submit the request to the service and store the response.
417 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
418 PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
419 commonPart.setLabel(client.getCommonPartName());
421 res = client.update(knownResourceId, output);
422 assertStatusCode(res, testName);
424 input = new PoxPayloadIn(res.getEntity());
425 ContactsCommon updatedContact =
426 (ContactsCommon) extractPart(input,
427 client.getCommonPartName(), ContactsCommon.class);
428 Assert.assertNotNull(updatedContact);
430 if (logger.isDebugEnabled()) {
431 logger.debug("object after update");
432 logger.debug(objectAsXmlString(updatedContact, ContactsCommon.class));
435 Assert.assertNotNull(updatedContact.getEmailGroupList().getEmailGroup().get(0));
436 Assert.assertEquals(updatedContact.getEmailGroupList().getEmailGroup().get(0).getEmail(),
437 contact.getEmailGroupList().getEmailGroup().get(0).getEmail(),
438 "Data in updated object did not match submitted data.");
443 // Placeholders until the three tests below can be uncommented.
444 // See Issue CSPACE-401.
446 public void updateWithEmptyEntityBody(String testName) throws Exception {
447 //Should this really be empty??
451 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
454 public void updateWithMalformedXml(String testName) throws Exception {
455 //Should this really be empty??
459 public void updateWithWrongXmlSchema(String testName) throws Exception {
460 //Should this really be empty??
465 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
466 dependsOnMethods = {"create", "update", "testSubmitRequest"})
467 public void updateWithEmptyEntityBody(String testName) throws Exception {
469 if (logger.isDebugEnabled()) {
470 logger.debug(testBanner(testName, CLASS_NAME));
473 setupUpdateWithEmptyEntityBody();
475 // Submit the request to the service and store the response.
476 String method = REQUEST_TYPE.httpMethodName();
477 String url = getResourceURL(knownResourceId);
478 String mediaType = MediaType.APPLICATION_XML;
479 final String entity = "";
480 int statusCode = submitRequest(method, url, mediaType, entity);
482 // Check the status code of the response: does it match
483 // the expected response(s)?
484 if(logger.isDebugEnabled()){
485 logger.debug(testName + ": url=" + url +
486 " status=" + statusCode);
488 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
489 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
490 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
494 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
495 dependsOnMethods = {"create", "update", "testSubmitRequest"})
496 public void updateWithMalformedXml(String testName) throws Exception {
498 if (logger.isDebugEnabled()) {
499 logger.debug(testBanner(testName, CLASS_NAME));
502 setupUpdateWithMalformedXml();
504 // Submit the request to the service and store the response.
505 String method = REQUEST_TYPE.httpMethodName();
506 String url = getResourceURL(knownResourceId);
507 String mediaType = MediaType.APPLICATION_XML;
508 final String entity = MALFORMED_XML_DATA;
509 int statusCode = submitRequest(method, url, mediaType, entity);
511 // Check the status code of the response: does it match
512 // the expected response(s)?
513 if(logger.isDebugEnabled()){
514 logger.debug(testName + ": url=" + url +
515 " status=" + statusCode);
517 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
518 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
519 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
523 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
524 dependsOnMethods = {"create", "update", "testSubmitRequest"})
525 public void updateWithWrongXmlSchema(String testName) throws Exception {
527 if (logger.isDebugEnabled()) {
528 logger.debug(testBanner(testName, CLASS_NAME));
531 setupUpdateWithWrongXmlSchema(testName, logger);
533 // Submit the request to the service and store the response.
534 String method = REQUEST_TYPE.httpMethodName();
535 String url = getResourceURL(knownResourceId);
536 String mediaType = MediaType.APPLICATION_XML;
537 final String entity = WRONG_XML_SCHEMA_DATA;
538 int statusCode = submitRequest(method, url, mediaType, entity);
540 // Check the status code of the response: does it match
541 // the expected response(s)?
542 if(logger.isDebugEnabled()){
543 logger.debug(testName + ": url=" + url +
544 " status=" + statusCode);
546 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
547 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
548 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
552 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
553 dependsOnMethods = {"update", "testSubmitRequest"})
554 public void updateNonExistent(String testName) throws Exception {
556 if (logger.isDebugEnabled()) {
557 logger.debug(testBanner(testName, CLASS_NAME));
560 setupUpdateNonExistent();
562 // Submit the request to the service and store the response.
563 // Note: The ID used in this 'create' call may be arbitrary.
564 // The only relevant ID may be the one used in update(), below.
565 ContactClient client = new ContactClient();
566 PoxPayloadOut multipart =
567 ContactClientUtils.createContactInstance(NON_EXISTENT_ID, client.getCommonPartName());
568 ClientResponse<String> res =
569 client.update(NON_EXISTENT_ID, multipart);
570 int statusCode = res.getStatus();
572 // Check the status code of the response: does it match
573 // the expected response(s)?
574 if (logger.isDebugEnabled()) {
575 logger.debug(testName + ": status = " + statusCode);
577 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
578 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
579 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
582 // ---------------------------------------------------------------
583 // CRUD tests : DELETE tests
584 // ---------------------------------------------------------------
587 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
588 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
589 public void delete(String testName) throws Exception {
591 if (logger.isDebugEnabled()) {
592 logger.debug(testBanner(testName, CLASS_NAME));
597 // Submit the request to the service and store the response.
598 ContactClient client = new ContactClient();
599 ClientResponse<Response> res = client.delete(knownResourceId);
600 int statusCode = res.getStatus();
602 // Check the status code of the response: does it match
603 // the expected response(s)?
604 if (logger.isDebugEnabled()) {
605 logger.debug(testName + ": status = " + statusCode);
607 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
608 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
609 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
614 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
615 dependsOnMethods = {"delete"})
616 public void deleteNonExistent(String testName) throws Exception {
618 if (logger.isDebugEnabled()) {
619 logger.debug(testBanner(testName, CLASS_NAME));
622 setupDeleteNonExistent();
624 // Submit the request to the service and store the response.
625 ContactClient client = new ContactClient();
626 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
627 int statusCode = res.getStatus();
629 // Check the status code of the response: does it match
630 // the expected response(s)?
631 if (logger.isDebugEnabled()) {
632 logger.debug(testName + ": status = " + statusCode);
634 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
635 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
636 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
639 // ---------------------------------------------------------------
640 // Utility tests : tests of code used in tests above
641 // ---------------------------------------------------------------
643 * Tests the code for manually submitting data that is used by several
644 * of the methods above.
646 @Test(dependsOnMethods = {"create", "read"})
647 public void testSubmitRequest() {
649 // Expected status code: 200 OK
650 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
652 // Submit the request to the service and store the response.
653 String method = ServiceRequestType.READ.httpMethodName();
654 String url = getResourceURL(knownResourceId);
655 int statusCode = submitRequest(method, url);
657 // Check the status code of the response: does it match
658 // the expected response(s)?
659 if (logger.isDebugEnabled()) {
660 logger.debug("testSubmitRequest: url=" + url
661 + " status=" + statusCode);
663 Assert.assertEquals(statusCode, EXPECTED_STATUS);