-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runner): add optional util provider extension
- Loading branch information
1 parent
83ee303
commit 90113ef
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/com/swisscom/aem/tools/impl/extension/UtilProviderExtension.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,28 @@ | ||
package com.swisscom.aem.tools.impl.extension; | ||
|
||
import com.swisscom.aem.tools.jcrhopper.RunnerBuilder; | ||
import com.swisscom.aem.tools.jcrhopper.osgi.RunnerBuilderExtension; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.osgi.service.component.annotations.Component; | ||
|
||
@Component(service = RunnerBuilderExtension.class) | ||
@Slf4j | ||
public class UtilProviderExtension implements RunnerBuilderExtension { | ||
|
||
@Override | ||
public void configure(RunnerBuilder builder) { | ||
registerHelper(builder, "io", "org.apache.commons.io.IOUtils"); | ||
registerHelper(builder, "arr", "org.apache.commons.lang3.ArrayUtils"); | ||
registerHelper(builder, "str", "org.apache.commons.lang3.StringUtils"); | ||
registerHelper(builder, "framework", "org.osgi.framework.FrameworkUtil"); | ||
} | ||
|
||
private void registerHelper(RunnerBuilder builder, String name, String className) { | ||
try { | ||
final Class<?> klazz = Class.forName(className); | ||
builder.addUtil(name, klazz); | ||
} catch (ClassNotFoundException e) { | ||
log.info("Not registering {} util because class {} is not available", name, className); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/test/java/com/swisscom/aem/tools/impl/extension/UtilProviderExtensionTest.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,70 @@ | ||
package com.swisscom.aem.tools.impl.extension; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import com.swisscom.aem.tools.impl.hops.Declare; | ||
import com.swisscom.aem.tools.impl.hops.RunScript; | ||
import com.swisscom.aem.tools.jcrhopper.HopperException; | ||
import com.swisscom.aem.tools.jcrhopper.config.LogLevel; | ||
import com.swisscom.aem.tools.jcrhopper.config.RunHandler; | ||
import com.swisscom.aem.tools.jcrhopper.config.Script; | ||
import com.swisscom.aem.tools.jcrhopper.osgi.RunnerService; | ||
import io.wcm.testing.mock.aem.junit5.AemContext; | ||
import io.wcm.testing.mock.aem.junit5.AemContextExtension; | ||
import io.wcm.testing.mock.aem.junit5.JcrMockAemContext; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.osgi.framework.FrameworkUtil; | ||
|
||
@ExtendWith(AemContextExtension.class) | ||
class UtilProviderExtensionTest { | ||
|
||
public final AemContext context = new JcrMockAemContext(); | ||
private RunnerService runnerService; | ||
|
||
private RunHandler mockRunHandler; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
context.registerInjectActivateService(Declare.class); | ||
context.registerInjectActivateService(RunScript.class); | ||
|
||
context.registerInjectActivateService(HopProviderExtension.class); | ||
context.registerInjectActivateService(UtilProviderExtension.class); | ||
|
||
runnerService = context.registerInjectActivateService(RunnerService.class); | ||
mockRunHandler = mock(RunHandler.class); | ||
} | ||
|
||
@Test | ||
void utilsPresent() throws HopperException, RepositoryException { | ||
runnerService | ||
.builder() | ||
.runHandler(mockRunHandler) | ||
.build( | ||
new Script( | ||
new RunScript.Config() | ||
.withExtension("js") | ||
.withCode( | ||
"log.info('ArrayUtils: {}', utils.arr);" + | ||
"log.info('IOUtils: {}', utils.io);" + | ||
"log.info('StringUtils: {}', utils.str);" + | ||
"log.info('FrameworkUtil: {}', utils.framework);" | ||
) | ||
) | ||
) | ||
.run(context.resourceResolver().adaptTo(Session.class), false); | ||
|
||
verify(mockRunHandler).log(LogLevel.INFO, "ArrayUtils: " + ArrayUtils.class, null, null); | ||
verify(mockRunHandler).log(LogLevel.INFO, "IOUtils: " + IOUtils.class, null, null); | ||
verify(mockRunHandler).log(LogLevel.INFO, "StringUtils: " + StringUtils.class, null, null); | ||
verify(mockRunHandler).log(LogLevel.INFO, "FrameworkUtil: " + FrameworkUtil.class, null, null); | ||
} | ||
} |