From 7def9837adf3080deecee4e46cc9c617f430dcc7 Mon Sep 17 00:00:00 2001 From: Roxana Lucanu Date: Tue, 31 Mar 2020 10:59:21 +0300 Subject: [PATCH 1/3] RM-7145 Fix for hasMoreItems --- .../capability/RMAfterInvocationProvider.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProvider.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProvider.java index d9659e312e..f876e1954e 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProvider.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProvider.java @@ -563,6 +563,13 @@ public class RMAfterInvocationProvider extends RMSecurityCommon } } + if (maxSize != null) + { + LimitBy limitBy = returnedObject.length() > maxSize ? LimitBy.FINAL_SIZE : LimitBy.UNLIMITED; + filteringResultSet.setResultSetMetaData(new SimpleResultSetMetaData(limitBy, + PermissionEvaluationMode.EAGER, returnedObject.getResultSetMetaData().getSearchParameters())); + } + filteringResultSet.setNumberFound(returnedObject.getNumberFound()); return filteringResultSet; From 94b209e0ea8899678ab3d2dc607ce1b036dd810e Mon Sep 17 00:00:00 2001 From: Tom Page Date: Wed, 1 Apr 2020 15:28:40 +0100 Subject: [PATCH 2/3] RM-7145 Unit tests for processing ResultSet with RMAfterInvocationProviderUnitTest. --- .../capability/RMAfterInvocationProvider.java | 6 +- .../RMAfterInvocationProviderUnitTest.java | 178 ++++++++++++++++++ 2 files changed, 181 insertions(+), 3 deletions(-) create mode 100644 rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProviderUnitTest.java diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProvider.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProvider.java index f876e1954e..e651ba7bb8 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProvider.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProvider.java @@ -39,10 +39,10 @@ import java.util.StringTokenizer; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel; +import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; import org.alfresco.repo.search.SimpleResultSetMetaData; import org.alfresco.repo.search.impl.lucene.PagingLuceneResultSet; import org.alfresco.repo.search.impl.querymodel.QueryEngineResults; -import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.permissions.PermissionCheckCollection; import org.alfresco.repo.security.permissions.PermissionCheckValue; import org.alfresco.repo.security.permissions.PermissionCheckedCollection.PermissionCheckedCollectionMixin; @@ -80,8 +80,8 @@ public class RMAfterInvocationProvider extends RMSecurityCommon private static final String AFTER_RM = "AFTER_RM"; + private AuthenticationUtil authenticationUtil; private int maxPermissionChecks; - private long maxPermissionCheckTimeMillis; public boolean supports(ConfigAttribute configAttribute) @@ -147,7 +147,7 @@ public class RMAfterInvocationProvider extends RMSecurityCommon } try { - if (AuthenticationUtil.isRunAsUserTheSystemUser()) + if (authenticationUtil.isRunAsUserTheSystemUser()) { if (logger.isDebugEnabled()) { diff --git a/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProviderUnitTest.java b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProviderUnitTest.java new file mode 100644 index 0000000000..36dc2efeb2 --- /dev/null +++ b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProviderUnitTest.java @@ -0,0 +1,178 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2020 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ + +package org.alfresco.module.org_alfresco_module_rm.capability; + +import static java.util.Arrays.asList; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +import java.util.List; + +import net.sf.acegisecurity.Authentication; +import net.sf.acegisecurity.ConfigAttribute; +import net.sf.acegisecurity.ConfigAttributeDefinition; +import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; +import org.alfresco.repo.security.permissions.impl.acegi.FilteringResultSet; +import org.alfresco.service.cmr.repository.ChildAssociationRef; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.cmr.search.LimitBy; +import org.alfresco.service.cmr.search.ResultSet; +import org.alfresco.service.cmr.search.ResultSetMetaData; +import org.alfresco.service.cmr.search.SearchParameters; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; + +/** Unit tests for {@link RMAfterInvocationProvider}. */ +public class RMAfterInvocationProviderUnitTest +{ + private static final NodeRef NODE_A = new NodeRef("test://node/a"); + + /** The class under test. */ + @InjectMocks + private RMAfterInvocationProvider rmAfterInvocationProvider; + @Mock + private Authentication authentication; + @Mock + Object object; + @Mock + ConfigAttributeDefinition config; + @Mock + AuthenticationUtil authenticationUtil; + @Mock + NodeService nodeService; + @Mock + ChildAssociationRef childAssocRefA; + + /** Set up the mocks and common test data. */ + @Before + public void setUp() + { + initMocks(this); + + // Set up the nodes and associations. + when(nodeService.exists(NODE_A)).thenReturn(true); + when(childAssocRefA.getParentRef()).thenReturn(NODE_A); + + // Create the config object for use by the tests. + ConfigAttribute configAttribute = mock(ConfigAttribute.class); + when(configAttribute.getAttribute()).thenReturn("AFTER_RM.test"); + List configAttributes = asList(configAttribute); + when(config.getConfigAttributes()).thenReturn(configAttributes.iterator()); + } + + /** Check that when all the results fit into a page then we get a response of "UNLIMITED". */ + @Test + public void testDecide_resultSet_unlimited() + { + // The returned object is a search result set. + ResultSet returnedObject = mock(ResultSet.class); + ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); + when(returnedObject.getResultSetMetaData()).thenReturn(resultSetMetaData); + + // Simulate a single result, and the user has access to it. + when(returnedObject.length()).thenReturn(1); + when(returnedObject.getNumberFound()).thenReturn(1L); + when(returnedObject.getNodeRef(0)).thenReturn(NODE_A); + when(returnedObject.getChildAssocRef(0)).thenReturn(childAssocRefA); + + // Set the page size to 1 and skip count to 0. + SearchParameters searchParameters = mock(SearchParameters.class); + when(searchParameters.getMaxItems()).thenReturn(1); + when(searchParameters.getSkipCount()).thenReturn(0); + when(resultSetMetaData.getSearchParameters()).thenReturn(searchParameters); + + // Call the method under test. + FilteringResultSet filteringResultSet = (FilteringResultSet) rmAfterInvocationProvider.decide(authentication, object, config, returnedObject); + + assertEquals("Expected total of one result.", 1, filteringResultSet.getNumberFound()); + assertEquals("Expected one result returned.", 1, filteringResultSet.length()); + assertEquals("Expected that results were not limited by the page size.", LimitBy.UNLIMITED, filteringResultSet.getResultSetMetaData().getLimitedBy()); + } + + /** Check that results can skipped due to the skip count. */ + @Test + public void testDecide_resultSet_skipped() + { + // The returned object is a search result set. + ResultSet returnedObject = mock(ResultSet.class); + ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); + when(returnedObject.getResultSetMetaData()).thenReturn(resultSetMetaData); + + // Simulate a single result that was skipped due to the skip count. + when(returnedObject.length()).thenReturn(0); + when(returnedObject.getNumberFound()).thenReturn(1L); + + // Set the page size to 1 and skip count to 1 (so the result is skipped). + SearchParameters searchParameters = mock(SearchParameters.class); + when(searchParameters.getMaxItems()).thenReturn(1); + when(searchParameters.getSkipCount()).thenReturn(1); + when(resultSetMetaData.getSearchParameters()).thenReturn(searchParameters); + + // Call the method under test. + FilteringResultSet filteringResultSet = (FilteringResultSet) rmAfterInvocationProvider.decide(authentication, object, config, returnedObject); + + assertEquals("Expected total of one result.", 1, filteringResultSet.getNumberFound()); + assertEquals("Expected no results returned.", 0, filteringResultSet.length()); + assertEquals("Expected that results were not limited by the page size.", LimitBy.UNLIMITED, filteringResultSet.getResultSetMetaData().getLimitedBy()); + } + + /** Check that results can be limited by the page size. */ + @Test + public void testDecide_resultSet_pageSize() + { + // The returned object is a search result set. + ResultSet returnedObject = mock(ResultSet.class); + ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); + when(returnedObject.getResultSetMetaData()).thenReturn(resultSetMetaData); + + // Simulate a single result, and the user has access to it. + when(returnedObject.length()).thenReturn(1); + when(returnedObject.getNumberFound()).thenReturn(1L); + when(returnedObject.getNodeRef(0)).thenReturn(NODE_A); + when(returnedObject.getChildAssocRef(0)).thenReturn(childAssocRefA); + + // Set the page size to 0 and skip count to 0 (so the result is not in page). + SearchParameters searchParameters = mock(SearchParameters.class); + when(searchParameters.getMaxItems()).thenReturn(0); + when(searchParameters.getSkipCount()).thenReturn(0); + when(resultSetMetaData.getSearchParameters()).thenReturn(searchParameters); + + // Call the method under test. + FilteringResultSet filteringResultSet = (FilteringResultSet) rmAfterInvocationProvider.decide(authentication, object, config, returnedObject); + + assertEquals("Expected total of one result.", 1, filteringResultSet.getNumberFound()); + assertEquals("Expected no results returned.", 0, filteringResultSet.length()); + assertEquals("Expected that results were limited by page size.", LimitBy.FINAL_SIZE, filteringResultSet.getResultSetMetaData().getLimitedBy()); + } +} From 98a1485baa8eaf0796e81ea3b400c210d00ce5f4 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Wed, 1 Apr 2020 15:34:25 +0100 Subject: [PATCH 3/3] RM-7145 Wire in RM AuthenticationUtil. --- .../rm-public-services-security-context.xml | 1 + .../capability/RMAfterInvocationProvider.java | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/rm-public-services-security-context.xml b/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/rm-public-services-security-context.xml index 6d90759e2b..c9856faeb8 100644 --- a/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/rm-public-services-security-context.xml +++ b/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/rm-public-services-security-context.xml @@ -156,6 +156,7 @@ ${system.acl.maxPermissionChecks} + diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProvider.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProvider.java index e651ba7bb8..c6ee235ae4 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProvider.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/capability/RMAfterInvocationProvider.java @@ -130,6 +130,16 @@ public class RMAfterInvocationProvider extends RMSecurityCommon this.maxPermissionCheckTimeMillis = maxPermissionCheckTimeMillis; } + /** + * Sets the authentication util + * + * @param authenticationUtil The authentication util to set + */ + public void setAuthenticationUtil(AuthenticationUtil authenticationUtil) + { + this.authenticationUtil = authenticationUtil; + } + @SuppressWarnings("rawtypes") public Object decide(Authentication authentication, Object object, ConfigAttributeDefinition config, Object returnedObject) {