Merge branch 'hotfix-2.3/RM-4097' into hotfix-2.3/RM-4096_RM_4097

This commit is contained in:
Silviu Dinuta
2016-09-26 18:26:36 +03:00
5 changed files with 385 additions and 79 deletions

View File

@@ -596,6 +596,7 @@
<property name="permissionService" ref="permissionService"/> <property name="permissionService" ref="permissionService"/>
<property name="extendedSecurityService" ref="extendedSecurityService"/> <property name="extendedSecurityService" ref="extendedSecurityService"/>
<property name="transactionService" ref="transactionService"/> <property name="transactionService" ref="transactionService"/>
<property name="contentStreamer" ref="webscript.content.streamer" />
</bean> </bean>
<!-- REST impl for GET Holds --> <!-- REST impl for GET Holds -->

View File

@@ -4,9 +4,10 @@
Removes dynamic authorities from in place records created in previous verssions.<br/> Removes dynamic authorities from in place records created in previous verssions.<br/>
URL parameter batchsize is mandatory, and represents the number of records that are processed in one transaction.<br/> URL parameter batchsize is mandatory, and represents the number of records that are processed in one transaction.<br/>
URL parameter maxProcessedRecords is optional, and represents the maximum number of records that will be processed in one request.<br/> URL parameter maxProcessedRecords is optional, and represents the maximum number of records that will be processed in one request.<br/>
URL parameter export is optional, and if the it's value is true, will export the processed records into a csv file.<br/>
]]> ]]>
</description> </description>
<url>/api/rm/rm-dynamicauthorities?batchsize={batchsize}&amp;maxProcessedRecords={maxProcessedRecords?}</url> <url>/api/rm/rm-dynamicauthorities?batchsize={batchsize}&amp;maxProcessedRecords={maxProcessedRecords?}&amp;export={export?}</url>
<format default="json">argument</format> <format default="json">argument</format>
<authentication>admin</authentication> <authentication>admin</authentication>
<transaction allow="readonly">required</transaction> <transaction allow="readonly">required</transaction>

View File

@@ -18,12 +18,21 @@
*/ */
package org.alfresco.repo.web.scripts.roles; package org.alfresco.repo.web.scripts.roles;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel; import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel;
import org.alfresco.module.org_alfresco_module_rm.security.ExtendedReaderDynamicAuthority; import org.alfresco.module.org_alfresco_module_rm.security.ExtendedReaderDynamicAuthority;
@@ -33,19 +42,24 @@ import org.alfresco.repo.domain.node.NodeDAO;
import org.alfresco.repo.domain.patch.PatchDAO; import org.alfresco.repo.domain.patch.PatchDAO;
import org.alfresco.repo.domain.qname.QNameDAO; import org.alfresco.repo.domain.qname.QNameDAO;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.repo.web.scripts.content.ContentStreamer;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.PermissionService; import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService; import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.Pair; import org.alfresco.util.Pair;
import org.alfresco.util.TempFileProvider;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.Cache; import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript; import org.springframework.extensions.webscripts.Format;
import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest; import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
/** /**
* Webscript used for removing dynamic authorities from the records. * Webscript used for removing dynamic authorities from the records.
@@ -54,7 +68,7 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
* @since 2.3.0.7 * @since 2.3.0.7
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class DynamicAuthoritiesGet extends DeclarativeWebScript implements RecordsManagementModel public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsManagementModel
{ {
private static final String MESSAGE_PARAMETER_BATCHSIZE_GREATER_THAN_ZERO = "Parameter batchsize should be a number greater than 0."; private static final String MESSAGE_PARAMETER_BATCHSIZE_GREATER_THAN_ZERO = "Parameter batchsize should be a number greater than 0.";
private static final String MESSAGE_PROCESSING_BEGIN = "Processing - BEGIN"; private static final String MESSAGE_PROCESSING_BEGIN = "Processing - BEGIN";
@@ -64,13 +78,13 @@ public class DynamicAuthoritiesGet extends DeclarativeWebScript implements Recor
private static final String MESSAGE_BATCHSIZE_IS_INVALID = "Parameter batchsize is invalid."; private static final String MESSAGE_BATCHSIZE_IS_INVALID = "Parameter batchsize is invalid.";
private static final String MESSAGE_BATCHSIZE_IS_MANDATORY = "Parameter batchsize is mandatory"; private static final String MESSAGE_BATCHSIZE_IS_MANDATORY = "Parameter batchsize is mandatory";
private static final String SUCCESS_STATUS = "success"; private static final String SUCCESS_STATUS = "success";
private static final String FAILED_STATUS = "failed";
/** /**
* The logger * The logger
*/ */
private static Log logger = LogFactory.getLog(DynamicAuthoritiesGet.class); private static Log logger = LogFactory.getLog(DynamicAuthoritiesGet.class);
private static final String BATCH_SIZE = "batchsize"; private static final String BATCH_SIZE = "batchsize";
private static final String TOTAL_NUMBER_TO_PROCESS = "maxProcessedRecords"; private static final String TOTAL_NUMBER_TO_PROCESS = "maxProcessedRecords";
private static final String PARAM_EXPORT = "export";
private static final String MODEL_STATUS = "responsestatus"; private static final String MODEL_STATUS = "responsestatus";
private static final String MODEL_MESSAGE = "message"; private static final String MODEL_MESSAGE = "message";
private static final String MESSAGE_ALL_TEMPLATE = "Processed {0} records."; private static final String MESSAGE_ALL_TEMPLATE = "Processed {0} records.";
@@ -86,50 +100,53 @@ public class DynamicAuthoritiesGet extends DeclarativeWebScript implements Recor
private PermissionService permissionService; private PermissionService permissionService;
private ExtendedSecurityService extendedSecurityService; private ExtendedSecurityService extendedSecurityService;
private TransactionService transactionService; private TransactionService transactionService;
/** Content Streamer */
protected ContentStreamer contentStreamer;
/** service setters */
public void setPatchDAO(PatchDAO patchDAO)
{
this.patchDAO = patchDAO;
}
/** service setters */ public void setNodeDAO(NodeDAO nodeDAO)
public void setPatchDAO(PatchDAO patchDAO) { this.patchDAO = patchDAO; } {
public void setNodeDAO(NodeDAO nodeDAO) { this.nodeDAO = nodeDAO; } this.nodeDAO = nodeDAO;
public void setQnameDAO(QNameDAO qnameDAO) { this.qnameDAO = qnameDAO; } }
public void setNodeService(NodeService nodeService) { this.nodeService = nodeService; }
public void setPermissionService(PermissionService permissionService) { this.permissionService = permissionService; }
public void setExtendedSecurityService(ExtendedSecurityService extendedSecurityService) { this.extendedSecurityService = extendedSecurityService; }
public void setTransactionService(TransactionService transactionService) { this.transactionService = transactionService; }
@Override public void setQnameDAO(QNameDAO qnameDAO)
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{ {
Map<String, Object> model = new HashMap<String, Object>(); this.qnameDAO = qnameDAO;
String batchSizeStr = req.getParameter(BATCH_SIZE); }
String totalToBeProcessedRecordsStr = req.getParameter(TOTAL_NUMBER_TO_PROCESS);
Long size = 0L; public void setNodeService(NodeService nodeService)
if (StringUtils.isBlank(batchSizeStr))
{ {
model.put(MODEL_STATUS, FAILED_STATUS); this.nodeService = nodeService;
model.put(MODEL_MESSAGE, MESSAGE_BATCHSIZE_IS_MANDATORY);
logger.info(MESSAGE_BATCHSIZE_IS_MANDATORY);
return model;
} }
try
public void setPermissionService(PermissionService permissionService)
{ {
size = Long.parseLong(batchSizeStr); this.permissionService = permissionService;
if(size <= 0) }
public void setExtendedSecurityService(ExtendedSecurityService extendedSecurityService)
{ {
model.put(MODEL_STATUS, FAILED_STATUS); this.extendedSecurityService = extendedSecurityService;
model.put(MODEL_MESSAGE, MESSAGE_PARAMETER_BATCHSIZE_GREATER_THAN_ZERO);
logger.info(MESSAGE_PARAMETER_BATCHSIZE_GREATER_THAN_ZERO);
return model;
} }
public void setTransactionService(TransactionService transactionService)
{
this.transactionService = transactionService;
} }
catch(NumberFormatException ex)
public void setContentStreamer(ContentStreamer contentStreamer)
{ {
model.put(MODEL_STATUS, FAILED_STATUS); this.contentStreamer = contentStreamer;
model.put(MODEL_MESSAGE, MESSAGE_BATCHSIZE_IS_INVALID);
logger.info(MESSAGE_BATCHSIZE_IS_INVALID);
return model;
} }
final Long batchSize = size;
protected Map<String, Object> buildModel(WebScriptRequest req, WebScriptResponse res) throws IOException
{
Map<String, Object> model = new HashMap<String, Object>();
final Long batchSize = getBatchSizeParameter(req);
// get the max node id and the extended security aspect // get the max node id and the extended security aspect
Long maxNodeId = patchDAO.getMaxAdmNodeID(); Long maxNodeId = patchDAO.getMaxAdmNodeID();
final Pair<Long, QName> recordAspectPair = qnameDAO.getQName(ASPECT_EXTENDED_SECURITY); final Pair<Long, QName> recordAspectPair = qnameDAO.getQName(ASPECT_EXTENDED_SECURITY);
@@ -141,6 +158,194 @@ public class DynamicAuthoritiesGet extends DeclarativeWebScript implements Recor
return model; return model;
} }
Long totalNumberOfRecordsToProcess = getMaxToProccessParameter(req, batchSize);
boolean attach = getExportParameter(req);
File file = TempFileProvider.createTempFile("processedNodes_", ".csv");
FileWriter writer = new FileWriter(file);
BufferedWriter out = new BufferedWriter(writer);
List<NodeRef> processedNodes = new ArrayList<NodeRef>();
try
{
processedNodes = processNodes(batchSize, maxNodeId, recordAspectPair, totalNumberOfRecordsToProcess, out,
attach);
}
finally
{
out.close();
}
int processedNodesSize = processedNodes.size();
String message = "";
if (totalNumberOfRecordsToProcess == 0
|| (totalNumberOfRecordsToProcess > 0 && processedNodesSize < totalNumberOfRecordsToProcess))
{
message = MessageFormat.format(MESSAGE_ALL_TEMPLATE, processedNodesSize);
}
if (totalNumberOfRecordsToProcess > 0 && totalNumberOfRecordsToProcess == processedNodesSize)
{
message = MessageFormat.format(MESSAGE_PARTIAL_TEMPLATE, totalNumberOfRecordsToProcess);
}
model.put(MODEL_STATUS, SUCCESS_STATUS);
model.put(MODEL_MESSAGE, message);
logger.info(message);
if (attach)
{
try
{
String fileName = file.getName();
contentStreamer.streamContent(req, res, file, null, attach, fileName, model);
model = null;
}
finally
{
if (file != null)
{
file.delete();
}
}
}
return model;
}
/**
* Get export parameter from the request
* @param req
* @return
*/
protected boolean getExportParameter(WebScriptRequest req)
{
boolean attach = false;
String export = req.getParameter(PARAM_EXPORT);
if (export != null && Boolean.parseBoolean(export))
{
attach = true;
}
return attach;
}
/*
* (non-Javadoc)
* @see org.alfresco.repo.web.scripts.content.StreamContent#execute(org.springframework.extensions.webscripts.
* WebScriptRequest, org.springframework.extensions.webscripts.WebScriptResponse)
*/
@Override
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException
{
// retrieve requested format
String format = req.getFormat();
try
{
String mimetype = getContainer().getFormatRegistry().getMimeType(req.getAgent(), format);
if (mimetype == null)
{
throw new WebScriptException("Web Script format '" + format + "' is not registered");
}
// construct model for script / template
Status status = new Status();
Cache cache = new Cache(getDescription().getRequiredCache());
Map<String, Object> model = buildModel(req, res);
if (model == null)
{
return;
}
model.put("status", status);
model.put("cache", cache);
Map<String, Object> templateModel = createTemplateParameters(req, res, model);
// render output
int statusCode = status.getCode();
if (statusCode != HttpServletResponse.SC_OK && !req.forceSuccessStatus())
{
if (logger.isDebugEnabled())
{
logger.debug("Force success status header in response: " + req.forceSuccessStatus());
logger.debug("Setting status " + statusCode);
}
res.setStatus(statusCode);
}
// apply location
String location = status.getLocation();
if (location != null && location.length() > 0)
{
if (logger.isDebugEnabled()) logger.debug("Setting location to " + location);
res.setHeader(WebScriptResponse.HEADER_LOCATION, location);
}
// apply cache
res.setCache(cache);
String callback = null;
if (getContainer().allowCallbacks())
{
callback = req.getJSONCallback();
}
if (format.equals(WebScriptResponse.JSON_FORMAT) && callback != null)
{
if (logger.isDebugEnabled()) logger.debug("Rendering JSON callback response: content type="
+ Format.JAVASCRIPT.mimetype() + ", status=" + statusCode + ", callback=" + callback);
// NOTE: special case for wrapping JSON results in a javascript function callback
res.setContentType(Format.JAVASCRIPT.mimetype() + ";charset=UTF-8");
res.getWriter().write((callback + "("));
}
else
{
if (logger.isDebugEnabled())
logger.debug("Rendering response: content type=" + mimetype + ", status=" + statusCode);
res.setContentType(mimetype + ";charset=UTF-8");
}
// render response according to requested format
renderFormatTemplate(format, templateModel, res.getWriter());
if (format.equals(WebScriptResponse.JSON_FORMAT) && callback != null)
{
// NOTE: special case for wrapping JSON results in a javascript function callback
res.getWriter().write(")");
}
}
catch (Throwable e)
{
if (logger.isDebugEnabled())
{
StringWriter stack = new StringWriter();
e.printStackTrace(new PrintWriter(stack));
logger.debug("Caught exception; decorating with appropriate status template : " + stack.toString());
}
throw createStatusException(e, req, res);
}
}
protected void renderFormatTemplate(String format, Map<String, Object> model, Writer writer)
{
format = (format == null) ? "" : format;
String templatePath = getDescription().getId() + "." + format;
if (logger.isDebugEnabled()) logger.debug("Rendering template '" + templatePath + "'");
renderTemplate(templatePath, model, writer);
}
/**
* Obtain maximum of the records to be processed from the request if it is specified or bachsize value otherwise
*
* @param req
* @return maximum of the records to be processed from the request if it is specified or bachsize value otherwise
*/
protected Long getMaxToProccessParameter(WebScriptRequest req, final Long batchSize)
{
String totalToBeProcessedRecordsStr = req.getParameter(TOTAL_NUMBER_TO_PROCESS);
//default total number of records to be processed to batch size value //default total number of records to be processed to batch size value
Long totalNumberOfRecordsToProcess = batchSize; Long totalNumberOfRecordsToProcess = batchSize;
if (StringUtils.isNotBlank(totalToBeProcessedRecordsStr)) if (StringUtils.isNotBlank(totalToBeProcessedRecordsStr))
@@ -154,7 +359,54 @@ public class DynamicAuthoritiesGet extends DeclarativeWebScript implements Recor
//do nothing here, the value will remain 0L in this case //do nothing here, the value will remain 0L in this case
} }
} }
return totalNumberOfRecordsToProcess;
}
/**
* Obtain batchsize parameter from the request.
*
* @param req
* @return batchsize parameter from the request
*/
protected Long getBatchSizeParameter(WebScriptRequest req)
{
String batchSizeStr = req.getParameter(BATCH_SIZE);
Long size = 0L;
if (StringUtils.isBlank(batchSizeStr))
{
logger.info(MESSAGE_BATCHSIZE_IS_MANDATORY);
throw new WebScriptException(Status.STATUS_BAD_REQUEST, MESSAGE_BATCHSIZE_IS_MANDATORY);
}
try
{
size = Long.parseLong(batchSizeStr);
if (size <= 0)
{
logger.info(MESSAGE_PARAMETER_BATCHSIZE_GREATER_THAN_ZERO);
throw new WebScriptException(Status.STATUS_BAD_REQUEST, MESSAGE_PARAMETER_BATCHSIZE_GREATER_THAN_ZERO);
}
}
catch (NumberFormatException ex)
{
logger.info(MESSAGE_BATCHSIZE_IS_INVALID);
throw new WebScriptException(Status.STATUS_BAD_REQUEST, MESSAGE_BATCHSIZE_IS_INVALID);
}
return size;
}
/**
* Process nodes all nodes or the maximum number of nodes specified by batchsize or totalNumberOfRecordsToProcess
* parameters
*
* @param batchSize
* @param maxNodeId
* @param recordAspectPair
* @param totalNumberOfRecordsToProcess
* @return the list of processed nodes
*/
protected List<NodeRef> processNodes(final Long batchSize, Long maxNodeId, final Pair<Long, QName> recordAspectPair,
Long totalNumberOfRecordsToProcess, final BufferedWriter out, final boolean attach)
{
final Long maxRecordsToProcess = totalNumberOfRecordsToProcess; final Long maxRecordsToProcess = totalNumberOfRecordsToProcess;
final List<NodeRef> processedNodes = new ArrayList<NodeRef>(); final List<NodeRef> processedNodes = new ArrayList<NodeRef>();
logger.info(MESSAGE_PROCESSING_BEGIN); logger.info(MESSAGE_PROCESSING_BEGIN);
@@ -172,7 +424,8 @@ public class DynamicAuthoritiesGet extends DeclarativeWebScript implements Recor
public Void execute() throws Throwable public Void execute() throws Throwable
{ {
// get the nodes with the extended security aspect applied // get the nodes with the extended security aspect applied
List<Long> nodeIds = patchDAO.getNodesByAspectQNameId(recordAspectPair.getFirst(), currentIndex, currentIndex + batchSize); List<Long> nodeIds = patchDAO.getNodesByAspectQNameId(recordAspectPair.getFirst(), currentIndex,
currentIndex + batchSize);
// process each one // process each one
for (Long nodeId : nodeIds) for (Long nodeId : nodeIds)
@@ -187,30 +440,23 @@ public class DynamicAuthoritiesGet extends DeclarativeWebScript implements Recor
processNode(record); processNode(record);
logger.info(MessageFormat.format(MESSAGE_PROCESSING_RECORD_END_TEMPLATE, recordName)); logger.info(MessageFormat.format(MESSAGE_PROCESSING_RECORD_END_TEMPLATE, recordName));
processedNodes.add(record); processedNodes.add(record);
if (attach)
{
out.write(recordName);
out.write(",");
out.write(record.toString());
out.write("\n");
} }
}
return null; return null;
} }
}, }, false, // read only
false, // read only
true); // requires new true); // requires new
} }
logger.info(MESSAGE_PROCESSING_END); logger.info(MESSAGE_PROCESSING_END);
int processedNodesSize = processedNodes.size(); return processedNodes;
String message = "";
if(totalNumberOfRecordsToProcess == 0 || (totalNumberOfRecordsToProcess > 0 && processedNodesSize < totalNumberOfRecordsToProcess))
{
message = MessageFormat.format(MESSAGE_ALL_TEMPLATE, processedNodesSize);
} }
if (totalNumberOfRecordsToProcess > 0 && totalNumberOfRecordsToProcess == processedNodesSize)
{
message = MessageFormat.format(MESSAGE_PARTIAL_TEMPLATE, totalNumberOfRecordsToProcess);
}
model.put(MODEL_STATUS, SUCCESS_STATUS);
model.put(MODEL_MESSAGE, message);
logger.info(message);
return model;
}
/** /**
* Process each node * Process each node
@@ -232,7 +478,7 @@ public class DynamicAuthoritiesGet extends DeclarativeWebScript implements Recor
permissionService.clearPermission(nodeRef, ExtendedWriterDynamicAuthority.EXTENDED_WRITER); permissionService.clearPermission(nodeRef, ExtendedWriterDynamicAuthority.EXTENDED_WRITER);
// if record then ... // if record then ...
if (nodeService.hasAspect(nodeRef, ASPECT_RECORD)) if (nodeService.hasAspect(nodeRef, ASPECT_RECORD) && readers != null && writers != null)
{ {
// re-set extended security via API // re-set extended security via API
extendedSecurityService.set(nodeRef, readers.keySet(), writers.keySet()); extendedSecurityService.set(nodeRef, readers.keySet(), writers.keySet());

View File

@@ -31,8 +31,8 @@ import org.json.JSONObject;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.springframework.extensions.surf.util.Content; import org.springframework.extensions.surf.util.Content;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.Container; import org.springframework.extensions.webscripts.Container;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Description; import org.springframework.extensions.webscripts.Description;
import org.springframework.extensions.webscripts.Description.RequiredCache; import org.springframework.extensions.webscripts.Description.RequiredCache;
import org.springframework.extensions.webscripts.DescriptionExtension; import org.springframework.extensions.webscripts.DescriptionExtension;
@@ -68,7 +68,7 @@ public abstract class BaseWebScriptUnitTest extends BaseUnitTest
/** /**
* @return declarative webscript * @return declarative webscript
*/ */
protected abstract DeclarativeWebScript getWebScript(); protected abstract AbstractWebScript getWebScript();
/** /**
* @return classpath location of webscript template * @return classpath location of webscript template
@@ -136,7 +136,7 @@ public abstract class BaseWebScriptUnitTest extends BaseUnitTest
*/ */
protected String executeWebScript(Map<String, String> parameters, String content) throws Exception protected String executeWebScript(Map<String, String> parameters, String content) throws Exception
{ {
DeclarativeWebScript webScript = getWebScript(); AbstractWebScript webScript = getWebScript();
String template = getWebScriptTemplate(); String template = getWebScriptTemplate();
// initialise webscript // initialise webscript
@@ -158,7 +158,7 @@ public abstract class BaseWebScriptUnitTest extends BaseUnitTest
* @return {@link WebScriptRequest} mocked web script request * @return {@link WebScriptRequest} mocked web script request
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
protected WebScriptRequest getMockedWebScriptRequest(DeclarativeWebScript webScript, final Map<String, String> parameters, String content) throws Exception protected WebScriptRequest getMockedWebScriptRequest(AbstractWebScript webScript, final Map<String, String> parameters, String content) throws Exception
{ {
Match match = new Match(null, parameters, null, webScript); Match match = new Match(null, parameters, null, webScript);
org.springframework.extensions.webscripts.Runtime mockedRuntime = mock(org.springframework.extensions.webscripts.Runtime.class); org.springframework.extensions.webscripts.Runtime mockedRuntime = mock(org.springframework.extensions.webscripts.Runtime.class);

View File

@@ -23,6 +23,7 @@ import static java.util.Collections.emptyMap;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyLong; import static org.mockito.Matchers.anyLong;
@@ -55,6 +56,7 @@ import org.alfresco.repo.domain.patch.PatchDAO;
import org.alfresco.repo.domain.qname.QNameDAO; import org.alfresco.repo.domain.qname.QNameDAO;
import org.alfresco.repo.transaction.RetryingTransactionHelper; import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.repo.web.scripts.content.ContentStreamer;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.PermissionService; import org.alfresco.service.cmr.security.PermissionService;
@@ -69,7 +71,9 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.springframework.extensions.webscripts.DeclarativeWebScript; import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
/** /**
* DynamicAuthoritiesGet Unit Test * DynamicAuthoritiesGet Unit Test
@@ -100,13 +104,15 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
private TransactionService mockedTransactionService; private TransactionService mockedTransactionService;
@Mock @Mock
private RetryingTransactionHelper mockedRetryingTransactionHelper; private RetryingTransactionHelper mockedRetryingTransactionHelper;
@Mock
private ContentStreamer contentStreamer;
/** test component */ /** test component */
@InjectMocks @InjectMocks
private DynamicAuthoritiesGet webScript; private DynamicAuthoritiesGet webScript;
@Override @Override
protected DeclarativeWebScript getWebScript() protected AbstractWebScript getWebScript()
{ {
return webScript; return webScript;
} }
@@ -281,39 +287,50 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
@Test @Test
public void missingBatchSizeParameter() throws Exception public void missingBatchSizeParameter() throws Exception
{ {
JSONObject json = executeJSONWebScript(emptyMap()); try
assertNotNull(json); {
String actualJSONString = json.toString(); executeJSONWebScript(emptyMap());
ObjectMapper mapper = new ObjectMapper(); fail("Expected exception as parameter batchsize is mandatory.");
String expectedJSONString = "{\"responsestatus\":\"failed\",\"message\":\"Parameter batchsize is mandatory\"}"; }
assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); catch (WebScriptException e)
{
assertEquals("If parameter batchsize is not provided then 'Bad request' should be returned.",
Status.STATUS_BAD_REQUEST, e.getStatus());
}
} }
@Test @Test
public void invalidBatchSizeParameter() throws Exception public void invalidBatchSizeParameter() throws Exception
{ {
try
{
// Set up parameters. // Set up parameters.
Map<String, String> parameters = ImmutableMap.of("batchsize", "dd"); Map<String, String> parameters = ImmutableMap.of("batchsize", "dd");
JSONObject json = executeJSONWebScript(parameters); executeJSONWebScript(parameters);
assertNotNull(json); fail("Expected exception as parameter batchsize is invalid.");
String actualJSONString = json.toString(); }
ObjectMapper mapper = new ObjectMapper(); catch (WebScriptException e)
String expectedJSONString = "{\"responsestatus\":\"failed\",\"message\":\"Parameter batchsize is invalid.\"}"; {
assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); assertEquals("If parameter batchsize is invalid then 'Bad request' should be returned.",
Status.STATUS_BAD_REQUEST, e.getStatus());
}
} }
@Test @Test
public void batchSizeShouldBeGraterThanZero() throws Exception public void batchSizeShouldBeGraterThanZero() throws Exception
{ {
when(mockedQnameDAO.getQName(ASPECT_EXTENDED_SECURITY)).thenReturn(null); try
{
// Set up parameters. // Set up parameters.
Map<String, String> parameters = ImmutableMap.of("batchsize", "0"); Map<String, String> parameters = ImmutableMap.of("batchsize", "0");
JSONObject json = executeJSONWebScript(parameters); executeJSONWebScript(parameters);
assertNotNull(json); fail("Expected exception as parameter batchsize is not a number greater than 0.");
String actualJSONString = json.toString(); }
ObjectMapper mapper = new ObjectMapper(); catch (WebScriptException e)
String expectedJSONString = "{\"responsestatus\":\"failed\",\"message\":\"Parameter batchsize should be a number greater than 0.\"}"; {
assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); assertEquals("If parameter batchsize is not a number greater than 0 then 'Bad request' should be returned.",
Status.STATUS_BAD_REQUEST, e.getStatus());
}
} }
@Test @Test
@@ -389,4 +406,45 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed first 4 records.\"}"; String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed first 4 records.\"}";
assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString)); assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString));
} }
@SuppressWarnings("unchecked")
@Test
public void recordsWithExtendedSecurityAspectAndNullWritersAndReaders() throws Exception
{
List<Long> ids = Stream.of(1l, 2l, 3l).collect(Collectors.toList());
when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong()))
.thenReturn(ids)
.thenReturn(Collections.emptyList());
ids.stream().forEach((i) -> {
NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService);
when(mockedNodeDAO.getNodePair(i)).thenReturn(new Pair<Long, NodeRef>(i, nodeRef));
when(mockedNodeService.hasAspect(nodeRef, ASPECT_RECORD)).thenReturn(true);
when(mockedNodeService.getProperty(nodeRef, PROP_READERS))
.thenReturn(null);
when(mockedNodeService.getProperty(nodeRef, PROP_WRITERS))
.thenReturn(null);
});
// Set up parameters.
Map<String, String> parameters = ImmutableMap.of("batchsize", "10", "maxProcessedRecords", "4");
JSONObject json = executeJSONWebScript(parameters);
assertNotNull(json);
String actualJSONString = json.toString();
ObjectMapper mapper = new ObjectMapper();
String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed 3 records.\"}";
assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString));
verify(mockedNodeService, times(3)).getProperty(any(NodeRef.class), eq(PROP_READERS));
verify(mockedNodeService, times(3)).getProperty(any(NodeRef.class), eq(PROP_WRITERS));
verify(mockedNodeService, times(3)).removeAspect(any(NodeRef.class), eq(ASPECT_EXTENDED_SECURITY));
verify(mockedPermissionService, times(3)).clearPermission(any(NodeRef.class),
eq(ExtendedReaderDynamicAuthority.EXTENDED_READER));
verify(mockedPermissionService, times(3)).clearPermission(any(NodeRef.class),
eq(ExtendedWriterDynamicAuthority.EXTENDED_WRITER));
verify(mockedExtendedSecurityService, never()).set(any(NodeRef.class), any(Set.class), any(Set.class));
}
} }