tests updates java docs and clean up data after test run

This commit is contained in:
Rodica Sutu
2019-08-20 11:33:42 +03:00
parent 0999684a26
commit 6238f6d99e
4 changed files with 81 additions and 61 deletions

View File

@@ -26,6 +26,7 @@
*/
package org.alfresco.rest.v0;
import static org.alfresco.rest.core.v0.APIUtils.convertHTTPResponseToJSON;
import static org.apache.http.HttpStatus.SC_OK;
import static org.testng.AssertJUnit.assertNotNull;
@@ -46,7 +47,6 @@ import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@@ -62,10 +62,16 @@ import org.springframework.stereotype.Component;
public class HoldsAPI extends BaseAPI
{
public static final String HOLDS_CONTAINER = "Holds";
/**
* The URI to create a hold
*/
private static final String CREATE_HOLDS_API = "{0}type/rma:hold/formprocessor";
/** The URI to add items to hold.*/
private static final String RM_HOLDS_API = "{0}rma/holds";
/**
* The URI to get holds.
*/
private static final String GET_RM_HOLDS = RM_HOLDS_API + "?{1}";
/**
@@ -122,15 +128,14 @@ public class HoldsAPI extends BaseAPI
try
{
return new JSONObject(EntityUtils.toString(httpResponse.getEntity()))
.getString("persistedObject")
return convertHTTPResponseToJSON(httpResponse).getString("persistedObject")
.replaceAll(NODE_REF_WORKSPACE_SPACES_STORE, "");
}
catch(JSONException error)
{
LOGGER.error("Converting message body to JSON failed. Body: {}", httpResponse, error);
}
catch(ParseException | IOException error)
catch(ParseException error)
{
LOGGER.error("Parsing message body failed.", error);
}
@@ -176,15 +181,15 @@ public class HoldsAPI extends BaseAPI
* @param holdName the hold name
* @return The HTTP response
*/
public HttpResponse addItemToHold(String user, String password, int expectedStatus, String itemNodeRef, String
holdName)
public HttpResponse addItemToHold(String user, String password, int expectedStatus, String itemNodeRef,
String holdName)
{
final JSONObject requestParams = createHoldJsonObject(user, password, itemNodeRef, holdName);
final JSONObject requestParams = addToHoldJsonObject(user, password, itemNodeRef, holdName);
return doPostJsonRequest(user, password, expectedStatus, requestParams, RM_HOLDS_API);
}
/**
* Util method to add item (active content /record/ record folder) to the hold and gets the error message
* Util method to add item (active content /record/ record folder) to the hold and get the error message
*
* @param user the user who adds the item to the hold
* @param password the user's password
@@ -207,7 +212,7 @@ public class HoldsAPI extends BaseAPI
* @param holdName
* @return JSONObject fo
*/
private JSONObject createHoldJsonObject(String user, String password, String itemNodeRef, String holdName)
private JSONObject addToHoldJsonObject(String user, String password, String itemNodeRef, String holdName)
{
final JSONArray nodeRefs = new JSONArray().put(getNodeRefSpacesStore() + itemNodeRef);
@@ -239,10 +244,11 @@ public class HoldsAPI extends BaseAPI
}
/**
* Adds item (record/ record folder) to the hold
* Remove item (record/ record folder) to the hold
*
* @param user the user who adds the item to the hold
* @param password the user's password
* @param expectedStatus https status code expected
* @param itemNodeRef the nodeRef of the item to be added to hold
* @param holdName the hold name
* @return The HTTP response
@@ -250,12 +256,12 @@ public class HoldsAPI extends BaseAPI
public HttpResponse removeItemFromHold(String user, String password, int expectedStatus, String itemNodeRef, String
holdName)
{
final JSONObject requestParams = createHoldJsonObject(user, password, itemNodeRef, holdName);
final JSONObject requestParams = addToHoldJsonObject(user, password, itemNodeRef, holdName);
return doPutJsonRequest(user, password, expectedStatus, requestParams, RM_HOLDS_API);
}
/**
* Util method to add item (active content /record/ record folder) to the hold and gets the error message
* Util method to remove item (active content /record/ record folder) from hold and get the error message
*
* @param user the user who adds the item to the hold
* @param password the user's password
@@ -278,9 +284,11 @@ public class HoldsAPI extends BaseAPI
private String extractErrorMessageFromHttpResponse(HttpResponse httpResponse)
{
final HttpEntity entity = httpResponse.getEntity();
try(InputStream responseStream = entity.getContent(); JsonReader reader = Json.createReader(responseStream))
JsonReader reader = null;
try
{
final InputStream responseStream = entity.getContent();
reader = Json.createReader(responseStream);
return reader.readObject().getString("message");
}
catch (JSONException error)
@@ -291,20 +299,26 @@ public class HoldsAPI extends BaseAPI
{
LOGGER.error("Parsing message body failed.", error);
}
finally
{
if (reader != null)
{
reader.close();
}
}
return null;
}
/**
* Get the list of the available holds which have the item node reference if includedInHold parameter is true
* Get the list of the available holds which have the item node reference if includedInHold parameter is true,
* otherwise a list of hold node references will be retrieved which do not include the given node reference.
*
* @param user The username of the user to use.
* @param password The password of the user.
* @param itemNodeRef
* @param includedInHold
* @param fileOnly
* @return return
* @param itemNodeRef The item node reference
* @param includedInHold True to retrieve the holds which have the item node reference
* @param fileOnly True if only files should be return
* @return return a list of hold entries
*/
public List<HoldEntry> getHolds(String user, String password, final String itemNodeRef,
final Boolean includedInHold, final Boolean fileOnly)
@@ -318,6 +332,4 @@ public class HoldsAPI extends BaseAPI
return PojoUtility.jsonToObject(holdEntries, HoldEntry.class);
}
}

View File

@@ -152,7 +152,7 @@ public class RoleService
}
/**
* Helper method to create a test user with rm role and permissions on the node ref
* Helper method to create a user with rm role and permissions on the node ref
*
* @param userRole the rm role
* @param userPermission the permissions over the record category
@@ -168,7 +168,7 @@ public class RoleService
return rmUser;
}
/**
* Helper method to create a test user with rm role and permissions over the recordCategory and collaborator role
* Helper method to create a user with rm role and permissions over the recordCategory and collaborator role
* in collaboration site
*
* @param siteModel collaboration site
@@ -180,8 +180,8 @@ public class RoleService
public UserModel createCollaboratorWithRMRoleAndPermission(SiteModel siteModel, RecordCategory recordCategory,
UserRoles userRole, UserPermissions userPermission)
{
return createUserWithSiteRoleRMRoleAndPermission(siteModel, UserRole.SiteCollaborator, recordCategory.getId(), userRole,
userPermission);
return createUserWithSiteRoleRMRoleAndPermission(siteModel, UserRole.SiteCollaborator, recordCategory.getId(),
userRole, userPermission);
}
@@ -190,9 +190,10 @@ public class RoleService
* in collaboration site
*
* @param siteModel collaboration site
* @param recordCategory the category on which permission should be given
* @param userSiteRoles user role in the collaboration site
* @param rmNodeId rm node id to grant rm permission
* @param userRole the rm role
* @param userPermission the permissions over the recordCategory
* @param userPermission the permissions over the rmNodeId
* @return the created user model
*/
public UserModel createUserWithSiteRoleRMRoleAndPermission(SiteModel siteModel, UserRole userSiteRoles,

View File

@@ -60,6 +60,8 @@ import org.alfresco.rest.model.RestNodeModel;
import org.alfresco.rest.rm.community.base.BaseRMRestTest;
import org.alfresco.rest.rm.community.model.hold.HoldEntry;
import org.alfresco.rest.rm.community.model.record.Record;
import org.alfresco.rest.rm.community.model.recordcategory.RecordCategory;
import org.alfresco.rest.rm.community.model.recordcategory.RecordCategoryChild;
import org.alfresco.rest.rm.community.model.user.UserRoles;
import org.alfresco.rest.rm.community.requests.gscore.api.RecordFolderAPI;
import org.alfresco.rest.v0.HoldsAPI;
@@ -92,16 +94,16 @@ public class AddContentToHoldsTests extends BaseRMRestTest
private SiteModel testSite;
private String holdNodeRef;
private FileModel document, contentToAddToHold, contentAddToHoldNoPermission;
private FileModel documentHeld, contentToAddToHold, contentAddToHoldNoPermission;
private UserModel userAddHoldPermission;
private List<UserModel> users = new ArrayList<>();
private List<String> nodesToBeClean = new ArrayList<>();
@Autowired
private HoldsAPI holdsAPI;
@Autowired
private RoleService roleService;
@BeforeClass (alwaysRun = true)
public void preconditionForAddContentToHold() throws Exception
{
@@ -111,7 +113,7 @@ public class AddContentToHoldsTests extends BaseRMRestTest
STEP("Create test files.");
testSite = dataSite.usingAdmin().createPublicRandomSite();
document = dataContent.usingSite(testSite)
documentHeld = dataContent.usingSite(testSite)
.createContent(CMISUtil.DocumentType.TEXT_PLAIN);
contentToAddToHold = dataContent.usingSite(testSite)
.createContent(CMISUtil.DocumentType.TEXT_PLAIN);
@@ -119,7 +121,7 @@ public class AddContentToHoldsTests extends BaseRMRestTest
.createContent(CMISUtil.DocumentType.TEXT_PLAIN);
STEP("Add the content to the hold.");
holdsAPI.addItemToHold(getAdminUser().getUsername(), getAdminUser().getPassword(), document
holdsAPI.addItemToHold(getAdminUser().getUsername(), getAdminUser().getPassword(), documentHeld
.getNodeRefWithoutVersion(), HOLD);
STEP("Create users");
@@ -142,13 +144,13 @@ public class AddContentToHoldsTests extends BaseRMRestTest
List<RestNodeModel> documentsHeld = restClient.authenticateUser(getAdminUser()).withCoreAPI()
.usingNode(toContentModel(holdNodeRef))
.listChildren().getEntries().stream()
.filter(child -> child.onModel().getName().contains(document
.filter(child -> child.onModel().getName().contains(documentHeld
.getName()))
.collect(Collectors.toList());
STEP("Check the list of active content");
assertEquals(documentsHeld.size(), 1, "The active content is not retrive when getting the children from the " +
"hold folder");
assertEquals(documentsHeld.get(0).onModel().getName(), document.getName());
assertEquals(documentsHeld.get(0).onModel().getName(), documentHeld.getName());
}
/**
@@ -160,7 +162,7 @@ public class AddContentToHoldsTests extends BaseRMRestTest
public void retrieveTheHoldWhereTheContentIsAdded()
{
List<HoldEntry> holdEntries = holdsAPI.getHolds(getAdminUser().getUsername(), getAdminUser().getPassword(),
document.getNodeRefWithoutVersion(), true, null);
documentHeld.getNodeRefWithoutVersion(), true, null);
assertTrue(holdEntries.stream().anyMatch(holdEntry -> holdEntry.getName().contains(HOLD)), "Could not find " +
"hold with name " + HOLD);
}
@@ -172,24 +174,26 @@ public class AddContentToHoldsTests extends BaseRMRestTest
public Object[][] getValidNodesForAddToHold() throws Exception
{
//create electronic and nonElectronic record in record folder
String recordFolderId = createCategoryFolderInFilePlan().getId();
RecordCategoryChild recordFolder = createCategoryFolderInFilePlan();
RecordFolderAPI recordFolderAPI = getRestAPIFactory().getRecordFolderAPI();
Record electronicRecord = recordFolderAPI.createRecord(createElectronicRecordModel(), recordFolderId, getFile
nodesToBeClean.add(recordFolder.getParentId());
Record electronicRecord = recordFolderAPI.createRecord(createElectronicRecordModel(), recordFolder.getId(), getFile
(IMAGE_FILE));
assertStatusCode(CREATED);
Record nonElectronicRecord = recordFolderAPI.createRecord(createNonElectronicRecordModel(), recordFolderId);
Record nonElectronicRecord = recordFolderAPI.createRecord(createNonElectronicRecordModel(), recordFolder.getId());
assertStatusCode(CREATED);
getRestAPIFactory().getRMUserAPI().addUserPermission(recordFolderId, userAddHoldPermission, PERMISSION_FILING);
String folderToHold = createCategoryFolderInFilePlan().getId();
getRestAPIFactory().getRMUserAPI().addUserPermission(folderToHold, userAddHoldPermission, PERMISSION_FILING);
getRestAPIFactory().getRMUserAPI().addUserPermission(recordFolder.getId(), userAddHoldPermission,
PERMISSION_FILING);
RecordCategoryChild folderToHold = createCategoryFolderInFilePlan();
getRestAPIFactory().getRMUserAPI().addUserPermission(folderToHold.getId(), userAddHoldPermission,
PERMISSION_FILING);
nodesToBeClean.add(folderToHold.getParentId());
return new String[][]
{ // record folder
{ folderToHold },
{ folderToHold.getId() },
//electronic record
{ electronicRecord.getId() },
// non electronic record
@@ -231,12 +235,13 @@ public class AddContentToHoldsTests extends BaseRMRestTest
@DataProvider (name = "userWithoutPermissionForAddToHold")
public Object[][] getUserWithoutPermissionForAddToHold() throws Exception
{
//create electronic and nonElectronic record in record folder
String recordFolderId = createCategoryFolderInFilePlan().getId();
UserModel user = roleService.createUserWithRMRoleAndRMNodePermission(ROLE_RM_MANAGER.roleId, recordFolderId,
//create record folder
RecordCategoryChild recordFolder = createCategoryFolderInFilePlan();
//create a rm manager and grant read permission over the record folder created
UserModel user = roleService.createUserWithRMRoleAndRMNodePermission(ROLE_RM_MANAGER.roleId, recordFolder.getId(),
PERMISSION_READ_RECORDS);
getRestAPIFactory().getRMUserAPI().addUserPermission(holdNodeRef, user, PERMISSION_FILING);
nodesToBeClean.add(recordFolder.getParentId());
return new Object[][]
{ // user without write permission on the content
{
@@ -261,7 +266,7 @@ public class AddContentToHoldsTests extends BaseRMRestTest
},
//user without write permission on RM record folder
{
user, recordFolderId
user, recordFolder.getId()
},
};
@@ -298,19 +303,20 @@ public class AddContentToHoldsTests extends BaseRMRestTest
/**
* Data provider withh invalid item types that can be added to a hold
* Data provider with invalid item types that can be added to a hold
*/
@DataProvider (name = "invalidNodesForAddToHold")
public Object[][] getInvalidNodesForAddToHold() throws Exception
{
RecordCategory category = createRootCategory(getRandomAlphanumeric());
nodesToBeClean.add(category.getId());
return new Object[][]
{ // file plan node id
{ getFilePlan(FILE_PLAN_ALIAS).getId(), SC_BAD_REQUEST, INVALID_TYPE_ERROR_MESSAGE },
//transfer container
{ getTransferContainer(TRANSFERS_ALIAS).getId(), SC_BAD_REQUEST, INVALID_TYPE_ERROR_MESSAGE },
// an arbitrary record category
{ createRootCategory(getRandomAlphanumeric()).getId(), SC_INTERNAL_SERVER_ERROR, ACCESS_DENIED_ERROR_MESSAGE },
// a record category
{ category.getId(), SC_INTERNAL_SERVER_ERROR, ACCESS_DENIED_ERROR_MESSAGE },
// unfiled records root
{ getUnfiledContainer(UNFILED_RECORDS_CONTAINER_ALIAS).getId(), SC_BAD_REQUEST, INVALID_TYPE_ERROR_MESSAGE },
// an arbitrary unfiled records folder
@@ -337,7 +343,7 @@ public class AddContentToHoldsTests extends BaseRMRestTest
String responseErrorMessage = holdsAPI.addToHoldAndGetMessage(userAddHoldPermission.getUsername(),
userAddHoldPermission.getPassword(), responseCode, itemNodeRef, HOLD);
assertTrue(responseErrorMessage.contains(errorMessage),
"Actual message " + responseErrorMessage);
"Actual error message " + responseErrorMessage + " expected " + responseErrorMessage);
STEP("Check active content is not frozen.");
RestNodeModel heldActiveContent = restClient.authenticateUser(getAdminUser())
@@ -351,5 +357,6 @@ public class AddContentToHoldsTests extends BaseRMRestTest
holdsAPI.deleteHold(getAdminUser().getUsername(), getAdminUser().getPassword(), HOLD);
dataSite.usingAdmin().deleteSite(testSite);
users.forEach(user -> getDataUser().usingAdmin().deleteUser(user));
nodesToBeClean.forEach( category -> getRestAPIFactory().getRecordCategoryAPI().deleteRecordCategory(category));
}
}

View File

@@ -67,7 +67,7 @@ public class PreventActionsOnFrozenContentTests extends BaseRMRestTest
private HoldsAPI holdsAPI;
@BeforeClass (alwaysRun = true)
public void preconditionForRemoveContentFromHold() throws Exception
public void preconditionForPreventActionsOnFrozenContent() throws Exception
{
STEP("Create a hold.");
holdNodeRef = holdsAPI.createHoldAndGetNodeRef(getAdminUser().getUsername(), getAdminUser().getUsername(),
@@ -138,7 +138,7 @@ public class PreventActionsOnFrozenContentTests extends BaseRMRestTest
STEP("Check the request failed.");
restClient.assertStatusCodeIs(HttpStatus.FORBIDDEN);
restClient.assertLastError().containsSummary("Frozen nodes can not be updated.");
restClient.assertLastError().containsSummary("Frozen nodes can not be deleted.");
}
@@ -173,12 +173,12 @@ public class PreventActionsOnFrozenContentTests extends BaseRMRestTest
STEP("Check the request failed.");
restClient.assertStatusCodeIs(HttpStatus.FORBIDDEN);
restClient.assertLastError().containsSummary("Frozen nodes can not be copied.");
restClient.assertLastError().containsSummary("Frozen nodes can not be updated.");
}
@AfterClass (alwaysRun = true)
public void cleanUpAddContentToHold() throws Exception
public void cleanUpPreventActionsOnFrozenContent() throws Exception
{
holdsAPI.deleteHold(getAdminUser().getUsername(), getAdminUser().getPassword(), HOLD_ONE);
dataSite.usingAdmin().deleteSite(testSite);