import com.google.gson.GsonBuilder;
import com.yammer.metrics.reporting.MetricsServlet;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.testing.HttpTester;
import org.eclipse.jetty.testing.ServletTester;
import org.greencheek.yammer.metrics.web.filter.ResponseCodeFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.*;

import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * User: dominictootell
 * Date: 21/10/2012
 * Time: 13:52
 */
public class ResponseCodeFilterRequestsPerSecondTest {


    private FilterChain mockFilterChain;
    ResponseCodeFilter filter;
    ServletTester tester;

    @Before
    public void setUp() throws Exception {

        ServletContext context = new MockServletContext();
        final MockRequestDispatcher requestDispatcher = new MockRequestDispatcher("");

        // http servlet mocked for mocked dispatcher
        MockFilterConfig config = new MockFilterConfig(context);
        mockFilterChain = new MockFilterChain()  {
            @Override
            public void doFilter(ServletRequest req, ServletResponse res) {
                try {
                    Thread.sleep(400);
                } catch (Exception e) {
                }
            }
        };

        filter = new ResponseCodeFilter();

        filter.init(config);


        tester = new ServletTester();
        ServletHolder sh = tester.addServlet(MetricsServlet.class.getName(),"/metrics");
        sh.setInitParameter(MetricsServlet.SHOW_JVM_METRICS,"false");

        try {
            tester.start();
        } catch (Exception e) {
            e.printStackTrace();;
            fail("Failed to start Jetty Context");
        }
    }

    @After
    public void tearDown() {
        try {
            tester.stop();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Failed to stop jetty context");
        }
    }

    @Test
    public void testRequestsPerSecond() {
        MockHttpServletRequest getRequest = new MockHttpServletRequest("GET","http://localhost:9090/");
        MockHttpServletRequest postRequest = new MockHttpServletRequest("POST","http://localhost:9090/");

        MockHttpServletResponse response = new MockHttpServletResponse();

        try {
            for(int i=0;i<10;i++) {
                filter.doFilter(getRequest,response,mockFilterChain);
                filter.doFilter(postRequest,response,mockFilterChain);
            }
        } catch(Exception e) {
            fail("failed with exception during filter request");
        }


        HttpTester statsrequest = new HttpTester();
        HttpTester statsresponse = new HttpTester();
        statsrequest.setMethod("GET");
        statsrequest.setHeader("Host","tester");
        statsrequest.setURI("/metrics");
        statsrequest.setVersion("HTTP/1.1");

        try {
            statsresponse.parse(tester.getResponses(statsrequest.generate()));
            assertTrue("Should have at least 2 requests per second", getMeterMean(statsresponse.getContent(), "requestsPerSecond") > 2);
            assertEquals("Should have handled 20 filter requests", 20.0,getMeterCount(statsresponse.getContent(), "requestsPerSecond"),0.0);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }


    private double getMeterCount(String json, String meterName) {
        return getMeterValue(json,meterName,"count");
    }
    private double getMeterMean(String json, String meterName) {
        return getMeterValue(json,meterName,"mean");
    }

    private double getMeterValue(String json, String meterName, String dataPoint) {
        Map m = new GsonBuilder().create().fromJson(json,Map.class);
        System.out.println(m);
        Double val = ((Map<String,Map<String,Double>>)m.get("org.greencheek.yammer.metrics.web.filter.ResponseCodeFilter")).get(meterName).get(dataPoint);
        return val;
    }
}

@@ -339,16 +339,41 @@
 Before;%0A
+import org.junit.Ignore;%0A
 import o
@@ -3455,16 +3455,17 @@
 %22, 20.0,
+ 
 getMeter
@@ -3519,16 +3519,17 @@
 econd%22),
+ 
 0.0);%0A  
@@ -3604,24 +3604,114 @@
   %7D%0A    %7D%0A%0A%0A
+    @Ignore%0A    @Test%0A    public void testRequestsPerSecondViaHttpGetRequests() %7B%0A%0A    %7D%0A%0A
     private 

add new test for invoking filter via a servlet request.