From 5a2daa0de103e59dcee57b267beabf6db67c87f1 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Wed, 2 Sep 2009 01:43:01 +0000 Subject: [PATCH] CSPACE-360,CSPACE-388: Renamed methods in Intake client classes for consistency with test classes. --- .../test/RelationIntegrationTest.java | 2 +- .../test/CollectionObjectServiceTest.java | 18 +- .../services/client/IntakeClient.java | 20 +- .../services/client/IntakeProxy.java | 10 +- .../client/test/IntakeServiceTest.java | 846 ++++++++---------- 5 files changed, 418 insertions(+), 478 deletions(-) diff --git a/services/IntegrationTests/src/test/java/org/collectionspace/services/ItegrationTests/test/RelationIntegrationTest.java b/services/IntegrationTests/src/test/java/org/collectionspace/services/ItegrationTests/test/RelationIntegrationTest.java index e53ba456d..31dbf1860 100644 --- a/services/IntegrationTests/src/test/java/org/collectionspace/services/ItegrationTests/test/RelationIntegrationTest.java +++ b/services/IntegrationTests/src/test/java/org/collectionspace/services/ItegrationTests/test/RelationIntegrationTest.java @@ -100,7 +100,7 @@ public class RelationIntegrationTest extends CollectionSpaceIntegrationTest { // Next, create an Intake record Intake intake = new Intake(); fillIntake(intake, createIdentifier()); - ClientResponse intakeResponse = intakeClient.createIntake(intake); + ClientResponse intakeResponse = intakeClient.create(intake); Assert.assertEquals(intakeResponse.getStatus(), Response.Status.CREATED.getStatusCode()); String intakeCsid = extractId(intakeResponse); diff --git a/services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java b/services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java index 97b8b322f..42c64cc02 100644 --- a/services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java +++ b/services/collectionobject/client/src/test/java/org/collectionspace/services/client/test/CollectionObjectServiceTest.java @@ -49,7 +49,7 @@ public class CollectionObjectServiceTest extends AbstractServiceTest { // Instance variables specific to this test. private CollectionObjectClient client = new CollectionObjectClient(); final String SERVICE_PATH_COMPONENT = "collectionobjects"; - private String knownObjectId = null; + private String knownObjectId = null; // --------------------------------------------------------------- // CRUD tests : CREATE tests @@ -89,7 +89,7 @@ public class CollectionObjectServiceTest extends AbstractServiceTest { @Test(dependsOnMethods = {"create"}) public void createList() { for(int i = 0; i < 3; i++){ - create (); + create(); } } @@ -424,19 +424,21 @@ public class CollectionObjectServiceTest extends AbstractServiceTest { @Override public String getServicePathComponent() { - // @TODO Determine if it is possible to obtain this value programmatically. + // @TODO Determine if it is possible to obtain this + // value programmatically. // - // We set this in an annotation in the CollectionObjectProxy interface, for instance. - // We also set service-specific constants in each service module, which might - // also return this value. + // We set this in an annotation in the CollectionObjectProxy + // interface, for instance. We also set service-specific + // constants in each service module, which might also + // return this value. return SERVICE_PATH_COMPONENT; } private CollectionObject createCollectionObject(String identifier) { CollectionObject collectionObject = createCollectionObject( - "objectNumber-" + identifier, - "objectName-" + identifier); + "objectNumber-" + identifier, + "objectName-" + identifier); return collectionObject; } diff --git a/services/intake/client/src/main/java/org/collectionspace/services/client/IntakeClient.java b/services/intake/client/src/main/java/org/collectionspace/services/client/IntakeClient.java index 607493911..ec51628ec 100644 --- a/services/intake/client/src/main/java/org/collectionspace/services/client/IntakeClient.java +++ b/services/intake/client/src/main/java/org/collectionspace/services/client/IntakeClient.java @@ -51,8 +51,8 @@ public class IntakeClient extends BaseServiceClient { * @return * @see org.collectionspace.hello.client.IntakeProxy#getIntake() */ - public ClientResponse getIntakeList() { - return intakeProxy.getIntakeList(); + public ClientResponse readList() { + return intakeProxy.readList(); } /** @@ -60,8 +60,8 @@ public class IntakeClient extends BaseServiceClient { * @return * @see org.collectionspace.hello.client.IntakeProxy#getIntake(java.lang.String) */ - public ClientResponse getIntake(String csid) { - return intakeProxy.getIntake(csid); + public ClientResponse read(String csid) { + return intakeProxy.read(csid); } /** @@ -69,8 +69,8 @@ public class IntakeClient extends BaseServiceClient { * @return * @see org.collectionspace.hello.client.IntakeProxy#createIntake(org.collectionspace.hello.Intake) */ - public ClientResponse createIntake(Intake intake) { - return intakeProxy.createIntake(intake); + public ClientResponse create(Intake intake) { + return intakeProxy.create(intake); } /** @@ -79,8 +79,8 @@ public class IntakeClient extends BaseServiceClient { * @return * @see org.collectionspace.hello.client.IntakeProxy#updateIntake(java.lang.Long, org.collectionspace.hello.Intake) */ - public ClientResponse updateIntake(String csid, Intake intake) { - return intakeProxy.updateIntake(csid, intake); + public ClientResponse update(String csid, Intake intake) { + return intakeProxy.update(csid, intake); } /** @@ -88,7 +88,7 @@ public class IntakeClient extends BaseServiceClient { * @return * @see org.collectionspace.hello.client.IntakeProxy#deleteIntake(java.lang.Long) */ - public ClientResponse deleteIntake(String csid) { - return intakeProxy.deleteIntake(csid); + public ClientResponse delete(String csid) { + return intakeProxy.delete(csid); } } diff --git a/services/intake/client/src/main/java/org/collectionspace/services/client/IntakeProxy.java b/services/intake/client/src/main/java/org/collectionspace/services/client/IntakeProxy.java index 7a5abee4c..40baeef75 100644 --- a/services/intake/client/src/main/java/org/collectionspace/services/client/IntakeProxy.java +++ b/services/intake/client/src/main/java/org/collectionspace/services/client/IntakeProxy.java @@ -23,24 +23,24 @@ import org.jboss.resteasy.client.ClientResponse; public interface IntakeProxy { @GET - ClientResponse getIntakeList(); + ClientResponse readList(); //(C)reate @POST - ClientResponse createIntake(Intake co); + ClientResponse create(Intake co); //(R)ead @GET @Path("/{csid}") - ClientResponse getIntake(@PathParam("csid") String csid); + ClientResponse read(@PathParam("csid") String csid); //(U)pdate @PUT @Path("/{csid}") - ClientResponse updateIntake(@PathParam("csid") String csid, Intake co); + ClientResponse update(@PathParam("csid") String csid, Intake co); //(D)elete @DELETE @Path("/{csid}") - ClientResponse deleteIntake(@PathParam("csid") String csid); + ClientResponse delete(@PathParam("csid") String csid); } \ No newline at end of file diff --git a/services/intake/client/src/test/java/org/collectionspace/services/client/test/IntakeServiceTest.java b/services/intake/client/src/test/java/org/collectionspace/services/client/test/IntakeServiceTest.java index 9f774ce32..c686465a1 100644 --- a/services/intake/client/src/test/java/org/collectionspace/services/client/test/IntakeServiceTest.java +++ b/services/intake/client/src/test/java/org/collectionspace/services/client/test/IntakeServiceTest.java @@ -21,23 +21,21 @@ * limitations under the License. */ -package org.collectionspace.services.client.test; + package org.collectionspace.services.client.test; -import java.util.ArrayList; import java.util.List; -import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.Marshaller; -import org.jboss.resteasy.client.ClientResponse; -import org.testng.Assert; -import org.testng.annotations.Test; +import javax.ws.rs.core.Response.Status; +import org.collectionspace.services.client.IntakeClient; +import org.collectionspace.services.client.test.ServiceRequestType; import org.collectionspace.services.intake.Intake; import org.collectionspace.services.intake.IntakeList; -import org.collectionspace.services.client.IntakeClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + +import org.jboss.resteasy.client.ClientResponse; + +import org.testng.Assert; +import org.testng.annotations.Test; /** * IntakeServiceTest, carries out tests against a @@ -46,469 +44,409 @@ import org.slf4j.LoggerFactory; * $LastChangedRevision: 511 $ * $LastChangedDate: 2009-08-06 20:16:16 +0000 (Thu, 06 Aug 2009) $ */ -public class IntakeServiceTest { - - private IntakeClient intakeClient = new IntakeClient(); - private String knownIntakeId = null; - private final String NON_EXISTENT_ID = createNonExistentIdentifier(); - final Logger logger = LoggerFactory.getLogger(IntakeServiceTest.class); - - // --------------------------------------------------------------- - // Service Discovery tests - // --------------------------------------------------------------- - - // TBA - - - // --------------------------------------------------------------- - // CRUD tests : CREATE tests - // --------------------------------------------------------------- - - // Success outcomes - // ---------------- - - /** - * Tests creation of a new Intake. - * - * Expected status code: 201 Created - * - * Also expected: The 'Location' header contains the URL for the newly created object. - * This is required by the extractId() utility method, below. - * - * The newly-created Intake is also used by other test(s) - * (e.g. update, delete) which follow, below. - */ - @Test - public void createIntake() { - String identifier = this.createIdentifier(); - - Intake intake = createIntake(identifier); - ClientResponse res = intakeClient.createIntake(intake); - verbose("createIntake: status = " + res.getStatus()); - Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode()); - - // Store the ID returned from this create operation for additional tests below. - knownIntakeId = extractId(res); - } - - /** - * Creates two or more new Intakes. - * - * Repeatedly calls the createIntake test, above, and relies on its - * test assertions. - * - * Expected status code: 201 Created - * - * The newly-created Intakes are also used by other test(s) - * (e.g. read multiple/list) which follow, below. - */ - @Test(dependsOnMethods = {"createIntake"}) - public void createCollection() { - for(int i = 0; i < 3; i++){ - this.createIntake(); +public class IntakeServiceTest extends AbstractServiceTest { + + // Instance variables specific to this test. + private IntakeClient client = new IntakeClient(); + final String SERVICE_PATH_COMPONENT = "intakes"; + private String knownObjectId = null; + + + // --------------------------------------------------------------- + // CRUD tests : CREATE tests + // --------------------------------------------------------------- + + // Success outcomes + + @Override + @Test + public void create() { + + // Perform setup, such as initializing the type of service request + // (e.g. CREATE, DELETE), its valid and expected status codes, and + // its associated HTTP method name (e.g. POST, DELETE). + setupCreate(); + + // Submit the request to the service and store the response. + String identifier = createIdentifier(); + Intake intake = createIntake(identifier); + ClientResponse res = client.create(intake); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match the expected response(s)? + // + // Does it fall within the set of valid status codes? + // Does it exactly match the expected status code? + verbose("create: status = " + statusCode); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + + // Store the ID returned from this create operation for additional tests below. + knownObjectId = extractId(res); } - } - - // Failure outcomes - // ---------------- - - /** - * Tests creation of a Intake by sending a null to the client proxy. - * - * Expected status code: (none) - * - * Expected result: IllegalArgumentException - * (Make sure this is a reported exception in the called class.) - */ - @Test(dependsOnMethods = {"createIntake"}, expectedExceptions = IllegalArgumentException.class) - public void createNullIntake() { - ClientResponse res = intakeClient.createIntake(null); - } - - /** - * Tests creation of an Intake by sending bad data - * (e.g. in a format that doesn't match the Intake schema) - * in the entity body of the request. - * - * Expected status code: 400 Bad Request - */ -/* - @Test(dependsOnMethods = {"createIntake"}) - public void createIntakeWithBadData() { - // Currently only a stub. - } -*/ - /** - * Tests creation of an Intake by a user who - * is not authorized to perform this action. - * - * Expected status code: 403 Forbidden - */ -/* - @Test(dependsOnMethods = {"createIntake"}) - public void createIntakeWithUnauthorizedUser() { - // Currently only a stub. - } -*/ + @Override + @Test(dependsOnMethods = {"create"}) + public void createList() { + for(int i = 0; i < 3; i++){ + create(); + } + } - /** - * Tests creation of a duplicate Intake, whose unique resource identifier - * duplicates that of an existing Intake. - * - * Expected status code: 409 Conflict - */ -/* - @Test(dependsOnMethods = {"createIntake"}) - public void createDuplicateIntake() { - Intake intake = createIntake(knownIntakeId); - ClientResponse res = - intakeClient.createIntake(intake); - verbose("createDuplicateIntake: status = " + res.getStatus()); - Assert.assertEquals(res.getStatus(), Response.Status.CONFLICT.getStatusCode()); - } -*/ + // Failure outcomes + + @Override + @Test(dependsOnMethods = {"create"}, expectedExceptions = IllegalArgumentException.class) + public void createNull() { + ClientResponse res = client.create(null); + } + + // Placeholders until the two tests below can be uncommented. See Issue CSPACE-401. + public void createWithMalformedXml() {} + public void createWithWrongXmlSchema() {} - // --------------------------------------------------------------- - // CRUD tests : READ tests - // --------------------------------------------------------------- - - // Success outcomes - // ---------------- - - /** - * Tests reading (i.e. retrieval) of a Intake. - * - * Expected status code: 200 OK - */ - @Test(dependsOnMethods = {"createIntake"}) - public void getIntake() { - ClientResponse res = - intakeClient.getIntake(knownIntakeId); - verbose("getIntake: status = " + res.getStatus()); - Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode()); - } - - // Failure outcomes - // ---------------- - - /** - * Tests reading (i.e. retrieval) of a Intake by a user who - * is not authorized to perform this action. - * - * Expected status code: 403 Forbidden - */ /* - @Test(dependsOnMethods = {"getIntake"}) - public void getIntakeWithUnauthorizedUser() { - // Currently only a stub - } -*/ + @Override + @Test(dependsOnMethods = {"create", "testSubmitRequest"}) + public void createWithMalformedXml() { + + // Perform setup. + setupCreateWithMalformedXml(); + + // Submit the request to the service and store the response. + String method = REQUEST_TYPE.httpMethodName(); + String url = getServiceRootURL(); + final String entity = MALFORMED_XML_DATA; // Constant from abstract base class. + int statusCode = submitRequest(method, url, entity); + + // Check the status code of the response: does it match the expected response(s)? + verbose("createWithMalformedXml url=" + url + " status=" + statusCode); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + } - /** - * Tests reading (i.e. retrieval) of a non-existent Intake, - * whose resource identifier does not exist at the specified URL. - * - * Expected status code: 404 Not Found - */ - @Test(dependsOnMethods = {"getIntake"}) - public void getNonExistentIntake() { - ClientResponse res = - intakeClient.getIntake(NON_EXISTENT_ID); - verbose("getNonExistentIntake: status = " + res.getStatus()); - Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); - } - - - // --------------------------------------------------------------- - // CRUD tests : READ (list, or multiple) tests - // --------------------------------------------------------------- - - // Success outcomes - // ---------------- - - /** - * Tests reading (i.e. retrieval) of a list of multiple Intakes. - * - * Expected status code: 200 OK - * - * Also expected: The entity body in the response contains - * a representation of the list of Intakes. - */ - @Test(dependsOnMethods = {"createCollection"}) - public void getIntakeList() { - // The resource method is expected to return at least an empty list - ClientResponse res = intakeClient.getIntakeList(); - IntakeList coList = res.getEntity(); - verbose("getIntakeList: status = " + res.getStatus()); - Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode()); - - List coItemList = - coList.getIntakeListItem(); - int i = 0; - for(IntakeList.IntakeListItem pli : coItemList){ - verbose("getIntakeList: list-item[" + i + "] csid=" + pli.getCsid()); - verbose("getIntakeList: list-item[" + i + "] entryNumber=" + pli.getEntryNumber()); - verbose("getIntakeList: list-item[" + i + "] URI=" + pli.getUri()); - i++; + @Override + @Test(dependsOnMethods = {"create", "testSubmitRequest"}) + public void createWithWrongXmlSchema() { + + // Perform setup. + setupCreateWithWrongXmlSchema(); + + // Submit the request to the service and store the response. + String method = REQUEST_TYPE.httpMethodName(); + String url = getServiceRootURL(); + final String entity = WRONG_XML_SCHEMA_DATA; + int statusCode = submitRequest(method, url, entity); + + // Check the status code of the response: does it match the expected response(s)? + verbose("createWithWrongSchema url=" + url + " status=" + statusCode); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); } - } - - /** - * Tests reading (i.e. retrieval) of a list of multiple Intakes - * when the contents of the list are expected to be empty. - * - * Expected status code: 200 OK - * (Note: *not* 204 No Content) - * - * Also expected: The entity body in the response contains - * a representation of an empty list of Intakes. - */ -/* - @Test(dependsOnMethods = {"getIntakeList"}) - public void getIntakeEmptyList() { - // Currently only a stub. - } -*/ - - // Failure outcomes - // ---------------- - - /** - * Tests reading (i.e. retrieval) of a list of Intakes - * when sending unrecognized query parameters with the request. - * - * Expected status code: 400 Bad Request - */ -/* - @Test(dependsOnMethods = {"getIntakeList"}) - public void getIntakeListWithBadParams() { - // Currently only a stub. - } */ - /** - * Tests reading (i.e. retrieval) of a list of Intakes by a user who - * is not authorized to perform this action. - * - * Expected status code: 403 Forbidden - */ -/* - @Test(dependsOnMethods = {"getIntakeList"}) - public void getIntakeListWithUnauthorizedUser() { - // Currently only a stub. - } -*/ + // --------------------------------------------------------------- + // CRUD tests : READ tests + // --------------------------------------------------------------- + + // Success outcomes - - - // --------------------------------------------------------------- - // CRUD tests : UPDATE tests - // --------------------------------------------------------------- - - // Success outcomes - // ---------------- - - /** - * Tests updating the content of a Intake. - * - * Expected status code: 200 OK - * - * Also expected: The entity body in the response contains - * a representation of the updated Intake. - */ - @Test(dependsOnMethods = {"createIntake"}) - public void updateIntake() { - ClientResponse res = - intakeClient.getIntake(knownIntakeId); - verbose("getIntake: status = " + res.getStatus()); - Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode()); - Intake intake = res.getEntity(); - verbose("Got Intake to update with ID: " + knownIntakeId, - intake, Intake.class); - - //intake.setCsid("updated-" + knownIntakeId); - intake.setEntryNumber("updated-" + intake.getEntryNumber()); - intake.setEntryDate("updated-" + intake.getEntryDate()); + @Override + @Test(dependsOnMethods = {"create"}) + public void read() { - // make call to update service - res = - intakeClient.updateIntake(knownIntakeId, intake); - verbose("updateIntake: status = " + res.getStatus()); - Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode()); + // Perform setup. + setupRead(); + + // Submit the request to the service and store the response. + ClientResponse res = client.read(knownObjectId); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match the expected response(s)? + verbose("read: status = " + statusCode); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + } + + @Override + @Test(dependsOnMethods = {"read"}) + public void readNonExistent() { + + // Perform setup. + setupReadNonExistent(); + + // Submit the request to the service and store the response. + ClientResponse res = client.read(NON_EXISTENT_ID); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match the expected response(s)? + verbose("readNonExistent: status = " + res.getStatus()); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + } + + + // --------------------------------------------------------------- + // CRUD tests : READ_LIST tests + // --------------------------------------------------------------- + + // Success outcomes + + @Override + @Test(dependsOnMethods = {"createList"}) + public void readList() { - // check the response - Intake updatedIntake = res.getEntity(); - Assert.assertEquals(updatedIntake.getEntryDate(), intake.getEntryDate()); - verbose("updateIntake ", updatedIntake, Intake.class); - } - - // Failure outcomes - // ---------------- - - /** - * Tests updating the content of a Intake by sending bad data - * (e.g. in a format that doesn't match the Intake schema) - * in the entity body of the request. - * - * Expected status code: 400 Bad Request - */ -/* - @Test(dependsOnMethods = {"updateIntake"}) - public void updateIntakeWithBadData() { - // Currently only a stub. - } -*/ + // Perform setup. + setupReadList(); + + // Submit the request to the service and store the response. + ClientResponse res = client.readList(); + IntakeList list = res.getEntity(); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match the expected response(s)? + verbose("readList: status = " + res.getStatus()); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + + // Optionally output additional data about list members for debugging. + boolean iterateThroughList = false; + if (iterateThroughList && logger.isDebugEnabled()) { + List items = + list.getIntakeListItem(); + int i = 0; + for(IntakeList.IntakeListItem item : items){ + verbose("readList: list-item[" + i + "] csid=" + item.getCsid()); + verbose("readList: list-item[" + i + "] objectNumber=" + item.getEntryNumber()); + verbose("readList: list-item[" + i + "] URI=" + item.getUri()); + i++; + } + } + + } + + // Failure outcomes + + // None at present. - /** - * Tests updating the content of a Intake by a user who - * is not authorized to perform this action. - * - * Expected status code: 403 Forbidden - */ -/* - @Test(dependsOnMethods = {"updateIntake"}) - public void updateIntakeWithUnauthorizedUser() { - // Currently only a stub. - } -*/ - /** - * Tests updating the content of a non-existent Intake, whose - * resource identifier does not exist. - * - * Expected status code: 404 Not Found - */ - @Test(dependsOnMethods = {"updateIntake"}) - public void updateNonExistentIntake() { - // Note: The ID used in this 'create' call may be arbitrary. - // The only relevant ID may be the one used in updateIntake(), below. - Intake intake = createIntake(NON_EXISTENT_ID); - // make call to update service - ClientResponse res = - intakeClient.updateIntake(NON_EXISTENT_ID, intake); - verbose("createIntake: status = " + res.getStatus()); - Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); - } - - - // --------------------------------------------------------------- - // CRUD tests : DELETE tests - // --------------------------------------------------------------- - - // Success outcomes - // ---------------- - - /** - * Tests deleting a Intake. - * - * Expected status code: 200 OK - */ - @Test(dependsOnMethods = {"createIntake", "getIntake", "updateIntake"}) - public void deleteIntake() { - verbose("Calling deleteIntake: " + knownIntakeId); - ClientResponse res = intakeClient.deleteIntake(knownIntakeId); - verbose("deleteIntake csid=" + knownIntakeId); - verbose("deleteIntake: status = " + res.getStatus()); - Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode()); - } - - // Failure outcomes - // ---------------- - - /** - * Tests deleting a Intake by a user who - * is not authorized to perform this action. - * - * Expected status code: 403 Forbidden - */ + // --------------------------------------------------------------- + // CRUD tests : UPDATE tests + // --------------------------------------------------------------- + + // Success outcomes + + @Override + @Test(dependsOnMethods = {"create"}) + public void update() { + + // Perform setup. + setupUpdate(); + + // Retrieve an existing resource that we can update. + ClientResponse res = client.read(knownObjectId); + verbose("read: status = " + res.getStatus()); + Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE); + Intake intake = res.getEntity(); + verbose("Got object to update with ID: " + knownObjectId, + intake, Intake.class); + + // Update the content of this resource. + intake.setEntryNumber("updated-" + intake.getEntryNumber()); + intake.setEntryDate("updated-" + intake.getEntryDate()); + + // Submit the request to the service and store the response. + res = client.update(knownObjectId, intake); + int statusCode = res.getStatus(); + Intake updatedObject = res.getEntity(); + + // Check the status code of the response: does it match the expected response(s)? + verbose("update: status = " + res.getStatus()); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + + // Check the contents of the response: does it match what was submitted? + verbose("update: ", updatedObject, Intake.class); + Assert.assertEquals(updatedObject.getEntryDate(), + intake.getEntryDate(), + "Data in updated object did not match submitted data."); + } + + // Placeholders until the two tests below can be uncommented. See Issue CSPACE-401. + public void updateWithMalformedXml() {} + public void updateWithWrongXmlSchema() {} + /* - @Test(dependsOnMethods = {"deleteIntake"}) - public void deleteIntakeWithUnauthorizedUser() { - // Currently only a stub. - } + @Override + @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"}) + public void updateWithMalformedXml() { + + // Perform setup. + setupUpdateWithMalformedXml(); + + // Submit the request to the service and store the response. + String method = REQUEST_TYPE.httpMethodName(); + String url = getResourceURL(knownObjectId); + final String entity = MALFORMED_XML_DATA; // Constant from abstract base class. + int statusCode = submitRequest(method, url, entity); + + // Check the status code of the response: does it match the expected response(s)? + verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + } + + @Override + @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"}) + public void updateWithWrongXmlSchema() { + + // Perform setup. + setupUpdateWithWrongXmlSchema(); + + // Submit the request to the service and store the response. + String method = REQUEST_TYPE.httpMethodName(); + String url = getResourceURL(knownObjectId); + final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class. + int statusCode = submitRequest(method, url, entity); + + // Check the status code of the response: does it match the expected response(s)? + verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + } */ - /** - * Tests deleting a non-existent Intake, whose - * resource identifier does not exist at the specified URL. - * - * Expected status code: 404 Not Found - */ - @Test(dependsOnMethods = {"deleteIntake"}) - public void deleteNonExistentIntake() { - verbose("Calling deleteIntake: " + NON_EXISTENT_ID); - ClientResponse res = - intakeClient.deleteIntake(NON_EXISTENT_ID); - verbose("deleteIntake: status = " + res.getStatus()); - Assert.assertEquals(res.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); - } - - - // --------------------------------------------------------------- - // Utility methods used by tests above - // --------------------------------------------------------------- - - private Intake createIntake(String identifier) { - Intake intake = createIntake("entryNumber-" + identifier, - "entryDate-" + identifier); - - return intake; - } - - private Intake createIntake(String entryNumber, String entryDate) { - Intake intake = new Intake(); - - intake.setEntryNumber(entryNumber); - intake.setEntryDate(entryDate); - - return intake; - } - - private String extractId(ClientResponse res) { - MultivaluedMap mvm = res.getMetadata(); - String uri = (String) ((ArrayList) mvm.get("Location")).get(0); - verbose("extractId:uri=" + uri); - String[] segments = uri.split("/"); - String id = segments[segments.length - 1]; - verbose("id=" + id); - return id; - } - - private void verbose(String msg) { -// if(logger.isInfoEnabled()){ -// logger.debug(msg); -// } - System.out.println(msg); - } - - private void verbose(String msg, Object o, Class clazz) { - try{ - verbose(msg); - JAXBContext jc = JAXBContext.newInstance(clazz); - Marshaller m = jc.createMarshaller(); - m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, - Boolean.TRUE); - m.marshal(o, System.out); - }catch(Exception e){ - e.printStackTrace(); + @Override + @Test(dependsOnMethods = {"update", "testSubmitRequest"}) + public void updateNonExistent() { + + // Perform setup. + setupUpdateNonExistent(); + + // Submit the request to the service and store the response. + // Note: The ID used in this 'create' call may be arbitrary. + // The only relevant ID may be the one used in update(), below. + Intake intake = createIntake(NON_EXISTENT_ID); + ClientResponse res = + client.update(NON_EXISTENT_ID, intake); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match the expected response(s)? + verbose("updateNonExistent: status = " + res.getStatus()); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); } - } - private void verboseMap(MultivaluedMap map) { - for(Object entry : map.entrySet()){ - MultivaluedMap.Entry mentry = (MultivaluedMap.Entry) entry; - verbose(" name=" + mentry.getKey() + " value=" + mentry.getValue()); + // --------------------------------------------------------------- + // CRUD tests : DELETE tests + // --------------------------------------------------------------- + + // Success outcomes + + @Override + @Test(dependsOnMethods = + {"create", "read", "update"}) + public void delete() { + + // Perform setup. + setupDelete(); + + // Submit the request to the service and store the response. + ClientResponse res = client.delete(knownObjectId); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match the expected response(s)? + verbose("delete: status = " + res.getStatus()); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); } - } - private String createIdentifier() { - long identifier = System.currentTimeMillis(); - return Long.toString(identifier); - } + // Failure outcomes + + @Override + @Test(dependsOnMethods = {"delete"}) + public void deleteNonExistent() { + + // Perform setup. + setupDeleteNonExistent(); + + // Submit the request to the service and store the response. + ClientResponse res = client.delete(NON_EXISTENT_ID); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match the expected response(s)? + verbose("deleteNonExistent: status = " + res.getStatus()); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + } + + + // --------------------------------------------------------------- + // Utility tests : tests of code used in tests above + // --------------------------------------------------------------- + + /** + * Tests the code for manually submitting data that is used by several + * of the methods above. + */ + @Test(dependsOnMethods = {"create", "read"}) + public void testSubmitRequest() { + + // Expected status code: 200 OK + final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode(); + + // Submit the request to the service and store the response. + String method = ServiceRequestType.READ.httpMethodName(); + String url = getResourceURL(knownObjectId); + int statusCode = submitRequest(method, url); + + // Check the status code of the response: does it match the expected response(s)? + verbose("testSubmitRequest: url=" + url + " status=" + statusCode); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + + } + + // --------------------------------------------------------------- + // Utility methods used by tests above + // --------------------------------------------------------------- + + @Override + public String getServicePathComponent() { + // @TODO Determine if it is possible to obtain this + // value programmatically. + // + // We set this in an annotation in the CollectionObjectProxy + // interface, for instance. We also set service-specific + // constants in each service module, which might also + // return this value. + return SERVICE_PATH_COMPONENT; + } + + private Intake createIntake(String identifier) { + Intake intake = + createIntake( + "entryNumber-" + identifier, + "entryDate-" + identifier); + return intake; + } + + private Intake createIntake(String entryNumber, String entryDate) { + Intake intake = new Intake(); + intake.setEntryNumber(entryNumber); + intake.setEntryDate(entryDate); + return intake; + } - private String createNonExistentIdentifier() { - return Long.toString(Long.MAX_VALUE); - } - } -- 2.47.3