package org.opennms.netmgt.provision.service;

import static org.junit.Assert.assertEquals;

import java.util.List;
import java.util.concurrent.ExecutionException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.opennms.core.tasks.Task;
import org.opennms.mock.snmp.JUnitSnmpAgent;
import org.opennms.mock.snmp.JUnitSnmpAgentExecutionListener;
import org.opennms.netmgt.dao.IpInterfaceDao;
import org.opennms.netmgt.dao.NodeDao;
import org.opennms.netmgt.dao.db.JUnitTemporaryDatabase;
import org.opennms.netmgt.dao.db.OpenNMSConfigurationExecutionListener;
import org.opennms.netmgt.dao.db.TemporaryDatabaseExecutionListener;
import org.opennms.netmgt.model.OnmsNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
    OpenNMSConfigurationExecutionListener.class,
    TemporaryDatabaseExecutionListener.class,
    JUnitSnmpAgentExecutionListener.class,
    DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class,
    TransactionalTestExecutionListener.class
})
@ContextConfiguration(locations={
        "classpath:/META-INF/opennms/applicationContext-dao.xml",
        "classpath:/META-INF/opennms/applicationContext-daemon.xml",
        "classpath:/META-INF/opennms/applicationContext-proxy-snmp.xml",
        "classpath:/META-INF/opennms/mockEventIpcManager.xml",
        "classpath:/META-INF/opennms/applicationContext-setupIpLike-enabled.xml",
        "classpath:/META-INF/opennms/applicationContext-provisiond.xml",
        "classpath*:/META-INF/opennms/component-dao.xml",
        "classpath*:/META-INF/opennms/detectors.xml",
        "classpath:/importerServiceTest.xml"
})
@JUnitTemporaryDatabase()
public class IfIndexNullTest {
    
    @Autowired
    private Provisioner m_provisioner;
    
    @Autowired
    private ResourceLoader m_resourceLoader;
    
    @Autowired
    private IpInterfaceDao m_ipInterfaceDao;
    
    @Autowired
    private NodeDao m_nodeDao;
    
    @Test
    @JUnitSnmpAgent(host="127.0.0.1", port=9161, resource="classpath:snmpTestData-null.properties")
    public void testNullIfIndex() throws Exception {
        m_provisioner.importModelFromResource(m_resourceLoader.getResource("classpath:/tec_dump.xml"));
        
        List<OnmsNode> nodes = getNodeDao().findAll();
        OnmsNode node = nodes.get(0);
        
        NodeScan scan = m_provisioner.createNodeScan(node.getId(), node.getForeignSource(), node.getForeignId());
        runScan(scan);
      //Verify ipinterface count
        assertEquals(2, getInterfaceDao().countAll());
        
    }
    
    public void runScan(NodeScan scan) throws InterruptedException, ExecutionException {
        Task t = scan.createTask();
        t.schedule();
        t.waitFor();
    }
    
    private NodeDao getNodeDao() {
        return m_nodeDao;
    }

    private IpInterfaceDao getInterfaceDao() {
        return m_ipInterfaceDao;
    }
}

@@ -103,20 +103,89 @@
 va.util.
-List
+Arrays;%0Aimport java.util.List;%0Aimport java.util.concurrent.CountDownLatch
 ;%0Aimport
@@ -418,32 +418,74 @@
 cutionListener;%0A
+import org.opennms.netmgt.EventConstants;%0A
 import org.openn
@@ -783,20 +783,169 @@
 t.mo
-del.OnmsNode
+ck.MockEventIpcManager;%0Aimport org.opennms.netmgt.model.OnmsNode;%0Aimport org.opennms.netmgt.model.events.EventListener;%0Aimport org.opennms.netmgt.xml.event.Event
 ;%0Aim
@@ -2781,24 +2781,91 @@
 o m_nodeDao;
+%0A%0A    @Autowired%0A    private MockEventIpcManager m_eventSubscriber;
 %0A    %0A    @T
@@ -3013,32 +3013,192 @@
 ows Exception %7B%0A
+        final CountDownLatch eventRecieved = anticipateEvents(EventConstants.PROVISION_SCAN_COMPLETE_UEI, EventConstants.PROVISION_SCAN_ABORTED_UEI );%0A        %0A
         m_provis
@@ -3388,32 +3388,72 @@
 et(0);%0A        %0A
+        eventRecieved.await();%0A        %0A
         NodeScan
@@ -3573,16 +3573,27 @@
 (scan);%0A
+        %0A  
       //
@@ -3867,24 +3867,492 @@
 %0A    %7D%0A    %0A
+    private CountDownLatch anticipateEvents(String... ueis) %7B%0A        final CountDownLatch eventRecieved = new CountDownLatch(1);%0A        m_eventSubscriber.addEventListener(new EventListener() %7B%0A%0A            public void onEvent(Event e) %7B%0A                eventRecieved.countDown();%0A            %7D%0A%0A            public String getName() %7B%0A                return %22Test Initial Setup%22;%0A            %7D%0A        %7D, Arrays.asList(ueis));%0A        return eventRecieved;%0A    %7D%0A    %0A
     private 

Fix IfIndexNullTest