Skip to content

Commit

Permalink
Add a unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
gicig committed Sep 10, 2024
1 parent d6b570e commit 3f1999b
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class RuntimeHelper {
public static final Logger LOG = LoggerFactory.getLogger(RuntimeHelper.class);

private static final String INSTALLER_CORE_BUNDLE_SYMBOLIC_ID = "org.apache.sling.installer.core";
public static final String INSTALLER_CORE_BUNDLE_SYMBOLIC_ID = "org.apache.sling.installer.core";

public static boolean isAppsReadOnly(Session session) {

Expand Down
21 changes: 21 additions & 0 deletions accesscontroltool-startuphook-bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- START: Test Dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<!-- use the uber-jar always as last dependency because a lot of classes are provided also by other artifacts
It is only used here as replacement for com.adobe.granite:com.adobe.granite.crypto:3.0.0 whose pom.xml is not publicly available -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ public enum StartupHookActivation {
}

@Reference(policyOption = ReferencePolicyOption.GREEDY)
private AcInstallationService acInstallationService;
AcInstallationService acInstallationService;

@Reference(policyOption = ReferencePolicyOption.GREEDY)
private SlingRepository repository;
SlingRepository repository;

private boolean isAppsReadOnly;

Expand Down Expand Up @@ -118,12 +118,7 @@ private void runAcTool(List<String> relevantPathsForInstallation, int currentSta
private void runAcToolAsync(final List<String> relevantPathsForInstallation, final int currentStartLevel, final boolean isCloudReady) {

final AcToolStartupHookServiceImpl startupHook = this;
new Thread(new Runnable() {
@Override
public void run() {
startupHook.runAcTool(relevantPathsForInstallation, currentStartLevel, isCloudReady);
}
}, THREAD_NAME_ASYNC).start();
new Thread(() -> startupHook.runAcTool(relevantPathsForInstallation, currentStartLevel, isCloudReady), THREAD_NAME_ASYNC).start();
}

private List<String> getRelevantPathsForInstallation() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package biz.netcentric.cq.tools.actool.startuphook.impl;

/*-
* #%L
* Access Control Tool Bundle
* %%
* Copyright (C) 2015 - 2024 Cognizant Netcentric
* %%
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
* #L%
*/

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.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.startlevel.FrameworkStartLevel;

import javax.jcr.RepositoryException;
import javax.jcr.Session;

import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class AcToolStartupHookServiceImplTest {

@Mock
Bundle bundle;

@Mock
BundleContext bundleContext;

@Mock
AcToolStartupHookServiceImpl.Config config;

@Mock
SlingRepository repository;

@Spy
AcInstallationService installationService;

@BeforeEach
void setup() throws RepositoryException {
FrameworkStartLevel startLevel = Mockito.mock(FrameworkStartLevel.class);
when(startLevel.getStartLevel()).thenReturn(0);

when(config.activationMode()).thenReturn(AcToolStartupHookServiceImpl.Config.StartupHookActivation.ALWAYS);

Session session = mock(Session.class);
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.getBundle(anyLong())).thenReturn(bundle);
}
@Test
void testActivation() {
try (MockedStatic<FrameworkUtil> mockedFrameworkUtil = mockStatic(FrameworkUtil.class)) {
mockedFrameworkUtil.when(() -> FrameworkUtil.getBundle(RuntimeHelper.class)).thenReturn(bundle);
AcToolStartupHookServiceImpl startupHookService = new AcToolStartupHookServiceImpl();
startupHookService.repository = repository;
startupHookService.acInstallationService = installationService;
startupHookService.activate(bundleContext, config);
verify(installationService, times(1)).apply(null, new String[]{}, true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<bouncycastle.version>1.64</bouncycastle.version>
<!-- release sets API classpath, source and target, see https://docs.oracle.com/javase/9/tools/javac.htm#GUID-AEEC9F07-CB49-4E96-8BC7-BCC2C7F725C9__GUID-D343F6B4-3FDD-43A8-AD24-43DD70214471 and http://openjdk.java.net/jeps/247 -->
<maven.compiler.release>8</maven.compiler.release>
<mockito.version>4.8.0</mockito.version>
<mockito.version>5.13.0</mockito.version>
<junit.version>5.10.0</junit.version>
<!-- default content package file to install on AEM -->
<contentPackageFile>${project.build.directory}/${project.build.finalName}.zip</contentPackageFile>
Expand Down

0 comments on commit 3f1999b

Please sign in to comment.