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.AbstractCommonListUtils;
30 import org.collectionspace.services.client.CollectionSpaceClient;
31 import org.collectionspace.services.client.ContactClient;
32 import org.collectionspace.services.client.ContactClientUtils;
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.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;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * ContactServiceTest, carries out tests against a
52 * deployed and running Contact Service.
54 * $LastChangedRevision: 917 $
55 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
57 public class ContactServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, ContactsCommon> {
59 private final String CLASS_NAME = ContactServiceTest.class.getName();
60 private final Logger logger = LoggerFactory.getLogger(ContactServiceTest.class);
61 // Instance variables specific to this test.
62 // final String SERVICE_PATH_COMPONENT = "contacts";
65 public String getServicePathComponent() {
66 return ContactClient.SERVICE_PATH_COMPONENT;
70 protected String getServiceName() {
71 return ContactClient.SERVICE_NAME;
75 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
78 protected CollectionSpaceClient getClientInstance() {
79 return new ContactClient();
83 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
86 protected AbstractCommonList getCommonList(Response response) {
87 return response.readEntity(AbstractCommonList.class);
91 // protected PoxPayloadOut createInstance(String identifier) {
92 // ContactClient client = new ContactClient();
93 // PoxPayloadOut multipart =
94 // ContactClientUtils.createContactInstance(identifier, client.getCommonPartName());
97 // ---------------------------------------------------------------
98 // CRUD tests : CREATE tests
99 // ---------------------------------------------------------------
103 // @Test(dataProvider = "testName", dataProviderClass = 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 ContactClient client = new ContactClient();
112 String identifier = createIdentifier();
113 PoxPayloadOut multipart =
114 ContactClientUtils.createContactInstance(identifier, client.getCommonPartName());
115 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);
132 newId = extractId(res);
137 // Store the ID returned from the first resource created
138 // for additional tests below.
139 if (knownResourceId == null) {
140 knownResourceId = newId;
141 if (logger.isDebugEnabled()) {
142 logger.debug(testName + ": knownResourceId=" + knownResourceId);
146 // Store the IDs from every resource created by tests,
147 // so they can be deleted after tests have been run.
148 allResourceIdsCreated.add(newId);
152 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
153 // dependsOnMethods = {"create"})
154 public void createList(String testName) throws Exception {
155 for (int i = 0; i < 3; i++) {
161 // Placeholders until the three tests below can be uncommented.
162 // See Issue CSPACE-401.
164 public void createWithEmptyEntityBody(String testName) throws Exception {
165 //Should this really be empty?
169 public void createWithMalformedXml(String testName) throws Exception {
170 //Should this really be empty??
174 public void createWithWrongXmlSchema(String testName) throws Exception {
175 //Should this really be empty??
180 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
181 dependsOnMethods = {"create", "testSubmitRequest"})
182 public void createWithEmptyEntityBody(String testName) throws Exception {
184 if (logger.isDebugEnabled()) {
185 logger.debug(testBanner(testName, CLASS_NAME));
188 setupCreateWithEmptyEntityBody(testName, logger);
190 // Submit the request to the service and store the response.
191 String method = REQUEST_TYPE.httpMethodName();
192 String url = getServiceRootURL();
193 String mediaType = MediaType.APPLICATION_XML;
194 final String entity = "";
195 int statusCode = submitRequest(method, url, mediaType, entity);
197 // Check the status code of the response: does it match
198 // the expected response(s)?
199 if(logger.isDebugEnabled()){
200 logger.debug("createWithEmptyEntityBody url=" + url +
201 " status=" + statusCode);
203 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
204 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
205 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
209 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
210 dependsOnMethods = {"create", "testSubmitRequest"})
211 public void createWithMalformedXml(String testName) throws Exception {
213 if (logger.isDebugEnabled()) {
214 logger.debug(testBanner(testName, CLASS_NAME));
217 setupCreateWithMalformedXml();
219 // Submit the request to the service and store the response.
220 String method = REQUEST_TYPE.httpMethodName();
221 String url = getServiceRootURL();
222 String mediaType = MediaType.APPLICATION_XML;
223 final String entity = MALFORMED_XML_DATA; // Constant from base class.
224 int statusCode = submitRequest(method, url, mediaType, entity);
226 // Check the status code of the response: does it match
227 // the expected response(s)?
228 if(logger.isDebugEnabled()){
229 logger.debug(testName + ": url=" + url +
230 " status=" + statusCode);
232 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
233 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
234 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
238 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
239 dependsOnMethods = {"create", "testSubmitRequest"})
240 public void createWithWrongXmlSchema(String testName) throws Exception {
242 if (logger.isDebugEnabled()) {
243 logger.debug(testBanner(testName, CLASS_NAME));
246 setupCreateWithWrongXmlSchema(testName, logger);
248 // Submit the request to the service and store the response.
249 String method = REQUEST_TYPE.httpMethodName();
250 String url = getServiceRootURL();
251 String mediaType = MediaType.APPLICATION_XML;
252 final String entity = WRONG_XML_SCHEMA_DATA;
253 int statusCode = submitRequest(method, url, mediaType, entity);
255 // Check the status code of the response: does it match
256 // the expected response(s)?
257 if(logger.isDebugEnabled()){
258 logger.debug(testName + ": url=" + url +
259 " status=" + statusCode);
261 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
262 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
263 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
266 // ---------------------------------------------------------------
267 // CRUD tests : READ tests
268 // ---------------------------------------------------------------
271 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
272 // dependsOnMethods = {"create"})
273 public void read(String testName) throws Exception {
277 // Submit the request to the service and store the response.
278 ContactClient client = new ContactClient();
279 Response res = client.read(knownResourceId);
281 assertStatusCode(res, testName);
282 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
283 ContactsCommon contact = (ContactsCommon) extractPart(input,
284 client.getCommonPartName(), ContactsCommon.class);
285 Assert.assertNotNull(contact);
293 // ---------------------------------------------------------------
294 // CRUD tests : READ_LIST tests
295 // ---------------------------------------------------------------
298 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
299 // dependsOnMethods = {"read"})
300 public void readList(String testName) throws Exception {
304 // Submit the request to the service and store the response.
305 ContactClient client = new ContactClient();
306 Response res = client.readList();
308 assertStatusCode(res, testName);
309 AbstractCommonList list = res.readEntity(getCommonListType());
311 // Optionally output additional data about list members for debugging.
312 boolean iterateThroughList = false;
313 if (iterateThroughList && logger.isDebugEnabled()) {
314 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
325 // ---------------------------------------------------------------
326 // CRUD tests : UPDATE tests
327 // ---------------------------------------------------------------
330 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
331 // dependsOnMethods = {"read"})
332 public void update(String testName) throws Exception {
336 // First read the object that will be updated
338 ContactClient client = new ContactClient();
339 Response res = client.read(knownResourceId);
340 ContactsCommon contact = null;
342 assertStatusCode(res, testName);
344 if (logger.isDebugEnabled()) {
345 logger.debug("got object to update with ID: " + knownResourceId);
347 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
348 contact = (ContactsCommon) extractPart(input,
349 client.getCommonPartName(), ContactsCommon.class);
350 Assert.assertNotNull(contact);
357 if(logger.isDebugEnabled()){
358 logger.debug("contact common before updating");
359 logger.debug(BaseServiceTest.objectAsXmlString(contact, ContactsCommon.class));
362 // Verify the contents of this resource
363 EmailGroupList emailGroupList = contact.getEmailGroupList();
364 Assert.assertNotNull(emailGroupList);
365 List<EmailGroup> emailGroups = emailGroupList.getEmailGroup();
366 Assert.assertNotNull(emailGroups);
367 Assert.assertTrue(emailGroups.size() > 0);
368 String email = emailGroups.get(0).getEmail();
369 Assert.assertNotNull(email);
371 AddressGroupList addressGroupList = contact.getAddressGroupList();
372 Assert.assertNotNull(addressGroupList);
373 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
374 Assert.assertNotNull(addressGroups);
375 Assert.assertTrue(addressGroups.size() > 0);
376 String addressType = addressGroups.get(0).getAddressType();
377 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
378 Assert.assertNotNull(addressType);
379 Assert.assertNotNull(addressPlace1);
381 // Update the contents of this resource.
382 emailGroups.get(0).setEmail("updated-" + email);
383 contact.setEmailGroupList(emailGroupList);
385 addressGroups.get(0).setAddressType("updated-" + addressType);
386 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
387 contact.setAddressGroupList(addressGroupList);
389 if (logger.isDebugEnabled()) {
390 logger.debug("to be updated object");
391 logger.debug(BaseServiceTest.objectAsXmlString(contact, ContactsCommon.class));
394 // Next, send the update to the server
396 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
397 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), contact);
399 res = client.update(knownResourceId, output);
400 ContactsCommon updatedContact = null;
402 assertStatusCode(res, testName);
403 PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
404 updatedContact = (ContactsCommon) extractPart(input,
405 client.getCommonPartName(), ContactsCommon.class);
406 Assert.assertNotNull(updatedContact);
413 if (logger.isDebugEnabled()) {
414 logger.debug("object after update");
415 logger.debug(objectAsXmlString(updatedContact, ContactsCommon.class));
418 Assert.assertNotNull(updatedContact.getEmailGroupList().getEmailGroup().get(0));
419 Assert.assertEquals(updatedContact.getEmailGroupList().getEmailGroup().get(0).getEmail(),
420 contact.getEmailGroupList().getEmailGroup().get(0).getEmail(),
421 "Data in updated object did not match submitted data.");
426 // Placeholders until the three tests below can be uncommented.
427 // See Issue CSPACE-401.
429 public void updateWithEmptyEntityBody(String testName) throws Exception {
430 //Should this really be empty??
434 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
437 public void updateWithMalformedXml(String testName) throws Exception {
438 //Should this really be empty??
442 public void updateWithWrongXmlSchema(String testName) throws Exception {
443 //Should this really be empty??
448 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
449 dependsOnMethods = {"create", "update", "testSubmitRequest"})
450 public void updateWithEmptyEntityBody(String testName) throws Exception {
452 if (logger.isDebugEnabled()) {
453 logger.debug(testBanner(testName, CLASS_NAME));
456 setupUpdateWithEmptyEntityBody();
458 // Submit the request to the service and store the response.
459 String method = REQUEST_TYPE.httpMethodName();
460 String url = getResourceURL(knownResourceId);
461 String mediaType = MediaType.APPLICATION_XML;
462 final String entity = "";
463 int statusCode = submitRequest(method, url, mediaType, entity);
465 // Check the status code of the response: does it match
466 // the expected response(s)?
467 if(logger.isDebugEnabled()){
468 logger.debug(testName + ": url=" + url +
469 " status=" + statusCode);
471 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
472 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
473 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
477 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
478 dependsOnMethods = {"create", "update", "testSubmitRequest"})
479 public void updateWithMalformedXml(String testName) throws Exception {
481 if (logger.isDebugEnabled()) {
482 logger.debug(testBanner(testName, CLASS_NAME));
485 setupUpdateWithMalformedXml();
487 // Submit the request to the service and store the response.
488 String method = REQUEST_TYPE.httpMethodName();
489 String url = getResourceURL(knownResourceId);
490 String mediaType = MediaType.APPLICATION_XML;
491 final String entity = MALFORMED_XML_DATA;
492 int statusCode = submitRequest(method, url, mediaType, entity);
494 // Check the status code of the response: does it match
495 // the expected response(s)?
496 if(logger.isDebugEnabled()){
497 logger.debug(testName + ": url=" + url +
498 " status=" + statusCode);
500 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
501 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
502 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
506 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
507 dependsOnMethods = {"create", "update", "testSubmitRequest"})
508 public void updateWithWrongXmlSchema(String testName) throws Exception {
510 if (logger.isDebugEnabled()) {
511 logger.debug(testBanner(testName, CLASS_NAME));
514 setupUpdateWithWrongXmlSchema(testName, logger);
516 // Submit the request to the service and store the response.
517 String method = REQUEST_TYPE.httpMethodName();
518 String url = getResourceURL(knownResourceId);
519 String mediaType = MediaType.APPLICATION_XML;
520 final String entity = WRONG_XML_SCHEMA_DATA;
521 int statusCode = submitRequest(method, url, mediaType, entity);
523 // Check the status code of the response: does it match
524 // the expected response(s)?
525 if(logger.isDebugEnabled()){
526 logger.debug(testName + ": url=" + url +
527 " status=" + statusCode);
529 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
530 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
531 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
535 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
536 // dependsOnMethods = {"update", "testSubmitRequest"})
537 public void updateNonExistent(String testName) throws Exception {
539 setupUpdateNonExistent();
541 // Submit the request to the service and store the response.
542 // Note: The ID used in this 'create' call may be arbitrary.
543 // The only relevant ID may be the one used in update(), below.
544 ContactClient client = new ContactClient();
545 PoxPayloadOut multipart =
546 ContactClientUtils.createContactInstance(NON_EXISTENT_ID, client.getCommonPartName());
548 client.update(NON_EXISTENT_ID, multipart);
550 int statusCode = res.getStatus();
552 // Check the status code of the response: does it match
553 // the expected response(s)?
554 if (logger.isDebugEnabled()) {
555 logger.debug(testName + ": status = " + statusCode);
557 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
558 invalidStatusCodeMessage(testRequestType, statusCode));
559 Assert.assertEquals(statusCode, testExpectedStatusCode);
565 // ---------------------------------------------------------------
566 // CRUD tests : DELETE tests
567 // ---------------------------------------------------------------
570 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
571 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
572 public void delete(String testName) throws Exception {
576 // Submit the request to the service and store the response.
577 ContactClient client = new ContactClient();
578 Response res = client.delete(knownResourceId);
580 int statusCode = res.getStatus();
582 // Check the status code of the response: does it match
583 // the expected response(s)?
584 if (logger.isDebugEnabled()) {
585 logger.debug(testName + ": status = " + statusCode);
587 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
588 invalidStatusCodeMessage(testRequestType, statusCode));
589 Assert.assertEquals(statusCode, testExpectedStatusCode);
597 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
598 // dependsOnMethods = {"delete"})
599 public void deleteNonExistent(String testName) throws Exception {
601 setupDeleteNonExistent();
603 // Submit the request to the service and store the response.
604 ContactClient client = new ContactClient();
605 Response res = client.delete(NON_EXISTENT_ID);
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(testRequestType.isValidStatusCode(statusCode),
615 invalidStatusCodeMessage(testRequestType, statusCode));
616 Assert.assertEquals(statusCode, testExpectedStatusCode);
622 // ---------------------------------------------------------------
623 // Utility tests : tests of code used in tests above
624 // ---------------------------------------------------------------
626 * Tests the code for manually submitting data that is used by several
627 * of the methods above.
629 // @Test(dependsOnMethods = {"create", "read"})
630 public void testSubmitRequest() {
632 // Expected status code: 200 OK
633 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
635 // Submit the request to the service and store the response.
636 String method = ServiceRequestType.READ.httpMethodName();
637 String url = getResourceURL(knownResourceId);
638 int statusCode = submitRequest(method, url);
640 // Check the status code of the response: does it match
641 // the expected response(s)?
642 if (logger.isDebugEnabled()) {
643 logger.debug("testSubmitRequest: url=" + url
644 + " status=" + statusCode);
646 Assert.assertEquals(statusCode, EXPECTED_STATUS);
650 public void readWorkflow(String testName) throws Exception {
651 // Not applicable for the Contact service
655 public void searchWorkflowDeleted(String testName) throws Exception {
656 // Not applicable for the Contact service
660 protected PoxPayloadOut createInstance(String commonPartName,
662 return ContactClientUtils.createContactInstance(identifier, commonPartName);
666 protected ContactsCommon updateInstance(ContactsCommon commonPartObject) {
667 // TODO Auto-generated method stub
672 protected void compareUpdatedInstances(ContactsCommon original,
673 ContactsCommon updated) throws Exception {
674 // TODO Auto-generated method stub
679 public void CRUDTests(String testName) {
680 // TODO Auto-generated method stub