/** Checks whether the start events are generated. */
  @Test
  public void testStartEventGenerated() {
    final NopHandler<EventA> aHandler = new NopHandler<>();
    final NopHandler<EventB> bHandler = new NopHandler<>();
    final NopHandler<EventC> cHandler = new NopHandler<>();

    final Simulator sim =
        Simulator.builder()
            .setTickLength(1L)
            .setTimeUnit(SI.SECOND)
            .addModel(
                ScenarioController.builder(scenario)
                    .withEventHandler(EventA.class, aHandler)
                    .withEventHandler(EventB.class, bHandler)
                    .withEventHandler(EventC.class, cHandler))
            .build();

    controller = sim.getModelProvider().getModel(ScenarioController.class);

    final ListenerEventHistory leh = new ListenerEventHistory();
    controller.getEventAPI().addListener(leh);

    controller.clock.tick();

    assertThat(aHandler.getEvents()).containsExactly(EventA.create(0L));
    assertThat(bHandler.getEvents()).containsExactly(EventB.create(0L), EventB.create(0L));
    assertThat(cHandler.getEvents()).isEmpty();
    assertThat(leh.getEventTypeHistory())
        .containsExactly(
            EventType.SCENARIO_STARTED,
            EventType.SCENARIO_EVENT,
            EventType.SCENARIO_EVENT,
            EventType.SCENARIO_EVENT);
  }
  /** Test run of whole scenario. */
  @Test
  public void runningWholeScenario() {
    final NopHandler<?> handler = new NopHandler<>();
    @SuppressWarnings("unchecked")
    final Simulator sim =
        Simulator.builder()
            .setTickLength(1L)
            .setTimeUnit(SI.SECOND)
            .addModel(
                ScenarioController.builder(scenario)
                    .withNumberOfTicks(-1)
                    .withEventHandler(EventA.class, (NopHandler<EventA>) handler)
                    .withEventHandler(EventB.class, (NopHandler<EventB>) handler)
                    .withEventHandler(EventC.class, (NopHandler<EventC>) handler))
            .build();

    controller = sim.getModelProvider().getModel(ScenarioController.class);
    controller
        .getEventAPI()
        .addListener(
            new Listener() {
              @Override
              public void handleEvent(Event e) {
                if (e.getEventType() == ScenarioController.EventType.SCENARIO_FINISHED) {
                  sim.stop();
                }
              }
            });
    sim.start();
    assertThat(handler.getEvents()).hasSize(scenario.getEvents().size());
    assertThat(controller.isScenarioFinished()).isTrue();
  }
  /** Tests proper dispatching of setup events. */
  @Test
  public void testSetupEvents() {
    final Scenario s =
        Scenario.builder()
            .addEvent(EventA.create(0))
            .addEvent(EventB.create(-1))
            .addEvent(EventB.create(2))
            .addEvent(EventA.create(2))
            .addEvent(EventC.create(-1))
            .addEvent(EventC.create(100))
            .build();

    final NopHandler<?> handler = new NopHandler<>();

    @SuppressWarnings("unchecked")
    final Simulator sim =
        Simulator.builder()
            .setTickLength(1L)
            .setTimeUnit(SI.SECOND)
            .addModel(
                ScenarioController.builder(s)
                    .withNumberOfTicks(1)
                    .withEventHandler(EventA.class, (NopHandler<EventA>) handler)
                    .withEventHandler(EventB.class, (NopHandler<EventB>) handler)
                    .withEventHandler(EventC.class, (NopHandler<EventC>) handler))
            .build();

    final ListenerEventHistory leh = new ListenerEventHistory();
    final ScenarioController sc = sim.getModelProvider().getModel(ScenarioController.class);
    sc.getEventAPI().addListener(leh);
    sim.start();

    assertThat(handler.getEvents())
        .containsExactly(EventB.create(-1), EventC.create(-1), EventA.create(0))
        .inOrder();

    assertThat(leh.getEventTypeHistory())
        .containsExactly(SCENARIO_EVENT, SCENARIO_EVENT, SCENARIO_STARTED, SCENARIO_EVENT)
        .inOrder();
  }
  /** Tests a scenario with a limited number of ticks. */
  @Test
  public void finiteSimulation() {
    final NopHandler<?> handler = new NopHandler<>();
    @SuppressWarnings("unchecked")
    final Simulator sim =
        Simulator.builder()
            .setTickLength(1L)
            .setTimeUnit(SI.SECOND)
            .addModel(
                ScenarioController.builder(scenario)
                    .withEventHandler(EventA.class, (NopHandler<EventA>) handler)
                    .withEventHandler(EventB.class, (NopHandler<EventB>) handler)
                    .withEventHandler(EventC.class, (NopHandler<EventC>) handler)
                    .withNumberOfTicks(101))
            .build();

    final List<Long> ticks = new ArrayList<>();
    sim.addTickListener(
        new TickListener() {
          @Override
          public void tick(TimeLapse timeLapse) {
            ticks.add(timeLapse.getStartTime());
          }

          @Override
          public void afterTick(TimeLapse timeLapse) {}
        });

    final ScenarioController sc = sim.getModelProvider().getModel(ScenarioController.class);

    final ListenerEventHistory leh = new ListenerEventHistory();
    sc.getEventAPI().addListener(leh);
    assertThat(sc.isScenarioFinished()).isFalse();
    sim.start();

    assertThat(handler.getEvents())
        .containsExactly(
            EventA.create(0),
            EventB.create(0),
            EventB.create(0),
            EventA.create(1),
            EventC.create(5),
            EventC.create(100))
        .inOrder();

    assertThat(leh.getEventTypeHistory())
        .containsAllOf(SCENARIO_STARTED, SCENARIO_FINISHED)
        .inOrder();

    assertThat(sc.isScenarioFinished()).isTrue();
    sim.stop();
    final long before = sc.clock.getCurrentTime();
    sim.start(); // should have no effect

    assertThat(ticks).hasSize(101);

    assertThat(before).isEqualTo(sc.clock.getCurrentTime());
    final TimeLapse emptyTime = TimeLapseFactory.create(0, 1);
    emptyTime.consumeAll();
    sc.tick(emptyTime);
  }