http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/meter/MeterRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/meter/MeterRouteTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/meter/MeterRouteTest.java new file mode 100644 index 0000000..86076e7 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/meter/MeterRouteTest.java @@ -0,0 +1,165 @@ +/** + * 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.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_METER_MARK; +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 = { 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/acf0fa39/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/timer/TimerEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/timer/TimerEndpointTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/timer/TimerEndpointTest.java new file mode 100644 index 0000000..5bc1138 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/timer/TimerEndpointTest.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.timer; + +import com.codahale.metrics.MetricRegistry; +import org.apache.camel.Producer; +import org.apache.camel.component.metrics.timer.TimerEndpoint.TimerAction; +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 TimerEndpoint endpoint; + + private InOrder inOrder; + + @Before + public void setUp() throws Exception { + endpoint = new TimerEndpoint(registry, 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(TimerAction.start); + assertThat(endpoint.getAction(), is(TimerAction.start)); + } + + @Test + public void testCreateEndpointUri() throws Exception { + assertThat(endpoint.createEndpointUri(), is(TimerEndpoint.ENDPOINT_URI)); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/acf0fa39/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/timer/TimerProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/timer/TimerProducerTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/timer/TimerProducerTest.java new file mode 100644 index 0000000..df94fef --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/timer/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.timer; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.component.metrics.timer.TimerEndpoint.TimerAction; +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_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 = TimerEndpoint.ENDPOINT_URI + ":" + METRICS_NAME; + + @Mock + private TimerEndpoint 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(TimerAction.start); + when(in.getHeader(HEADER_TIMER_ACTION, TimerAction.start, TimerAction.class)).thenReturn(TimerAction.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, TimerAction.start, TimerAction.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(TimerAction.start); + when(in.getHeader(HEADER_TIMER_ACTION, TimerAction.start, TimerAction.class)).thenReturn(TimerAction.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, TimerAction.start, TimerAction.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(TimerAction.stop); + when(in.getHeader(HEADER_TIMER_ACTION, TimerAction.stop, TimerAction.class)).thenReturn(TimerAction.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, TimerAction.stop, TimerAction.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(TimerAction.stop); + when(in.getHeader(HEADER_TIMER_ACTION, TimerAction.stop, TimerAction.class)).thenReturn(TimerAction.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, TimerAction.stop, TimerAction.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, TimerAction.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, TimerAction.class); + inOrder.verifyNoMoreInteractions(); + } + + @Test + public void testProcessNoActionOverride() throws Exception { + when(endpoint.getAction()).thenReturn(null); + when(in.getHeader(HEADER_TIMER_ACTION, null, TimerAction.class)).thenReturn(TimerAction.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, TimerAction.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(TimerEndpoint.ENDPOINT_URI + ":" + 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/acf0fa39/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/timer/TimerRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/timer/TimerRouteTest.java b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/timer/TimerRouteTest.java new file mode 100644 index 0000000..b6941b5 --- /dev/null +++ b/components/camel-metrics/src/test/java/org/apache/camel/component/metrics/timer/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.timer; + +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.component.metrics.timer.TimerEndpoint.TimerAction; +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_METRIC_NAME; +import static org.apache.camel.component.metrics.MetricsComponent.HEADER_TIMER_ACTION; +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 = { 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, TimerAction.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, TimerAction.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/acf0fa39/components/camel-metrics/src/test/java/org/apache/camel/metrics/AbstractMetricsEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/metrics/AbstractMetricsEndpointTest.java b/components/camel-metrics/src/test/java/org/apache/camel/metrics/AbstractMetricsEndpointTest.java deleted file mode 100644 index a4e1218..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/metrics/AbstractMetricsEndpointTest.java +++ /dev/null @@ -1,105 +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.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/metrics/AbstractMetricsProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/metrics/AbstractMetricsProducerTest.java b/components/camel-metrics/src/test/java/org/apache/camel/metrics/AbstractMetricsProducerTest.java deleted file mode 100644 index b740634..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/metrics/AbstractMetricsProducerTest.java +++ /dev/null @@ -1,186 +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.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.metrics.AbstractMetricsProducer.HEADER_PATTERN; -import static org.apache.camel.metrics.MetricsComponent.HEADER_HISTOGRAM_VALUE; -import static org.apache.camel.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/metrics/MetricComponentSpringTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/metrics/MetricComponentSpringTest.java b/components/camel-metrics/src/test/java/org/apache/camel/metrics/MetricComponentSpringTest.java deleted file mode 100644 index 417cab6..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/metrics/MetricComponentSpringTest.java +++ /dev/null @@ -1,91 +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.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/metrics/MetricsComponentRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/metrics/MetricsComponentRouteTest.java b/components/camel-metrics/src/test/java/org/apache/camel/metrics/MetricsComponentRouteTest.java deleted file mode 100644 index 09270c1..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/metrics/MetricsComponentRouteTest.java +++ /dev/null @@ -1,113 +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.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.metrics.MetricsComponent.HEADER_HISTOGRAM_VALUE; -import static org.apache.camel.metrics.MetricsComponent.HEADER_METRIC_NAME; -import static org.apache.camel.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/metrics/MetricsComponentTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/metrics/MetricsComponentTest.java b/components/camel-metrics/src/test/java/org/apache/camel/metrics/MetricsComponentTest.java deleted file mode 100644 index 8fd02ce..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/metrics/MetricsComponentTest.java +++ /dev/null @@ -1,217 +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.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.metrics.counter.CounterEndpoint; -import org.apache.camel.metrics.histogram.HistogramEndpoint; -import org.apache.camel.metrics.meter.MeterEndpoint; -import org.apache.camel.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/metrics/MetricsTypeTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/metrics/MetricsTypeTest.java b/components/camel-metrics/src/test/java/org/apache/camel/metrics/MetricsTypeTest.java deleted file mode 100644 index 9a3129a..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/metrics/MetricsTypeTest.java +++ /dev/null @@ -1,33 +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.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/metrics/counter/CounterEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/metrics/counter/CounterEndpointTest.java b/components/camel-metrics/src/test/java/org/apache/camel/metrics/counter/CounterEndpointTest.java deleted file mode 100644 index 0d5c015..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/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.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/metrics/counter/CounterProducerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/metrics/counter/CounterProducerTest.java b/components/camel-metrics/src/test/java/org/apache/camel/metrics/counter/CounterProducerTest.java deleted file mode 100644 index e33429d..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/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.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.metrics.MetricsComponent.HEADER_COUNTER_DECREMENT; -import static org.apache.camel.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/metrics/counter/CounterRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-metrics/src/test/java/org/apache/camel/metrics/counter/CounterRouteTest.java b/components/camel-metrics/src/test/java/org/apache/camel/metrics/counter/CounterRouteTest.java deleted file mode 100644 index 246f11e..0000000 --- a/components/camel-metrics/src/test/java/org/apache/camel/metrics/counter/CounterRouteTest.java +++ /dev/null @@ -1,209 +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.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.metrics.MetricsComponent.HEADER_COUNTER_DECREMENT; -import static org.apache.camel.metrics.MetricsComponent.HEADER_COUNTER_INCREMENT; -import static org.apache.camel.metrics.MetricsComponent.HEADER_METRIC_NAME; -import static org.apache.camel.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(); - } -}