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.ArrayList;
26 import java.util.List;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
30 import org.collectionspace.services.client.ContactClient;
31 import org.collectionspace.services.contact.ContactsCommon;
32 import org.collectionspace.services.contact.ContactsCommonList;
34 import org.jboss.resteasy.client.ClientResponse;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
38 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
39 import org.testng.Assert;
40 import org.testng.annotations.AfterClass;
41 import org.testng.annotations.Test;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * ContactServiceTest, carries out tests against a
48 * deployed and running Contact Service.
50 * $LastChangedRevision: 917 $
51 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
53 public class ContactServiceTest extends AbstractServiceTest {
55 private final Logger logger =
56 LoggerFactory.getLogger(ContactServiceTest.class);
58 // Instance variables specific to this test.
59 private ContactClient client = new ContactClient();
60 final String SERVICE_PATH_COMPONENT = "contacts";
61 private String knownResourceId = null;
62 private List<String> allResourceIdsCreated = new ArrayList();
64 // ---------------------------------------------------------------
65 // CRUD tests : CREATE tests
66 // ---------------------------------------------------------------
69 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
70 public void create(String testName) throws Exception {
72 // Perform setup, such as initializing the type of service request
73 // (e.g. CREATE, DELETE), its valid and expected status codes, and
74 // its associated HTTP method name (e.g. POST, DELETE).
75 setupCreate(testName);
77 // Submit the request to the service and store the response.
78 String identifier = createIdentifier();
80 MultipartOutput multipart = createContactInstance(identifier);
81 ClientResponse<Response> res = client.create(multipart);
83 int statusCode = res.getStatus();
85 // Check the status code of the response: does it match
86 // the expected response(s)?
89 // Does it fall within the set of valid status codes?
90 // Does it exactly match the expected status code?
91 if(logger.isDebugEnabled()){
92 logger.debug(testName + ": status = " + statusCode);
94 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
95 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
96 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
98 // Store the ID returned from the first resource created
99 // for additional tests below.
100 if (knownResourceId == null){
101 knownResourceId = extractId(res);
102 if (logger.isDebugEnabled()) {
103 logger.debug(testName + ": knownResourceId=" + knownResourceId);
107 // Store the IDs from every resource created by tests,
108 // so they can be deleted after tests have been run.
109 allResourceIdsCreated.add(extractId(res));
113 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
114 dependsOnMethods = {"create"})
115 public void createList(String testName) throws Exception {
116 for(int i = 0; i < 3; i++){
122 // Placeholders until the three tests below can be uncommented.
123 // See Issue CSPACE-401.
125 public void createWithEmptyEntityBody(String testName) throws Exception {
129 public void createWithMalformedXml(String testName) throws Exception {
133 public void createWithWrongXmlSchema(String testName) throws Exception {
138 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
139 dependsOnMethods = {"create", "testSubmitRequest"})
140 public void createWithEmptyEntityBody(String testName) throws Exception {
143 setupCreateWithEmptyEntityBody(testName);
145 // Submit the request to the service and store the response.
146 String method = REQUEST_TYPE.httpMethodName();
147 String url = getServiceRootURL();
148 String mediaType = MediaType.APPLICATION_XML;
149 final String entity = "";
150 int statusCode = submitRequest(method, url, mediaType, entity);
152 // Check the status code of the response: does it match
153 // the expected response(s)?
154 if(logger.isDebugEnabled()){
155 logger.debug("createWithEmptyEntityBody url=" + url +
156 " status=" + statusCode);
158 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
159 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
160 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
164 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
165 dependsOnMethods = {"create", "testSubmitRequest"})
166 public void createWithMalformedXml(String testName) throws Exception {
169 setupCreateWithMalformedXml(testName);
171 // Submit the request to the service and store the response.
172 String method = REQUEST_TYPE.httpMethodName();
173 String url = getServiceRootURL();
174 String mediaType = MediaType.APPLICATION_XML;
175 final String entity = MALFORMED_XML_DATA; // Constant from base class.
176 int statusCode = submitRequest(method, url, mediaType, entity);
178 // Check the status code of the response: does it match
179 // the expected response(s)?
180 if(logger.isDebugEnabled()){
181 logger.debug(testName + ": url=" + url +
182 " status=" + statusCode);
184 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
185 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
186 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
190 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
191 dependsOnMethods = {"create", "testSubmitRequest"})
192 public void createWithWrongXmlSchema(String testName) throws Exception {
195 setupCreateWithWrongXmlSchema(testName);
197 // Submit the request to the service and store the response.
198 String method = REQUEST_TYPE.httpMethodName();
199 String url = getServiceRootURL();
200 String mediaType = MediaType.APPLICATION_XML;
201 final String entity = WRONG_XML_SCHEMA_DATA;
202 int statusCode = submitRequest(method, url, mediaType, entity);
204 // Check the status code of the response: does it match
205 // the expected response(s)?
206 if(logger.isDebugEnabled()){
207 logger.debug(testName + ": url=" + url +
208 " status=" + statusCode);
210 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
211 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
212 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
216 // ---------------------------------------------------------------
217 // CRUD tests : READ tests
218 // ---------------------------------------------------------------
221 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
222 dependsOnMethods = {"create"})
223 public void read(String testName) throws Exception {
228 // Submit the request to the service and store the response.
229 ClientResponse<MultipartInput> res = client.read(knownResourceId);
230 int statusCode = res.getStatus();
232 // Check the status code of the response: does it match
233 // the expected response(s)?
234 if(logger.isDebugEnabled()){
235 logger.debug(testName + ": status = " + statusCode);
237 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
238 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
239 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
241 MultipartInput input = (MultipartInput) res.getEntity();
242 ContactsCommon contact = (ContactsCommon) extractPart(input,
243 client.getCommonPartName(), ContactsCommon.class);
244 Assert.assertNotNull(contact);
249 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
250 dependsOnMethods = {"read"})
251 public void readNonExistent(String testName) throws Exception {
254 setupReadNonExistent(testName);
256 // Submit the request to the service and store the response.
257 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
258 int statusCode = res.getStatus();
260 // Check the status code of the response: does it match
261 // the expected response(s)?
262 if(logger.isDebugEnabled()){
263 logger.debug(testName + ": status = " + statusCode);
265 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
266 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
267 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
270 // ---------------------------------------------------------------
271 // CRUD tests : READ_LIST tests
272 // ---------------------------------------------------------------
275 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
276 dependsOnMethods = {"read"})
277 public void readList(String testName) throws Exception {
280 setupReadList(testName);
282 // Submit the request to the service and store the response.
283 ClientResponse<ContactsCommonList> res = client.readList();
284 ContactsCommonList list = res.getEntity();
285 int statusCode = res.getStatus();
287 // Check the status code of the response: does it match
288 // the expected response(s)?
289 if(logger.isDebugEnabled()){
290 logger.debug(testName + ": status = " + statusCode);
292 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
293 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
294 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
296 // Optionally output additional data about list members for debugging.
297 boolean iterateThroughList = false;
298 if(iterateThroughList && logger.isDebugEnabled()){
299 List<ContactsCommonList.ContactListItem> items =
300 list.getContactListItem();
302 for(ContactsCommonList.ContactListItem item : items){
303 logger.debug(testName + ": list-item[" + i + "] csid=" +
305 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
306 item.getAddressText());
307 logger.debug(testName + ": list-item[" + i + "] URI=" +
317 // ---------------------------------------------------------------
318 // CRUD tests : UPDATE tests
319 // ---------------------------------------------------------------
322 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
323 dependsOnMethods = {"read"})
324 public void update(String testName) throws Exception {
327 setupUpdate(testName);
329 ClientResponse<MultipartInput> res =
330 client.read(knownResourceId);
331 if(logger.isDebugEnabled()){
332 logger.debug(testName + ": read status = " + res.getStatus());
334 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
336 if(logger.isDebugEnabled()){
337 logger.debug("got object to update with ID: " + knownResourceId);
339 MultipartInput input = (MultipartInput) res.getEntity();
340 ContactsCommon contact = (ContactsCommon) extractPart(input,
341 client.getCommonPartName(), ContactsCommon.class);
342 Assert.assertNotNull(contact);
344 // Update the content of this resource.
345 contact.setAddressText("updated-" + contact.getAddressText());
346 contact.setPostcode("updated-" + contact.getPostcode());
347 if(logger.isDebugEnabled()){
348 logger.debug("to be updated object");
349 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
351 // Submit the request to the service and store the response.
352 MultipartOutput output = new MultipartOutput();
353 OutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
354 commonPart.getHeaders().add("label", client.getCommonPartName());
356 res = client.update(knownResourceId, output);
357 int statusCode = res.getStatus();
358 // Check the status code of the response: does it match the expected response(s)?
359 if(logger.isDebugEnabled()){
360 logger.debug(testName + ": status = " + statusCode);
362 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
363 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
364 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
367 input = (MultipartInput) res.getEntity();
368 ContactsCommon updatedContact =
369 (ContactsCommon) extractPart(input,
370 client.getCommonPartName(), ContactsCommon.class);
371 Assert.assertNotNull(updatedContact);
373 Assert.assertEquals(updatedContact.getPostcode(),
374 contact.getPostcode(),
375 "Data in updated object did not match submitted data.");
380 // Placeholders until the three tests below can be uncommented.
381 // See Issue CSPACE-401.
383 public void updateWithEmptyEntityBody(String testName) throws Exception{
386 public void updateWithMalformedXml(String testName) throws Exception {
389 public void updateWithWrongXmlSchema(String testName) throws Exception {
394 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
395 dependsOnMethods = {"create", "update", "testSubmitRequest"})
396 public void updateWithEmptyEntityBody(String testName) throws Exception {
399 setupUpdateWithEmptyEntityBody(testName);
401 // Submit the request to the service and store the response.
402 String method = REQUEST_TYPE.httpMethodName();
403 String url = getResourceURL(knownResourceId);
404 String mediaType = MediaType.APPLICATION_XML;
405 final String entity = "";
406 int statusCode = submitRequest(method, url, mediaType, entity);
408 // Check the status code of the response: does it match
409 // the expected response(s)?
410 if(logger.isDebugEnabled()){
411 logger.debug(testName + ": url=" + url +
412 " status=" + statusCode);
414 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
415 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
416 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
420 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
421 dependsOnMethods = {"create", "update", "testSubmitRequest"})
422 public void updateWithMalformedXml(String testName) throws Exception {
425 setupUpdateWithMalformedXml(testName);
427 // Submit the request to the service and store the response.
428 String method = REQUEST_TYPE.httpMethodName();
429 String url = getResourceURL(knownResourceId);
430 String mediaType = MediaType.APPLICATION_XML;
431 final String entity = MALFORMED_XML_DATA;
432 int statusCode = submitRequest(method, url, mediaType, entity);
434 // Check the status code of the response: does it match
435 // the expected response(s)?
436 if(logger.isDebugEnabled()){
437 logger.debug(testName + ": url=" + url +
438 " status=" + statusCode);
440 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
441 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
442 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
446 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
447 dependsOnMethods = {"create", "update", "testSubmitRequest"})
448 public void updateWithWrongXmlSchema(String testName) throws Exception {
451 setupUpdateWithWrongXmlSchema(testName);
453 // Submit the request to the service and store the response.
454 String method = REQUEST_TYPE.httpMethodName();
455 String url = getResourceURL(knownResourceId);
456 String mediaType = MediaType.APPLICATION_XML;
457 final String entity = WRONG_XML_SCHEMA_DATA;
458 int statusCode = submitRequest(method, url, mediaType, entity);
460 // Check the status code of the response: does it match
461 // the expected response(s)?
462 if(logger.isDebugEnabled()){
463 logger.debug(testName + ": url=" + url +
464 " status=" + statusCode);
466 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
467 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
468 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
473 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
474 dependsOnMethods = {"update", "testSubmitRequest"})
475 public void updateNonExistent(String testName) throws Exception {
478 setupUpdateNonExistent(testName);
480 // Submit the request to the service and store the response.
481 // Note: The ID used in this 'create' call may be arbitrary.
482 // The only relevant ID may be the one used in update(), below.
484 // The only relevant ID may be the one used in update(), below.
485 MultipartOutput multipart = createContactInstance(NON_EXISTENT_ID);
486 ClientResponse<MultipartInput> res =
487 client.update(NON_EXISTENT_ID, multipart);
488 int statusCode = res.getStatus();
490 // Check the status code of the response: does it match
491 // the expected response(s)?
492 if(logger.isDebugEnabled()){
493 logger.debug(testName + ": status = " + statusCode);
495 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
496 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
497 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
500 // ---------------------------------------------------------------
501 // CRUD tests : DELETE tests
502 // ---------------------------------------------------------------
505 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
506 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
507 public void delete(String testName) throws Exception {
510 setupDelete(testName);
512 // Submit the request to the service and store the response.
513 ClientResponse<Response> res = client.delete(knownResourceId);
514 int statusCode = res.getStatus();
516 // Check the status code of the response: does it match
517 // the expected response(s)?
518 if(logger.isDebugEnabled()){
519 logger.debug(testName + ": status = " + statusCode);
521 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
522 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
523 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
528 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
529 dependsOnMethods = {"delete"})
530 public void deleteNonExistent(String testName) throws Exception {
533 setupDeleteNonExistent(testName);
535 // Submit the request to the service and store the response.
536 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
537 int statusCode = res.getStatus();
539 // Check the status code of the response: does it match
540 // the expected response(s)?
541 if(logger.isDebugEnabled()){
542 logger.debug(testName + ": status = " + statusCode);
544 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
545 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
546 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
549 // ---------------------------------------------------------------
550 // Utility tests : tests of code used in tests above
551 // ---------------------------------------------------------------
553 * Tests the code for manually submitting data that is used by several
554 * of the methods above.
556 @Test(dependsOnMethods = {"create", "read"})
557 public void testSubmitRequest() {
559 // Expected status code: 200 OK
560 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
562 // Submit the request to the service and store the response.
563 String method = ServiceRequestType.READ.httpMethodName();
564 String url = getResourceURL(knownResourceId);
565 int statusCode = submitRequest(method, url);
567 // Check the status code of the response: does it match
568 // the expected response(s)?
569 if(logger.isDebugEnabled()){
570 logger.debug("testSubmitRequest: url=" + url +
571 " status=" + statusCode);
573 Assert.assertEquals(statusCode, EXPECTED_STATUS);
577 // ---------------------------------------------------------------
578 // Cleanup of resources created during testing
579 // ---------------------------------------------------------------
582 * Deletes all resources created by tests, after all tests have been run.
584 * This cleanup method will always be run, even if one or more tests fail.
585 * For this reason, it attempts to remove all resources created
586 * at any point during testing, even if some of those resources
587 * may be expected to be deleted by certain tests.
589 @AfterClass(alwaysRun=true)
590 public void cleanUp() {
591 if (logger.isDebugEnabled()) {
592 logger.debug("Cleaning up temporary resources created for testing ...");
594 for (String resourceId : allResourceIdsCreated) {
595 // Note: Any non-success responses are ignored and not reported.
596 ClientResponse<Response> res = client.delete(resourceId);
600 // ---------------------------------------------------------------
601 // Utility methods used by tests above
602 // ---------------------------------------------------------------
604 public String getServicePathComponent() {
605 return SERVICE_PATH_COMPONENT;
608 private MultipartOutput createContactInstance(String identifier) {
609 return createContactInstance(
610 "addressText-" + identifier,
611 "postcode-" + identifier,
612 "addressType-" + identifier);
615 private MultipartOutput createContactInstance(String addressText,
616 String postcode, String addressType) {
617 ContactsCommon contact = new ContactsCommon();
618 contact.setAddressText(addressText);
619 contact.setPostcode(postcode);
620 contact.setAddressType(addressType);
621 MultipartOutput multipart = new MultipartOutput();
622 OutputPart commonPart =
623 multipart.addPart(contact, MediaType.APPLICATION_XML_TYPE);
624 commonPart.getHeaders().add("label", client.getCommonPartName());
626 if(logger.isDebugEnabled()){
627 logger.debug("to be created, contact common");
628 logger.debug(objectAsXmlString(contact, ContactsCommon.class));