public void testOneMatchingSpec() throws Exception { OnmsIpInterface iface = getInterface(); setupCollector("SNMP", true); setupInterface(iface); setupTransactionManager(); expect(m_collectdConfig.getPackages()) .andReturn(Collections.singletonList(getCollectionPackageThatMatchesSNMP())); expect(m_collectdConfigFactory.interfaceInPackage(iface, getCollectionPackageThatMatchesSNMP())) .andReturn(true); m_easyMockUtils.replayAll(); assertEquals("scheduler entry count", 0, m_scheduler.getEntryCount()); m_collectd.afterPropertiesSet(); m_collectd.start(); m_scheduler.next(); assertEquals("scheduler entry count", 1, m_scheduler.getEntryCount()); m_scheduler.next(); m_collectd.stop(); m_easyMockUtils.verifyAll(); }
public void testCreate() throws Exception { setupCollector("SNMP", false); setupTransactionManager(); // Use a mock scheduler to track calls to the Collectd scheduler Scheduler m_scheduler = m_easyMockUtils.createMock(Scheduler.class); m_collectd.setScheduler(m_scheduler); // Expect one task to be scheduled m_scheduler.schedule(eq(0L), isA(ReadyRunnable.class)); // Expect the scheduler to be started and stopped during Collectd // start() and stop() m_scheduler.start(); m_scheduler.stop(); m_easyMockUtils.replayAll(); // Initialize Collectd m_collectd.afterPropertiesSet(); // Start and stop collectd m_collectd.start(); m_collectd.stop(); m_easyMockUtils.verifyAll(); }
/** @throws Exception */ public void testNoMatchingSpecs() throws Exception { setupCollector("SNMP", false); expect(m_ipIfDao.findByServiceType("SNMP")).andReturn(new ArrayList<OnmsIpInterface>(0)); setupTransactionManager(); m_easyMockUtils.replayAll(); m_collectd.afterPropertiesSet(); m_collectd.start(); m_scheduler.next(); assertEquals(0, m_scheduler.getEntryCount()); m_collectd.stop(); m_easyMockUtils.verifyAll(); }
/** Add a dummy transaction manager that has mock calls to commit() and rollback() */ private void setupTransactionManager() { PlatformTransactionManager m_transactionManager = m_easyMockUtils.createMock(PlatformTransactionManager.class); TransactionTemplate transactionTemplate = new TransactionTemplate(m_transactionManager); m_collectd.setTransactionTemplate(transactionTemplate); expect(m_transactionManager.getTransaction(isA(TransactionDefinition.class))) .andReturn(new SimpleTransactionStatus()) .anyTimes(); m_transactionManager.rollback(isA(TransactionStatus.class)); expectLastCall().anyTimes(); m_transactionManager.commit(isA(TransactionStatus.class)); expectLastCall().anyTimes(); }
private void setupCollector(String svcName, boolean successfulInit) throws CollectionInitializationException { ServiceCollector svcCollector = m_easyMockUtils.createMock(ServiceCollector.class); if (successfulInit) { svcCollector.initialize(isA(CollectionAgent.class), isAMap(String.class, Object.class)); } svcCollector.initialize(Collections.<String, String>emptyMap()); MockServiceCollector.setDelegate(svcCollector); // Tell the config to use the MockServiceCollector for the specified service Collector collector = new Collector(); collector.setService(svcName); collector.setClassName(MockServiceCollector.class.getName()); m_collectdConfigFactory = m_easyMockUtils.createMock(CollectdConfigFactory.class); m_collectdConfig = m_easyMockUtils.createMock(CollectdConfiguration.class); expect(m_collectdConfigFactory.getCollectdConfig()).andReturn(m_collectdConfig).anyTimes(); expect(m_collectdConfig.getCollectors()) .andReturn(Collections.singletonList(collector)) .anyTimes(); expect(m_collectdConfig.getThreads()).andReturn(1).anyTimes(); m_collectd.setCollectdConfigFactory(m_collectdConfigFactory); }
@Override protected void setUp() throws Exception { EventIpcManager m_eventIpcManager; NodeDao m_nodeDao; MockLogAppender.setupLogging(); Resource threshdResource = new ClassPathResource("/etc/thresholds.xml"); File homeDir = threshdResource.getFile().getParentFile().getParentFile(); System.setProperty("opennms.home", homeDir.getAbsolutePath()); // Test setup m_eventIpcManager = m_easyMockUtils.createMock(EventIpcManager.class); EventIpcManagerFactory.setIpcManager(m_eventIpcManager); m_nodeDao = m_easyMockUtils.createMock(NodeDao.class); m_ipIfDao = m_easyMockUtils.createMock(IpInterfaceDao.class); m_scheduler = new MockScheduler(); m_eventIpcManager.addEventListener(isA(EventListener.class)); expectLastCall().anyTimes(); m_eventIpcManager.addEventListener(isA(EventListener.class), isACollection(String.class)); expectLastCall().anyTimes(); m_eventIpcManager.addEventListener(isA(EventListener.class), isA(String.class)); expectLastCall().anyTimes(); m_eventIpcManager.removeEventListener(isA(EventListener.class)); expectLastCall().anyTimes(); // MockNetwork 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.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.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"); // // MockDatabase m_db = new MockDatabase(); // m_db.populate(m_network); // // DataSourceFactory.setInstance(m_db); // Mock the FilterDao without using EasyMockUtils so that it can be verified separately m_filterDao = EasyMock.createMock(FilterDao.class); List<InetAddress> allIps = new ArrayList<InetAddress>(); allIps.add(addr("192.168.1.1")); allIps.add(addr("192.168.1.2")); allIps.add(addr("192.168.1.3")); allIps.add(addr("192.168.1.4")); allIps.add(addr("192.168.1.5")); expect(m_filterDao.getActiveIPAddressList("IPADDR IPLIKE *.*.*.*")) .andReturn(allIps) .anyTimes(); expect(m_filterDao.getActiveIPAddressList("IPADDR IPLIKE 1.1.1.1")) .andReturn(new ArrayList<InetAddress>(0)) .anyTimes(); EasyMock.replay(m_filterDao); FilterDaoFactory.setInstance(m_filterDao); // This call will also ensure that the poll-outages.xml file can parse IPv4 // and IPv6 addresses. Resource resource = new ClassPathResource("etc/poll-outages.xml"); PollOutagesConfigFactory factory = new PollOutagesConfigFactory(resource); factory.afterPropertiesSet(); PollOutagesConfigFactory.setInstance(factory); final MockTransactionTemplate transTemplate = new MockTransactionTemplate(); transTemplate.afterPropertiesSet(); m_collectd = new Collectd(); m_collectd.setEventIpcManager(m_eventIpcManager); // m_collectd.setCollectdConfigFactory(m_collectdConfigFactory); m_collectd.setNodeDao(m_nodeDao); m_collectd.setIpInterfaceDao(m_ipIfDao); m_collectd.setFilterDao(m_filterDao); m_collectd.setScheduler(m_scheduler); m_collectd.setTransactionTemplate(transTemplate); // m_collectd.afterPropertiesSet(); ThresholdingConfigFactory.setInstance( new ThresholdingConfigFactory( ConfigurationTestUtils.getInputStreamForConfigFile("thresholds.xml"))); }