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 String CLASS_NAME = ContactServiceTest.class.getName();
59 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
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 if (logger.isDebugEnabled()) {
91 logger.debug(testBanner(testName, CLASS_NAME));
93 // Perform setup, such as initializing the type of service request
94 // (e.g. CREATE, DELETE), its valid and expected status codes, and
95 // its associated HTTP method name (e.g. POST, DELETE).
98 // Submit the request to the service and store the response.
99 ContactClient client = new ContactClient();
100 String identifier = createIdentifier();
101 MultipartOutput multipart =
102 ContactClientUtils.createContactInstance(identifier, client.getCommonPartName());
103 ClientResponse<Response> res = client.create(multipart);
105 int statusCode = res.getStatus();
107 // Check the status code of the response: does it match
108 // the expected response(s)?
111 // Does it fall within the set of valid status codes?
112 // Does it exactly match the expected status code?
113 if(logger.isDebugEnabled()){
114 logger.debug(testName + ": status = " + statusCode);
116 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
117 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
118 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
120 // Store the ID returned from the first resource created
121 // for additional tests below.
122 if (knownResourceId == null){
123 knownResourceId = extractId(res);
124 if (logger.isDebugEnabled()) {
125 logger.debug(testName + ": knownResourceId=" + knownResourceId);
129 // Store the IDs from every resource created by tests,
130 // so they can be deleted after tests have been run.
131 allResourceIdsCreated.add(extractId(res));
135 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
136 dependsOnMethods = {"create"})
137 public void createList(String testName) throws Exception {
138 for(int i = 0; i < 3; i++){
144 // Placeholders until the three tests below can be uncommented.
145 // See Issue CSPACE-401.
147 public void createWithEmptyEntityBody(String testName) throws Exception {
148 //Should this really be empty?
152 public void createWithMalformedXml(String testName) throws Exception {
153 //Should this really be empty??
157 public void createWithWrongXmlSchema(String testName) throws Exception {
158 //Should this really be empty??
163 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
164 dependsOnMethods = {"create", "testSubmitRequest"})
165 public void createWithEmptyEntityBody(String testName) throws Exception {
167 if (logger.isDebugEnabled()) {
168 logger.debug(testBanner(testName, CLASS_NAME));
171 setupCreateWithEmptyEntityBody(testName, logger);
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 = "";
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("createWithEmptyEntityBody 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 createWithMalformedXml(String testName) throws Exception {
196 if (logger.isDebugEnabled()) {
197 logger.debug(testBanner(testName, CLASS_NAME));
200 setupCreateWithMalformedXml();
202 // Submit the request to the service and store the response.
203 String method = REQUEST_TYPE.httpMethodName();
204 String url = getServiceRootURL();
205 String mediaType = MediaType.APPLICATION_XML;
206 final String entity = MALFORMED_XML_DATA; // Constant from base class.
207 int statusCode = submitRequest(method, url, mediaType, entity);
209 // Check the status code of the response: does it match
210 // the expected response(s)?
211 if(logger.isDebugEnabled()){
212 logger.debug(testName + ": url=" + url +
213 " status=" + statusCode);
215 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
216 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
217 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
221 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
222 dependsOnMethods = {"create", "testSubmitRequest"})
223 public void createWithWrongXmlSchema(String testName) throws Exception {
225 if (logger.isDebugEnabled()) {
226 logger.debug(testBanner(testName, CLASS_NAME));
229 setupCreateWithWrongXmlSchema(testName, logger);
231 // Submit the request to the service and store the response.
232 String method = REQUEST_TYPE.httpMethodName();
233 String url = getServiceRootURL();
234 String mediaType = MediaType.APPLICATION_XML;
235 final String entity = WRONG_XML_SCHEMA_DATA;
236 int statusCode = submitRequest(method, url, mediaType, entity);
238 // Check the status code of the response: does it match
239 // the expected response(s)?
240 if(logger.isDebugEnabled()){
241 logger.debug(testName + ": url=" + url +
242 " status=" + statusCode);
244 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
245 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
246 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
250 // ---------------------------------------------------------------
251 // CRUD tests : READ tests
252 // ---------------------------------------------------------------
255 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
256 dependsOnMethods = {"create"})
257 public void read(String testName) throws Exception {
259 if (logger.isDebugEnabled()) {
260 logger.debug(testBanner(testName, CLASS_NAME));
265 // Submit the request to the service and store the response.
266 ContactClient client = new ContactClient();
267 ClientResponse<MultipartInput> res = client.read(knownResourceId);
268 int statusCode = res.getStatus();
270 // Check the status code of the response: does it match
271 // the expected response(s)?
272 if(logger.isDebugEnabled()){
273 logger.debug(testName + ": status = " + statusCode);
275 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
276 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
277 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
279 MultipartInput input = (MultipartInput) res.getEntity();
280 ContactsCommon contact = (ContactsCommon) extractPart(input,
281 client.getCommonPartName(), ContactsCommon.class);
282 Assert.assertNotNull(contact);
287 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
288 dependsOnMethods = {"read"})
289 public void readNonExistent(String testName) throws Exception {
291 if (logger.isDebugEnabled()) {
292 logger.debug(testBanner(testName, CLASS_NAME));
295 setupReadNonExistent();
297 // Submit the request to the service and store the response.
298 ContactClient client = new ContactClient();
299 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
300 int statusCode = res.getStatus();
302 // Check the status code of the response: does it match
303 // the expected response(s)?
304 if(logger.isDebugEnabled()){
305 logger.debug(testName + ": status = " + statusCode);
307 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
308 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
309 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
312 // ---------------------------------------------------------------
313 // CRUD tests : READ_LIST tests
314 // ---------------------------------------------------------------
317 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
318 dependsOnMethods = {"read"})
319 public void readList(String testName) throws Exception {
321 if (logger.isDebugEnabled()) {
322 logger.debug(testBanner(testName, CLASS_NAME));
327 // Submit the request to the service and store the response.
328 ContactClient client = new ContactClient();
329 ClientResponse<ContactsCommonList> res = client.readList();
330 ContactsCommonList list = res.getEntity();
331 int statusCode = res.getStatus();
333 // Check the status code of the response: does it match
334 // the expected response(s)?
335 if(logger.isDebugEnabled()){
336 logger.debug(testName + ": status = " + statusCode);
338 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
339 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
340 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
342 // Optionally output additional data about list members for debugging.
343 boolean iterateThroughList = false;
344 if(iterateThroughList && logger.isDebugEnabled()){
345 List<ContactsCommonList.ContactListItem> items =
346 list.getContactListItem();
348 for(ContactsCommonList.ContactListItem item : items){
349 logger.debug(testName + ": list-item[" + i + "] csid=" +
351 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
352 item.getAddressPlace());
353 logger.debug(testName + ": list-item[" + i + "] URI=" +
363 // ---------------------------------------------------------------
364 // CRUD tests : UPDATE tests
365 // ---------------------------------------------------------------
368 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
369 dependsOnMethods = {"read"})
370 public void update(String testName) throws Exception {
372 if (logger.isDebugEnabled()) {
373 logger.debug(testBanner(testName, CLASS_NAME));
378 // Submit the request to the service and store the response.
379 ContactClient client = new ContactClient();
380 ClientResponse<MultipartInput> res =
381 client.read(knownResourceId);
382 if(logger.isDebugEnabled()){
383 logger.debug(testName + ": read status = " + res.getStatus());
385 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
387 if(logger.isDebugEnabled()){
388 logger.debug("got object to update with ID: " + knownResourceId);
390 MultipartInput input = (MultipartInput) res.getEntity();
391 ContactsCommon contact = (ContactsCommon) extractPart(input,
392 client.getCommonPartName(), ContactsCommon.class);
393 Assert.assertNotNull(contact);
395 // Update the content of this resource.
396 contact.setAddressType("updated-" + contact.getAddressType());
397 contact.setAddressPlace("updated-" + contact.getAddressPlace());
398 contact.setEmail("updated-" + contact.getEmail());
399 if(logger.isDebugEnabled()){
400 logger.debug("to be updated object");
401 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
403 // Submit the request to the service and store the response.
404 MultipartOutput output = new MultipartOutput();
405 OutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
406 commonPart.getHeaders().add("label", client.getCommonPartName());
408 res = client.update(knownResourceId, output);
409 int statusCode = res.getStatus();
410 // Check the status code of the response: does it match the expected response(s)?
411 if(logger.isDebugEnabled()){
412 logger.debug(testName + ": status = " + statusCode);
414 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
415 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
416 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
419 input = (MultipartInput) res.getEntity();
420 ContactsCommon updatedContact =
421 (ContactsCommon) extractPart(input,
422 client.getCommonPartName(), ContactsCommon.class);
423 Assert.assertNotNull(updatedContact);
425 Assert.assertEquals(updatedContact.getEmail(),
427 "Data in updated object did not match submitted data.");
432 // Placeholders until the three tests below can be uncommented.
433 // See Issue CSPACE-401.
435 public void updateWithEmptyEntityBody(String testName) throws Exception {
436 //Should this really be empty??
440 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
443 public void updateWithMalformedXml(String testName) throws Exception {
444 //Should this really be empty??
448 public void updateWithWrongXmlSchema(String testName) throws Exception {
449 //Should this really be empty??
454 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
455 dependsOnMethods = {"create", "update", "testSubmitRequest"})
456 public void updateWithEmptyEntityBody(String testName) throws Exception {
458 if (logger.isDebugEnabled()) {
459 logger.debug(testBanner(testName, CLASS_NAME));
462 setupUpdateWithEmptyEntityBody();
464 // Submit the request to the service and store the response.
465 String method = REQUEST_TYPE.httpMethodName();
466 String url = getResourceURL(knownResourceId);
467 String mediaType = MediaType.APPLICATION_XML;
468 final String entity = "";
469 int statusCode = submitRequest(method, url, mediaType, entity);
471 // Check the status code of the response: does it match
472 // the expected response(s)?
473 if(logger.isDebugEnabled()){
474 logger.debug(testName + ": url=" + url +
475 " status=" + statusCode);
477 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
478 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
479 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
483 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
484 dependsOnMethods = {"create", "update", "testSubmitRequest"})
485 public void updateWithMalformedXml(String testName) throws Exception {
487 if (logger.isDebugEnabled()) {
488 logger.debug(testBanner(testName, CLASS_NAME));
491 setupUpdateWithMalformedXml();
493 // Submit the request to the service and store the response.
494 String method = REQUEST_TYPE.httpMethodName();
495 String url = getResourceURL(knownResourceId);
496 String mediaType = MediaType.APPLICATION_XML;
497 final String entity = MALFORMED_XML_DATA;
498 int statusCode = submitRequest(method, url, mediaType, entity);
500 // Check the status code of the response: does it match
501 // the expected response(s)?
502 if(logger.isDebugEnabled()){
503 logger.debug(testName + ": url=" + url +
504 " status=" + statusCode);
506 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
507 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
508 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
512 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
513 dependsOnMethods = {"create", "update", "testSubmitRequest"})
514 public void updateWithWrongXmlSchema(String testName) throws Exception {
516 if (logger.isDebugEnabled()) {
517 logger.debug(testBanner(testName, CLASS_NAME));
520 setupUpdateWithWrongXmlSchema(testName, logger);
522 // Submit the request to the service and store the response.
523 String method = REQUEST_TYPE.httpMethodName();
524 String url = getResourceURL(knownResourceId);
525 String mediaType = MediaType.APPLICATION_XML;
526 final String entity = WRONG_XML_SCHEMA_DATA;
527 int statusCode = submitRequest(method, url, mediaType, entity);
529 // Check the status code of the response: does it match
530 // the expected response(s)?
531 if(logger.isDebugEnabled()){
532 logger.debug(testName + ": url=" + url +
533 " status=" + statusCode);
535 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
536 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
537 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
542 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
543 dependsOnMethods = {"update", "testSubmitRequest"})
544 public void updateNonExistent(String testName) throws Exception {
546 if (logger.isDebugEnabled()) {
547 logger.debug(testBanner(testName, CLASS_NAME));
550 setupUpdateNonExistent();
552 // Submit the request to the service and store the response.
553 // Note: The ID used in this 'create' call may be arbitrary.
554 // The only relevant ID may be the one used in update(), below.
555 ContactClient client = new ContactClient();
556 MultipartOutput multipart =
557 ContactClientUtils.createContactInstance(NON_EXISTENT_ID, client.getCommonPartName());
558 ClientResponse<MultipartInput> res =
559 client.update(NON_EXISTENT_ID, multipart);
560 int statusCode = res.getStatus();
562 // Check the status code of the response: does it match
563 // the expected response(s)?
564 if(logger.isDebugEnabled()){
565 logger.debug(testName + ": status = " + statusCode);
567 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
568 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
569 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
572 // ---------------------------------------------------------------
573 // CRUD tests : DELETE tests
574 // ---------------------------------------------------------------
577 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
578 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
579 public void delete(String testName) throws Exception {
581 if (logger.isDebugEnabled()) {
582 logger.debug(testBanner(testName, CLASS_NAME));
587 // Submit the request to the service and store the response.
588 ContactClient client = new ContactClient();
589 ClientResponse<Response> res = client.delete(knownResourceId);
590 int statusCode = res.getStatus();
592 // Check the status code of the response: does it match
593 // the expected response(s)?
594 if(logger.isDebugEnabled()){
595 logger.debug(testName + ": status = " + statusCode);
597 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
598 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
599 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
604 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
605 dependsOnMethods = {"delete"})
606 public void deleteNonExistent(String testName) throws Exception {
608 if (logger.isDebugEnabled()) {
609 logger.debug(testBanner(testName, CLASS_NAME));
612 setupDeleteNonExistent();
614 // Submit the request to the service and store the response.
615 ContactClient client = new ContactClient();
616 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
617 int statusCode = res.getStatus();
619 // Check the status code of the response: does it match
620 // the expected response(s)?
621 if(logger.isDebugEnabled()){
622 logger.debug(testName + ": status = " + statusCode);
624 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
625 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
626 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
629 // ---------------------------------------------------------------
630 // Utility tests : tests of code used in tests above
631 // ---------------------------------------------------------------
633 * Tests the code for manually submitting data that is used by several
634 * of the methods above.
636 @Test(dependsOnMethods = {"create", "read"})
637 public void testSubmitRequest() {
639 // Expected status code: 200 OK
640 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
642 // Submit the request to the service and store the response.
643 String method = ServiceRequestType.READ.httpMethodName();
644 String url = getResourceURL(knownResourceId);
645 int statusCode = submitRequest(method, url);
647 // Check the status code of the response: does it match
648 // the expected response(s)?
649 if(logger.isDebugEnabled()){
650 logger.debug("testSubmitRequest: url=" + url +
651 " status=" + statusCode);
653 Assert.assertEquals(statusCode, EXPECTED_STATUS);
657 // ---------------------------------------------------------------
658 // Utility methods used by tests above
659 // ---------------------------------------------------------------
661 public String getServicePathComponent() {
662 return SERVICE_PATH_COMPONENT;