ConditionFunction extends ConditionImpl { @Override public boolean evaluate(AdaptrisMessage msg) throws ServiceException { try { Object o = ((Invocable) engine).invokeFunction("evaluateScript", msg); return BooleanUtils.toBoolean(o.toString()); } catch (Exception e) { throw ExceptionHelper.wrapServiceException(e); } } ConditionFunction();  ConditionFunction(String func); @Override boolean evaluate(AdaptrisMessage msg); @Override void init(); String getDefinition(); void setDefinition(String f);  }
@Test public void testScriptCondition_True() throws Exception { ConditionFunction condition = new ConditionFunction("function evaluateScript(message) { return true;}"); try { LifecycleHelper.initAndStart(condition); assertTrue(condition.evaluate(createMessage())); } finally { LifecycleHelper.stopAndClose(condition); } }
@Test public void testScriptCondition_False() throws Exception { ConditionFunction condition = new ConditionFunction( "function evaluateScript(message) { return false; }"); try { LifecycleHelper.initAndStart(condition); assertFalse(condition.evaluate(createMessage())); } finally { LifecycleHelper.stopAndClose(condition); } }
@Test public void testScriptConditionAgainstMetadata() throws Exception { ConditionFunction condition = new ConditionFunction( "function evaluateScript(message) { return message.getMetadataValue('key').equals('value');}"); try { LifecycleHelper.initAndStart(condition); assertTrue(condition.evaluate(createMessage())); assertFalse(condition.evaluate(createMessage("key", "anotherValue"))); } finally { LifecycleHelper.stopAndClose(condition); } }
@Test(expected = CoreException.class) public void testScriptCondition_WrongFunction() throws Exception { ConditionFunction condition = new ConditionFunction("function wrongFunctionName(message) {return true;}"); try { LifecycleHelper.initAndStart(condition); condition.evaluate(createMessage()); } finally { LifecycleHelper.stopAndClose(condition); } }