From e1a1101ac60b047e8f08e1ca36d0661742813ff2 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Mon, 31 Aug 2009 22:36:26 +0000 Subject: [PATCH] CSPACE-360,CSPACE-386,CSPACE-387: Replaced HttpClient with RESTEasy ClientRequest in client test framework, CollectionObject tests. --- .../client/test/AbstractServiceTest.java | 122 ++++++++---------- .../client/test/ServiceRequestType.java | 27 +++- .../test/CollectionObjectServiceTest.java | 75 +++++------ 3 files changed, 120 insertions(+), 104 deletions(-) diff --git a/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTest.java b/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTest.java index ed763da47..df8f11acc 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTest.java +++ b/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTest.java @@ -26,27 +26,16 @@ package org.collectionspace.services.client.test; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.Marshaller; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; - -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpException; -import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.methods.EntityEnclosingMethod; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.PutMethod; -import org.apache.commons.httpclient.methods.RequestEntity; -import org.apache.commons.httpclient.methods.StringRequestEntity; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; import org.collectionspace.services.client.TestServiceClient; import org.collectionspace.services.client.test.ServiceRequestType; +import org.jboss.resteasy.client.ClientRequest; import org.jboss.resteasy.client.ClientResponse; import org.slf4j.Logger; @@ -62,12 +51,6 @@ public abstract class AbstractServiceTest implements ServiceTest { final Logger logger = LoggerFactory.getLogger(AbstractServiceTest.class); - // An HTTP client, used for performing several negative (failure) tests. - // - // @TODO To be replaced with RESTeasy's ClientRequest (a higher-level API - // that is based on HttpClient), per Issue CSPACE-386. - protected HttpClient httpClient = new HttpClient(); - // A base-level client, used (only) to obtain the base service URL. private static final TestServiceClient serviceClient = new TestServiceClient(); @@ -83,7 +66,23 @@ public abstract class AbstractServiceTest implements ServiceTest { // // This makes it possible to check behavior specific to that type of request, // such as the set of valid status codes that may be returned. - ServiceRequestType REQUEST_TYPE = ServiceRequestType.NON_EXISTENT; + // + // Note that the default value is set to a guard value. + protected ServiceRequestType REQUEST_TYPE = ServiceRequestType.NON_EXISTENT; + + // Static data to be submitted in various tests + protected final static String XML_DECLARATION = + ""; + + // Note: this constant is intentionally missing its last angle bracket. + protected final static String MALFORMED_XML_DATA = + XML_DECLARATION + + "wrong schema contentswrong schema contents"; + // --------------------------------------------------------------- // CRUD tests : CREATE tests @@ -310,59 +309,48 @@ public abstract class AbstractServiceTest implements ServiceTest { return getServiceRootURL() + "/" + resourceIdentifier; } - protected int submitRequest(HttpMethod method) { - int statusCode = 0; + protected int submitRequest(String method, String url) { + int statusCode = 0; try { - statusCode = httpClient.executeMethod(method); - } catch(HttpException e) { - logger.error("Fatal protocol violation: ", e); - } catch(IOException e) { - logger.error("Fatal transport error: ", e); - } catch(Exception e) { - logger.error("Unknown exception: ", e); - } finally { - // Release the connection. - method.releaseConnection(); + ClientRequest request = new ClientRequest(url); + if (method.equalsIgnoreCase("DELETE")) { + ClientResponse res = request.delete(); + statusCode = res.getStatus(); + } else if (method.equalsIgnoreCase("GET")) { + ClientResponse res = request.get(); + statusCode = res.getStatus(); + } else { + // Do nothing - leave status code at default value. + } + } catch (Exception e) { + logger.error( + "Exception during HTTP " + method + " request to " + url + " :", + e); } return statusCode; - } - - protected int submitRequest(EntityEnclosingMethod method, RequestEntity entity) { + } + + protected int submitRequest(String method, String url, String entityStr) { int statusCode = 0; try { - method.setRequestEntity(entity); - statusCode = httpClient.executeMethod(method); - } catch(HttpException e) { - logger.error("Fatal protocol violation: ", e); - } catch(IOException e) { - logger.error("Fatal transport error: ", e); - } catch(Exception e) { - logger.error("Unknown exception: ", e); - } finally { - // Release the connection. - method.releaseConnection(); + ClientRequest request = new ClientRequest(url); + request.body(MediaType.APPLICATION_XML, entityStr); + if (method.equalsIgnoreCase("POST")) { + ClientResponse res = request.post(); + statusCode = res.getStatus(); + } else if (method.equalsIgnoreCase("PUT")) { + ClientResponse res = request.put(); + statusCode = res.getStatus(); + } else { + // Do nothing - leave status code at default value. + } + } catch (Exception e) { + logger.error( + "Exception during HTTP " + method + " request to " + url + " :", + e); } return statusCode; - } - - protected StringRequestEntity getXmlEntity(String contents) { - if (contents == null) { - contents = ""; - } - StringRequestEntity entity = null; - final String XML_DECLARATION = - ""; - final String XML_CONTENT_TYPE=MediaType.APPLICATION_XML; - final String UTF8_CHARSET_NAME = "UTF-8"; - try { - entity = - new StringRequestEntity( - XML_DECLARATION + contents, XML_CONTENT_TYPE, UTF8_CHARSET_NAME); - } catch (UnsupportedEncodingException e) { - logger.error("Unsupported character encoding error: ", e); - } - return entity; - } + } protected String extractId(ClientResponse res) { MultivaluedMap mvm = res.getMetadata(); diff --git a/services/client/src/main/java/org/collectionspace/services/client/test/ServiceRequestType.java b/services/client/src/main/java/org/collectionspace/services/client/test/ServiceRequestType.java index 65ec7734a..802e0ccdd 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/test/ServiceRequestType.java +++ b/services/client/src/main/java/org/collectionspace/services/client/test/ServiceRequestType.java @@ -40,7 +40,7 @@ import org.slf4j.LoggerFactory; * $LastChangedDate$ */ public enum ServiceRequestType { - + // Define each of the service request types and their valid HTTP status codes. CREATE { @@ -68,6 +68,10 @@ public enum ServiceRequestType { public String validStatusCodesAsString() { return Arrays.toString(CREATE.validStatusCodes()); } + @Override + public String httpMethodName() { + return javax.ws.rs.HttpMethod.POST; + } }, // Note that commas are required at the end of each enum block, except the last. @@ -95,6 +99,10 @@ public enum ServiceRequestType { public String validStatusCodesAsString() { return Arrays.toString(READ.validStatusCodes()); } + @Override + public String httpMethodName() { + return javax.ws.rs.HttpMethod.GET; + } }, @@ -122,6 +130,10 @@ public enum ServiceRequestType { public String validStatusCodesAsString() { return Arrays.toString(READ_MULTIPLE.validStatusCodes()); } + @Override + public String httpMethodName() { + return javax.ws.rs.HttpMethod.GET; + } }, @@ -150,6 +162,10 @@ public enum ServiceRequestType { public String validStatusCodesAsString() { return Arrays.toString(UPDATE.validStatusCodes()); } + @Override + public String httpMethodName() { + return javax.ws.rs.HttpMethod.PUT; + } }, @@ -177,6 +193,10 @@ public enum ServiceRequestType { public String validStatusCodesAsString() { return Arrays.toString(DELETE.validStatusCodes()); } + @Override + public String httpMethodName() { + return javax.ws.rs.HttpMethod.DELETE; + } }, @@ -196,6 +216,10 @@ public enum ServiceRequestType { public String validStatusCodesAsString() { return Arrays.toString(NON_EXISTENT.validStatusCodes()); } + @Override + public String httpMethodName() { + return ""; + } }; // Template methods to be implemented by each ServiceRequestType. @@ -206,4 +230,5 @@ public enum ServiceRequestType { public abstract String validStatusCodesAsString(); + public abstract String httpMethodName(); } 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 a96b7b55b..8ee73fa2c 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 @@ -27,11 +27,6 @@ import java.util.List; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.PutMethod; -import org.apache.commons.httpclient.methods.StringRequestEntity; - import org.collectionspace.services.client.CollectionObjectClient; import org.collectionspace.services.client.test.ServiceRequestType; import org.collectionspace.services.collectionobject.CollectionObject; @@ -67,7 +62,8 @@ public class CollectionObjectServiceTest extends AbstractServiceTest { public void create() { // Perform setup, such as initializing the type of service request - // and its valid and expected status codes. + // (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. @@ -105,6 +101,11 @@ public class CollectionObjectServiceTest extends AbstractServiceTest { ClientResponse res = client.createCollectionObject(null); } + // Placeholders until the two tests below can be uncommented. See Issue CSPACE-401. + public void createWithMalformedXml() {} + public void createWithWrongXmlSchema() {} + +/* @Override @Test(dependsOnMethods = {"create", "testSubmitRequest"}) public void createWithMalformedXml() { @@ -113,12 +114,10 @@ public class CollectionObjectServiceTest extends AbstractServiceTest { setupCreateWithMalformedXml(); // Submit the request to the service and store the response. + String method = REQUEST_TYPE.httpMethodName(); String url = getServiceRootURL(); - PostMethod method = new PostMethod(url); - final String MALFORMED_XML_DATA = - "wrong schema contentswrong schema contents