Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #31 from otarsko/#9-groovy-handler-testing
Browse files Browse the repository at this point in the history
#9: groovy handler testing
  • Loading branch information
nc-andreashaller authored Oct 26, 2017
2 parents 1598f64 + 9b79f13 commit a44a431
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 6 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@
<url>https://github.com/Netcentric/vault-upgrade-hook/issues</url>
</issueManagement>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<defaultGoal>clean install</defaultGoal>
<pluginManagement>
Expand Down
4 changes: 4 additions & 0 deletions vault-upgrade-hook/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,9 @@
<version>1.7.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ public UpgradeInfo(InstallContext ctx, UpgradeStatus status, Node node) throws R
installationMode = InstallationMode.valueOf(JcrUtils.getStringProperty(node, PN_INSTALLATION_MODE, DEFAULT_INSTALLATION_MODE).toUpperCase());
defaultPhase = Phase.valueOf(JcrUtils.getStringProperty(node, PN_DEFAULT_PHASE, DEFAULT_PHASE).toUpperCase());
handler = UpgradeType.create(ctx, JcrUtils.getStringProperty(node, PN_HANDLER, DEFAULT_HANDLER));
loadActions(ctx);
LOG.debug(ctx, "UpgradeInfo loaded [{}]", this);
}

private void loadActions(InstallContext ctx) throws RepositoryException {
public void loadActions(InstallContext ctx) throws RepositoryException {
for (Phase availablePhase : Phase.values()) {
actions.put(availablePhase, new ArrayList<UpgradeAction>());
}
Expand Down Expand Up @@ -125,7 +124,7 @@ public String toString() {
+ ", defaultPhase=" + defaultPhase + ", handler=" + handler + ", actions=" + actions + "]";
}

public Map<Phase, List<UpgradeAction>> getActions() throws RepositoryException {
public Map<Phase, List<UpgradeAction>> getActions() {
return actions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ protected void loadInfos(final InstallContext ctx) throws RepositoryException {
while (nodes.hasNext()) {
Node child = nodes.nextNode();
final UpgradeInfo info = new UpgradeInfo(ctx, status, child);
info.loadActions(ctx);
LOG.debug(ctx, "info [{}]", info);
infos.add(info);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
*/
public class GroovyConsoleHandler implements UpgradeHandler {

private static final PackageInstallLogger LOG = PackageInstallLogger.create(GroovyConsoleHandler.class);
static PackageInstallLogger log = PackageInstallLogger.create(GroovyConsoleHandler.class);

OsgiUtil osgi = new OsgiUtil();

@Override
public boolean isAvailable(InstallContext ctx) {
boolean available = osgi.hasService(GroovyConsoleService.class);
if (!available) {
LOG.warn(ctx, "Could not load GroovyConsoleService.");
log.warn(ctx, "Could not load GroovyConsoleService.");
}
return available;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public void testConstructorDefaults() throws Exception {
try {
sling.build().resource("/test");
Node node = session.getNode("/test");
new UpgradeInfo(ctx, status, node);
UpgradeInfo upgradeInfo = new UpgradeInfo(ctx, status, node);
upgradeInfo.loadActions(ctx);

Assert.fail("Exception expected");
} catch (IllegalStateException e) {
Assert.assertNotNull(e);
Expand All @@ -69,6 +71,7 @@ public void testConstructorDefaults() throws Exception {
Node node = session.getNode("/test");
Mockito.when(ctx.getPackage().getId().getVersionString()).thenReturn("1.0.0");
UpgradeInfo info = new UpgradeInfo(ctx, status, node);
info.loadActions(ctx);
Assert.assertSame(node, info.getNode());
Assert.assertSame(status, info.getStatus());
Assert.assertEquals(UpgradeInfo.DEFAULT_INSTALLATION_MODE, info.getInstallationMode().toString());
Expand All @@ -88,6 +91,7 @@ public void testConstructor() throws Exception {
UpgradeInfo.PN_HANDLER, TEST_HANDLER);
Node node = session.getNode("/test");
UpgradeInfo info = new UpgradeInfo(ctx, status, node);
info.loadActions(ctx);

Assert.assertEquals(InstallationMode.ALWAYS, info.getInstallationMode());
Assert.assertEquals(Phase.PREPARE, info.getDefaultPhase());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* (C) Copyright 2016 Netcentric AG.
*
* 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
*/
package biz.netcentric.vlt.upgrade.handler.groovy;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.apache.jackrabbit.vault.packaging.impl.InstallContextImpl;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

import com.icfolson.aem.groovy.console.GroovyConsoleService;

import biz.netcentric.vlt.upgrade.UpgradeAction;
import biz.netcentric.vlt.upgrade.UpgradeInfo;
import biz.netcentric.vlt.upgrade.handler.OsgiUtil;
import biz.netcentric.vlt.upgrade.testUtils.LoggerStub;

@RunWith(Enclosed.class)
public class GroovyConsoleHandlerTest {

public static final class IsAvailable extends GroovyConsoleHandlerAbstractTest {


@Test
public void shouldReturnTrueIfGroovyConsoleInstalled() throws RepositoryException {
Mockito.doReturn(true).when(osgi).hasService(GroovyConsoleService.class);

boolean result = groovyConsoleHandler.isAvailable(installContext);
assertThat(result).as("Should return true if GroovyConsoleService is available").isTrue();
}

@Test
public void shouldReturnFalseIfGroovyConsoleNotInstalled() throws RepositoryException {
Mockito.doReturn(false).when(osgi).hasService(GroovyConsoleService.class);

boolean result = groovyConsoleHandler.isAvailable(installContext);
assertThat(result).as("Should return false if GroovyConsoleService is not available").isFalse();
}
}

public static final class Create extends GroovyConsoleHandlerAbstractTest {

@Test
public void shouldReturnGroovyScriptActions() throws RepositoryException {
List<UpgradeAction> upgradeActions = groovyConsoleHandler.create(installContext, info);
assertThat(upgradeActions).hasSize(2);
}

@Test
public void shouldReturnOnlyGroovyScripts() throws RepositoryException {
List<UpgradeAction> upgradeActions = groovyConsoleHandler.create(installContext, info);

for (UpgradeAction action : upgradeActions) {
assertThat(action.getName()).endsWith(".groovy");
}
}
}

public static abstract class GroovyConsoleHandlerAbstractTest {

@Rule
public final SlingContext sling = new SlingContext(ResourceResolverType.JCR_MOCK);

GroovyConsoleHandler groovyConsoleHandler;
OsgiUtil osgi;
InstallContextImpl installContext;
Node node;
UpgradeInfo info;

@Before
public void setUp() throws RepositoryException, UnsupportedEncodingException {
groovyConsoleHandler = new GroovyConsoleHandler();
GroovyConsoleHandler.log = new LoggerStub();

osgi = Mockito.spy(new OsgiUtil());
groovyConsoleHandler.osgi = osgi;

sling.build()
.resource("/some/resource")
.siblingsMode()
.file("script1.groovy", new ByteArrayInputStream("testContent1".getBytes(StandardCharsets.UTF_8.name())), "application/octet-stream", new Date().getTime())
.file("script2.groovy", new ByteArrayInputStream("testContent2".getBytes(StandardCharsets.UTF_8.name())), "application/octet-stream", new Date().getTime())
.file("script3.java", new ByteArrayInputStream("Not a groovy script".getBytes(StandardCharsets.UTF_8.name())), "application/octet-stream", new Date().getTime())
.resource("notAFileNode.groovy")
.commit();

node = sling.resourceResolver().getResource("/some/resource").adaptTo(Node.class);
installContext = new InstallContextImpl(node, null, null, null);
info = new UpgradeInfo(installContext, null, node);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* (C) Copyright 2016 Netcentric AG.
*
* 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
*/
package biz.netcentric.vlt.upgrade.testUtils;

import org.apache.jackrabbit.vault.packaging.InstallContext;

import biz.netcentric.vlt.upgrade.util.PackageInstallLogger;

public class LoggerStub extends PackageInstallLogger {

public LoggerStub() {
super(null);
}

@Override
public void debug(InstallContext ctx, String format, Object... arguments) {

}

@Override
public void info(InstallContext ctx, String format, Object... arguments) {

}

@Override
public void status(InstallContext ctx, String format, String path, Object... arguments) {

}

@Override
public void warn(InstallContext ctx, String format, Object... arguments) {

}

@Override
public void error(InstallContext ctx, String format, Object... arguments) {

}
}

0 comments on commit a44a431

Please sign in to comment.