/*
 * This file is part of the OpenNMS(R) Application.
 *
 * OpenNMS(R) is Copyright (C) 2006 The OpenNMS Group, Inc.  All rights reserved.
 * OpenNMS(R) is a derivative work, containing both original code, included code and modified
 * code that was published under the GNU General Public License. Copyrights for modified
 * and included code are below.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * Modifications:
 *
 * 2008 Jan 27: Enable the service test and make it do something for real. - dj@opennms.org
 * 2008 Jan 26: Created this file. - dj@opennms.org
 *
 * Copyright (C) 2008 Daniel J. Gregor, Jr..  All rights reserved.
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * For more information contact:
 *      OpenNMS Licensing       <license@opennms.org>
 *      http://www.opennms.org/
 *      http://www.opennms.com/
 */
package org.opennms.netmgt.eventd;

import org.opennms.netmgt.dao.db.AbstractTransactionalTemporaryDatabaseSpringContextTests;
import org.opennms.netmgt.utils.EventBuilder;
import org.opennms.test.DaoTestConfigBean;

/**
 * @author <a href="mailto:dj@opennms.org">DJ Gregor</a>
 */
public class EventdSpringTest extends AbstractTransactionalTemporaryDatabaseSpringContextTests {
    private Eventd m_daemon;
    private EventIpcManager m_eventIpcManager;
    
    public EventdSpringTest() {
        super();

        DaoTestConfigBean daoTestConfig = new DaoTestConfigBean();
        daoTestConfig.afterPropertiesSet();
    }

    @Override
    protected String[] getConfigLocations() {
        return new String[] {
                "classpath:META-INF/opennms/applicationContext-dao.xml",
                "classpath:META-INF/opennms/applicationContext-daemon.xml",
                "classpath:META-INF/opennms/applicationContext-eventDaemon.xml"
        };
    }
    
    // FIXME: I get OutOfMemory errors if I run more than one test in this TestCase
    public void FIXMEtestDaemon() throws Exception {
        assertNotNull("daemon bean", m_daemon);
        
        m_daemon.onStart();
        m_daemon.onStop();
    }
    
    public void testSendEventWithService() throws Exception {
        int serviceId = 1;
        String serviceName = "some bogus service";

        m_daemon.onStart();

        getSimpleJdbcTemplate().update("insert into service (serviceId, serviceName) values (?, ?)", new Object[] { serviceId, serviceName });
        setComplete();
        endTransaction();
        startNewTransaction();
        
        EventBuilder builder = new EventBuilder("uei.opennms.org/foo", "someSource");
        builder.setService(serviceName);
        m_eventIpcManager.sendNow(builder.getEvent());
        
        Thread.sleep(1000);
        
        assertEquals("event count", 1, getJdbcTemplate().queryForInt("select count(*) from events"));
        assertEquals("event service ID", serviceId, getJdbcTemplate().queryForInt("select serviceID from events"));

        m_daemon.onStop();
    }
    
    public Eventd getDaemon() {
        return m_daemon;
    }

    public void setDaemon(Eventd daemon) {
        m_daemon = daemon;
    }

    public EventIpcManager getEventIpcManager() {
        return m_eventIpcManager;
    }

    public void setEventIpcManager(EventIpcManager eventIpcManager) {
        m_eventIpcManager = eventIpcManager;
    }
}

@@ -442,16 +442,103 @@
 ons:%0A *%0A
+ * 2008 Feb 05: Load up a smaller EventConfDao and run one more test. - dj@opennms.org%0A
  * 2008 
@@ -2564,16 +2564,17 @@
 mon.xml%22
+,
 %0A       
@@ -2578,105 +2578,88 @@
     
-%7D;%0A
     
-%7D%0A
     
-%0A    // FIXME: I get OutOfMemory errors if I run more than one test in this TestCase
+%22classpath:META-INF/opennms/smallEventConfDao.xml%22%0A        %7D;%0A    %7D%0A    
 %0A   
@@ -2675,13 +2675,8 @@
 oid 
-FIXME
 test

Load up a smaller EventConfDao and run one more test.