From: Aron Roberts Date: Fri, 21 May 2010 21:26:55 +0000 (+0000) Subject: CSPACE-384: Minor cleanup of base classes in client test framework, in preparation... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=3957052d151a79f443fc3e1bfaac60685fed4230;p=tmp%2Fjakarta-migration.git CSPACE-384: Minor cleanup of base classes in client test framework, in preparation for work on issues such as CSPACE-1685, as well as re-familiarization for current work on CSPACE-809. --- diff --git a/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java b/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java index 1c509d05b..bd0e1faf3 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java +++ b/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java @@ -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 allResourceIdsCreated = new ArrayList(); - -// // 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 allResourceIdsCreated = new ArrayList(); /** * 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 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 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 + } + } + } diff --git a/services/client/src/main/java/org/collectionspace/services/client/test/BaseServiceTest.java b/services/client/src/main/java/org/collectionspace/services/client/test/BaseServiceTest.java index 02747faa6..9c47fc20f 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/test/BaseServiceTest.java +++ b/services/client/src/main/java/org/collectionspace/services/client/test/BaseServiceTest.java @@ -75,23 +75,31 @@ public abstract class BaseServiceTest { static protected final Logger logger = LoggerFactory.getLogger(BaseServiceTest.class); /** The Constant serviceClient. */ protected static final TestServiceClient serviceClient = new TestServiceClient(); - /** The NO n_ existen t_ id. */ + /** The non-existent id. */ protected final String NON_EXISTENT_ID = createNonExistentIdentifier(); - /** The EXPECTE d_ statu s_ code. */ + /** The expected status code. */ protected int EXPECTED_STATUS_CODE = 0; - /** The REQUES t_ type. */ + /** The request type type. */ protected ServiceRequestType REQUEST_TYPE = ServiceRequestType.NON_EXISTENT; /** The Constant XML_DECLARATION. */ protected static final String XML_DECLARATION = ""; /** The Constant MALFORMED_XML_DATA. */ protected static final String MALFORMED_XML_DATA = XML_DECLARATION + "wrong schema contentswrong schema contents"; - /** The NUL l_ charset. */ + /** The null charset. */ final String NULL_CHARSET = null; + + /** + * Instantiates a new base service test. + */ + public BaseServiceTest() { + super(); + } + /** * Gets the client. * @@ -121,8 +129,8 @@ public abstract class BaseServiceTest { @DataProvider(name = "testName") public static Object[][] testName(Method m) { return new Object[][]{ - new Object[]{m.getName()} - }; + new Object[]{m.getName()} + }; } /** @@ -145,28 +153,29 @@ public abstract class BaseServiceTest { } /** - * Initializes setup valuesfor a given test. + * Initializes setup values for a given test. + * + * @param expectedStatusCode A status code expected to be returned in the response. + * + * @param serviceRequestType A type of service request (e.g. CREATE, DELETE). + * + * @param testName The name of the test being run. */ protected void testSetup( int expectedStatusCode, ServiceRequestType reqType, - String bannerLabel) { + String testName) { + clearSetup(); EXPECTED_STATUS_CODE = expectedStatusCode; REQUEST_TYPE = reqType; - // Print a banner identifying the test that will be run. + + // Print a banner identifying the test being run. if (logger.isDebugEnabled()) { - banner(bannerLabel); + testBanner(testName); } } - /** - * Instantiates a new base service test. - */ - public BaseServiceTest() { - super(); - } - /** * Returns an error message indicating that the status code returned by a * specific call to a service does not fall within a set of valid status @@ -285,6 +294,9 @@ public abstract class BaseServiceTest { return statusCode; } + // FIXME: Move some or all of the methods below to a common client and + // server utilities package, when this package becomes available. + /** * Extract id. * @@ -304,7 +316,7 @@ public abstract class BaseServiceTest { } return id; } - + /** * Creates the identifier. * @@ -516,14 +528,24 @@ public abstract class BaseServiceTest { } /** - * Banner. + * Print label text inside a test-specific banner. * - * @param label the label + * @param testName The name of a test method. + */ + protected static void testBanner(String testName) { + testName = (testName == null) ? "Test = no test name specified" : " Test = " + testName; + banner(testName); + } + + /** + * Print label text inside a banner. + * + * @param label The label to be printed inside a banner. */ - protected void banner(String label) { + protected static void banner(String label) { if (logger.isDebugEnabled()) { logger.debug("==================================================="); - logger.debug(" Test = " + label); + logger.debug(" " + label); logger.debug("==================================================="); } }