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.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.collectionspace.services.client.CollectionSpaceClient;
30 import org.collectionspace.services.client.ContactClient;
31 import org.collectionspace.services.client.ContactClientUtils;
32 import org.collectionspace.services.client.PayloadOutputPart;
33 import org.collectionspace.services.client.PoxPayloadIn;
34 import org.collectionspace.services.client.PoxPayloadOut;
35 import org.collectionspace.services.contact.ContactsCommon;
36 import org.collectionspace.services.contact.ContactsCommonList;
37 import org.collectionspace.services.jaxb.AbstractCommonList;
39 import org.jboss.resteasy.client.ClientResponse;
40 //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 {
57 private final String CLASS_NAME = ContactServiceTest.class.getName();
58 private final Logger logger = LoggerFactory.getLogger(ContactServiceTest.class);
60 // Instance variables specific to this test.
61 // final String SERVICE_PATH_COMPONENT = "contacts";
63 private String knownResourceId = null;
66 public String getServicePathComponent() {
67 return ContactClient.SERVICE_PATH_COMPONENT;
71 protected String getServiceName() {
72 return ContactClient.SERVICE_NAME;
76 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
79 protected CollectionSpaceClient getClientInstance() {
80 return new ContactClient();
84 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
87 protected AbstractCommonList getAbstractCommonList(
88 ClientResponse<AbstractCommonList> response) {
89 return response.getEntity(ContactsCommonList.class);
92 // ---------------------------------------------------------------
93 // CRUD tests : CREATE tests
94 // ---------------------------------------------------------------
97 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
98 public void create(String testName) throws Exception {
100 if (logger.isDebugEnabled()) {
101 logger.debug(testBanner(testName, CLASS_NAME));
103 // Perform setup, such as initializing the type of service request
104 // (e.g. CREATE, DELETE), its valid and expected status codes, and
105 // its associated HTTP method name (e.g. POST, DELETE).
108 // Submit the request to the service and store the response.
109 ContactClient client = new ContactClient();
110 String identifier = createIdentifier();
111 PoxPayloadOut multipart =
112 ContactClientUtils.createContactInstance(identifier, client.getCommonPartName());
113 ClientResponse<Response> res = client.create(multipart);
115 int statusCode = res.getStatus();
117 // Check the status code of the response: does it match
118 // the expected response(s)?
121 // Does it fall within the set of valid status codes?
122 // Does it exactly match the expected status code?
123 if(logger.isDebugEnabled()){
124 logger.debug(testName + ": status = " + statusCode);
126 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
127 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
128 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
130 // Store the ID returned from the first resource created
131 // for additional tests below.
132 if (knownResourceId == null){
133 knownResourceId = extractId(res);
134 if (logger.isDebugEnabled()) {
135 logger.debug(testName + ": knownResourceId=" + knownResourceId);
139 // Store the IDs from every resource created by tests,
140 // so they can be deleted after tests have been run.
141 allResourceIdsCreated.add(extractId(res));
145 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
146 dependsOnMethods = {"create"})
147 public void createList(String testName) throws Exception {
148 for(int i = 0; i < 3; i++){
154 // Placeholders until the three tests below can be uncommented.
155 // See Issue CSPACE-401.
157 public void createWithEmptyEntityBody(String testName) throws Exception {
158 //Should this really be empty?
162 public void createWithMalformedXml(String testName) throws Exception {
163 //Should this really be empty??
167 public void createWithWrongXmlSchema(String testName) throws Exception {
168 //Should this really be empty??
173 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
174 dependsOnMethods = {"create", "testSubmitRequest"})
175 public void createWithEmptyEntityBody(String testName) throws Exception {
177 if (logger.isDebugEnabled()) {
178 logger.debug(testBanner(testName, CLASS_NAME));
181 setupCreateWithEmptyEntityBody(testName, logger);
183 // Submit the request to the service and store the response.
184 String method = REQUEST_TYPE.httpMethodName();
185 String url = getServiceRootURL();
186 String mediaType = MediaType.APPLICATION_XML;
187 final String entity = "";
188 int statusCode = submitRequest(method, url, mediaType, entity);
190 // Check the status code of the response: does it match
191 // the expected response(s)?
192 if(logger.isDebugEnabled()){
193 logger.debug("createWithEmptyEntityBody url=" + url +
194 " status=" + statusCode);
196 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
197 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
198 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
202 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
203 dependsOnMethods = {"create", "testSubmitRequest"})
204 public void createWithMalformedXml(String testName) throws Exception {
206 if (logger.isDebugEnabled()) {
207 logger.debug(testBanner(testName, CLASS_NAME));
210 setupCreateWithMalformedXml();
212 // Submit the request to the service and store the response.
213 String method = REQUEST_TYPE.httpMethodName();
214 String url = getServiceRootURL();
215 String mediaType = MediaType.APPLICATION_XML;
216 final String entity = MALFORMED_XML_DATA; // Constant from base class.
217 int statusCode = submitRequest(method, url, mediaType, entity);
219 // Check the status code of the response: does it match
220 // the expected response(s)?
221 if(logger.isDebugEnabled()){
222 logger.debug(testName + ": url=" + url +
223 " status=" + statusCode);
225 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
226 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
227 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
231 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
232 dependsOnMethods = {"create", "testSubmitRequest"})
233 public void createWithWrongXmlSchema(String testName) throws Exception {
235 if (logger.isDebugEnabled()) {
236 logger.debug(testBanner(testName, CLASS_NAME));
239 setupCreateWithWrongXmlSchema(testName, logger);
241 // Submit the request to the service and store the response.
242 String method = REQUEST_TYPE.httpMethodName();
243 String url = getServiceRootURL();
244 String mediaType = MediaType.APPLICATION_XML;
245 final String entity = WRONG_XML_SCHEMA_DATA;
246 int statusCode = submitRequest(method, url, mediaType, entity);
248 // Check the status code of the response: does it match
249 // the expected response(s)?
250 if(logger.isDebugEnabled()){
251 logger.debug(testName + ": url=" + url +
252 " status=" + statusCode);
254 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
255 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
256 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
260 // ---------------------------------------------------------------
261 // CRUD tests : READ tests
262 // ---------------------------------------------------------------
265 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
266 dependsOnMethods = {"create"})
267 public void read(String testName) throws Exception {
269 if (logger.isDebugEnabled()) {
270 logger.debug(testBanner(testName, CLASS_NAME));
275 // Submit the request to the service and store the response.
276 ContactClient client = new ContactClient();
277 ClientResponse<String> res = client.read(knownResourceId);
278 int statusCode = res.getStatus();
280 // Check the status code of the response: does it match
281 // the expected response(s)?
282 if(logger.isDebugEnabled()){
283 logger.debug(testName + ": status = " + statusCode);
285 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
286 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
287 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
289 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
290 ContactsCommon contact = (ContactsCommon) extractPart(input,
291 client.getCommonPartName(), ContactsCommon.class);
292 Assert.assertNotNull(contact);
297 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
298 dependsOnMethods = {"read"})
299 public void readNonExistent(String testName) throws Exception {
301 if (logger.isDebugEnabled()) {
302 logger.debug(testBanner(testName, CLASS_NAME));
305 setupReadNonExistent();
307 // Submit the request to the service and store the response.
308 ContactClient client = new ContactClient();
309 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
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);
322 // ---------------------------------------------------------------
323 // CRUD tests : READ_LIST tests
324 // ---------------------------------------------------------------
327 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
328 dependsOnMethods = {"read"})
329 public void readList(String testName) throws Exception {
331 if (logger.isDebugEnabled()) {
332 logger.debug(testBanner(testName, CLASS_NAME));
337 // Submit the request to the service and store the response.
338 ContactClient client = new ContactClient();
339 ClientResponse<ContactsCommonList> res = client.readList();
340 ContactsCommonList list = res.getEntity();
341 int statusCode = res.getStatus();
343 // Check the status code of the response: does it match
344 // the expected response(s)?
345 if(logger.isDebugEnabled()){
346 logger.debug(testName + ": status = " + statusCode);
348 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
349 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
350 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
352 // Optionally output additional data about list members for debugging.
353 boolean iterateThroughList = false;
354 if(iterateThroughList && logger.isDebugEnabled()){
355 List<ContactsCommonList.ContactListItem> items =
356 list.getContactListItem();
358 for(ContactsCommonList.ContactListItem item : items){
359 logger.debug(testName + ": list-item[" + i + "] csid=" +
361 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
362 item.getAddressPlace());
363 logger.debug(testName + ": list-item[" + i + "] URI=" +
373 // ---------------------------------------------------------------
374 // CRUD tests : UPDATE tests
375 // ---------------------------------------------------------------
378 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
379 dependsOnMethods = {"read"})
380 public void update(String testName) throws Exception {
382 if (logger.isDebugEnabled()) {
383 logger.debug(testBanner(testName, CLASS_NAME));
388 // Submit the request to the service and store the response.
389 ContactClient client = new ContactClient();
390 ClientResponse<String> res = client.read(knownResourceId);
391 if(logger.isDebugEnabled()){
392 logger.debug(testName + ": read status = " + res.getStatus());
394 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
396 if(logger.isDebugEnabled()){
397 logger.debug("got object to update with ID: " + knownResourceId);
399 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
400 ContactsCommon contact = (ContactsCommon) extractPart(input,
401 client.getCommonPartName(), ContactsCommon.class);
402 Assert.assertNotNull(contact);
404 // Update the content of this resource.
405 contact.setAddressType("updated-" + contact.getAddressType());
406 contact.setAddressPlace("updated-" + contact.getAddressPlace());
407 contact.setEmail("updated-" + contact.getEmail());
408 if(logger.isDebugEnabled()){
409 logger.debug("to be updated object");
410 logger.debug(objectAsXmlString(contact, ContactsCommon.class));
412 // Submit the request to the service and store the response.
413 PoxPayloadOut output = new PoxPayloadOut(ContactClient.SERVICE_PAYLOAD_NAME);
414 PayloadOutputPart commonPart = output.addPart(contact, MediaType.APPLICATION_XML_TYPE);
415 commonPart.setLabel(client.getCommonPartName());
417 res = client.update(knownResourceId, output);
418 int statusCode = res.getStatus();
419 // Check the status code of the response: does it match the expected response(s)?
420 if(logger.isDebugEnabled()){
421 logger.debug(testName + ": status = " + statusCode);
423 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
424 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
425 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
428 input = new PoxPayloadIn(res.getEntity());
429 ContactsCommon updatedContact =
430 (ContactsCommon) extractPart(input,
431 client.getCommonPartName(), ContactsCommon.class);
432 Assert.assertNotNull(updatedContact);
434 Assert.assertEquals(updatedContact.getEmail(),
436 "Data in updated object did not match submitted data.");
441 // Placeholders until the three tests below can be uncommented.
442 // See Issue CSPACE-401.
444 public void updateWithEmptyEntityBody(String testName) throws Exception {
445 //Should this really be empty??
449 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
452 public void updateWithMalformedXml(String testName) throws Exception {
453 //Should this really be empty??
457 public void updateWithWrongXmlSchema(String testName) throws Exception {
458 //Should this really be empty??
463 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
464 dependsOnMethods = {"create", "update", "testSubmitRequest"})
465 public void updateWithEmptyEntityBody(String testName) throws Exception {
467 if (logger.isDebugEnabled()) {
468 logger.debug(testBanner(testName, CLASS_NAME));
471 setupUpdateWithEmptyEntityBody();
473 // Submit the request to the service and store the response.
474 String method = REQUEST_TYPE.httpMethodName();
475 String url = getResourceURL(knownResourceId);
476 String mediaType = MediaType.APPLICATION_XML;
477 final String entity = "";
478 int statusCode = submitRequest(method, url, mediaType, entity);
480 // Check the status code of the response: does it match
481 // the expected response(s)?
482 if(logger.isDebugEnabled()){
483 logger.debug(testName + ": url=" + url +
484 " status=" + statusCode);
486 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
487 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
488 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
492 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
493 dependsOnMethods = {"create", "update", "testSubmitRequest"})
494 public void updateWithMalformedXml(String testName) throws Exception {
496 if (logger.isDebugEnabled()) {
497 logger.debug(testBanner(testName, CLASS_NAME));
500 setupUpdateWithMalformedXml();
502 // Submit the request to the service and store the response.
503 String method = REQUEST_TYPE.httpMethodName();
504 String url = getResourceURL(knownResourceId);
505 String mediaType = MediaType.APPLICATION_XML;
506 final String entity = MALFORMED_XML_DATA;
507 int statusCode = submitRequest(method, url, mediaType, entity);
509 // Check the status code of the response: does it match
510 // the expected response(s)?
511 if(logger.isDebugEnabled()){
512 logger.debug(testName + ": url=" + url +
513 " status=" + statusCode);
515 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
516 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
517 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
521 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
522 dependsOnMethods = {"create", "update", "testSubmitRequest"})
523 public void updateWithWrongXmlSchema(String testName) throws Exception {
525 if (logger.isDebugEnabled()) {
526 logger.debug(testBanner(testName, CLASS_NAME));
529 setupUpdateWithWrongXmlSchema(testName, logger);
531 // Submit the request to the service and store the response.
532 String method = REQUEST_TYPE.httpMethodName();
533 String url = getResourceURL(knownResourceId);
534 String mediaType = MediaType.APPLICATION_XML;
535 final String entity = WRONG_XML_SCHEMA_DATA;
536 int statusCode = submitRequest(method, url, mediaType, entity);
538 // Check the status code of the response: does it match
539 // the expected response(s)?
540 if(logger.isDebugEnabled()){
541 logger.debug(testName + ": url=" + url +
542 " status=" + statusCode);
544 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
545 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
546 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
551 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
552 dependsOnMethods = {"update", "testSubmitRequest"})
553 public void updateNonExistent(String testName) throws Exception {
555 if (logger.isDebugEnabled()) {
556 logger.debug(testBanner(testName, CLASS_NAME));
559 setupUpdateNonExistent();
561 // Submit the request to the service and store the response.
562 // Note: The ID used in this 'create' call may be arbitrary.
563 // The only relevant ID may be the one used in update(), below.
564 ContactClient client = new ContactClient();
565 PoxPayloadOut multipart =
566 ContactClientUtils.createContactInstance(NON_EXISTENT_ID, client.getCommonPartName());
567 ClientResponse<String> res =
568 client.update(NON_EXISTENT_ID, multipart);
569 int statusCode = res.getStatus();
571 // Check the status code of the response: does it match
572 // the expected response(s)?
573 if(logger.isDebugEnabled()){
574 logger.debug(testName + ": status = " + statusCode);
576 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
577 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
578 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
581 // ---------------------------------------------------------------
582 // CRUD tests : DELETE tests
583 // ---------------------------------------------------------------
586 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
587 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
588 public void delete(String testName) throws Exception {
590 if (logger.isDebugEnabled()) {
591 logger.debug(testBanner(testName, CLASS_NAME));
596 // Submit the request to the service and store the response.
597 ContactClient client = new ContactClient();
598 ClientResponse<Response> res = client.delete(knownResourceId);
599 int statusCode = res.getStatus();
601 // Check the status code of the response: does it match
602 // the expected response(s)?
603 if(logger.isDebugEnabled()){
604 logger.debug(testName + ": status = " + statusCode);
606 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
607 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
608 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
613 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
614 dependsOnMethods = {"delete"})
615 public void deleteNonExistent(String testName) throws Exception {
617 if (logger.isDebugEnabled()) {
618 logger.debug(testBanner(testName, CLASS_NAME));
621 setupDeleteNonExistent();
623 // Submit the request to the service and store the response.
624 ContactClient client = new ContactClient();
625 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
626 int statusCode = res.getStatus();
628 // Check the status code of the response: does it match
629 // the expected response(s)?
630 if(logger.isDebugEnabled()){
631 logger.debug(testName + ": status = " + statusCode);
633 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
634 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
635 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
638 // ---------------------------------------------------------------
639 // Utility tests : tests of code used in tests above
640 // ---------------------------------------------------------------
642 * Tests the code for manually submitting data that is used by several
643 * of the methods above.
645 @Test(dependsOnMethods = {"create", "read"})
646 public void testSubmitRequest() {
648 // Expected status code: 200 OK
649 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
651 // Submit the request to the service and store the response.
652 String method = ServiceRequestType.READ.httpMethodName();
653 String url = getResourceURL(knownResourceId);
654 int statusCode = submitRequest(method, url);
656 // Check the status code of the response: does it match
657 // the expected response(s)?
658 if(logger.isDebugEnabled()){
659 logger.debug("testSubmitRequest: url=" + url +
660 " status=" + statusCode);
662 Assert.assertEquals(statusCode, EXPECTED_STATUS);