package de.otto.edison.metrics.http;

import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;
import de.otto.edison.acceptance.api.StatusApi;
import de.otto.edison.testsupport.dsl.Then;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Optional;

import static de.otto.edison.acceptance.api.StatusApi.internal_status_is_retrieved_as;
import static de.otto.edison.acceptance.api.StatusApi.the_returned_content;
import static de.otto.edison.acceptance.api.StatusApi.the_status_code;
import static de.otto.edison.testsupport.dsl.Then.then;
import static de.otto.edison.testsupport.dsl.When.when;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

public class MetricsIntegrationTest {

    @Test
    public void shouldReportHttpCountAndTimeToGraphite() throws Exception {
        MetricRegistry metricRegistry = StatusApi.applicationContext().getBean(MetricRegistry.class);
        long counterBefore = Optional.ofNullable(metricRegistry.getCounters().get("counter.http.get.200")).orElse(new Counter()).getCount();

        //when
        internal_status_is_retrieved_as("text/html");

        //then
        long counterAfter = metricRegistry.getCounters().get("counter.http.get.200").getCount();
        assertThat(counterAfter - counterBefore).isEqualTo(1);
        long timerValues = metricRegistry.getTimers().get("timer.http.get").getSnapshot().size();
        assertThat(timerValues).isEqualTo(1);
    }

}

@@ -123,1274 +123,401 @@
 ort 
-de.otto.edison.acceptance.api.StatusApi;%0Aimport de.otto.edison.testsupport.dsl.Then;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.boot.context.embedded.LocalServerPort;%0Aimport org.springframework.boot.test.context.SpringBootTest;%0Aimport org.springframework.boot.test.web.client.TestRestTemplate;%0Aimport org.springframework.http.MediaType;%0Aimport org.springframework.http.ResponseEntity;%0Aimport org.springframework.test.context.junit4.SpringRunner;%0A%0Aimport java.util.Optional;%0A%0Aimport static de.otto.edison.acceptance.api.StatusApi.internal_status_is_retrieved_as;%0Aimport static de.otto.edison.acceptance.api.StatusApi.the_returned_content;%0Aimport static de.otto.edison.acceptance.api.StatusApi.the_status_code;%0Aimport static de.otto.edison.testsupport.dsl.Then.then;%0Aimport static de.otto.edison.testsupport.dsl.When.when;%0Aimport static org.assertj.core.api.Assertions.assertThat;%0Aimport static org.hamcrest.Matchers.containsString;%0Aimport static org.hamcrest.Matchers.is;%0Aimport static org.hamcrest.Matchers.startsWith;%0Aimport static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;%0A%0Apublic class MetricsIntegrationTest %7B
+com.codahale.metrics.Timer;%0Aimport de.otto.edison.acceptance.api.StatusApi;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Aimport java.util.Optional;%0A%0Aimport static de.otto.edison.acceptance.api.StatusApi.internal_status_is_retrieved_as;%0Aimport static org.assertj.core.api.Assertions.assertThat;%0A%0Apublic class MetricsIntegrationTest %7B%0A%0A    private MetricRegistry metricRegistry;
 %0A%0A    @
-Test
+Before
 %0A   
@@ -534,45 +534,12 @@
 id s
-houldReportHttpCountAndTimeToGraphite
+etUp
 () t
@@ -564,31 +564,16 @@
         
-MetricRegistry 
 metricRe
@@ -643,16 +643,102 @@
 class);%0A
+    %7D%0A%0A    @Test%0A    public void shouldReportHttpCountToGraphite() throws Exception %7B%0A
         
@@ -1124,28 +1124,361 @@
     
-    long timerValues
+%7D%0A%0A    @Test%0A    public void shouldReportHttpTimeToGraphite() throws Exception %7B%0A        long timerSnapshotSizeBefore = Optional.ofNullable(metricRegistry.getTimers().get(%22timer.http.get%22)).orElse(new Timer()).getSnapshot().size();%0A%0A        //when%0A        internal_status_is_retrieved_as(%22text/html%22);%0A%0A        //then%0A        long timerSnapshotSizeAfter
  = m
@@ -1575,14 +1575,51 @@
 imer
-Values
+SnapshotSizeAfter - timerSnapshotSizeBefore
 ).is

Fix MetricsIntegrationTest.