Search in sources :

Example 1 with TriggeredBot

use of won.bot.framework.bot.base.TriggeredBot in project webofneeds by researchstudio-sat.

the class SecurityBotTests method runBot.

private void runBot(Class botClass) throws ExecutionException, InterruptedException {
    IntegrationtestBot bot = null;
    // create a bot instance and auto-wire it
    AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
    bot = (IntegrationtestBot) beanFactory.autowire(botClass, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    Object botBean = beanFactory.initializeBean(bot, "theBot");
    bot = (IntegrationtestBot) botBean;
    // the bot also needs a trigger so its act() method is called regularly.
    // (there is no trigger bean in the context)
    PeriodicTrigger trigger = new PeriodicTrigger(500, TimeUnit.MILLISECONDS);
    trigger.setInitialDelay(100);
    ((TriggeredBot) bot).setTrigger(trigger);
    // adding the bot to the bot manager will cause it to be initialized.
    // at that point, the trigger starts.
    botManager.addBot(bot);
    final SettableListenableFuture<TestFinishedEvent> futureTestResult = new SettableListenableFuture();
    final EventListenerContext ctx = bot.getExposedEventListenerContext();
    // action for setting the future when we get a test result
    EventBotAction setFutureAction = new SetFutureAction(ctx, futureTestResult);
    // action for setting the future when an error occurs
    EventBotAction setFutureFromErrorAction = new SetFutureFromErrorEventAction(ctx, futureTestResult, bot);
    // add a listener for test success
    ctx.getEventBus().subscribe(TestPassedEvent.class, new ActionOnEventListener(ctx, setFutureAction));
    // add a listener for test failure
    ctx.getEventBus().subscribe(TestFailedEvent.class, new ActionOnEventListener(ctx, setFutureAction));
    // add a listener for errors
    ctx.getEventBus().subscribe(ErrorEvent.class, new ActionOnEventListener(ctx, setFutureFromErrorAction));
    // wait for the result
    TestFinishedEvent result = futureTestResult.get();
    if (result instanceof TestFailedEvent) {
        Assert.fail(((TestFailedEvent) result).getMessage());
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) IntegrationtestBot(won.bot.IntegrationtestBot) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) TriggeredBot(won.bot.framework.bot.base.TriggeredBot) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) TestFailedEvent(won.bot.framework.eventbot.event.impl.test.TestFailedEvent) TestFinishedEvent(won.bot.framework.eventbot.event.impl.test.TestFinishedEvent) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventBotAction(won.bot.framework.eventbot.action.EventBotAction) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction)

Example 2 with TriggeredBot

use of won.bot.framework.bot.base.TriggeredBot in project webofneeds by researchstudio-sat.

the class BotRunnerApp method main.

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("arguments: [bot class name]");
        System.exit(1);
    }
    String botClass = args[0];
    SpringApplication app = new SpringApplication(new Object[] { "classpath:/spring/app/botRunner.xml" });
    app.setWebEnvironment(false);
    ConfigurableApplicationContext applicationContext = app.run(args);
    Bot bot = null;
    // create a bot instance and auto-wire it
    AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
    bot = (Bot) beanFactory.autowire(Class.forName(botClass), AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    Object botBean = beanFactory.initializeBean(bot, "theBot");
    bot = (Bot) botBean;
    // (there is no trigger bean in the context)
    if (bot instanceof TriggeredBot) {
        PeriodicTrigger trigger = new PeriodicTrigger(5000, TimeUnit.MILLISECONDS);
        trigger.setInitialDelay(1000);
        ((TriggeredBot) bot).setTrigger(trigger);
    }
    BotManager botManager = (BotManager) applicationContext.getBean("botManager");
    // adding the bot to the bot manager will cause it to be initialized.
    // at that point, the trigger starts.
    botManager.addBot(bot);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BotManager(won.bot.framework.manager.BotManager) SpringApplication(org.springframework.boot.SpringApplication) TriggeredBot(won.bot.framework.bot.base.TriggeredBot) Bot(won.bot.framework.bot.Bot) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) TriggeredBot(won.bot.framework.bot.base.TriggeredBot) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger)

Aggregations

AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)2 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)2 TriggeredBot (won.bot.framework.bot.base.TriggeredBot)2 SpringApplication (org.springframework.boot.SpringApplication)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 SettableListenableFuture (org.springframework.util.concurrent.SettableListenableFuture)1 IntegrationtestBot (won.bot.IntegrationtestBot)1 Bot (won.bot.framework.bot.Bot)1 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)1 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 EventBotAction (won.bot.framework.eventbot.action.EventBotAction)1 TestFailedEvent (won.bot.framework.eventbot.event.impl.test.TestFailedEvent)1 TestFinishedEvent (won.bot.framework.eventbot.event.impl.test.TestFinishedEvent)1 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)1 BotManager (won.bot.framework.manager.BotManager)1