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 ClientResponse<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);
133 // Store the ID returned from the first resource created
134 // for additional tests below.
135 if (knownResourceId == null) {
136 knownResourceId = extractId(res);
137 if (logger.isDebugEnabled()) {
138 logger.debug(testName + ": knownResourceId=" + knownResourceId);
142 // Store the IDs from every resource created by tests,
143 // so they can be deleted after tests have been run.
144 allResourceIdsCreated.add(extractId(res));
148 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
149 // dependsOnMethods = {"create"})
150 public void createList(String testName) throws Exception {
151 for (int i = 0; i < 3; i++) {
157 // Placeholders until the three tests below can be uncommented.
158 // See Issue CSPACE-401.
160 public void createWithEmptyEntityBody(String testName) throws Exception {
161 //Should this really be empty?
165 public void createWithMalformedXml(String testName) throws Exception {
166 //Should this really be empty??
170 public void createWithWrongXmlSchema(String testName) throws Exception {
171 //Should this really be empty??
176 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
177 dependsOnMethods = {"create", "testSubmitRequest"})
178 public void createWithEmptyEntityBody(String testName) throws Exception {
180 if (logger.isDebugEnabled()) {
181 logger.debug(testBanner(testName, CLASS_NAME));
184 setupCreateWithEmptyEntityBody(testName, logger);
186 // Submit the request to the service and store the response.
187 String method = REQUEST_TYPE.httpMethodName();
188 String url = getServiceRootURL();
189 String mediaType = MediaType.APPLICATION_XML;
190 final String entity = "";
191 int statusCode = submitRequest(method, url, mediaType, entity);
193 // Check the status code of the response: does it match
194 // the expected response(s)?
195 if(logger.isDebugEnabled()){
196 logger.debug("createWithEmptyEntityBody url=" + url +
197 " status=" + statusCode);
199 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
200 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
201 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
205 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
206 dependsOnMethods = {"create", "testSubmitRequest"})
207 public void createWithMalformedXml(String testName) throws Exception {
209 if (logger.isDebugEnabled()) {
210 logger.debug(testBanner(testName, CLASS_NAME));
213 setupCreateWithMalformedXml();
215 // Submit the request to the service and store the response.
216 String method = REQUEST_TYPE.httpMethodName();
217 String url = getServiceRootURL();
218 String mediaType = MediaType.APPLICATION_XML;
219 final String entity = MALFORMED_XML_DATA; // Constant from base class.
220 int statusCode = submitRequest(method, url, mediaType, entity);
222 // Check the status code of the response: does it match
223 // the expected response(s)?
224 if(logger.isDebugEnabled()){
225 logger.debug(testName + ": url=" + url +
226 " status=" + statusCode);
228 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
229 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
230 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
234 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
235 dependsOnMethods = {"create", "testSubmitRequest"})
236 public void createWithWrongXmlSchema(String testName) throws Exception {
238 if (logger.isDebugEnabled()) {
239 logger.debug(testBanner(testName, CLASS_NAME));
242 setupCreateWithWrongXmlSchema(testName, logger);
244 // Submit the request to the service and store the response.
245 String method = REQUEST_TYPE.httpMethodName();
246 String url = getServiceRootURL();
247 String mediaType = MediaType.APPLICATION_XML;
248 final String entity = WRONG_XML_SCHEMA_DATA;
249 int statusCode = submitRequest(method, url, mediaType, entity);
251 // Check the status code of the response: does it match
252 // the expected response(s)?
253 if(logger.isDebugEnabled()){
254 logger.debug(testName + ": url=" + url +
255 " status=" + statusCode);
257 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
258 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
259 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
262 // ---------------------------------------------------------------
263 // CRUD tests : READ tests
264 // ---------------------------------------------------------------
267 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
268 // dependsOnMethods = {"create"})
269 public void read(String testName) throws Exception {
273 // Submit the request to the service and store the response.
274 ContactClient client = new ContactClient();
275 ClientResponse<String> res = client.read(knownResourceId);
277 assertStatusCode(res, testName);
278 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
279 ContactsCommon contact = (ContactsCommon) extractPart(input,
280 client.getCommonPartName(), ContactsCommon.class);
281 Assert.assertNotNull(contact);
284 res.releaseConnection();
289 // ---------------------------------------------------------------
290 // CRUD tests : READ_LIST tests
291 // ---------------------------------------------------------------
294 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
295 // dependsOnMethods = {"read"})
296 public void readList(String testName) throws Exception {
300 // Submit the request to the service and store the response.
301 ContactClient client = new ContactClient();
302 ClientResponse<AbstractCommonList> res = client.readList();
304 assertStatusCode(res, testName);
305 AbstractCommonList list = res.getEntity();
307 // Optionally output additional data about list members for debugging.
308 boolean iterateThroughList = false;
309 if (iterateThroughList && logger.isDebugEnabled()) {
310 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
314 res.releaseConnection();
321 // ---------------------------------------------------------------
322 // CRUD tests : UPDATE tests
323 // ---------------------------------------------------------------
326 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
327 // dependsOnMethods = {"read"})
328 public void update(String testName) throws Exception {
332 // First read the object that will be updated
334 ContactClient client = new ContactClient();
335 ClientResponse<String> res = client.read(knownResourceId);
336 ContactsCommon contact = null;
338 assertStatusCode(res, testName);
340 if (logger.isDebugEnabled()) {
341 logger.debug("got object to update with ID: " + knownResourceId);
343 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
344 contact = (ContactsCommon) extractPart(input,
345 client.getCommonPartName(), ContactsCommon.class);
346 Assert.assertNotNull(contact);
349 res.releaseConnection();
353 if(logger.isDebugEnabled()){
354 logger.debug("contact common before updating");
355 logger.debug(BaseServiceTest.objectAsXmlString(contact, ContactsCommon.class));
358 // Verify the contents of this resource
359 EmailGroupList emailGroupList = contact.getEmailGroupList();
360 Assert.assertNotNull(emailGroupList);
361 List<EmailGroup> emailGroups = emailGroupList.getEmailGroup();
362 Assert.assertNotNull(emailGroups);
363 Assert.assertTrue(emailGroups.size() > 0);
364 String email = emailGroups.get(0).getEmail();
365 Assert.assertNotNull(email);
367 AddressGroupList addressGroupList = contact.getAddressGroupList();
368 Assert.assertNotNull(addressGroupList);
369 List<AddressGroup> addressGroups = addressGroupList.getAddressGroup();
370 Assert.assertNotNull(addressGroups);
371 Assert.assertTrue(addressGroups.size() > 0);
372 String addressType = addressGroups.get(0).getAddressType();
373 String addressPlace1 = addressGroups.get(0).getAddressPlace1();
374 Assert.assertNotNull(addressType);
375 Assert.assertNotNull(addressPlace1);
377 // Update the contents of this resource.
378 emailGroups.get(0).setEmail("updated-" + email);
379 contact.setEmailGroupList(emailGroupList);
381 addressGroups.get(0).setAddressType("updated-" + addressType);
382 addressGroups.get(0).setAddressPlace1("updated-" + addressPlace1);
383 contact.setAddressGroupList(addressGroupList);
385 if (logger.isDebugEnabled()) {
386 logger.debug("to be updated object");
387 logger.debug(BaseServiceTest.objectAsXmlString(contact, ContactsCommon.class));
390 // Next, send the update to the server
392 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
393 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), contact);
395 res = client.update(knownResourceId, output);
396 ContactsCommon updatedContact = null;
398 assertStatusCode(res, testName);
399 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
400 updatedContact = (ContactsCommon) extractPart(input,
401 client.getCommonPartName(), ContactsCommon.class);
402 Assert.assertNotNull(updatedContact);
405 res.releaseConnection();
409 if (logger.isDebugEnabled()) {
410 logger.debug("object after update");
411 logger.debug(objectAsXmlString(updatedContact, ContactsCommon.class));
414 Assert.assertNotNull(updatedContact.getEmailGroupList().getEmailGroup().get(0));
415 Assert.assertEquals(updatedContact.getEmailGroupList().getEmailGroup().get(0).getEmail(),
416 contact.getEmailGroupList().getEmailGroup().get(0).getEmail(),
417 "Data in updated object did not match submitted data.");
422 // Placeholders until the three tests below can be uncommented.
423 // See Issue CSPACE-401.
425 public void updateWithEmptyEntityBody(String testName) throws Exception {
426 //Should this really be empty??
430 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
433 public void updateWithMalformedXml(String testName) throws Exception {
434 //Should this really be empty??
438 public void updateWithWrongXmlSchema(String testName) throws Exception {
439 //Should this really be empty??
444 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
445 dependsOnMethods = {"create", "update", "testSubmitRequest"})
446 public void updateWithEmptyEntityBody(String testName) throws Exception {
448 if (logger.isDebugEnabled()) {
449 logger.debug(testBanner(testName, CLASS_NAME));
452 setupUpdateWithEmptyEntityBody();
454 // Submit the request to the service and store the response.
455 String method = REQUEST_TYPE.httpMethodName();
456 String url = getResourceURL(knownResourceId);
457 String mediaType = MediaType.APPLICATION_XML;
458 final String entity = "";
459 int statusCode = submitRequest(method, url, mediaType, entity);
461 // Check the status code of the response: does it match
462 // the expected response(s)?
463 if(logger.isDebugEnabled()){
464 logger.debug(testName + ": url=" + url +
465 " status=" + statusCode);
467 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
468 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
469 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
473 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
474 dependsOnMethods = {"create", "update", "testSubmitRequest"})
475 public void updateWithMalformedXml(String testName) throws Exception {
477 if (logger.isDebugEnabled()) {
478 logger.debug(testBanner(testName, CLASS_NAME));
481 setupUpdateWithMalformedXml();
483 // Submit the request to the service and store the response.
484 String method = REQUEST_TYPE.httpMethodName();
485 String url = getResourceURL(knownResourceId);
486 String mediaType = MediaType.APPLICATION_XML;
487 final String entity = MALFORMED_XML_DATA;
488 int statusCode = submitRequest(method, url, mediaType, entity);
490 // Check the status code of the response: does it match
491 // the expected response(s)?
492 if(logger.isDebugEnabled()){
493 logger.debug(testName + ": url=" + url +
494 " status=" + statusCode);
496 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
497 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
498 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
502 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
503 dependsOnMethods = {"create", "update", "testSubmitRequest"})
504 public void updateWithWrongXmlSchema(String testName) throws Exception {
506 if (logger.isDebugEnabled()) {
507 logger.debug(testBanner(testName, CLASS_NAME));
510 setupUpdateWithWrongXmlSchema(testName, logger);
512 // Submit the request to the service and store the response.
513 String method = REQUEST_TYPE.httpMethodName();
514 String url = getResourceURL(knownResourceId);
515 String mediaType = MediaType.APPLICATION_XML;
516 final String entity = WRONG_XML_SCHEMA_DATA;
517 int statusCode = submitRequest(method, url, mediaType, entity);
519 // Check the status code of the response: does it match
520 // the expected response(s)?
521 if(logger.isDebugEnabled()){
522 logger.debug(testName + ": url=" + url +
523 " status=" + statusCode);
525 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
526 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
527 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
531 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
532 // dependsOnMethods = {"update", "testSubmitRequest"})
533 public void updateNonExistent(String testName) throws Exception {
535 setupUpdateNonExistent();
537 // Submit the request to the service and store the response.
538 // Note: The ID used in this 'create' call may be arbitrary.
539 // The only relevant ID may be the one used in update(), below.
540 ContactClient client = new ContactClient();
541 PoxPayloadOut multipart =
542 ContactClientUtils.createContactInstance(NON_EXISTENT_ID, client.getCommonPartName());
543 ClientResponse<String> res =
544 client.update(NON_EXISTENT_ID, multipart);
545 int statusCode = res.getStatus();
547 // Check the status code of the response: does it match
548 // the expected response(s)?
549 if (logger.isDebugEnabled()) {
550 logger.debug(testName + ": status = " + statusCode);
552 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
553 invalidStatusCodeMessage(testRequestType, statusCode));
554 Assert.assertEquals(statusCode, testExpectedStatusCode);
557 // ---------------------------------------------------------------
558 // CRUD tests : DELETE tests
559 // ---------------------------------------------------------------
562 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
563 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
564 public void delete(String testName) throws Exception {
568 // Submit the request to the service and store the response.
569 ContactClient client = new ContactClient();
570 ClientResponse<Response> res = client.delete(knownResourceId);
571 int statusCode = res.getStatus();
573 // Check the status code of the response: does it match
574 // the expected response(s)?
575 if (logger.isDebugEnabled()) {
576 logger.debug(testName + ": status = " + statusCode);
578 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
579 invalidStatusCodeMessage(testRequestType, statusCode));
580 Assert.assertEquals(statusCode, testExpectedStatusCode);
585 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
586 // dependsOnMethods = {"delete"})
587 public void deleteNonExistent(String testName) throws Exception {
589 setupDeleteNonExistent();
591 // Submit the request to the service and store the response.
592 ContactClient client = new ContactClient();
593 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
594 int statusCode = res.getStatus();
596 // Check the status code of the response: does it match
597 // the expected response(s)?
598 if (logger.isDebugEnabled()) {
599 logger.debug(testName + ": status = " + statusCode);
601 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
602 invalidStatusCodeMessage(testRequestType, statusCode));
603 Assert.assertEquals(statusCode, testExpectedStatusCode);
606 // ---------------------------------------------------------------
607 // Utility tests : tests of code used in tests above
608 // ---------------------------------------------------------------
610 * Tests the code for manually submitting data that is used by several
611 * of the methods above.
613 // @Test(dependsOnMethods = {"create", "read"})
614 public void testSubmitRequest() {
616 // Expected status code: 200 OK
617 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
619 // Submit the request to the service and store the response.
620 String method = ServiceRequestType.READ.httpMethodName();
621 String url = getResourceURL(knownResourceId);
622 int statusCode = submitRequest(method, url);
624 // Check the status code of the response: does it match
625 // the expected response(s)?
626 if (logger.isDebugEnabled()) {
627 logger.debug("testSubmitRequest: url=" + url
628 + " status=" + statusCode);
630 Assert.assertEquals(statusCode, EXPECTED_STATUS);
634 public void readWorkflow(String testName) throws Exception {
635 // Not applicable for the Contact service
639 public void searchWorkflowDeleted(String testName) throws Exception {
640 // Not applicable for the Contact service
644 protected PoxPayloadOut createInstance(String commonPartName,
646 return ContactClientUtils.createContactInstance(identifier, commonPartName);
650 protected ContactsCommon updateInstance(ContactsCommon commonPartObject) {
651 // TODO Auto-generated method stub
656 protected void compareUpdatedInstances(ContactsCommon original,
657 ContactsCommon updated) throws Exception {
658 // TODO Auto-generated method stub
663 public void CRUDTests(String testName) {
664 // TODO Auto-generated method stub