/*******************************************************************************
 * 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 <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.web.svclayer.support;

import static org.easymock.EasyMock.expect;

import java.util.SortedSet;

import junit.framework.TestCase;

import org.opennms.netmgt.dao.ResourceDao;
import org.opennms.netmgt.dao.StatisticsReportDao;
import org.opennms.netmgt.model.ResourceReference;
import org.opennms.netmgt.model.StatisticsReport;
import org.opennms.netmgt.model.StatisticsReportData;
import org.opennms.test.ThrowableAnticipator;
import org.opennms.test.mock.EasyMockUtils;
import org.opennms.web.command.StatisticsReportCommand;
import org.opennms.web.svclayer.support.StatisticsReportModel.Datum;
import org.springframework.validation.BindException;

/**
 * Test case for DefaultStatisticsReportService.
 * 
 * @see DefaultStatisticsReportService
 * @author <a href="dj@opennms.org">DJ Gregor</a>
 */
public class DefaultStatisticsReportServiceTest extends TestCase {
    private EasyMockUtils m_mocks = new EasyMockUtils();
    
    private DefaultStatisticsReportService m_service = new DefaultStatisticsReportService();
    private ResourceDao m_resourceDao = m_mocks.createMock(ResourceDao.class);
    private StatisticsReportDao m_statisticsReportDao = m_mocks.createMock(StatisticsReportDao.class);

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        
        m_service.setResourceDao(m_resourceDao);
        m_service.setStatisticsReportDao(m_statisticsReportDao );
        m_service.afterPropertiesSet();
    }

    @Override
    protected void runTest() throws Throwable {
        super.runTest();
        
        m_mocks.verifyAll();
    }
    
    public void testNullCommandObjectId() {
        StatisticsReportCommand command = new StatisticsReportCommand();
        BindException errors = new BindException(command, "");
        
        ThrowableAnticipator ta = new ThrowableAnticipator();
        ta.anticipate(new IllegalArgumentException("id property on command object cannot be null"));

        m_mocks.replayAll();
        try {
            m_service.getReport(command , errors);
        } catch (Throwable t) {
            ta.throwableReceived(t);
        }
        ta.verifyAnticipated();
    }

    public void testDatumWithNonExistentResource() {
        StatisticsReport report = new StatisticsReport();
        report.setId(1);
        
        StatisticsReportData datum = new StatisticsReportData();
        ResourceReference resourceRef = new ResourceReference();
        resourceRef.setId(1);
        resourceRef.setResourceId("node[1].interfaceSnmp[en0]");
        datum.setId(1);
        datum.setResource(resourceRef);
        datum.setReport(report);
        report.addData(datum);

        StatisticsReportCommand command = new StatisticsReportCommand();
        command.setId(report.getId());
        
        BindException errors = new BindException(command, "");
        
        expect(m_statisticsReportDao.load(report.getId())).andReturn(report);
        m_statisticsReportDao.initialize(report);
        m_statisticsReportDao.initialize(report.getData());
        expect(m_resourceDao.getResourceById(resourceRef.getResourceId())).andReturn(null);
        
        m_mocks.replayAll();
        StatisticsReportModel model = m_service.getReport(command, errors);
        
        assertNotNull("model should not be null", model);
        assertNotNull("model.getData() should not be null", model.getData());
        
        SortedSet<Datum> data = model.getData();
        assertEquals("data size", 1, data.size());
        Datum d = data.first();
        assertNotNull("first datum should not be null", d);
        assertNull("first datum resource should be null", d.getResource());
    }
}

@@ -1265,16 +1265,150 @@
 .expect;
+%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertNotNull;%0Aimport static org.junit.Assert.assertNull;
 %0A%0Aimport
@@ -1441,34 +1441,72 @@
 ort 
+org.
 junit.
-framework.TestCase;%0A
+After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;
 %0Aimp
@@ -2222,25 +2222,8 @@
 est 
-extends TestCase 
 %7B%0A  
@@ -2563,38 +2563,33 @@
 ;%0A%0A    @
-Override%0A    protected
+Before%0A    public
  void se
@@ -2617,40 +2617,8 @@
 n %7B%0A
-        super.setUp();%0A        %0A
     
@@ -2784,98 +2784,57 @@
    @
-Override%0A    protected void runTest() throws Throwable %7B%0A        super.runTest();%0A        
+After%0A    public void verify() throws Throwable %7B
 %0A   
@@ -2866,24 +2866,34 @@
 %0A    %7D%0A    %0A
+    @Test%0A
     public v
@@ -3441,24 +3441,34 @@
 d();%0A    %7D%0A%0A
+    @Test%0A
     public v

convert to junit4