This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#9: groovy handler testing
- Loading branch information
Showing
8 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...ook/src/test/java/biz/netcentric/vlt/upgrade/handler/groovy/GroovyConsoleHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/testUtils/LoggerStub.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} | ||
} |