diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceImplUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceImplUnitTest.java index 5f2555b759..550a415603 100644 --- a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceImplUnitTest.java +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/ClassificationServiceImplUnitTest.java @@ -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() - { - @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).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); diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/BaseUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/BaseUnitTest.java index aa550b3673..e613e5e6c7 100644 --- a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/BaseUnitTest.java +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/BaseUnitTest.java @@ -170,7 +170,7 @@ public class BaseUnitTest implements RecordsManagementModel, ContentModel doAnswer(doInTransactionAnswer).when(mockedRetryingTransactionHelper).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() - { - @SuppressWarnings("rawtypes") - @Override - public Object answer(InvocationOnMock invocation) throws Throwable - { - RunAsWork work = (RunAsWork)invocation.getArguments()[0]; - return work.doWork(); - } - - }).when(mockedAuthenticationUtil).runAsSystem(any(RunAsWork.class)); - - // just do the work - doAnswer(new Answer() - { - @SuppressWarnings("rawtypes") - @Override - public Object answer(InvocationOnMock invocation) throws Throwable - { - RunAsWork work = (RunAsWork)invocation.getArguments()[0]; - return work.doWork(); - } - - }).when(mockedAuthenticationUtil).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 diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/MockAuthenticationUtilHelper.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/MockAuthenticationUtilHelper.java new file mode 100644 index 0000000000..d527117b09 --- /dev/null +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/test/util/MockAuthenticationUtilHelper.java @@ -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 . + */ + +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 AuthenticationUtil 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 AuthenticationUtil 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". + *

+ * 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() + { + @SuppressWarnings("rawtypes") + @Override + public Object answer(InvocationOnMock invocation) throws Throwable + { + RunAsWork work = (RunAsWork) invocation.getArguments()[0]; + return work.doWork(); + } + + }).when(mockAuthenticationUtil). runAsSystem(any(RunAsWork.class)); + + // just do the work + doAnswer(new Answer() + { + @SuppressWarnings("rawtypes") + @Override + public Object answer(InvocationOnMock invocation) throws Throwable + { + RunAsWork work = (RunAsWork) invocation.getArguments()[0]; + return work.doWork(); + } + + }).when(mockAuthenticationUtil). runAs(any(RunAsWork.class), anyString()); + + // assume admin + doReturn("admin").when(mockAuthenticationUtil).getAdminUserName(); + doReturn("admin").when(mockAuthenticationUtil).getFullyAuthenticatedUser(); + } +}