mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
RM-4162: added extra parentNodeRef parameter, checked writers and
readers individually for null and added unit tests for the changes
This commit is contained in:
@@ -597,6 +597,7 @@
|
||||
<property name="extendedSecurityService" ref="extendedSecurityService"/>
|
||||
<property name="transactionService" ref="transactionService"/>
|
||||
<property name="contentStreamer" ref="webscript.content.streamer" />
|
||||
<property name="fileFolderService" ref="FileFolderService"/>
|
||||
</bean>
|
||||
|
||||
<!-- REST impl for GET Holds -->
|
||||
|
@@ -2,12 +2,13 @@
|
||||
<shortname>Removes dynamic authorities</shortname>
|
||||
<description><![CDATA[
|
||||
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 maximum number of records that can be 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 export is optional, and if the it's value is true, will export the processed records into a csv file.<br/>
|
||||
URL parameter parentNodeRef is optional, and represents the nodeRef of the folder that contains the records to be processed.<br/>
|
||||
]]>
|
||||
</description>
|
||||
<url>/api/rm/rm-dynamicauthorities?batchsize={batchsize}&maxProcessedRecords={maxProcessedRecords?}&export={export?}</url>
|
||||
<url>/api/rm/rm-dynamicauthorities?batchsize={batchsize}&maxProcessedRecords={maxProcessedRecords?}&export={export?}&parentNodeRef={parentNodeRef?}</url>
|
||||
<format default="json">argument</format>
|
||||
<authentication>admin</authentication>
|
||||
<transaction allow="readonly">required</transaction>
|
||||
|
@@ -30,6 +30,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@@ -43,6 +44,8 @@ import org.alfresco.repo.domain.patch.PatchDAO;
|
||||
import org.alfresco.repo.domain.qname.QNameDAO;
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
|
||||
import org.alfresco.repo.web.scripts.content.ContentStreamer;
|
||||
import org.alfresco.service.cmr.model.FileFolderService;
|
||||
import org.alfresco.service.cmr.model.FileInfo;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.security.PermissionService;
|
||||
@@ -77,6 +80,7 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
private static final String MESSAGE_PROCESSING_RECORD_BEGIN_TEMPLATE = "Processing record {0} - BEGIN";
|
||||
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_NODE_REF_DOES_NOT_EXIST_TEMPLATE = "Parameter parentNodeRef = {0} does not exist.";
|
||||
private static final String SUCCESS_STATUS = "success";
|
||||
/**
|
||||
* The logger
|
||||
@@ -85,13 +89,13 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
private static final String BATCH_SIZE = "batchsize";
|
||||
private static final String TOTAL_NUMBER_TO_PROCESS = "maxProcessedRecords";
|
||||
private static final String PARAM_EXPORT = "export";
|
||||
private static final String PARAM_PARENT_NODE_REF = "parentNodeRef";
|
||||
private static final String MODEL_STATUS = "responsestatus";
|
||||
private static final String MODEL_MESSAGE = "message";
|
||||
private static final String MESSAGE_ALL_TEMPLATE = "Processed {0} records.";
|
||||
private static final String MESSAGE_PARTIAL_TEMPLATE = "Processed first {0} records.";
|
||||
private static final String MESSAGE_NO_RECORDS_TO_PROCESS = "There where no records to be processed.";
|
||||
|
||||
|
||||
/** services */
|
||||
private PatchDAO patchDAO;
|
||||
private NodeDAO nodeDAO;
|
||||
@@ -102,6 +106,8 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
private TransactionService transactionService;
|
||||
/** Content Streamer */
|
||||
protected ContentStreamer contentStreamer;
|
||||
private FileFolderService fileFolderService;
|
||||
|
||||
/** service setters */
|
||||
public void setPatchDAO(PatchDAO patchDAO)
|
||||
{
|
||||
@@ -119,29 +125,34 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public void setContentStreamer(ContentStreamer contentStreamer)
|
||||
{
|
||||
{
|
||||
this.contentStreamer = contentStreamer;
|
||||
}
|
||||
}
|
||||
|
||||
public void setFileFolderService(FileFolderService fileFolderService)
|
||||
{
|
||||
this.fileFolderService = fileFolderService;
|
||||
}
|
||||
|
||||
protected Map<String, Object> buildModel(WebScriptRequest req, WebScriptResponse res) throws IOException
|
||||
{
|
||||
@@ -150,7 +161,7 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
// get the max node id and the extended security aspect
|
||||
Long maxNodeId = patchDAO.getMaxAdmNodeID();
|
||||
final Pair<Long, QName> recordAspectPair = qnameDAO.getQName(ASPECT_EXTENDED_SECURITY);
|
||||
if(recordAspectPair == null)
|
||||
if (recordAspectPair == null)
|
||||
{
|
||||
model.put(MODEL_STATUS, SUCCESS_STATUS);
|
||||
model.put(MODEL_MESSAGE, MESSAGE_NO_RECORDS_TO_PROCESS);
|
||||
@@ -168,8 +179,17 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
List<NodeRef> processedNodes = new ArrayList<NodeRef>();
|
||||
try
|
||||
{
|
||||
processedNodes = processNodes(batchSize, maxNodeId, recordAspectPair, totalNumberOfRecordsToProcess, out,
|
||||
attach);
|
||||
NodeRef parentNodeRef = getParentNodeRefParameter(req);
|
||||
if (parentNodeRef != null)
|
||||
{
|
||||
processedNodes = processChildrenNodes(parentNodeRef, batchSize.intValue(), recordAspectPair,
|
||||
totalNumberOfRecordsToProcess.intValue(), out, attach);
|
||||
}
|
||||
else
|
||||
{
|
||||
processedNodes = processNodes(batchSize, maxNodeId, recordAspectPair, totalNumberOfRecordsToProcess,
|
||||
out, attach);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -213,6 +233,7 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
|
||||
/**
|
||||
* Get export parameter from the request
|
||||
*
|
||||
* @param req
|
||||
* @return
|
||||
*/
|
||||
@@ -241,19 +262,14 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
try
|
||||
{
|
||||
String mimetype = getContainer().getFormatRegistry().getMimeType(req.getAgent(), format);
|
||||
if (mimetype == null)
|
||||
{
|
||||
throw new WebScriptException("Web Script format '" + format + "' is not registered");
|
||||
}
|
||||
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;
|
||||
}
|
||||
if (model == null) { return; }
|
||||
model.put("status", status);
|
||||
model.put("cache", cache);
|
||||
|
||||
@@ -346,7 +362,7 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
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;
|
||||
if (StringUtils.isNotBlank(totalToBeProcessedRecordsStr))
|
||||
{
|
||||
@@ -354,9 +370,9 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
{
|
||||
totalNumberOfRecordsToProcess = Long.parseLong(totalToBeProcessedRecordsStr);
|
||||
}
|
||||
catch(NumberFormatException ex)
|
||||
catch (NumberFormatException ex)
|
||||
{
|
||||
//do nothing here, the value will remain 0L in this case
|
||||
// do nothing here, the value will remain 0L in this case
|
||||
}
|
||||
}
|
||||
return totalNumberOfRecordsToProcess;
|
||||
@@ -394,6 +410,29 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parentNodeRef parameter from the request
|
||||
*
|
||||
* @param req
|
||||
* @return
|
||||
*/
|
||||
protected NodeRef getParentNodeRefParameter(WebScriptRequest req)
|
||||
{
|
||||
String parentNodeRefStr = req.getParameter(PARAM_PARENT_NODE_REF);
|
||||
NodeRef parentNodeRef = null;
|
||||
if (StringUtils.isNotBlank(parentNodeRefStr))
|
||||
{
|
||||
parentNodeRef = new NodeRef(parentNodeRefStr);
|
||||
if(!nodeService.exists(parentNodeRef))
|
||||
{
|
||||
String message = MessageFormat.format(MESSAGE_NODE_REF_DOES_NOT_EXIST_TEMPLATE, parentNodeRef.toString());
|
||||
logger.info(message);
|
||||
throw new WebScriptException(Status.STATUS_BAD_REQUEST, message);
|
||||
}
|
||||
}
|
||||
return parentNodeRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process nodes all nodes or the maximum number of nodes specified by batchsize or totalNumberOfRecordsToProcess
|
||||
* parameters
|
||||
@@ -411,9 +450,9 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
final List<NodeRef> processedNodes = new ArrayList<NodeRef>();
|
||||
logger.info(MESSAGE_PROCESSING_BEGIN);
|
||||
// by batch size
|
||||
for (Long i = 0L; i < maxNodeId; i+=batchSize)
|
||||
for (Long i = 0L; i < maxNodeId; i += batchSize)
|
||||
{
|
||||
if(maxRecordsToProcess != 0 && processedNodes.size() >= maxRecordsToProcess)
|
||||
if (maxRecordsToProcess != 0 && processedNodes.size() >= maxRecordsToProcess)
|
||||
{
|
||||
break;
|
||||
}
|
||||
@@ -430,7 +469,7 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
// process each one
|
||||
for (Long nodeId : nodeIds)
|
||||
{
|
||||
if(maxRecordsToProcess != 0 && processedNodes.size() >= maxRecordsToProcess)
|
||||
if (maxRecordsToProcess != 0 && processedNodes.size() >= maxRecordsToProcess)
|
||||
{
|
||||
break;
|
||||
}
|
||||
@@ -446,29 +485,84 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
out.write(",");
|
||||
out.write(record.toString());
|
||||
out.write("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}, false, // read only
|
||||
true); // requires new
|
||||
true); // requires new
|
||||
}
|
||||
logger.info(MESSAGE_PROCESSING_END);
|
||||
return processedNodes;
|
||||
}
|
||||
|
||||
protected List<NodeRef> processChildrenNodes(NodeRef parentNodeRef, final int batchSize,
|
||||
final Pair<Long, QName> recordAspectPair, final int maxRecordsToProcess, final BufferedWriter out,
|
||||
final boolean attach)
|
||||
{
|
||||
final List<NodeRef> processedNodes = new ArrayList<NodeRef>();
|
||||
final List<FileInfo> children = fileFolderService.search(parentNodeRef, "*", /*filesSearch*/true, /*folderSearch*/true, /*includeSubfolders*/true);
|
||||
logger.info(MESSAGE_PROCESSING_BEGIN);
|
||||
// by batch size
|
||||
for (int i = 0; i < children.size(); i += batchSize)
|
||||
{
|
||||
if (maxRecordsToProcess != 0 && processedNodes.size() >= maxRecordsToProcess)
|
||||
{
|
||||
break;
|
||||
}
|
||||
final int currentIndex = i;
|
||||
|
||||
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>()
|
||||
{
|
||||
public Void execute() throws Throwable
|
||||
{
|
||||
List<FileInfo> nodes = children.subList(currentIndex, Math.min(currentIndex + batchSize, children.size()));
|
||||
// process each one
|
||||
for (FileInfo node : nodes)
|
||||
{
|
||||
if (maxRecordsToProcess != 0 && processedNodes.size() >= maxRecordsToProcess)
|
||||
{
|
||||
break;
|
||||
}
|
||||
NodeRef record = node.getNodeRef();
|
||||
if (nodeService.hasAspect(record, recordAspectPair.getSecond()))
|
||||
{
|
||||
String recordName = (String) nodeService.getProperty(record, ContentModel.PROP_NAME);
|
||||
logger.info(MessageFormat.format(MESSAGE_PROCESSING_RECORD_BEGIN_TEMPLATE, recordName));
|
||||
processNode(record);
|
||||
logger.info(MessageFormat.format(MESSAGE_PROCESSING_RECORD_END_TEMPLATE, recordName));
|
||||
processedNodes.add(record);
|
||||
if (attach)
|
||||
{
|
||||
out.write(recordName);
|
||||
out.write(",");
|
||||
out.write(record.toString());
|
||||
out.write("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}, false, // read only
|
||||
true); // requires new
|
||||
}
|
||||
logger.info(MESSAGE_PROCESSING_END);
|
||||
return processedNodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process each node
|
||||
*
|
||||
* @param nodeRef
|
||||
*/
|
||||
@SuppressWarnings({ "unchecked"})
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
protected void processNode(NodeRef nodeRef)
|
||||
{
|
||||
// get the reader/writer data
|
||||
Map<String, Integer> readers = (Map<String, Integer>)nodeService.getProperty(nodeRef, PROP_READERS);
|
||||
Map<String, Integer> writers = (Map<String, Integer>)nodeService.getProperty(nodeRef, PROP_WRITERS);
|
||||
Map<String, Integer> readers = (Map<String, Integer>) nodeService.getProperty(nodeRef, PROP_READERS);
|
||||
Map<String, Integer> writers = (Map<String, Integer>) nodeService.getProperty(nodeRef, PROP_WRITERS);
|
||||
|
||||
// remove extended security aspect
|
||||
nodeService.removeAspect(nodeRef, ASPECT_EXTENDED_SECURITY);
|
||||
@@ -478,10 +572,20 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
|
||||
permissionService.clearPermission(nodeRef, ExtendedWriterDynamicAuthority.EXTENDED_WRITER);
|
||||
|
||||
// if record then ...
|
||||
if (nodeService.hasAspect(nodeRef, ASPECT_RECORD) && readers != null && writers != null)
|
||||
if (nodeService.hasAspect(nodeRef, ASPECT_RECORD))
|
||||
{
|
||||
Set<String> readersKeySet = null;
|
||||
if (readers != null)
|
||||
{
|
||||
readersKeySet = readers.keySet();
|
||||
}
|
||||
Set<String> writersKeySet = null;
|
||||
if (writers != null)
|
||||
{
|
||||
writersKeySet = writers.keySet();
|
||||
}
|
||||
// re-set extended security via API
|
||||
extendedSecurityService.set(nodeRef, readers.keySet(), writers.keySet());
|
||||
extendedSecurityService.set(nodeRef, readersKeySet, writersKeySet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -23,12 +23,14 @@ import static java.util.Collections.emptyMap;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyBoolean;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -36,6 +38,7 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -59,6 +62,8 @@ import org.alfresco.repo.domain.qname.QNameDAO;
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper;
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
|
||||
import org.alfresco.repo.web.scripts.content.ContentStreamer;
|
||||
import org.alfresco.service.cmr.model.FileFolderService;
|
||||
import org.alfresco.service.cmr.model.FileInfo;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.security.PermissionService;
|
||||
@@ -111,6 +116,8 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
private RetryingTransactionHelper mockedRetryingTransactionHelper;
|
||||
@Mock
|
||||
private ContentStreamer contentStreamer;
|
||||
@Mock
|
||||
private FileFolderService mockedFileFolderService;
|
||||
|
||||
/** test component */
|
||||
@InjectMocks
|
||||
@@ -139,6 +146,7 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
webScript.setNodeService(mockedNodeService);
|
||||
webScript.setPermissionService(mockedPermissionService);
|
||||
webScript.setExtendedSecurityService(mockedExtendedSecurityService);
|
||||
webScript.setFileFolderService(mockedFileFolderService);
|
||||
// setup retrying transaction helper
|
||||
Answer<Object> doInTransactionAnswer = new Answer<Object>()
|
||||
{
|
||||
@@ -165,6 +173,7 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
|
||||
/**
|
||||
* Given that there are no nodes with the extended security aspect When the action is executed Nothing happens
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
@@ -185,7 +194,6 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed 0 records.\"}";
|
||||
assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString));
|
||||
|
||||
|
||||
verify(mockedNodeService, never()).getProperty(any(NodeRef.class), eq(PROP_READERS));
|
||||
verify(mockedNodeService, never()).getProperty(any(NodeRef.class), eq(PROP_WRITERS));
|
||||
verify(mockedNodeService, never()).removeAspect(any(NodeRef.class), eq(ASPECT_EXTENDED_SECURITY));
|
||||
@@ -199,6 +207,7 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
/**
|
||||
* Given that there are records with the extended security aspect When the action is executed Then the aspect is
|
||||
* removed And the dynamic authorities permissions are cleared And extended security is set via the updated API
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -207,8 +216,7 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
{
|
||||
List<Long> ids = Stream.of(1l, 2l, 3l).collect(Collectors.toList());
|
||||
|
||||
when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong()))
|
||||
.thenReturn(ids)
|
||||
when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())).thenReturn(ids)
|
||||
.thenReturn(Collections.emptyList());
|
||||
|
||||
ids.stream().forEach((i) -> {
|
||||
@@ -231,7 +239,6 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
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));
|
||||
@@ -244,8 +251,9 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that there are non-records with the extended security aspect When the web script is executed Then the aspect is
|
||||
* removed And the dynamic authorities permissions are cleared
|
||||
* Given that there are non-records with the extended security aspect When the web script is executed Then the
|
||||
* aspect is removed And the dynamic authorities permissions are cleared
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -254,9 +262,8 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
{
|
||||
List<Long> ids = Stream.of(1l, 2l, 3l).collect(Collectors.toList());
|
||||
|
||||
when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong()))
|
||||
.thenReturn(ids)
|
||||
.thenReturn(Collections.emptyList());
|
||||
when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())).thenReturn(ids)
|
||||
.thenReturn(Collections.emptyList());
|
||||
|
||||
ids.stream().forEach((i) -> {
|
||||
NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService);
|
||||
@@ -278,7 +285,6 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
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));
|
||||
@@ -309,8 +315,8 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
{
|
||||
try
|
||||
{
|
||||
// Set up parameters.
|
||||
Map<String, String> parameters = ImmutableMap.of("batchsize", "dd");
|
||||
// Set up parameters.
|
||||
Map<String, String> parameters = ImmutableMap.of("batchsize", "dd");
|
||||
executeJSONWebScript(parameters);
|
||||
fail("Expected exception as parameter batchsize is invalid.");
|
||||
}
|
||||
@@ -326,8 +332,8 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
{
|
||||
try
|
||||
{
|
||||
// Set up parameters.
|
||||
Map<String, String> parameters = ImmutableMap.of("batchsize", "0");
|
||||
// Set up parameters.
|
||||
Map<String, String> parameters = ImmutableMap.of("batchsize", "0");
|
||||
executeJSONWebScript(parameters);
|
||||
fail("Expected exception as parameter batchsize is not a number greater than 0.");
|
||||
}
|
||||
@@ -355,11 +361,10 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
@Test
|
||||
public void processAllRecordsWhenMaxProcessedRecordsIsZero() throws Exception
|
||||
{
|
||||
List<Long> ids = Stream.of(1l, 2l, 3l,4l).collect(Collectors.toList());
|
||||
List<Long> ids = Stream.of(1l, 2l, 3l, 4l).collect(Collectors.toList());
|
||||
|
||||
when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong()))
|
||||
.thenReturn(ids)
|
||||
.thenReturn(Collections.emptyList());
|
||||
when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())).thenReturn(ids)
|
||||
.thenReturn(Collections.emptyList());
|
||||
|
||||
ids.stream().forEach((i) -> {
|
||||
NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService);
|
||||
@@ -387,9 +392,8 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
{
|
||||
List<Long> ids = Stream.of(1l, 2l, 3l, 4l, 5l).collect(Collectors.toList());
|
||||
|
||||
when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong()))
|
||||
.thenReturn(ids)
|
||||
.thenReturn(Collections.emptyList());
|
||||
when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())).thenReturn(ids)
|
||||
.thenReturn(Collections.emptyList());
|
||||
|
||||
ids.stream().forEach((i) -> {
|
||||
NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService);
|
||||
@@ -412,14 +416,13 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@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)
|
||||
when(mockedPatchDAO.getNodesByAspectQNameId(eq(ASPECT_ID), anyLong(), anyLong())).thenReturn(ids)
|
||||
.thenReturn(Collections.emptyList());
|
||||
|
||||
ids.stream().forEach((i) -> {
|
||||
@@ -439,6 +442,8 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed 3 records.\"}";
|
||||
assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString));
|
||||
ArgumentCaptor<Set> readerKeysCaptor = ArgumentCaptor.forClass(Set.class);
|
||||
ArgumentCaptor<Set> writersKeysCaptor = ArgumentCaptor.forClass(Set.class);
|
||||
|
||||
verify(mockedNodeService, times(3)).getProperty(any(NodeRef.class), eq(PROP_READERS));
|
||||
verify(mockedNodeService, times(3)).getProperty(any(NodeRef.class), eq(PROP_WRITERS));
|
||||
@@ -447,14 +452,76 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
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));
|
||||
verify(mockedExtendedSecurityService, times(3)).set(any(NodeRef.class), readerKeysCaptor.capture(),
|
||||
writersKeysCaptor.capture());
|
||||
List<Set> allReaderKeySets = readerKeysCaptor.getAllValues();
|
||||
List<Set> allWritersKeySets = writersKeysCaptor.getAllValues();
|
||||
for (Set keySet : allReaderKeySets)
|
||||
{
|
||||
assertNull(keySet);
|
||||
}
|
||||
for (Set keySet : allWritersKeySets)
|
||||
{
|
||||
assertNull(keySet);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Test
|
||||
public void recordsWithExtendedSecurityAspectAndNullWriters() 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((Serializable) Collections.emptyMap());
|
||||
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));
|
||||
ArgumentCaptor<Set> readerKeysCaptor = ArgumentCaptor.forClass(Set.class);
|
||||
ArgumentCaptor<Set> writersKeysCaptor = ArgumentCaptor.forClass(Set.class);
|
||||
|
||||
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, times(3)).set(any(NodeRef.class), readerKeysCaptor.capture(),
|
||||
writersKeysCaptor.capture());
|
||||
List<Set> allReaderKeySets = readerKeysCaptor.getAllValues();
|
||||
List<Set> allWritersKeySets = writersKeysCaptor.getAllValues();
|
||||
for (Set keySet : allReaderKeySets)
|
||||
{
|
||||
assertNotNull(keySet);
|
||||
}
|
||||
for (Set keySet : allWritersKeySets)
|
||||
{
|
||||
assertNull(keySet);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Given I have records that require migration
|
||||
* And I am interested in knowning which records are migrated
|
||||
* When I run the migration tool
|
||||
* Then I will be returned a CSV file containing the name and node reference of the record migrated
|
||||
* Given I have records that require migration And I am interested in knowning which records are migrated When I run
|
||||
* the migration tool Then I will be returned a CSV file containing the name and node reference of the record
|
||||
* migrated
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -491,10 +558,9 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
}
|
||||
|
||||
/**
|
||||
* Given that I have record that require migration
|
||||
* And I'm not interested in knowing which records were migrated
|
||||
* When I run the migration tool
|
||||
* And I will not be returned a CSV file of details.
|
||||
* Given that I have record that require migration And I'm not interested in knowing which records were migrated
|
||||
* When I run the migration tool And I will not be returned a CSV file of details.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -527,4 +593,121 @@ public class DynamicAuthoritiesGetUnitTest extends BaseWebScriptUnitTest impleme
|
||||
verify(contentStreamer, never()).streamContent(any(WebScriptRequest.class), any(WebScriptResponse.class),
|
||||
any(File.class), any(Long.class), any(Boolean.class), any(String.class), any(Map.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidParentNodeRefParameter() throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
// Set up parameters.
|
||||
Map<String, String> parameters = ImmutableMap.of("batchsize", "10", "parentNodeRef", "invalidNodeRef");
|
||||
executeJSONWebScript(parameters);
|
||||
fail("Expected exception as parameter parentNodeRef is invalid.");
|
||||
}
|
||||
catch (WebScriptException e)
|
||||
{
|
||||
assertEquals("If parameter parentNodeRef is invalid then 'Internal server error' should be returned.",
|
||||
Status.STATUS_INTERNAL_SERVER_ERROR, e.getStatus());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void inexistentParentNodeRefParameter() throws Exception
|
||||
{
|
||||
try
|
||||
{
|
||||
NodeRef parentNodeRef = AlfMock.generateNodeRef(mockedNodeService);
|
||||
when(mockedNodeService.exists(parentNodeRef)).thenReturn(false);
|
||||
// Set up parameters.
|
||||
Map<String, String> parameters = ImmutableMap.of("batchsize", "10", "parentNodeRef",
|
||||
parentNodeRef.toString());
|
||||
executeJSONWebScript(parameters);
|
||||
fail("Expected exception as parameter parentNodeRef does not exist.");
|
||||
}
|
||||
catch (WebScriptException e)
|
||||
{
|
||||
assertEquals("If parameter parentNodeRef is does not exist then 'Bad Reequest' should be returned.",
|
||||
Status.STATUS_BAD_REQUEST, e.getStatus());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void processedWithParentNodeRef() throws Exception
|
||||
{
|
||||
List<Long> ids = Stream.of(1l, 2l, 3l).collect(Collectors.toList());
|
||||
NodeRef parentNodeRef = AlfMock.generateNodeRef(mockedNodeService);
|
||||
List<FileInfo> children = new ArrayList<FileInfo>();
|
||||
ids.stream().forEach((i) -> {
|
||||
NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService);
|
||||
when(mockedNodeService.hasAspect(nodeRef, ASPECT_RECORD)).thenReturn(true);
|
||||
when(mockedNodeService.hasAspect(nodeRef, ASPECT)).thenReturn(true);
|
||||
when(mockedNodeService.getProperty(nodeRef, PROP_READERS))
|
||||
.thenReturn((Serializable) Collections.emptyMap());
|
||||
when(mockedNodeService.getProperty(nodeRef, PROP_WRITERS))
|
||||
.thenReturn((Serializable) Collections.emptyMap());
|
||||
String name = "name" + i;
|
||||
when(mockedNodeService.getProperty(nodeRef, ContentModel.PROP_NAME)).thenReturn((Serializable) name);
|
||||
FileInfo mockedFileInfo = mock(FileInfo.class);
|
||||
when(mockedFileInfo.getNodeRef()).thenReturn(nodeRef);
|
||||
children.add(mockedFileInfo);
|
||||
});
|
||||
when(mockedFileFolderService.search(eq(parentNodeRef), eq("*"), eq(true), eq(true), eq(true)))
|
||||
.thenReturn(children);
|
||||
|
||||
Map<String, String> parameters = ImmutableMap.of("batchsize", "3", "maxProcessedRecords", "4", "export",
|
||||
"false", "parentNodeRef", parentNodeRef.toString());
|
||||
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(contentStreamer, never()).streamContent(any(WebScriptRequest.class), any(WebScriptResponse.class),
|
||||
any(File.class), any(Long.class), any(Boolean.class), any(String.class), any(Map.class));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void processedWithParentNodeRefWithFirstTwoBatchesAlreadyProcessed() throws Exception
|
||||
{
|
||||
List<Long> ids = Stream.of(1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l).collect(Collectors.toList());
|
||||
NodeRef parentNodeRef = AlfMock.generateNodeRef(mockedNodeService);
|
||||
List<FileInfo> children = new ArrayList<FileInfo>();
|
||||
ids.stream().forEach((i) -> {
|
||||
NodeRef nodeRef = AlfMock.generateNodeRef(mockedNodeService);
|
||||
when(mockedNodeService.hasAspect(nodeRef, ASPECT_RECORD)).thenReturn(true);
|
||||
if (i <= 6l)
|
||||
{
|
||||
when(mockedNodeService.hasAspect(nodeRef, ASPECT)).thenReturn(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
when(mockedNodeService.hasAspect(nodeRef, ASPECT)).thenReturn(true);
|
||||
}
|
||||
when(mockedNodeService.getProperty(nodeRef, PROP_READERS))
|
||||
.thenReturn((Serializable) Collections.emptyMap());
|
||||
when(mockedNodeService.getProperty(nodeRef, PROP_WRITERS))
|
||||
.thenReturn((Serializable) Collections.emptyMap());
|
||||
String name = "name" + i;
|
||||
when(mockedNodeService.getProperty(nodeRef, ContentModel.PROP_NAME)).thenReturn((Serializable) name);
|
||||
FileInfo mockedFileInfo = mock(FileInfo.class);
|
||||
when(mockedFileInfo.getNodeRef()).thenReturn(nodeRef);
|
||||
children.add(mockedFileInfo);
|
||||
});
|
||||
when(mockedFileFolderService.search(eq(parentNodeRef), eq("*"), eq(true), eq(true), eq(true)))
|
||||
.thenReturn(children);
|
||||
|
||||
Map<String, String> parameters = ImmutableMap.of("batchsize", "3", "parentNodeRef", parentNodeRef.toString());
|
||||
JSONObject json = executeJSONWebScript(parameters);
|
||||
assertNotNull(json);
|
||||
String actualJSONString = json.toString();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String expectedJSONString = "{\"responsestatus\":\"success\",\"message\":\"Processed 2 records.\"}";
|
||||
assertEquals(mapper.readTree(expectedJSONString), mapper.readTree(actualJSONString));
|
||||
|
||||
verify(contentStreamer, never()).streamContent(any(WebScriptRequest.class), any(WebScriptResponse.class),
|
||||
any(File.class), any(Long.class), any(Boolean.class), any(String.class), any(Map.class));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user