]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-384: Minor cleanup of base classes in client test framework, in preparation...
authorAron Roberts <aron@socrates.berkeley.edu>
Fri, 21 May 2010 21:26:55 +0000 (21:26 +0000)
committerAron Roberts <aron@socrates.berkeley.edu>
Fri, 21 May 2010 21:26:55 +0000 (21:26 +0000)
services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java
services/client/src/main/java/org/collectionspace/services/client/test/BaseServiceTest.java

index 1c509d05b83853fffcbfecfee5a5172c73eee893..bd0e1faf34572b53d9aca917e3843f21d195e315 100644 (file)
@@ -32,7 +32,6 @@ import javax.ws.rs.core.Response;
 
 import org.collectionspace.services.jaxb.AbstractCommonList;
 import org.collectionspace.services.client.CollectionSpaceClient;
-//import org.collectionspace.services.client.AbstractServiceClientImpl;
 import org.jboss.resteasy.client.ClientResponse;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -49,24 +48,15 @@ import org.testng.annotations.Test;
  */
 public abstract class AbstractServiceTestImpl extends BaseServiceTest implements ServiceTest {
 
-       /** The logger. */
-       protected final Logger logger = LoggerFactory.getLogger(AbstractServiceTestImpl.class);
-       
-       /** The Constant DEFAULT_LIST_SIZE. */
-       static protected final int DEFAULT_LIST_SIZE = 10;
-       static protected final int DEFAULT_PAGINATEDLIST_SIZE = 10;
-       
-       /* Use this to keep track of resources to delete */
-       protected List<String> allResourceIdsCreated = new ArrayList<String>(); 
-
-//    // Success outcomes
-//    /* (non-Javadoc)
-//     * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
-//     */
-//    @Override
-//    public void create(String testName) throws Exception {
-//     //empty?
-//    }
+    /** The logger. */
+    protected final Logger logger = LoggerFactory.getLogger(AbstractServiceTestImpl.class);
+
+    /** The Constant DEFAULT_LIST_SIZE. */
+    static protected final int DEFAULT_LIST_SIZE = 10;
+    static protected final int DEFAULT_PAGINATEDLIST_SIZE = 10;
+
+    /* Use this to keep track of resources to delete */
+    protected List<String> allResourceIdsCreated = new ArrayList<String>();
     
     /**
      * Gets the logger.
@@ -77,20 +67,36 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
        return this.logger;
     }
 
+    // ---------------------------------------------------------------
+    // CRUD tests : CREATE tests
+    //
+    // (See below for utility methods in support of create list tests.)
+    // ---------------------------------------------------------------
+
+    // Success outcomes
+
+    /* (non-Javadoc)
+     * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
+     */
+    @Override
+    public abstract void create(String testName) throws Exception;
+
     /**
-     * Setup create.
+     * Sets up create tests.
      */
     protected void setupCreate() {
         setupCreate("Create");
     }
 
     /**
-     * Sets the up create.
+     * Sets up create tests.
      *
-     * @param label the new up create
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupCreate(String label) {
-       testSetup(Response.Status.CREATED.getStatusCode(), ServiceRequestType.CREATE, label);
+        EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
+        REQUEST_TYPE = ServiceRequestType.CREATE;
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
 
     /* (non-Javadoc)
@@ -99,8 +105,11 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
     @Override
     public abstract void createList(String testName) throws Exception;
 
-    // No setup required for createList()
+    // Note: No setup is required for createList(), as it currently
+    // just invokes create() multiple times.
+
     // Failure outcomes
+
     /* (non-Javadoc)
      * @see org.collectionspace.services.client.test.ServiceTest#createWithEmptyEntityBody(java.lang.String)
      */
@@ -109,24 +118,21 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
             throws Exception;
 
     /**
-     * Setup create with empty entity body.
+     * Sets up create tests with empty entity body.
      */
     protected void setupCreateWithEmptyEntityBody() {
         setupCreateWithEmptyEntityBody("CreateWithEmptyEntityBody");
     }
 
     /**
-     * Sets the up create with empty entity body.
+     * Sets up create tests with empty entity body.
      *
-     * @param label the new up create with empty entity body
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupCreateWithEmptyEntityBody(String label) {
-        clearSetup();
         EXPECTED_STATUS_CODE = Response.Status.BAD_REQUEST.getStatusCode();
         REQUEST_TYPE = ServiceRequestType.CREATE;
-        if (logger.isDebugEnabled()) {
-            banner(label);
-        }
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
 
     /* (non-Javadoc)
@@ -136,25 +142,21 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
     public abstract void createWithMalformedXml(String testName) throws Exception;
 
     /**
-     * Setup create with malformed xml.
+     * Sets up create tests with malformed xml.
      */
     protected void setupCreateWithMalformedXml() {
         setupCreateWithMalformedXml("CreateWithMalformedXml");
     }
 
     /**
-     * Sets the up create with malformed xml.
+     * Sets up create tests with malformed xml.
      *
-     * @param label the new up create with malformed xml
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupCreateWithMalformedXml(String label) {
-        clearSetup();
-        // Expected status code: 400 Bad Request
         EXPECTED_STATUS_CODE = Response.Status.BAD_REQUEST.getStatusCode();
         REQUEST_TYPE = ServiceRequestType.CREATE;
-        if (logger.isDebugEnabled()) {
-            banner(label);
-        }
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
 
     /* (non-Javadoc)
@@ -164,31 +166,29 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
     public abstract void createWithWrongXmlSchema(String testName) throws Exception;
 
     /**
-     * Setup create with wrong xml schema.
+     * Sets up create tests with wrong xml schema.
      */
     protected void setupCreateWithWrongXmlSchema() {
         setupCreateWithWrongXmlSchema("CreateWithWrongXmlSchema");
     }
 
     /**
-     * Sets the up create with wrong xml schema.
+     * Sets up create tests with wrong xml schema.
      *
      * @param label the new up create with wrong xml schema
      */
     protected void setupCreateWithWrongXmlSchema(String label) {
-        clearSetup();
-        // Expected status code: 400 Bad Request
         EXPECTED_STATUS_CODE = Response.Status.BAD_REQUEST.getStatusCode();
         REQUEST_TYPE = ServiceRequestType.CREATE;
-        if (logger.isDebugEnabled()) {
-            banner(label);
-        }
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
 
     // ---------------------------------------------------------------
     // CRUD tests : READ tests
     // ---------------------------------------------------------------
+
     // Success outcomes
+
     /* (non-Javadoc)
      * @see org.collectionspace.services.client.test.ServiceTest#read(java.lang.String)
      */
@@ -196,22 +196,25 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
     public abstract void read(String testName) throws Exception;
 
     /**
-     * Setup read.
+     * Sets up read tests.
      */
     protected void setupRead() {
         setupRead("Read");
     }
 
     /**
-     * Sets the up read.
+     * Sets up read tests.
      *
-     * @param label the new up read
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupRead(String label) {
-       testSetup(Response.Status.OK.getStatusCode(), ServiceRequestType.READ, label);
+        EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
+        REQUEST_TYPE = ServiceRequestType.READ;
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
 
     // Failure outcomes
+
     /* (non-Javadoc)
      * @see org.collectionspace.services.client.test.ServiceTest#readNonExistent(java.lang.String)
      */
@@ -219,196 +222,65 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
     public abstract void readNonExistent(String testName) throws Exception;
 
     /**
-     * Setup read non existent.
+     * Sets up read non existent tests.
      */
     protected void setupReadNonExistent() {
         setupReadNonExistent("ReadNonExistent");
     }
 
     /**
-     * Sets the up read non existent.
+     * Sets up read non existent tests.
      *
-     * @param label the new up read non existent
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupReadNonExistent(String label) {
-        // Expected status code: 404 Not Found
-       testSetup(Response.Status.NOT_FOUND.getStatusCode(), ServiceRequestType.READ, label);
-    }
-
-       /**
-        * Read list.
-        *
-        * @param testName the test name
-        * @param client the client
-        * @param pageSize the page size
-        * @param pageNumber the page number
-        * @return the abstract common list
-        * @throws Exception the exception
-        */
-       private AbstractCommonList readList(String testName,
-                       CollectionSpaceClient client,
-                       long pageSize, long pageNumber) throws Exception {
-               ClientResponse<AbstractCommonList> response =
-                       client.readList(Long.toString(pageSize), Long.toString(pageNumber));
-               AbstractCommonList result = null;
-               try {
-                       int statusCode = response.getStatus();
-       
-                       // Check the status code of the response: does it match
-                       // the expected response(s)?
-                       if (getLogger().isDebugEnabled()) {
-                               getLogger().debug(testName + ": status = " + statusCode);
-                       }
-                       Assert.assertTrue(this.REQUEST_TYPE.isValidStatusCode(statusCode),
-                                       invalidStatusCodeMessage(this.REQUEST_TYPE, statusCode));
-                       Assert.assertEquals(statusCode, this.EXPECTED_STATUS_CODE);
-       
-                       result = this.getAbstractCommonList(response);
-               } finally {
-                       response.releaseConnection();
-               }
-               
-               return result;
-       }
-
-    /**
-     * Creates the list.
-     *
-     * @param testName the test name
-     * @param listSize the list size
-     * @throws Exception the exception
-     */
-    protected void createPaginatedList(String testName, int listSize) throws Exception {
-        for (int i = 0; i < listSize; i++) {
-            create(testName);
-        }
-    }
-       
-    /*@Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
-    public void leafCeate(String testName) throws Exception {
-       this.create(testName);
-    }*/
-    
-    private void assertPaginationInfo(String testName,
-               AbstractCommonList list,
-               long expectedPageNum,
-               long expectedPageSize,
-               long expectedListSize,
-               long expectedTotalItems) {
-       Assert.assertNotNull(list);
-       
-       long pageNum = list.getPageNum();
-       Assert.assertEquals(pageNum, expectedPageNum);
-       if (getLogger().isDebugEnabled() == true) {
-               getLogger().debug(testName + ":" + "page number is " + pageNum);
-       }
-       
-       long pageSizeReturned = list.getPageSize();
-       Assert.assertEquals(pageSizeReturned, expectedPageSize);
-       if (getLogger().isDebugEnabled() == true) {
-               getLogger().debug(testName + ":" + "page size is " + list.getPageSize());
-       }
-       
-       long itemsInPage = list.getItemsInPage();
-       Assert.assertEquals(itemsInPage, expectedListSize);
-       if (getLogger().isDebugEnabled() == true) {
-               getLogger().debug(testName + ":" + "actual items in page was/were " + itemsInPage);
-       }               
-       
-       long totalItemsReturned = list.getTotalItems();
-       Assert.assertEquals(totalItemsReturned, expectedTotalItems);
-       if (getLogger().isDebugEnabled() == true) {
-               getLogger().debug(testName + ":" + "total number of items is " + list.getTotalItems());
-       }        
-    }
-    
-       /**
-        * Read paginated list.
-        *
-        * @param testName the test name
-        * @throws Exception the exception
-        */
-    @Test(dataProvider = "testName") /*, dataProviderClass = AbstractServiceTestImpl.class,
-           dependsOnMethods = {"leafCeate"}) */
-    public void readPaginatedList(String testName) throws Exception {
-        // Perform setup.
-        setupReadList(testName);
-        CollectionSpaceClient client = this.getClientInstance();
-        
-        // Get the current total number of items.
-        // If there are no items then create some
-        AbstractCommonList list = (AbstractCommonList) this.readList(testName, client, 1 /*pgSz*/, 0 /*pgNum*/);
-        if (list == null || list.getTotalItems() == 0) {
-               this.createPaginatedList(testName, DEFAULT_PAGINATEDLIST_SIZE);
-               list = (AbstractCommonList) this.readList(testName, client, 1 /*pgSz*/, 0 /*pgNum*/);
-        }
-
-        // Print out the current list size to be paginated
-        Assert.assertNotNull(list);
-        long totalItems = list.getTotalItems();
-        Assert.assertFalse(totalItems == 0);
-        if (getLogger().isDebugEnabled() == true) {
-               getLogger().debug(testName + ":" + "created list of " + 
-                               totalItems + " to be paginated.");
-        }
-        
-        long pageSize = totalItems / 3; //create up to 3 pages to iterate over
-        long pagesTotal = pageSize > 0 ? (totalItems / pageSize) : 0;
-        for (int i = 0; i < pagesTotal; i++) {
-               list = (AbstractCommonList) this.readList(testName, client, pageSize, i);
-               assertPaginationInfo(testName,
-                               list,
-                               i,                      //expected page number
-                               pageSize,       //expected page size
-                               pageSize,       //expected num of items in page
-                               totalItems);//expected total num of items
-        }
-        
-        // if there are any remainders be sure to paginate them as well
-        long mod = pageSize != 0 ? totalItems % pageSize : totalItems;
-        if (mod != 0) {
-               list = (AbstractCommonList) this.readList(testName, client, pageSize, pagesTotal);
-               assertPaginationInfo(testName,
-                               list, 
-                               pagesTotal, //expected page number
-                               pageSize,       //expected page size
-                               mod,            //expected num of items in page
-                               totalItems);//expected total num of items
-        }
+        EXPECTED_STATUS_CODE = Response.Status.NOT_FOUND.getStatusCode();
+        REQUEST_TYPE = ServiceRequestType.READ;
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
 
-       // ---------------------------------------------------------------
+    // ---------------------------------------------------------------
     // CRUD tests : READ (list, or multiple) tests
+    //
+    // (See below for utility methods in support of list tests.)
     // ---------------------------------------------------------------
+
     // Success outcomes
+
     /* (non-Javadoc)
         * @see org.collectionspace.services.client.test.ServiceTest#readList(java.lang.String)
         */
-       @Override
+    @Override
     public abstract void readList(String testName) throws Exception;
 
     /**
-     * Setup read list.
+     * Sets up read list tests.
      */
     protected void setupReadList() {
         setupReadList("ReadList");
     }
 
     /**
-     * Sets the up read list.
+     * Sets up read list tests.
      *
-     * @param label the new up read list
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupReadList(String label) {
-       testSetup(Response.Status.OK.getStatusCode(), ServiceRequestType.READ_LIST, label);
+        EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
+        REQUEST_TYPE = ServiceRequestType.READ_LIST;
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
 
     // Failure outcomes
+
     // None tested at present.
+
     // ---------------------------------------------------------------
     // CRUD tests : UPDATE tests
     // ---------------------------------------------------------------
+
     // Success outcomes
+
     /* (non-Javadoc)
      * @see org.collectionspace.services.client.test.ServiceTest#update(java.lang.String)
      */
@@ -416,23 +288,25 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
     public abstract void update(String testName) throws Exception;
 
     /**
-     * Setup update.
+     * Sets up update tests.
      */
     protected void setupUpdate() {
         setupUpdate("Update");
     }
 
     /**
-     * Sets the up update.
+     * Sets up update tests.
      *
-     * @param label the new up update
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupUpdate(String label) {
-        // Expected status code: 200 OK
-       testSetup(Response.Status.OK.getStatusCode(), ServiceRequestType.UPDATE, label);
+        EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
+        REQUEST_TYPE = ServiceRequestType.UPDATE;
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
 
     // Failure outcomes
+
     /* (non-Javadoc)
      * @see org.collectionspace.services.client.test.ServiceTest#updateWithEmptyEntityBody(java.lang.String)
      */
@@ -440,20 +314,21 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
     public abstract void updateWithEmptyEntityBody(String testName) throws Exception;
 
     /**
-     * Setup update with empty entity body.
+     * Sets up update tests with an empty entity body.
      */
     protected void setupUpdateWithEmptyEntityBody() {
         setupUpdateWithEmptyEntityBody("UpdateWithEmptyEntityBody");
     }
 
     /**
-     * Sets the up update with empty entity body.
+     * Sets up update tests with an empty entity body.
      *
-     * @param label the new up update with empty entity body
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupUpdateWithEmptyEntityBody(String label) {
-        // Expected status code: 400 Bad Request
-       testSetup(Response.Status.BAD_REQUEST.getStatusCode(), ServiceRequestType.UPDATE, label);
+        EXPECTED_STATUS_CODE = Response.Status.BAD_REQUEST.getStatusCode();
+        REQUEST_TYPE = ServiceRequestType.UPDATE;
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
 
     /* (non-Javadoc)
@@ -463,20 +338,21 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
     public abstract void updateWithMalformedXml(String testName) throws Exception;
 
     /**
-     * Setup update with malformed xml.
+     * Sets up update tests with malformed xml.
      */
     protected void setupUpdateWithMalformedXml() {
         setupUpdateWithMalformedXml("UpdateWithMalformedXml");
     }
 
     /**
-     * Sets the up update with malformed xml.
+     * Sets up update tests with malformed xml.
      *
-     * @param label the new up update with malformed xml
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupUpdateWithMalformedXml(String label) {
-        // Expected status code: 400 Bad Request
-       testSetup(Response.Status.BAD_REQUEST.getStatusCode(), ServiceRequestType.UPDATE, label);
+        EXPECTED_STATUS_CODE = Response.Status.BAD_REQUEST.getStatusCode();
+        REQUEST_TYPE = ServiceRequestType.UPDATE;
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
 
     /* (non-Javadoc)
@@ -486,49 +362,23 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
     public abstract void updateWithWrongXmlSchema(String testName) throws Exception;
 
     /**
-     * Setup update with wrong xml schema.
+     * Sets up update tests with wrong xml schema.
      */
     protected void setupUpdateWithWrongXmlSchema() {
         setupUpdateWithWrongXmlSchema("UpdateWithWrongXmlSchema");
     }
 
     /**
-     * Sets the up update with wrong xml schema.
+     * Sets up update tests with wrong xml schema.
      *
-     * @param label the new up update with wrong xml schema
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupUpdateWithWrongXmlSchema(String label) {
-        // Expected status code: 400 Bad Request
-       testSetup(Response.Status.BAD_REQUEST.getStatusCode(), ServiceRequestType.UPDATE, label);
+        EXPECTED_STATUS_CODE = Response.Status.BAD_REQUEST.getStatusCode();
+        REQUEST_TYPE = ServiceRequestType.UPDATE;
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
     
-    /**
-     * Deletes all resources created by tests, after all tests have been run.
-     *
-     * This cleanup method will always be run, even if one or more tests fail.
-     * For this reason, it attempts to remove all resources created
-     * at any point during testing, even if some of those resources
-     * may be expected to be deleted by certain tests.
-     */
-    @AfterClass(alwaysRun=true)
-    public void cleanUp() {
-        String noTest = System.getProperty("noTestCleanup");
-       if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
-            if (logger.isDebugEnabled()) {
-                logger.debug("Skipping Cleanup phase ...");
-            }
-            return;
-       }
-        if (logger.isDebugEnabled()) {
-            logger.debug("Cleaning up temporary resources created for testing ...");
-        }
-        CollectionSpaceClient client = this.getClientInstance();
-        for (String resourceId : allResourceIdsCreated) {
-            // Note: Any non-success responses are ignored and not reported.
-            client.delete(resourceId).releaseConnection();
-        }
-    }    
-
     /* (non-Javadoc)
      * @see org.collectionspace.services.client.test.ServiceTest#updateNonExistent(java.lang.String)
      */
@@ -536,26 +386,29 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
     public abstract void updateNonExistent(String testName) throws Exception;
 
     /**
-     * Setup update non existent.
+     * Sets up update non existent tests.
      */
     protected void setupUpdateNonExistent() {
         setupUpdateNonExistent("UpdateNonExistent");
     }
 
     /**
-     * Sets the up update non existent.
+     * Sets up update non existent tests
      *
-     * @param label the new up update non existent
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupUpdateNonExistent(String label) {
-        // Expected status code: 404 Not Found
-       testSetup(Response.Status.NOT_FOUND.getStatusCode(), ServiceRequestType.UPDATE, label);
+        EXPECTED_STATUS_CODE = Response.Status.NOT_FOUND.getStatusCode();
+        REQUEST_TYPE = ServiceRequestType.UPDATE;
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
 
     // ---------------------------------------------------------------
     // CRUD tests : DELETE tests
     // ---------------------------------------------------------------
+
     // Success outcomes
+
     /* (non-Javadoc)
      * @see org.collectionspace.services.client.test.ServiceTest#delete(java.lang.String)
      */
@@ -563,22 +416,25 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
     public abstract void delete(String testName) throws Exception;
 
     /**
-     * Setup delete.
+     * Sets up delete tests.
      */
     protected void setupDelete() {
         setupDelete("Delete");
     }
 
     /**
-     * Sets the up delete.
+     * Sets up delete tests.
      *
-     * @param label the new up delete
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupDelete(String label) {
-       testSetup(Response.Status.OK.getStatusCode(), ServiceRequestType.DELETE, label);
+        EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
+        REQUEST_TYPE = ServiceRequestType.DELETE;
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
     }
 
     // Failure outcomes
+
     /* (non-Javadoc)
      * @see org.collectionspace.services.client.test.ServiceTest#deleteNonExistent(java.lang.String)
      */
@@ -586,26 +442,200 @@ public abstract class AbstractServiceTestImpl extends BaseServiceTest implements
     public abstract void deleteNonExistent(String testName) throws Exception;
 
     /**
-     * Setup delete non existent.
+     * Sets up delete non existent tests.
      */
     protected void setupDeleteNonExistent() {
         setupDeleteNonExistent("DeleteNonExistent");
     }
 
     /**
-     * Sets the up delete non existent.
+     * Sets up delete non existent tests.
      *
-     * @param label the new up delete non existent
+     * @param label A label to be displayed prior to running this test.
      */
     protected void setupDeleteNonExistent(String label) {
-        clearSetup();
-        // Expected status code: 404 Not Found
         EXPECTED_STATUS_CODE = Response.Status.NOT_FOUND.getStatusCode();
         REQUEST_TYPE = ServiceRequestType.DELETE;
+        testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE, label);
+    }
+
+    // ---------------------------------------------------------------
+    // Utility methods to clean up resources created during tests.
+    // ---------------------------------------------------------------
+
+    /**
+     * Deletes all resources created by tests, after all tests have been run.
+     *
+     * This cleanup method will always be run, even if one or more tests fail.
+     * For this reason, it attempts to remove all resources created
+     * at any point during testing, even if some of those resources
+     * may be expected to be deleted by certain tests.
+     */
+    @AfterClass(alwaysRun=true)
+    public void cleanUp() {
+        String noTest = System.getProperty("noTestCleanup");
+       if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
+            if (logger.isDebugEnabled()) {
+                logger.debug("Skipping Cleanup phase ...");
+            }
+            return;
+       }
         if (logger.isDebugEnabled()) {
-            banner(label);
+            logger.debug("Cleaning up temporary resources created for testing ...");
+        }
+        CollectionSpaceClient client = this.getClientInstance();
+        for (String resourceId : allResourceIdsCreated) {
+            // Note: Any non-success responses are ignored and not reported.
+            client.delete(resourceId).releaseConnection();
         }
     }
+
+    // ---------------------------------------------------------------
+    // Utility methods in support of list tests.
+    // ---------------------------------------------------------------
+
+    /**
+     * Read list.
+     *
+     * @param testName the test name
+     * @param client the client
+     * @param pageSize the page size
+     * @param pageNumber the page number
+     * @return the abstract common list
+     * @throws Exception the exception
+     */
+    private AbstractCommonList readList(String testName,
+                    CollectionSpaceClient client,
+                    long pageSize, long pageNumber) throws Exception {
+        ClientResponse<AbstractCommonList> response =
+                client.readList(Long.toString(pageSize), Long.toString(pageNumber));
+        AbstractCommonList result = null;
+        try {
+            int statusCode = response.getStatus();
+
+            // Check the status code of the response: does it match
+            // the expected response(s)?
+            if (getLogger().isDebugEnabled()) {
+                    getLogger().debug(testName + ": status = " + statusCode);
+            }
+            Assert.assertTrue(this.REQUEST_TYPE.isValidStatusCode(statusCode),
+                            invalidStatusCodeMessage(this.REQUEST_TYPE, statusCode));
+            Assert.assertEquals(statusCode, this.EXPECTED_STATUS_CODE);
+
+            result = this.getAbstractCommonList(response);
+        } finally {
+            response.releaseConnection();
+        }
+
+        return result;
+    }
+
+    /**
+     * Creates the list.
+     *
+     * @param testName the test name
+     * @param listSize the list size
+     * @throws Exception the exception
+     */
+    protected void createPaginatedList(String testName, int listSize) throws Exception {
+        for (int i = 0; i < listSize; i++) {
+            create(testName);
+        }
+    }
+
+    /*@Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
+    public void leafCreate(String testName) throws Exception {
+       this.create(testName);
+    }*/
+
+    private void assertPaginationInfo(String testName,
+               AbstractCommonList list,
+               long expectedPageNum,
+               long expectedPageSize,
+               long expectedListSize,
+               long expectedTotalItems) {
+       Assert.assertNotNull(list);
+
+       long pageNum = list.getPageNum();
+       Assert.assertEquals(pageNum, expectedPageNum);
+       if (getLogger().isDebugEnabled() == true) {
+               getLogger().debug(testName + ":" + "page number is " + pageNum);
+       }
+
+       long pageSizeReturned = list.getPageSize();
+       Assert.assertEquals(pageSizeReturned, expectedPageSize);
+       if (getLogger().isDebugEnabled() == true) {
+               getLogger().debug(testName + ":" + "page size is " + list.getPageSize());
+       }
+
+       long itemsInPage = list.getItemsInPage();
+       Assert.assertEquals(itemsInPage, expectedListSize);
+       if (getLogger().isDebugEnabled() == true) {
+               getLogger().debug(testName + ":" + "actual items in page was/were " + itemsInPage);
+       }
+
+       long totalItemsReturned = list.getTotalItems();
+       Assert.assertEquals(totalItemsReturned, expectedTotalItems);
+       if (getLogger().isDebugEnabled() == true) {
+               getLogger().debug(testName + ":" + "total number of items is " + list.getTotalItems());
+       }
+    }
+
+    /**
+     * Read paginated list.
+     *
+     * @param testName the test name
+     * @throws Exception the exception
+     */
+    @Test(dataProvider = "testName") /*, dataProviderClass = AbstractServiceTestImpl.class,
+           dependsOnMethods = {"leafCreate"}) */
+    public void readPaginatedList(String testName) throws Exception {
+        // Perform setup.
+        setupReadList(testName);
+        CollectionSpaceClient client = this.getClientInstance();
+
+        // Get the current total number of items.
+        // If there are no items then create some
+        AbstractCommonList list = (AbstractCommonList) this.readList(testName, client, 1 /*pgSz*/, 0 /*pgNum*/);
+        if (list == null || list.getTotalItems() == 0) {
+               this.createPaginatedList(testName, DEFAULT_PAGINATEDLIST_SIZE);
+               list = (AbstractCommonList) this.readList(testName, client, 1 /*pgSz*/, 0 /*pgNum*/);
+        }
+
+        // Print out the current list size to be paginated
+        Assert.assertNotNull(list);
+        long totalItems = list.getTotalItems();
+        Assert.assertFalse(totalItems == 0);
+        if (getLogger().isDebugEnabled() == true) {
+               getLogger().debug(testName + ":" + "created list of " +
+                               totalItems + " to be paginated.");
+        }
+
+        long pageSize = totalItems / 3; //create up to 3 pages to iterate over
+        long pagesTotal = pageSize > 0 ? (totalItems / pageSize) : 0;
+        for (int i = 0; i < pagesTotal; i++) {
+               list = (AbstractCommonList) this.readList(testName, client, pageSize, i);
+               assertPaginationInfo(testName,
+                               list,
+                               i,                      //expected page number
+                               pageSize,       //expected page size
+                               pageSize,       //expected num of items in page
+                               totalItems);//expected total num of items
+        }
+
+        // if there are any remainders be sure to paginate them as well
+        long mod = pageSize != 0 ? totalItems % pageSize : totalItems;
+        if (mod != 0) {
+               list = (AbstractCommonList) this.readList(testName, client, pageSize, pagesTotal);
+               assertPaginationInfo(testName,
+                               list,
+                               pagesTotal, //expected page number
+                               pageSize,       //expected page size
+                               mod,            //expected num of items in page
+                               totalItems);//expected total num of items
+        }
+    }
+
 }
 
 
index 02747faa642369c6029a3dc9f90b4ae50498d986..9c47fc20fc75a869163be1ec45df6746887b4f70 100644 (file)
@@ -75,23 +75,31 @@ public abstract class BaseServiceTest {
     static protected final Logger logger = LoggerFactory.getLogger(BaseServiceTest.class);\r
     /** The Constant serviceClient. */\r
     protected static final TestServiceClient serviceClient = new TestServiceClient();\r
-    /** The NO n_ existen t_ id. */\r
+    /** The non-existent id. */\r
     protected final String NON_EXISTENT_ID = createNonExistentIdentifier();\r
-    /** The EXPECTE d_ statu s_ code. */\r
+    /** The expected status code. */\r
     protected int EXPECTED_STATUS_CODE = 0;\r
-    /** The REQUES t_ type. */\r
+    /** The request type type. */\r
     protected ServiceRequestType REQUEST_TYPE = ServiceRequestType.NON_EXISTENT;\r
     /** The Constant XML_DECLARATION. */\r
     protected static final String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";\r
     /** The Constant MALFORMED_XML_DATA. */\r
     protected static final String MALFORMED_XML_DATA = XML_DECLARATION\r
             + "<malformed_xml>wrong schema contents</malformed_xml";\r
-    /** The WRON g_ xm l_ schem a_ data. */\r
+    /** The wrong XML schema data. */\r
     protected final String WRONG_XML_SCHEMA_DATA = XML_DECLARATION\r
             + "<wrong_schema>wrong schema contents</wrong_schema>";\r
-    /** The NUL l_ charset. */\r
+    /** The null charset. */\r
     final String NULL_CHARSET = null;\r
 \r
+\r
+    /**\r
+     * Instantiates a new base service test.\r
+     */\r
+    public BaseServiceTest() {\r
+        super();\r
+    }\r
+\r
     /**\r
      * Gets the client.\r
      *\r
@@ -121,8 +129,8 @@ public abstract class BaseServiceTest {
     @DataProvider(name = "testName")\r
     public static Object[][] testName(Method m) {\r
         return new Object[][]{\r
-                    new Object[]{m.getName()}\r
-                };\r
+            new Object[]{m.getName()}\r
+        };\r
     }\r
 \r
     /**\r
@@ -145,28 +153,29 @@ public abstract class BaseServiceTest {
     }\r
 \r
     /**\r
-     * Initializes setup valuesfor a given test.\r
+     * Initializes setup values for a given test.\r
+     *\r
+     * @param expectedStatusCode  A status code expected to be returned in the response.\r
+     *\r
+     * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
+     *\r
+     * @param testName The name of the test being run.\r
      */\r
     protected void testSetup(\r
             int expectedStatusCode,\r
             ServiceRequestType reqType,\r
-            String bannerLabel) {\r
+            String testName) {\r
+\r
         clearSetup();\r
         EXPECTED_STATUS_CODE = expectedStatusCode;\r
         REQUEST_TYPE = reqType;\r
-        // Print a banner identifying the test that will be run.\r
+        \r
+        // Print a banner identifying the test being run.\r
         if (logger.isDebugEnabled()) {\r
-            banner(bannerLabel);\r
+            testBanner(testName);\r
         }\r
     }\r
 \r
-    /**\r
-     * Instantiates a new base service test.\r
-     */\r
-    public BaseServiceTest() {\r
-        super();\r
-    }\r
-\r
     /**\r
      * Returns an error message indicating that the status code returned by a\r
      * specific call to a service does not fall within a set of valid status\r
@@ -285,6 +294,9 @@ public abstract class BaseServiceTest {
         return statusCode;\r
     }\r
 \r
+    // FIXME: Move some or all of the methods below to a common client and\r
+    // server utilities package, when this package becomes available.\r
+\r
     /**\r
      * Extract id.\r
      *\r
@@ -304,7 +316,7 @@ public abstract class BaseServiceTest {
         }\r
         return id;\r
     }\r
-\r
\r
     /**\r
      * Creates the identifier.\r
      *\r
@@ -516,14 +528,24 @@ public abstract class BaseServiceTest {
     }\r
 \r
     /**\r
-     * Banner.\r
+     * Print label text inside a test-specific banner.\r
      *\r
-     * @param label the label\r
+     * @param testName The name of a test method.\r
+     */\r
+    protected static void testBanner(String testName) {\r
+        testName = (testName == null) ? "Test = no test name specified" : " Test = " + testName;\r
+        banner(testName);\r
+    }\r
+\r
+    /**\r
+     * Print label text inside a banner.\r
+     *\r
+     * @param label The label to be printed inside a banner.\r
      */\r
-    protected void banner(String label) {\r
+    protected static void banner(String label) {\r
         if (logger.isDebugEnabled()) {\r
             logger.debug("===================================================");\r
-            logger.debug(" Test = " + label);\r
+            logger.debug(" " + label);\r
             logger.debug("===================================================");\r
         }\r
     }\r