Search in sources :

Example 11 with MockDatabase

use of org.opennms.core.test.db.MockDatabase in project opennms by OpenNMS.

the class ThresholdingVisitorIT method testReloadThreshdConfig.

/*
     * Use case A:
     * 
     * I have 5 nodes. The current threshd-config matches 2 of them. The new threshd-config will match the other 2, by
     * adding a new threshold package. For example: n1 y n2 belongs to category CAT1, n2, n3 y n4 belongs to category CAT2.
     * The initial configuration is related with CAT1 and the new package is related with CAT2. In both cases, n5 should
     * never match any threshold package.
     * 
     * Use case B:
     * 
     * I have a package with SNMP thresholds. Then update the package by adding HTTP thresholds. The test node should
     * support both services.
     * 
     * IMPORTANT:
     *     The reload should be do it first, then notify all visitors (I think this is the current behavior)
     *     The reload should not be executed inside the visitor because every collector thread has their own visitor.
     */
@Test
public void testReloadThreshdConfig() throws Exception {
    String baseIpAddress = "10.0.0.";
    // Initialize Mock Network
    MockNetwork network = new MockNetwork();
    network.setCriticalService("ICMP");
    for (int i = 1; i <= 5; i++) {
        String ipAddress = baseIpAddress + i;
        network.addNode(i, "testNode-" + ipAddress);
        network.addInterface(ipAddress);
        network.setIfAlias("eth0");
        network.addService("ICMP");
        network.addService("SNMP");
        if (i == 5) {
            // Adding HTTP on node 5
            network.addService("HTTP");
        }
    }
    network.addPathOutage(1, InetAddressUtils.addr("192.168.1.1"), "ICMP");
    MockDatabase db = new MockDatabase();
    db.populate(network);
    db.update("insert into categories (categoryid, categoryname) values (?, ?)", 10, "CAT1");
    db.update("insert into categories (categoryid, categoryname) values (?, ?)", 11, "CAT2");
    for (int i = 1; i <= 5; i++) {
        db.update("update snmpinterface set snmpifname=?, snmpifdescr=? where id=?", "eth0", "eth0", i);
        db.update("update node set nodesysoid=? where nodeid=?", ".1.3.6.1.4.1.9.1.222", i);
    }
    for (int i = 1; i <= 2; i++) {
        db.update("insert into category_node values (?, ?)", 10, i);
    }
    for (int i = 3; i <= 5; i++) {
        db.update("insert into category_node values (?, ?)", 11, i);
    }
    DataSourceFactory.setInstance(db);
    // Initialize Filter DAO
    System.setProperty("opennms.home", "src/test/resources");
    DatabaseSchemaConfigFactory.init();
    JdbcFilterDao jdbcFilterDao = new JdbcFilterDao();
    jdbcFilterDao.setDataSource(db);
    jdbcFilterDao.setDatabaseSchemaConfigFactory(DatabaseSchemaConfigFactory.getInstance());
    jdbcFilterDao.afterPropertiesSet();
    FilterDaoFactory.setInstance(jdbcFilterDao);
    // Initialize Factories
    initFactories("/threshd-configuration-reload-use-case-a.xml", "/test-thresholds-reload-use-cases.xml");
    // Initialize Thresholding Visitors
    System.err.println("-----------------------------------------------------------------------------------");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("thresholding-enabled", "true");
    ServiceParameters svcParams = new ServiceParameters(params);
    List<ThresholdingVisitor> visitors = new ArrayList<>();
    for (int i = 1; i <= 5; i++) {
        String ipAddress = baseIpAddress + i;
        ThresholdingVisitor visitor = ThresholdingVisitor.create(i, ipAddress, "SNMP", getRepository(), svcParams, m_resourceStorageDao);
        assertNotNull(visitor);
        visitors.add(visitor);
        if (i == 5) {
            ThresholdingVisitor httpVisitor = ThresholdingVisitor.create(i, ipAddress, "HTTP", getRepository(), svcParams, m_resourceStorageDao);
            assertNotNull(httpVisitor);
            visitors.add(httpVisitor);
        }
    }
    System.err.println("-----------------------------------------------------------------------------------");
    // Check Visitors
    for (int i = 0; i < 2; i++) {
        // Nodes n1 and n2 has thresholds defined on one threshold group.
        assertTrue(visitors.get(i).hasThresholds());
        assertEquals(1, visitors.get(i).getThresholdGroups().size());
    }
    for (int i = 2; i < 6; i++) {
        // Nodes n3, n4 and n5 should not have thresholds defined.
        assertFalse(visitors.get(i).hasThresholds());
        assertEquals(0, visitors.get(i).getThresholdGroups().size());
    }
    // Re-Initialize Factories
    initFactories("/threshd-configuration-reload-use-case-b.xml", "/test-thresholds-reload-use-cases.xml");
    // Reload state on each visitor
    System.err.println("-----------------------------------------------------------------------------------");
    for (ThresholdingVisitor visitor : visitors) {
        visitor.reload();
    }
    System.err.println("-----------------------------------------------------------------------------------");
    // Check Visitors
    for (int i = 0; i < 6; i++) {
        assertTrue(visitors.get(i).hasThresholds());
        assertEquals(1, visitors.get(i).getThresholdGroups().size());
        if (i == 5) {
            assertEquals("web-services", visitors.get(i).getThresholdGroups().get(0).getName());
        }
    }
}
Also used : MockNetwork(org.opennms.netmgt.mock.MockNetwork) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MockDatabase(org.opennms.core.test.db.MockDatabase) MibObject(org.opennms.netmgt.config.datacollection.MibObject) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) JdbcFilterDao(org.opennms.netmgt.filter.JdbcFilterDao) Test(org.junit.Test)

Example 12 with MockDatabase

use of org.opennms.core.test.db.MockDatabase in project opennms by OpenNMS.

the class ThresholdingVisitorIT method runTestForBug3554.

private void runTestForBug3554() throws Exception {
    MockLogAppender.resetState();
    System.err.println("----------------------------------------------------------------------------------- begin test");
    String baseIpAddress = "10.0.0.";
    int numOfNodes = 5;
    // Initialize Mock Network
    MockNetwork network = new MockNetwork();
    network.setCriticalService("ICMP");
    for (int i = 1; i <= numOfNodes; i++) {
        String ipAddress = baseIpAddress + i;
        network.addNode(i, "testNode-" + ipAddress);
        network.addInterface(ipAddress);
        network.setIfAlias("eth0");
        network.addService("ICMP");
        network.addService("SNMP");
    }
    network.addPathOutage(1, InetAddressUtils.addr("192.168.1.1"), "ICMP");
    MockDatabase db = new MockDatabase();
    db.populate(network);
    db.update("insert into categories (categoryid, categoryname) values (?, ?)", 10, "IPRA");
    db.update("insert into categories (categoryid, categoryname) values (?, ?)", 11, "NAS");
    for (int i = 1; i <= numOfNodes; i++) {
        db.update("update snmpinterface set snmpifname=?, snmpifdescr=? where id=?", "eth0", "eth0", i);
        db.update("update node set nodesysoid=? where nodeid=?", ".1.3.6.1.4.1.9.1.222", i);
        db.update("insert into category_node values (?, ?)", 10, i);
        db.update("insert into category_node values (?, ?)", 11, i);
    }
    DataSourceFactory.setInstance(db);
    // Initialize Filter DAO
    System.setProperty("opennms.home", "src/test/resources");
    DatabaseSchemaConfigFactory.init();
    JdbcFilterDao jdbcFilterDao = new JdbcFilterDao();
    jdbcFilterDao.setDataSource(db);
    jdbcFilterDao.setDatabaseSchemaConfigFactory(DatabaseSchemaConfigFactory.getInstance());
    jdbcFilterDao.afterPropertiesSet();
    FilterDaoFactory.setInstance(jdbcFilterDao);
    // Initialize Factories
    initFactories("/threshd-configuration-bug3554.xml", "/test-thresholds-bug3554.xml");
    // Initialize Thresholding Visitors
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("thresholding-enabled", "true");
    ServiceParameters svcParams = new ServiceParameters(params);
    for (int i = 1; i <= numOfNodes; i++) {
        System.err.println("----------------------------------------------------------------------------------- visitor #" + i);
        String ipAddress = baseIpAddress + i;
        ThresholdingVisitor visitor = ThresholdingVisitor.create(1, ipAddress, "SNMP", getRepository(), svcParams, m_resourceStorageDao);
        assertNotNull(visitor);
        // mib2, cisco, ciscoIPRA, ciscoNAS
        assertEquals(4, visitor.getThresholdGroups().size());
    }
    System.err.println("----------------------------------------------------------------------------------- end");
}
Also used : MockNetwork(org.opennms.netmgt.mock.MockNetwork) HashMap(java.util.HashMap) MockDatabase(org.opennms.core.test.db.MockDatabase) MibObject(org.opennms.netmgt.config.datacollection.MibObject) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) JdbcFilterDao(org.opennms.netmgt.filter.JdbcFilterDao)

Example 13 with MockDatabase

use of org.opennms.core.test.db.MockDatabase in project opennms by OpenNMS.

the class CollectionResourceWrapperIT method testInterfaceResource.

@Test
public void testInterfaceResource() throws Exception {
    // Set Defaults
    String ipAddress = "10.0.0.1";
    String ifName = "eth0";
    int ifIndex = 2;
    // Initialize Database
    MockNetwork network = new MockNetwork();
    network.setCriticalService("ICMP");
    network.addNode(1, "testNode");
    network.addInterface(ipAddress);
    network.setIfAlias(ifName);
    network.setIfIndex(ifIndex);
    network.addService("ICMP");
    network.addService("SNMP");
    network.addService("HTTP");
    MockDatabase db = new MockDatabase();
    db.populate(network);
    DataSourceFactory.setInstance(db);
    // Create Mock Collection Agent
    SnmpCollectionAgent agent = createCollectionAgent();
    // Create SnmpIfData
    OnmsNode node = new OnmsNode();
    node.setId(agent.getNodeId());
    node.setLabel("testNode");
    node.setForeignSource(agent.getForeignSource());
    node.setForeignId(agent.getForeignId());
    OnmsSnmpInterface snmpIface = new OnmsSnmpInterface(node, ifIndex);
    snmpIface.setIfDescr(ifName);
    snmpIface.setIfName(ifName);
    snmpIface.setIfAlias(ifName);
    snmpIface.setIfSpeed(10000000l);
    snmpIface.setPhysAddr("001122334455");
    SnmpIfData ifData = new SnmpIfData(snmpIface);
    // Creating IfResourceType
    MockDataCollectionConfig dataCollectionConfig = new MockDataCollectionConfig();
    OnmsSnmpCollection collection = new OnmsSnmpCollection(agent, new ServiceParameters(new HashMap<String, Object>()), dataCollectionConfig, m_locationAwareSnmpClient);
    IfResourceType resourceType = new IfResourceType(agent, collection);
    // Creating Resource
    SnmpCollectionResource resource = new IfInfo(resourceType, agent, ifData);
    SnmpAttribute attribute = addAttributeToCollectionResource(resource, "ifInOctets", AttributeType.COUNTER, "ifIndex", "5000");
    Map<String, CollectionAttribute> attributes = new HashMap<String, CollectionAttribute>();
    attributes.put(attribute.getName(), attribute);
    // Create Wrapper
    CollectionResourceWrapper wrapper = createWrapper(resource, attributes);
    // Validations
    Assert.assertEquals(node.getId().intValue(), wrapper.getNodeId());
    // Should be the address of the SNMP Agent (Bug 3808)
    Assert.assertEquals("127.0.0.1", wrapper.getHostAddress());
    Assert.assertEquals("eth0-001122334455", wrapper.getIfLabel());
    Assert.assertEquals("if", wrapper.getResourceTypeName());
    Assert.assertEquals("SNMP", wrapper.getServiceName());
    Assert.assertEquals(true, wrapper.isAnInterfaceResource());
    Assert.assertEquals(Integer.toString(ifIndex), wrapper.getInstance());
    Assert.assertEquals(Integer.toString(ifIndex), wrapper.getIfIndex());
    // IfLabel is called only once
    Assert.assertEquals(Integer.toString(ifIndex), wrapper.getIfIndex());
    // IfLabel is called only once
    Assert.assertEquals(Integer.toString(ifIndex), wrapper.getIfIndex());
    // IfLabel is called only once
    Assert.assertEquals("eth0", wrapper.getIfInfoValue("snmpifname"));
    Assert.assertEquals("eth0-001122334455", wrapper.getInstanceLabel());
    Assert.assertEquals("nodeSource[JUnit:T001].interfaceSnmp[eth0-001122334455]", wrapper.getResourceId().toString());
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) SnmpIfData(org.opennms.netmgt.collectd.SnmpIfData) HashMap(java.util.HashMap) OnmsSnmpInterface(org.opennms.netmgt.model.OnmsSnmpInterface) MockDataCollectionConfig(org.opennms.netmgt.mock.MockDataCollectionConfig) IfResourceType(org.opennms.netmgt.collectd.IfResourceType) CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) MockNetwork(org.opennms.netmgt.mock.MockNetwork) SnmpAttribute(org.opennms.netmgt.collectd.SnmpAttribute) MockDatabase(org.opennms.core.test.db.MockDatabase) IfInfo(org.opennms.netmgt.collectd.IfInfo) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) SnmpCollectionResource(org.opennms.netmgt.collectd.SnmpCollectionResource) OnmsSnmpCollection(org.opennms.netmgt.collectd.OnmsSnmpCollection) Test(org.junit.Test)

Example 14 with MockDatabase

use of org.opennms.core.test.db.MockDatabase in project opennms by OpenNMS.

the class HttpRemotingContextIT method testLoadContext.

public void testLoadContext() throws Throwable {
    DaoTestConfigBean bean = new DaoTestConfigBean();
    bean.afterPropertiesSet();
    MockDatabase db = new MockDatabase(true);
    DataSourceFactory.setInstance(db);
    servletContext = new MockServletContext("file:src/main/webapp");
    servletContext.addInitParameter("contextConfigLocation", "classpath:/META-INF/opennms/applicationContext-commonConfigs.xml " + "classpath:/META-INF/opennms/applicationContext-soa.xml " + "classpath:/META-INF/opennms/applicationContext-mockDao.xml " + "classpath*:/META-INF/opennms/component-service.xml " + "classpath*:/META-INF/opennms/component-dao.xml " + // Contexts within this project
    "/WEB-INF/applicationContext-common.xml " + "/WEB-INF/applicationContext-serviceRegistryRemoting.xml " + "/WEB-INF/applicationContext-spring-security.xml " + "/WEB-INF/applicationContext-svclayer.xml ");
    servletContext.addInitParameter("parentContextKey", "daoContext");
    ServletContextEvent e = new ServletContextEvent(servletContext);
    contextListener = new ContextLoaderListener();
    contextListener.contextInitialized(e);
    servletContext.setContextPath(contextPath);
    servletConfig = new MockServletConfig(servletContext, "dispatcher");
    servletConfig.addInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
    servletConfig.addInitParameter("com.sun.jersey.config.property.packages", "org.opennms.web.rest");
    try {
        MockFilterConfig filterConfig = new MockFilterConfig(servletContext, "openSessionInViewFilter");
        filter = new OpenSessionInViewFilter();
        filter.init(filterConfig);
    } catch (ServletException se) {
        throw se.getRootCause();
    }
}
Also used : DaoTestConfigBean(org.opennms.test.DaoTestConfigBean) OpenSessionInViewFilter(org.springframework.orm.hibernate3.support.OpenSessionInViewFilter) ServletException(javax.servlet.ServletException) MockDatabase(org.opennms.core.test.db.MockDatabase) MockServletConfig(org.springframework.mock.web.MockServletConfig) ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) MockServletContext(org.springframework.mock.web.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) MockFilterConfig(org.springframework.mock.web.MockFilterConfig)

Example 15 with MockDatabase

use of org.opennms.core.test.db.MockDatabase in project opennms by OpenNMS.

the class PollerConfigWithPSMIT method setUp.

@Before
public void setUp() throws Exception {
    MockLogAppender.setupLogging();
    Resource dbConfig = new ClassPathResource("/org/opennms/netmgt/config/test-database-schema.xml");
    InputStream s = dbConfig.getInputStream();
    DatabaseSchemaConfigFactory dscf = new DatabaseSchemaConfigFactory(s);
    s.close();
    DatabaseSchemaConfigFactory.setInstance(dscf);
    MockNetwork network = new MockNetwork();
    network.setCriticalService("ICMP");
    network.addNode(1, "Router");
    network.addInterface("192.168.1.1");
    network.addService("ICMP");
    network.addService("SMTP");
    network.addInterface("192.168.1.2");
    network.addService("ICMP");
    network.addService("SMTP");
    network.addNode(2, "Server");
    network.addInterface("192.168.1.3");
    network.addService("ICMP");
    network.addService("HTTP");
    network.addNode(3, "Firewall");
    network.addInterface("192.168.1.4");
    network.addService("SMTP");
    network.addService("HTTP");
    network.addInterface("192.168.1.5");
    network.addService("SMTP");
    network.addService("HTTP");
    network.addInterface("192.169.1.5");
    network.addService("SMTP");
    network.addService("HTTP");
    network.addNode(4, "TestNode121");
    network.addInterface("123.12.123.121");
    network.addService("HTTP");
    network.addNode(5, "TestNode122");
    network.addInterface("123.12.123.122");
    network.addService("HTTP");
    MockDatabase db = new MockDatabase();
    db.populate(network);
    DataSourceFactory.setInstance(db);
}
Also used : MockNetwork(org.opennms.netmgt.mock.MockNetwork) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockDatabase(org.opennms.core.test.db.MockDatabase) ClassPathResource(org.springframework.core.io.ClassPathResource) Before(org.junit.Before)

Aggregations

MockDatabase (org.opennms.core.test.db.MockDatabase)16 MockNetwork (org.opennms.netmgt.mock.MockNetwork)8 Before (org.junit.Before)5 InputStream (java.io.InputStream)4 DaoTestConfigBean (org.opennms.test.DaoTestConfigBean)4 HashMap (java.util.HashMap)3 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)3 GregorianCalendar (java.util.GregorianCalendar)2 ServletException (javax.servlet.ServletException)2 Test (org.junit.Test)2 DatabaseSchemaConfigFactory (org.opennms.netmgt.config.DatabaseSchemaConfigFactory)2 MibObject (org.opennms.netmgt.config.datacollection.MibObject)2 JdbcFilterDao (org.opennms.netmgt.filter.JdbcFilterDao)2 MockCategoryFactory (org.opennms.netmgt.mock.MockCategoryFactory)2 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)2 MockServletConfig (org.springframework.mock.web.MockServletConfig)2 OpenSessionInViewFilter (org.springframework.orm.hibernate3.support.OpenSessionInViewFilter)2 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 ServletContextEvent (javax.servlet.ServletContextEvent)1