@Before
  public void setUp() throws Exception {
    MockLogAppender.setupLogging();
    MockDatabase db = new MockDatabase();
    DataSourceFactory.setInstance(db);

    RrdUtils.setStrategy(m_strategy);

    m_provisioner = new OpenNMSProvisioner();

    m_eventManager = new MockEventIpcManager();
    m_provisioner.setEventManager(m_eventManager);

    m_capsdConfig = new TestCapsdConfigManager(CAPSD_CONFIG);
    CapsdConfigFactory.setInstance(m_capsdConfig);

    m_pollerConfig = new TestPollerConfigManager(POLLER_CONFIG, "localhost", false);
    PollerConfigFactory.setInstance(m_pollerConfig);

    m_provisioner.setCapsdConfig(m_capsdConfig);
    m_provisioner.setPollerConfig(m_pollerConfig);

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

    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.setInstance(
        new CollectdConfigFactory(
            configStream, onmsSvrConfig.getServerName(), onmsSvrConfig.verifyServer()));
    configStream.close();

    JdbcTemplate jdbcTemplate = new JdbcTemplate(db);

    m_syncer = new JdbcCapsdDbSyncer();
    m_syncer.setJdbcTemplate(jdbcTemplate);
    m_syncer.setOpennmsServerConfig(OpennmsServerConfigFactory.getInstance());
    m_syncer.setCapsdConfig(m_capsdConfig);
    m_syncer.setPollerConfig(m_pollerConfig);
    m_syncer.setCollectdConfig(CollectdConfigFactory.getInstance());
    m_syncer.setNextSvcIdSql(db.getNextServiceIdStatement());
    m_syncer.afterPropertiesSet();

    m_syncer.syncServices();
    m_provisioner.setCapsdDbSyncer(m_syncer);
  }
  // TODO: Add test for exception on save of XML file
  @Test
  public void testAddServiceDatabase() throws Exception {
    expectUpdateEvent();
    m_mocks.replayAll();

    m_provisioner.addServiceDatabase(
        "MyDB",
        13,
        2001,
        54321,
        71,
        23456,
        "dbuser",
        "dbPasswd",
        "org.mydb.MyDriver",
        "jdbc://mydbhost:2");
    checkDatabaseConfiguration(
        "MyDB",
        "MyDB",
        13,
        2001,
        54321,
        71,
        23456,
        "dbuser",
        "dbPasswd",
        "org.mydb.MyDriver",
        "jdbc://mydbhost:2");

    m_mocks.verifyAll();
    verifyEvents();
  }
 @Test
 public void testReaddServiceTCP() throws Exception {
   testAddServiceTCP();
   expectUpdateEvent();
   m_provisioner.addServiceTCP("MyTCP", 5, 55, 555, 5555, 55555, 505, "AHOY");
   checkTCPConfiguration("MyTCP", "MyTCP", 5, 55, 555, 5555, 55555, 505, "AHOY");
   verifyEvents();
 }
  @Test
  public void testGetServiceConfigInvalidServiceId() {
    try {
      m_provisioner.getServiceConfiguration("default", "invalid");
      fail("Expected exception");
    } catch (IllegalArgumentException e) {

    }
  }
  @Test
  public void testGetServiceConfigNullServiceId() {
    try {
      m_provisioner.getServiceConfiguration("default", null);
      fail("Expected exception");
    } catch (NullPointerException e) {

    }
  }
  @Test
  public void testGetServiceConfigNullPkgName() {
    try {
      m_provisioner.getServiceConfiguration(null, "ICMP");
      fail("Expected exception");
    } catch (NullPointerException e) {

    }
  }
  @Test
  public void testAddServiceTCP() throws Exception {
    expectUpdateEvent();
    m_mocks.replayAll();

    m_provisioner.addServiceTCP("MyTCP", 4, 44, 444, 4444, 44444, 404, "HELO");
    checkTCPConfiguration("MyTCP", "MyTCP", 4, 44, 444, 4444, 44444, 404, "HELO");

    m_mocks.verifyAll();
    verifyEvents();
  }
  @Test
  public void testAddServiceDNS() throws Exception {
    expectUpdateEvent();
    m_mocks.replayAll();

    m_provisioner.addServiceDNS("MyDNS", 11, 1111, 11111, 111, 111111, 101, "www.opennms.org");
    checkDNSConfiguration("MyDNS", "MyDNS", 11, 1111, 11111, 111, 111111, 101, "www.opennms.org");

    m_mocks.verifyAll();
    verifyEvents();
  }
  @Test
  public void testAddServiceIcmp() throws Exception {

    m_provisioner.addServiceICMP("MyIcmp", 77, 1066, 36, 5, 1812);
    checkServiceConfiguration("MyIcmp", "MyIcmp", 77, 1066, 36, 5, 1812);

    TestPollerConfigManager mgr =
        new TestPollerConfigManager(m_pollerConfig.getXml(), "localhost", false);

    Package pkg = mgr.getPackage("MyIcmp");
    assertNotNull(pkg);
    assertNotNull(mgr.getServiceInPackage("MyIcmp", pkg));
  }
  private Map<String, Object> checkServiceConfiguration(
      String pkgName,
      String svcName,
      int retries,
      int timeout,
      int interval,
      int downtimeInterval,
      int downtimeDuration)
      throws Exception {
    Map<String, Object> configParams = m_provisioner.getServiceConfiguration(pkgName, svcName);
    assertEquals(svcName, configParams.get("serviceid"));
    assertEquals(Integer.valueOf(interval), configParams.get("interval"));
    assertEquals(Integer.valueOf(downtimeInterval), configParams.get("downtime_interval"));
    assertEquals(Integer.valueOf(downtimeDuration), configParams.get("downtime_duration"));
    assertNull(configParams.get("downtime_interval1"));
    assertNull(configParams.get("downtime_duration1"));
    assertEquals(Integer.valueOf(retries), configParams.get("retries"));
    assertEquals(Integer.valueOf(timeout), configParams.get("timeout"));

    TestPollerConfigManager mgr =
        new TestPollerConfigManager(m_pollerConfig.getXml(), "localhost", false);

    Package pkg = mgr.getPackage(pkgName);
    assertNotNull(pkg);
    Service svc = mgr.getServiceInPackage(svcName, pkg);
    assertNotNull(svc);
    assertEquals(interval, svc.getInterval());
    assertNotNull(
        "Unables to find monitor for svc " + svcName + " in origonal config",
        m_pollerConfig.getServiceMonitor(svcName));
    assertNotNull("Unable to find monitor for svc " + svcName, mgr.getServiceMonitor(svcName));

    assertNotNull(
        "Unable to find protocol plugin in capsdConfig for svc " + svcName,
        m_capsdConfig.getProtocolPlugin(svcName));
    assertNotNull(
        "Unable to find service table entry in capsdConfig for svc " + svcName,
        m_syncer.getServiceId(svcName));

    return configParams;
  }
  @Test
  public void testAddServiceHTTPS() throws Exception {
    expectUpdateEvent();
    m_mocks.replayAll();

    m_provisioner.addServiceHTTPS(
        "MyHTTPS",
        33,
        3333,
        33333,
        333,
        333333,
        "opennms.com",
        313,
        "303",
        "Secure",
        "/secure.html",
        "user",
        "pw",
        "");
    checkHTTPSConfiguration(
        "MyHTTPS",
        "MyHTTPS",
        33,
        3333,
        33333,
        333,
        333333,
        "opennms.com",
        313,
        "303",
        "Secure",
        "/secure.html",
        "user",
        "pw",
        "");

    m_mocks.verifyAll();
    verifyEvents();
  }
  @Test
  public void testAddServiceHTTPNoResponseCode() throws Exception {
    expectUpdateEvent();
    m_mocks.replayAll();

    m_provisioner.addServiceHTTP(
        "MyHTTP",
        22,
        2222,
        22222,
        222,
        222222,
        "opennms.com",
        212,
        "",
        "Home",
        "/index.html",
        "user",
        "pw",
        "");
    checkHTTPConfiguration(
        "MyHTTP",
        "MyHTTP",
        22,
        2222,
        22222,
        222,
        222222,
        "opennms.com",
        212,
        null,
        "Home",
        "/index.html",
        "user",
        "pw",
        "");

    m_mocks.verifyAll();
    verifyEvents();
  }