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.CollectionSpaceClient;
31 import org.collectionspace.services.client.ContactClient;
32 import org.collectionspace.services.client.ContactClientUtils;
33 import org.collectionspace.services.contact.ContactsCommon;
34 import org.collectionspace.services.contact.ContactsCommonList;
35 import org.collectionspace.services.jaxb.AbstractCommonList;
37 import org.jboss.resteasy.client.ClientResponse;
39 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
40 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
41 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
42 import org.testng.Assert;
43 import org.testng.annotations.AfterClass;
44 import org.testng.annotations.Test;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * ContactServiceTest, carries out tests against a
51 * deployed and running Contact Service.
53 * $LastChangedRevision: 917 $
54 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
56 public class ContactServiceTest extends AbstractServiceTestImpl {
58 private final Logger logger =
59 LoggerFactory.getLogger(ContactServiceTest.class);
61 // Instance variables specific to this test.
62 final String SERVICE_PATH_COMPONENT = "contacts";
63 private String knownResourceId = null;
64 private List<String> allResourceIdsCreated = new ArrayList();
67 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
70 protected CollectionSpaceClient getClientInstance() {
71 return new ContactClient();
75 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
78 protected AbstractCommonList getAbstractCommonList(
79 ClientResponse<AbstractCommonList> response) {
80 return response.getEntity(ContactsCommonList.class);
83 // ---------------------------------------------------------------
84 // CRUD tests : CREATE tests
85 // ---------------------------------------------------------------
88 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
89 public void create(String testName) throws Exception {
91 // Perform setup, such as initializing the type of service request
92 // (e.g. CREATE, DELETE), its valid and expected status codes, and
93 // its associated HTTP method name (e.g. POST, DELETE).
94 setupCreate(testName);
96 // Submit the request to the service and store the response.
97 ContactClient client = new ContactClient();
98 String identifier = createIdentifier();
99 MultipartOutput multipart =
100 ContactClientUtils.createContactInstance(identifier, client.getCommonPartName());
101 ClientResponse<Response> res = client.create(multipart);
103 int statusCode = res.getStatus();
105 // Check the status code of the response: does it match
106 // the expected response(s)?
109 // Does it fall within the set of valid status codes?
110 // Does it exactly match the expected status code?
111 if(logger.isDebugEnabled()){
112 logger.debug(testName + ": status = " + statusCode);
114 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
115 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
116 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
118 // Store the ID returned from the first resource created
119 // for additional tests below.
120 if (knownResourceId == null){
121 knownResourceId = extractId(res);
122 if (logger.isDebugEnabled()) {
123 logger.debug(testName + ": knownResourceId=" + knownResourceId);
127 // Store the IDs from every resource created by tests,
128 // so they can be deleted after tests have been run.
129 allResourceIdsCreated.add(extractId(res));
133 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
134 dependsOnMethods = {"create"})
135 public void createList(String testName) throws Exception {
136 for(int i = 0; i < 3; i++){
142 // Placeholders until the three tests below can be uncommented.
143 // See Issue CSPACE-401.
145 public void createWithEmptyEntityBody(String testName) throws Exception {
149 public void createWithMalformedXml(String testName) throws Exception {
153 public void createWithWrongXmlSchema(String testName) throws Exception {
158 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
159 dependsOnMethods = {"create", "testSubmitRequest"})
160 public void createWithEmptyEntityBody(String testName) throws Exception {
163 setupCreateWithEmptyEntityBody(testName);
165 // Submit the request to the service and store the response.
166 String method = REQUEST_TYPE.httpMethodName();
167 String url = getServiceRootURL();
168 String mediaType = MediaType.APPLICATION_XML;
169 final String entity = "";
170 int statusCode = submitRequest(method, url, mediaType, entity);
172 // Check the status code of the response: does it match
173 // the expected response(s)?
174 if(logger.isDebugEnabled()){
175 logger.debug("createWithEmptyEntityBody url=" + url +
176 " status=" + statusCode);
178 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
184 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
185 dependsOnMethods = {"create", "testSubmitRequest"})
186 public void createWithMalformedXml(String testName) throws Exception {
189 setupCreateWithMalformedXml(testName);
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 = MALFORMED_XML_DATA; // Constant from base class.
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(testName + ": 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 createWithWrongXmlSchema(String testName) throws Exception {
215 setupCreateWithWrongXmlSchema(testName);
217 // Submit the request to the service and store the response.
218 String method = REQUEST_TYPE.httpMethodName();
219 String url = getServiceRootURL();
220 String mediaType = MediaType.APPLICATION_XML;
221 final String entity = WRONG_XML_SCHEMA_DATA;
222 int statusCode = submitRequest(method, url, mediaType, entity);
224 // Check the status code of the response: does it match
225 // the expected response(s)?
226 if(logger.isDebugEnabled()){
227 logger.debug(testName + ": url=" + url +
228 " status=" + statusCode);
230 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
231 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
232 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
236 // ---------------------------------------------------------------
237 // CRUD tests : READ tests
238 // ---------------------------------------------------------------
241 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
242 dependsOnMethods = {"create"})
243 public void read(String testName) throws Exception {
248 // Submit the request to the service and store the response.
249 ContactClient client = new ContactClient();
250 ClientResponse<MultipartInput> res = client.read(knownResourceId);
251 int statusCode = res.getStatus();
253 // Check the status code of the response: does it match
254 // the expected response(s)?
255 if(logger.isDebugEnabled()){
256 logger.debug(testName + ": status = " + statusCode);
258 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
259 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
260 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
262 MultipartInput input = (MultipartInput) res.getEntity();
263 ContactsCommon contact = (ContactsCommon) extractPart(input,
264 client.getCommonPartName(), ContactsCommon.class);
265 Assert.assertNotNull(contact);
270 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
271 dependsOnMethods = {"read"})
272 public void readNonExistent(String testName) throws Exception {
275 setupReadNonExistent(testName);
277 // Submit the request to the service and store the response.
278 ContactClient client = new ContactClient();
279 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
280 int statusCode = res.getStatus();
282 // Check the status code of the response: does it match
283 // the expected response(s)?
284 if(logger.isDebugEnabled()){
285 logger.debug(testName + ": status = " + statusCode);
287 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
288 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
289 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
292 // ---------------------------------------------------------------
293 // CRUD tests : READ_LIST tests
294 // ---------------------------------------------------------------
297 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
298 dependsOnMethods = {"read"})
299 public void readList(String testName) throws Exception {
302 setupReadList(testName);
304 // Submit the request to the service and store the response.
305 ContactClient client = new ContactClient();
306 ClientResponse<ContactsCommonList> res = client.readList();
307 ContactsCommonList list = res.getEntity();
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);
319 // Optionally output additional data about list members for debugging.
320 boolean iterateThroughList = false;
321 if(iterateThroughList && logger.isDebugEnabled()){
322 List<ContactsCommonList.ContactListItem> items =
323 list.getContactListItem();
325 for(ContactsCommonList.ContactListItem item : items){
326 logger.debug(testName + ": list-item[" + i + "] csid=" +
328 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
329 item.getAddressPlace());
330 logger.debug(testName + ": list-item[" + i + "] URI=" +
340 // ---------------------------------------------------------------
341 // CRUD tests : UPDATE tests
342 // ---------------------------------------------------------------
345 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
346 dependsOnMethods = {"read"})
347 public void update(String testName) throws Exception {
350 setupUpdate(testName);
352 // Submit the request to the service and store the response.
353 ContactClient client = new ContactClient();
354 ClientResponse<MultipartInput> res =
355 client.read(knownResourceId);
356 if(logger.isDebugEnabled()){
357 logger.debug(testName + ": read status = " + res.getStatus());
359 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
361 if(logger.isDebugEnabled()){
362 logger.debug("got object to update with ID: " + knownResourceId);
364 MultipartInput input = (MultipartInput) res.getEntity();
365 ContactsCommon contact = (ContactsCommon) extractPart(input,
366 client.getCommonPartName(), ContactsCommon.class);
367 Assert.assertNotNull(contact);
369 // Update the content of this resource.
370 contact.setAddressType("updated-" + contact.getAddressType());
371 contact.setAddressPlace("updated-" + contact.getAddressPlace());
372 contact.setEmail("updated-" + contact.getEmail());
373 if(logger.isDebugEnabled()){
374 logger.debug("to be updated object");
375 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
377 // Submit the request to the service and store the response.
378 MultipartOutput output = new MultipartOutput();
379 OutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
380 commonPart.getHeaders().add("label", client.getCommonPartName());
382 res = client.update(knownResourceId, output);
383 int statusCode = res.getStatus();
384 // Check the status code of the response: does it match the expected response(s)?
385 if(logger.isDebugEnabled()){
386 logger.debug(testName + ": status = " + statusCode);
388 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
389 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
390 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
393 input = (MultipartInput) res.getEntity();
394 ContactsCommon updatedContact =
395 (ContactsCommon) extractPart(input,
396 client.getCommonPartName(), ContactsCommon.class);
397 Assert.assertNotNull(updatedContact);
399 Assert.assertEquals(updatedContact.getEmail(),
401 "Data in updated object did not match submitted data.");
406 // Placeholders until the three tests below can be uncommented.
407 // See Issue CSPACE-401.
409 public void updateWithEmptyEntityBody(String testName) throws Exception{
412 public void updateWithMalformedXml(String testName) throws Exception {
415 public void updateWithWrongXmlSchema(String testName) throws Exception {
420 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
421 dependsOnMethods = {"create", "update", "testSubmitRequest"})
422 public void updateWithEmptyEntityBody(String testName) throws Exception {
425 setupUpdateWithEmptyEntityBody(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 = "";
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 updateWithMalformedXml(String testName) throws Exception {
451 setupUpdateWithMalformedXml(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 = MALFORMED_XML_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);
472 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
473 dependsOnMethods = {"create", "update", "testSubmitRequest"})
474 public void updateWithWrongXmlSchema(String testName) throws Exception {
477 setupUpdateWithWrongXmlSchema(testName);
479 // Submit the request to the service and store the response.
480 String method = REQUEST_TYPE.httpMethodName();
481 String url = getResourceURL(knownResourceId);
482 String mediaType = MediaType.APPLICATION_XML;
483 final String entity = WRONG_XML_SCHEMA_DATA;
484 int statusCode = submitRequest(method, url, mediaType, entity);
486 // Check the status code of the response: does it match
487 // the expected response(s)?
488 if(logger.isDebugEnabled()){
489 logger.debug(testName + ": url=" + url +
490 " status=" + statusCode);
492 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
493 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
494 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
499 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
500 dependsOnMethods = {"update", "testSubmitRequest"})
501 public void updateNonExistent(String testName) throws Exception {
504 setupUpdateNonExistent(testName);
506 // Submit the request to the service and store the response.
507 // Note: The ID used in this 'create' call may be arbitrary.
508 // The only relevant ID may be the one used in update(), below.
509 ContactClient client = new ContactClient();
510 MultipartOutput multipart =
511 ContactClientUtils.createContactInstance(NON_EXISTENT_ID, client.getCommonPartName());
512 ClientResponse<MultipartInput> res =
513 client.update(NON_EXISTENT_ID, multipart);
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);
526 // ---------------------------------------------------------------
527 // CRUD tests : DELETE tests
528 // ---------------------------------------------------------------
531 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
532 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
533 public void delete(String testName) throws Exception {
536 setupDelete(testName);
538 // Submit the request to the service and store the response.
539 ContactClient client = new ContactClient();
540 ClientResponse<Response> res = client.delete(knownResourceId);
541 int statusCode = res.getStatus();
543 // Check the status code of the response: does it match
544 // the expected response(s)?
545 if(logger.isDebugEnabled()){
546 logger.debug(testName + ": status = " + statusCode);
548 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
549 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
550 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
555 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
556 dependsOnMethods = {"delete"})
557 public void deleteNonExistent(String testName) throws Exception {
560 setupDeleteNonExistent(testName);
562 // Submit the request to the service and store the response.
563 ContactClient client = new ContactClient();
564 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
565 int statusCode = res.getStatus();
567 // Check the status code of the response: does it match
568 // the expected response(s)?
569 if(logger.isDebugEnabled()){
570 logger.debug(testName + ": status = " + statusCode);
572 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
573 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
574 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
577 // ---------------------------------------------------------------
578 // Utility tests : tests of code used in tests above
579 // ---------------------------------------------------------------
581 * Tests the code for manually submitting data that is used by several
582 * of the methods above.
584 @Test(dependsOnMethods = {"create", "read"})
585 public void testSubmitRequest() {
587 // Expected status code: 200 OK
588 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
590 // Submit the request to the service and store the response.
591 String method = ServiceRequestType.READ.httpMethodName();
592 String url = getResourceURL(knownResourceId);
593 int statusCode = submitRequest(method, url);
595 // Check the status code of the response: does it match
596 // the expected response(s)?
597 if(logger.isDebugEnabled()){
598 logger.debug("testSubmitRequest: url=" + url +
599 " status=" + statusCode);
601 Assert.assertEquals(statusCode, EXPECTED_STATUS);
605 // ---------------------------------------------------------------
606 // Cleanup of resources created during testing
607 // ---------------------------------------------------------------
610 * Deletes all resources created by tests, after all tests have been run.
612 * This cleanup method will always be run, even if one or more tests fail.
613 * For this reason, it attempts to remove all resources created
614 * at any point during testing, even if some of those resources
615 * may be expected to be deleted by certain tests.
617 @AfterClass(alwaysRun=true)
618 public void cleanUp() {
619 if (logger.isDebugEnabled()) {
620 logger.debug("Cleaning up temporary resources created for testing ...");
622 ContactClient client = new ContactClient();
623 for (String resourceId : allResourceIdsCreated) {
624 // Note: Any non-success responses are ignored and not reported.
625 ClientResponse<Response> res = client.delete(resourceId);
629 // ---------------------------------------------------------------
630 // Utility methods used by tests above
631 // ---------------------------------------------------------------
633 public String getServicePathComponent() {
634 return SERVICE_PATH_COMPONENT;