Unit test Sling Event Handler
Hi,
I would like to test the "event_filter" path behavior , specific to if allowed path replication event fired then event handler gets executed vs replication event fired on non-allowed path, event handler does not get executed. This question is more of how I test event handler getting executed only on specific path defined by EVENT_FITLER.
I appreciate any help!
Thanks,
Sri
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.wcm.api.Page;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.settings.SlingSettingsService;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
/**
* Event handler for handling the replication of disclaimer content.
* setting configuration policy to REQUIRE to ensure that the configuration is available only in author instance
* (runmodes) and code executed in author instance not on publish instances.
*
*/
@Component(service = EventHandler.class,
immediate = true,
property = {
EventConstants.EVENT_TOPIC +"=" + ReplicationAction.EVENT_TOPIC,
EventConstants.EVENT_FILTER +"=(paths=/content/siteallowed/*)"
},
configurationPolicy = ConfigurationPolicy.REQUIRE)
public class PublicationEventHandler implements EventHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(PublicationEventHandler.class);
public static final String CONTENT_JOB_TOPIC = "CONTENT-JOB-TOPIC";
@3214626
private ResourceResolverFactory resourceResolverFactory;
@3214626
private JobManager jobManager;
/**
* Handles the event of clearing the cache whenever a page replication event occurs.
*/
public void handleEvent(Event event) {
try {
ReplicationAction action = ReplicationAction.fromEvent(event);
if (action != null) {
final Map<String, Object> props = new HashMap<>();
props.put("path", action.getPath());
jobManager.addJob(CONTENT_JOB_TOPIC, props);
LOGGER.info("Replication action {} occured on {} ", action.getType().getName(), action.getPath());
}
} catch(Exception e) {
LOGGER.error("Error occurred while handling the event PublicationEventHandler", e);
}
}
}