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.client.ContactClientUtils;
32 import org.collectionspace.services.contact.ContactsCommon;
33 import org.collectionspace.services.contact.ContactsCommonList;
35 import org.jboss.resteasy.client.ClientResponse;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
38 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
39 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
40 import org.testng.Assert;
41 import org.testng.annotations.AfterClass;
42 import org.testng.annotations.Test;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * ContactServiceTest, carries out tests against a
49 * deployed and running Contact Service.
51 * $LastChangedRevision: 917 $
52 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
54 public class ContactServiceTest extends AbstractServiceTestImpl {
56 private final Logger logger =
57 LoggerFactory.getLogger(ContactServiceTest.class);
59 // Instance variables specific to this test.
60 private ContactClient client = new ContactClient();
61 final String SERVICE_PATH_COMPONENT = "contacts";
62 private String knownResourceId = null;
63 private List<String> allResourceIdsCreated = new ArrayList();
65 // ---------------------------------------------------------------
66 // CRUD tests : CREATE tests
67 // ---------------------------------------------------------------
70 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
71 public void create(String testName) throws Exception {
73 // Perform setup, such as initializing the type of service request
74 // (e.g. CREATE, DELETE), its valid and expected status codes, and
75 // its associated HTTP method name (e.g. POST, DELETE).
76 setupCreate(testName);
78 // Submit the request to the service and store the response.
79 String identifier = createIdentifier();
81 MultipartOutput multipart =
82 ContactClientUtils.createContactInstance(identifier, client.getCommonPartName());
83 ClientResponse<Response> res = client.create(multipart);
85 int statusCode = res.getStatus();
87 // Check the status code of the response: does it match
88 // the expected response(s)?
91 // Does it fall within the set of valid status codes?
92 // Does it exactly match the expected status code?
93 if(logger.isDebugEnabled()){
94 logger.debug(testName + ": status = " + statusCode);
96 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
97 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
98 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
100 // Store the ID returned from the first resource created
101 // for additional tests below.
102 if (knownResourceId == null){
103 knownResourceId = extractId(res);
104 if (logger.isDebugEnabled()) {
105 logger.debug(testName + ": knownResourceId=" + knownResourceId);
109 // Store the IDs from every resource created by tests,
110 // so they can be deleted after tests have been run.
111 allResourceIdsCreated.add(extractId(res));
115 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
116 dependsOnMethods = {"create"})
117 public void createList(String testName) throws Exception {
118 for(int i = 0; i < 3; i++){
124 // Placeholders until the three tests below can be uncommented.
125 // See Issue CSPACE-401.
127 public void createWithEmptyEntityBody(String testName) throws Exception {
131 public void createWithMalformedXml(String testName) throws Exception {
135 public void createWithWrongXmlSchema(String testName) throws Exception {
140 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
141 dependsOnMethods = {"create", "testSubmitRequest"})
142 public void createWithEmptyEntityBody(String testName) throws Exception {
145 setupCreateWithEmptyEntityBody(testName);
147 // Submit the request to the service and store the response.
148 String method = REQUEST_TYPE.httpMethodName();
149 String url = getServiceRootURL();
150 String mediaType = MediaType.APPLICATION_XML;
151 final String entity = "";
152 int statusCode = submitRequest(method, url, mediaType, entity);
154 // Check the status code of the response: does it match
155 // the expected response(s)?
156 if(logger.isDebugEnabled()){
157 logger.debug("createWithEmptyEntityBody url=" + url +
158 " status=" + statusCode);
160 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
161 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
162 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
166 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
167 dependsOnMethods = {"create", "testSubmitRequest"})
168 public void createWithMalformedXml(String testName) throws Exception {
171 setupCreateWithMalformedXml(testName);
173 // Submit the request to the service and store the response.
174 String method = REQUEST_TYPE.httpMethodName();
175 String url = getServiceRootURL();
176 String mediaType = MediaType.APPLICATION_XML;
177 final String entity = MALFORMED_XML_DATA; // Constant from base class.
178 int statusCode = submitRequest(method, url, mediaType, entity);
180 // Check the status code of the response: does it match
181 // the expected response(s)?
182 if(logger.isDebugEnabled()){
183 logger.debug(testName + ": url=" + url +
184 " status=" + statusCode);
186 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
187 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
188 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
192 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
193 dependsOnMethods = {"create", "testSubmitRequest"})
194 public void createWithWrongXmlSchema(String testName) throws Exception {
197 setupCreateWithWrongXmlSchema(testName);
199 // Submit the request to the service and store the response.
200 String method = REQUEST_TYPE.httpMethodName();
201 String url = getServiceRootURL();
202 String mediaType = MediaType.APPLICATION_XML;
203 final String entity = WRONG_XML_SCHEMA_DATA;
204 int statusCode = submitRequest(method, url, mediaType, entity);
206 // Check the status code of the response: does it match
207 // the expected response(s)?
208 if(logger.isDebugEnabled()){
209 logger.debug(testName + ": url=" + url +
210 " status=" + statusCode);
212 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
213 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
214 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
218 // ---------------------------------------------------------------
219 // CRUD tests : READ tests
220 // ---------------------------------------------------------------
223 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
224 dependsOnMethods = {"create"})
225 public void read(String testName) throws Exception {
230 // Submit the request to the service and store the response.
231 ClientResponse<MultipartInput> res = client.read(knownResourceId);
232 int statusCode = res.getStatus();
234 // Check the status code of the response: does it match
235 // the expected response(s)?
236 if(logger.isDebugEnabled()){
237 logger.debug(testName + ": status = " + statusCode);
239 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
240 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
241 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
243 MultipartInput input = (MultipartInput) res.getEntity();
244 ContactsCommon contact = (ContactsCommon) extractPart(input,
245 client.getCommonPartName(), ContactsCommon.class);
246 Assert.assertNotNull(contact);
251 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
252 dependsOnMethods = {"read"})
253 public void readNonExistent(String testName) throws Exception {
256 setupReadNonExistent(testName);
258 // Submit the request to the service and store the response.
259 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
260 int statusCode = res.getStatus();
262 // Check the status code of the response: does it match
263 // the expected response(s)?
264 if(logger.isDebugEnabled()){
265 logger.debug(testName + ": status = " + statusCode);
267 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
268 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
269 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
272 // ---------------------------------------------------------------
273 // CRUD tests : READ_LIST tests
274 // ---------------------------------------------------------------
277 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
278 dependsOnMethods = {"read"})
279 public void readList(String testName) throws Exception {
282 setupReadList(testName);
284 // Submit the request to the service and store the response.
285 ClientResponse<ContactsCommonList> res = client.readList();
286 ContactsCommonList list = res.getEntity();
287 int statusCode = res.getStatus();
289 // Check the status code of the response: does it match
290 // the expected response(s)?
291 if(logger.isDebugEnabled()){
292 logger.debug(testName + ": status = " + statusCode);
294 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
295 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
296 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
298 // Optionally output additional data about list members for debugging.
299 boolean iterateThroughList = false;
300 if(iterateThroughList && logger.isDebugEnabled()){
301 List<ContactsCommonList.ContactListItem> items =
302 list.getContactListItem();
304 for(ContactsCommonList.ContactListItem item : items){
305 logger.debug(testName + ": list-item[" + i + "] csid=" +
307 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
308 item.getAddressPlace());
309 logger.debug(testName + ": list-item[" + i + "] URI=" +
319 // ---------------------------------------------------------------
320 // CRUD tests : UPDATE tests
321 // ---------------------------------------------------------------
324 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
325 dependsOnMethods = {"read"})
326 public void update(String testName) throws Exception {
329 setupUpdate(testName);
331 ClientResponse<MultipartInput> res =
332 client.read(knownResourceId);
333 if(logger.isDebugEnabled()){
334 logger.debug(testName + ": read status = " + res.getStatus());
336 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
338 if(logger.isDebugEnabled()){
339 logger.debug("got object to update with ID: " + knownResourceId);
341 MultipartInput input = (MultipartInput) res.getEntity();
342 ContactsCommon contact = (ContactsCommon) extractPart(input,
343 client.getCommonPartName(), ContactsCommon.class);
344 Assert.assertNotNull(contact);
346 // Update the content of this resource.
347 contact.setAddressType("updated-" + contact.getAddressType());
348 contact.setAddressPlace("updated-" + contact.getAddressPlace());
349 contact.setEmail("updated-" + contact.getEmail());
350 if(logger.isDebugEnabled()){
351 logger.debug("to be updated object");
352 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
354 // Submit the request to the service and store the response.
355 MultipartOutput output = new MultipartOutput();
356 OutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
357 commonPart.getHeaders().add("label", client.getCommonPartName());
359 res = client.update(knownResourceId, output);
360 int statusCode = res.getStatus();
361 // Check the status code of the response: does it match the expected response(s)?
362 if(logger.isDebugEnabled()){
363 logger.debug(testName + ": status = " + statusCode);
365 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
366 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
367 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
370 input = (MultipartInput) res.getEntity();
371 ContactsCommon updatedContact =
372 (ContactsCommon) extractPart(input,
373 client.getCommonPartName(), ContactsCommon.class);
374 Assert.assertNotNull(updatedContact);
376 Assert.assertEquals(updatedContact.getEmail(),
378 "Data in updated object did not match submitted data.");
383 // Placeholders until the three tests below can be uncommented.
384 // See Issue CSPACE-401.
386 public void updateWithEmptyEntityBody(String testName) throws Exception{
389 public void updateWithMalformedXml(String testName) throws Exception {
392 public void updateWithWrongXmlSchema(String testName) throws Exception {
397 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
398 dependsOnMethods = {"create", "update", "testSubmitRequest"})
399 public void updateWithEmptyEntityBody(String testName) throws Exception {
402 setupUpdateWithEmptyEntityBody(testName);
404 // Submit the request to the service and store the response.
405 String method = REQUEST_TYPE.httpMethodName();
406 String url = getResourceURL(knownResourceId);
407 String mediaType = MediaType.APPLICATION_XML;
408 final String entity = "";
409 int statusCode = submitRequest(method, url, mediaType, entity);
411 // Check the status code of the response: does it match
412 // the expected response(s)?
413 if(logger.isDebugEnabled()){
414 logger.debug(testName + ": url=" + url +
415 " status=" + statusCode);
417 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
418 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
419 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
423 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
424 dependsOnMethods = {"create", "update", "testSubmitRequest"})
425 public void updateWithMalformedXml(String testName) throws Exception {
428 setupUpdateWithMalformedXml(testName);
430 // Submit the request to the service and store the response.
431 String method = REQUEST_TYPE.httpMethodName();
432 String url = getResourceURL(knownResourceId);
433 String mediaType = MediaType.APPLICATION_XML;
434 final String entity = MALFORMED_XML_DATA;
435 int statusCode = submitRequest(method, url, mediaType, entity);
437 // Check the status code of the response: does it match
438 // the expected response(s)?
439 if(logger.isDebugEnabled()){
440 logger.debug(testName + ": url=" + url +
441 " status=" + statusCode);
443 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
444 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
445 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
449 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
450 dependsOnMethods = {"create", "update", "testSubmitRequest"})
451 public void updateWithWrongXmlSchema(String testName) throws Exception {
454 setupUpdateWithWrongXmlSchema(testName);
456 // Submit the request to the service and store the response.
457 String method = REQUEST_TYPE.httpMethodName();
458 String url = getResourceURL(knownResourceId);
459 String mediaType = MediaType.APPLICATION_XML;
460 final String entity = WRONG_XML_SCHEMA_DATA;
461 int statusCode = submitRequest(method, url, mediaType, entity);
463 // Check the status code of the response: does it match
464 // the expected response(s)?
465 if(logger.isDebugEnabled()){
466 logger.debug(testName + ": url=" + url +
467 " status=" + statusCode);
469 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
470 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
471 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
476 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
477 dependsOnMethods = {"update", "testSubmitRequest"})
478 public void updateNonExistent(String testName) throws Exception {
481 setupUpdateNonExistent(testName);
483 // Submit the request to the service and store the response.
484 // Note: The ID used in this 'create' call may be arbitrary.
485 // The only relevant ID may be the one used in update(), below.
487 // The only relevant ID may be the one used in update(), below.
488 MultipartOutput multipart =
489 ContactClientUtils.createContactInstance(NON_EXISTENT_ID, client.getCommonPartName());
490 ClientResponse<MultipartInput> res =
491 client.update(NON_EXISTENT_ID, multipart);
492 int statusCode = res.getStatus();
494 // Check the status code of the response: does it match
495 // the expected response(s)?
496 if(logger.isDebugEnabled()){
497 logger.debug(testName + ": status = " + statusCode);
499 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
500 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
501 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
504 // ---------------------------------------------------------------
505 // CRUD tests : DELETE tests
506 // ---------------------------------------------------------------
509 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
510 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
511 public void delete(String testName) throws Exception {
514 setupDelete(testName);
516 // Submit the request to the service and store the response.
517 ClientResponse<Response> res = client.delete(knownResourceId);
518 int statusCode = res.getStatus();
520 // Check the status code of the response: does it match
521 // the expected response(s)?
522 if(logger.isDebugEnabled()){
523 logger.debug(testName + ": status = " + statusCode);
525 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
526 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
527 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
532 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
533 dependsOnMethods = {"delete"})
534 public void deleteNonExistent(String testName) throws Exception {
537 setupDeleteNonExistent(testName);
539 // Submit the request to the service and store the response.
540 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
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);
553 // ---------------------------------------------------------------
554 // Utility tests : tests of code used in tests above
555 // ---------------------------------------------------------------
557 * Tests the code for manually submitting data that is used by several
558 * of the methods above.
560 @Test(dependsOnMethods = {"create", "read"})
561 public void testSubmitRequest() {
563 // Expected status code: 200 OK
564 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
566 // Submit the request to the service and store the response.
567 String method = ServiceRequestType.READ.httpMethodName();
568 String url = getResourceURL(knownResourceId);
569 int statusCode = submitRequest(method, url);
571 // Check the status code of the response: does it match
572 // the expected response(s)?
573 if(logger.isDebugEnabled()){
574 logger.debug("testSubmitRequest: url=" + url +
575 " status=" + statusCode);
577 Assert.assertEquals(statusCode, EXPECTED_STATUS);
581 // ---------------------------------------------------------------
582 // Cleanup of resources created during testing
583 // ---------------------------------------------------------------
586 * Deletes all resources created by tests, after all tests have been run.
588 * This cleanup method will always be run, even if one or more tests fail.
589 * For this reason, it attempts to remove all resources created
590 * at any point during testing, even if some of those resources
591 * may be expected to be deleted by certain tests.
593 @AfterClass(alwaysRun=true)
594 public void cleanUp() {
595 if (logger.isDebugEnabled()) {
596 logger.debug("Cleaning up temporary resources created for testing ...");
598 for (String resourceId : allResourceIdsCreated) {
599 // Note: Any non-success responses are ignored and not reported.
600 ClientResponse<Response> res = client.delete(resourceId);
604 // ---------------------------------------------------------------
605 // Utility methods used by tests above
606 // ---------------------------------------------------------------
608 public String getServicePathComponent() {
609 return SERVICE_PATH_COMPONENT;