mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
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:
@@ -19,30 +19,27 @@
|
||||
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.test.util.MockAuthenticationUtilHelper;
|
||||
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
|
||||
import org.alfresco.service.cmr.attributes.AttributeService;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.fail;
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
/**
|
||||
@@ -91,31 +88,13 @@ public class ClassificationServiceImplUnitTest
|
||||
private ClassificationServiceImpl classificationService;
|
||||
|
||||
private AttributeService mockedAttributeService = mock(AttributeService.class);
|
||||
private AuthenticationUtil mockedAuthenticationUtil = mock(AuthenticationUtil.class);
|
||||
private AuthenticationUtil mockedAuthenticationUtil;
|
||||
private Configuration mockConfig = mock(Configuration.class);
|
||||
|
||||
@Before public void setUp()
|
||||
{
|
||||
reset(mockConfig, mockedAttributeService);
|
||||
|
||||
// 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();
|
||||
mockedAuthenticationUtil = MockAuthenticationUtilHelper.create();
|
||||
|
||||
classificationService = new ClassificationServiceImpl(mockConfig);
|
||||
classificationService.setAttributeService(mockedAttributeService);
|
||||
|
@@ -170,7 +170,7 @@ public class BaseUnitTest implements RecordsManagementModel, ContentModel
|
||||
doAnswer(doInTransactionAnswer).when(mockedRetryingTransactionHelper).<Object>doInTransaction(any(RetryingTransactionCallback.class));
|
||||
|
||||
// setup mocked authentication util
|
||||
setupAuthenticationUtilMock();
|
||||
MockAuthenticationUtilHelper.setup(mockedAuthenticationUtil);
|
||||
|
||||
// setup 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* value or node name
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user