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;
66 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
69 protected CollectionSpaceClient getClientInstance() {
70 return new ContactClient();
74 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
77 protected AbstractCommonList getAbstractCommonList(
78 ClientResponse<AbstractCommonList> response) {
79 return response.getEntity(ContactsCommonList.class);
82 // ---------------------------------------------------------------
83 // CRUD tests : CREATE tests
84 // ---------------------------------------------------------------
87 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
88 public void create(String testName) throws Exception {
90 // Perform setup, such as initializing the type of service request
91 // (e.g. CREATE, DELETE), its valid and expected status codes, and
92 // its associated HTTP method name (e.g. POST, DELETE).
93 setupCreate(testName);
95 // Submit the request to the service and store the response.
96 ContactClient client = new ContactClient();
97 String identifier = createIdentifier();
98 MultipartOutput multipart =
99 ContactClientUtils.createContactInstance(identifier, client.getCommonPartName());
100 ClientResponse<Response> res = client.create(multipart);
102 int statusCode = res.getStatus();
104 // Check the status code of the response: does it match
105 // the expected response(s)?
108 // Does it fall within the set of valid status codes?
109 // Does it exactly match the expected status code?
110 if(logger.isDebugEnabled()){
111 logger.debug(testName + ": status = " + statusCode);
113 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
114 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
115 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
117 // Store the ID returned from the first resource created
118 // for additional tests below.
119 if (knownResourceId == null){
120 knownResourceId = extractId(res);
121 if (logger.isDebugEnabled()) {
122 logger.debug(testName + ": knownResourceId=" + knownResourceId);
126 // Store the IDs from every resource created by tests,
127 // so they can be deleted after tests have been run.
128 allResourceIdsCreated.add(extractId(res));
132 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
133 dependsOnMethods = {"create"})
134 public void createList(String testName) throws Exception {
135 for(int i = 0; i < 3; i++){
141 // Placeholders until the three tests below can be uncommented.
142 // See Issue CSPACE-401.
144 public void createWithEmptyEntityBody(String testName) throws Exception {
145 //Should this really be empty?
149 public void createWithMalformedXml(String testName) throws Exception {
150 //Should this really be empty??
154 public void createWithWrongXmlSchema(String testName) throws Exception {
155 //Should this really be empty??
160 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
161 dependsOnMethods = {"create", "testSubmitRequest"})
162 public void createWithEmptyEntityBody(String testName) throws Exception {
165 setupCreateWithEmptyEntityBody(testName);
167 // Submit the request to the service and store the response.
168 String method = REQUEST_TYPE.httpMethodName();
169 String url = getServiceRootURL();
170 String mediaType = MediaType.APPLICATION_XML;
171 final String entity = "";
172 int statusCode = submitRequest(method, url, mediaType, entity);
174 // Check the status code of the response: does it match
175 // the expected response(s)?
176 if(logger.isDebugEnabled()){
177 logger.debug("createWithEmptyEntityBody url=" + url +
178 " status=" + statusCode);
180 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
181 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
182 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
186 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
187 dependsOnMethods = {"create", "testSubmitRequest"})
188 public void createWithMalformedXml(String testName) throws Exception {
191 setupCreateWithMalformedXml(testName);
193 // Submit the request to the service and store the response.
194 String method = REQUEST_TYPE.httpMethodName();
195 String url = getServiceRootURL();
196 String mediaType = MediaType.APPLICATION_XML;
197 final String entity = MALFORMED_XML_DATA; // Constant from base class.
198 int statusCode = submitRequest(method, url, mediaType, entity);
200 // Check the status code of the response: does it match
201 // the expected response(s)?
202 if(logger.isDebugEnabled()){
203 logger.debug(testName + ": url=" + url +
204 " status=" + statusCode);
206 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
207 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
208 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
212 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
213 dependsOnMethods = {"create", "testSubmitRequest"})
214 public void createWithWrongXmlSchema(String testName) throws Exception {
217 setupCreateWithWrongXmlSchema(testName);
219 // Submit the request to the service and store the response.
220 String method = REQUEST_TYPE.httpMethodName();
221 String url = getServiceRootURL();
222 String mediaType = MediaType.APPLICATION_XML;
223 final String entity = WRONG_XML_SCHEMA_DATA;
224 int statusCode = submitRequest(method, url, mediaType, entity);
226 // Check the status code of the response: does it match
227 // the expected response(s)?
228 if(logger.isDebugEnabled()){
229 logger.debug(testName + ": url=" + url +
230 " status=" + statusCode);
232 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
233 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
234 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
238 // ---------------------------------------------------------------
239 // CRUD tests : READ tests
240 // ---------------------------------------------------------------
243 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
244 dependsOnMethods = {"create"})
245 public void read(String testName) throws Exception {
250 // Submit the request to the service and store the response.
251 ContactClient client = new ContactClient();
252 ClientResponse<MultipartInput> res = client.read(knownResourceId);
253 int statusCode = res.getStatus();
255 // Check the status code of the response: does it match
256 // the expected response(s)?
257 if(logger.isDebugEnabled()){
258 logger.debug(testName + ": status = " + statusCode);
260 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
261 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
262 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
264 MultipartInput input = (MultipartInput) res.getEntity();
265 ContactsCommon contact = (ContactsCommon) extractPart(input,
266 client.getCommonPartName(), ContactsCommon.class);
267 Assert.assertNotNull(contact);
272 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
273 dependsOnMethods = {"read"})
274 public void readNonExistent(String testName) throws Exception {
277 setupReadNonExistent(testName);
279 // Submit the request to the service and store the response.
280 ContactClient client = new ContactClient();
281 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
282 int statusCode = res.getStatus();
284 // Check the status code of the response: does it match
285 // the expected response(s)?
286 if(logger.isDebugEnabled()){
287 logger.debug(testName + ": status = " + statusCode);
289 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
290 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
291 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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 {
304 setupReadList(testName);
306 // Submit the request to the service and store the response.
307 ContactClient client = new ContactClient();
308 ClientResponse<ContactsCommonList> res = client.readList();
309 ContactsCommonList list = res.getEntity();
310 int statusCode = res.getStatus();
312 // Check the status code of the response: does it match
313 // the expected response(s)?
314 if(logger.isDebugEnabled()){
315 logger.debug(testName + ": status = " + statusCode);
317 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
318 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
319 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
321 // Optionally output additional data about list members for debugging.
322 boolean iterateThroughList = false;
323 if(iterateThroughList && logger.isDebugEnabled()){
324 List<ContactsCommonList.ContactListItem> items =
325 list.getContactListItem();
327 for(ContactsCommonList.ContactListItem item : items){
328 logger.debug(testName + ": list-item[" + i + "] csid=" +
330 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
331 item.getAddressPlace());
332 logger.debug(testName + ": list-item[" + i + "] URI=" +
342 // ---------------------------------------------------------------
343 // CRUD tests : UPDATE tests
344 // ---------------------------------------------------------------
347 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
348 dependsOnMethods = {"read"})
349 public void update(String testName) throws Exception {
352 setupUpdate(testName);
354 // Submit the request to the service and store the response.
355 ContactClient client = new ContactClient();
356 ClientResponse<MultipartInput> res =
357 client.read(knownResourceId);
358 if(logger.isDebugEnabled()){
359 logger.debug(testName + ": read status = " + res.getStatus());
361 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
363 if(logger.isDebugEnabled()){
364 logger.debug("got object to update with ID: " + knownResourceId);
366 MultipartInput input = (MultipartInput) res.getEntity();
367 ContactsCommon contact = (ContactsCommon) extractPart(input,
368 client.getCommonPartName(), ContactsCommon.class);
369 Assert.assertNotNull(contact);
371 // Update the content of this resource.
372 contact.setAddressType("updated-" + contact.getAddressType());
373 contact.setAddressPlace("updated-" + contact.getAddressPlace());
374 contact.setEmail("updated-" + contact.getEmail());
375 if(logger.isDebugEnabled()){
376 logger.debug("to be updated object");
377 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
379 // Submit the request to the service and store the response.
380 MultipartOutput output = new MultipartOutput();
381 OutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
382 commonPart.getHeaders().add("label", client.getCommonPartName());
384 res = client.update(knownResourceId, output);
385 int statusCode = res.getStatus();
386 // Check the status code of the response: does it match the expected response(s)?
387 if(logger.isDebugEnabled()){
388 logger.debug(testName + ": status = " + statusCode);
390 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
391 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
392 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
395 input = (MultipartInput) res.getEntity();
396 ContactsCommon updatedContact =
397 (ContactsCommon) extractPart(input,
398 client.getCommonPartName(), ContactsCommon.class);
399 Assert.assertNotNull(updatedContact);
401 Assert.assertEquals(updatedContact.getEmail(),
403 "Data in updated object did not match submitted data.");
408 // Placeholders until the three tests below can be uncommented.
409 // See Issue CSPACE-401.
411 public void updateWithEmptyEntityBody(String testName) throws Exception {
412 //Should this really be empty??
416 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
419 public void updateWithMalformedXml(String testName) throws Exception {
420 //Should this really be empty??
424 public void updateWithWrongXmlSchema(String testName) throws Exception {
425 //Should this really be empty??
430 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
431 dependsOnMethods = {"create", "update", "testSubmitRequest"})
432 public void updateWithEmptyEntityBody(String testName) throws Exception {
435 setupUpdateWithEmptyEntityBody(testName);
437 // Submit the request to the service and store the response.
438 String method = REQUEST_TYPE.httpMethodName();
439 String url = getResourceURL(knownResourceId);
440 String mediaType = MediaType.APPLICATION_XML;
441 final String entity = "";
442 int statusCode = submitRequest(method, url, mediaType, entity);
444 // Check the status code of the response: does it match
445 // the expected response(s)?
446 if(logger.isDebugEnabled()){
447 logger.debug(testName + ": url=" + url +
448 " status=" + statusCode);
450 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
451 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
452 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
456 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
457 dependsOnMethods = {"create", "update", "testSubmitRequest"})
458 public void updateWithMalformedXml(String testName) throws Exception {
461 setupUpdateWithMalformedXml(testName);
463 // Submit the request to the service and store the response.
464 String method = REQUEST_TYPE.httpMethodName();
465 String url = getResourceURL(knownResourceId);
466 String mediaType = MediaType.APPLICATION_XML;
467 final String entity = MALFORMED_XML_DATA;
468 int statusCode = submitRequest(method, url, mediaType, entity);
470 // Check the status code of the response: does it match
471 // the expected response(s)?
472 if(logger.isDebugEnabled()){
473 logger.debug(testName + ": url=" + url +
474 " status=" + statusCode);
476 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
477 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
478 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
482 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
483 dependsOnMethods = {"create", "update", "testSubmitRequest"})
484 public void updateWithWrongXmlSchema(String testName) throws Exception {
487 setupUpdateWithWrongXmlSchema(testName);
489 // Submit the request to the service and store the response.
490 String method = REQUEST_TYPE.httpMethodName();
491 String url = getResourceURL(knownResourceId);
492 String mediaType = MediaType.APPLICATION_XML;
493 final String entity = WRONG_XML_SCHEMA_DATA;
494 int statusCode = submitRequest(method, url, mediaType, entity);
496 // Check the status code of the response: does it match
497 // the expected response(s)?
498 if(logger.isDebugEnabled()){
499 logger.debug(testName + ": url=" + url +
500 " status=" + statusCode);
502 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
503 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
504 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
509 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
510 dependsOnMethods = {"update", "testSubmitRequest"})
511 public void updateNonExistent(String testName) throws Exception {
514 setupUpdateNonExistent(testName);
516 // Submit the request to the service and store the response.
517 // Note: The ID used in this 'create' call may be arbitrary.
518 // The only relevant ID may be the one used in update(), below.
519 ContactClient client = new ContactClient();
520 MultipartOutput multipart =
521 ContactClientUtils.createContactInstance(NON_EXISTENT_ID, client.getCommonPartName());
522 ClientResponse<MultipartInput> res =
523 client.update(NON_EXISTENT_ID, multipart);
524 int statusCode = res.getStatus();
526 // Check the status code of the response: does it match
527 // the expected response(s)?
528 if(logger.isDebugEnabled()){
529 logger.debug(testName + ": status = " + statusCode);
531 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
532 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
533 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
536 // ---------------------------------------------------------------
537 // CRUD tests : DELETE tests
538 // ---------------------------------------------------------------
541 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
542 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
543 public void delete(String testName) throws Exception {
546 setupDelete(testName);
548 // Submit the request to the service and store the response.
549 ContactClient client = new ContactClient();
550 ClientResponse<Response> res = client.delete(knownResourceId);
551 int statusCode = res.getStatus();
553 // Check the status code of the response: does it match
554 // the expected response(s)?
555 if(logger.isDebugEnabled()){
556 logger.debug(testName + ": status = " + statusCode);
558 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
559 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
560 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
565 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
566 dependsOnMethods = {"delete"})
567 public void deleteNonExistent(String testName) throws Exception {
570 setupDeleteNonExistent(testName);
572 // Submit the request to the service and store the response.
573 ContactClient client = new ContactClient();
574 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
575 int statusCode = res.getStatus();
577 // Check the status code of the response: does it match
578 // the expected response(s)?
579 if(logger.isDebugEnabled()){
580 logger.debug(testName + ": status = " + statusCode);
582 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
583 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
584 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
587 // ---------------------------------------------------------------
588 // Utility tests : tests of code used in tests above
589 // ---------------------------------------------------------------
591 * Tests the code for manually submitting data that is used by several
592 * of the methods above.
594 @Test(dependsOnMethods = {"create", "read"})
595 public void testSubmitRequest() {
597 // Expected status code: 200 OK
598 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
600 // Submit the request to the service and store the response.
601 String method = ServiceRequestType.READ.httpMethodName();
602 String url = getResourceURL(knownResourceId);
603 int statusCode = submitRequest(method, url);
605 // Check the status code of the response: does it match
606 // the expected response(s)?
607 if(logger.isDebugEnabled()){
608 logger.debug("testSubmitRequest: url=" + url +
609 " status=" + statusCode);
611 Assert.assertEquals(statusCode, EXPECTED_STATUS);
615 // ---------------------------------------------------------------
616 // Utility methods used by tests above
617 // ---------------------------------------------------------------
619 public String getServicePathComponent() {
620 return SERVICE_PATH_COMPONENT;