Search in sources :

Example 1 with CountdownLatchAction

use of won.integrationtest.support.CountdownLatchAction in project webofneeds by researchstudio-sat.

the class AbstractBotBasedTest method runTestWithAsserts.

protected void runTestWithAsserts(Consumer<EventListenerContext> botInitializer, Runnable asserter) throws Exception {
    logger.debug("preparing bot test in runTestWithAsserts(..)");
    MutableStringHolder errorMessage = new MutableStringHolder();
    bot.setInitializer(ctx -> {
        bot.newCountDownLatch(1);
        EventBus bus = ctx.getEventBus();
        bus.clear();
        // now, add a listener to the WorkDoneEvent.
        // its only purpose is to trip the CyclicBarrier instance that
        // the test method is waiting on
        bus.subscribe(TestPassedEvent.class, new ActionOnEventListener(ctx, new CountdownLatchAction(ctx, bot.getCountDownLatch())));
        bus.subscribe(TestFailedEvent.class, new ActionOnEventListener(ctx, new MultipleActions(ctx, false, new BaseEventBotAction(ctx) {

            @Override
            protected void doRun(Event event, EventListener executingListener) throws Exception {
                if (event instanceof TestFailedEvent) {
                    errorMessage.set(((TestFailedEvent) event).getMessage());
                    if (errorMessage.isEmpty()) {
                        errorMessage.set("[no error message provided]");
                    }
                }
            }
        }, new CountdownLatchAction(ctx, bot.getCountDownLatch()))));
        // now run test-specific initializer
        if (botInitializer != null) {
            botInitializer.accept(ctx);
        }
    });
    // possibly, the bot is already running (the SpringAwareBotManager finds and
    // initializes it)
    // make sure we can re-initialize it here
    bot.shutdown();
    // re-initialize
    bot.initialize();
    logger.debug("bot re-initialized");
    logger.debug("waiting for test to finish...");
    bot.getCountDownLatch().await();
    if (errorMessage.isPresent()) {
        logger.debug("test failed");
        Assert.fail(errorMessage.get());
    }
    logger.debug("executing asserts (if any)");
    // execute any asserts
    asserter.run();
    logger.debug("test passed");
}
Also used : MultipleActions(won.bot.framework.eventbot.action.impl.MultipleActions) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) TestPassedEvent(won.bot.framework.eventbot.event.impl.test.TestPassedEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) DeliveryResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.DeliveryResponseEvent) Event(won.bot.framework.eventbot.event.Event) SuccessResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent) TestFailedEvent(won.bot.framework.eventbot.event.impl.test.TestFailedEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) CountdownLatchAction(won.integrationtest.support.CountdownLatchAction) TestFailedEvent(won.bot.framework.eventbot.event.impl.test.TestFailedEvent)

Aggregations

BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 MultipleActions (won.bot.framework.eventbot.action.impl.MultipleActions)1 EventBus (won.bot.framework.eventbot.bus.EventBus)1 Event (won.bot.framework.eventbot.event.Event)1 TestFailedEvent (won.bot.framework.eventbot.event.impl.test.TestFailedEvent)1 TestPassedEvent (won.bot.framework.eventbot.event.impl.test.TestPassedEvent)1 DeliveryResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.DeliveryResponseEvent)1 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)1 SuccessResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent)1 EventListener (won.bot.framework.eventbot.listener.EventListener)1 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)1 CountdownLatchAction (won.integrationtest.support.CountdownLatchAction)1