Skip to content

Commit

Permalink
more classes to 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcin committed Aug 28, 2020
1 parent b08086e commit d755acf
Show file tree
Hide file tree
Showing 9 changed files with 976 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void identifySubpackage(final PackageId packageId, final PackageId parent

@Override
public void beforeSlingInstall(final PackageId lastPackage,
final SlingInstallable slingInstallable,
final SlingInstallable<?> slingInstallable,
final Session inspectSession) throws RepositoryException {
wrapped.beforeSlingInstall(lastPackage, slingInstallable, inspectSession);
}
Expand All @@ -151,7 +151,7 @@ public void identifyEmbeddedPackage(final PackageId packageId, final PackageId p

@Override
public void appliedRepoInitScripts(final PackageId lastPackage,
final SlingInstallable slingInstallable,
final SlingInstallable<?> slingInstallable,
final Session inspectSession) throws RepositoryException {
wrapped.appliedRepoInitScripts(lastPackage, slingInstallable, inspectSession);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void identifySubpackage(final PackageId packageId, final PackageId parent

@Override
public void beforeSlingInstall(final PackageId lastPackage,
final SlingInstallable slingInstallable,
final SlingInstallable<?> slingInstallable,
final Session inspectSession) throws RepositoryException {
if (!silenced) {
wrapped.beforeSlingInstall(lastPackage, slingInstallable, inspectSession);
Expand All @@ -166,7 +166,7 @@ public void identifyEmbeddedPackage(final PackageId packageId, final PackageId p

@Override
public void appliedRepoInitScripts(final PackageId lastPackage,
final SlingInstallable slingInstallable,
final SlingInstallable<?> slingInstallable,
final Session inspectSession) throws RepositoryException {
if (!silenced) {
wrapped.appliedRepoInitScripts(lastPackage, slingInstallable, inspectSession);
Expand Down
37 changes: 36 additions & 1 deletion core/src/main/java/net/adamcin/oakpal/core/checks/Echo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import net.adamcin.oakpal.api.PathAction;
import net.adamcin.oakpal.api.ProgressCheck;
import net.adamcin.oakpal.api.SlingInstallable;
import net.adamcin.oakpal.api.SlingSimulator;
import net.adamcin.oakpal.api.Violation;
import org.apache.jackrabbit.vault.fs.config.MetaInf;
import org.apache.jackrabbit.vault.packaging.PackageId;
import org.apache.jackrabbit.vault.packaging.PackageProperties;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ConsumerType;

import javax.jcr.Node;
Expand All @@ -34,6 +37,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.jar.Manifest;

Expand Down Expand Up @@ -134,7 +138,38 @@ public void deletedPath(final PackageId packageId, final String path, final Sess
@Override
public void afterExtract(final PackageId packageId, final Session inspectSession) throws RepositoryException {
echo("afterExtract(packageId: %s, inspectSession: %s)", packageId,
Optional.ofNullable(inspectSession).map(Session::getUserID).orElse(null));
sessionToString(inspectSession));
}

@Override
public void simulateSling(final SlingSimulator slingSimulator, final Set<String> runModes) {
echo("simulateSling(slingSimulator: %s, runModes: %s)", slingSimulator.getClass().getName(), runModes);
}

@Override
public void identifyEmbeddedPackage(final PackageId packageId, final PackageId parentId, final String jcrPath) {
echo("identifyEmbeddedPackage(packageId: %s, parentId: %s, jcrPath: %s)", packageId, parentId, jcrPath);
}

@Override
public void beforeSlingInstall(final PackageId lastPackage, final SlingInstallable<?> slingInstallable, final Session inspectSession) throws RepositoryException {
echo("beforeSlingInstall(lastPackage: %s, slingInstallable: %s, inspectSession: %s)", lastPackage, slingInstallable,
sessionToString(inspectSession));
}

@Override
public void appliedRepoInitScripts(final PackageId lastPackage, final SlingInstallable<?> slingInstallable, final Session inspectSession) throws RepositoryException {
echo("appliedRepoInitScripts(lastPackage: %s, slingInstallable: %s, inspectSession: %s)", lastPackage, slingInstallable,
sessionToString(inspectSession));
}

@Override
public void afterScanPackage(final PackageId packageId, final Session inspectSession) throws RepositoryException {
echo("afterScanPackage(packageId: %s, inspectSession: %s)", packageId, sessionToString(inspectSession));
}

@Nullable String sessionToString(final @Nullable Session session) throws RepositoryException {
return Optional.ofNullable(session).map(Session::getUserID).orElse(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.adamcin.oakpal.api.ProgressCheck;
import net.adamcin.oakpal.api.ProgressCheckFactory;
import net.adamcin.oakpal.api.Severity;
import net.adamcin.oakpal.api.SilenceableCheck;
import net.adamcin.oakpal.api.SimpleProgressCheckFactoryCheck;
import net.adamcin.oakpal.api.SlingSimulator;
import org.apache.jackrabbit.vault.fs.api.WorkspaceFilter;
Expand Down Expand Up @@ -73,7 +74,10 @@ public ProgressCheck newInstance(final JsonObject config) {
return new Check(rootPaths);
}

static final class Check extends SimpleProgressCheckFactoryCheck<SlingJcrInstaller> {
/**
* This check implements {@link SilenceableCheck} specifically to avoid be wrapped with a silencing facade.
*/
static final class Check extends SimpleProgressCheckFactoryCheck<SlingJcrInstaller> implements SilenceableCheck {
private final List<String> rootPaths;

private SlingSimulator slingSimulator;
Expand All @@ -89,6 +93,11 @@ public void startedScan() {
super.startedScan();
}

@Override
public void setSilenced(final boolean silenced) {
/* do nothing, because we currently collect no violations in this check */
}

@Override
public void simulateSling(final SlingSimulator slingSimulator, final Set<String> runModes) {
this.slingSimulator = slingSimulator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

import net.adamcin.oakpal.api.PathAction;
import net.adamcin.oakpal.api.ProgressCheck;
import net.adamcin.oakpal.api.SilenceableCheck;
import net.adamcin.oakpal.api.SlingInstallable;
import net.adamcin.oakpal.api.SlingSimulator;
import net.adamcin.oakpal.api.Violation;
import net.adamcin.oakpal.core.sling.NoopSlingSimulator;
import org.apache.jackrabbit.vault.fs.config.MetaInf;
import org.apache.jackrabbit.vault.packaging.PackageId;
import org.apache.jackrabbit.vault.packaging.PackageProperties;
Expand All @@ -33,11 +37,15 @@
import java.util.Collection;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
Expand Down Expand Up @@ -88,6 +96,41 @@ public void testFinishedScan() {
assertTrue("did it", didIt.getNow(false));
}


@Test
public void testSetSilenced() {
final CompletableFuture<Boolean> didIt = new CompletableFuture<>();
final SilenceableCheck delegate = mock(SilenceableCheck.class);
doAnswer(call -> didIt.complete(call.getArgument(0))).when(delegate).setSilenced(anyBoolean());
final ProgressCheckAliasFacade alias = new ProgressCheckAliasFacade(delegate, null);
alias.setSilenced(true);
assertTrue("silenced it", didIt.getNow(false));
}

@Test
public void testSimulateSling() {
final SlingSimulator arg0 = NoopSlingSimulator.instance();
final Set<String> arg1 = Stream.of("author", "publish").collect(Collectors.toSet());
final CompletableFuture<SlingSimulator> slot0 = new CompletableFuture<>();
final CompletableFuture<Set<?>> slot1 = new CompletableFuture<>();

final ProgressCheck delegate = mock(ProgressCheck.class);

doAnswer(call -> {
slot0.complete(call.getArgument(0, SlingSimulator.class));
slot1.complete(call.getArgument(1, Set.class));
return true;
}).when(delegate).simulateSling(
any(SlingSimulator.class),
any(Set.class));

final ProgressCheckAliasFacade alias = new ProgressCheckAliasFacade(delegate, null);
alias.simulateSling(arg0, arg1);

assertSame("same arg0", arg0, slot0.getNow(null));
assertSame("same arg1", arg1, slot1.getNow(null));
}

@Test
public void testIdentifyPackage() {
final PackageId arg0 = PackageId.fromString("my_packages:example:1.0");
Expand Down Expand Up @@ -376,4 +419,167 @@ public void testSetResourceBundle() {
facade.setResourceBundle(ResourceBundle.getBundle(facade.getResourceBundleBaseName()));
assertSame("expect same resource bundle", expected, slot.getNow(null));
}

@Test(expected = RepositoryException.class)
public void testBeforeSlingInstall_throws() throws Exception {
final PackageId arg0 = PackageId.fromString("my_packages:example:1.0");
final SlingInstallable<?> arg1 = mock(SlingInstallable.class);
final Session arg2 = mock(Session.class);

final ProgressCheck delegate = mock(ProgressCheck.class);
doThrow(RepositoryException.class).when(delegate).beforeSlingInstall(
any(PackageId.class),
any(SlingInstallable.class),
any(Session.class));

final ProgressCheckAliasFacade alias = new ProgressCheckAliasFacade(delegate, null);
alias.beforeSlingInstall(arg0, arg1, arg2);
}

@Test
public void testBeforeSlingInstall() throws Exception {
final PackageId arg0 = PackageId.fromString("my_packages:example:1.0");
final SlingInstallable<?> arg1 = mock(SlingInstallable.class);
final Session arg2 = mock(Session.class);

final CompletableFuture<PackageId> slot0 = new CompletableFuture<>();
final CompletableFuture<SlingInstallable<?>> slot1 = new CompletableFuture<>();
final CompletableFuture<Session> slot2 = new CompletableFuture<>();

final ProgressCheck delegate = mock(ProgressCheck.class);

doAnswer(call -> {
slot0.complete(call.getArgument(0, PackageId.class));
slot1.complete(call.getArgument(1, SlingInstallable.class));
slot2.complete(call.getArgument(2, Session.class));
return true;
}).when(delegate).beforeSlingInstall(
any(PackageId.class),
any(SlingInstallable.class),
any(Session.class));

final ProgressCheckAliasFacade alias = new ProgressCheckAliasFacade(delegate, null);
alias.beforeSlingInstall(arg0, arg1, arg2);

assertSame("same arg0", arg0, slot0.getNow(null));
assertSame("same arg1", arg1, slot1.getNow(null));
assertSame("same arg2", arg2, slot2.getNow(null));
}

@Test
public void testIdentifyEmbeddedPackage() {
final PackageId arg0 = PackageId.fromString("my_packages:example:1.0");
final PackageId arg1 = PackageId.fromString("my_packages:other_example:1.0");
final String arg2 = "/some/path";

final CompletableFuture<PackageId> slot0 = new CompletableFuture<>();
final CompletableFuture<PackageId> slot1 = new CompletableFuture<>();
final CompletableFuture<String> slot2 = new CompletableFuture<>();

final ProgressCheck delegate = mock(ProgressCheck.class);

doAnswer(call -> {
slot0.complete(call.getArgument(0, PackageId.class));
slot1.complete(call.getArgument(1, PackageId.class));
slot2.complete(call.getArgument(2, String.class));
return true;
}).when(delegate).identifyEmbeddedPackage(
any(PackageId.class),
any(PackageId.class),
any(String.class));

final ProgressCheckAliasFacade alias = new ProgressCheckAliasFacade(delegate, null);
alias.identifyEmbeddedPackage(arg0, arg1, arg2);

assertSame("same arg0", arg0, slot0.getNow(null));
assertSame("same arg1", arg1, slot1.getNow(null));
assertSame("same arg2", arg2, slot2.getNow(null));
}

@Test(expected = RepositoryException.class)
public void testAppliedRepoInitScripts_throws() throws Exception {
final PackageId arg0 = PackageId.fromString("my_packages:example:1.0");
final SlingInstallable<?> arg1 = mock(SlingInstallable.class);
final Session arg2 = mock(Session.class);

final ProgressCheck delegate = mock(ProgressCheck.class);
doThrow(RepositoryException.class).when(delegate).appliedRepoInitScripts(
any(PackageId.class),
any(SlingInstallable.class),
any(Session.class));

final ProgressCheckAliasFacade alias = new ProgressCheckAliasFacade(delegate, null);
alias.appliedRepoInitScripts(arg0, arg1, arg2);
}

@Test
public void testAppliedRepoInitScripts() throws Exception {
final PackageId arg0 = PackageId.fromString("my_packages:example:1.0");
final SlingInstallable<?> arg1 = mock(SlingInstallable.class);
final Session arg2 = mock(Session.class);

final CompletableFuture<PackageId> slot0 = new CompletableFuture<>();
final CompletableFuture<SlingInstallable<?>> slot1 = new CompletableFuture<>();
final CompletableFuture<Session> slot2 = new CompletableFuture<>();

final ProgressCheck delegate = mock(ProgressCheck.class);

doAnswer(call -> {
slot0.complete(call.getArgument(0, PackageId.class));
slot1.complete(call.getArgument(1, SlingInstallable.class));
slot2.complete(call.getArgument(2, Session.class));
return true;
}).when(delegate).appliedRepoInitScripts(
any(PackageId.class),
any(SlingInstallable.class),
any(Session.class));

final ProgressCheckAliasFacade alias = new ProgressCheckAliasFacade(delegate, null);
alias.appliedRepoInitScripts(arg0, arg1, arg2);

assertSame("same arg0", arg0, slot0.getNow(null));
assertSame("same arg1", arg1, slot1.getNow(null));
assertSame("same arg2", arg2, slot2.getNow(null));
}

@Test(expected = RepositoryException.class)
public void testAfterScanPackage_throws() throws Exception {
final PackageId arg0 = PackageId.fromString("my_packages:example:1.0");
final Session arg1 = mock(Session.class);

final ProgressCheck delegate = mock(ProgressCheck.class);
doThrow(RepositoryException.class).when(delegate).afterScanPackage(
any(PackageId.class),
any(Session.class));

final ProgressCheckAliasFacade alias = new ProgressCheckAliasFacade(delegate, null);
alias.afterScanPackage(arg0, arg1);
}

@Test
public void testAfterScanPackage() throws Exception {
final PackageId arg0 = PackageId.fromString("my_packages:example:1.0");
final Session arg1 = mock(Session.class);

final CompletableFuture<PackageId> slot0 = new CompletableFuture<>();
final CompletableFuture<Session> slot1 = new CompletableFuture<>();

final ProgressCheck delegate = mock(ProgressCheck.class);

doAnswer(call -> {
slot0.complete(call.getArgument(0, PackageId.class));
slot1.complete(call.getArgument(1, Session.class));
return true;
}).when(delegate).afterScanPackage(
any(PackageId.class),
any(Session.class));

final ProgressCheckAliasFacade alias = new ProgressCheckAliasFacade(delegate, null);

alias.afterScanPackage(arg0, arg1);

assertSame("same arg0", arg0, slot0.getNow(null));
assertSame("same arg1", arg1, slot1.getNow(null));
}

}
Loading

0 comments on commit d755acf

Please sign in to comment.