http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/AbstractMetricsEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/AbstractMetricsEndpointTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/AbstractMetricsEndpointTest.java new file mode 100644 index 0000000..e663153 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/AbstractMetricsEndpointTest.java @@ -0,0 +1,105 @@ +/** + * 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 AbstractMetricsEndpointTest { + + 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 AbstractMetricsEndpoint endpoint; + + private InOrder inOrder; + + @Before + public void setUp() throws Exception { + endpoint = new AbstractMetricsEndpoint(registry, 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(false)); + } + + @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/acf0fa39/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/AbstractMetricsProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/AbstractMetricsProducerTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/AbstractMetricsProducerTest.java new file mode 100644 index 0000000..f651389 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/AbstractMetricsProducerTest.java @@ -0,0 +1,186 @@ +/** + * 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.impl.DefaultMessage; +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.AbstractMetricsProducer.HEADER_PATTERN; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_HISTOGRAM_VALUE; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_METRIC_NAME; +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 AbstractMetricsProducerTest { + + public static final String METRIC_NAME = "a metric"; + + @Mock + private AbstractMetricsEndpoint endpoint; + + @Mock + private Exchange exchange; + + @Mock + private Message in; + + @Mock + private MetricRegistry registry; + + private AbstractMetricsProducer<AbstractMetricsEndpoint> okProducer; + + private AbstractMetricsProducer<AbstractMetricsEndpoint> failProducer; + + private InOrder inOrder; + + @Before + public void setUp() throws Exception { + okProducer = new AbstractMetricsProducer<AbstractMetricsEndpoint>(endpoint) { + @Override + protected void doProcess(Exchange exchange, AbstractMetricsEndpoint endpoint, MetricRegistry registry, String metricsName) throws Exception { + } + }; + failProducer = new AbstractMetricsProducer<AbstractMetricsEndpoint>(endpoint) { + + @Override + protected void doProcess(Exchange exchange, AbstractMetricsEndpoint endpoint, MetricRegistry registry, String metricsName) throws Exception { + throw new Exception("Muchos problemos"); + } + }; + inOrder = Mockito.inOrder(endpoint, exchange, in, registry); + when(exchange.getIn()).thenReturn(in); + when(endpoint.getMetricsName()).thenReturn(METRIC_NAME); + when(endpoint.getRegistry()).thenReturn(registry); + } + + @Test + public void testDoProcess() throws Exception { + when(in.getHeader(HEADER_METRIC_NAME, String.class)).thenReturn(null); + when(in.removeHeaders(HEADER_PATTERN)).thenReturn(true); + okProducer.process(exchange); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(endpoint, times(1)).getMetricsName(); + inOrder.verify(in, times(1)).getHeader(HEADER_METRIC_NAME, String.class); + inOrder.verify(endpoint, times(1)).getRegistry(); + inOrder.verify(in, times(1)).removeHeaders(HEADER_PATTERN); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testDoProcessWithException() throws Exception { + when(in.getHeader(HEADER_METRIC_NAME, String.class)).thenReturn(null); + when(in.removeHeaders(HEADER_PATTERN)).thenReturn(true); + failProducer.process(exchange); + inOrder.verify(exchange, times(1)).getIn(); + inOrder.verify(endpoint, times(1)).getMetricsName(); + inOrder.verify(in, times(1)).getHeader(HEADER_METRIC_NAME, String.class); + inOrder.verify(endpoint, times(1)).getRegistry(); + inOrder.verify(in, times(1)).removeHeaders(HEADER_PATTERN); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testGetMetricsName() throws Exception { + when(in.getHeader(HEADER_METRIC_NAME, String.class)).thenReturn("A"); + assertThat(okProducer.getMetricsName(in, "value"), is("A")); + inOrder.verify(in, times(1)).getHeader(HEADER_METRIC_NAME, String.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testGetMetricsNameNotSet() throws Exception { + when(in.getHeader(HEADER_METRIC_NAME, String.class)).thenReturn(null); + assertThat(okProducer.getMetricsName(in, "name"), is("name")); + inOrder.verify(in, times(1)).getHeader(HEADER_METRIC_NAME, String.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testGetStringHeader() throws Exception { + when(in.getHeader(HEADER_METRIC_NAME, String.class)).thenReturn("A"); + assertThat(okProducer.getStringHeader(in, HEADER_METRIC_NAME, "value"), is("A")); + inOrder.verify(in, times(1)).getHeader(HEADER_METRIC_NAME, String.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testGetStringHeaderWithNullValue() throws Exception { + when(in.getHeader(HEADER_METRIC_NAME, String.class)).thenReturn(null); + assertThat(okProducer.getStringHeader(in, HEADER_METRIC_NAME, "value"), is("value")); + inOrder.verify(in, times(1)).getHeader(HEADER_METRIC_NAME, String.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testGetStringHeaderWithWhiteSpaces() throws Exception { + when(in.getHeader(HEADER_METRIC_NAME, String.class)).thenReturn(" "); + assertThat(okProducer.getStringHeader(in, HEADER_METRIC_NAME, "value"), is("value")); + inOrder.verify(in, times(1)).getHeader(HEADER_METRIC_NAME, String.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testGetStringHeaderWithEmptySrting() throws Exception { + when(in.getHeader(HEADER_METRIC_NAME, String.class)).thenReturn(""); + assertThat(okProducer.getStringHeader(in, HEADER_METRIC_NAME, "value"), is("value")); + inOrder.verify(in, times(1)).getHeader(HEADER_METRIC_NAME, String.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testGetLongHeader() throws Exception { + when(in.getHeader(HEADER_HISTOGRAM_VALUE, 19L, Long.class)).thenReturn(201L); + assertThat(okProducer.getLongHeader(in, HEADER_HISTOGRAM_VALUE, 19L), is(201L)); + inOrder.verify(in, times(1)).getHeader(HEADER_HISTOGRAM_VALUE, 19L, Long.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testClearMetricsHeaders() throws Exception { + when(in.removeHeaders(HEADER_PATTERN)).thenReturn(true); + assertThat(okProducer.clearMetricsHeaders(in), is(true)); + inOrder.verify(in, times(1)).removeHeaders(HEADER_PATTERN); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testClearRealHeaders() throws Exception { + Message msg = new DefaultMessage(); + Object val = new Object(); + msg.setHeader(HEADER_HISTOGRAM_VALUE, 109L); + msg.setHeader(HEADER_METRIC_NAME, "the metric"); + msg.setHeader("notRemoved", val); + assertThat(msg.getHeaders().size(), is(3)); + assertThat(msg.getHeader(HEADER_HISTOGRAM_VALUE, Long.class), is(109L)); + assertThat(msg.getHeader(HEADER_METRIC_NAME, String.class), is("the metric")); + assertThat(msg.getHeader("notRemoved"), is(val)); + okProducer.clearMetricsHeaders(msg); + assertThat(msg.getHeaders().size(), is(1)); + assertThat(msg.getHeader("notRemoved"), is(val)); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/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 new file mode 100644 index 0000000..2cd5335 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricComponentSpringTest.java @@ -0,0 +1,91 @@ +/** + * 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.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.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.mockito.Mockito.times; +import static org.mockito.Mockito.when; + +@RunWith(CamelSpringJUnit4ClassRunner.class) +@ContextConfiguration( + classes = { MetricComponentSpringTest.TestConfig.class }, + loader = CamelSpringDelegatingTestContextLoader.class) +@MockEndpoints +public class MetricComponentSpringTest { + + @EndpointInject(uri = "mock:out") + private MockEndpoint endpoint; + + @Produce(uri = "direct:in") + private ProducerTemplate producer; + + @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:counter:A?increment=512") + .to("mock:out"); + } + }; + } + + @Bean(name = MetricsComponent.METRIC_REGISTRY_NAME) + public MetricRegistry getMetricRegistry() { + return Mockito.mock(MetricRegistry.class); + } + } + + @Test + public void testMetricsRegistryFromCamelRegistry() throws Exception { + // TODO - 12.05.2014, Lauri - is there any better way to set this up? + MetricRegistry mockRegistry = endpoint.getCamelContext().getRegistry().lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME, MetricRegistry.class); + Counter mockCounter = Mockito.mock(Counter.class); + InOrder inOrder = Mockito.inOrder(mockRegistry, mockCounter); + when(mockRegistry.counter("A")).thenReturn(mockCounter); + + endpoint.expectedMessageCount(1); + producer.sendBody(new Object()); + endpoint.assertIsSatisfied(); + inOrder.verify(mockRegistry, times(1)).counter("A"); + inOrder.verify(mockCounter, times(1)).inc(512L); + inOrder.verifyNoMoreInteractions(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/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 new file mode 100644 index 0000000..9fed201 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentRouteTest.java @@ -0,0 +1,113 @@ +/** + * 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 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.MetricsComponent.HEADER_HISTOGRAM_VALUE; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_METRIC_NAME; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_PERFIX; + +public class MetricsComponentRouteTest extends CamelTestSupport { + + @Produce(uri = "direct:start-1") + protected ProducerTemplate template1; + + @Produce(uri = "direct:start-2") + protected ProducerTemplate template2; + + @Test + public void testMetrics() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMinimumMessageCount(1); + template1.sendBody(new Object()); + assertMockEndpointsSatisfied(); + } + + @Test + public void testMessageContentDelivery() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + String body = "Message Body"; + String header1 = "Header 1"; + String header2 = "Header 2"; + Object value1 = new Date(); + Object value2 = System.currentTimeMillis(); + mock.expectedBodiesReceived(body); + mock.expectedHeaderReceived(header1, value1); + mock.expectedHeaderReceived(header2, value2); + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put(header1, value1); + headers.put(header2, value2); + template1.sendBodyAndHeaders(body, headers); + assertMockEndpointsSatisfied(); + } + + @Test + public void testHeaderRemoval() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + Object body = new Object(); + Date now = new Date(); + + mock.expectedBodiesReceived(body); + mock.expectedHeaderReceived("." + HEADER_PERFIX, "value"); + mock.expectedHeaderReceived("date", now); + + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put(HEADER_METRIC_NAME, "a name"); + headers.put(HEADER_HISTOGRAM_VALUE, 34L); + headers.put(HEADER_PERFIX + "notExistingHeader", "?"); + headers.put("." + HEADER_PERFIX, "value"); + headers.put("date", now); + + template2.sendBodyAndHeaders(body, headers); + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start-1") + .to("metrics:timer:T?action=start") + .to("metrics:A") + .to("metrics:counter://B") + .to("metrics:counter:C?increment=19291") + .to("metrics:counter:C?decrement=19292") + .to("metrics:counter:C") + .to("metrics:meter:D") + .to("metrics:meter:D?mark=90001") + .to("metrics:histogram:E") + .to("metrics:timer:T") + .to("metrics:histogram:E?value=12000000031") + .to("metrics:timer:T?action=stop") + .to("mock:result"); + + from("direct:start-2") + .to("metrics:meter:F?mark=88") + .to("mock:result"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/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 new file mode 100644 index 0000000..fd4b4a2 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsComponentTest.java @@ -0,0 +1,217 @@ +/** + * 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 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; +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.Matchers.instanceOf; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class MetricsComponentTest { + + @Mock + private CamelContext camelContext; + + @Mock + private Registry camelRegistry; + + @Mock + private MetricRegistry metricRegistry; + + private InOrder inOrder; + + private MetricsComponent component; + + @Before + public void setUp() throws Exception { + component = new MetricsComponent(); + inOrder = Mockito.inOrder(camelContext, camelRegistry, metricRegistry); + + } + + @Test + public void testCreateEndpoint() throws Exception { + component.setCamelContext(camelContext); + when(camelContext.getRegistry()).thenReturn(camelRegistry); + when(camelRegistry.lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME, MetricRegistry.class)).thenReturn(metricRegistry); + Map<String, Object> params = new HashMap<String, Object>(); + Long value = System.currentTimeMillis(); + 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(me.getMark(), is(value)); + assertThat(me.getMetricsName(), is("long.meter")); + assertThat(me.getRegistry(), is(metricRegistry)); + inOrder.verify(camelContext, times(1)).getRegistry(); + inOrder.verify(camelRegistry, times(1)).lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME, MetricRegistry.class); + inOrder.verify(camelContext, times(1)).getTypeConverter(); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testCreateEndpoints() throws Exception { + component.setCamelContext(camelContext); + when(camelContext.getRegistry()).thenReturn(camelRegistry); + when(camelRegistry.lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME, MetricRegistry.class)).thenReturn(metricRegistry); + Map<String, Object> params = new HashMap<String, Object>(); + Long value = System.currentTimeMillis(); + 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(me.getMark(), is(value)); + assertThat(me.getMetricsName(), is("long.meter")); + assertThat(me.getRegistry(), is(metricRegistry)); + + params = new HashMap<String, Object>(); + params.put("increment", value + 1); + params.put("decrement", value - 1); + + 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(ce.getIncrement(), is(value + 1)); + assertThat(ce.getDecrement(), is(value - 1)); + assertThat(ce.getMetricsName(), is("long.counter")); + assertThat(ce.getRegistry(), is(metricRegistry)); + + inOrder.verify(camelContext, times(1)).getRegistry(); + inOrder.verify(camelRegistry, times(1)).lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME, MetricRegistry.class); + inOrder.verify(camelContext, times(2)).getTypeConverter(); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testGetMetricsName() throws Exception { + assertThat(component.getMetricsName("meter:metric-a"), is("metric-a")); + assertThat(component.getMetricsName("meter:metric-a:sub-b"), is("metric-a:sub-b")); + assertThat(component.getMetricsName("metric-a"), is("metric-a")); + assertThat(component.getMetricsName("//metric-a"), is("//metric-a")); + assertThat(component.getMetricsName("meter://metric-a"), is("//metric-a")); + } + + @Test + public void testCreateNewEndpointForCounter() throws Exception { + Endpoint endpoint = component.createNewEndpoint(metricRegistry, MetricsType.COUNTER, "a name"); + assertThat(endpoint, is(notNullValue())); + assertThat(endpoint, is(instanceOf(CounterEndpoint.class))); + } + + @Test + public void testCreateNewEndpointForMeter() throws Exception { + Endpoint endpoint = component.createNewEndpoint(metricRegistry, MetricsType.METER, "a name"); + assertThat(endpoint, is(notNullValue())); + assertThat(endpoint, is(instanceOf(MeterEndpoint.class))); + } + + @Test(expected = RuntimeCamelException.class) + public void testCreateNewEndpointForGauge() throws Exception { + component.createNewEndpoint(metricRegistry, MetricsType.GAUGE, "a name"); + } + + @Test + public void testCreateNewEndpointForHistogram() throws Exception { + Endpoint endpoint = component.createNewEndpoint(metricRegistry, MetricsType.HISTOGRAM, "a name"); + assertThat(endpoint, is(notNullValue())); + assertThat(endpoint, is(instanceOf(HistogramEndpoint.class))); + } + + @Test + public void testCreateNewEndpointForTimer() throws Exception { + Endpoint endpoint = component.createNewEndpoint(metricRegistry, MetricsType.TIMER, "a name"); + assertThat(endpoint, is(notNullValue())); + assertThat(endpoint, is(instanceOf(TimerEndpoint.class))); + } + + @Test + public void testGetMetricsType() throws Exception { + for (MetricsType type : EnumSet.allOf(MetricsType.class)) { + assertThat(component.getMetricsType(type.toString() + ":metrics-name"), is(type)); + } + } + + @Test + public void testGetMetricsTypeNotSet() throws Exception { + assertThat(component.getMetricsType("no-metrics-type"), is(MetricsComponent.DEFAULT_METRICS_TYPE)); + } + + @Test(expected = RuntimeCamelException.class) + public void testGetMetricsTypeNotFound() throws Exception { + component.getMetricsType("unknown-metrics:metrics-name"); + } + + @Test + public void testGetOrCreateMetricRegistryFoundInCamelRegistry() throws Exception { + when(camelRegistry.lookupByNameAndType("name", MetricRegistry.class)).thenReturn(metricRegistry); + MetricRegistry result = component.getOrCreateMetricRegistry(camelRegistry, "name"); + assertThat(result, is(metricRegistry)); + inOrder.verify(camelRegistry, times(1)).lookupByNameAndType("name", MetricRegistry.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testGetOrCreateMetricRegistryNotFoundInCamelRegistry() throws Exception { + when(camelRegistry.lookupByNameAndType("name", MetricRegistry.class)).thenReturn(null); + MetricRegistry result = component.getOrCreateMetricRegistry(camelRegistry, "name"); + assertThat(result, is(notNullValue())); + assertThat(result, is(not(metricRegistry))); + inOrder.verify(camelRegistry, times(1)).lookupByNameAndType("name", MetricRegistry.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testGetMetricRegistryFromCamelRegistry() throws Exception { + when(camelRegistry.lookupByNameAndType("name", MetricRegistry.class)).thenReturn(metricRegistry); + MetricRegistry result = component.getMetricRegistryFromCamelRegistry(camelRegistry, "name"); + assertThat(result, is(metricRegistry)); + inOrder.verify(camelRegistry, times(1)).lookupByNameAndType("name", MetricRegistry.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testCreateMetricRegistry() throws Exception { + MetricRegistry registry = component.createMetricRegistry(); + assertThat(registry, is(notNullValue())); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/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 new file mode 100644 index 0000000..145a1db --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/MetricsTypeTest.java @@ -0,0 +1,34 @@ +/** + * 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 java.util.EnumSet; + +import org.junit.Test; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class MetricsTypeTest { + + @Test + public void testGetByName() throws Exception { + for (MetricsType type : EnumSet.allOf(MetricsType.class)) { + MetricsType t = MetricsType.getByName(type.toString()); + assertThat(t, is(type)); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/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 new file mode 100644 index 0000000..c5da95e --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterEndpointTest.java @@ -0,0 +1,102 @@ +/** + * 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/acf0fa39/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 new file mode 100644 index 0000000..005ae65 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterProducerTest.java @@ -0,0 +1,195 @@ +/** + * 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.MetricsComponent.HEADER_COUNTER_DECREMENT; +import static org.apache.camel.component.metrics.MetricsComponent.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/acf0fa39/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 new file mode 100644 index 0000000..c33ec39 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/counter/CounterRouteTest.java @@ -0,0 +1,209 @@ +/** + * 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.HEADER_COUNTER_DECREMENT; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_COUNTER_INCREMENT; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_METRIC_NAME; +import static org.apache.camel.component.metrics.MetricsComponent.METRIC_REGISTRY_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/acf0fa39/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 new file mode 100644 index 0000000..86049d5 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramEndpointTest.java @@ -0,0 +1,88 @@ +/** + * 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)); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramProducerTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramProducerTest.java new file mode 100644 index 0000000..6665796 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramProducerTest.java @@ -0,0 +1,125 @@ +/** + * 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.Histogram; +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.MetricsComponent.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 HistogramEndpoint 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/acf0fa39/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramRouteTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramRouteTest.java new file mode 100644 index 0000000..ee5dad8 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/histogram/HistogramRouteTest.java @@ -0,0 +1,123 @@ +/** + * 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.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.HEADER_HISTOGRAM_VALUE; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_METRIC_NAME; +import static org.apache.camel.component.metrics.MetricsComponent.METRIC_REGISTRY_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/acf0fa39/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/meter/MeterEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/meter/MeterEndpointTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/meter/MeterEndpointTest.java new file mode 100644 index 0000000..e2fb8c1 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/meter/MeterEndpointTest.java @@ -0,0 +1,89 @@ +/** + * 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.meter; + +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 MeterEndpoint endpoint; + + private InOrder inOrder; + + @Before + public void setUp() throws Exception { + endpoint = new MeterEndpoint(registry, 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)); + } + + @Test + public void testCreateEndpointUri() throws Exception { + assertThat(endpoint.createEndpointUri(), is(MeterEndpoint.ENDPOINT_URI)); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/meter/MeterProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/meter/MeterProducerTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/meter/MeterProducerTest.java new file mode 100644 index 0000000..1392768 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/meter/MeterProducerTest.java @@ -0,0 +1,126 @@ +/** + * 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.meter; + +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.MetricsComponent.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 MeterEndpoint 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(); + } +}