<commit_before>/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2007-2012 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2012 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 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <<EMAIL>>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.netmgt.capsd;

import static org.junit.Assert.assertEquals;

import java.io.IOException;

import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opennms.core.spring.BeanUtils;
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.test.snmp.annotations.JUnitSnmpAgent;
import org.opennms.core.test.snmp.annotations.JUnitSnmpAgents;
import org.opennms.netmgt.config.CapsdConfig;
import org.opennms.netmgt.mock.MockNetwork;
import org.opennms.test.JUnitConfigurationEnvironment;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
@RunWith(OpenNMSJUnit4ClassRunner.class)
@ContextConfiguration(locations={
        "classpath:/META-INF/opennms/mockEventIpcManager.xml",
        "classpath:/META-INF/opennms/applicationContext-soa.xml",
        "classpath:/META-INF/opennms/applicationContext-dao.xml",
        "classpath*:/META-INF/opennms/component-dao.xml",
        "classpath:/META-INF/opennms/applicationContext-daemon.xml",
        "classpath:/META-INF/opennms/applicationContext-commonConfigs.xml",
        "classpath:/META-INF/opennms/applicationContext-capsd.xml",
        // Override the capsd config with a stripped-down version
        "classpath:/META-INF/opennms/capsdTest.xml",
        "classpath:/META-INF/opennms/applicationContext-proxy-snmp.xml",
        "classpath*:/META-INF/opennms/applicationContext-minimal-conf.xml"
})
@JUnitConfigurationEnvironment(systemProperties="org.opennms.provisiond.enableDiscovery=false")
@JUnitTemporaryDatabase
public class CapsdIntegrationTest implements TemporaryDatabaseAware<MockDatabase>, InitializingBean {

    private static final int FOREIGN_NODEID = 77;
    private static final String FOREIGN_NODE_IP_ADDRESS = "192.0.2.201";

    private MockNetwork m_mockNetwork = new MockNetwork();

    @Autowired
    private Capsd m_capsd;

    @Autowired
    private CapsdConfig m_capsdConfig;

    private MockDatabase m_db;

    @Override
    public void setTemporaryDatabase(final MockDatabase database) {
        m_db = database;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        BeanUtils.assertAutowiring(this);
    }

    @Before
    public void setUp() throws Exception {
        assertEquals(2, m_capsdConfig.getConfiguredProtocols().size());

        m_mockNetwork.createStandardNetwork();

        m_mockNetwork.addNode(FOREIGN_NODEID, "ForeignNode");
        m_mockNetwork.addInterface(FOREIGN_NODE_IP_ADDRESS);
        m_mockNetwork.addInterface("fe80:0000:0000:0000:ffff:eeee:dddd:cccc");
        m_mockNetwork.addService("ICMP");
        m_mockNetwork.addService("SNMP");

        m_db.populate(m_mockNetwork);
    }

    @Test
    @JUnitSnmpAgents(value={
    	    @JUnitSnmpAgent(host="192.0.2.201", resource="classpath:org/opennms/netmgt/capsd/snmpTestData1.properties"),
    	    @JUnitSnmpAgent(host="192.0.2.204", resource="classpath:org/opennms/netmgt/capsd/snmpTestData1.properties"),
    	    @JUnitSnmpAgent(host="192.0.2.205", resource="classpath:org/opennms/netmgt/capsd/snmpTestData1.properties")
    })
    @JUnitTemporaryDatabase(tempDbClass=MockDatabase.class)
    public final void testRescan() throws Exception {

        assertEquals("Initally only 2 interfaces", 2, m_db.countRows("select * from ipinterface where nodeid = ?", FOREIGN_NODEID));

        m_capsd.init();
        m_capsd.start();

        m_capsd.rescanInterfaceParent(FOREIGN_NODEID);

        Thread.sleep(15000);

        m_capsd.stop();

        assertEquals("after scanning should be 3 interfaces", 3, m_db.countRows("select * from ipinterface where nodeid = ?", FOREIGN_NODEID));
    }

    /**
     * Refactored from org.opennms.netmgt.capsd.CapsdTest
     */
    @Test
    @JUnitSnmpAgents(value={
    	    @JUnitSnmpAgent(host="192.0.2.201", resource="classpath:org/opennms/netmgt/snmp/snmpTestData1.properties"),
    	    @JUnitSnmpAgent(host="192.0.2.204", resource="classpath:org/opennms/netmgt/snmp/snmpTestData1.properties"),
    	    @JUnitSnmpAgent(host="192.0.2.205", resource="classpath:org/opennms/netmgt/snmp/snmpTestData1.properties")
    })
    @JUnitTemporaryDatabase(tempDbClass=MockDatabase.class)
    public final void testRescanOfForeignNode() throws Exception {

        m_db.getJdbcTemplate().update("update node set foreignSource='testSource', foreignId='123' where nodeid = ?", FOREIGN_NODEID);

        assertEquals("Initally only 2 interfaces", 2, m_db.countRows("select * from ipinterface where nodeid = ?", FOREIGN_NODEID));

        m_capsd.init();
        m_capsd.start();

        m_capsd.rescanInterfaceParent(FOREIGN_NODEID);

        Thread.sleep(10000);

        m_capsd.stop();

        assertEquals("after scanning should still be 2 since its foreign", 2, m_db.countRows("select * from ipinterface where nodeid = ?", FOREIGN_NODEID));
    }

    /**
     * Refactored from org.opennms.netmgt.capsd.ScanSuspectTest
     * 
     * TODO: Add checks to this unit test to make sure that the suspect scan works correctly
     */<commit_msg>ugh, extending timeouts makes this pass<commit_after>/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2007-2012 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2012 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 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <<EMAIL>>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.netmgt.capsd;

import static org.junit.Assert.assertEquals;

import java.io.IOException;

import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opennms.core.spring.BeanUtils;
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.test.snmp.annotations.JUnitSnmpAgent;
import org.opennms.core.test.snmp.annotations.JUnitSnmpAgents;
import org.opennms.netmgt.config.CapsdConfig;
import org.opennms.netmgt.mock.MockNetwork;
import org.opennms.test.JUnitConfigurationEnvironment;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

/**
 * @author <a href="mailto:<EMAIL>.org"><NAME></a>
 */
@RunWith(OpenNMSJUnit4ClassRunner.class)
@ContextConfiguration(locations={
        "classpath:/META-INF/opennms/mockEventIpcManager.xml",
        "classpath:/META-INF/opennms/applicationContext-soa.xml",
        "classpath:/META-INF/opennms/applicationContext-dao.xml",
        "classpath*:/META-INF/opennms/component-dao.xml",
        "classpath:/META-INF/opennms/applicationContext-daemon.xml",
        "classpath:/META-INF/opennms/applicationContext-commonConfigs.xml",
        "classpath:/META-INF/opennms/applicationContext-capsd.xml",
        // Override the capsd config with a stripped-down version
        "classpath:/META-INF/opennms/capsdTest.xml",
        "classpath:/META-INF/opennms/applicationContext-proxy-snmp.xml",
        "classpath*:/META-INF/opennms/applicationContext-minimal-conf.xml"
})
@JUnitConfigurationEnvironment(systemProperties="org.opennms.provisiond.enableDiscovery=false")
@JUnitTemporaryDatabase
public class CapsdIntegrationTest implements TemporaryDatabaseAware<MockDatabase>, InitializingBean {

    private static final int FOREIGN_NODEID = 77;
    private static final String FOREIGN_NODE_IP_ADDRESS = "192.0.2.201";

    private MockNetwork m_mockNetwork = new MockNetwork();

    @Autowired
    private Capsd m_capsd;

    @Autowired
    private CapsdConfig m_capsdConfig;

    private MockDatabase m_db;

    @Override
    public void setTemporaryDatabase(final MockDatabase database) {
        m_db = database;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        BeanUtils.assertAutowiring(this);
    }

    @Before
    public void setUp() throws Exception {
        assertEquals(2, m_capsdConfig.getConfiguredProtocols().size());

        m_mockNetwork.createStandardNetwork();

        m_mockNetwork.addNode(FOREIGN_NODEID, "ForeignNode");
        m_mockNetwork.addInterface(FOREIGN_NODE_IP_ADDRESS);
        m_mockNetwork.addInterface("fe80:0000:0000:0000:ffff:eeee:dddd:cccc");
        m_mockNetwork.addService("ICMP");
        m_mockNetwork.addService("SNMP");

        m_db.populate(m_mockNetwork);
    }

    @Test
    @JUnitSnmpAgents(value={
    	    @JUnitSnmpAgent(host="192.0.2.201", resource="classpath:org/opennms/netmgt/capsd/snmpTestData1.properties"),
    	    @JUnitSnmpAgent(host="192.0.2.204", resource="classpath:org/opennms/netmgt/capsd/snmpTestData1.properties"),
    	    @JUnitSnmpAgent(host="192.0.2.205", resource="classpath:org/opennms/netmgt/capsd/snmpTestData1.properties")
    })
    @JUnitTemporaryDatabase(tempDbClass=MockDatabase.class)
    public final void testRescan() throws Exception {

        assertEquals("Initally only 2 interfaces", 2, m_db.countRows("select * from ipinterface where nodeid = ?", FOREIGN_NODEID));

        m_capsd.init();
        m_capsd.start();

        m_capsd.rescanInterfaceParent(FOREIGN_NODEID);

        Thread.sleep(30000);

        m_capsd.stop();

        assertEquals("after scanning should be 3 interfaces", 3, m_db.countRows("select * from ipinterface where nodeid = ?", FOREIGN_NODEID));
    }

    /**
     * Refactored from org.opennms.netmgt.capsd.CapsdTest
     */
    @Test
    @JUnitSnmpAgents(value={
    	    @JUnitSnmpAgent(host="192.0.2.201", resource="classpath:org/opennms/netmgt/snmp/snmpTestData1.properties"),
    	    @JUnitSnmpAgent(host="192.0.2.204", resource="classpath:org/opennms/netmgt/snmp/snmpTestData1.properties"),
    	    @JUnitSnmpAgent(host="192.0.2.205", resource="classpath:org/opennms/netmgt/snmp/snmpTestData1.properties")
    })
    @JUnitTemporaryDatabase(tempDbClass=MockDatabase.class)
    public final void testRescanOfForeignNode() throws Exception {

        m_db.getJdbcTemplate().update("update node set foreignSource='testSource', foreignId='123' where nodeid = ?", FOREIGN_NODEID);

        assertEquals("Initally only 2 interfaces", 2, m_db.countRows("select * from ipinterface where nodeid = ?", FOREIGN_NODEID));

        m_capsd.init();
        m_capsd.start();

        m_capsd.rescanInterfaceParent(FOREIGN_NODEID);

        Thread.sleep(30000);

        m_capsd.stop();

        assertEquals("after scanning should still be 2 since its foreign", 2, m_db.countRows("select * from ipinterface where nodeid = ?", FOREIGN_NODEID));
    }

    /**
     * Refactored from org.opennms.netmgt.capsd.ScanSuspectTest
     * 
     * TODO: Add checks to this unit test to make sure that the suspect scan works correctly
     */
{'fraction_non_alphanumeric': 0.13367149037744294, 'fraction_numerical': 0.01775324481575414, 'mean_word_length': 4.103540159878188, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 24}, 'pii_count': 14, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5939597', 'n_tokens_mistral': 4427, 'n_tokens_neox': 4173, 'n_words': 789}