Skip to content

Commit

Permalink
feat(runner): add optional util provider extension
Browse files Browse the repository at this point in the history
  • Loading branch information
sabberworm committed Oct 10, 2024
1 parent 83ee303 commit 90113ef
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
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);
}
}
}
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);
}
}

0 comments on commit 90113ef

Please sign in to comment.