private void anticipateDown(MockElement element, boolean force) {
   if (force || !element.getPollStatus().equals(PollStatus.down())) {
     Event event = element.createDownEvent();
     m_anticipator.anticipateEvent(event);
     m_outageAnticipator.anticipateOutageOpened(element, event);
   }
 }
  @Test
  public void testNodeGainedDynamicService() throws Exception {
    m_pollerConfig.setNodeOutageProcessingEnabled(true);

    startDaemons();

    TestCapsdConfigManager capsdConfig = new TestCapsdConfigManager(CAPSD_CONFIG);

    InputStream configStream =
        ConfigurationTestUtils.getInputStreamForConfigFile("opennms-server.xml");
    OpennmsServerConfigFactory onmsSvrConfig = new OpennmsServerConfigFactory(configStream);
    configStream.close();

    configStream = ConfigurationTestUtils.getInputStreamForConfigFile("database-schema.xml");
    DatabaseSchemaConfigFactory.setInstance(new DatabaseSchemaConfigFactory(configStream));
    configStream.close();

    configStream =
        ConfigurationTestUtils.getInputStreamForResource(
            this, "/org/opennms/netmgt/capsd/collectd-configuration.xml");
    CollectdConfigFactory collectdConfig =
        new CollectdConfigFactory(
            configStream, onmsSvrConfig.getServerName(), onmsSvrConfig.verifyServer());
    configStream.close();

    JdbcTemplate jdbcTemplate = new JdbcTemplate(m_db);

    JdbcCapsdDbSyncer syncer = new JdbcCapsdDbSyncer();
    syncer.setJdbcTemplate(jdbcTemplate);
    syncer.setOpennmsServerConfig(onmsSvrConfig);
    syncer.setCapsdConfig(capsdConfig);
    syncer.setPollerConfig(m_pollerConfig);
    syncer.setCollectdConfig(collectdConfig);
    syncer.setNextSvcIdSql(m_db.getNextServiceIdStatement());
    syncer.afterPropertiesSet();

    OpenNMSProvisioner provisioner = new OpenNMSProvisioner();
    provisioner.setPollerConfig(m_pollerConfig);
    provisioner.setCapsdConfig(capsdConfig);
    provisioner.setCapsdDbSyncer(syncer);

    provisioner.setEventManager(m_eventMgr);
    provisioner.addServiceDNS("MyDNS", 3, 100, 1000, 500, 3000, 53, "www.opennms.org");

    assertNotNull("The service id for MyDNS is null", m_db.getServiceID("MyDNS"));
    MockUtil.println("The service id for MyDNS is: " + m_db.getServiceID("MyDNS").toString());

    m_anticipator.reset();

    testSendNodeGainedService("MyDNS", "HTTP");
  }
 private void verifyAnticipated(long millis, boolean checkUnanticipated) {
   // make sure the down events are received
   MockEventUtil.printEvents(
       "Events we're still waiting for: ", m_anticipator.waitForAnticipated(millis));
   assertTrue("Expected events not forthcoming", m_anticipator.waitForAnticipated(0).isEmpty());
   if (checkUnanticipated) {
     sleep(2000);
     MockEventUtil.printEvents("Unanticipated: ", m_anticipator.unanticipatedEvents());
     assertEquals("Received unexpected events", 0, m_anticipator.unanticipatedEvents().size());
   }
   sleep(1000);
   m_eventMgr.finishProcessingEvents();
   assertEquals(
       "Wrong number of outages opened",
       m_outageAnticipator.getExpectedOpens(),
       m_outageAnticipator.getActualOpens());
   assertEquals(
       "Wrong number of outages in outage table",
       m_outageAnticipator.getExpectedOutages(),
       m_outageAnticipator.getActualOutages());
   assertTrue(
       "Created outages don't match the expected outages", m_outageAnticipator.checkAnticipated());
 }
  @Test
  public void testNodeLostServiceIncludesReason() throws Exception {
    MockService element = m_network.getService(1, "192.168.1.1", "SMTP");
    String expectedReason = "Oh No!! An Outage!!";
    startDaemons();

    resetAnticipated();
    anticipateDown(element);

    MockUtil.println("Bringing down element: " + element);
    element.bringDown(expectedReason);
    MockUtil.println("Finished bringing down element: " + element);

    verifyAnticipated(8000);

    Collection<Event> receivedEvents = m_anticipator.getAnticipatedEventsRecieved();

    assertEquals(1, receivedEvents.size());

    Event event = receivedEvents.iterator().next();

    assertEquals(expectedReason, EventUtils.getParm(event, EventConstants.PARM_LOSTSERVICE_REASON));
  }
 private void resetAnticipated() {
   m_anticipator.reset();
   m_outageAnticipator.reset();
 }