diff --git a/amps/ags/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java b/amps/ags/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java index 117613e04e..d45a8748fb 100644 --- a/amps/ags/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java +++ b/amps/ags/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/core/v0/BaseAPI.java @@ -512,7 +512,12 @@ public abstract class BaseAPI try { HttpResponse httpResponse = doRequestJson(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams); - assertEquals("POST request to " + requestUrl + " was not successful.", expectedStatusCode, httpResponse.getStatusLine().getStatusCode()); + if (httpResponse.getStatusLine().getStatusCode() != expectedStatusCode) + { + // It's only possible to stream the response body once, so ensure we only do this if the test has failed. + JSONObject responseJson = responseBodyToJson(httpResponse); + assertEquals("POST request to " + requestUrl + " was not successful. Response: " + responseJson, expectedStatusCode, httpResponse.getStatusLine().getStatusCode()); + } return httpResponse; } catch (InstantiationException | IllegalAccessException error) @@ -521,6 +526,32 @@ public abstract class BaseAPI } } + /** + * Try to convert the response body to a JSON object. + * + * @param response The response. + * @return The JSON object or null if it was not possible to convert the response. + */ + private JSONObject responseBodyToJson(HttpResponse response) + { + try + { + try + { + return new JSONObject(EntityUtils.toString(response.getEntity())); + } + catch (JSONException error) + { + LOGGER.error("Converting message body to JSON failed. Body: {}", response.getEntity().getContent(), error); + } + } + catch (ParseException | IOException error) + { + LOGGER.error("Parsing message body failed.", error); + } + return null; + } + /** * Helper method for handling generic HTTP requests * @param requestType request type (a subclass of {@link HttpRequestBase}) @@ -558,18 +589,7 @@ public abstract class BaseAPI HttpResponse response = client.execute(adminUser, adminPassword, request); LOGGER.info("Response: {}", response.getStatusLine()); - try - { - responseBody = new JSONObject(EntityUtils.toString(response.getEntity())); - } - catch (JSONException error) - { - LOGGER.error("Converting message body to JSON failed. Body: {}", responseBody, error); - } - catch (ParseException | IOException error) - { - LOGGER.error("Parsing message body failed.", error); - } + responseBody = responseBodyToJson(response); switch (response.getStatusLine().getStatusCode()) {