]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bebc7bc5ed64d42963f58650395bb19ad8953bca
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23 package org.collectionspace.services.client.test;
24
25 import java.text.DateFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Calendar;
28 import java.util.Date;
29 import java.util.List;
30 import java.util.TimeZone;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33
34 import org.collectionspace.services.client.CollectionSpaceClient;
35 import org.collectionspace.services.client.MovementClient;
36 import org.collectionspace.services.jaxb.AbstractCommonList;
37 import org.collectionspace.services.movement.MovementsCommon;
38 import org.collectionspace.services.movement.MovementsCommonList;
39 import org.collectionspace.services.movement.MovementMethodsList;
40
41 import org.jboss.resteasy.client.ClientResponse;
42
43 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
44 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
45 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
46 import org.testng.Assert;
47 import org.testng.annotations.Test;
48
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * MovementServiceTest, carries out tests against a
54  * deployed and running Movement Service.
55  *
56  * $LastChangedRevision$
57  * $LastChangedDate$
58  */
59 public class MovementServiceTest extends AbstractServiceTestImpl {
60
61    /** The logger. */
62     private final String CLASS_NAME = MovementServiceTest.class.getName();
63     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
64
65     // Instance variables specific to this test.
66     /** The service path component. */
67     final String SERVICE_PATH_COMPONENT = "movements";
68     
69     /** The known resource id. */
70     private String knownResourceId = null;
71     
72     /* (non-Javadoc)
73      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
74      */
75     @Override
76     protected CollectionSpaceClient getClientInstance() {
77         return new MovementClient();
78     }
79     
80     /* (non-Javadoc)
81      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
82      */
83     @Override
84         protected AbstractCommonList getAbstractCommonList(
85                         ClientResponse<AbstractCommonList> response) {
86         return response.getEntity(MovementsCommonList.class);
87     }
88  
89     // ---------------------------------------------------------------
90     // CRUD tests : CREATE tests
91     // ---------------------------------------------------------------
92     // Success outcomes
93     /* (non-Javadoc)
94      * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
95      */
96     @Override
97     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
98     public void create(String testName) throws Exception {
99
100         if (logger.isDebugEnabled()) {
101             logger.debug(testBanner(testName, CLASS_NAME));
102         }
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).
106         setupCreate();
107
108         // Submit the request to the service and store the response.
109         MovementClient client = new MovementClient();
110         String identifier = createIdentifier();
111         MultipartOutput multipart = createMovementInstance(identifier);
112         ClientResponse<Response> res = client.create(multipart);
113
114         int statusCode = res.getStatus();
115
116         // Check the status code of the response: does it match
117         // the expected response(s)?
118         //
119         // Specifically:
120         // Does it fall within the set of valid status codes?
121         // Does it exactly match the expected status code?
122         if(logger.isDebugEnabled()){
123             logger.debug(testName + ": status = " + statusCode);
124         }
125         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
126                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
127         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
128
129         // Store the ID returned from the first resource created
130         // for additional tests below.
131         if (knownResourceId == null){
132             knownResourceId = extractId(res);
133             if (logger.isDebugEnabled()) {
134                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
135             }
136         }
137         
138         // Store the IDs from every resource created by tests,
139         // so they can be deleted after tests have been run.
140         allResourceIdsCreated.add(extractId(res));
141     }
142
143     /* (non-Javadoc)
144      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
145      */
146     @Override
147     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
148         dependsOnMethods = {"create"})
149     public void createList(String testName) throws Exception {
150         for(int i = 0; i < 3; i++){
151             create(testName);
152         }
153     }
154
155     // Failure outcomes
156     // Placeholders until the three tests below can be uncommented.
157     // See Issue CSPACE-401.
158     /* (non-Javadoc)
159      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
160      */
161     @Override
162     public void createWithEmptyEntityBody(String testName) throws Exception {
163         //Should this really be empty?
164     }
165
166     /* (non-Javadoc)
167      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
168      */
169     @Override
170     public void createWithMalformedXml(String testName) throws Exception {
171         //Should this really be empty?
172     }
173
174     /* (non-Javadoc)
175      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
176      */
177     @Override
178     public void createWithWrongXmlSchema(String testName) throws Exception {
179         //Should this really be empty?
180     }
181
182     /*
183     @Override
184     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
185         dependsOnMethods = {"create", "testSubmitRequest"})
186     public void createWithEmptyEntityBody(String testName) throws Exception {
187
188         if (logger.isDebugEnabled()) {
189             logger.debug(testBanner(testName, CLASS_NAME));
190         }
191         // Perform setup.
192         setupCreateWithEmptyEntityBody();
193
194         // Submit the request to the service and store the response.
195         String method = REQUEST_TYPE.httpMethodName();
196         String url = getServiceRootURL();
197         String mediaType = MediaType.APPLICATION_XML;
198         final String entity = "";
199         int statusCode = submitRequest(method, url, mediaType, entity);
200
201         // Check the status code of the response: does it match
202         // the expected response(s)?
203         if(logger.isDebugEnabled()){
204             logger.debug("createWithEmptyEntityBody url=" + url +
205                 " status=" + statusCode);
206          }
207         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
208         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
209         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
210     }
211
212     @Override
213     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
214         dependsOnMethods = {"create", "testSubmitRequest"})
215     public void createWithMalformedXml(String testName) throws Exception {
216
217         if (logger.isDebugEnabled()) {
218             logger.debug(testBanner(testName, CLASS_NAME));
219         }
220         // Perform setup.
221         setupCreateWithMalformedXml();
222
223         // Submit the request to the service and store the response.
224         String method = REQUEST_TYPE.httpMethodName();
225         String url = getServiceRootURL();
226         String mediaType = MediaType.APPLICATION_XML;
227         final String entity = MALFORMED_XML_DATA; // Constant from base class.
228         int statusCode = submitRequest(method, url, mediaType, entity);
229
230         // Check the status code of the response: does it match
231         // the expected response(s)?
232         if(logger.isDebugEnabled()){
233             logger.debug(testName + ": url=" + url +
234                 " status=" + statusCode);
235          }
236         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
237         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
238         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
239     }
240
241     @Override
242     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
243         dependsOnMethods = {"create", "testSubmitRequest"})
244     public void createWithWrongXmlSchema(String testName) throws Exception {
245
246         if (logger.isDebugEnabled()) {
247             logger.debug(testBanner(testName, CLASS_NAME));
248         }
249         // Perform setup.
250         setupCreateWithWrongXmlSchema();
251
252         // Submit the request to the service and store the response.
253         String method = REQUEST_TYPE.httpMethodName();
254         String url = getServiceRootURL();
255         String mediaType = MediaType.APPLICATION_XML;
256         final String entity = WRONG_XML_SCHEMA_DATA;
257         int statusCode = submitRequest(method, url, mediaType, entity);
258
259         // Check the status code of the response: does it match
260         // the expected response(s)?
261         if(logger.isDebugEnabled()){
262             logger.debug(testName + ": url=" + url +
263                 " status=" + statusCode);
264          }
265         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
266         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
267         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
268     }
269      */
270
271     // ---------------------------------------------------------------
272     // CRUD tests : READ tests
273     // ---------------------------------------------------------------
274     // Success outcomes
275     /* (non-Javadoc)
276      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
277      */
278     @Override
279     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
280         dependsOnMethods = {"create"})
281     public void read(String testName) throws Exception {
282
283         if (logger.isDebugEnabled()) {
284             logger.debug(testBanner(testName, CLASS_NAME));
285         }
286         // Perform setup.
287         setupRead();
288
289         // Submit the request to the service and store the response.
290         MovementClient client = new MovementClient();
291         ClientResponse<MultipartInput> res = client.read(knownResourceId);
292         int statusCode = res.getStatus();
293
294         // Check the status code of the response: does it match
295         // the expected response(s)?
296         if(logger.isDebugEnabled()){
297             logger.debug(testName + ": status = " + statusCode);
298         }
299         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
300                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
301         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
302
303         MultipartInput input = (MultipartInput) res.getEntity();
304         MovementsCommon movement = (MovementsCommon) extractPart(input,
305                 client.getCommonPartName(), MovementsCommon.class);
306         Assert.assertNotNull(movement);
307     }
308
309     // Failure outcomes
310     /* (non-Javadoc)
311      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
312      */
313     @Override
314     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
315         dependsOnMethods = {"read"})
316     public void readNonExistent(String testName) throws Exception {
317
318         if (logger.isDebugEnabled()) {
319             logger.debug(testBanner(testName, CLASS_NAME));
320         }
321         // Perform setup.
322         setupReadNonExistent();
323
324         // Submit the request to the service and store the response.
325         MovementClient client = new MovementClient();
326         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
327         int statusCode = res.getStatus();
328
329         // Check the status code of the response: does it match
330         // the expected response(s)?
331         if(logger.isDebugEnabled()){
332             logger.debug(testName + ": status = " + statusCode);
333         }
334         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
335                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
336         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
337     }
338
339     // ---------------------------------------------------------------
340     // CRUD tests : READ_LIST tests
341     // ---------------------------------------------------------------
342     // Success outcomes
343     /* (non-Javadoc)
344      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
345      */
346     @Override
347     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
348         dependsOnMethods = {"createList", "read"})
349     public void readList(String testName) throws Exception {
350
351         if (logger.isDebugEnabled()) {
352             logger.debug(testBanner(testName, CLASS_NAME));
353         }
354         // Perform setup.
355         setupReadList();
356
357         // Submit the request to the service and store the response.
358         MovementClient client = new MovementClient();
359         ClientResponse<MovementsCommonList> res = client.readList();
360         MovementsCommonList list = res.getEntity();
361         int statusCode = res.getStatus();
362
363         // Check the status code of the response: does it match
364         // the expected response(s)?
365         if(logger.isDebugEnabled()){
366             logger.debug(testName + ": status = " + statusCode);
367         }
368         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
369                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
370         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
371
372         // Optionally output additional data about list members for debugging.
373         boolean iterateThroughList = true;
374         if(iterateThroughList && logger.isDebugEnabled()){
375             List<MovementsCommonList.MovementListItem> items =
376                     list.getMovementListItem();
377             int i = 0;
378             for(MovementsCommonList.MovementListItem item : items){
379                 logger.debug(testName + ": list-item[" + i + "] csid=" +
380                         item.getCsid());
381                 logger.debug(testName + ": list-item[" + i + "] movementReferenceNumber=" +
382                         item.getMovementReferenceNumber());
383                 logger.debug(testName + ": list-item[" + i + "] locationDate=" +
384                         item.getLocationDate());
385                 logger.debug(testName + ": list-item[" + i + "] URI=" +
386                         item.getUri());
387                 i++;
388             }
389         }
390
391     }
392
393     // Failure outcomes
394     // None at present.
395     // ---------------------------------------------------------------
396     // CRUD tests : UPDATE tests
397     // ---------------------------------------------------------------
398     // Success outcomes
399     /* (non-Javadoc)
400      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
401      */
402     @Override
403     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
404         dependsOnMethods = {"read"})
405     public void update(String testName) throws Exception {
406
407         if (logger.isDebugEnabled()) {
408             logger.debug(testBanner(testName, CLASS_NAME));
409         }
410         // Perform setup.
411         setupUpdate();
412
413         // Retrieve the contents of a resource to update.
414         MovementClient client = new MovementClient();
415         ClientResponse<MultipartInput> res =
416                 client.read(knownResourceId);
417         if(logger.isDebugEnabled()){
418             logger.debug(testName + ": read status = " + res.getStatus());
419         }
420         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
421
422         if(logger.isDebugEnabled()){
423             logger.debug("got object to update with ID: " + knownResourceId);
424         }
425         MultipartInput input = (MultipartInput) res.getEntity();
426         MovementsCommon movement = (MovementsCommon) extractPart(input,
427                 client.getCommonPartName(), MovementsCommon.class);
428         Assert.assertNotNull(movement);
429
430         // Update the content of this resource.
431         movement.setMovementReferenceNumber("updated-" + movement.getMovementReferenceNumber());
432         movement.setMovementNote("updated movement note-" + movement.getMovementNote());
433         if(logger.isDebugEnabled()){
434             logger.debug("to be updated object");
435             logger.debug(objectAsXmlString(movement, MovementsCommon.class));
436         }
437         // Submit the request to the service and store the response.
438         MultipartOutput output = new MultipartOutput();
439         OutputPart commonPart = output.addPart(movement, MediaType.APPLICATION_XML_TYPE);
440         commonPart.getHeaders().add("label", client.getCommonPartName());
441
442         res = client.update(knownResourceId, output);
443         int statusCode = res.getStatus();
444         // Check the status code of the response: does it match the expected response(s)?
445         if(logger.isDebugEnabled()){
446             logger.debug(testName + ": status = " + statusCode);
447         }
448         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
449                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
450         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
451
452
453         input = (MultipartInput) res.getEntity();
454         MovementsCommon updatedMovement =
455                 (MovementsCommon) extractPart(input,
456                         client.getCommonPartName(), MovementsCommon.class);
457         Assert.assertNotNull(updatedMovement);
458
459         Assert.assertEquals(updatedMovement.getMovementNote(),
460                 movement.getMovementNote(),
461                 "Data in updated object did not match submitted data.");
462
463     }
464
465     // Failure outcomes
466     // Placeholders until the three tests below can be uncommented.
467     // See Issue CSPACE-401.
468     /* (non-Javadoc)
469      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
470      */
471     @Override
472     public void updateWithEmptyEntityBody(String testName) throws Exception{
473         //Should this really be empty?
474     }
475     
476     /* (non-Javadoc)
477      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
478      */
479     @Override
480     public void updateWithMalformedXml(String testName) throws Exception {
481         //Should this really be empty?
482     }
483     
484     /* (non-Javadoc)
485      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
486      */
487     @Override
488     public void updateWithWrongXmlSchema(String testName) throws Exception {
489         //Should this really be empty?
490     }
491
492     /*
493     @Override
494     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
495         dependsOnMethods = {"create", "update", "testSubmitRequest"})
496     public void updateWithEmptyEntityBody(String testName) throws Exception {
497
498         if (logger.isDebugEnabled()) {
499             logger.debug(testBanner(testName, CLASS_NAME));
500         }
501         // Perform setup.
502         setupUpdateWithEmptyEntityBody();
503
504         // Submit the request to the service and store the response.
505         String method = REQUEST_TYPE.httpMethodName();
506         String url = getResourceURL(knownResourceId);
507         String mediaType = MediaType.APPLICATION_XML;
508         final String entity = "";
509         int statusCode = submitRequest(method, url, mediaType, entity);
510
511         // Check the status code of the response: does it match
512         // the expected response(s)?
513         if(logger.isDebugEnabled()){
514             logger.debug(testName + ": url=" + url +
515                 " status=" + statusCode);
516          }
517         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
518         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
519         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
520     }
521
522     @Override
523     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
524         dependsOnMethods = {"create", "update", "testSubmitRequest"})
525     public void updateWithMalformedXml(String testName) throws Exception {
526
527         if (logger.isDebugEnabled()) {
528             logger.debug(testBanner(testName, CLASS_NAME));
529         }
530         // Perform setup.
531         setupUpdateWithMalformedXml();
532
533         // Submit the request to the service and store the response.
534         String method = REQUEST_TYPE.httpMethodName();
535         String url = getResourceURL(knownResourceId);
536         String mediaType = MediaType.APPLICATION_XML;
537         final String entity = MALFORMED_XML_DATA;
538         int statusCode = submitRequest(method, url, mediaType, entity);
539
540         // Check the status code of the response: does it match
541         // the expected response(s)?
542         if(logger.isDebugEnabled()){
543             logger.debug(testName + ": url=" + url +
544              " status=" + statusCode);
545          }
546         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
547         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
548         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
549     }
550
551     @Override
552     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
553         dependsOnMethods = {"create", "update", "testSubmitRequest"})
554     public void updateWithWrongXmlSchema(String testName) throws Exception {
555
556         if (logger.isDebugEnabled()) {
557             logger.debug(testBanner(testName, CLASS_NAME));
558         }
559         // Perform setup.
560         setupUpdateWithWrongXmlSchema();
561
562         // Submit the request to the service and store the response.
563         String method = REQUEST_TYPE.httpMethodName();
564         String url = getResourceURL(knownResourceId);
565         String mediaType = MediaType.APPLICATION_XML;
566         final String entity = WRONG_XML_SCHEMA_DATA;
567         int statusCode = submitRequest(method, url, mediaType, entity);
568
569         // Check the status code of the response: does it match
570         // the expected response(s)?
571         if(logger.isDebugEnabled()){
572             logger.debug(testName + ": url=" + url +
573             " status=" + statusCode);
574          }
575         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
576         invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
577         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
578     }
579      */
580
581     /* (non-Javadoc)
582      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
583      */
584     @Override
585     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
586         dependsOnMethods = {"update", "testSubmitRequest"})
587     public void updateNonExistent(String testName) throws Exception {
588
589         if (logger.isDebugEnabled()) {
590             logger.debug(testBanner(testName, CLASS_NAME));
591         }
592         // Perform setup.
593         setupUpdateNonExistent();
594
595         // Submit the request to the service and store the response.
596         // Note: The ID used in this 'create' call may be arbitrary.
597         // The only relevant ID may be the one used in update(), below.
598         MovementClient client = new MovementClient();
599         MultipartOutput multipart = createMovementInstance(NON_EXISTENT_ID);
600         ClientResponse<MultipartInput> res =
601                 client.update(NON_EXISTENT_ID, multipart);
602         int statusCode = res.getStatus();
603
604         // Check the status code of the response: does it match
605         // the expected response(s)?
606         if(logger.isDebugEnabled()){
607             logger.debug(testName + ": status = " + statusCode);
608         }
609         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
610                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
611         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
612     }
613
614     // ---------------------------------------------------------------
615     // CRUD tests : DELETE tests
616     // ---------------------------------------------------------------
617     // Success outcomes
618     /* (non-Javadoc)
619      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
620      */
621     @Override
622     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
623         dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
624     public void delete(String testName) throws Exception {
625
626         if (logger.isDebugEnabled()) {
627             logger.debug(testBanner(testName, CLASS_NAME));
628         }
629         // Perform setup.
630         setupDelete();
631
632         // Submit the request to the service and store the response.
633         MovementClient client = new MovementClient();
634         ClientResponse<Response> res = client.delete(knownResourceId);
635         int statusCode = res.getStatus();
636
637         // Check the status code of the response: does it match
638         // the expected response(s)?
639         if(logger.isDebugEnabled()){
640             logger.debug(testName + ": status = " + statusCode);
641         }
642         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
643                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
644         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
645     }
646
647     // Failure outcomes
648     /* (non-Javadoc)
649      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
650      */
651     @Override
652     @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
653         dependsOnMethods = {"delete"})
654     public void deleteNonExistent(String testName) throws Exception {
655
656         if (logger.isDebugEnabled()) {
657             logger.debug(testBanner(testName, CLASS_NAME));
658         }
659         // Perform setup.
660         setupDeleteNonExistent();
661
662         // Submit the request to the service and store the response.
663         MovementClient client = new MovementClient();
664         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
665         int statusCode = res.getStatus();
666
667         // Check the status code of the response: does it match
668         // the expected response(s)?
669         if(logger.isDebugEnabled()){
670             logger.debug(testName + ": status = " + statusCode);
671         }
672         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
673                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
674         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
675     }
676
677     // ---------------------------------------------------------------
678     // Utility tests : tests of code used in tests above
679     // ---------------------------------------------------------------
680     /**
681      * Tests the code for manually submitting data that is used by several
682      * of the methods above.
683      */
684     @Test(dependsOnMethods = {"create", "read"})
685     public void testSubmitRequest() {
686
687         // Expected status code: 200 OK
688         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
689
690         // Submit the request to the service and store the response.
691         String method = ServiceRequestType.READ.httpMethodName();
692         String url = getResourceURL(knownResourceId);
693         int statusCode = submitRequest(method, url);
694
695         // Check the status code of the response: does it match
696         // the expected response(s)?
697         if(logger.isDebugEnabled()){
698             logger.debug("testSubmitRequest: url=" + url +
699                 " status=" + statusCode);
700         }
701         Assert.assertEquals(statusCode, EXPECTED_STATUS);
702
703     }
704
705     // ---------------------------------------------------------------
706     // Utility methods used by tests above
707     // ---------------------------------------------------------------
708     /* (non-Javadoc)
709      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
710      */
711     @Override
712     public String getServicePathComponent() {
713         return SERVICE_PATH_COMPONENT;
714     }
715
716     /**
717      * Creates the movement instance.
718      *
719      * @param identifier the identifier
720      * @return the multipart output
721      */
722     private MultipartOutput createMovementInstance(String identifier) {
723         return createInstance("movementReferenceNumber-" + identifier);
724     }
725
726     /**
727      * Creates an instance of a Movement record for testing.
728      *
729      * @param movementReferenceNumber A movement reference number.
730      * @return Multipart output suitable for use as a payload
731      *     in a create or update request.
732      */
733     private MultipartOutput createInstance(String movementReferenceNumber) {
734         MovementsCommon movement = new MovementsCommon();
735         // FIXME: Values of currentLocation, normalLocation,
736         // and movementContact should be refNames.
737         movement.setCurrentLocation("currentLocation value");
738         movement.setCurrentLocationFitness("currentLocationFitness value");
739         movement.setCurrentLocationNote("currentLocationNote value");
740         movement.setLocationDate(timestampUTC());
741         movement.setNormalLocation("normalLocation value");
742         movement.setMovementContact("movementContact value");
743         MovementMethodsList movementMethodsList = new MovementMethodsList();
744         List<String> methods = movementMethodsList.getMovementMethod();
745         // @TODO Use properly formatted refNames for representative movement
746         // methods in this example record. The values below are placeholders.
747         String identifier = createIdentifier();
748         methods.add("First Movement Method-" + identifier);
749         methods.add("Second Movement Method-" + identifier);
750         movement.setMovementMethods(movementMethodsList);
751         movement.setMovementNote("movementNote value");
752         movement.setMovementReferenceNumber(movementReferenceNumber);
753         movement.setPlannedRemovalDate("plannedRemovalDate value");
754         movement.setRemovalDate("removalDate value");
755         movement.setReasonForMove("reasonForMove value");
756         MultipartOutput multipart = new MultipartOutput();
757         OutputPart commonPart =
758             multipart.addPart(movement, MediaType.APPLICATION_XML_TYPE);
759         commonPart.getHeaders().add("label", new MovementClient().getCommonPartName());
760
761         if(logger.isDebugEnabled()){
762             logger.debug("to be created, movement common");
763             logger.debug(objectAsXmlString(movement, MovementsCommon.class));
764         }
765
766         return multipart;
767     }
768
769     // FIXME Should be moved to a common class, as these are general utilities.
770     // FIXME Should be refactored to become a convenience variant of a
771     // general method to return a current datestamp or timestamp in any
772     // provided time zone.
773
774    /**
775     * Returns an ISO 8601 formatted timestamp of the
776     * current time instance in the UTC time zone.
777     */
778     public String datestampUTC() {
779         final String ISO_8601_DATE_FORMAT_PATTERN = "yyyy-MM-dd";
780         final DateFormat ISO_8601_DATE_FORMAT =
781             new SimpleDateFormat(ISO_8601_DATE_FORMAT_PATTERN);
782
783         final String UTC_TIMEZONE_IDENTIFIER = "UTC";
784         final TimeZone UTC_TIMEZONE = TimeZone.getTimeZone(UTC_TIMEZONE_IDENTIFIER);
785
786         Date timestamp = new Date();
787         return formatDate(timestamp, UTC_TIMEZONE, ISO_8601_DATE_FORMAT);
788     }
789
790    /**
791     * Returns an ISO 8601 formatted timestamp of the
792     * current time instance in the UTC time zone.
793     */
794     public String timestampUTC() {
795         final String ISO_8601_FORMAT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";
796         final DateFormat ISO_8601_FORMAT =
797             new SimpleDateFormat(ISO_8601_FORMAT_PATTERN);
798
799         final String UTC_TIMEZONE_IDENTIFIER = "UTC";
800         final TimeZone UTC_TIMEZONE = TimeZone.getTimeZone(UTC_TIMEZONE_IDENTIFIER);
801
802         Date timestamp = new Date();
803         return formatDate(timestamp, UTC_TIMEZONE, ISO_8601_FORMAT);
804     }
805
806    /**
807     * Formats a provided date using a provided date formatter,
808     * in the default system time zone.
809     *
810     * @param date  A date to format.
811     * @param df    A date formatter to apply.
812     * @return      A formatted date string.
813     */
814     public String formatDate(Date date, DateFormat df) {
815         return formatDate(date, TimeZone.getDefault(), df);
816     }
817
818     // FIXME Add error handling.
819
820    /**
821     * Formats a provided date using a provided date formatter,
822     * in a provided time zone.
823     *
824     * @param date  A date to format.
825     * @param tz    The time zone qualifier for the date to format.
826     * @param df    A date formatter to apply.
827     *
828     * @return      A formatted date string.
829     */
830     public String formatDate(Date date, TimeZone tz, DateFormat df) {
831         df.setTimeZone(tz);
832         return df.format(date);
833     }
834
835 }