Skip to content

Commit

Permalink
Refactor test methods to increase coverage of activate
Browse files Browse the repository at this point in the history
  • Loading branch information
gicig committed Sep 10, 2024
1 parent f5dc3ca commit e8f1f48
Showing 1 changed file with 52 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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<FrameworkUtil> 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<FrameworkUtil> 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<FrameworkUtil> 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<FrameworkUtil> 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<FrameworkUtil> mockedFrameworkUtil, boolean runAsyncForMutableContent) {
private void createAndActivateStartupHookService(MockedStatic<FrameworkUtil> 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);
}
}

0 comments on commit e8f1f48

Please sign in to comment.