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

package org.opennms.web.rest.v1;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.opennms.core.test.MockLogAppender;
import org.opennms.core.test.OpenNMSJUnit4ClassRunner;
import org.opennms.core.test.db.annotations.JUnitTemporaryDatabase;
import org.opennms.core.test.rest.AbstractSpringJerseyRestTestCase;
import org.opennms.netmgt.dao.DatabasePopulator;
import org.opennms.test.JUnitConfigurationEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;

@RunWith(OpenNMSJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={
        "classpath:/META-INF/opennms/applicationContext-soa.xml",
        "classpath:/META-INF/opennms/applicationContext-commonConfigs.xml",
        "classpath:/META-INF/opennms/applicationContext-minimal-conf.xml",
        "classpath:/META-INF/opennms/applicationContext-dao.xml",
        "classpath*:/META-INF/opennms/component-service.xml",
        "classpath*:/META-INF/opennms/component-dao.xml",
        "classpath:/META-INF/opennms/applicationContext-databasePopulator.xml",
        "classpath:/META-INF/opennms/mockEventIpcManager.xml",
        "file:src/main/webapp/WEB-INF/applicationContext-svclayer.xml",
        "file:src/main/webapp/WEB-INF/applicationContext-cxf-common.xml"
})
@JUnitConfigurationEnvironment
@JUnitTemporaryDatabase
@Transactional
public class EventRestServiceIT extends AbstractSpringJerseyRestTestCase {

    @Autowired
    private DatabasePopulator m_databasePopulator;

    @Override
    protected void afterServletStart() {
        MockLogAppender.setupLogging(true, "DEBUG");
        m_databasePopulator.populateDatabase();
    }

    @Test
    public void testBetween() throws Exception {
        String xml;
        xml = sendRequest(GET, "/events/between", parseParamData("begin=2010-01-01T00:00:00Z"), 200);
        assertTrue(xml.contains("<createTime>"));
        xml = sendRequest(GET, "/events/between", parseParamData("begin=2010-01-01T00:00:00Z&end=2010-01-01T01:00:00Z"), 200);
        assertTrue(xml.contains("totalCount=\"0\""));
        xml = sendRequest(GET, "/events/between", parseParamData("end=2010-01-01T01:00:00Z"), 200);
        assertTrue(xml.contains("totalCount=\"0\""));
    }
}

@@ -1271,19 +1271,100 @@
 t.assert
-Tru
+Equals;%0Aimport static org.junit.Assert.assertTrue;%0A%0Aimport javax.ws.rs.core.MediaTyp
 e;%0A%0Aimpo
@@ -1648,24 +1648,63 @@
 stTestCase;%0A
+import org.opennms.core.xml.JaxbUtils;%0A
 import org.o
@@ -1740,16 +1740,168 @@
 ulator;%0A
+import org.opennms.netmgt.dao.mock.EventAnticipator;%0Aimport org.opennms.netmgt.dao.mock.MockEventIpcManager;%0Aimport org.opennms.netmgt.xml.event.Event;%0A
 import o
@@ -2010,16 +2010,77 @@
 owired;%0A
+import org.springframework.mock.web.MockHttpServletResponse;%0A
 import o
@@ -3267,16 +3267,76 @@
 lator;%0A%0A
+    @Autowired%0A    private MockEventIpcManager m_eventMgr;%0A%0A
     @Ove
@@ -3994,32 +3994,32 @@
 00:00Z%22), 200);%0A
-
         assertTr
@@ -4061,11 +4061,697 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void canPublishEventToBus() throws Exception %7B%0A        // Create some test event%0A        Event e = new Event();%0A        e.setUei(%22some.uei%22);%0A        e.setHost(%22from-some-host%22);%0A%0A        // Setup the anticipator%0A        EventAnticipator anticipator = m_eventMgr.getEventAnticipator();%0A        anticipator.anticipateEvent(e);%0A%0A        // POST the event to the REST API%0A        MockHttpServletResponse response = sendData(POST, MediaType.APPLICATION_XML, %22/events%22, JaxbUtils.marshal(e));%0A%0A        // Verify%0A        assertEquals(200, response.getStatus());%0A        m_eventMgr.finishProcessingEvents();%0A        anticipator.verifyAnticipated(1000, 0, 0, 0, 0);%0A    %7D
 %0A%7D%0A

Test the event publishing via REST.