Test for MNT-16748: Failing to extract auditing entries from SearchService

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@130631 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2016-09-14 09:59:18 +00:00
parent 4a1cd62d4d
commit aad9baaf63
2 changed files with 143 additions and 38 deletions

View File

@@ -25,34 +25,36 @@
*/ */
package org.alfresco.repo.audit; package org.alfresco.repo.audit;
import java.io.Serializable; import java.io.Serializable;
import java.net.URL; import java.net.URL;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.audit.model.AuditModelRegistryImpl; import org.alfresco.repo.audit.model.AuditModelRegistryImpl;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.repo.transaction.TransactionServiceImpl; import org.alfresco.repo.transaction.TransactionServiceImpl;
import org.alfresco.service.ServiceRegistry; import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.audit.AuditQueryParameters; import org.alfresco.service.cmr.audit.AuditQueryParameters;
import org.alfresco.service.cmr.audit.AuditService; import org.alfresco.service.cmr.audit.AuditService;
import org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback; import org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback;
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.repository.StoreRef; import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.namespace.QName; import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.transaction.TransactionService; import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.util.ApplicationContextHelper; import org.alfresco.service.namespace.QName;
import org.apache.commons.lang.mutable.MutableInt; import org.alfresco.service.transaction.TransactionService;
import org.apache.commons.logging.Log; import org.alfresco.util.ApplicationContextHelper;
import org.apache.commons.logging.LogFactory; import org.apache.commons.lang.mutable.MutableInt;
import org.springframework.context.ApplicationContext; import org.apache.commons.logging.Log;
import org.springframework.util.ResourceUtils; import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.util.ResourceUtils;
/** /**
* Tests AuditMethodInterceptor * Tests AuditMethodInterceptor
@@ -67,7 +69,8 @@ public class AuditMethodInterceptorTest extends TestCase
private AuditModelRegistryImpl auditModelRegistry; private AuditModelRegistryImpl auditModelRegistry;
private TransactionServiceImpl transactionServiceImpl; private TransactionServiceImpl transactionServiceImpl;
private NodeService nodeService; private NodeService nodeService;
private SearchService searchService;
private ServiceRegistry serviceRegistry; private ServiceRegistry serviceRegistry;
private AuditComponent auditComponent; private AuditComponent auditComponent;
private AuditService auditService; private AuditService auditService;
@@ -75,9 +78,11 @@ public class AuditMethodInterceptorTest extends TestCase
private NodeRef nodeRef; private NodeRef nodeRef;
private static String APPLICATION_NAME = "alfresco-mnt-11072"; private static String APPLICATION_NAME_MNT_11072 = "alfresco-mnt-11072";
private static String APPLICATION_NAME_MNT_16748 = "alfresco-mnt-16748";
private static final Log logger = LogFactory.getLog(AuditMethodInterceptorTest.class); private static final Log logger = LogFactory.getLog(AuditMethodInterceptorTest.class);
@SuppressWarnings("deprecation")
@Override @Override
public void setUp() throws Exception public void setUp() throws Exception
{ {
@@ -88,20 +93,24 @@ public class AuditMethodInterceptorTest extends TestCase
transactionService = serviceRegistry.getTransactionService(); transactionService = serviceRegistry.getTransactionService();
transactionServiceImpl = (TransactionServiceImpl) ctx.getBean("transactionService"); transactionServiceImpl = (TransactionServiceImpl) ctx.getBean("transactionService");
nodeService = serviceRegistry.getNodeService(); nodeService = serviceRegistry.getNodeService();
searchService = serviceRegistry.getSearchService();
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName()); AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
nodeRef = nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE); nodeRef = nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
// Register the model // Register the models
URL modelUrl = ResourceUtils.getURL("classpath:alfresco/testaudit/alfresco-audit-test-mnt-11072.xml"); URL modelUrlMnt11072 = ResourceUtils.getURL("classpath:alfresco/testaudit/alfresco-audit-test-mnt-11072.xml");
auditModelRegistry.registerModel(modelUrl); URL modelUrlMnt16748 = ResourceUtils.getURL("classpath:alfresco/testaudit/alfresco-audit-test-mnt-16748.xml");
auditModelRegistry.registerModel(modelUrlMnt11072);
auditModelRegistry.registerModel(modelUrlMnt16748);
auditModelRegistry.loadAuditModels(); auditModelRegistry.loadAuditModels();
} }
@Override @Override
public void tearDown() public void tearDown()
{ {
auditService.clearAudit(APPLICATION_NAME, null, null); auditService.clearAudit(APPLICATION_NAME_MNT_11072, null, null);
auditService.clearAudit(APPLICATION_NAME_MNT_16748, null, null);
auditModelRegistry.destroy(); auditModelRegistry.destroy();
AuthenticationUtil.clearCurrentSecurityContext(); AuthenticationUtil.clearCurrentSecurityContext();
} }
@@ -109,10 +118,8 @@ public class AuditMethodInterceptorTest extends TestCase
/** /**
* Test for <a href="https://issues.alfresco.com/jira/browse/MNT-11072">MNT-11072</a> <br> * Test for <a href="https://issues.alfresco.com/jira/browse/MNT-11072">MNT-11072</a> <br>
* Use NodeService, as it is wrapped by the AuditMethodInterceptor, to get node props in read-only server mode. * Use NodeService, as it is wrapped by the AuditMethodInterceptor, to get node props in read-only server mode.
*
* @throws Exception
*/ */
public void testAuditInReadOnly() throws Exception public void testAuditInReadOnly_MNT11072() throws Exception
{ {
// Run as admin // Run as admin
AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser(); AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();
@@ -173,7 +180,7 @@ public class AuditMethodInterceptorTest extends TestCase
AuditQueryParameters params = new AuditQueryParameters(); AuditQueryParameters params = new AuditQueryParameters();
params.setForward(true); params.setForward(true);
params.setUser(AuthenticationUtil.getAdminUserName()); params.setUser(AuthenticationUtil.getAdminUserName());
params.setApplicationName(APPLICATION_NAME); params.setApplicationName(APPLICATION_NAME_MNT_11072);
rowCount.setValue(0); rowCount.setValue(0);
auditComponent.auditQuery(callback, params, Integer.MAX_VALUE); auditComponent.auditQuery(callback, params, Integer.MAX_VALUE);
@@ -191,4 +198,76 @@ public class AuditMethodInterceptorTest extends TestCase
transactionServiceImpl.setAllowWrite(true, veto); transactionServiceImpl.setAllowWrite(true, veto);
} }
} }
/**
* Test for <a href="https://issues.alfresco.com/jira/browse/MNT-16748">MNT-16748</a> <br>
* Use {@link SearchService} to perform a query.
*/
public void testAuditSearchService_MNT16748() throws Exception
{
// Run as admin
AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();
// Perform a search
@SuppressWarnings("unused")
ResultSet rs = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<ResultSet>()
{
@Override
public ResultSet execute() throws Throwable
{
return searchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_XPATH, "/");
}
}, true, false);
// Search for audit
final StringBuilder sb = new StringBuilder();
final MutableInt rowCount = new MutableInt();
AuditQueryCallback callback = new AuditQueryCallback()
{
@Override
public boolean valuesRequired()
{
return true;
}
@Override
public boolean handleAuditEntry(Long entryId, String applicationName,
String user, long time, Map<String, Serializable> values)
{
assertNotNull(applicationName);
assertNotNull(user);
sb.append("Row: ").append(entryId).append(" | ")
.append(applicationName).append(" | ")
.append(user).append(" | ")
.append(new Date(time)).append(" | ")
.append(values).append(" | ").append("\n");
rowCount.setValue(rowCount.intValue() + 1);
return true;
}
@Override
public boolean handleAuditEntryError(Long entryId, String errorMsg, Throwable error)
{
throw new AlfrescoRuntimeException(errorMsg, error);
}
};
AuditQueryParameters params = new AuditQueryParameters();
params.setForward(true);
params.setUser(AuthenticationUtil.getAdminUserName());
params.setApplicationName(APPLICATION_NAME_MNT_16748);
rowCount.setValue(0);
auditComponent.auditQuery(callback, params, Integer.MAX_VALUE);
assertEquals("There should be one audit entry.", 1, rowCount.intValue());
assertTrue("The requested language should be in the audit entry.",
sb.toString().contains(SearchService.LANGUAGE_XPATH));
if (logger.isDebugEnabled())
{
logger.debug(sb.toString());
}
}
} }

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<Audit xmlns="http://www.alfresco.org/repo/audit/model/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.alfresco.org/repo/audit/model/3.2 alfresco-audit-3.2.xsd">
<DataExtractors>
<DataExtractor name="simpleValue" registeredName="auditModel.extractor.simpleValue" />
<DataExtractor name="nullValue" registeredName="auditModel.extractor.nullValue" />
</DataExtractors>
<PathMappings>
<PathMap source="/alfresco-mnt-16748" target="/alfresco-mnt-16748" />
<PathMap source="/alfresco-api/pre/SearchService/query/args/language"
target="/alfresco-mnt-16748/language" />
</PathMappings>
<Application name="alfresco-mnt-16748" key="alfresco-mnt-16748">
<AuditPath key="language">
<RecordValue key="language" dataExtractor="simpleValue"
dataSource="/alfresco-mnt-16748/language" dataTrigger="/alfresco-mnt-16748/language" />
</AuditPath>
</Application>
</Audit>