Author: bibryam Date: Thu Nov 15 11:47:47 2012 New Revision: 1409743 URL: http://svn.apache.org/viewvc?rev=1409743&view=rev Log: Added Amazon CloudWatch component with a producer
Added: camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/ camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwComponent.java camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwConfiguration.java camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwConstants.java camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwEndpoint.java camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwProducer.java camel/trunk/components/camel-aws/src/main/resources/META-INF/services/org/apache/camel/component/aws-cw camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/ camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/CwComponentConfigurationTest.java camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/CwComponentTest.java camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/integration/ camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/integration/CwComponentIntegrationTest.java Modified: camel/trunk/components/camel-aws/pom.xml Modified: camel/trunk/components/camel-aws/pom.xml URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/pom.xml?rev=1409743&r1=1409742&r2=1409743&view=diff ============================================================================== --- camel/trunk/components/camel-aws/pom.xml (original) +++ camel/trunk/components/camel-aws/pom.xml Thu Nov 15 11:47:47 2012 @@ -122,5 +122,10 @@ <artifactId>slf4j-log4j12</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> Added: camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwComponent.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwComponent.java?rev=1409743&view=auto ============================================================================== --- camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwComponent.java (added) +++ camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwComponent.java Thu Nov 15 11:47:47 2012 @@ -0,0 +1,52 @@ +/** + * 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.aws.cw; + +import java.util.Map; +import org.apache.camel.CamelContext; +import org.apache.camel.Endpoint; +import org.apache.camel.impl.DefaultComponent; + +/** + * Defines the <a href="http://aws.amazon.com/cloudwatch/">AWS CloudWatch Component</a> + */ +public class CwComponent extends DefaultComponent { + + public CwComponent() { + } + + public CwComponent(CamelContext context) { + super(context); + } + + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { + CwConfiguration configuration = new CwConfiguration(); + setProperties(configuration, parameters); + + if (remaining == null || remaining.trim().length() == 0) { + throw new IllegalArgumentException("Metric namespace must be specified."); + } + configuration.setNamespace(remaining); + + if (configuration.getAmazonCwClient() == null && (configuration.getAccessKey() == null || configuration.getSecretKey() == null)) { + throw new IllegalArgumentException("AmazonCwClient or accessKey and secretKey must be specified"); + } + + CwEndpoint endpoint = new CwEndpoint(uri, this, configuration); + return endpoint; + } +} \ No newline at end of file Added: camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwConfiguration.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwConfiguration.java?rev=1409743&view=auto ============================================================================== --- camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwConfiguration.java (added) +++ camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwConfiguration.java Thu Nov 15 11:47:47 2012 @@ -0,0 +1,121 @@ +/** + * 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.aws.cw; + +/** + * The AWS CW component configuration properties + * + */ +import java.util.Date; + +import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient; + +public class CwConfiguration implements Cloneable { + + private AmazonCloudWatchClient amazonCwClient; + private String amazonCwEndpoint; + private String accessKey; + private String secretKey; + private String name; + private Double value; + private String unit; + private String namespace; + private Date timestamp; + + public void setAmazonCwEndpoint(String amazonCwEndpoint) { + this.amazonCwEndpoint = amazonCwEndpoint; + } + + public String getAmazonCwEndpoint() { + return amazonCwEndpoint; + } + + public String getAccessKey() { + return accessKey; + } + + public void setAccessKey(String accessKey) { + this.accessKey = accessKey; + } + + public String getSecretKey() { + return secretKey; + } + + public void setSecretKey(String secretKey) { + this.secretKey = secretKey; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Double getValue() { + return value; + } + + public void setValue(Double value) { + this.value = value; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + public void setTimestamp(Date timestamp) { + this.timestamp = timestamp; + } + + public Date getTimestamp() { + return timestamp; + } + + @Override + public String toString() { + return "CwConfiguration[name=" + name + + ", amazonCwClient=" + amazonCwClient + + ", accessKey=" + accessKey + + ", secretKey=xxxxxxxxxxxxxxx" + + ", value=" + value + + ", unit=" + unit + + "]"; + } + + public AmazonCloudWatchClient getAmazonCwClient() { + return amazonCwClient; + } + + public void setAmazonCwClient(AmazonCloudWatchClient amazonCwClient) { + this.amazonCwClient = amazonCwClient; + } +} \ No newline at end of file Added: camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwConstants.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwConstants.java?rev=1409743&view=auto ============================================================================== --- camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwConstants.java (added) +++ camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwConstants.java Thu Nov 15 11:47:47 2012 @@ -0,0 +1,28 @@ +/** + * 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.aws.cw; + +/** + * Constants used in Camel AWS CW module + */ +public interface CwConstants { + String METRIC_NAMESPACE = "CamelAwsCwMetricNamespace"; + String METRIC_NAME = "CamelAwsCwMetricName"; + String METRIC_VALUE = "CamelAwsCwMetricValue"; + String METRIC_UNIT = "CamelAwsCwMetricUnit"; + String METRIC_TIMESTAMP = "CamelAwsCwMetricTimestamp"; +} \ No newline at end of file Added: camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwEndpoint.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwEndpoint.java?rev=1409743&view=auto ============================================================================== --- camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwEndpoint.java (added) +++ camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwEndpoint.java Thu Nov 15 11:47:47 2012 @@ -0,0 +1,93 @@ +/** + * 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.aws.cw; + +import com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient; +import org.apache.camel.CamelContext; +import org.apache.camel.Component; +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.impl.DefaultEndpoint; + +/** + * Defines the <a href="http://aws.amazon.com/cloudwatch/">AWS CloudWatch Endpoint</a> + */ +public class CwEndpoint extends DefaultEndpoint { + + private CwConfiguration configuration; + private AmazonCloudWatchClient cloudWatchClient; + + @Deprecated + public CwEndpoint(String uri, CamelContext context, CwConfiguration configuration) { + super(uri, context); + this.configuration = configuration; + } + + public CwEndpoint(String uri, Component component, CwConfiguration configuration) { + super(uri, component); + this.configuration = configuration; + } + + public Consumer createConsumer(Processor processor) throws Exception { + throw new UnsupportedOperationException("You cannot receive messages from this endpoint"); + } + + public Producer createProducer() throws Exception { + return new CwProducer(this); + } + + public boolean isSingleton() { + return true; + } + + @Override + public void doStart() throws Exception { + super.doStart(); + } + + public CwConfiguration getConfiguration() { + return configuration; + } + + public void setConfiguration(CwConfiguration configuration) { + this.configuration = configuration; + } + + public void setCloudWatchClient(AmazonCloudWatchClient cloudWatchClient) { + this.cloudWatchClient = cloudWatchClient; + } + + public AmazonCloudWatchClient getCloudWatchClient() { + if (cloudWatchClient == null) { + cloudWatchClient = configuration.getAmazonCwClient() != null + ? configuration.getAmazonCwClient() : createCloudWatchClient(); + } + return cloudWatchClient; + } + + AmazonCloudWatchClient createCloudWatchClient() { + AWSCredentials credentials = new BasicAWSCredentials(configuration.getAccessKey(), configuration.getSecretKey()); + AmazonCloudWatchClient client = new AmazonCloudWatchClient(credentials); + if (configuration.getAmazonCwEndpoint() != null) { + client.setEndpoint(configuration.getAmazonCwEndpoint()); + } + return client; + } +} \ No newline at end of file Added: camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwProducer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwProducer.java?rev=1409743&view=auto ============================================================================== --- camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwProducer.java (added) +++ camel/trunk/components/camel-aws/src/main/java/org/apache/camel/component/aws/cw/CwProducer.java Thu Nov 15 11:47:47 2012 @@ -0,0 +1,122 @@ +/** + * 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.aws.cw; + + +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import com.amazonaws.services.cloudwatch.model.MetricDatum; +import com.amazonaws.services.cloudwatch.model.PutMetricDataRequest; +import com.amazonaws.services.cloudwatch.model.StandardUnit; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.util.URISupport; + +/** + * A Producer which sends messages to the AWS CloudWatch Service + */ +public class CwProducer extends DefaultProducer { + + public CwProducer(Endpoint endpoint) { + super(endpoint); + } + + public void process(Exchange exchange) throws Exception { + List<MetricDatum> metricData = getMetricData(exchange); + + PutMetricDataRequest request = new PutMetricDataRequest() + .withMetricData(metricData) + .withNamespace(determineNameSpace(exchange)); + + log.info("Sending request [{}] from exchange [{}]...", request, exchange); + getEndpoint().getCloudWatchClient().putMetricData(request); + } + + private List<MetricDatum> getMetricData(Exchange exchange) { + Object body = exchange.getIn().getBody(); + if (body instanceof List) { + return (List<MetricDatum>) body; + } + + if (body instanceof MetricDatum) { + return Arrays.asList((MetricDatum) body); + } + + return Arrays.asList(new MetricDatum() + .withMetricName(determineName(exchange)) + .withValue(determineValue(exchange)) + .withUnit(determineUnit(exchange)) + .withTimestamp(determineTimestamp(exchange))); + } + + private Date determineTimestamp(Exchange exchange) { + Date timestamp = exchange.getIn().getHeader(CwConstants.METRIC_TIMESTAMP, Date.class); + if (timestamp == null) { + timestamp = getConfiguration().getTimestamp(); + } + return timestamp; + } + + private String determineNameSpace(Exchange exchange) { + String namespace = exchange.getIn().getHeader(CwConstants.METRIC_NAMESPACE, String.class); + if (namespace == null) { + namespace = getConfiguration().getNamespace(); + } + return namespace; + } + + private String determineName(Exchange exchange) { + String name = exchange.getIn().getHeader(CwConstants.METRIC_NAME, String.class); + if (name == null) { + name = getConfiguration().getName(); + } + return name; + } + + private Double determineValue(Exchange exchange) { + Double value = exchange.getIn().getHeader(CwConstants.METRIC_VALUE, Double.class); + if (value == null) { + value = getConfiguration().getValue(); + } + return value != null ? value : Double.valueOf(1); + } + + private StandardUnit determineUnit(Exchange exchange) { + String unit = exchange.getIn().getHeader(CwConstants.METRIC_UNIT, String.class); + if (unit == null) { + unit = getConfiguration().getUnit(); + } + return unit != null ? StandardUnit.valueOf(unit) : StandardUnit.Count; + } + + protected CwConfiguration getConfiguration() { + return getEndpoint().getConfiguration(); + } + + @Override + public String toString() { + return "CwProducer[" + URISupport.sanitizeUri(getEndpoint().getEndpointUri()) + "]"; + } + + @Override + public CwEndpoint getEndpoint() { + return (CwEndpoint) super.getEndpoint(); + } +} \ No newline at end of file Added: camel/trunk/components/camel-aws/src/main/resources/META-INF/services/org/apache/camel/component/aws-cw URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/main/resources/META-INF/services/org/apache/camel/component/aws-cw?rev=1409743&view=auto ============================================================================== --- camel/trunk/components/camel-aws/src/main/resources/META-INF/services/org/apache/camel/component/aws-cw (added) +++ camel/trunk/components/camel-aws/src/main/resources/META-INF/services/org/apache/camel/component/aws-cw Thu Nov 15 11:47:47 2012 @@ -0,0 +1,18 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +class=org.apache.camel.component.aws.cw.CwComponent Added: camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/CwComponentConfigurationTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/CwComponentConfigurationTest.java?rev=1409743&view=auto ============================================================================== --- camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/CwComponentConfigurationTest.java (added) +++ camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/CwComponentConfigurationTest.java Thu Nov 15 11:47:47 2012 @@ -0,0 +1,65 @@ +/** + * 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.aws.cw; + + +import java.util.Date; + +import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient; +import org.apache.camel.component.aws.sns.SnsComponent; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +import static org.mockito.Mockito.*; + +public class CwComponentConfigurationTest extends CamelTestSupport { + private static final Date NOW = new Date(); + AmazonCloudWatchClient amazonCwClient = mock(AmazonCloudWatchClient.class); + + @Test + public void createEndpointWithAllOptions() throws Exception { + CwComponent component = new CwComponent(context); + CwEndpoint endpoint = (CwEndpoint) component.createEndpoint("aws-cw://camel.apache.org/test?amazonCwClient=#amazonCwClient&name=testMetric&value=2&unit=Count×tamp=#now"); + + assertEquals("camel.apache.org/test", endpoint.getConfiguration().getNamespace()); + assertEquals("testMetric", endpoint.getConfiguration().getName()); + assertEquals(Double.valueOf(2), endpoint.getConfiguration().getValue()); + assertEquals("Count", endpoint.getConfiguration().getUnit()); + assertEquals(NOW, endpoint.getConfiguration().getTimestamp()); + } + + @Test(expected = IllegalArgumentException.class) + public void createEndpointWithoutAccessKeyConfiguration() throws Exception { + SnsComponent component = new SnsComponent(context); + component.createEndpoint("aws-cw://camel.apache.org/test?secretKey=yyy"); + } + + @Test(expected = IllegalArgumentException.class) + public void createEndpointWithoutSecretKeyConfiguration() throws Exception { + CwComponent component = new CwComponent(context); + component.createEndpoint("aws-cw://camel.apache.org/test?accessKey=xxx"); + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry registry = super.createRegistry(); + registry.bind("amazonCwClient", amazonCwClient); + registry.bind("now", NOW); + return registry; + } +} \ No newline at end of file Added: camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/CwComponentTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/CwComponentTest.java?rev=1409743&view=auto ============================================================================== --- camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/CwComponentTest.java (added) +++ camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/CwComponentTest.java Thu Nov 15 11:47:47 2012 @@ -0,0 +1,120 @@ +/** + * 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.aws.cw; + +import java.util.Date; + +import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient; +import com.amazonaws.services.cloudwatch.model.MetricDatum; +import com.amazonaws.services.cloudwatch.model.PutMetricDataRequest; +import com.amazonaws.services.cloudwatch.model.StandardUnit; +import org.apache.camel.Exchange; +import org.apache.camel.ExchangePattern; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +import org.mockito.ArgumentCaptor; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class CwComponentTest extends CamelTestSupport { + private static final Date NOW = new Date(); + private static final Date LATER = new Date(NOW.getTime() + 1); + private AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class); + + @Test + public void sendMetricFromHeaderValues() throws Exception { + Exchange exchange = template.send("direct:start", ExchangePattern.InOnly, new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(CwConstants.METRIC_NAMESPACE, "camel.apache.org/overriden"); + exchange.getIn().setHeader(CwConstants.METRIC_NAME, "OverridenMetric"); + exchange.getIn().setHeader(CwConstants.METRIC_VALUE, Double.valueOf(3)); + exchange.getIn().setHeader(CwConstants.METRIC_UNIT, StandardUnit.Bytes.toString()); + exchange.getIn().setHeader(CwConstants.METRIC_TIMESTAMP, LATER); + } + }); + + + ArgumentCaptor<PutMetricDataRequest> argument = ArgumentCaptor.forClass(PutMetricDataRequest.class); + verify(cloudWatchClient).putMetricData(argument.capture()); + + assertEquals("camel.apache.org/overriden", argument.getValue().getNamespace()); + assertEquals("OverridenMetric", argument.getValue().getMetricData().get(0).getMetricName()); + assertEquals(Double.valueOf(3), argument.getValue().getMetricData().get(0).getValue()); + assertEquals(StandardUnit.Bytes.toString(), argument.getValue().getMetricData().get(0).getUnit()); + assertEquals(LATER, argument.getValue().getMetricData().get(0).getTimestamp()); + } + + @Test + public void sendManuallyCreatedMetric() throws Exception { + Exchange exchange = template.send("direct:start", ExchangePattern.InOnly, new Processor() { + public void process(Exchange exchange) throws Exception { + MetricDatum metricDatum = new MetricDatum() + .withMetricName("errorCount") + .withValue(Double.valueOf(0)); + exchange.getIn().setBody(metricDatum); + } + }); + + + ArgumentCaptor<PutMetricDataRequest> argument = ArgumentCaptor.forClass(PutMetricDataRequest.class); + verify(cloudWatchClient).putMetricData(argument.capture()); + + assertEquals("errorCount", argument.getValue().getMetricData().get(0).getMetricName()); + assertEquals(Double.valueOf(0), argument.getValue().getMetricData().get(0).getValue()); + } + + + @Test + public void useDefaultValuesForMetricUnitAndMetricValue() throws Exception { + Exchange exchange = template.send("direct:start", ExchangePattern.InOnly, new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(CwConstants.METRIC_NAME, "errorCount"); + } + }); + + + ArgumentCaptor<PutMetricDataRequest> argument = ArgumentCaptor.forClass(PutMetricDataRequest.class); + verify(cloudWatchClient).putMetricData(argument.capture()); + + assertEquals("errorCount", argument.getValue().getMetricData().get(0).getMetricName()); + assertEquals(Double.valueOf(1), argument.getValue().getMetricData().get(0).getValue()); + assertEquals(StandardUnit.Count.toString(), argument.getValue().getMetricData().get(0).getUnit()); + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry registry = super.createRegistry(); + registry.bind("amazonCwClient", cloudWatchClient); + registry.bind("now", NOW); + return registry; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .to("aws-cw://camel.apache.org/test?amazonCwClient=#amazonCwClient&name=testMetric&unit=Count×tamp=#now"); + } + }; + } +} Added: camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/integration/CwComponentIntegrationTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/integration/CwComponentIntegrationTest.java?rev=1409743&view=auto ============================================================================== --- camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/integration/CwComponentIntegrationTest.java (added) +++ camel/trunk/components/camel-aws/src/test/java/org/apache/camel/component/aws/cw/integration/CwComponentIntegrationTest.java Thu Nov 15 11:47:47 2012 @@ -0,0 +1,64 @@ +/** + * 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.aws.cw.integration; + +import java.util.Date; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Exchange; +import org.apache.camel.ExchangePattern; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.aws.cw.CwConstants; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Ignore; +import org.junit.Test; + + +//@Ignore("Must be manually tested. Provide your own accessKey and secretKey!") +public class CwComponentIntegrationTest extends CamelTestSupport { + + @EndpointInject(uri = "mock:result") + private MockEndpoint mock; + + @Test + public void sendInOnly() throws Exception { + mock.expectedMessageCount(1); + + Exchange exchange = template.send("direct:start", ExchangePattern.InOnly, new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(CwConstants.METRIC_NAME, "ExchangesCompleted"); + exchange.getIn().setHeader(CwConstants.METRIC_VALUE, "2.0"); + exchange.getIn().setHeader(CwConstants.METRIC_UNIT, "Count"); + } + }); + + assertMockEndpointsSatisfied(); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .to("aws-cw://http://camel.apache.org/aws-cw?accessKey=XXX&secretKey=XXX") + .to("mock:result"); + } + }; + } +} \ No newline at end of file