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.MovementClient;
31 import org.collectionspace.services.jaxb.AbstractCommonList;
32 import org.collectionspace.services.movement.MovementsCommon;
33 import org.collectionspace.services.movement.MovementsCommonList;
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.Test;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * MovementServiceTest, carries out tests against a
48 * deployed and running Movement Service.
50 * $LastChangedRevision: 1327 $
51 * $LastChangedDate: 2010-02-12 10:35:11 -0800 (Fri, 12 Feb 2010) $
53 public class MovementServiceTest extends AbstractServiceTestImpl {
56 private final Logger logger =
57 LoggerFactory.getLogger(MovementServiceTest.class);
59 // Instance variables specific to this test.
60 /** The service path component. */
61 final String SERVICE_PATH_COMPONENT = "movements";
63 /** The known resource id. */
64 private String knownResourceId = null;
67 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
70 protected CollectionSpaceClient getClientInstance() {
71 return new MovementClient();
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(MovementsCommonList.class);
83 // ---------------------------------------------------------------
84 // CRUD tests : CREATE tests
85 // ---------------------------------------------------------------
88 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
91 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
92 public void create(String testName) throws Exception {
94 // Perform setup, such as initializing the type of service request
95 // (e.g. CREATE, DELETE), its valid and expected status codes, and
96 // its associated HTTP method name (e.g. POST, DELETE).
97 setupCreate(testName);
99 // Submit the request to the service and store the response.
100 MovementClient client = new MovementClient();
101 String identifier = createIdentifier();
102 MultipartOutput multipart = createMovementInstance(identifier);
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 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
138 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
139 dependsOnMethods = {"create"})
140 public void createList(String testName) throws Exception {
141 for(int i = 0; i < 3; i++){
147 // Placeholders until the three tests below can be uncommented.
148 // See Issue CSPACE-401.
150 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
153 public void createWithEmptyEntityBody(String testName) throws Exception {
154 //Should this really be empty?
158 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
161 public void createWithMalformedXml(String testName) throws Exception {
162 //Should this really be empty?
166 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
169 public void createWithWrongXmlSchema(String testName) throws Exception {
170 //Should this really be empty?
175 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
176 dependsOnMethods = {"create", "testSubmitRequest"})
177 public void createWithEmptyEntityBody(String testName) throws Exception {
180 setupCreateWithEmptyEntityBody(testName);
182 // Submit the request to the service and store the response.
183 String method = REQUEST_TYPE.httpMethodName();
184 String url = getServiceRootURL();
185 String mediaType = MediaType.APPLICATION_XML;
186 final String entity = "";
187 int statusCode = submitRequest(method, url, mediaType, entity);
189 // Check the status code of the response: does it match
190 // the expected response(s)?
191 if(logger.isDebugEnabled()){
192 logger.debug("createWithEmptyEntityBody url=" + url +
193 " status=" + statusCode);
195 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
201 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
202 dependsOnMethods = {"create", "testSubmitRequest"})
203 public void createWithMalformedXml(String testName) throws Exception {
206 setupCreateWithMalformedXml(testName);
208 // Submit the request to the service and store the response.
209 String method = REQUEST_TYPE.httpMethodName();
210 String url = getServiceRootURL();
211 String mediaType = MediaType.APPLICATION_XML;
212 final String entity = MALFORMED_XML_DATA; // Constant from base class.
213 int statusCode = submitRequest(method, url, mediaType, entity);
215 // Check the status code of the response: does it match
216 // the expected response(s)?
217 if(logger.isDebugEnabled()){
218 logger.debug(testName + ": url=" + url +
219 " status=" + statusCode);
221 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
222 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
223 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
227 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
228 dependsOnMethods = {"create", "testSubmitRequest"})
229 public void createWithWrongXmlSchema(String testName) throws Exception {
232 setupCreateWithWrongXmlSchema(testName);
234 // Submit the request to the service and store the response.
235 String method = REQUEST_TYPE.httpMethodName();
236 String url = getServiceRootURL();
237 String mediaType = MediaType.APPLICATION_XML;
238 final String entity = WRONG_XML_SCHEMA_DATA;
239 int statusCode = submitRequest(method, url, mediaType, entity);
241 // Check the status code of the response: does it match
242 // the expected response(s)?
243 if(logger.isDebugEnabled()){
244 logger.debug(testName + ": url=" + url +
245 " status=" + statusCode);
247 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
248 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
249 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
253 // ---------------------------------------------------------------
254 // CRUD tests : READ tests
255 // ---------------------------------------------------------------
258 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
261 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
262 dependsOnMethods = {"create"})
263 public void read(String testName) throws Exception {
268 // Submit the request to the service and store the response.
269 MovementClient client = new MovementClient();
270 ClientResponse<MultipartInput> res = client.read(knownResourceId);
271 int statusCode = res.getStatus();
273 // Check the status code of the response: does it match
274 // the expected response(s)?
275 if(logger.isDebugEnabled()){
276 logger.debug(testName + ": status = " + statusCode);
278 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
279 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
280 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
282 MultipartInput input = (MultipartInput) res.getEntity();
283 MovementsCommon movement = (MovementsCommon) extractPart(input,
284 client.getCommonPartName(), MovementsCommon.class);
285 Assert.assertNotNull(movement);
290 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
293 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
294 dependsOnMethods = {"read"})
295 public void readNonExistent(String testName) throws Exception {
298 setupReadNonExistent(testName);
300 // Submit the request to the service and store the response.
301 MovementClient client = new MovementClient();
302 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
303 int statusCode = res.getStatus();
305 // Check the status code of the response: does it match
306 // the expected response(s)?
307 if(logger.isDebugEnabled()){
308 logger.debug(testName + ": status = " + statusCode);
310 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
311 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
312 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
315 // ---------------------------------------------------------------
316 // CRUD tests : READ_LIST tests
317 // ---------------------------------------------------------------
320 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
323 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
324 dependsOnMethods = {"createList", "read"})
325 public void readList(String testName) throws Exception {
328 setupReadList(testName);
330 // Submit the request to the service and store the response.
331 MovementClient client = new MovementClient();
332 ClientResponse<MovementsCommonList> res = client.readList();
333 MovementsCommonList list = res.getEntity();
334 int statusCode = res.getStatus();
336 // Check the status code of the response: does it match
337 // the expected response(s)?
338 if(logger.isDebugEnabled()){
339 logger.debug(testName + ": status = " + statusCode);
341 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
342 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
343 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
345 // Optionally output additional data about list members for debugging.
346 boolean iterateThroughList = false;
347 if(iterateThroughList && logger.isDebugEnabled()){
348 List<MovementsCommonList.MovementListItem> items =
349 list.getMovementListItem();
351 for(MovementsCommonList.MovementListItem item : items){
352 logger.debug(testName + ": list-item[" + i + "] csid=" +
354 logger.debug(testName + ": list-item[" + i + "] movementReferenceNumber=" +
355 item.getMovementReferenceNumber());
356 logger.debug(testName + ": list-item[" + i + "] URI=" +
366 // ---------------------------------------------------------------
367 // CRUD tests : UPDATE tests
368 // ---------------------------------------------------------------
371 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
374 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
375 dependsOnMethods = {"read"})
376 public void update(String testName) throws Exception {
379 setupUpdate(testName);
381 // Retrieve the contents of a resource to update.
382 MovementClient client = new MovementClient();
383 ClientResponse<MultipartInput> res =
384 client.read(knownResourceId);
385 if(logger.isDebugEnabled()){
386 logger.debug(testName + ": read status = " + res.getStatus());
388 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
390 if(logger.isDebugEnabled()){
391 logger.debug("got object to update with ID: " + knownResourceId);
393 MultipartInput input = (MultipartInput) res.getEntity();
394 MovementsCommon movement = (MovementsCommon) extractPart(input,
395 client.getCommonPartName(), MovementsCommon.class);
396 Assert.assertNotNull(movement);
398 // Update the content of this resource.
399 movement.setMovementReferenceNumber("updated-" + movement.getMovementReferenceNumber());
400 movement.setLocationDate("updated-" + movement.getLocationDate());
401 if(logger.isDebugEnabled()){
402 logger.debug("to be updated object");
403 logger.debug(objectAsXmlString(movement, MovementsCommon.class));
405 // Submit the request to the service and store the response.
406 MultipartOutput output = new MultipartOutput();
407 OutputPart commonPart = output.addPart(movement, MediaType.APPLICATION_XML_TYPE);
408 commonPart.getHeaders().add("label", client.getCommonPartName());
410 res = client.update(knownResourceId, output);
411 int statusCode = res.getStatus();
412 // Check the status code of the response: does it match the expected response(s)?
413 if(logger.isDebugEnabled()){
414 logger.debug(testName + ": status = " + statusCode);
416 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
417 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
418 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
421 input = (MultipartInput) res.getEntity();
422 MovementsCommon updatedMovement =
423 (MovementsCommon) extractPart(input,
424 client.getCommonPartName(), MovementsCommon.class);
425 Assert.assertNotNull(updatedMovement);
427 Assert.assertEquals(updatedMovement.getLocationDate(),
428 movement.getLocationDate(),
429 "Data in updated object did not match submitted data.");
434 // Placeholders until the three tests below can be uncommented.
435 // See Issue CSPACE-401.
437 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
440 public void updateWithEmptyEntityBody(String testName) throws Exception{
441 //Should this really be empty?
445 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
448 public void updateWithMalformedXml(String testName) throws Exception {
449 //Should this really be empty?
453 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
456 public void updateWithWrongXmlSchema(String testName) throws Exception {
457 //Should this really be empty?
462 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
463 dependsOnMethods = {"create", "update", "testSubmitRequest"})
464 public void updateWithEmptyEntityBody(String testName) throws Exception {
467 setupUpdateWithEmptyEntityBody(testName);
469 // Submit the request to the service and store the response.
470 String method = REQUEST_TYPE.httpMethodName();
471 String url = getResourceURL(knownResourceId);
472 String mediaType = MediaType.APPLICATION_XML;
473 final String entity = "";
474 int statusCode = submitRequest(method, url, mediaType, entity);
476 // Check the status code of the response: does it match
477 // the expected response(s)?
478 if(logger.isDebugEnabled()){
479 logger.debug(testName + ": url=" + url +
480 " status=" + statusCode);
482 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
483 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
484 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
488 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
489 dependsOnMethods = {"create", "update", "testSubmitRequest"})
490 public void updateWithMalformedXml(String testName) throws Exception {
493 setupUpdateWithMalformedXml(testName);
495 // Submit the request to the service and store the response.
496 String method = REQUEST_TYPE.httpMethodName();
497 String url = getResourceURL(knownResourceId);
498 String mediaType = MediaType.APPLICATION_XML;
499 final String entity = MALFORMED_XML_DATA;
500 int statusCode = submitRequest(method, url, mediaType, entity);
502 // Check the status code of the response: does it match
503 // the expected response(s)?
504 if(logger.isDebugEnabled()){
505 logger.debug(testName + ": url=" + url +
506 " status=" + statusCode);
508 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
509 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
510 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
514 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
515 dependsOnMethods = {"create", "update", "testSubmitRequest"})
516 public void updateWithWrongXmlSchema(String testName) throws Exception {
519 setupUpdateWithWrongXmlSchema(testName);
521 // Submit the request to the service and store the response.
522 String method = REQUEST_TYPE.httpMethodName();
523 String url = getResourceURL(knownResourceId);
524 String mediaType = MediaType.APPLICATION_XML;
525 final String entity = WRONG_XML_SCHEMA_DATA;
526 int statusCode = submitRequest(method, url, mediaType, entity);
528 // Check the status code of the response: does it match
529 // the expected response(s)?
530 if(logger.isDebugEnabled()){
531 logger.debug(testName + ": url=" + url +
532 " status=" + statusCode);
534 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
535 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
536 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
541 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
544 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
545 dependsOnMethods = {"update", "testSubmitRequest"})
546 public void updateNonExistent(String testName) throws Exception {
549 setupUpdateNonExistent(testName);
551 // Submit the request to the service and store the response.
552 // Note: The ID used in this 'create' call may be arbitrary.
553 // The only relevant ID may be the one used in update(), below.
554 MovementClient client = new MovementClient();
555 MultipartOutput multipart = createMovementInstance(NON_EXISTENT_ID);
556 ClientResponse<MultipartInput> res =
557 client.update(NON_EXISTENT_ID, multipart);
558 int statusCode = res.getStatus();
560 // Check the status code of the response: does it match
561 // the expected response(s)?
562 if(logger.isDebugEnabled()){
563 logger.debug(testName + ": status = " + statusCode);
565 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
566 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
567 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
570 // ---------------------------------------------------------------
571 // CRUD tests : DELETE tests
572 // ---------------------------------------------------------------
575 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
578 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
579 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
580 public void delete(String testName) throws Exception {
583 setupDelete(testName);
585 // Submit the request to the service and store the response.
586 MovementClient client = new MovementClient();
587 ClientResponse<Response> res = client.delete(knownResourceId);
588 int statusCode = res.getStatus();
590 // Check the status code of the response: does it match
591 // the expected response(s)?
592 if(logger.isDebugEnabled()){
593 logger.debug(testName + ": status = " + statusCode);
595 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
596 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
597 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
602 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
605 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
606 dependsOnMethods = {"delete"})
607 public void deleteNonExistent(String testName) throws Exception {
610 setupDeleteNonExistent(testName);
612 // Submit the request to the service and store the response.
613 MovementClient client = new MovementClient();
614 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
615 int statusCode = res.getStatus();
617 // Check the status code of the response: does it match
618 // the expected response(s)?
619 if(logger.isDebugEnabled()){
620 logger.debug(testName + ": status = " + statusCode);
622 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
623 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
624 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
627 // ---------------------------------------------------------------
628 // Utility tests : tests of code used in tests above
629 // ---------------------------------------------------------------
631 * Tests the code for manually submitting data that is used by several
632 * of the methods above.
634 @Test(dependsOnMethods = {"create", "read"})
635 public void testSubmitRequest() {
637 // Expected status code: 200 OK
638 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
640 // Submit the request to the service and store the response.
641 String method = ServiceRequestType.READ.httpMethodName();
642 String url = getResourceURL(knownResourceId);
643 int statusCode = submitRequest(method, url);
645 // Check the status code of the response: does it match
646 // the expected response(s)?
647 if(logger.isDebugEnabled()){
648 logger.debug("testSubmitRequest: url=" + url +
649 " status=" + statusCode);
651 Assert.assertEquals(statusCode, EXPECTED_STATUS);
655 // ---------------------------------------------------------------
656 // Utility methods used by tests above
657 // ---------------------------------------------------------------
659 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
662 public String getServicePathComponent() {
663 return SERVICE_PATH_COMPONENT;
667 * Creates the movement instance.
669 * @param identifier the identifier
670 * @return the multipart output
672 private MultipartOutput createMovementInstance(String identifier) {
673 return createMovementInstance(
674 "movementReferenceNumber-" + identifier,
675 "locationDate-" + identifier);
679 * Creates the movement instance.
681 * @param movementReferenceNumber the movement reference number
682 * @param locationDate the location date
683 * @return the multipart output
685 private MultipartOutput createMovementInstance(String movementReferenceNumber,
686 String locationDate) {
687 MovementsCommon movement = new MovementsCommon();
688 movement.setMovementReferenceNumber(movementReferenceNumber);
689 movement.setLocationDate(locationDate);
690 MultipartOutput multipart = new MultipartOutput();
691 OutputPart commonPart =
692 multipart.addPart(movement, MediaType.APPLICATION_XML_TYPE);
693 commonPart.getHeaders().add("label", new MovementClient().getCommonPartName());
695 if(logger.isDebugEnabled()){
696 logger.debug("to be created, movement common");
697 logger.debug(objectAsXmlString(movement, MovementsCommon.class));