Merge pull request #1807 from Alfresco/feature/APPS-1593_ImproveAGSRESTTestReporting

APPS-1593 Try to make REST test failure messages more useful. [ags]
This commit is contained in:
Tom Page
2023-06-16 15:31:12 +01:00
committed by GitHub

View File

@@ -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())
{