gt: 		MockService svc = iface.getService("SMTP");
context: /*******************************************************************************
   * This file is part of OpenNMS(R).
   *
   * Copyright (C) 2004-2014 The OpenNMS Group, Inc.
   * OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
   *
   * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
   *
   * OpenNMS(R) is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License as published
   * by the Free Software Foundation, either version 3 of the License,
   * or (at your option) any later version.
   *
   * OpenNMS(R) is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Affero General Public License for more details.
   *
   * You should have received a copy of the GNU Affero General Public License
   * along with OpenNMS(R).  If not, see:
   *      http://www.gnu.org/licenses/
   *
   * For more information contact:
   *     OpenNMS(R) Licensing <license@opennms.org>
   *     http://www.opennms.org/
   *     http://www.opennms.com/
   *******************************************************************************/
  
  package org.opennms.netmgt.poller;
  
  import static org.junit.Assert.assertEquals;
  import static org.junit.Assert.assertFalse;
  import static org.junit.Assert.assertNotNull;
  import static org.junit.Assert.assertNull;
  import static org.junit.Assert.assertTrue;
  import static org.mockito.Mockito.mock;
  
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Timestamp;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Properties;
  
  import org.junit.After;
  import org.junit.Before;
  import org.junit.Ignore;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.opennms.core.db.DataSourceFactory;
  import org.opennms.core.test.MockLogAppender;
  import org.opennms.core.test.OpenNMSJUnit4ClassRunner;
  import org.opennms.core.test.db.MockDatabase;
  import org.opennms.core.test.db.TemporaryDatabaseAware;
  import org.opennms.core.test.db.annotations.JUnitTemporaryDatabase;
  import org.opennms.core.utils.Querier;
  import org.opennms.netmgt.config.poller.Package;
  import org.opennms.netmgt.dao.api.MonitoredServiceDao;
  import org.opennms.netmgt.dao.mock.MockEventIpcManager;
  import org.opennms.netmgt.eventd.AbstractEventUtil;
  import org.opennms.netmgt.events.api.EventConstants;
  import org.opennms.netmgt.icmp.proxy.LocationAwarePingClient;
  import org.opennms.netmgt.mock.MockElement;
  import org.opennms.netmgt.mock.MockEventUtil;
  import org.opennms.netmgt.mock.MockInterface;
  import org.opennms.netmgt.mock.MockNetwork;
  import org.opennms.netmgt.mock.MockNode;
  import org.opennms.netmgt.mock.MockPollerConfig;
  import org.opennms.netmgt.mock.MockService;
  import org.opennms.netmgt.mock.MockService.SvcMgmtStatus;
  import org.opennms.netmgt.mock.MockVisitor;
  import org.opennms.netmgt.mock.MockVisitorAdapter;
  import org.opennms.netmgt.mock.OutageAnticipator;
  import org.opennms.netmgt.mock.PollAnticipator;
  import org.opennms.netmgt.model.events.EventUtils;
  import org.opennms.netmgt.poller.pollables.PollableNetwork;
  import org.opennms.netmgt.xml.event.Event;
  import org.opennms.test.JUnitConfigurationEnvironment;
  import org.opennms.test.mock.MockUtil;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.transaction.support.TransactionTemplate;
  
  @RunWith(OpenNMSJUnit4ClassRunner.class)
  @ContextConfiguration(locations={
          "classpath:/META-INF/opennms/applicationContext-soa.xml",
          "classpath:/META-INF/opennms/applicationContext-dao.xml",
          "classpath:/META-INF/opennms/applicationContext-commonConfigs.xml",
          "classpath:/META-INF/opennms/applicationContext-minimal-conf.xml",
          "classpath*:/META-INF/opennms/component-dao.xml",
          "classpath*:/META-INF/opennms/component-service.xml",
          "classpath:/META-INF/opennms/applicationContext-daemon.xml",
          "classpath:/META-INF/opennms/applicationContext-eventUtil.xml",
          "classpath:/META-INF/opennms/mockEventIpcManager.xml",
          "classpath:/META-INF/opennms/applicationContext-pinger.xml",
          "classpath:/META-INF/opennms/applicationContext-rpc-client-mock.xml",
          "classpath:/META-INF/opennms/applicationContext-rpc-poller.xml",
  
          // Override the default QueryManager with the DAO version
          "classpath:/META-INF/opennms/applicationContext-pollerdTest.xml"
  })
  @JUnitConfigurationEnvironment(systemProperties={
          "org.opennms.netmgt.icmp.pingerClass=org.opennms.netmgt.icmp.jna.JnaPinger"
  })
  @JUnitTemporaryDatabase(tempDbClass=MockDatabase.class,reuseDatabase=false)
  public class PollerQueryManagerDaoIT implements TemporaryDatabaseAware<MockDatabase> {
  
  	private Poller m_poller;
  
  	private MockNetwork m_network;
  
  	private MockDatabase m_db;
  
  	private MockPollerConfig m_pollerConfig;
  
  	@Autowired
  	private MockEventIpcManager m_eventMgr;
  
  	private boolean m_daemonsStarted = false;
  
  	private OutageAnticipator m_outageAnticipator;
  
  	@Autowired
  	private QueryManager m_queryManager;
  	
  	@Autowired
  	private MonitoredServiceDao m_monitoredServiceDao;
  
  	@Autowired
  	private TransactionTemplate m_transactionTemplate;
  
  	@Autowired
  	private LocationAwarePollerClient m_locationAwarePollerClient;
  
  	private LocationAwarePingClient m_locationAwarePingClient;
  
  	@Override
  	public void setTemporaryDatabase(MockDatabase database) {
  		m_db = database;
  	}
  
  	//
  	// SetUp and TearDown
  	//
  
  	@Before
  	public void setUp() throws Exception {
  
  		MockUtil.println("------------ Begin Test  --------------------------");
  		MockLogAppender.setupLogging();
  
  		m_network = new MockNetwork();
  		m_network.setCriticalService("ICMP");
  		m_network.addNode(1, "Router");
  		m_network.addInterface("192.168.1.1");
  		m_network.addService("ICMP");
  		m_network.addService("SMTP");
  		m_network.addService("SNMP");
  		m_network.addInterface("192.168.1.2");
  		m_network.addService("ICMP");
  		m_network.addService("SMTP");
  		m_network.addNode(2, "Server");
  		m_network.addInterface("192.168.1.3");
  		m_network.addService("ICMP");
  		m_network.addService("HTTP");
  		m_network.addService("SMTP");
  		m_network.addService("SNMP");
  		m_network.addNode(3, "Firewall");
  		m_network.addInterface("192.168.1.4");
  		m_network.addService("SMTP");
  		m_network.addService("HTTP");
  		m_network.addInterface("192.168.1.5");
  		m_network.addService("SMTP");
  		m_network.addService("HTTP");
  		m_network.addNode(4, "DownNode");
  		m_network.addInterface("192.168.1.6");
  		m_network.addService("SNMP");
  //		m_network.addInterface("fe80:0000:0000:0000:0231:f982:0123:4567");
  //		m_network.addService("SNMP");
  
  		m_db.populate(m_network);
  		DataSourceFactory.setInstance(m_db);
  
  		m_pollerConfig = new MockPollerConfig(m_network);
  		m_pollerConfig.setNextOutageIdSql(m_db.getNextOutageIdStatement());
  		m_pollerConfig.setNodeOutageProcessingEnabled(true);
  		m_pollerConfig.setCriticalService("ICMP");
  		m_pollerConfig.addPackage("TestPackage");
  		m_pollerConfig.addDowntime(1000L, 0L, -1L, false);
  		m_pollerConfig.setDefaultPollInterval(1000L);
  		m_pollerConfig.populatePackage(m_network);
  		m_pollerConfig.addPackage("TestPkg2");
  		m_pollerConfig.addDowntime(1000L, 0L, -1L, false);
  		m_pollerConfig.setDefaultPollInterval(2000L);
  		m_pollerConfig.addService(m_network.getService(2, "192.168.1.3", "HTTP"));
  
  		m_outageAnticipator = new OutageAnticipator(m_db);
  
  		m_eventMgr = new MockEventIpcManager();
  		m_eventMgr.setEventWriter(m_db);
  		m_eventMgr.addEventListener(m_outageAnticipator);
  		m_eventMgr.setSynchronous(false);
  
  		m_locationAwarePingClient = mock(LocationAwarePingClient.class);
  
  		DefaultPollContext pollContext = new DefaultPollContext();
  		pollContext.setEventManager(m_eventMgr);
  		pollContext.setLocalHostName("localhost");
  		pollContext.setName("Test.DefaultPollContext");
  		pollContext.setPollerConfig(m_pollerConfig);
  		pollContext.setQueryManager(m_queryManager);
  		pollContext.setLocationAwarePingClient(m_locationAwarePingClient);
  
  		PollableNetwork network = new PollableNetwork(pollContext);
  
  		m_poller = new Poller();
          m_poller.setMonitoredServiceDao(m_monitoredServiceDao);
          m_poller.setTransactionTemplate(m_transactionTemplate);
  		m_poller.setEventIpcManager(m_eventMgr);
  		m_poller.setNetwork(network);
  		m_poller.setQueryManager(m_queryManager);
  		m_poller.setPollerConfig(m_pollerConfig);
  		m_poller.setPollOutagesConfig(m_pollerConfig);
  		m_poller.setLocationAwarePollerClient(m_locationAwarePollerClient);
  	}
  
  	@After
  	public void tearDown() throws Exception {
  		m_eventMgr.finishProcessingEvents();
  		stopDaemons();
  		sleep(200);
  		m_db.drop();
  		MockUtil.println("------------ End Test  --------------------------");
  	}
  
  	//
  	// Tests
  	//
      @Test
      public void testIsRemotePackage() {
      	Properties p = new Properties();
          p.setProperty("org.opennms.netmgt.ConfigFileConstants", "ERROR");
      	MockLogAppender.setupLogging(p);
          Package pkg = new Package();
          pkg.setName("SFO");
          pkg.setRemote(true);
          Poller poller = new Poller();
          assertFalse(poller.pollableServiceInPackage(null, null, pkg));
          poller = null;
      }
  
      @Test
      public void testNullInterfaceOnNodeDown() {
          // NODE processing = true;
          m_pollerConfig.setNodeOutageProcessingEnabled(true);
          MockNode node = m_network.getNode(2);
          MockService icmpService = m_network.getService(2, "192.168.1.3", "ICMP");
          MockService smtpService = m_network.getService(2, "192.168.1.3", "SMTP");
          MockService snmpService = m_network.getService(2, "192.168.1.3", "SNMP");
  
          // start the poller
          startDaemons();
  
          anticipateDown(node);
  
          icmpService.bringDown();
          smtpService.bringDown();
          snmpService.bringDown();
  
          verifyAnticipated(10000);
  
          // node is down at this point
          boolean foundNodeDown = false;
          for (final Event event : m_eventMgr.getEventAnticipator().getAnticipatedEventsReceived()) {
              if (EventConstants.NODE_DOWN_EVENT_UEI.equals(event.getUei())) {
                  foundNodeDown = true;
                  assertNull(event.getInterfaceAddress());
              }
          }
          assertTrue(foundNodeDown);
      }
  
      @Test
      @Ignore
  	public void testBug1564() {
  		// NODE processing = true;
  		m_pollerConfig.setNodeOutageProcessingEnabled(true);
  		MockNode node = m_network.getNode(2);
  		MockService icmpService = m_network.getService(2, "192.168.1.3", "ICMP");
  		MockService smtpService = m_network.getService(2, "192.168.1.3", "SMTP");
  		MockService snmpService = m_network.getService(2, "192.168.1.3", "SNMP");
  
  		// start the poller
  		startDaemons();
  
  		//
  		// Bring Down the HTTP service and expect nodeLostService Event
  		//
  
  		resetAnticipated();
  		anticipateDown(snmpService);
  		// One service works fine
  		snmpService.bringDown();
  
  		verifyAnticipated(10000);
  
  		// Now we simulate the restart, the node
  		// looses all three at the same time
  
  		resetAnticipated();
  		anticipateDown(node);
  
  		icmpService.bringDown();
  		smtpService.bringDown();
  		snmpService.bringDown();
  
  		verifyAnticipated(10000);
  		
  		anticipateDown(smtpService);
  		verifyAnticipated(10000);
  		anticipateDown(snmpService);
  		verifyAnticipated(10000);
  
  		// This is to simulate a restart,
  		// where I turn off the node behaviour
  
  		m_pollerConfig.setNodeOutageProcessingEnabled(false);
  
  		anticipateUp(snmpService);
  		snmpService.bringUp();
  
  		verifyAnticipated(10000);
  
  		anticipateUp(smtpService);
  		smtpService.bringUp();
  
  		verifyAnticipated(10000);
  
  		// Another restart - let's see if this will work?
  
  		m_pollerConfig.setNodeOutageProcessingEnabled(true);
  		// So everything is down, now
  		// SNMP will regain and SMTP will regain
  		// will the node come up?
  
  		
  		smtpService.bringDown();
  		
  		anticipateUp(smtpService);
  		smtpService.bringUp();
  
  		verifyAnticipated(10000,true);
  
  		anticipateUp(snmpService);
  		snmpService.bringUp();
  
  		verifyAnticipated(10000);
  
  	}
  
      @Test
  	public void testBug709() {
  
  		m_pollerConfig.setNodeOutageProcessingEnabled(true);
  
  		MockNode node = m_network.getNode(2);
  		MockService icmpService = m_network
  				.getService(2, "192.168.1.3", "ICMP");
  		MockService httpService = m_network
  				.getService(2, "192.168.1.3", "HTTP");
  
  		// start the poller
  		startDaemons();
  
  		//
  		// Bring Down the HTTP service and expect nodeLostService Event
  		//
  
  		resetAnticipated();
  		anticipateDown(httpService);
  
  		// bring down the HTTP service
  		httpService.bringDown();
  
  		verifyAnticipated(10000);
  
  		//
  		// Bring Down the ICMP (on the only interface on the node) now expect
  		// nodeDown
  		// only.
  		//
  
  		resetAnticipated();
  		anticipateDown(node);
  
  		// bring down the ICMP service
  		icmpService.bringDown();
  
  		// make sure the down events are received
  		// verifyAnticipated(10000);
  		sleep(5000);
  		//
  		// Bring up both the node and the httpService at the same time. Expect
  		// both a nodeUp and a nodeRegainedService
  		//
  
  		resetAnticipated();
  		// the order matters here
  		anticipateUp(httpService);
  		anticipateUp(node);
  
  		// bring up all the services on the node
  		node.bringUp();
  
  		// make sure the down events are received
  		verifyAnticipated(10000);
  
  	}
  
  	private void resetAnticipated() {
  		m_eventMgr.getEventAnticipator().reset();
  		m_outageAnticipator.reset();
  	}
  
      @Test
  	public void testNodeLostServiceWithReason() {
  		m_pollerConfig.setNodeOutageProcessingEnabled(true);
  
  		MockService svc = m_network.getService(1, "192.168.1.1", "ICMP");
  		Event e = svc.createDownEvent();
  		String reasonParm = "eventReason";
  		String val = (String) AbstractEventUtil.getInstance().getNamedParmValue("parm[" + reasonParm + "]", e);
  		assertEquals("Service Not Responding.", val);
  	}
  
      @Test
  	public void testCritSvcStatusPropagation() {
  		m_pollerConfig.setNodeOutageProcessingEnabled(true);
  
  		MockNode node = m_network.getNode(1);
  
  		anticipateDown(node);
  
  		startDaemons();
  
  		bringDownCritSvcs(node);
  
  		verifyAnticipated(8000);
  	}
  
      @Test
  	public void testInterfaceWithNoCriticalService() {
  		m_pollerConfig.setNodeOutageProcessingEnabled(true);
  
  		MockInterface iface = m_network.getInterface(3, "192.168.1.4");
  