RM-2027 Refactor mock authentication code.

Create a new class to handle creation of the mock authentication util.
It would be nice to always use it as a factory to create a mock util, but
unfortunately this causes many of the existing unit tests to fail. Something
clever is happening in org.mockito.MockitoAnnotations.initMocks(Object) that
I currently don't understand.

+review RM

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@100292 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-03-27 11:30:20 +00:00
parent bb20d042cf
commit 928d9b3190
3 changed files with 107 additions and 66 deletions

View File

@@ -19,30 +19,27 @@
package org.alfresco.module.org_alfresco_module_rm.classification; package org.alfresco.module.org_alfresco_module_rm.classification;
import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.MissingConfiguration; import org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceException.MissingConfiguration;
import org.alfresco.module.org_alfresco_module_rm.test.util.MockAuthenticationUtilHelper;
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil; import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
import org.alfresco.service.cmr.attributes.AttributeService; import org.alfresco.service.cmr.attributes.AttributeService;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.mockito.Mockito.any; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset; import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.junit.Assert.*; import static org.junit.Assert.fail;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
/** /**
@@ -91,31 +88,13 @@ public class ClassificationServiceImplUnitTest
private ClassificationServiceImpl classificationService; private ClassificationServiceImpl classificationService;
private AttributeService mockedAttributeService = mock(AttributeService.class); private AttributeService mockedAttributeService = mock(AttributeService.class);
private AuthenticationUtil mockedAuthenticationUtil = mock(AuthenticationUtil.class); private AuthenticationUtil mockedAuthenticationUtil;
private Configuration mockConfig = mock(Configuration.class); private Configuration mockConfig = mock(Configuration.class);
@Before public void setUp() @Before public void setUp()
{ {
reset(mockConfig, mockedAttributeService); reset(mockConfig, mockedAttributeService);
mockedAuthenticationUtil = MockAuthenticationUtilHelper.create();
// FIXME This should be out of here (and BaseUnitTest) and into a common utility class.
// We don't care about authentication here.
doAnswer(new Answer<Object>()
{
@SuppressWarnings("rawtypes")
@Override
public Object answer(InvocationOnMock invocation) throws Throwable
{
org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork work
= (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)invocation.getArguments()[0];
return work.doWork();
}
}).when(mockedAuthenticationUtil).<Object>runAs(any(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork.class), anyString());
// Use the admin user
doReturn("admin").when(mockedAuthenticationUtil).getAdminUserName();
doReturn("admin").when(mockedAuthenticationUtil).getFullyAuthenticatedUser();
classificationService = new ClassificationServiceImpl(mockConfig); classificationService = new ClassificationServiceImpl(mockConfig);
classificationService.setAttributeService(mockedAttributeService); classificationService.setAttributeService(mockedAttributeService);

View File

@@ -170,7 +170,7 @@ public class BaseUnitTest implements RecordsManagementModel, ContentModel
doAnswer(doInTransactionAnswer).when(mockedRetryingTransactionHelper).<Object>doInTransaction(any(RetryingTransactionCallback.class)); doAnswer(doInTransactionAnswer).when(mockedRetryingTransactionHelper).<Object>doInTransaction(any(RetryingTransactionCallback.class));
// setup mocked authentication util // setup mocked authentication util
setupAuthenticationUtilMock(); MockAuthenticationUtilHelper.setup(mockedAuthenticationUtil);
// setup file plan // setup file plan
filePlan = generateNodeRef(TYPE_FILE_PLAN); filePlan = generateNodeRef(TYPE_FILE_PLAN);
@@ -198,43 +198,6 @@ public class BaseUnitTest implements RecordsManagementModel, ContentModel
doReturn(Collections.singletonList(record)).when(mockedRecordService).getRecords(recordFolder); doReturn(Collections.singletonList(record)).when(mockedRecordService).getRecords(recordFolder);
} }
/**
* Setup authentication util mock
*/
@SuppressWarnings("unchecked")
private void setupAuthenticationUtilMock()
{
// just do the work
doAnswer(new Answer<Object>()
{
@SuppressWarnings("rawtypes")
@Override
public Object answer(InvocationOnMock invocation) throws Throwable
{
RunAsWork work = (RunAsWork)invocation.getArguments()[0];
return work.doWork();
}
}).when(mockedAuthenticationUtil).<Object>runAsSystem(any(RunAsWork.class));
// just do the work
doAnswer(new Answer<Object>()
{
@SuppressWarnings("rawtypes")
@Override
public Object answer(InvocationOnMock invocation) throws Throwable
{
RunAsWork work = (RunAsWork)invocation.getArguments()[0];
return work.doWork();
}
}).when(mockedAuthenticationUtil).<Object>runAs(any(RunAsWork.class), anyString());
// assume admin
doReturn("admin").when(mockedAuthenticationUtil).getAdminUserName();
doReturn("admin").when(mockedAuthenticationUtil).getFullyAuthenticatedUser();
}
/** /**
* Helper to generate random text value suitable for a property * Helper to generate random text value suitable for a property
* value or node name * value or node name

View File

@@ -0,0 +1,99 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.test.util;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
/**
* A helper to create or initialise mock {@link AuthenticationUtil}s.
*
* @author tpage
*/
public class MockAuthenticationUtilHelper
{
/**
* Create a Mockito mock <code>AuthenticationUtil</code> that executes all methods assuming the user has permission.
* If the mock is asked for details about the user then it assumes the user is "admin".
*
* @return The new mock.
*/
public static AuthenticationUtil create()
{
AuthenticationUtil mockAuthenticationUtil = mock(AuthenticationUtil.class);
setup(mockAuthenticationUtil);
return mockAuthenticationUtil;
}
/**
* Set up a Mockito mock <code>AuthenticationUtil</code> so that it executes all methods assuming the user has
* permissions. If the mock is asked for details about the user then it assumes the user is "admin".
* <p>
* TODO: Change this method to private and this class to be a factory.
*
* @param mockAuthenticationUtil The mock to initialise.
*/
@SuppressWarnings("unchecked")
protected static void setup(AuthenticationUtil mockAuthenticationUtil)
{
reset(mockAuthenticationUtil);
// just do the work
doAnswer(new Answer<Object>()
{
@SuppressWarnings("rawtypes")
@Override
public Object answer(InvocationOnMock invocation) throws Throwable
{
RunAsWork work = (RunAsWork) invocation.getArguments()[0];
return work.doWork();
}
}).when(mockAuthenticationUtil).<Object> runAsSystem(any(RunAsWork.class));
// just do the work
doAnswer(new Answer<Object>()
{
@SuppressWarnings("rawtypes")
@Override
public Object answer(InvocationOnMock invocation) throws Throwable
{
RunAsWork work = (RunAsWork) invocation.getArguments()[0];
return work.doWork();
}
}).when(mockAuthenticationUtil).<Object> runAs(any(RunAsWork.class), anyString());
// assume admin
doReturn("admin").when(mockAuthenticationUtil).getAdminUserName();
doReturn("admin").when(mockAuthenticationUtil).getFullyAuthenticatedUser();
}
}