RM-5837 Always assert status code when making a v0 POST request.

Refactor the DestroyRecordFolderActions UI test as this previously relied
on the create retention schedule API call failing silently for the second
test.
This commit is contained in:
Tom Page
2017-11-08 12:44:22 +00:00
parent 350ea660ae
commit 592275b1aa
7 changed files with 51 additions and 52 deletions

View File

@@ -342,59 +342,60 @@ public abstract class BaseAPI
/** /**
* Helper method for POST requests * Helper method for POST requests
* *
* @param adminUser user with administrative privileges * @param adminUser user with administrative privileges
* @param adminPassword password for adminUser * @param adminPassword password for adminUser
* @param expectFailure If false then an exception will be thrown if the POST is not successful. * @param expectedStatusCode The expected return status code.
* @param requestParams zero or more endpoint specific request parameters * @param requestParams zero or more endpoint specific request parameters
* @param urlTemplate request URL template * @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i> * @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/ */
protected HttpResponse doPostJsonRequest(String adminUser, protected HttpResponse doPostJsonRequest(String adminUser,
String adminPassword, String adminPassword,
boolean expectFailure, int expectedStatusCode,
JSONObject requestParams, JSONObject requestParams,
String urlTemplate, String urlTemplate,
String... urlTemplateParams) String... urlTemplateParams)
{ {
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
return doPostJsonRequest(adminUser, adminPassword, expectFailure, client.getApiUrl(), requestParams, urlTemplate, urlTemplateParams); return doPostJsonRequest(adminUser, adminPassword, expectedStatusCode, client.getApiUrl(), requestParams, urlTemplate, urlTemplateParams);
} }
/** /**
* Helper method for POST requests to slingshot. * Helper method for POST requests to slingshot.
* *
* @param adminUser user with administrative privileges * @param adminUser user with administrative privileges
* @param adminPassword password for adminUser * @param adminPassword password for adminUser
* @param expectFailure If false then an exception will be thrown if the POST is not successful. * @param expectedStatusCode The expected return status code.
* @param requestParams zero or more endpoint specific request parameters * @param requestParams zero or more endpoint specific request parameters
* @param urlTemplate request URL template * @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i> * @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
*/ */
protected HttpResponse doSlingshotPostJsonRequest(String adminUser, protected HttpResponse doSlingshotPostJsonRequest(String adminUser,
String adminPassword, String adminPassword,
boolean expectFailure, int expectedStatusCode,
JSONObject requestParams, JSONObject requestParams,
String urlTemplate, String urlTemplate,
String... urlTemplateParams) String... urlTemplateParams)
{ {
AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject(); AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
return doPostJsonRequest(adminUser, adminPassword, expectFailure, client.getAlfrescoUrl() + SLINGSHOT_PREFIX, requestParams, urlTemplate, urlTemplateParams); return doPostJsonRequest(adminUser, adminPassword, expectedStatusCode, client.getAlfrescoUrl() + SLINGSHOT_PREFIX, requestParams, urlTemplate, urlTemplateParams);
} }
/** /**
* Helper method for POST requests * Helper method for POST requests
* *
* @param adminUser user with administrative privileges * @param adminUser user with administrative privileges
* @param adminPassword password for adminUser * @param adminPassword password for adminUser
* @param expectFailure If false then an exception will be thrown if the POST is not successful. * @param expectedStatusCode The expected return status code.
* @param urlStart the start of the URL (for example "alfresco/s/slingshot"). * @param urlStart the start of the URL (for example "alfresco/s/slingshot").
* @param requestParams zero or more endpoint specific request parameters * @param requestParams zero or more endpoint specific request parameters
* @param urlTemplate request URL template * @param urlTemplate request URL template
* @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i> * @param urlTemplateParams zero or more parameters used with <i>urlTemplate</i>
* @throws AssertionError if the returned status code is not as expected.
*/ */
private HttpResponse doPostJsonRequest(String adminUser, private HttpResponse doPostJsonRequest(String adminUser,
String adminPassword, String adminPassword,
boolean expectFailure, int expectedStatusCode,
String urlStart, String urlStart,
JSONObject requestParams, JSONObject requestParams,
String urlTemplate, String urlTemplate,
@@ -408,10 +409,7 @@ public abstract class BaseAPI
try try
{ {
HttpResponse httpResponse = doRequestJson(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams); HttpResponse httpResponse = doRequestJson(HttpPost.class, requestUrl, adminUser, adminPassword, requestParams);
if (!expectFailure) assertEquals("POST request to " + requestUrl + " was not successful.", httpResponse.getStatusLine().getStatusCode(), expectedStatusCode);
{
assertEquals("POST request was not successful.", httpResponse.getStatusLine().getStatusCode(), 200);
}
return httpResponse; return httpResponse;
} }
catch (InstantiationException | IllegalAccessException error) catch (InstantiationException | IllegalAccessException error)

View File

@@ -65,7 +65,7 @@ public class CopyToAPI extends BaseAPI
*/ */
public HttpResponse copyTo(String user, String password, String targetContainerPath, List<String> nodeRefs) public HttpResponse copyTo(String user, String password, String targetContainerPath, List<String> nodeRefs)
{ {
return copyToAndGetResponse(user, password, false, targetContainerPath, nodeRefs); return copyTo(user, password, 200, targetContainerPath, nodeRefs);
} }
/** /**
@@ -73,19 +73,20 @@ public class CopyToAPI extends BaseAPI
* *
* @param user The username of the user to use. * @param user The username of the user to use.
* @param password The password of the user. * @param password The password of the user.
* @param expectFailure If false then an exception will be thrown if the POST is not successful. * @param expectedStatusCode The expected return status code.
* @param targetContainerPath The destination to copy the nodes to. This should be in the format * @param targetContainerPath The destination to copy the nodes to. This should be in the format
* "{site}/{container}/{path}", "{site}/{container}", "{store_type}/{store_id}/{id}/{path}", * "{site}/{container}/{path}", "{site}/{container}", "{store_type}/{store_id}/{id}/{path}",
* "{store_type}/{store_id}/{id}" or "{store_type}/{store_id}". * "{store_type}/{store_id}/{id}" or "{store_type}/{store_id}".
* @param nodeRefs The list of nodes to copy. * @param nodeRefs The list of nodes to copy.
* @return The HTTP Response. * @return The HTTP Response.
* @throws AssertionError If the API didn't return the expected status code.
*/ */
public HttpResponse copyToAndGetResponse(String user, String password, boolean expectFailure, String targetContainerPath, List<String> nodeRefs) public HttpResponse copyTo(String user, String password, int expectedStatusCode, String targetContainerPath, List<String> nodeRefs)
{ {
JSONObject requestParams = new JSONObject(); JSONObject requestParams = new JSONObject();
requestParams.put("nodeRefs", new JSONArray(nodeRefs)); requestParams.put("nodeRefs", new JSONArray(nodeRefs));
return doSlingshotPostJsonRequest(user, password, expectFailure, requestParams, return doSlingshotPostJsonRequest(user, password, expectedStatusCode, requestParams,
MessageFormat.format(COPY_TO_API, "{0}", targetContainerPath)); MessageFormat.format(COPY_TO_API, "{0}", targetContainerPath));
} }
} }

View File

@@ -238,7 +238,7 @@ public class RMRolesAndActionsAPI extends BaseAPI
) )
); );
} }
return doPostJsonRequest(user, password, false, requestParams, RM_ACTIONS_API); return doPostJsonRequest(user, password, 200, requestParams, RM_ACTIONS_API);
} }
/** /**
@@ -303,7 +303,7 @@ public class RMRolesAndActionsAPI extends BaseAPI
requestParams.put("prop_rma_holdReason", reason); requestParams.put("prop_rma_holdReason", reason);
// Make the POST request and throw an assertion error if it fails. // Make the POST request and throw an assertion error if it fails.
HttpResponse httpResponse = doPostJsonRequest(user, password, false, requestParams, CREATE_HOLDS_API); HttpResponse httpResponse = doPostJsonRequest(user, password, 200, requestParams, CREATE_HOLDS_API);
assertNotNull("Expected object to have been created at " + fullHoldPath, assertNotNull("Expected object to have been created at " + fullHoldPath,
getObjectByPath(user, password, fullHoldPath)); getObjectByPath(user, password, fullHoldPath));
return httpResponse; return httpResponse;
@@ -325,6 +325,6 @@ public class RMRolesAndActionsAPI extends BaseAPI
addPropertyToRequest(requestParams, "prop_cm_description", properties, RMProperty.DESCRIPTION); addPropertyToRequest(requestParams, "prop_cm_description", properties, RMProperty.DESCRIPTION);
addPropertyToRequest(requestParams, "prop_cm_author", properties, RMProperty.AUTHOR); addPropertyToRequest(requestParams, "prop_cm_author", properties, RMProperty.AUTHOR);
return doPostJsonRequest(username, password, false, requestParams, MessageFormat.format(UPDATE_METADATA_API, "{0}", itemNodeRef)); return doPostJsonRequest(username, password, 200, requestParams, MessageFormat.format(UPDATE_METADATA_API, "{0}", itemNodeRef));
} }
} }

View File

@@ -66,7 +66,7 @@ public class RecordCategoriesAPI extends BaseAPI
requestParams.put("name", "createDispositionSchedule"); requestParams.put("name", "createDispositionSchedule");
requestParams.put("nodeRef", catNodeRef); requestParams.put("nodeRef", catNodeRef);
return doPostJsonRequest(user, password, false, requestParams, RM_ACTIONS_API); return doPostJsonRequest(user, password, 200, requestParams, RM_ACTIONS_API);
} }
/** /**
@@ -85,7 +85,7 @@ public class RecordCategoriesAPI extends BaseAPI
requestParams.put("prop_rma_dispositionAuthority", getPropertyValue(retentionProperties, RETENTION_SCHEDULE.RETENTION_AUTHORITY)); requestParams.put("prop_rma_dispositionAuthority", getPropertyValue(retentionProperties, RETENTION_SCHEDULE.RETENTION_AUTHORITY));
requestParams.put("prop_rma_dispositionInstructions", getPropertyValue(retentionProperties, RETENTION_SCHEDULE.RETENTION_INSTRUCTIONS)); requestParams.put("prop_rma_dispositionInstructions", getPropertyValue(retentionProperties, RETENTION_SCHEDULE.RETENTION_INSTRUCTIONS));
requestParams.put("prop_rma_recordLevelDisposition", appliedToRecords.toString()); requestParams.put("prop_rma_recordLevelDisposition", appliedToRecords.toString());
return doPostJsonRequest(user, password, false, requestParams, MessageFormat.format(UPDATE_METADATA_API, "{0}", dispRetentionNodeRef)); return doPostJsonRequest(user, password, 200, requestParams, MessageFormat.format(UPDATE_METADATA_API, "{0}", dispRetentionNodeRef));
} }
/** /**
@@ -109,7 +109,7 @@ public class RecordCategoriesAPI extends BaseAPI
addPropertyToRequest(requestParams, "events", properties, RETENTION_SCHEDULE.RETENTION_EVENTS); addPropertyToRequest(requestParams, "events", properties, RETENTION_SCHEDULE.RETENTION_EVENTS);
addPropertyToRequest(requestParams, "eligibleOnFirstCompleteEvent", properties, RETENTION_SCHEDULE.RETENTION_ELIGIBLE_FIRST_EVENT); addPropertyToRequest(requestParams, "eligibleOnFirstCompleteEvent", properties, RETENTION_SCHEDULE.RETENTION_ELIGIBLE_FIRST_EVENT);
return doPostJsonRequest(user, password, false, requestParams, MessageFormat.format(DISPOSITION_ACTIONS_API, "{0}", catNodeRef)); return doPostJsonRequest(user, password, 200, requestParams, MessageFormat.format(DISPOSITION_ACTIONS_API, "{0}", catNodeRef));
} }
/** /**

View File

@@ -68,7 +68,7 @@ public class RecordFoldersAPI extends BaseAPI
requestParams.put("name", "closeRecordFolder"); requestParams.put("name", "closeRecordFolder");
requestParams.put("nodeRef", recNodeRef); requestParams.put("nodeRef", recNodeRef);
return doPostJsonRequest(user, password, false, requestParams, RM_ACTIONS_API); return doPostJsonRequest(user, password, 200, requestParams, RM_ACTIONS_API);
} }
catch (JSONException error) catch (JSONException error)
{ {

View File

@@ -77,7 +77,7 @@ public class RecordsAPI extends BaseAPI
requestParams.put("actionedUponNode", docNodeRef); requestParams.put("actionedUponNode", docNodeRef);
requestParams.put("actionDefinitionName", "create-record"); requestParams.put("actionDefinitionName", "create-record");
return doPostJsonRequest(user, password, false, requestParams, ACTIONS_API); return doPostJsonRequest(user, password, 200, requestParams, ACTIONS_API);
} }
/** /**
@@ -96,7 +96,7 @@ public class RecordsAPI extends BaseAPI
requestParams.put("name", "declareRecord"); requestParams.put("name", "declareRecord");
requestParams.put("nodeRef", recNodeRef); requestParams.put("nodeRef", recNodeRef);
return doPostJsonRequest(user, password, false, requestParams, RM_ACTIONS_API); return doPostJsonRequest(user, password, 200, requestParams, RM_ACTIONS_API);
} }
/** /**
@@ -111,21 +111,21 @@ public class RecordsAPI extends BaseAPI
*/ */
public HttpResponse rejectRecord(String user, String password, String recordName, String reason) public HttpResponse rejectRecord(String user, String password, String recordName, String reason)
{ {
return rejectRecordAndGetResponse(user, password, false, recordName, reason); return rejectRecord(user, password, 200, recordName, reason);
} }
/** /**
* Reject the record given as parameter * Reject the record given as parameter
* *
* @param user the user declaring the document as record * @param user the user declaring the document as record
* @param password the user's password * @param password the user's password
* @param expectFailure If false then throws an exception if the POST call fails. * @param expectedStatusCode The expected return status code.
* @param recordName the record name * @param recordName the record name
* @param reason reject reason * @param reason reject reason
* @return The HTTP Response. * @return The HTTP Response.
* @throws AssertionError If expectFailure is false and the POST call is not successful. * @throws AssertionError If the expectedStatusCode was not returned.
*/ */
public HttpResponse rejectRecordAndGetResponse(String user, String password, boolean expectFailure, String recordName, String reason) public HttpResponse rejectRecord(String user, String password, int expectedStatusCode, String recordName, String reason)
{ {
String recNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, RM_SITE_ID, recordName); String recNodeRef = getNodeRefSpacesStore() + contentService.getNodeRef(user, password, RM_SITE_ID, recordName);
@@ -135,7 +135,7 @@ public class RecordsAPI extends BaseAPI
requestParams.put("params",new JSONObject() requestParams.put("params",new JSONObject()
.put("reason",reason)); .put("reason",reason));
return doPostJsonRequest(user, password, expectFailure, requestParams, RM_ACTIONS_API); return doPostJsonRequest(user, password, expectedStatusCode, requestParams, RM_ACTIONS_API);
} }
/** /**
@@ -155,7 +155,7 @@ public class RecordsAPI extends BaseAPI
requestParams.put("actionedUponNode", docNodeRef); requestParams.put("actionedUponNode", docNodeRef);
requestParams.put("actionDefinitionName", "declare-as-version-record"); requestParams.put("actionDefinitionName", "declare-as-version-record");
return doPostJsonRequest(user, password, false, requestParams, ACTIONS_API); return doPostJsonRequest(user, password, 200, requestParams, ACTIONS_API);
} }
/** /**
@@ -217,7 +217,7 @@ public class RecordsAPI extends BaseAPI
requestParams.put("prop_rma_box", box); requestParams.put("prop_rma_box", box);
requestParams.put("prop_rma_file", file); requestParams.put("prop_rma_file", file);
return doPostJsonRequest(username, password, false, requestParams, CREATE_NON_ELECTRONIC_RECORD_API); return doPostJsonRequest(username, password, 200, requestParams, CREATE_NON_ELECTRONIC_RECORD_API);
} }
/** /**
@@ -343,7 +343,7 @@ public class RecordsAPI extends BaseAPI
requestParams.put("actionedUponNode", docNodeRef); requestParams.put("actionedUponNode", docNodeRef);
requestParams.put("actionDefinitionName", "hide-record"); requestParams.put("actionDefinitionName", "hide-record");
return doPostJsonRequest(user, password, false, requestParams, ACTIONS_API); return doPostJsonRequest(user, password, 200, requestParams, ACTIONS_API);
} }
} }

View File

@@ -71,7 +71,7 @@ public class RulesAPI extends BaseAPI
{ {
try try
{ {
return doPostJsonRequest(username, password, false, getRuleRequest(ruleProperties), MessageFormat.format(RULES_API, "{0}", containerNodeRef)); return doPostJsonRequest(username, password, 200, getRuleRequest(ruleProperties), MessageFormat.format(RULES_API, "{0}", containerNodeRef));
} }
catch (JSONException error) catch (JSONException error)
{ {
@@ -316,7 +316,7 @@ public class RulesAPI extends BaseAPI
{ {
if(containerInheritsRulesFromParent(username, password, containerNodeRef)) if(containerInheritsRulesFromParent(username, password, containerNodeRef))
{ {
return doPostJsonRequest(username, password, false, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef)); return doPostJsonRequest(username, password, 200, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef));
} }
return null; return null;
} }
@@ -333,7 +333,7 @@ public class RulesAPI extends BaseAPI
{ {
if (!containerInheritsRulesFromParent(username, password, containerNodeRef)) if (!containerInheritsRulesFromParent(username, password, containerNodeRef))
{ {
return doPostJsonRequest(username, password, false, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef)); return doPostJsonRequest(username, password, 200, new JSONObject(), MessageFormat.format(INHERIT_RULES_API, "{0}", containerNodeRef));
} }
return null; return null;
} }