()
{
public String doWork() throws Exception
{
repoService.deleteUser(username, network);
return null;
}
}, tenantDomain);
}
}, networkAdmin);
}
protected SiteMember addSiteMember(String siteId, String userId, final SiteRole siteRole) throws Exception
{
SiteMember siteMember = new SiteMember(userId, siteRole.name());
HttpResponse response = publicApiClient.post(getScope(), "sites", siteId, "members", null, siteMember.toJSON().toString());
checkStatus(201, response.getStatusCode());
return SiteMember.parseSiteMember(siteMember.getSiteId(), (JSONObject)response.getJsonResponse().get("entry"));
}
protected Site createSite(String siteTitle, SiteVisibility siteVisibility) throws Exception
{
return createSite(null, siteTitle, null, siteVisibility, 201);
}
protected Site createSite(String siteId, String siteTitle, String siteDescription, SiteVisibility siteVisibility, int expectedStatus) throws Exception
{
Site site = new Site();
site.setId(siteId);
site.setTitle(siteTitle);
site.setVisibility(siteVisibility);
site.setDescription(siteDescription);
HttpResponse response = publicApiClient.post(getScope(), "sites", null, null, null, toJsonAsStringNonNull(site));
checkStatus(expectedStatus, response.getStatusCode());
return RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Site.class);
}
protected HttpResponse deleteSite(String siteId, boolean permanent, int expectedStatus) throws Exception
{
Map params = null;
if (permanent == true)
{
params = Collections.singletonMap("permanent", "true");
}
HttpResponse response = publicApiClient.delete(getScope(), 1, "sites", siteId, null, null, params);
checkStatus(expectedStatus, response.getStatusCode());
return response;
}
/**
* Helper: to get site container id (see also RepoService.getContainerNodeRef -> SiteService.getContainer)
*
* GET /nodes/-root?relativePath=/Sites/siteId/documentLibrary
*
* alternatively:
*
* GET /nodes/siteNodeId?relativePath=documentLibrary
*/
protected String getSiteContainerNodeId(String siteId, String containerNameId) throws Exception
{
Map params = Collections.singletonMap(Nodes.PARAM_RELATIVE_PATH, "/Sites/" + siteId + "/" + containerNameId);
HttpResponse response = publicApiClient.get(NodesEntityResource.class, Nodes.PATH_ROOT, null, params);
checkStatus(200, response.getStatusCode());
Node node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
return node.getId();
}
protected void checkStatus(int expectedStatus, int actualStatus)
{
if (expectedStatus > 0 && expectedStatus != actualStatus)
{
fail("Status code " + actualStatus + " returned, but expected " + expectedStatus);
}
}
/**
* @deprecated
*
* @param runAsUser
*/
protected void setRequestContext(String runAsUser)
{
String password = null;
if ((runAsUser != null) && runAsUser.equals(DEFAULT_ADMIN))
{
// TODO improve "admin" related tests
password = DEFAULT_ADMIN_PWD;
}
// Assume "networkN1" if set !
String runAsNetwork = (networkOne != null ? networkOne.getId() : null);
setRequestContext(runAsNetwork, runAsUser, password);
}
/**
* TODO implement as remote (login) api call
*/
protected void setRequestContext(String runAsNetwork, String runAsUser, String password)
{
if ((runAsNetwork == null) || TenantService.DEFAULT_DOMAIN.equals(runAsNetwork))
{
runAsNetwork = "-default-";
}
else if ((runAsUser != null) && runAsUser.equals(DEFAULT_ADMIN))
{
runAsUser = runAsUser+"@"+runAsNetwork;
}
publicApiClient.setRequestContext(new RequestContext(runAsNetwork, runAsUser, password));
}
// -root- (eg. Company Home for on-prem)
protected String getRootNodeId() throws Exception
{
HttpResponse response = getSingle(NodesEntityResource.class, Nodes.PATH_ROOT, null, 200);
Node node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
return node.getId();
}
// -my- (eg. User's Home for on-prem)
protected String getMyNodeId() throws Exception
{
HttpResponse response = getSingle(NodesEntityResource.class, Nodes.PATH_MY, null, 200);
Node node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
return node.getId();
}
// -shared- (eg. "Shared" folder for on-prem)
protected String getSharedNodeId() throws Exception
{
HttpResponse response = getSingle(NodesEntityResource.class, Nodes.PATH_SHARED, null, 200);
Node node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
return node.getId();
}
protected Folder createFolder(String parentId, String folderName) throws Exception
{
return createFolder(parentId, folderName, null);
}
protected Folder createFolder(String parentId, String folderName, Map props) throws Exception
{
return createNode(parentId, folderName, TYPE_CM_FOLDER, props, Folder.class);
}
protected Node createNode(String parentId, String nodeName, String nodeType, Map props) throws Exception
{
return createNode(parentId, nodeName, nodeType, props, Node.class);
}
protected T createNode(String parentId, String nodeName, String nodeType, Map props, Class returnType)
throws Exception
{
Node n = new Node();
n.setName(nodeName);
n.setNodeType(nodeType);
n.setProperties(props);
// create node
HttpResponse response = post(getNodeChildrenUrl(parentId), RestApiUtil.toJsonAsStringNonNull(n), 201);
return RestApiUtil.parseRestApiEntry(response.getJsonResponse(), returnType);
}
protected void deleteNode(String nodeId) throws Exception
{
deleteNode(nodeId, 204);
}
protected void deleteNode(String nodeId, int expectedStatus) throws Exception
{
deleteNode(nodeId, false, expectedStatus);
}
protected void deleteNode(String nodeId, boolean permanent, int expectedStatus) throws Exception
{
Map params = null;
if (permanent == true)
{
params = Collections.singletonMap("permanent", "true");
}
delete(URL_NODES, nodeId, params, expectedStatus);
}
protected Document createTextFile(String parentId, String fileName, String textContent) throws IOException, Exception
{
return createTextFile(parentId, fileName, textContent, "UTF-8", null);
}
protected Document createTextFile(String parentId, String fileName, String textContent, String encoding, Map props) throws IOException, Exception
{
return createTextFile(parentId, fileName, textContent, encoding, props, 201);
}
protected Document createTextFile(String parentId, String fileName, String textContent, String encoding, Map props, int expectedStatus) throws IOException, Exception
{
if (props == null)
{
props = Collections.EMPTY_MAP;
}
ByteArrayInputStream inputStream = new ByteArrayInputStream(textContent.getBytes());
File txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
MultiPartBuilder.MultiPartRequest reqBody = MultiPartBuilder.create()
.setFileData(new MultiPartBuilder.FileData(fileName, txtFile))
.setProperties(props)
.build();
HttpResponse response = post(getNodeChildrenUrl(parentId), reqBody.getBody(), null, reqBody.getContentType(), expectedStatus);
if (response.getJsonResponse().get("error") != null)
{
return null;
}
return RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
}
protected Document createEmptyTextFile(String parentFolderId, String docName) throws Exception
{
return createEmptyTextFile(parentFolderId, docName, null, 201);
}
protected Document createEmptyTextFile(String parentFolderId, String docName, Map params, int expectedStatus) throws Exception
{
Document d1 = new Document();
d1.setName(docName);
d1.setNodeType("cm:content");
ContentInfo ci = new ContentInfo();
ci.setMimeType("text/plain");
d1.setContent(ci);
// create empty file
HttpResponse response = post(getNodeChildrenUrl(parentFolderId), toJsonAsStringNonNull(d1), params, null, "alfresco", expectedStatus);
if (expectedStatus != 201)
{
return null;
}
return RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
}
protected Document updateTextFile(String contentId, String textContent, Map params) throws Exception
{
return updateTextFile(contentId, textContent, params, 200);
}
protected Document updateTextFile(String contentId, String textContent, Map params, int expectedStatus) throws Exception
{
ByteArrayInputStream inputStream = new ByteArrayInputStream(textContent.getBytes());
File txtFile = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
BinaryPayload payload = new BinaryPayload(txtFile);
HttpResponse response = putBinary(getNodeContentUrl(contentId), payload, null, params, expectedStatus);
if (expectedStatus != 200)
{
return null;
}
return RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
}
protected File getResourceFile(String fileName) throws FileNotFoundException
{
URL url = NodeApiTest.class.getClassLoader().getResource(RESOURCE_PREFIX + fileName);
if (url == null)
{
fail("Cannot get the resource: " + fileName);
}
return ResourceUtils.getFile(url);
}
protected Document lock(String nodeId, String body) throws Exception
{
HttpResponse response = post(getNodeOperationUrl(nodeId, "lock"), body, null, 200);
return RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
}
protected Document unlock(String nodeId) throws Exception
{
HttpResponse response = post(getNodeOperationUrl(nodeId, "unlock"), null, null, 200);
return RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
}
protected static final long PAUSE_TIME = 5000; //millisecond
protected static final int MAX_RETRY = 20;
protected Rendition waitAndGetRendition(String sourceNodeId, String renditionId) throws Exception
{
return waitAndGetRendition(sourceNodeId, renditionId, MAX_RETRY, PAUSE_TIME);
}
protected Rendition waitAndGetRendition(String sourceNodeId, String renditionId, int maxRetry, long pauseTime) throws Exception
{
int retryCount = 0;
while (retryCount < maxRetry)
{
try
{
HttpResponse response = getSingle(getNodeRenditionsUrl(sourceNodeId), renditionId, 200);
Rendition rendition = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Rendition.class);
assertNotNull(rendition);
assertEquals(Rendition.RenditionStatus.CREATED, rendition.getStatus());
return rendition;
}
catch (AssertionError ex)
{
// If the asynchronous create rendition action is not finished yet,
// wait for 'PAUSE_TIME' and try again.
retryCount++;
System.out.println("waitAndGetRendition: "+retryCount);
Thread.sleep(pauseTime);
}
}
return null;
}
protected Rendition createAndGetRendition(String sourceNodeId, String renditionId) throws Exception
{
Rendition renditionRequest = new Rendition();
renditionRequest.setId(renditionId);
int retryCount = 0;
while (retryCount < MAX_RETRY)
{
try
{
HttpResponse res = post(getNodeRenditionsUrl(sourceNodeId), toJsonAsString(renditionRequest), 202);
assertNull(res.getJsonResponse());
break;
}
catch (AssertionError ex)
{
// If no transformer is currently available,
// wait for 'PAUSE_TIME' and try again.
retryCount++;
System.out.println("waitAndGetRendition: "+retryCount);
Thread.sleep(PAUSE_TIME);
}
}
return waitAndGetRendition(sourceNodeId, renditionId);
}
protected String getNodeRenditionsUrl(String nodeId)
{
return URL_NODES + "/" + nodeId + "/" + URL_RENDITIONS;
}
protected String getNodeChildrenUrl(String nodeId)
{
return URL_NODES + "/" + nodeId + "/" + URL_CHILDREN;
}
protected String getNodeContentUrl(String nodeId)
{
return URL_NODES + "/" + nodeId + "/" + URL_CONTENT;
}
protected String getNodeOperationUrl(String nodeId, String operation)
{
return URL_NODES + "/" + nodeId + "/" + operation;
}
}