From f3f360cf9ec49f288aee05db940a8f5d86ac27d7 Mon Sep 17 00:00:00 2001 From: Laramie Crocker Date: Fri, 3 Dec 2010 19:43:05 +0000 Subject: [PATCH] CSPACE-338 proper handling of POST,PUT,GET,DELETE errors when connecting. Moved java.net and java.io calls inside try block and report errors in ServiceResult struct, which then properly reports errors to maven surefire. --- .../IntegrationTests/xmlreplay/XmlReplay.java | 3 +- .../xmlreplay/XmlReplayTransport.java | 124 +++++++++--------- .../test-data/xmlreplay/dev-master.xml | 2 +- 3 files changed, 68 insertions(+), 61 deletions(-) diff --git a/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplay.java b/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplay.java index 4bd05f791..a0bdc5e98 100755 --- a/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplay.java +++ b/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplay.java @@ -33,6 +33,7 @@ public class XmlReplay { public static final String DEFAULT_CONTROL = "xml-replay-control.xml"; public static final String DEFAULT_MASTER_CONTROL = "xml-replay-master.xml"; + public static final String DEFAULT_DEV_MASTER_CONTROL = "dev-master.xml"; private String basedir = "."; //set from constructor. public String getBaseDir(){ @@ -89,7 +90,7 @@ public class XmlReplay { public String toString(){ - return "XmlReplay{"+this.basedir+", "+this.controlFileName+", "+this.defaultAuthsMap+", "+this.dump+'}'; + return "XmlReplay{"+this.basedir+", "+this.defaultAuthsMap+", "+this.dump+'}'; } // ============== METHODS =========================================================== diff --git a/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplayTransport.java b/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplayTransport.java index 78a411e41..c19555751 100755 --- a/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplayTransport.java +++ b/services/IntegrationTests/src/main/java/org/collectionspace/services/IntegrationTests/xmlreplay/XmlReplayTransport.java @@ -59,11 +59,11 @@ public class XmlReplayTransport { getMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID); ServiceResult pr = new ServiceResult(); - int statusCode1 = client.executeMethod(getMethod); - pr.responseCode = statusCode1; pr.fromTestID = fromTestID; pr.method = "GET"; try { + int statusCode1 = client.executeMethod(getMethod); + pr.responseCode = statusCode1; pr.result = getMethod.getResponseBodyAsString(); pr.responseMessage = getMethod.getStatusText(); Header[] headers = getMethod.getResponseHeaders(); @@ -74,13 +74,13 @@ public class XmlReplayTransport { pr.boundary = PayloadLogger.parseBoundary(hdrStr); } pr.contentLength = getMethod.getResponseContentLength(); + getMethod.releaseConnection(); } catch (Throwable t){ //System.err.println("ERROR getting content from response: "+t); pr.error = t.toString(); } - getMethod.releaseConnection(); return pr; } @@ -183,69 +183,75 @@ public class XmlReplayTransport { public static ServiceResult doPOST_PUT(String urlString, String content, String boundary, String method, String contentType, String authForTest, String fromTestID) throws Exception { - URL url = new URL(urlString); - HttpURLConnection conn; - conn = (HttpURLConnection) url.openConnection(); - - if (MULTIPART_MIXED.equalsIgnoreCase(contentType)){ - conn.setRequestProperty("Accept", "multipart/mixed"); - conn.setRequestProperty("content-type", "multipart/mixed; boundary=" + boundary); - } else { - conn.setRequestProperty("Accept", "application/xml"); - conn.setRequestProperty("content-type", contentType); - } - conn.setRequestProperty("Authorization", "Basic " + authForTest); //TODO: remove test user : hard-coded as "dGVzdDp0ZXN0" - conn.setRequestProperty("Connection", "close"); - conn.setRequestProperty("X-XmlReplay-fromTestID", fromTestID); - conn.setDoOutput(true); - conn.setDoInput(true); - conn.setRequestMethod(method); // "POST" or "PUT" - OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); - wr.write(content); - wr.flush(); - ServiceResult result = new ServiceResult(); + result.method = method; try { - result.requestPayload = content; - result.responseCode = conn.getResponseCode(); - //System.out.println("responseCode: "+result.responseCode); - if (400 <= result.responseCode && result.responseCode <= 499){ - return result; + URL url = new URL(urlString); + HttpURLConnection conn; + conn = (HttpURLConnection) url.openConnection(); + + if (MULTIPART_MIXED.equalsIgnoreCase(contentType)){ + conn.setRequestProperty("Accept", "multipart/mixed"); + conn.setRequestProperty("content-type", "multipart/mixed; boundary=" + boundary); + } else { + conn.setRequestProperty("Accept", "application/xml"); + conn.setRequestProperty("content-type", contentType); } - BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String line; - StringBuffer sb = new StringBuffer(); - while ((line = rd.readLine()) != null) { - sb.append(line).append("\r\n"); + conn.setRequestProperty("Authorization", "Basic " + authForTest); //TODO: remove test user : hard-coded as "dGVzdDp0ZXN0" + conn.setRequestProperty("Connection", "close"); + conn.setRequestProperty("X-XmlReplay-fromTestID", fromTestID); + conn.setDoOutput(true); + conn.setDoInput(true); + conn.setRequestMethod(method); // "POST" or "PUT" + OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); + wr.write(content); + wr.flush(); + + + try { + result.requestPayload = content; + result.responseCode = conn.getResponseCode(); + //System.out.println("responseCode: "+result.responseCode); + if (400 <= result.responseCode && result.responseCode <= 499){ + return result; + } + BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); + String line; + StringBuffer sb = new StringBuffer(); + while ((line = rd.readLine()) != null) { + sb.append(line).append("\r\n"); + } + String msg = sb.toString(); + result.result = msg; + result.boundary = PayloadLogger.parseBoundary(conn.getHeaderField("CONTENT-TYPE")); + + rd.close(); + } catch (Throwable t){ + //System.err.println("ERROR getting content from response: "+t); + result.error = t.toString(); } - String msg = sb.toString(); - result.result = msg; - result.boundary = PayloadLogger.parseBoundary(conn.getHeaderField("CONTENT-TYPE")); + wr.close(); - rd.close(); - } catch (Throwable t){ - //System.err.println("ERROR getting content from response: "+t); - result.error = t.toString(); - } - wr.close(); - - - String deleteURL = ""; - String location = ""; - Map> headers = conn.getHeaderFields(); - List locations = headers.get("Location"); - if (locations != null){ - String locationZero = locations.get(0); - if (locationZero != null){ - String[] segments = locationZero.split("/"); - location = segments[segments.length - 1]; - deleteURL = Tools.glue(urlString, "/", location); + + String deleteURL = ""; + String location = ""; + Map> headers = conn.getHeaderFields(); + List locations = headers.get("Location"); + if (locations != null){ + String locationZero = locations.get(0); + if (locationZero != null){ + String[] segments = locationZero.split("/"); + location = segments[segments.length - 1]; + deleteURL = Tools.glue(urlString, "/", location); + } } + result.location = location; + result.deleteURL = deleteURL; + result.CSID = location; + + } catch (Throwable t2){ + result.error = "ERROR in XmlReplayTransport: "+t2; } - result.location = location; - result.deleteURL = deleteURL; - result.CSID = location; - result.method = method; return result; } diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/dev-master.xml b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/dev-master.xml index 82c0a15c1..062569316 100644 --- a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/dev-master.xml +++ b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/dev-master.xml @@ -1,7 +1,7 @@ - http://localhost:8280 + http://localhost:8180 -- 2.47.3