Skip to content

Commit

Permalink
CAMEL-17938 Add tests in camel-aws2-cw-starter
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriOndrusek committed Apr 12, 2022
1 parent 3498fca commit 2086d85
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
7 changes: 7 additions & 0 deletions components-starter/camel-aws2-cw-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
</exclusions>
<!--END OF GENERATED CODE-->
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-infra-aws-v2</artifactId>
<version>${camel-version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<!--START OF GENERATED CODE-->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.aws2.cw;

import org.apache.camel.CamelContext;
import org.apache.camel.Configuration;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.infra.aws2.clients.AWSSDKClientUtils;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.apache.camel.test.infra.aws.common.services.AWSService;
import org.apache.camel.test.infra.aws2.services.AWSServiceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;

public class BaseCw {

@Autowired
protected CamelContext context;

@Autowired
protected ProducerTemplate template;

@RegisterExtension
public static AWSService service = AWSServiceFactory.createCloudWatchService();

protected void assertMockEndpointsSatisfied() throws InterruptedException {
MockEndpoint.assertIsSatisfied(this.context);
}

// *************************************
// Config
// *************************************

@Configuration
public static class TestConfiguration {

@Bean
public CloudWatchClient cloudWatchClient() {
return AWSSDKClientUtils.newCloudWatchClient();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.aws2.cw;

import org.apache.camel.Configuration;
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.mock.MockEndpoint;
import org.apache.camel.spring.boot.CamelAutoConfiguration;
import org.apache.camel.test.infra.aws2.clients.AWSSDKClientUtils;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.test.annotation.DirtiesContext;
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
import software.amazon.awssdk.services.cloudwatch.model.GetMetricDataRequest;
import software.amazon.awssdk.services.cloudwatch.model.ListMetricsRequest;
import software.amazon.awssdk.services.cloudwatch.model.ListMetricsResponse;

import java.time.Instant;

//Based on CwComponentIT
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
@CamelSpringBootTest
@SpringBootTest(
classes = {
CamelAutoConfiguration.class,
CwComponentTest.class,
CwComponentTest.TestConfiguration.class
}
)
public class CwComponentTest extends BaseCw {

@EndpointInject("mock:result")
private MockEndpoint mock;

@Test
public void sendInOnly() throws Exception {
mock.expectedMessageCount(1);

Exchange e = template.send("direct:start", ExchangePattern.InOnly, new Processor() {
public void process(Exchange exchange) {
exchange.getIn().setHeader(Cw2Constants.METRIC_NAME, "ExchangesCompleted");
exchange.getIn().setHeader(Cw2Constants.METRIC_DIMENSION_NAME, "dimension");
exchange.getIn().setHeader(Cw2Constants.METRIC_VALUE, "2.0");
exchange.getIn().setHeader(Cw2Constants.METRIC_UNIT, "Count");
}
});

assertMockEndpointsSatisfied();

CloudWatchClient client = AWSSDKClientUtils.newCloudWatchClient();
ListMetricsResponse resp = client.listMetrics( ListMetricsRequest.builder().namespace("http://camel.apache.org/aws-cw").build());

Assertions.assertEquals(1, resp.metrics().size());
Assertions.assertEquals("ExchangesCompleted", resp.metrics().get(0).metricName());
}

// *************************************
// Config
// *************************************

@Configuration
public class TestConfiguration extends BaseCw.TestConfiguration {
@Bean
public RouteBuilder routeBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
from("direct:start").to("aws2-cw://http://camel.apache.org/aws-cw")
.to("mock:result");
}
};
}
}
}

0 comments on commit 2086d85

Please sign in to comment.