]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0ce3a876e39348b2886119abe33cc200ed95598d
[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.Collator;
26 import java.util.ArrayList;
27 import java.util.Comparator;
28 import java.util.List;
29 import java.util.Locale;
30
31 import javax.ws.rs.core.Response;
32
33 import org.collectionspace.services.MovementJAXBSchema;
34 import org.collectionspace.services.client.AbstractCommonListUtils;
35 import org.collectionspace.services.client.CollectionSpaceClient;
36 import org.collectionspace.services.client.MovementClient;
37 import org.collectionspace.services.client.PayloadInputPart;
38 import org.collectionspace.services.client.PayloadOutputPart;
39 import org.collectionspace.services.client.PoxPayloadIn;
40 import org.collectionspace.services.client.PoxPayloadOut;
41 import org.collectionspace.services.movement.MovementsCommon;
42 import org.collectionspace.services.jaxb.AbstractCommonList;
43
44 import org.testng.Assert;
45 import org.testng.annotations.AfterClass;
46 import org.testng.annotations.DataProvider;
47 import org.testng.annotations.Test;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 /**
52  * MovementSortByTest, tests sorting of summary lists by fields
53  * of various datatypes.
54  *
55  * $LastChangedRevision: 2562 $
56  * $LastChangedDate: 2010-06-22 23:26:51 -0700 (Tue, 22 Jun 2010) $
57  */
58 public class MovementSortByTest extends BaseServiceTest<AbstractCommonList> {
59
60     private final String CLASS_NAME = MovementSortByTest.class.getName();
61     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
62     final String SERVICE_NAME = "movements";
63
64     // Instance variables specific to this test.
65     private final String DELIMITER_SCHEMA_AND_FIELD = ":";
66     private final String KEYWORD_DESCENDING_SEARCH = "DESC";
67     private final String SERVICE_PATH_COMPONENT = "movements";
68     private final String TEST_SPECIFIC_KEYWORD = "msotebstpfscn";
69     private List<String> movementIdsCreated = new ArrayList<String>();
70     private final String SORT_FIELD_SEPARATOR = ", ";
71     private final Locale LOCALE = Locale.US;
72     private final String LOCATION_DATE_EL_NAME = "locationDate";
73     private final Comparator<String> CASE_INSENSITIVE_STRING_COMPARATOR =
74             String.CASE_INSENSITIVE_ORDER;
75     private final Collator LOCALE_SPECIFIC_COLLATOR = Collator.getInstance(LOCALE);
76
77
78
79     /* (non-Javadoc)
80      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
81      */
82     @Override
83     protected CollectionSpaceClient getClientInstance() {
84         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
85     }
86
87         @Override
88         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
89         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
90         }
91
92     /* (non-Javadoc)
93      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
94      */
95     @Override
96     protected AbstractCommonList getCommonList(Response response) {
97         throw new UnsupportedOperationException(); //method not supported (or needed) in this test class
98     }
99
100     // ---------------------------------------------------------------
101     // Sort tests
102     // ---------------------------------------------------------------
103
104     // Success outcomes
105
106     /*
107      * Tests whether a list of records, sorted by a String field in
108      * ascending order, is returned in the expected order.
109      */
110     @Test(dataProvider = "testName",
111                 dependsOnMethods = {"createList"})
112     public void sortByStringFieldAscending(String testName) throws Exception {
113         String sortFieldName = qualifySortFieldName(MovementJAXBSchema.MOVEMENT_NOTE);
114         if (logger.isDebugEnabled()) {
115             logger.debug("Sorting on field name=" + sortFieldName);
116         }
117         AbstractCommonList list = readSortedList(sortFieldName);
118         List<AbstractCommonList.ListItem> items =
119                 list.getListItem();
120
121         ArrayList<String> values = new ArrayList<String>();
122         int i = 0;
123         for (AbstractCommonList.ListItem item : items) {
124             // Because movementNote is not currently a summary field
125             // (returned in summary list items), we will need to verify
126             // sort order by using the IDs provided in the summary list
127             // items to retrieve full records, and then obtaining
128             // the value of that field from each of those records.
129             MovementsCommon movement = read(AbstractCommonListUtils.ListItemGetCSID(item));
130             values.add(i, movement.getMovementNote());
131             if (logger.isTraceEnabled()) {
132                 logger.trace("list-item[" + i + "] movementNote=" + values.get(i));
133             }
134             // Verify that the value of the specified field in the current record
135             // is equal to or greater than its value in the previous record.
136             //
137             // (Note: when used with certain text, this test case could potentially
138             // reflect inconsistencies, if any, between Java's collator and the
139             // collator used for ordering by the database.  To help avoid this,
140             // it might be useful to keep test strings fairly generic.)
141             if (i > 0 && values.get(i) != null && values.get(i - 1) != null) {
142                 if (logger.isDebugEnabled()) {
143                     logger.debug("Comparing " + values.get(i) + " with previous value " + values.get(i - 1) + "...");
144                 }
145                 Assert.assertTrue(LOCALE_SPECIFIC_COLLATOR.compare(values.get(i), values.get(i - 1)) >= 0);
146             }
147             i++;
148         }
149
150     }
151
152     /*
153      * Tests whether a list of records, obtained by a keyword search, and
154      * sorted by a String field in ascending order, is returned in the expected order.
155      *
156      * This verifies that summary list results from keyword searches, in
157      * addition to 'read list' requests, can be returned in sorted order.
158      */
159     @Test(dataProvider = "testName",
160                 dependsOnMethods = {"createList"})
161     public void sortKeywordSearchResultsByStringFieldAscending(String testName) throws Exception {
162         String sortFieldName = qualifySortFieldName(MovementJAXBSchema.MOVEMENT_NOTE);
163         if (logger.isDebugEnabled()) {
164             logger.debug("Sorting on field name=" + sortFieldName);
165         }
166         AbstractCommonList list = keywordSearchSortedBy(TEST_SPECIFIC_KEYWORD, sortFieldName);
167         List<AbstractCommonList.ListItem> items =
168                 list.getListItem();
169
170         ArrayList<String> values = new ArrayList<String>();
171         int i = 0;
172         for (AbstractCommonList.ListItem item : items) {
173             // Because movementNote is not currently a summary field
174             // (returned in summary list items), we will need to verify
175             // sort order by using the IDs provided in the summary list
176             // items to retrieve full records, and then obtaining
177             // the value of that field from each of those records.
178             MovementsCommon movement = read(AbstractCommonListUtils.ListItemGetCSID(item));
179             values.add(i, movement.getMovementNote());
180             if (logger.isTraceEnabled()) {
181                 logger.trace("list-item[" + i + "] movementNote=" + values.get(i));
182             }
183             // Verify that the value of the specified field in the current record
184             // is equal to or greater than its value in the previous record.
185             //
186             // (Note: when used with certain text, this test case could potentially
187             // reflect inconsistencies, if any, between Java's collator and the
188             // collator used for ordering by the database.  To help avoid this,
189             // it might be useful to keep test strings fairly generic.)
190             if (i > 0 && values.get(i) != null && values.get(i - 1) != null) {
191                 if (logger.isDebugEnabled()) {
192                     logger.debug("Comparing " + values.get(i) + " with previous value " + values.get(i - 1) + "...");
193                 }
194                 Assert.assertTrue(LOCALE_SPECIFIC_COLLATOR.compare(values.get(i), values.get(i - 1)) >= 0);
195             }
196             i++;
197         }
198
199     }
200
201     /*
202      * Tests whether a list of records, sorted by a String field in
203      * descending order, is returned in the expected order.
204      */
205     @Test(dataProvider = "testName",
206                 dependsOnMethods = {"createList"})
207     public void sortByStringFieldDescending(String testName) throws Exception {
208         String sortFieldName =
209                 asDescendingSort(qualifySortFieldName(MovementJAXBSchema.MOVEMENT_NOTE));
210         if (logger.isDebugEnabled()) {
211             logger.debug("Sorting on field name=" + sortFieldName);
212         }
213         AbstractCommonList list = readSortedList(sortFieldName);
214         List<AbstractCommonList.ListItem> items =
215                 list.getListItem();
216
217         ArrayList<String> values = new ArrayList<String>();
218         int i = 0;
219         for (AbstractCommonList.ListItem item : items) {
220             // Because movementNote is not currently a summary field
221             // (returned in summary list items), we will need to verify
222             // sort order by using the IDs provided in the summary list
223             // items to retrieve full records, and then obtaining
224             // the value of that field from each of those records.
225             MovementsCommon movement = read(AbstractCommonListUtils.ListItemGetCSID(item));
226             values.add(i, movement.getMovementNote());
227             if (logger.isTraceEnabled()) {
228                 logger.trace("list-item[" + i + "] movementNote=" + values.get(i));
229             }
230             // Verify that the value of the specified field in the current record
231             // is less than or equal to than its value in the previous record.
232             //
233             // (Note: when used with certain text, this test case could potentially
234             // reflect inconsistencies, if any, between Java's comparator or collator,
235             // and the collator used for ordering by the database.  To help avoid this,
236             // it might be useful to keep test strings fairly generic.)
237             if (i > 0 && values.get(i) != null && values.get(i - 1) != null) {
238                 if (logger.isDebugEnabled()) {
239                     logger.debug("Comparing " + values.get(i) + " with previous value " + values.get(i - 1) + "...");
240                 }
241                 Assert.assertTrue(LOCALE_SPECIFIC_COLLATOR.compare(values.get(i), values.get(i - 1)) <= 0);
242             }
243             i++;
244         }
245
246     }
247
248     /*
249      * Tests whether a list of records, sorted by a dateTime field in
250      * ascending order, is returned in the expected order.
251      */
252     @Test(dataProvider = "testName",
253                 dependsOnMethods = {"createList"})
254     public void sortByDateTimeFieldAscending(String testName) throws Exception {
255         String sortFieldName = qualifySortFieldName(MovementJAXBSchema.LOCATION_DATE);
256         if (logger.isDebugEnabled()) {
257             logger.debug("Sorting on field name=" + sortFieldName);
258         }
259         AbstractCommonList list = readSortedList(sortFieldName);
260         List<AbstractCommonList.ListItem> items =
261                 list.getListItem();
262
263         ArrayList<String> values = new ArrayList<String>();
264         int i = 0;
265         for (AbstractCommonList.ListItem item : items) {
266                 String locDate = 
267                         AbstractCommonListUtils.ListItemGetElementValue(item, LOCATION_DATE_EL_NAME);
268                 values.add(i, locDate);
269             if (logger.isTraceEnabled()) {
270                 logger.trace("list-item[" + i + "] locationDate=" + values.get(i));
271             }
272             // Verify that the value of the specified field in the current record
273             // is equal to or greater than its value in the previous record.
274             if (i > 0 && values.get(i) != null && values.get(i - 1) != null) {
275                 if (logger.isDebugEnabled()) {
276                     logger.debug("Comparing " + values.get(i) + " with previous value " + values.get(i - 1) + "...");
277                 }
278                 Assert.assertTrue(CASE_INSENSITIVE_STRING_COMPARATOR.compare(values.get(i), values.get(i - 1)) >= 0);
279             }
280             i++;
281         }
282     }
283
284     /*
285      * Tests whether a list of records, sorted by a dateTime field in
286      * descending order, is returned in the expected order.
287      */
288     @Test(dataProvider = "testName",
289                 dependsOnMethods = {"createList"})
290     public void sortByDateTimeFieldDescending(String testName) throws Exception {
291         String sortFieldName =
292                 asDescendingSort(qualifySortFieldName(MovementJAXBSchema.LOCATION_DATE));
293         if (logger.isDebugEnabled()) {
294             logger.debug("Sorting on field name=" + sortFieldName);
295         }
296         AbstractCommonList list = readSortedList(sortFieldName);
297         List<AbstractCommonList.ListItem> items =
298                 list.getListItem();
299
300         ArrayList<String> values = new ArrayList<String>();
301         int i = 0;
302         for (AbstractCommonList.ListItem item : items) {
303                 String locDate = 
304                         AbstractCommonListUtils.ListItemGetElementValue(item, LOCATION_DATE_EL_NAME);
305                 values.add(i, locDate);
306             if (logger.isTraceEnabled()) {
307                 logger.trace("list-item[" + i + "] locationDate=" + values.get(i));
308             }
309             // Verify that the value of the specified field in the current record
310             // is less than or equal to its value in the previous record.
311             if (i > 0 && values.get(i) != null && values.get(i - 1) != null) {
312                 if (logger.isDebugEnabled()) {
313                     logger.debug("Comparing " + values.get(i) + " with previous value " + values.get(i - 1) + "...");
314                 }
315                 Assert.assertTrue(CASE_INSENSITIVE_STRING_COMPARATOR.compare(values.get(i), values.get(i - 1)) <= 0);
316             }
317             i++;
318         }
319     }
320
321     /*
322      * Tests whether a list of records, sorted by two different fields in
323      * ascending order, is returned in the expected order.
324      */
325     @Test(dataProvider = "testName",
326                 dependsOnMethods = {"createList"})
327     public void sortByTwoFieldsAscending(String testName) throws Exception {
328         String firstSortFieldName = qualifySortFieldName(MovementJAXBSchema.MOVEMENT_NOTE);
329         String secondSortFieldName = qualifySortFieldName(MovementJAXBSchema.LOCATION_DATE);
330         if (logger.isDebugEnabled()) {
331             logger.debug("Sorting on field names=" + firstSortFieldName + " and " + secondSortFieldName);
332         }
333         String sortExpression = firstSortFieldName + SORT_FIELD_SEPARATOR + secondSortFieldName;
334         AbstractCommonList list = readSortedList(sortExpression);
335         List<AbstractCommonList.ListItem> items =
336                 list.getListItem();
337
338         ArrayList<String> firstFieldValues = new ArrayList<String>();
339         ArrayList<String> secondFieldValues = new ArrayList<String>();
340         int i = 0;
341         for (AbstractCommonList.ListItem item : items) {
342             // Because movementNote is not currently a summary field
343             // (returned in summary list items), we will need to verify
344             // sort order by using the IDs provided in the summary list
345             // items to retrieve full records, and then obtaining
346             // the value of that field from each of those records.
347             MovementsCommon movement = read(AbstractCommonListUtils.ListItemGetCSID(item));
348             firstFieldValues.add(i, movement.getMovementNote());
349             secondFieldValues.add(i, movement.getLocationDate());
350             if (logger.isDebugEnabled()) {
351                 logger.debug("list-item[" + i + "] movementNote=" + firstFieldValues.get(i));
352                 logger.debug("list-item[" + i + "] locationDate=" + secondFieldValues.get(i));
353             }
354             // Verify that the value of the specified field in the current record
355             // is less than or greater than its value in the previous record.
356             if (i > 0 && firstFieldValues.get(i) != null && firstFieldValues.get(i - 1) != null) {
357                 Assert.assertTrue(LOCALE_SPECIFIC_COLLATOR.compare(firstFieldValues.get(i), firstFieldValues.get(i - 1)) >= 0);
358                 // If the value of the first sort field in the current record is identical to
359                 // its value in the previous record, verify that the value of the second sort
360                 // field is equal to or greater than its value in the previous record.
361                 if (LOCALE_SPECIFIC_COLLATOR.compare(firstFieldValues.get(i), firstFieldValues.get(i - 1)) == 0) {
362                     if (i > 0 && secondFieldValues.get(i) != null && secondFieldValues.get(i - 1) != null) {
363                         Assert.assertTrue(CASE_INSENSITIVE_STRING_COMPARATOR.compare(secondFieldValues.get(i), secondFieldValues.get(i - 1)) >= 0);
364                     }
365                 }
366             }
367             i++;
368         }
369     }
370
371     /*
372      * Tests whether a list of records, sorted by one different fields in
373      * descending order and a second field in ascending order, is returned in the expected order.
374      */
375     @Test(dataProvider = "testName",
376                 dependsOnMethods = {"createList"})
377     public void sortByOneFieldAscendingOneFieldDescending(String testName) throws Exception {
378         String firstSortFieldName =
379                 asDescendingSort(qualifySortFieldName(MovementJAXBSchema.LOCATION_DATE));
380         String secondSortFieldName = qualifySortFieldName(MovementJAXBSchema.MOVEMENT_NOTE);
381         if (logger.isDebugEnabled()) {
382             logger.debug("Sorting on field names=" + firstSortFieldName + " and " + secondSortFieldName);
383         }
384         String sortExpression = firstSortFieldName + SORT_FIELD_SEPARATOR + secondSortFieldName;
385         AbstractCommonList list = readSortedList(sortExpression);
386         List<AbstractCommonList.ListItem> items =
387                 list.getListItem();
388
389         ArrayList<String> firstFieldValues = new ArrayList<String>();
390         ArrayList<String> secondFieldValues = new ArrayList<String>();
391         int i = 0;
392         for (AbstractCommonList.ListItem item : items) {
393             // Because movementNote is not currently a summary field
394             // (returned in summary list items), we will need to verify
395             // sort order by using the IDs provided in the summary list
396             // items to retrieve full records, and then obtaining
397             // the value of that field from each of those records.
398             MovementsCommon movement = read(AbstractCommonListUtils.ListItemGetCSID(item));
399             firstFieldValues.add(i, movement.getLocationDate());
400             secondFieldValues.add(i, movement.getMovementNote());
401             if (logger.isDebugEnabled()) {
402                 logger.debug("list-item[" + i + "] locationDate=" + firstFieldValues.get(i));
403                 logger.debug("list-item[" + i + "] movementNote=" + secondFieldValues.get(i));
404             }
405             // Verify that the value of the specified field in the current record
406             // is less than or equal to than its value in the previous record.
407             if (i > 0 && firstFieldValues.get(i) != null && firstFieldValues.get(i - 1) != null) {
408                 Assert.assertTrue(CASE_INSENSITIVE_STRING_COMPARATOR.compare(firstFieldValues.get(i), firstFieldValues.get(i - 1)) <= 0);
409                 // If the value of the first sort field in the current record is identical to
410                 // its value in the previous record, verify that the value of the second sort
411                 // field is equal to or greater than its value in the previous record,
412                 // using a locale-specific collator.
413                 if (CASE_INSENSITIVE_STRING_COMPARATOR.compare(firstFieldValues.get(i), firstFieldValues.get(i - 1)) == 0) {
414                     if (i > 0 && secondFieldValues.get(i) != null && secondFieldValues.get(i - 1) != null) {
415                         Assert.assertTrue(LOCALE_SPECIFIC_COLLATOR.compare(secondFieldValues.get(i), secondFieldValues.get(i - 1)) >= 0);
416                     }
417                 }
418             }
419             i++;
420         }
421     }
422
423
424     /*
425      * Tests whether a request to sort by an empty field name is handled
426      * as expected: the query parameter is simply ignored, and a list
427      * of records is returned, unsorted, with a success result.
428      */
429     @Test(dataProvider = "testName")
430     public void sortWithEmptySortFieldName(String testName) throws Exception {
431         testSetup(STATUS_OK, ServiceRequestType.READ);
432
433         // Submit the request to the service and store the response.
434         MovementClient client = new MovementClient();
435         final String EMPTY_SORT_FIELD_NAME = "";
436         Response res = client.readListSortedBy(EMPTY_SORT_FIELD_NAME);
437         try {
438                 assertStatusCode(res, testName);
439         } finally {
440                 if (res != null) {
441                 res.close();
442             }
443         }
444     }
445
446     // Failure outcomes
447
448     /*
449      * Tests whether a request to sort by an unqualified field name is
450      * handled as expected.  The field name provided in this test is valid,
451      * but has not been qualified by being prefixed by a schema name and delimiter.
452      */
453     @Test(dataProvider = "testName")
454     public void sortWithUnqualifiedFieldName(String testName) throws Exception {
455         testSetup(STATUS_BAD_REQUEST, ServiceRequestType.READ);
456
457         // Submit the request to the service and store the response.
458         MovementClient client = new MovementClient();
459         Response res = client.readListSortedBy(MovementJAXBSchema.LOCATION_DATE);
460         try {
461                 assertStatusCode(res, testName);
462         } finally {
463                 if (res != null) {
464                 res.close();
465             }
466         }
467     }
468
469     /*
470      * Tests whether a request to sort by an invalid identifier for the
471      * sort order (ascending or descending) is handled as expected.
472      */
473     @Test(dataProvider = "testName")
474     public void sortWithInvalidSortOrderIdentifier(String testName) throws Exception {
475         testSetup(STATUS_BAD_REQUEST, ServiceRequestType.READ);
476
477         // Submit the request to the service and store the response.
478         MovementClient client = new MovementClient();
479         final String INVALID_SORT_ORDER_IDENTIFIER = "NO_DIRECTION";
480         Response res = client.readListSortedBy(MovementJAXBSchema.LOCATION_DATE
481                 + " " + INVALID_SORT_ORDER_IDENTIFIER);
482         try {
483                 assertStatusCode(res, testName);
484         } finally {
485                 if (res != null) {
486                 res.close();
487             }
488         }
489     }
490
491     // ---------------------------------------------------------------
492     // Cleanup of resources created during testing
493     // ---------------------------------------------------------------
494     /**
495      * Deletes all resources created by tests, after all tests have been run.
496      *
497      * This cleanup method will always be run, even if one or more tests fail.
498      * For this reason, it attempts to remove all resources created
499      * at any point during testing, even if some of those resources
500      * may be expected to be deleted by certain tests.
501      */
502     @AfterClass(alwaysRun = true)
503     public void cleanUp() {
504         String noTest = System.getProperty("noTestCleanup");
505         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
506             if (logger.isDebugEnabled()) {
507                 logger.debug("Skipping Cleanup phase ...");
508             }
509             return;
510         }
511         if (logger.isDebugEnabled()) {
512             logger.debug("Cleaning up temporary resources created for testing ...");
513         }
514         // Delete all Movement resource(s) created during this test.
515         MovementClient movementClient = new MovementClient();
516         for (String resourceId : movementIdsCreated) {
517             // Note: Any non-success responses are ignored and not reported.
518             movementClient.delete(resourceId).close();
519         }
520     }
521
522     // ---------------------------------------------------------------
523     // Utility methods used by tests above
524     // ---------------------------------------------------------------
525
526     @Override
527     protected String getServiceName() {
528         return SERVICE_NAME;
529     }
530
531     @Override
532     public String getServicePathComponent() {
533         return SERVICE_PATH_COMPONENT;
534     }
535
536     private String getCommonSchemaName() {
537         // FIXME: While this convention - appending a suffix to the name of
538         // the service's first unique URL path component - works, it would
539         // be preferable to get the common schema name from configuration.
540         //
541         // Such configuration is provided for example, on the services side, in
542         // org.collectionspace.services.common.context.AbstractServiceContextImpl
543         return getServicePathComponent() + "_" + "common";
544     }
545
546     public String qualifySortFieldName(String fieldName) {
547         return getCommonSchemaName() + DELIMITER_SCHEMA_AND_FIELD + fieldName;
548     }
549
550     public String asDescendingSort(String qualifiedFieldName) {
551         return qualifiedFieldName + " " + KEYWORD_DESCENDING_SEARCH;
552     }
553
554     /*
555      * A data provider that provides a set of unsorted values, which are
556      * to be used in populating (seeding) values in test records.
557      *
558      * Data elements provided for each test record consist of:
559      * * An integer, reflecting expected sort order.
560      * * US English text, to populate the value of a free text (String) field.
561      * * An ISO 8601 timestamp, to populate the value of a calendar date (dateTime) field.
562      */
563     @DataProvider(name = "unsortedValues")
564     public Object[][] unsortedValues() {
565         // FIXME: ADR Add a test record-specific string so we have the option of
566         // constraining tests to only test records, in list or search results.
567         final String TEST_RECORD_SPECIFIC_STRING = CLASS_NAME + " " + TEST_SPECIFIC_KEYWORD;
568         return new Object[][]{
569                     {1, "aardvark and plumeria. " + TEST_RECORD_SPECIFIC_STRING, "2009-01-29T00:00:05Z"},
570                     {10, "zounds! " + TEST_RECORD_SPECIFIC_STRING, "2010-08-31T00:00:00Z"},
571                     {3, "aardvark and plumeria. " + TEST_RECORD_SPECIFIC_STRING, "2010-08-30T00:00:00Z"},
572                     {7, "bat fling off wall. " + TEST_RECORD_SPECIFIC_STRING, "2010-08-30T00:00:00Z"},
573                     {4, "aardvarks and plumeria. " + TEST_RECORD_SPECIFIC_STRING, "2009-01-29T08:00:00Z"},
574                     {5, "aardvarks and plumeria. " + TEST_RECORD_SPECIFIC_STRING, "2009-05-29T00:00:00Z"},
575                     {2, "aardvark and plumeria. " + TEST_RECORD_SPECIFIC_STRING, "2009-05-29T00:00:00Z"},
576                     {9, "zounds! " + TEST_RECORD_SPECIFIC_STRING, "2009-05-29T00:00:00Z"}, // Identical to next record
577                     {8, "zounds! " + TEST_RECORD_SPECIFIC_STRING, "2009-05-29T00:00:00Z"},
578                     {6, "bat flies off ball. " + TEST_RECORD_SPECIFIC_STRING, "2009-05-29T00:00:00Z"}
579                 };
580     }
581
582     /*
583      * Create multiple test records, initially in unsorted order,
584      * using values for various fields obtained from the data provider.
585      */
586     @Test(dataProvider = "unsortedValues")
587     public void createList(int expectedSortOrder, String movementNote,
588             String locationDate) throws Exception {
589
590         String testName = "createList";
591         if (logger.isDebugEnabled()) {
592             logger.debug(getTestBanner(testName, CLASS_NAME));
593         }
594         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
595
596         // Iterates through the sets of values returned by the data provider,
597         // and creates a corresponding test record for each set of values.
598         create(movementNote, locationDate);
599     }
600
601     private void create(String movementNote, String locationDate) throws Exception {
602         String result = null;
603         
604         String testName = "create";
605         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
606
607         // Submit the request to the service and store the response.
608         PoxPayloadOut multipart = createMovementInstance(createIdentifier(),
609                 movementNote, locationDate);
610         MovementClient client = new MovementClient();
611         Response res = client.create(multipart);
612         try {
613                 assertStatusCode(res, testName);
614             // Store the IDs from every resource created by tests,
615             // so they can be deleted after tests have been run.
616                 result = extractId(res);
617         } finally {
618                 if (res != null) {
619                 res.close();
620             }
621         }
622         
623         if (result != null) {
624                 movementIdsCreated.add(result);
625         }
626     }
627
628     private MovementsCommon read(String csid) throws Exception {
629         String testName = "read";
630         testSetup(STATUS_OK, ServiceRequestType.READ);
631
632         // Submit the request to the service and store the response.
633         MovementClient client = new MovementClient();
634         Response res = client.read(csid);
635         MovementsCommon movementCommon = null;
636         try {
637                 assertStatusCode(res, testName);
638                 // Extract and return the common part of the record.
639                 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
640                 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
641                 if (payloadInputPart != null) {
642                         movementCommon = (MovementsCommon) payloadInputPart.getBody();
643                 }
644         } finally {
645                 if (res != null) {
646                 res.close();
647             }
648         }
649
650         return movementCommon;
651     }
652
653     private PoxPayloadOut createMovementInstance(
654             String movementReferenceNumber,
655             String movementNote,
656             String locationDate) {
657         MovementsCommon movementCommon = new MovementsCommon();
658         movementCommon.setMovementReferenceNumber(movementReferenceNumber);
659         movementCommon.setMovementNote(movementNote);
660         movementCommon.setLocationDate(locationDate);
661
662         PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
663         PayloadOutputPart commonPart =
664             multipart.addPart(new MovementClient().getCommonPartName(), movementCommon);
665         if (logger.isDebugEnabled()) {
666             logger.debug("to be created, movement common");
667             logger.debug(objectAsXmlString(movementCommon, MovementsCommon.class));
668         }
669
670         return multipart;
671     }
672
673     private AbstractCommonList readSortedList(String sortFieldName) throws Exception {
674         String testName = "readSortedList";
675         testSetup(STATUS_OK, ServiceRequestType.READ);
676
677         // Submit the request to the service and store the response.
678         MovementClient client = new MovementClient();
679
680         Response res = client.readListSortedBy(sortFieldName);
681         AbstractCommonList list = null;
682         try {
683                 assertStatusCode(res, testName);
684                 list = res.readEntity(AbstractCommonList.class);
685         } finally {
686                 if (res != null) {
687                 res.close();
688             }
689         }        
690
691         return list;
692
693     }
694
695     private AbstractCommonList keywordSearchSortedBy(String keywords,
696             String sortFieldName) throws Exception {
697         AbstractCommonList result = null;
698         
699         String testName = "keywordSearchSortedBy";
700         testSetup(STATUS_OK, ServiceRequestType.READ);
701
702         // Submit the request to the service and store the response.
703         MovementClient client = new MovementClient();
704
705         Response res = client.keywordSearchSortedBy(keywords, sortFieldName);
706         AbstractCommonList list = null;
707         try {
708                 assertStatusCode(res, testName);
709                 list = res.readEntity(AbstractCommonList.class);
710         } finally {
711                 if (res != null) {
712                 res.close();
713             }
714         }
715
716         return list;
717     }
718
719 }