http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/HistogramProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/HistogramProducerTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/HistogramProducerTest.java new file mode 100644 index 0000000..21f9a8b --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/HistogramProducerTest.java @@ -0,0 +1,128 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics; + +import com.codahale.metrics.Histogram; +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.impl.DefaultCamelContext; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.apache.camel.component.metrics.MetricsConstants.HEADER_HISTOGRAM_VALUE; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class HistogramProducerTest { + + private static final String METRICS_NAME = "metrics.name"; + private static final Long VALUE = System.currentTimeMillis(); + + @Mock + private MetricsEndpoint endpoint; + + @Mock + private MetricRegistry registry; + + @Mock + private Histogram histogram; + + @Mock + private Exchange exchange; + + @Mock + private Message in; + + private HistogramProducer producer; + + private InOrder inOrder; + + @Before + public void setUp() throws Exception { + producer = new HistogramProducer(endpoint); + inOrder = Mockito.inOrder(endpoint, registry, histogram, exchange, in); + when(endpoint.getRegistry()).thenReturn(registry); + when(registry.histogram(METRICS_NAME)).thenReturn(histogram); + when(exchange.getIn()).thenReturn(in); + } + + @Test + public void testHistogramProducer() throws Exception { + assertThat(producer.getEndpoint().equals(endpoint), is(true)); + } + + @Test + public void testProcessValueSet() throws Exception { + when(endpoint.getValue()).thenReturn(VALUE); + when(in.getHeader(HEADER_HISTOGRAM_VALUE, VALUE, Long.class)).thenReturn(VALUE); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(registry, times(1)).histogram(METRICS_NAME); + inOrder.verify(endpoint, times(1)).getValue(); + inOrder.verify(in, times(1)).getHeader(HEADER_HISTOGRAM_VALUE, VALUE, Long.class); + inOrder.verify(histogram, times(1)).update(VALUE); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testProcessValueNotSet() throws Exception { + when(endpoint.getValue()).thenReturn(null); + when(in.getHeader(HEADER_HISTOGRAM_VALUE, null, Long.class)).thenReturn(null); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(registry, times(1)).histogram(METRICS_NAME); + inOrder.verify(endpoint, times(1)).getValue(); + inOrder.verify(in, times(1)).getHeader(HEADER_HISTOGRAM_VALUE, null, Long.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testProcessOverrideValue() throws Exception { + when(endpoint.getValue()).thenReturn(VALUE); + when(in.getHeader(HEADER_HISTOGRAM_VALUE, VALUE, Long.class)).thenReturn(VALUE + 3); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(registry, times(1)).histogram(METRICS_NAME); + inOrder.verify(endpoint, times(1)).getValue(); + inOrder.verify(in, times(1)).getHeader(HEADER_HISTOGRAM_VALUE, VALUE, Long.class); + inOrder.verify(histogram, times(1)).update(VALUE + 3); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testProcessOverrideUriValueNotSet() throws Exception { + when(endpoint.getValue()).thenReturn(null); + when(in.getHeader(HEADER_HISTOGRAM_VALUE, null, Long.class)).thenReturn(VALUE + 2); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(registry, times(1)).histogram(METRICS_NAME); + inOrder.verify(endpoint, times(1)).getValue(); + inOrder.verify(in, times(1)).getHeader(HEADER_HISTOGRAM_VALUE, null, Long.class); + inOrder.verify(histogram, times(1)).update(VALUE + 2); + inOrder.verifyNoMoreInteractions(); + } +}
http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/HistogramRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/HistogramRouteTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/HistogramRouteTest.java new file mode 100644 index 0000000..e22909f --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/HistogramRouteTest.java @@ -0,0 +1,124 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics; + +import com.codahale.metrics.Histogram; +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration; +import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader; +import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner; +import org.apache.camel.test.spring.MockEndpoints; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InOrder; +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; + +import static org.apache.camel.component.metrics.MetricsComponent.METRIC_REGISTRY_NAME; +import static org.apache.camel.component.metrics.MetricsConstants.HEADER_HISTOGRAM_VALUE; +import static org.apache.camel.component.metrics.MetricsConstants.HEADER_METRIC_NAME; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + +@RunWith(CamelSpringJUnit4ClassRunner.class) +@ContextConfiguration( + classes = { HistogramRouteTest.TestConfig.class }, + loader = CamelSpringDelegatingTestContextLoader.class) +@MockEndpoints +public class HistogramRouteTest { + + @EndpointInject(uri = "mock:out") + private MockEndpoint endpoint; + + @Produce(uri = "direct:in") + private ProducerTemplate producer; + + private MetricRegistry mockRegistry; + + private Histogram mockHistogram; + + private InOrder inOrder; + + @Configuration + public static class TestConfig extends SingleRouteCamelConfiguration { + + @Bean + @Override + public RouteBuilder route() { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + from("direct:in") + .to("metrics:histogram:A?value=332491") + .to("mock:out"); + } + }; + } + + @Bean(name = METRIC_REGISTRY_NAME) + public MetricRegistry getMetricRegistry() { + return Mockito.mock(MetricRegistry.class); + } + } + + @Before + public void setup() { + // TODO - 12.05.2014, Lauri - is there any better way to set this up? + mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(METRIC_REGISTRY_NAME, MetricRegistry.class); + mockHistogram = Mockito.mock(Histogram.class); + inOrder = Mockito.inOrder(mockRegistry, mockHistogram); + } + + @After + public void tearDown() { + endpoint.reset(); + reset(mockRegistry); + } + + @Test + public void testOverrideMetricsName() throws Exception { + when(mockRegistry.histogram("B")).thenReturn(mockHistogram); + endpoint.expectedMessageCount(1); + producer.sendBodyAndHeader(new Object(), HEADER_METRIC_NAME, "B"); + endpoint.assertIsSatisfied(); + inOrder.verify(mockRegistry, times(1)).histogram("B"); + inOrder.verify(mockHistogram, times(1)).update(332491L); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testOverrideValue() throws Exception { + when(mockRegistry.histogram("A")).thenReturn(mockHistogram); + endpoint.expectedMessageCount(1); + producer.sendBodyAndHeader(new Object(), HEADER_HISTOGRAM_VALUE, 181L); + endpoint.assertIsSatisfied(); + inOrder.verify(mockRegistry, times(1)).histogram("A"); + inOrder.verify(mockHistogram, times(1)).update(181L); + inOrder.verifyNoMoreInteractions(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MeterEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MeterEndpointTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MeterEndpointTest.java new file mode 100644 index 0000000..5842cd0 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MeterEndpointTest.java @@ -0,0 +1,86 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics; + +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Producer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; + +@RunWith(MockitoJUnitRunner.class) +public class MeterEndpointTest { + + private static final String METRICS_NAME = "metrics.name"; + private static final Long VALUE = System.currentTimeMillis(); + + @Mock + private MetricRegistry registry; + + private MetricsEndpoint endpoint; + + private InOrder inOrder; + + @Before + public void setUp() throws Exception { + endpoint = new MetricsEndpoint(null, null, registry, MetricsType.METER, METRICS_NAME); + inOrder = Mockito.inOrder(registry); + } + + @After + public void tearDown() throws Exception { + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testMeterEndpoint() throws Exception { + assertThat(endpoint, is(notNullValue())); + assertThat(endpoint.getRegistry(), is(registry)); + assertThat(endpoint.getMetricsName(), is(METRICS_NAME)); + } + + @Test + public void testCreateProducer() throws Exception { + Producer producer = endpoint.createProducer(); + assertThat(producer, is(notNullValue())); + assertThat(producer, is(instanceOf(MeterProducer.class))); + } + + @Test + public void testGetMark() throws Exception { + assertThat(endpoint.getMark(), is(nullValue())); + } + + @Test + public void testSetMark() throws Exception { + assertThat(endpoint.getMark(), is(nullValue())); + endpoint.setMark(VALUE); + assertThat(endpoint.getMark(), is(VALUE)); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MeterProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MeterProducerTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MeterProducerTest.java new file mode 100644 index 0000000..dfa85c4 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MeterProducerTest.java @@ -0,0 +1,127 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics; + +import com.codahale.metrics.Meter; +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.apache.camel.component.metrics.MetricsConstants.HEADER_METER_MARK; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class MeterProducerTest { + + private static final String METRICS_NAME = "metrics.name"; + private static final Long MARK = 9919120L; + + @Mock + private MetricsEndpoint endpoint; + + @Mock + private MetricRegistry registry; + + @Mock + private Meter meter; + + @Mock + private Exchange exchange; + + @Mock + private Message in; + + private MeterProducer producer; + + private InOrder inOrder; + + @Before + public void setUp() throws Exception { + producer = new MeterProducer(endpoint); + inOrder = Mockito.inOrder(endpoint, registry, meter, exchange, in); + when(endpoint.getRegistry()).thenReturn(registry); + when(registry.meter(METRICS_NAME)).thenReturn(meter); + when(exchange.getIn()).thenReturn(in); + } + + @Test + public void testMeterProducer() throws Exception { + assertThat(producer, is(notNullValue())); + assertThat(producer.getEndpoint().equals(endpoint), is(true)); + } + + @Test + public void testProcessMarkSet() throws Exception { + when(endpoint.getMark()).thenReturn(MARK); + when(in.getHeader(HEADER_METER_MARK, MARK, Long.class)).thenReturn(MARK); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(registry, times(1)).meter(METRICS_NAME); + inOrder.verify(endpoint, times(1)).getMark(); + inOrder.verify(in, times(1)).getHeader(HEADER_METER_MARK, MARK, Long.class); + inOrder.verify(meter, times(1)).mark(MARK); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testProcessMarkSetOverrideByHeaderValue() throws Exception { + when(endpoint.getMark()).thenReturn(MARK); + when(in.getHeader(HEADER_METER_MARK, MARK, Long.class)).thenReturn(MARK + 101); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(registry, times(1)).meter(METRICS_NAME); + inOrder.verify(endpoint, times(1)).getMark(); + inOrder.verify(in, times(1)).getHeader(HEADER_METER_MARK, MARK, Long.class); + inOrder.verify(meter, times(1)).mark(MARK + 101); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testProcessMarkNotSet() throws Exception { + when(endpoint.getMark()).thenReturn(null); + when(in.getHeader(HEADER_METER_MARK, null, Long.class)).thenReturn(null); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(registry, times(1)).meter(METRICS_NAME); + inOrder.verify(endpoint, times(1)).getMark(); + inOrder.verify(in, times(1)).getHeader(HEADER_METER_MARK, null, Long.class); + inOrder.verify(meter, times(1)).mark(); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testProcessMarkNotSetOverrideByHeaderValue() throws Exception { + when(endpoint.getMark()).thenReturn(null); + when(in.getHeader(HEADER_METER_MARK, null, Long.class)).thenReturn(MARK); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(registry, times(1)).meter(METRICS_NAME); + inOrder.verify(endpoint, times(1)).getMark(); + inOrder.verify(in, times(1)).getHeader(HEADER_METER_MARK, null, Long.class); + inOrder.verify(meter, times(1)).mark(MARK); + inOrder.verifyNoMoreInteractions(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MeterRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MeterRouteTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MeterRouteTest.java new file mode 100644 index 0000000..1e2dfa9 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MeterRouteTest.java @@ -0,0 +1,166 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics; + +import com.codahale.metrics.Meter; +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration; +import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader; +import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner; +import org.apache.camel.test.spring.MockEndpoints; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InOrder; +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; + +import static org.apache.camel.component.metrics.MetricsComponent.METRIC_REGISTRY_NAME; +import static org.apache.camel.component.metrics.MetricsConstants.HEADER_METER_MARK; +import static org.apache.camel.component.metrics.MetricsConstants.HEADER_METRIC_NAME; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + +@RunWith(CamelSpringJUnit4ClassRunner.class) +@ContextConfiguration( + classes = { MeterRouteTest.TestConfig.class }, + loader = CamelSpringDelegatingTestContextLoader.class) +@MockEndpoints +public class MeterRouteTest { + + @EndpointInject(uri = "mock:out") + private MockEndpoint endpoint; + + @Produce(uri = "direct:in-1") + private ProducerTemplate producer1; + + @Produce(uri = "direct:in-2") + private ProducerTemplate producer2; + + private MetricRegistry mockRegistry; + + private Meter mockMeter; + + private InOrder inOrder; + + @Configuration + public static class TestConfig extends SingleRouteCamelConfiguration { + + @Bean + @Override + public RouteBuilder route() { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + from("direct:in-1") + .to("metrics:meter:A?mark=3179") + .to("mock:out"); + + from("direct:in-2") + .to("metrics:meter:A") + .to("mock:out"); + } + }; + } + + @Bean(name = METRIC_REGISTRY_NAME) + public MetricRegistry getMetricRegistry() { + return Mockito.mock(MetricRegistry.class); + } + } + + @Before + public void setup() { + // TODO - 12.05.2014, Lauri - is there any better way to set this up? + mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(METRIC_REGISTRY_NAME, MetricRegistry.class); + mockMeter = Mockito.mock(Meter.class); + inOrder = Mockito.inOrder(mockRegistry, mockMeter); + when(mockRegistry.meter("A")).thenReturn(mockMeter); + } + + @After + public void tearDown() { + endpoint.reset(); + reset(mockRegistry, mockMeter); + } + + @Test + public void testValueSetInUri() throws Exception { + Object body = new Object(); + endpoint.expectedBodiesReceived(body); + producer1.sendBody(body); + endpoint.assertIsSatisfied(); + inOrder.verify(mockRegistry, times(1)).meter("A"); + inOrder.verify(mockMeter, times(1)).mark(3179L); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testValueNoSetInUri() throws Exception { + Object body = new Object(); + endpoint.expectedBodiesReceived(body); + producer2.sendBody(body); + endpoint.assertIsSatisfied(); + inOrder.verify(mockRegistry, times(1)).meter("A"); + inOrder.verify(mockMeter, times(1)).mark(); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testOverrideMetricsName() throws Exception { + when(mockRegistry.meter("B")).thenReturn(mockMeter); + Object body = new Object(); + endpoint.expectedBodiesReceived(body); + producer1.sendBodyAndHeader(body, HEADER_METRIC_NAME, "B"); + endpoint.assertIsSatisfied(); + inOrder.verify(mockRegistry, times(1)).meter("B"); + inOrder.verify(mockMeter, times(1)).mark(3179L); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testOverrideValueWithHeader() throws Exception { + Object body = new Object(); + endpoint.expectedBodiesReceived(body); + producer1.sendBodyAndHeader(body, HEADER_METER_MARK, 9926L); + endpoint.assertIsSatisfied(); + inOrder.verify(mockRegistry, times(1)).meter("A"); + inOrder.verify(mockMeter, times(1)).mark(9926L); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testValueNoSetInUriOverrideWithHeader() throws Exception { + Object body = new Object(); + endpoint.expectedBodiesReceived(body); + producer2.sendBodyAndHeader(body, HEADER_METER_MARK, 7707370L); + endpoint.assertIsSatisfied(); + inOrder.verify(mockRegistry, times(1)).meter("A"); + inOrder.verify(mockMeter, times(1)).mark(7707370L); + inOrder.verifyNoMoreInteractions(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricComponentSpringTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricComponentSpringTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricComponentSpringTest.java index 2cd5335..8ddd5ca 100644 --- a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricComponentSpringTest.java +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricComponentSpringTest.java @@ -34,6 +34,7 @@ import org.mockito.Mockito; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.ContextConfiguration; + import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentRouteTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentRouteTest.java index 7b14213..6f71bf6 100644 --- a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentRouteTest.java +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentRouteTest.java @@ -19,12 +19,14 @@ package org.apache.camel.component.metrics; import java.util.Date; import java.util.HashMap; import java.util.Map; + import org.apache.camel.Produce; import org.apache.camel.ProducerTemplate; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test; + import static org.apache.camel.component.metrics.MetricsConstants.HEADER_HISTOGRAM_VALUE; import static org.apache.camel.component.metrics.MetricsConstants.HEADER_METRIC_NAME; import static org.apache.camel.component.metrics.MetricsConstants.HEADER_PERFIX; http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java index fd4b4a2..ec687a0 100644 --- a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java @@ -19,14 +19,11 @@ package org.apache.camel.component.metrics; import java.util.EnumSet; import java.util.HashMap; import java.util.Map; + import com.codahale.metrics.MetricRegistry; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.RuntimeCamelException; -import org.apache.camel.component.metrics.counter.CounterEndpoint; -import org.apache.camel.component.metrics.histogram.HistogramEndpoint; -import org.apache.camel.component.metrics.meter.MeterEndpoint; -import org.apache.camel.component.metrics.timer.TimerEndpoint; import org.apache.camel.spi.Registry; import org.junit.Before; import org.junit.Test; @@ -35,6 +32,7 @@ import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; + import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.not; @@ -63,7 +61,6 @@ public class MetricsComponentTest { public void setUp() throws Exception { component = new MetricsComponent(); inOrder = Mockito.inOrder(camelContext, camelRegistry, metricRegistry); - } @Test @@ -76,8 +73,8 @@ public class MetricsComponentTest { params.put("mark", value); Endpoint result = component.createEndpoint("metrics:meter:long.meter", "meter:long.meter", params); assertThat(result, is(notNullValue())); - assertThat(result, is(instanceOf(MeterEndpoint.class))); - MeterEndpoint me = (MeterEndpoint) result; + assertThat(result, is(instanceOf(MetricsEndpoint.class))); + MetricsEndpoint me = (MetricsEndpoint) result; assertThat(me.getMark(), is(value)); assertThat(me.getMetricsName(), is("long.meter")); assertThat(me.getRegistry(), is(metricRegistry)); @@ -97,8 +94,8 @@ public class MetricsComponentTest { params.put("mark", value); Endpoint result = component.createEndpoint("metrics:meter:long.meter", "meter:long.meter", params); assertThat(result, is(notNullValue())); - assertThat(result, is(instanceOf(MeterEndpoint.class))); - MeterEndpoint me = (MeterEndpoint) result; + assertThat(result, is(instanceOf(MetricsEndpoint.class))); + MetricsEndpoint me = (MetricsEndpoint) result; assertThat(me.getMark(), is(value)); assertThat(me.getMetricsName(), is("long.meter")); assertThat(me.getRegistry(), is(metricRegistry)); @@ -109,8 +106,8 @@ public class MetricsComponentTest { result = component.createEndpoint("metrics:counter:long.counter", "counter:long.counter", params); assertThat(result, is(notNullValue())); - assertThat(result, is(instanceOf(CounterEndpoint.class))); - CounterEndpoint ce = (CounterEndpoint) result; + assertThat(result, is(instanceOf(MetricsEndpoint.class))); + MetricsEndpoint ce = (MetricsEndpoint) result; assertThat(ce.getIncrement(), is(value + 1)); assertThat(ce.getDecrement(), is(value - 1)); assertThat(ce.getMetricsName(), is("long.counter")); @@ -133,35 +130,36 @@ public class MetricsComponentTest { @Test public void testCreateNewEndpointForCounter() throws Exception { - Endpoint endpoint = component.createNewEndpoint(metricRegistry, MetricsType.COUNTER, "a name"); + Endpoint endpoint = new MetricsEndpoint(null, null, metricRegistry, MetricsType.COUNTER, "a name"); assertThat(endpoint, is(notNullValue())); - assertThat(endpoint, is(instanceOf(CounterEndpoint.class))); + assertThat(endpoint, is(instanceOf(MetricsEndpoint.class))); } @Test public void testCreateNewEndpointForMeter() throws Exception { - Endpoint endpoint = component.createNewEndpoint(metricRegistry, MetricsType.METER, "a name"); + Endpoint endpoint = new MetricsEndpoint(null, null, metricRegistry, MetricsType.METER, "a name"); assertThat(endpoint, is(notNullValue())); - assertThat(endpoint, is(instanceOf(MeterEndpoint.class))); + assertThat(endpoint, is(instanceOf(MetricsEndpoint.class))); } - @Test(expected = RuntimeCamelException.class) + @Test(expected = IllegalArgumentException.class) public void testCreateNewEndpointForGauge() throws Exception { - component.createNewEndpoint(metricRegistry, MetricsType.GAUGE, "a name"); + MetricsEndpoint endpoint = new MetricsEndpoint(null, null, metricRegistry, MetricsType.GAUGE, "a name"); + endpoint.createProducer(); } @Test public void testCreateNewEndpointForHistogram() throws Exception { - Endpoint endpoint = component.createNewEndpoint(metricRegistry, MetricsType.HISTOGRAM, "a name"); + Endpoint endpoint = new MetricsEndpoint(null, null, metricRegistry, MetricsType.HISTOGRAM, "a name"); assertThat(endpoint, is(notNullValue())); - assertThat(endpoint, is(instanceOf(HistogramEndpoint.class))); + assertThat(endpoint, is(instanceOf(MetricsEndpoint.class))); } @Test public void testCreateNewEndpointForTimer() throws Exception { - Endpoint endpoint = component.createNewEndpoint(metricRegistry, MetricsType.TIMER, "a name"); + Endpoint endpoint = new MetricsEndpoint(null, null, metricRegistry, MetricsType.TIMER, "a name"); assertThat(endpoint, is(notNullValue())); - assertThat(endpoint, is(instanceOf(TimerEndpoint.class))); + assertThat(endpoint, is(instanceOf(MetricsEndpoint.class))); } @Test http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsEndpointTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsEndpointTest.java new file mode 100644 index 0000000..12978f0 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsEndpointTest.java @@ -0,0 +1,106 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics; + +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.RuntimeCamelException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class MetricsEndpointTest { + + private static final String METRICS_NAME = "metrics.name"; + + @Mock + private MetricRegistry registry; + + @Mock + private Processor processor; + + @Mock + private Exchange exchange; + + @Mock + private Message in; + + private MetricsEndpoint endpoint; + + private InOrder inOrder; + + @Before + public void setUp() throws Exception { + endpoint = new MetricsEndpoint(null, null, registry, MetricsType.METER, METRICS_NAME) { + @Override + public Producer createProducer() throws Exception { + return null; + } + + @Override + protected String createEndpointUri() { + return "not real endpoint"; + } + }; + inOrder = Mockito.inOrder(registry, processor, exchange, in); + when(exchange.getIn()).thenReturn(in); + } + + @After + public void tearDown() throws Exception { + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testAbstractMetricsEndpoint() throws Exception { + assertThat(endpoint.getMetricsName(), is(METRICS_NAME)); + assertThat(endpoint.getRegistry(), is(registry)); + } + + @Test(expected = RuntimeCamelException.class) + public void testCreateConsumer() throws Exception { + endpoint.createConsumer(processor); + } + + @Test + public void testIsSingleton() throws Exception { + assertThat(endpoint.isSingleton(), is(true)); + } + + @Test + public void testGetRegistry() throws Exception { + assertThat(endpoint.getRegistry(), is(registry)); + } + + @Test + public void testGetMetricsName() throws Exception { + assertThat(endpoint.getMetricsName(), is(METRICS_NAME)); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsTypeTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsTypeTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsTypeTest.java index 145a1db..e8f39d5 100644 --- a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsTypeTest.java +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsTypeTest.java @@ -19,6 +19,7 @@ package org.apache.camel.component.metrics; import java.util.EnumSet; import org.junit.Test; + import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/TimerEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/TimerEndpointTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/TimerEndpointTest.java new file mode 100644 index 0000000..a9b7929 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/TimerEndpointTest.java @@ -0,0 +1,85 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics; + +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Producer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; + +@RunWith(MockitoJUnitRunner.class) +public class TimerEndpointTest { + + private static final String METRICS_NAME = "metrics.name"; + + @Mock + private MetricRegistry registry; + + private MetricsEndpoint endpoint; + + private InOrder inOrder; + + @Before + public void setUp() throws Exception { + endpoint = new MetricsEndpoint(null, null, registry, MetricsType.TIMER, METRICS_NAME); + inOrder = Mockito.inOrder(registry); + } + + @After + public void tearDown() throws Exception { + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testTimerEndpoint() throws Exception { + assertThat(endpoint, is(notNullValue())); + assertThat(endpoint.getRegistry(), is(registry)); + assertThat(endpoint.getMetricsName(), is(METRICS_NAME)); + } + + @Test + public void testCreateProducer() throws Exception { + Producer producer = endpoint.createProducer(); + assertThat(producer, is(notNullValue())); + assertThat(producer, is(instanceOf(TimerProducer.class))); + } + + @Test + public void testGetAction() throws Exception { + assertThat(endpoint.getAction(), is(nullValue())); + } + + @Test + public void testSetAction() throws Exception { + assertThat(endpoint.getAction(), is(nullValue())); + endpoint.setAction(MetricsTimerAction.start); + assertThat(endpoint.getAction(), is(MetricsTimerAction.start)); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/TimerProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/TimerProducerTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/TimerProducerTest.java new file mode 100644 index 0000000..ce08aa9 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/TimerProducerTest.java @@ -0,0 +1,229 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.apache.camel.component.metrics.MetricsConstants.HEADER_TIMER_ACTION; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class TimerProducerTest { + + private static final String METRICS_NAME = "metrics.name"; + private static final String PROPERTY_NAME = "timer" + ":" + METRICS_NAME; + + @Mock + private MetricsEndpoint endpoint; + + @Mock + private Exchange exchange; + + @Mock + private MetricRegistry registry; + + @Mock + private Timer timer; + + @Mock + private Timer.Context context; + + @Mock + private Message in; + + private TimerProducer producer; + + @Mock + private InOrder inOrder; + + @Before + public void setUp() throws Exception { + producer = new TimerProducer(endpoint); + inOrder = Mockito.inOrder(endpoint, exchange, registry, timer, context, in); + when(endpoint.getRegistry()).thenReturn(registry); + when(registry.timer(METRICS_NAME)).thenReturn(timer); + when(timer.time()).thenReturn(context); + when(exchange.getIn()).thenReturn(in); + } + + @Test + public void testTimerProducer() throws Exception { + assertThat(producer, is(notNullValue())); + assertThat(producer.getEndpoint().equals(endpoint), is(true)); + } + + @Test + public void testProcessStart() throws Exception { + when(endpoint.getAction()).thenReturn(MetricsTimerAction.start); + when(in.getHeader(HEADER_TIMER_ACTION, MetricsTimerAction.start, MetricsTimerAction.class)).thenReturn(MetricsTimerAction.start); + when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(null); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(endpoint, times(1)).getAction(); + inOrder.verify(in, times(1)).getHeader(HEADER_TIMER_ACTION, MetricsTimerAction.start, MetricsTimerAction.class); + inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); + inOrder.verify(registry, times(1)).timer(METRICS_NAME); + inOrder.verify(timer, times(1)).time(); + inOrder.verify(exchange, times(1)).setProperty(PROPERTY_NAME, context); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testProcessStartWithOverride() throws Exception { + when(endpoint.getAction()).thenReturn(MetricsTimerAction.start); + when(in.getHeader(HEADER_TIMER_ACTION, MetricsTimerAction.start, MetricsTimerAction.class)).thenReturn(MetricsTimerAction.stop); + when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(context); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(endpoint, times(1)).getAction(); + inOrder.verify(in, times(1)).getHeader(HEADER_TIMER_ACTION, MetricsTimerAction.start, MetricsTimerAction.class); + inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); + inOrder.verify(context, times(1)).stop(); + inOrder.verify(exchange, times(1)).removeProperty(PROPERTY_NAME); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testProcessStop() throws Exception { + when(endpoint.getAction()).thenReturn(MetricsTimerAction.stop); + when(in.getHeader(HEADER_TIMER_ACTION, MetricsTimerAction.stop, MetricsTimerAction.class)).thenReturn(MetricsTimerAction.stop); + when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(context); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(endpoint, times(1)).getAction(); + inOrder.verify(in, times(1)).getHeader(HEADER_TIMER_ACTION, MetricsTimerAction.stop, MetricsTimerAction.class); + inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); + inOrder.verify(context, times(1)).stop(); + inOrder.verify(exchange, times(1)).removeProperty(PROPERTY_NAME); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testProcessStopWithOverride() throws Exception { + when(endpoint.getAction()).thenReturn(MetricsTimerAction.stop); + when(in.getHeader(HEADER_TIMER_ACTION, MetricsTimerAction.stop, MetricsTimerAction.class)).thenReturn(MetricsTimerAction.start); + when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(null); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(endpoint, times(1)).getAction(); + inOrder.verify(in, times(1)).getHeader(HEADER_TIMER_ACTION, MetricsTimerAction.stop, MetricsTimerAction.class); + inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); + inOrder.verify(registry, times(1)).timer(METRICS_NAME); + inOrder.verify(timer, times(1)).time(); + inOrder.verify(exchange, times(1)).setProperty(PROPERTY_NAME, context); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testProcessNoAction() throws Exception { + when(endpoint.getAction()).thenReturn(null); + when(in.getHeader(HEADER_TIMER_ACTION, null, MetricsTimerAction.class)).thenReturn(null); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(endpoint, times(1)).getAction(); + inOrder.verify(in, times(1)).getHeader(HEADER_TIMER_ACTION, null, MetricsTimerAction.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testProcessNoActionOverride() throws Exception { + when(endpoint.getAction()).thenReturn(null); + when(in.getHeader(HEADER_TIMER_ACTION, null, MetricsTimerAction.class)).thenReturn(MetricsTimerAction.start); + producer.doProcess(exchange, endpoint, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(endpoint, times(1)).getAction(); + inOrder.verify(in, times(1)).getHeader(HEADER_TIMER_ACTION, null, MetricsTimerAction.class); + inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); + inOrder.verify(registry, times(1)).timer(METRICS_NAME); + inOrder.verify(timer, times(1)).time(); + inOrder.verify(exchange, times(1)).setProperty(PROPERTY_NAME, context); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testHandleStart() throws Exception { + when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(null); + producer.handleStart(exchange, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); + inOrder.verify(registry, times(1)).timer(METRICS_NAME); + inOrder.verify(timer, times(1)).time(); + inOrder.verify(exchange, times(1)).setProperty(PROPERTY_NAME, context); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testHandleStartAlreadyRunning() throws Exception { + when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(context); + producer.handleStart(exchange, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testHandleStop() throws Exception { + when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(context); + producer.handleStop(exchange, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); + inOrder.verify(context, times(1)).stop(); + inOrder.verify(exchange, times(1)).removeProperty(PROPERTY_NAME); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testHandleStopContextNotFound() throws Exception { + when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(null); + producer.handleStop(exchange, registry, METRICS_NAME); + inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testGetPropertyName() throws Exception { + assertThat(producer.getPropertyName(METRICS_NAME), is("timer" + ":" + METRICS_NAME)); + } + + @Test + public void testGetTimerContextFromExchange() throws Exception { + when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(context); + assertThat(producer.getTimerContextFromExchange(exchange, PROPERTY_NAME), is(context)); + inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testGetTimerContextFromExchangeNotFound() throws Exception { + when(exchange.getProperty(PROPERTY_NAME, Timer.Context.class)).thenReturn(null); + assertThat(producer.getTimerContextFromExchange(exchange, PROPERTY_NAME), is(nullValue())); + inOrder.verify(exchange, times(1)).getProperty(PROPERTY_NAME, Timer.Context.class); + inOrder.verifyNoMoreInteractions(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/TimerRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/TimerRouteTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/TimerRouteTest.java new file mode 100644 index 0000000..adc6913 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/TimerRouteTest.java @@ -0,0 +1,144 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.metrics; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration; +import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader; +import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner; +import org.apache.camel.test.spring.MockEndpoints; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InOrder; +import org.mockito.Mockito; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; + +import static org.apache.camel.component.metrics.MetricsComponent.METRIC_REGISTRY_NAME; +import static org.apache.camel.component.metrics.MetricsConstants.HEADER_METRIC_NAME; +import static org.apache.camel.component.metrics.MetricsConstants.HEADER_TIMER_ACTION; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + +@RunWith(CamelSpringJUnit4ClassRunner.class) +@ContextConfiguration( + classes = { TimerRouteTest.TestConfig.class }, + loader = CamelSpringDelegatingTestContextLoader.class) +@MockEndpoints +public class TimerRouteTest { + + @EndpointInject(uri = "mock:out") + private MockEndpoint endpoint; + + @Produce(uri = "direct:in-1") + private ProducerTemplate producer1; + + @Produce(uri = "direct:in-2") + private ProducerTemplate producer2; + + private MetricRegistry mockRegistry; + + private Timer mockTimer; + + private InOrder inOrder; + + @Configuration + public static class TestConfig extends SingleRouteCamelConfiguration { + + @Bean + @Override + public RouteBuilder route() { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + from("direct:in-1") + .to("metrics:timer:A?action=start") + .to("mock:out"); + + from("direct:in-2") + .to("metrics:timer:A") + .to("mock:out"); + } + }; + } + + @Bean(name = METRIC_REGISTRY_NAME) + public MetricRegistry getMetricRegistry() { + return Mockito.mock(MetricRegistry.class); + } + } + + @Before + public void setup() { + // TODO - 12.05.2014, Lauri - is there any better way to set this up? + mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(METRIC_REGISTRY_NAME, MetricRegistry.class); + mockTimer = Mockito.mock(Timer.class); + inOrder = Mockito.inOrder(mockRegistry, mockTimer); + } + + @After + public void tearDown() { + endpoint.reset(); + reset(mockRegistry, mockTimer); + } + + @Test + public void testOverrideMetricsName() throws Exception { + when(mockRegistry.timer("B")).thenReturn(mockTimer); + + Object body = new Object(); + endpoint.expectedBodiesReceived(body); + producer1.sendBodyAndHeader(body, HEADER_METRIC_NAME, "B"); + endpoint.assertIsSatisfied(); + inOrder.verify(mockRegistry, times(1)).timer("B"); + inOrder.verify(mockTimer, times(1)).time(); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testOverrideExistingAction() throws Exception { + when(mockRegistry.timer("A")).thenReturn(mockTimer); + Object body = new Object(); + endpoint.expectedBodiesReceived(body); + producer1.sendBodyAndHeader(body, HEADER_TIMER_ACTION, MetricsTimerAction.stop); + endpoint.assertIsSatisfied(); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testOverrideNoAction() throws Exception { + when(mockRegistry.timer("A")).thenReturn(mockTimer); + Object body = new Object(); + endpoint.expectedBodiesReceived(body); + producer2.sendBodyAndHeader(body, HEADER_TIMER_ACTION, MetricsTimerAction.start); + endpoint.assertIsSatisfied(); + inOrder.verify(mockRegistry, times(1)).timer("A"); + inOrder.verify(mockTimer, times(1)).time(); + inOrder.verifyNoMoreInteractions(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterEndpointTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterEndpointTest.java deleted file mode 100644 index c5da95e..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterEndpointTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.metrics.counter; - -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.Producer; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InOrder; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.nullValue; - -@RunWith(MockitoJUnitRunner.class) -public class CounterEndpointTest { - - private static final String METRICS_NAME = "metrics.name"; - private static final Long VALUE = System.currentTimeMillis(); - - @Mock - private MetricRegistry registry; - - private CounterEndpoint endpoint; - - private InOrder inOrder; - - @Before - public void setUp() throws Exception { - endpoint = new CounterEndpoint(registry, METRICS_NAME); - inOrder = Mockito.inOrder(registry); - } - - @After - public void tearDown() { - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testCounterEndpoint() throws Exception { - assertThat(endpoint.getRegistry(), is(registry)); - assertThat(endpoint.getMetricsName(), is(METRICS_NAME)); - assertThat(endpoint.getIncrement(), is(nullValue())); - assertThat(endpoint.getDecrement(), is(nullValue())); - } - - @Test - public void testCreateProducer() throws Exception { - Producer producer = endpoint.createProducer(); - assertThat(producer, is(notNullValue())); - assertThat(producer, is(instanceOf(CounterProducer.class))); - } - - @Test - public void testGetIncrement() throws Exception { - assertThat(endpoint.getIncrement(), is(nullValue())); - } - - @Test - public void testSetIncrement() throws Exception { - assertThat(endpoint.getIncrement(), is(nullValue())); - endpoint.setIncrement(VALUE); - assertThat(endpoint.getIncrement(), is(VALUE)); - } - - @Test - public void testGetDecrement() throws Exception { - assertThat(endpoint.getDecrement(), is(nullValue())); - } - - @Test - public void testSetDecrement() throws Exception { - assertThat(endpoint.getDecrement(), is(nullValue())); - endpoint.setDecrement(VALUE); - assertThat(endpoint.getDecrement(), is(VALUE)); - } - - @Test - public void testCreateEndpointUri() throws Exception { - assertThat(endpoint.createEndpointUri(), is(CounterEndpoint.ENDPOINT_URI)); - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterProducerTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterProducerTest.java deleted file mode 100644 index 4878cde..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterProducerTest.java +++ /dev/null @@ -1,195 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.metrics.counter; - -import com.codahale.metrics.Counter; -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.Exchange; -import org.apache.camel.Message; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InOrder; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; -import static org.apache.camel.component.metrics.MetricsConstants.HEADER_COUNTER_DECREMENT; -import static org.apache.camel.component.metrics.MetricsConstants.HEADER_COUNTER_INCREMENT; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class CounterProducerTest { - - private static final String METRICS_NAME = "metrics.name"; - private static final Long INCREMENT = 100000L; - private static final Long DECREMENT = 91929199L; - - @Mock - private CounterEndpoint endpoint; - - @Mock - private Exchange exchange; - - @Mock - private MetricRegistry registry; - - @Mock - private Counter counter; - - @Mock - private Message in; - - private CounterProducer producer; - - private InOrder inOrder; - - @Before - public void setUp() throws Exception { - producer = new CounterProducer(endpoint); - inOrder = Mockito.inOrder(endpoint, exchange, registry, counter, in); - when(endpoint.getRegistry()).thenReturn(registry); - when(registry.counter(METRICS_NAME)).thenReturn(counter); - when(exchange.getIn()).thenReturn(in); - } - - @Test - public void testCounterProducer() throws Exception { - assertThat(producer.getEndpoint().equals(endpoint), is(true)); - } - - @Test - public void testProcessWithIncrementOnly() throws Exception { - when(endpoint.getIncrement()).thenReturn(INCREMENT); - when(endpoint.getDecrement()).thenReturn(null); - when(in.getHeader(HEADER_COUNTER_INCREMENT, INCREMENT, Long.class)).thenReturn(INCREMENT); - when(in.getHeader(HEADER_COUNTER_DECREMENT, null, Long.class)).thenReturn(null); - producer.doProcess(exchange, endpoint, registry, METRICS_NAME); - inOrder.verify(exchange, times(1)).getIn(); - inOrder.verify(registry, times(1)).counter(METRICS_NAME); - inOrder.verify(endpoint, times(1)).getIncrement(); - inOrder.verify(endpoint, times(1)).getDecrement(); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_INCREMENT, INCREMENT, Long.class); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_DECREMENT, null, Long.class); - inOrder.verify(counter, times(1)).inc(INCREMENT); - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testProcessWithDecrementOnly() throws Exception { - when(endpoint.getIncrement()).thenReturn(null); - when(endpoint.getDecrement()).thenReturn(DECREMENT); - when(in.getHeader(HEADER_COUNTER_INCREMENT, null, Long.class)).thenReturn(null); - when(in.getHeader(HEADER_COUNTER_DECREMENT, DECREMENT, Long.class)).thenReturn(DECREMENT); - producer.doProcess(exchange, endpoint, registry, METRICS_NAME); - inOrder.verify(exchange, times(1)).getIn(); - inOrder.verify(registry, times(1)).counter(METRICS_NAME); - inOrder.verify(endpoint, times(1)).getIncrement(); - inOrder.verify(endpoint, times(1)).getDecrement(); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_INCREMENT, null, Long.class); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_DECREMENT, DECREMENT, Long.class); - inOrder.verify(counter, times(1)).dec(DECREMENT); - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testDoProcessWithIncrementAndDecrement() throws Exception { - when(endpoint.getIncrement()).thenReturn(INCREMENT); - when(endpoint.getDecrement()).thenReturn(DECREMENT); - when(in.getHeader(HEADER_COUNTER_INCREMENT, INCREMENT, Long.class)).thenReturn(INCREMENT); - when(in.getHeader(HEADER_COUNTER_DECREMENT, DECREMENT, Long.class)).thenReturn(DECREMENT); - producer.doProcess(exchange, endpoint, registry, METRICS_NAME); - inOrder.verify(exchange, times(1)).getIn(); - inOrder.verify(registry, times(1)).counter(METRICS_NAME); - inOrder.verify(endpoint, times(1)).getIncrement(); - inOrder.verify(endpoint, times(1)).getDecrement(); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_INCREMENT, INCREMENT, Long.class); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_DECREMENT, DECREMENT, Long.class); - inOrder.verify(counter, times(1)).inc(INCREMENT); - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testProcessWithOutIncrementAndDecrement() throws Exception { - when(endpoint.getIncrement()).thenReturn(null); - when(endpoint.getDecrement()).thenReturn(null); - when(in.getHeader(HEADER_COUNTER_INCREMENT, null, Long.class)).thenReturn(null); - when(in.getHeader(HEADER_COUNTER_DECREMENT, null, Long.class)).thenReturn(null); - producer.doProcess(exchange, endpoint, registry, METRICS_NAME); - inOrder.verify(exchange, times(1)).getIn(); - inOrder.verify(registry, times(1)).counter(METRICS_NAME); - inOrder.verify(endpoint, times(1)).getIncrement(); - inOrder.verify(endpoint, times(1)).getDecrement(); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_INCREMENT, null, Long.class); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_DECREMENT, null, Long.class); - inOrder.verify(counter, times(1)).inc(); - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testProcessWithHeaderValuesOnly() throws Exception { - when(endpoint.getIncrement()).thenReturn(null); - when(endpoint.getDecrement()).thenReturn(null); - when(in.getHeader(HEADER_COUNTER_INCREMENT, null, Long.class)).thenReturn(INCREMENT + 1); - when(in.getHeader(HEADER_COUNTER_DECREMENT, null, Long.class)).thenReturn(DECREMENT - 1); - producer.doProcess(exchange, endpoint, registry, METRICS_NAME); - inOrder.verify(exchange, times(1)).getIn(); - inOrder.verify(registry, times(1)).counter(METRICS_NAME); - inOrder.verify(endpoint, times(1)).getIncrement(); - inOrder.verify(endpoint, times(1)).getDecrement(); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_INCREMENT, null, Long.class); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_DECREMENT, null, Long.class); - inOrder.verify(counter, times(1)).inc(INCREMENT + 1); - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testProcessOverridingIncrement() throws Exception { - when(endpoint.getIncrement()).thenReturn(INCREMENT); - when(endpoint.getDecrement()).thenReturn(DECREMENT); - when(in.getHeader(HEADER_COUNTER_INCREMENT, INCREMENT, Long.class)).thenReturn(INCREMENT + 1); - when(in.getHeader(HEADER_COUNTER_DECREMENT, DECREMENT, Long.class)).thenReturn(DECREMENT); - producer.doProcess(exchange, endpoint, registry, METRICS_NAME); - inOrder.verify(exchange, times(1)).getIn(); - inOrder.verify(registry, times(1)).counter(METRICS_NAME); - inOrder.verify(endpoint, times(1)).getIncrement(); - inOrder.verify(endpoint, times(1)).getDecrement(); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_INCREMENT, INCREMENT, Long.class); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_DECREMENT, DECREMENT, Long.class); - inOrder.verify(counter, times(1)).inc(INCREMENT + 1); - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testProcessOverridingDecrement() throws Exception { - when(endpoint.getIncrement()).thenReturn(null); - when(endpoint.getDecrement()).thenReturn(DECREMENT); - when(in.getHeader(HEADER_COUNTER_INCREMENT, null, Long.class)).thenReturn(null); - when(in.getHeader(HEADER_COUNTER_DECREMENT, DECREMENT, Long.class)).thenReturn(DECREMENT - 1); - producer.doProcess(exchange, endpoint, registry, METRICS_NAME); - inOrder.verify(exchange, times(1)).getIn(); - inOrder.verify(registry, times(1)).counter(METRICS_NAME); - inOrder.verify(endpoint, times(1)).getIncrement(); - inOrder.verify(endpoint, times(1)).getDecrement(); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_INCREMENT, null, Long.class); - inOrder.verify(in, times(1)).getHeader(HEADER_COUNTER_DECREMENT, DECREMENT, Long.class); - inOrder.verify(counter, times(1)).dec(DECREMENT - 1); - inOrder.verifyNoMoreInteractions(); - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterRouteTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterRouteTest.java deleted file mode 100644 index 0540822..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterRouteTest.java +++ /dev/null @@ -1,210 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.metrics.counter; - -import java.util.HashMap; -import java.util.Map; - -import com.codahale.metrics.Counter; -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.EndpointInject; -import org.apache.camel.Produce; -import org.apache.camel.ProducerTemplate; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration; -import org.apache.camel.test.spring.CamelSpringDelegatingTestContextLoader; -import org.apache.camel.test.spring.CamelSpringJUnit4ClassRunner; -import org.apache.camel.test.spring.MockEndpoints; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InOrder; -import org.mockito.Mockito; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; - -import static org.apache.camel.component.metrics.MetricsComponent.METRIC_REGISTRY_NAME; -import static org.apache.camel.component.metrics.MetricsConstants.HEADER_COUNTER_DECREMENT; -import static org.apache.camel.component.metrics.MetricsConstants.HEADER_COUNTER_INCREMENT; -import static org.apache.camel.component.metrics.MetricsConstants.HEADER_METRIC_NAME; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.when; - -@RunWith(CamelSpringJUnit4ClassRunner.class) -@ContextConfiguration( - classes = { CounterRouteTest.TestConfig.class }, - loader = CamelSpringDelegatingTestContextLoader.class) -@MockEndpoints -public class CounterRouteTest { - - @EndpointInject(uri = "mock:out") - private MockEndpoint endpoint; - - @Produce(uri = "direct:in-1") - private ProducerTemplate producer1; - - @Produce(uri = "direct:in-2") - private ProducerTemplate producer2; - - @Produce(uri = "direct:in-3") - private ProducerTemplate producer3; - - @Produce(uri = "direct:in-4") - private ProducerTemplate producer4; - - private MetricRegistry mockRegistry; - - private Counter mockCounter; - - private InOrder inOrder; - - @Configuration - public static class TestConfig extends SingleRouteCamelConfiguration { - - @Bean - @Override - public RouteBuilder route() { - return new RouteBuilder() { - - @Override - public void configure() throws Exception { - from("direct:in-1") - .to("metrics:counter:A?increment=5") - .to("mock:out"); - - from("direct:in-2") - .to("metrics:counter:A?decrement=9") - .to("mock:out"); - - from("direct:in-3") - .setHeader(HEADER_COUNTER_INCREMENT, constant(417L)) - .to("metrics:counter:A") - .to("mock:out"); - - from("direct:in-4") - .setHeader(HEADER_COUNTER_INCREMENT, simple("${body.length}")) - .to("metrics:counter:A") - .to("mock:out"); - } - }; - } - - @Bean(name = METRIC_REGISTRY_NAME) - public MetricRegistry getMetricRegistry() { - return Mockito.mock(MetricRegistry.class); - } - } - - @Before - public void setup() { - // TODO - 12.05.2014, Lauri - is there any better way to set this up? - mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(METRIC_REGISTRY_NAME, MetricRegistry.class); - mockCounter = mock(Counter.class); - inOrder = Mockito.inOrder(mockRegistry, mockCounter); - } - - @After - public void tearDown() { - endpoint.reset(); - reset(mockRegistry, mockCounter); - } - - @Test - public void testOverrideMetricsName() throws Exception { - when(mockRegistry.counter("B")).thenReturn(mockCounter); - endpoint.expectedMessageCount(1); - producer1.sendBodyAndHeader(new Object(), HEADER_METRIC_NAME, "B"); - endpoint.assertIsSatisfied(); - inOrder.verify(mockRegistry, times(1)).counter("B"); - inOrder.verify(mockCounter, times(1)).inc(5L); - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testOverrideIncrement() throws Exception { - when(mockRegistry.counter("A")).thenReturn(mockCounter); - endpoint.expectedMessageCount(1); - producer1.sendBodyAndHeader(new Object(), HEADER_COUNTER_INCREMENT, 14L); - endpoint.assertIsSatisfied(); - inOrder.verify(mockRegistry, times(1)).counter("A"); - inOrder.verify(mockCounter, times(1)).inc(14L); - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testOverrideIncrementAndDecrement() throws Exception { - when(mockRegistry.counter("A")).thenReturn(mockCounter); - endpoint.expectedMessageCount(1); - Map<String, Object> headers = new HashMap<String, Object>(); - headers.put(HEADER_COUNTER_INCREMENT, 912L); - headers.put(HEADER_COUNTER_DECREMENT, 43219L); - producer1.sendBodyAndHeaders(new Object(), headers); - endpoint.assertIsSatisfied(); - inOrder.verify(mockRegistry, times(1)).counter("A"); - inOrder.verify(mockCounter, times(1)).inc(912L); - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testOverrideDecrement() throws Exception { - when(mockRegistry.counter("A")).thenReturn(mockCounter); - endpoint.expectedMessageCount(1); - producer2.sendBodyAndHeader(new Object(), HEADER_COUNTER_DECREMENT, 7L); - endpoint.assertIsSatisfied(); - inOrder.verify(mockRegistry, times(1)).counter("A"); - inOrder.verify(mockCounter, times(1)).dec(7L); - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testOverrideIncrementWithWrongType() throws Exception { - when(mockRegistry.counter("A")).thenReturn(mockCounter); - endpoint.expectedMessageCount(1); - producer1.sendBodyAndHeader(new Object(), HEADER_COUNTER_INCREMENT, "this is not a valid long value"); - endpoint.assertIsSatisfied(); - inOrder.verify(mockRegistry, times(1)).counter("A"); - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testOverrideUsingConstantValue() throws Exception { - when(mockRegistry.counter("A")).thenReturn(mockCounter); - endpoint.expectedMessageCount(1); - producer3.sendBody(new Object()); - endpoint.assertIsSatisfied(); - inOrder.verify(mockRegistry, times(1)).counter("A"); - inOrder.verify(mockCounter, times(1)).inc(417L); - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testOverrideUsingScriptEvaluation() throws Exception { - when(mockRegistry.counter("A")).thenReturn(mockCounter); - endpoint.expectedMessageCount(1); - String message = "Hello from Camel Metrics!"; - producer4.sendBody(message); - endpoint.assertIsSatisfied(); - inOrder.verify(mockRegistry, times(1)).counter("A"); - inOrder.verify(mockCounter, times(1)).inc(message.length()); - inOrder.verifyNoMoreInteractions(); - } -} http://git-wip-us.apache.org/repos/asf/camel/blob/4a84f061/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramEndpointTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramEndpointTest.java deleted file mode 100644 index 86049d5..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramEndpointTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.metrics.histogram; - -import com.codahale.metrics.MetricRegistry; -import org.apache.camel.Producer; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InOrder; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.nullValue; - -@RunWith(MockitoJUnitRunner.class) -public class HistogramEndpointTest { - - private static final String METRICS_NAME = "metrics.name"; - private static final Long VALUE = System.currentTimeMillis(); - - @Mock - private MetricRegistry registry; - - private HistogramEndpoint endpoint; - - private InOrder inOrder; - - @Before - public void setUp() throws Exception { - endpoint = new HistogramEndpoint(registry, METRICS_NAME); - inOrder = Mockito.inOrder(registry); - } - - @After - public void tearDown() throws Exception { - inOrder.verifyNoMoreInteractions(); - } - - @Test - public void testHistogramEndpoint() throws Exception { - assertThat(endpoint, is(notNullValue())); - assertThat(endpoint.getRegistry(), is(registry)); - assertThat(endpoint.getMetricsName(), is(METRICS_NAME)); - } - - @Test - public void testCreateProducer() throws Exception { - Producer producer = endpoint.createProducer(); - assertThat(producer, is(notNullValue())); - assertThat(producer, is(HistogramProducer.class)); - } - - @Test - public void testGetValue() throws Exception { - assertThat(endpoint.getValue(), is(nullValue())); - } - - @Test - public void testSetValue() throws Exception { - assertThat(endpoint.getValue(), is(nullValue())); - endpoint.setValue(VALUE); - assertThat(endpoint.getValue(), is(VALUE)); - } - - @Test - public void testCreateEndpointUri() throws Exception { - assertThat(endpoint.createEndpointUri(), is(HistogramEndpoint.ENDPOINT_URI)); - } -}