diff --git a/accesscontroltool-startuphook-bundle/src/test/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImplTest.java b/accesscontroltool-startuphook-bundle/src/test/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImplTest.java index 2318d2e1..5d4a22ed 100644 --- a/accesscontroltool-startuphook-bundle/src/test/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImplTest.java +++ b/accesscontroltool-startuphook-bundle/src/test/java/biz/netcentric/cq/tools/actool/startuphook/impl/AcToolStartupHookServiceImplTest.java @@ -16,11 +16,8 @@ import biz.netcentric.cq.tools.actool.api.AcInstallationService; import biz.netcentric.cq.tools.actool.helper.runtime.RuntimeHelper; import org.apache.sling.jcr.api.SlingRepository; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; @@ -36,7 +33,6 @@ import javax.jcr.RepositoryException; import javax.jcr.Session; -import static org.apache.sling.api.resource.runtime.dto.AuthType.no; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) @@ -66,53 +62,81 @@ class AcToolStartupHookServiceImplTest { @Spy AcInstallationService installationService; - @ParameterizedTest - @CsvSource({ - "false, false, true", - "false, true, false", - "true, false, true", - "true, true, false" - }) - void testActivationSync(boolean runAsync, boolean canReadApps, boolean pathsForInstallationEmpty) throws RepositoryException, InterruptedException { - setup(canReadApps); + @Test + void testActivationWithModifiableRoot() throws RepositoryException { + boolean canSetPropertiesOnRootNode = true, runAsync = false, cloudOnly = false; + setup(canSetPropertiesOnRootNode, runAsync, cloudOnly); try (MockedStatic mockedFrameworkUtil = mockStatic(FrameworkUtil.class)) { - createAndActivateStartupHookService(mockedFrameworkUtil, runAsync); - // wait for the thread in AcToolStartupHookServiceImpl#runAcToolAsync to get started - if (runAsync && canReadApps) { - Thread.sleep(1000L); - } - verify(installationService, times(1)).apply(null, pathsForInstallationEmpty ? new String[]{} : new String[]{ "^/$", "^$" }, true); + createAndActivateStartupHookService(mockedFrameworkUtil); + verify(installationService, times(1)).apply(null, new String[]{"^/$", "^$"}, true); } } - void setup(boolean canReadApps) throws RepositoryException { + @Test + void testActivationWithUnmodifiableRoot() throws RepositoryException { + boolean canSetPropertiesOnRootNode = false, runAsync = false, cloudOnly = false; + setup(canSetPropertiesOnRootNode, runAsync, cloudOnly); + try (MockedStatic mockedFrameworkUtil = mockStatic(FrameworkUtil.class)) { + createAndActivateStartupHookService(mockedFrameworkUtil); + verify(installationService, times(1)).apply(null, new String[]{}, true); + } + } + + @Test + void testActivationWithCloudOnly() throws RepositoryException { + boolean canSetPropertiesOnRootNode = false, runAsync = false, cloudOnly = true; + setup(canSetPropertiesOnRootNode, runAsync, cloudOnly); + try (MockedStatic mockedFrameworkUtil = mockStatic(FrameworkUtil.class)) { + createAndActivateStartupHookService(mockedFrameworkUtil); + verify(installationService, never()).apply(null, new String[]{"^/$", "^$"}, true); + } + } + + @Test + void testActivationWithAsync() throws RepositoryException, InterruptedException { + boolean canSetPropertiesOnRootNode = true, runAsync = true, cloudOnly = false; + setup(canSetPropertiesOnRootNode, runAsync, cloudOnly); + try (MockedStatic mockedFrameworkUtil = mockStatic(FrameworkUtil.class)) { + createAndActivateStartupHookService(mockedFrameworkUtil); + Thread.sleep(1000L); + verify(installationService, times(1)).apply(null, new String[]{"^/$", "^$"}, true); + } + } + + void setup(boolean canSetPropertiesOnRootNode, boolean runAsyncForMutableContent, boolean cloudOnly) throws RepositoryException { FrameworkStartLevel startLevel = Mockito.mock(FrameworkStartLevel.class); when(startLevel.getStartLevel()).thenReturn(0); - when(config.activationMode()).thenReturn(AcToolStartupHookServiceImpl.Config.StartupHookActivation.ALWAYS); - - when(session.hasPermission("/", Session.ACTION_SET_PROPERTY)).thenReturn(canReadApps); - if (canReadApps) { + if (canSetPropertiesOnRootNode) { + when(session.hasPermission("/", Session.ACTION_SET_PROPERTY)).thenReturn(true); when(noChildren.hasNext()).thenReturn(false); when(rootNode.getNodes()).thenReturn(noChildren); when(session.getRootNode()).thenReturn(rootNode); + } else if (!canSetPropertiesOnRootNode && !cloudOnly){ + when(session.hasPermission("/", Session.ACTION_SET_PROPERTY)).thenReturn(false); + } + if (!cloudOnly) { + when(repository.loginService(null, null)).thenReturn(session); } - when(repository.loginService(null, null)).thenReturn(session); when(bundle.getBundleContext()).thenReturn(bundleContext); when(bundle.getSymbolicName()).thenReturn(RuntimeHelper.INSTALLER_CORE_BUNDLE_SYMBOLIC_ID); when(bundle.adapt(FrameworkStartLevel.class)).thenReturn(startLevel); - when(bundleContext.getBundles()).thenReturn(new Bundle[] { bundle }); + when(bundleContext.getBundles()).thenReturn(new Bundle[]{bundle}); when(bundleContext.getBundle(anyLong())).thenReturn(bundle); + + when(config.runAsyncForMutableConent()).thenReturn(runAsyncForMutableContent); + when(config.activationMode()).thenReturn(cloudOnly ? + AcToolStartupHookServiceImpl.Config.StartupHookActivation.CLOUD_ONLY : + AcToolStartupHookServiceImpl.Config.StartupHookActivation.ALWAYS); } - private void createAndActivateStartupHookService(MockedStatic mockedFrameworkUtil, boolean runAsyncForMutableContent) { + private void createAndActivateStartupHookService(MockedStatic mockedFrameworkUtil) { mockedFrameworkUtil.when(() -> FrameworkUtil.getBundle(RuntimeHelper.class)).thenReturn(bundle); AcToolStartupHookServiceImpl startupHookService = new AcToolStartupHookServiceImpl(); startupHookService.repository = repository; startupHookService.acInstallationService = installationService; - when(config.runAsyncForMutableConent()).thenReturn(runAsyncForMutableContent); startupHookService.activate(bundleContext, config); } } \ No newline at end of file