diff --git a/components/camel-aws/camel-aws2-ec2/pom.xml b/components/camel-aws/camel-aws2-ec2/pom.xml
index 0994f2a0cac41..3ca83d4adf210 100644
--- a/components/camel-aws/camel-aws2-ec2/pom.xml
+++ b/components/camel-aws/camel-aws2-ec2/pom.xml
@@ -77,5 +77,10 @@
${project.version}
test
+
+ org.assertj
+ assertj-core
+ test
+
diff --git a/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Producer.java b/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Producer.java
index f0c99eb95d351..f32cb241e7c43 100644
--- a/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Producer.java
+++ b/components/camel-aws/camel-aws2-ec2/src/main/java/org/apache/camel/component/aws2/ec2/AWS2EC2Producer.java
@@ -160,6 +160,9 @@ private void createAndRunInstance(Ec2Client ec2Client, Exchange exchange) throws
LOG.trace("Creating and running instances requests performing");
Message message = getMessageForResponse(exchange);
message.setBody(result);
+ } else {
+ throw new IllegalArgumentException(
+ "createAndRunInstances operation requires RunInstancesRequest in POJO mode");
}
} else {
RunInstancesRequest.Builder builder = RunInstancesRequest.builder();
@@ -251,6 +254,9 @@ private void startInstances(Ec2Client ec2Client, Exchange exchange) throws Inval
LOG.trace("Starting instances with Ids [{}] ", startInstancesRequest.instanceIds());
Message message = getMessageForResponse(exchange);
message.setBody(result);
+ } else {
+ throw new IllegalArgumentException(
+ "startInstances operation requires StartInstancesRequest in POJO mode");
}
} else {
StartInstancesRequest.Builder builder = StartInstancesRequest.builder();
@@ -293,6 +299,9 @@ private void stopInstances(Ec2Client ec2Client, Exchange exchange) throws Invali
LOG.trace("Stopping instances with Ids [{}] ", stopInstancesRequest.instanceIds());
Message message = getMessageForResponse(exchange);
message.setBody(result);
+ } else {
+ throw new IllegalArgumentException(
+ "stopInstances operation requires StopInstancesRequest in POJO mode");
}
} else {
StopInstancesRequest.Builder builder = StopInstancesRequest.builder();
@@ -335,6 +344,9 @@ private void terminateInstances(Ec2Client ec2Client, Exchange exchange) throws I
LOG.trace("Terminating instances with Ids [{}] ", terminateInstancesRequest.instanceIds());
Message message = getMessageForResponse(exchange);
message.setBody(result);
+ } else {
+ throw new IllegalArgumentException(
+ "terminateInstances operation requires TerminateInstancesRequest in POJO mode");
}
} else {
TerminateInstancesRequest.Builder builder = TerminateInstancesRequest.builder();
@@ -434,6 +446,9 @@ private void rebootInstances(Ec2Client ec2Client, Exchange exchange) throws Inva
LOG.trace("Reboot Instances command returned the error code {}", ase.awsErrorDetails().errorCode());
throw ase;
}
+ } else {
+ throw new IllegalArgumentException(
+ "rebootInstances operation requires RebootInstancesRequest in POJO mode");
}
} else {
RebootInstancesRequest.Builder builder = RebootInstancesRequest.builder();
@@ -472,6 +487,9 @@ private void monitorInstances(Ec2Client ec2Client, Exchange exchange) throws Inv
LOG.trace("Start Monitoring instances with Ids [{}] ", monitorInstancesRequest.instanceIds());
Message message = getMessageForResponse(exchange);
message.setBody(result);
+ } else {
+ throw new IllegalArgumentException(
+ "monitorInstances operation requires MonitorInstancesRequest in POJO mode");
}
} else {
MonitorInstancesRequest.Builder builder = MonitorInstancesRequest.builder();
@@ -514,6 +532,9 @@ private void unmonitorInstances(Ec2Client ec2Client, Exchange exchange) throws I
LOG.trace("Stop Monitoring instances with Ids [{}] ", unmonitorInstancesRequest.instanceIds());
Message message = getMessageForResponse(exchange);
message.setBody(result);
+ } else {
+ throw new IllegalArgumentException(
+ "unmonitorInstances operation requires UnmonitorInstancesRequest in POJO mode");
}
} else {
UnmonitorInstancesRequest.Builder builder = UnmonitorInstancesRequest.builder();
@@ -556,6 +577,9 @@ private void createTags(Ec2Client ec2Client, Exchange exchange) throws InvalidPa
LOG.trace("Created tags [{}] ", createTagsRequest.tags());
Message message = getMessageForResponse(exchange);
message.setBody(result);
+ } else {
+ throw new IllegalArgumentException(
+ "createTags operation requires CreateTagsRequest in POJO mode");
}
} else {
Collection tags;
@@ -605,6 +629,9 @@ private void deleteTags(Ec2Client ec2Client, Exchange exchange) throws InvalidPa
LOG.trace("Delete tags [{}] ", deleteTagsRequest.tags());
Message message = getMessageForResponse(exchange);
message.setBody(result);
+ } else {
+ throw new IllegalArgumentException(
+ "deleteTags operation requires DeleteTagsRequest in POJO mode");
}
} else {
Collection tags;
diff --git a/components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/EC2ProducerTest.java b/components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/EC2ProducerTest.java
index 7a124e29f7f91..05b00d086fff6 100644
--- a/components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/EC2ProducerTest.java
+++ b/components/camel-aws/camel-aws2-ec2/src/test/java/org/apache/camel/component/aws2/ec2/EC2ProducerTest.java
@@ -27,6 +27,8 @@
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit6.CamelTestSupport;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
import software.amazon.awssdk.services.ec2.model.DescribeInstanceStatusResponse;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse;
import software.amazon.awssdk.services.ec2.model.InstanceStateName;
@@ -40,6 +42,7 @@
import software.amazon.awssdk.services.ec2.model.TerminateInstancesResponse;
import software.amazon.awssdk.services.ec2.model.UnmonitorInstancesResponse;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -256,6 +259,24 @@ public void process(Exchange exchange) {
assertEquals(MonitoringState.DISABLED, resultGet.instanceMonitorings().get(0).monitoring().state());
}
+ @ParameterizedTest
+ @CsvSource({
+ "direct:createAndRunPojo,createAndRunInstances operation requires RunInstancesRequest in POJO mode",
+ "direct:startPojo,startInstances operation requires StartInstancesRequest in POJO mode",
+ "direct:stopPojo,stopInstances operation requires StopInstancesRequest in POJO mode",
+ "direct:terminatePojo,terminateInstances operation requires TerminateInstancesRequest in POJO mode",
+ "direct:rebootPojo,rebootInstances operation requires RebootInstancesRequest in POJO mode",
+ "direct:monitorPojo,monitorInstances operation requires MonitorInstancesRequest in POJO mode",
+ "direct:unmonitorPojo,unmonitorInstances operation requires UnmonitorInstancesRequest in POJO mode",
+ "direct:createTagsPojo,createTags operation requires CreateTagsRequest in POJO mode",
+ "direct:deleteTagsPojo,deleteTags operation requires DeleteTagsRequest in POJO mode",
+ })
+ void pojoRequestWithWrongBodyTypeThrows(String route, String expectedMessage) {
+ assertThatThrownBy(() -> template.requestBody(route, "not the expected request type"))
+ .hasRootCauseInstanceOf(IllegalArgumentException.class)
+ .hasRootCauseMessage(expectedMessage);
+ }
+
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@@ -288,6 +309,22 @@ public void configure() {
.to("mock:result");
from("direct:deleteTags").to("aws2-ec2://test?amazonEc2Client=#amazonEc2Client&operation=deleteTags")
.to("mock:result");
+ from("direct:startPojo")
+ .to("aws2-ec2://test?amazonEc2Client=#amazonEc2Client&operation=startInstances&pojoRequest=true");
+ from("direct:stopPojo")
+ .to("aws2-ec2://test?amazonEc2Client=#amazonEc2Client&operation=stopInstances&pojoRequest=true");
+ from("direct:terminatePojo")
+ .to("aws2-ec2://test?amazonEc2Client=#amazonEc2Client&operation=terminateInstances&pojoRequest=true");
+ from("direct:rebootPojo")
+ .to("aws2-ec2://test?amazonEc2Client=#amazonEc2Client&operation=rebootInstances&pojoRequest=true");
+ from("direct:monitorPojo")
+ .to("aws2-ec2://test?amazonEc2Client=#amazonEc2Client&operation=monitorInstances&pojoRequest=true");
+ from("direct:unmonitorPojo")
+ .to("aws2-ec2://test?amazonEc2Client=#amazonEc2Client&operation=unmonitorInstances&pojoRequest=true");
+ from("direct:createTagsPojo")
+ .to("aws2-ec2://test?amazonEc2Client=#amazonEc2Client&operation=createTags&pojoRequest=true");
+ from("direct:deleteTagsPojo")
+ .to("aws2-ec2://test?amazonEc2Client=#amazonEc2Client&operation=deleteTags&pojoRequest=true");
}
};
}