Skip to content

Commit

Permalink
Test RuntimeHelper#isCompositeNodeStore too
Browse files Browse the repository at this point in the history
  • Loading branch information
gicig committed Sep 11, 2024
1 parent 7920456 commit b160bed
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@
* #L%
*/

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;

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

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand All @@ -30,15 +36,44 @@ class RuntimeHelperTest {
@Mock
Session session;

@Mock
Bundle bundle;

@Mock
BundleContext bundleContext;

@Test
void shouldBeReadonlyIfRootWritable() throws RepositoryException {
when(session.hasPermission("/", Session.ACTION_SET_PROPERTY)).thenReturn(true);
Assertions.assertTrue(RuntimeHelper.isAppsReadOnly(session));
assertTrue(RuntimeHelper.isAppsReadOnly(session));
}

@Test
void shouldNotBeReadonlyIfRootNotWritable() throws RepositoryException {
when(session.hasPermission("/", Session.ACTION_SET_PROPERTY)).thenReturn(false);
Assertions.assertFalse(RuntimeHelper.isAppsReadOnly(session));
assertFalse(RuntimeHelper.isAppsReadOnly(session));
}

@Test
void shouldNotBeCompositeStoreWhenCoreInstallerPresent() {
try (MockedStatic<FrameworkUtil> mockedFrameworkUtil = mockStatic(FrameworkUtil.class)) {
setupOsgi(RuntimeHelper.INSTALLER_CORE_BUNDLE_SYMBOLIC_ID, mockedFrameworkUtil);
assertFalse(RuntimeHelper.isCompositeNodeStore());
}
}

@Test
void shouldBeCompositeStoreWhenCoreInstallerNotPresent() {
try (MockedStatic<FrameworkUtil> mockedFrameworkUtil = mockStatic(FrameworkUtil.class)) {
setupOsgi("unknown.bundle", mockedFrameworkUtil);
assertTrue(RuntimeHelper.isCompositeNodeStore());
}
}

private void setupOsgi(String t, MockedStatic<FrameworkUtil> mockedFrameworkUtil) {
when(bundle.getBundleContext()).thenReturn(bundleContext);
when(bundle.getSymbolicName()).thenReturn(t);
when(bundleContext.getBundles()).thenReturn(new Bundle[]{bundle});
mockedFrameworkUtil.when(() -> FrameworkUtil.getBundle(RuntimeHelper.class)).thenReturn(bundle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void testActivationWithAsync() throws RepositoryException, InterruptedException
setup(canSetPropertiesOnRootNode, runAsync, cloudOnly);
try (MockedStatic<FrameworkUtil> mockedFrameworkUtil = mockStatic(FrameworkUtil.class)) {
createAndActivateStartupHookService(mockedFrameworkUtil);
// best attempt to test asynchronous invocation in AcToolStartupHookServiceImpl#runAcToolAsync
Thread.sleep(1000L);
verify(installationService, times(1)).apply(null, new String[]{"^/$", "^$"}, true);
}
Expand All @@ -112,7 +113,7 @@ void setup(boolean canSetPropertiesOnRootNode, boolean runAsyncForMutableContent
when(noChildren.hasNext()).thenReturn(false);
when(rootNode.getNodes()).thenReturn(noChildren);
when(session.getRootNode()).thenReturn(rootNode);
} else if (!canSetPropertiesOnRootNode && !cloudOnly) {
} else if (!cloudOnly) {
when(session.hasPermission("/", Session.ACTION_SET_PROPERTY)).thenReturn(false);
}
if (!cloudOnly) {
Expand Down

0 comments on commit b160bed

Please sign in to comment.