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(
87 ClientResponse<AbstractCommonList> response) {
88 return response.getEntity(AbstractCommonList.class);
92 // protected PoxPayloadOut createInstance(String identifier) {
93 // ContactClient client = new ContactClient();
94 // PoxPayloadOut multipart =
95 // ContactClientUtils.createContactInstance(identifier, client.getCommonPartName());
98 // ---------------------------------------------------------------
99 // CRUD tests : CREATE tests
100 // ---------------------------------------------------------------
104 // @Test(dataProvider = "testName", dataProviderClass = 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 ContactClient client = new ContactClient();
113 String identifier = createIdentifier();
114 PoxPayloadOut multipart =
115 ContactClientUtils.createContactInstance(identifier, client.getCommonPartName());
116 Response res = client.create(multipart);
119 int statusCode = res.getStatus();
121 // Check the status code of the response: does it match
122 // the expected response(s)?
125 // Does it fall within the set of valid status codes?
126 // Does it exactly match the expected status code?
127 if (logger.isDebugEnabled()) {
128 logger.debug(testName + ": status = " + statusCode);
130 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
131 invalidStatusCodeMessage(testRequestType, statusCode));
132 Assert.assertEquals(statusCode, testExpectedStatusCode);
133 newId = extractId(res);
138 // Store the ID returned from the first resource created
139 // for additional tests below.
140 if (knownResourceId == null) {
141 knownResourceId = newId;
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(newId);
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 {
278 // Submit the request to the service and store the response.
279 ContactClient client = new ContactClient();
280 ClientResponse<String> res = client.read(knownResourceId);
282 assertStatusCode(res, testName);
283 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
284 ContactsCommon contact = (ContactsCommon) extractPart(input,
285 client.getCommonPartName(), ContactsCommon.class);
286 Assert.assertNotNull(contact);
289 res.releaseConnection();
294 // ---------------------------------------------------------------
295 // CRUD tests : READ_LIST tests
296 // ---------------------------------------------------------------
299 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
300 // dependsOnMethods = {"read"})
301 public void readList(String testName) throws Exception {
305 // Submit the request to the service and store the response.
306 ContactClient client = new ContactClient();
307 ClientResponse<AbstractCommonList> res = client.readList();
309 assertStatusCode(res, testName);
310 AbstractCommonList list = res.getEntity();
312 // Optionally output additional data about list members for debugging.
313 boolean iterateThroughList = false;
314 if (iterateThroughList && logger.isDebugEnabled()) {
315 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
319 res.releaseConnection();
326 // ---------------------------------------------------------------
327 // CRUD tests : UPDATE tests
328 // ---------------------------------------------------------------
331 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
332 // dependsOnMethods = {"read"})
333 public void update(String testName) throws Exception {
337 // First read the object that will be updated
339 ContactClient client = new ContactClient();
340 ClientResponse<String> res = client.read(knownResourceId);
341 ContactsCommon contact = null;
343 assertStatusCode(res, testName);
345 if (logger.isDebugEnabled()) {
346 logger.debug("got object to update with ID: " + knownResourceId);
348 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
349 contact = (ContactsCommon) extractPart(input,
350 client.getCommonPartName(), ContactsCommon.class);
351 Assert.assertNotNull(contact);
354 res.releaseConnection();
358 if(logger.isDebugEnabled()){
359 logger.debug("contact common before updating");
360 logger.debug(BaseServiceTest.objectAsXmlString(contact, ContactsCommon.class));
363 // Verify the contents of this resource
364 EmailGroupList emailGroupList = contact.getEmailGroupList();
365 Assert.assertNotNull(emailGroupList);
366 List<EmailGroup> emailGroups = emailGroupList.getEmailGroup();
367 Assert.assertNotNull(emailGroups);
368 Assert.assertTrue(emailGroups.size() > 0);
369 String email = emailGroups.get(0).getEmail();
370 Assert.assertNotNull(email);
372 AddressGroupList addressGroupList = contact.getAddressGroupList();
373 Assert.assertNotNull(addressGroupList);
374 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
375 Assert.assertNotNull(addressGroups);
376 Assert.assertTrue(addressGroups.size() > 0);
377 String addressType = addressGroups.get(0).getAddressType();
378 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
379 Assert.assertNotNull(addressType);
380 Assert.assertNotNull(addressPlace1);
382 // Update the contents of this resource.
383 emailGroups.get(0).setEmail("updated-" + email);
384 contact.setEmailGroupList(emailGroupList);
386 addressGroups.get(0).setAddressType("updated-" + addressType);
387 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
388 contact.setAddressGroupList(addressGroupList);
390 if (logger.isDebugEnabled()) {
391 logger.debug("to be updated object");
392 logger.debug(BaseServiceTest.objectAsXmlString(contact, ContactsCommon.class));
395 // Next, send the update to the server
397 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
398 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), contact);
400 res = client.update(knownResourceId, output);
401 ContactsCommon updatedContact = null;
403 assertStatusCode(res, testName);
404 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
405 updatedContact = (ContactsCommon) extractPart(input,
406 client.getCommonPartName(), ContactsCommon.class);
407 Assert.assertNotNull(updatedContact);
410 res.releaseConnection();
414 if (logger.isDebugEnabled()) {
415 logger.debug("object after update");
416 logger.debug(objectAsXmlString(updatedContact, ContactsCommon.class));
419 Assert.assertNotNull(updatedContact.getEmailGroupList().getEmailGroup().get(0));
420 Assert.assertEquals(updatedContact.getEmailGroupList().getEmailGroup().get(0).getEmail(),
421 contact.getEmailGroupList().getEmailGroup().get(0).getEmail(),
422 "Data in updated object did not match submitted data.");
427 // Placeholders until the three tests below can be uncommented.
428 // See Issue CSPACE-401.
430 public void updateWithEmptyEntityBody(String testName) throws Exception {
431 //Should this really be empty??
435 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
438 public void updateWithMalformedXml(String testName) throws Exception {
439 //Should this really be empty??
443 public void updateWithWrongXmlSchema(String testName) throws Exception {
444 //Should this really be empty??
449 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
450 dependsOnMethods = {"create", "update", "testSubmitRequest"})
451 public void updateWithEmptyEntityBody(String testName) throws Exception {
453 if (logger.isDebugEnabled()) {
454 logger.debug(testBanner(testName, CLASS_NAME));
457 setupUpdateWithEmptyEntityBody();
459 // Submit the request to the service and store the response.
460 String method = REQUEST_TYPE.httpMethodName();
461 String url = getResourceURL(knownResourceId);
462 String mediaType = MediaType.APPLICATION_XML;
463 final String entity = "";
464 int statusCode = submitRequest(method, url, mediaType, entity);
466 // Check the status code of the response: does it match
467 // the expected response(s)?
468 if(logger.isDebugEnabled()){
469 logger.debug(testName + ": url=" + url +
470 " status=" + statusCode);
472 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
473 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
474 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
478 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
479 dependsOnMethods = {"create", "update", "testSubmitRequest"})
480 public void updateWithMalformedXml(String testName) throws Exception {
482 if (logger.isDebugEnabled()) {
483 logger.debug(testBanner(testName, CLASS_NAME));
486 setupUpdateWithMalformedXml();
488 // Submit the request to the service and store the response.
489 String method = REQUEST_TYPE.httpMethodName();
490 String url = getResourceURL(knownResourceId);
491 String mediaType = MediaType.APPLICATION_XML;
492 final String entity = MALFORMED_XML_DATA;
493 int statusCode = submitRequest(method, url, mediaType, entity);
495 // Check the status code of the response: does it match
496 // the expected response(s)?
497 if(logger.isDebugEnabled()){
498 logger.debug(testName + ": url=" + url +
499 " status=" + statusCode);
501 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
502 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
503 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
507 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
508 dependsOnMethods = {"create", "update", "testSubmitRequest"})
509 public void updateWithWrongXmlSchema(String testName) throws Exception {
511 if (logger.isDebugEnabled()) {
512 logger.debug(testBanner(testName, CLASS_NAME));
515 setupUpdateWithWrongXmlSchema(testName, logger);
517 // Submit the request to the service and store the response.
518 String method = REQUEST_TYPE.httpMethodName();
519 String url = getResourceURL(knownResourceId);
520 String mediaType = MediaType.APPLICATION_XML;
521 final String entity = WRONG_XML_SCHEMA_DATA;
522 int statusCode = submitRequest(method, url, mediaType, entity);
524 // Check the status code of the response: does it match
525 // the expected response(s)?
526 if(logger.isDebugEnabled()){
527 logger.debug(testName + ": url=" + url +
528 " status=" + statusCode);
530 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
531 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
532 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
536 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
537 // dependsOnMethods = {"update", "testSubmitRequest"})
538 public void updateNonExistent(String testName) throws Exception {
540 setupUpdateNonExistent();
542 // Submit the request to the service and store the response.
543 // Note: The ID used in this 'create' call may be arbitrary.
544 // The only relevant ID may be the one used in update(), below.
545 ContactClient client = new ContactClient();
546 PoxPayloadOut multipart =
547 ContactClientUtils.createContactInstance(NON_EXISTENT_ID, client.getCommonPartName());
548 ClientResponse<String> res =
549 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);
562 // ---------------------------------------------------------------
563 // CRUD tests : DELETE tests
564 // ---------------------------------------------------------------
567 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
568 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
569 public void delete(String testName) throws Exception {
573 // Submit the request to the service and store the response.
574 ContactClient client = new ContactClient();
575 Response res = client.delete(knownResourceId);
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(testRequestType.isValidStatusCode(statusCode),
585 invalidStatusCodeMessage(testRequestType, statusCode));
586 Assert.assertEquals(statusCode, testExpectedStatusCode);
594 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
595 // dependsOnMethods = {"delete"})
596 public void deleteNonExistent(String testName) throws Exception {
598 setupDeleteNonExistent();
600 // Submit the request to the service and store the response.
601 ContactClient client = new ContactClient();
602 Response res = client.delete(NON_EXISTENT_ID);
604 int statusCode = res.getStatus();
606 // Check the status code of the response: does it match
607 // the expected response(s)?
608 if (logger.isDebugEnabled()) {
609 logger.debug(testName + ": status = " + statusCode);
611 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
612 invalidStatusCodeMessage(testRequestType, statusCode));
613 Assert.assertEquals(statusCode, testExpectedStatusCode);
619 // ---------------------------------------------------------------
620 // Utility tests : tests of code used in tests above
621 // ---------------------------------------------------------------
623 * Tests the code for manually submitting data that is used by several
624 * of the methods above.
626 // @Test(dependsOnMethods = {"create", "read"})
627 public void testSubmitRequest() {
629 // Expected status code: 200 OK
630 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
632 // Submit the request to the service and store the response.
633 String method = ServiceRequestType.READ.httpMethodName();
634 String url = getResourceURL(knownResourceId);
635 int statusCode = submitRequest(method, url);
637 // Check the status code of the response: does it match
638 // the expected response(s)?
639 if (logger.isDebugEnabled()) {
640 logger.debug("testSubmitRequest: url=" + url
641 + " status=" + statusCode);
643 Assert.assertEquals(statusCode, EXPECTED_STATUS);
647 public void readWorkflow(String testName) throws Exception {
648 // Not applicable for the Contact service
652 public void searchWorkflowDeleted(String testName) throws Exception {
653 // Not applicable for the Contact service
657 protected PoxPayloadOut createInstance(String commonPartName,
659 return ContactClientUtils.createContactInstance(identifier, commonPartName);
663 protected ContactsCommon updateInstance(ContactsCommon commonPartObject) {
664 // TODO Auto-generated method stub
669 protected void compareUpdatedInstances(ContactsCommon original,
670 ContactsCommon updated) throws Exception {
671 // TODO Auto-generated method stub
676 public void CRUDTests(String testName) {
677 // TODO Auto-generated method stub