@Test public void testFailRuleExecution() throws RepositoryException, RuleExecutionException { String path = "/test/ruleset"; Mockito.when(request.getResourceResolver()).thenReturn(resourceResolver); Mockito.when(resourceResolver.adaptTo(Session.class)).thenReturn(session); Mockito.when(session.getUserID()).thenReturn("ieb"); Mockito.when(resourceResolver.getResource(path)).thenReturn(ruleSet); Mockito.when(ruleSet.getResourceType()).thenReturn(RuleConstants.SAKAI_RULE_SET); Mockito.when(ruleSet.adaptTo(Node.class)).thenReturn(ruleSetNode); Mockito.when(context.getBundleContext()).thenReturn(bundleContext); Mockito.when(ruleSetNode.getPath()).thenReturn(path); Mockito.when(ruleSetNode.getNodes()).thenReturn(nodeIterator); // specify the rule processor Mockito.when(ruleSetNode.hasProperty(RuleConstants.PROP_SAKAI_RULE_EXECUTION_PREPROCESSOR)) .thenReturn(true); Mockito.when(ruleSetNode.getProperty(RuleConstants.PROP_SAKAI_RULE_EXECUTION_PREPROCESSOR)) .thenReturn(preprocessorProperty); // force the pre-processor to fail Mockito.when(preprocessorProperty.getString()).thenReturn("message-test-preprocessor-missing"); // when the service referece is invoked we need to give it out processor Mockito.when(reference.getProperty(RuleConstants.PROCESSOR_NAME)) .thenReturn("message-test-preprocessor"); RuleExecutionPreProcessor messageRuleExcutionPreProcessor = new MesageRuleExcutionPreProcessor(); Mockito.when(bundleContext.getService(reference)).thenReturn(messageRuleExcutionPreProcessor); // turn on debug for this rule execution, just needs the property to exist. Mockito.when(ruleSetNode.hasProperty(RuleConstants.PROP_SAKAI_RULE_DEBUG)).thenReturn(true); Mockito.when(nodeIterator.hasNext()).thenReturn(true, false, true, false); Mockito.when(nodeIterator.nextNode()).thenReturn(packageNode, packageNode); Mockito.when(packageNode.getPrimaryNodeType()).thenReturn(packageNodeType); Mockito.when(packageNodeType.getName()).thenReturn(NodeType.NT_FILE); Mockito.when(packageNode.getPath()).thenReturn(path + "/package1"); Mockito.when(packageNode.getNode(Node.JCR_CONTENT)).thenReturn(packageFileNode); Mockito.when(packageFileNode.getProperty(Property.JCR_LAST_MODIFIED)).thenReturn(property); Calendar lastModified = GregorianCalendar.getInstance(); lastModified.setTimeInMillis(System.currentTimeMillis()); Mockito.when(property.getDate()).thenReturn(lastModified); Mockito.when(packageFileNode.getProperty(Property.JCR_DATA)).thenReturn(packageFileBody); Mockito.when(packageFileBody.getBinary()).thenReturn(binary); Mockito.when(binary.getStream()) .thenAnswer( new Answer<InputStream>() { public InputStream answer(InvocationOnMock invocation) throws Throwable { return this.getClass() .getResourceAsStream( "/SLING-INF/content/var/rules/org.sakaiproject.nakamura.rules/org.sakaiproject.nakamura.rules.example/0.7-SNAPSHOT/org.sakaiproject.nakamura.rules.example-0.7-SNAPSHOT.pkg"); } }); RuleExecutionServiceImpl res = new RuleExecutionServiceImpl(); res.bindProcessor(reference); res.activate(context); try { @SuppressWarnings("unused") Map<String, Object> result = res.executeRuleSet(path, request, targetResource, ruleContext, null); Assert.fail(); } catch (RuleExecutionException e) { } res.unbindProcessor(reference); res.deactivate(context); res.unbindProcessor(reference); }