This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new e67cadea0c27 CAMEL-27032: camel-alibaba-eventbridge - generate the
event id when a JSON text or Map body has none (#30205)
e67cadea0c27 is described below
commit e67cadea0c27d1e26ef99b8c4ea7bf32dab7c022
Author: Saravanakumar Selvaraj <[email protected]>
AuthorDate: Mon Nov 16 21:56:31 2026 +0000
CAMEL-27032: camel-alibaba-eventbridge - generate the event id when a JSON
text or Map body has none (#30205)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../docs/alibaba-eventbridge-component.adoc | 4 +
.../main/docs/alibaba-eventbridge-component.adoc | 4 +
.../eventbridge/AlibabaEventBridgeUtils.java | 3 +
.../eventbridge/MapCloudEventValidator.java | 6 +-
.../alibaba/eventbridge/PutEventsEventIdTest.java | 199 +++++++++++++++++++++
5 files changed, 213 insertions(+), 3 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-eventbridge-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-eventbridge-component.adoc
index 02aaa45d56e4..af536e9d0921 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-eventbridge-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-eventbridge-component.adoc
@@ -129,6 +129,10 @@ When the message body is a `Map` (or `List<Map>` for
publishing batch events), t
|=======================================================================
+For a JSON text body, the producer also generates the `id` as a random UUID.
The producer generates a new `id` each time
+it sends the event, so a redelivery sends the event with a different `id`. To
send the same `id` on a redelivery, use a
+`Map` body with the `id` key.
+
=== Event Validation and Caching
The component supports a 3-level hierarchical validation model:
diff --git
a/components/camel-alibaba/camel-alibaba-eventbridge/src/main/docs/alibaba-eventbridge-component.adoc
b/components/camel-alibaba/camel-alibaba-eventbridge/src/main/docs/alibaba-eventbridge-component.adoc
index 02aaa45d56e4..af536e9d0921 100644
---
a/components/camel-alibaba/camel-alibaba-eventbridge/src/main/docs/alibaba-eventbridge-component.adoc
+++
b/components/camel-alibaba/camel-alibaba-eventbridge/src/main/docs/alibaba-eventbridge-component.adoc
@@ -129,6 +129,10 @@ When the message body is a `Map` (or `List<Map>` for
publishing batch events), t
|=======================================================================
+For a JSON text body, the producer also generates the `id` as a random UUID.
The producer generates a new `id` each time
+it sends the event, so a redelivery sends the event with a different `id`. To
send the same `id` on a redelivery, use a
+`Map` body with the `id` key.
+
=== Event Validation and Caching
The component supports a 3-level hierarchical validation model:
diff --git
a/components/camel-alibaba/camel-alibaba-eventbridge/src/main/java/org/apache/camel/component/alibaba/eventbridge/AlibabaEventBridgeUtils.java
b/components/camel-alibaba/camel-alibaba-eventbridge/src/main/java/org/apache/camel/component/alibaba/eventbridge/AlibabaEventBridgeUtils.java
index c338288903d6..7978421866b6 100644
---
a/components/camel-alibaba/camel-alibaba-eventbridge/src/main/java/org/apache/camel/component/alibaba/eventbridge/AlibabaEventBridgeUtils.java
+++
b/components/camel-alibaba/camel-alibaba-eventbridge/src/main/java/org/apache/camel/component/alibaba/eventbridge/AlibabaEventBridgeUtils.java
@@ -26,6 +26,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -145,7 +146,9 @@ public final class AlibabaEventBridgeUtils {
configuration.eventBusName(), configuration.eventSource(),
configuration.eventType(),
configuration, client);
+ // the SDK client requires an id and generates none
EventBuilder builder = EventBuilder.builder()
+ .withId(UUID.randomUUID().toString())
.withSource(URI.create(configuration.eventSource()))
.withType(configuration.eventType())
.withAliyunEventBus(configuration.eventBusName())
diff --git
a/components/camel-alibaba/camel-alibaba-eventbridge/src/main/java/org/apache/camel/component/alibaba/eventbridge/MapCloudEventValidator.java
b/components/camel-alibaba/camel-alibaba-eventbridge/src/main/java/org/apache/camel/component/alibaba/eventbridge/MapCloudEventValidator.java
index 71e664f6968f..27f340e7ae84 100644
---
a/components/camel-alibaba/camel-alibaba-eventbridge/src/main/java/org/apache/camel/component/alibaba/eventbridge/MapCloudEventValidator.java
+++
b/components/camel-alibaba/camel-alibaba-eventbridge/src/main/java/org/apache/camel/component/alibaba/eventbridge/MapCloudEventValidator.java
@@ -23,6 +23,7 @@ import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.Map;
+import java.util.UUID;
import com.aliyun.eventbridge.EventBridgeClient;
import com.aliyun.eventbridge.models.CloudEvent;
@@ -101,10 +102,9 @@ final class MapCloudEventValidator {
.withType(type)
.withAliyunEventBus(eventBusName);
+ // the SDK client requires an id and generates none
String id = resolveString(getMapValue(map, "id", null), null);
- if (ObjectHelper.isNotEmpty(id)) {
- builder.withId(id);
- }
+ builder.withId(ObjectHelper.isNotEmpty(id) ? id :
UUID.randomUUID().toString());
String subject = resolveString(getMapValue(map,
AlibabaEventBridgeConstants.EVENT_SUBJECT, "subject"),
configuration.eventSubject());
diff --git
a/components/camel-alibaba/camel-alibaba-eventbridge/src/test/java/org/apache/camel/component/alibaba/eventbridge/PutEventsEventIdTest.java
b/components/camel-alibaba/camel-alibaba-eventbridge/src/test/java/org/apache/camel/component/alibaba/eventbridge/PutEventsEventIdTest.java
new file mode 100644
index 000000000000..6114a750a4cf
--- /dev/null
+++
b/components/camel-alibaba/camel-alibaba-eventbridge/src/test/java/org/apache/camel/component/alibaba/eventbridge/PutEventsEventIdTest.java
@@ -0,0 +1,199 @@
+/*
+ * 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.alibaba.eventbridge;
+
+import java.net.URI;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import com.aliyun.eventbridge.EventBridgeClient;
+import com.aliyun.eventbridge.models.CloudEvent;
+import com.aliyun.eventbridge.models.Config;
+import com.aliyun.eventbridge.util.EventBuilder;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import
org.apache.camel.component.alibaba.eventbridge.constants.AlibabaEventBridgeHeaders;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+/**
+ * The SDK client checks each event before it sends the request, and an event
without an id fails that check. Here a
+ * real {@link EventBridgeClient} runs the check, and only its doRequest is
replaced, so that no request is sent.
+ */
+class PutEventsEventIdTest extends CamelTestSupport {
+
+ private static final String JSON = "{\"orderId\":\"123\"}";
+
+ private final TestConfiguration testConfiguration = new
TestConfiguration();
+
+ @BindToRegistry("eventBridgeClient")
+ EventBridgeClient eventBridgeClient = clientThatSendsNothing();
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:put")
+ .to("alibaba-eventbridge:putEvents"
+ + "?eventBusName=" +
testConfiguration.getProperty("eventBusName")
+ + "&eventSource=" +
testConfiguration.getProperty("eventSource")
+ + "&eventType=" +
testConfiguration.getProperty("eventType")
+ + "&eventBridgeClient=#eventBridgeClient");
+ }
+ };
+ }
+
+ @Test
+ void aJsonTextBodyGetsAnId() {
+ template.sendBody("direct:put", JSON);
+
+ List<CloudEvent> events = checkedEvents();
+ assertThat(events).hasSize(1);
+ assertGeneratedId(events.get(0).getId());
+ }
+
+ @Test
+ void aMapBodyWithoutAnIdGetsAnId() {
+ template.sendBody("direct:put", Map.of("eventData", Map.of("orderId",
"123")));
+
+ List<CloudEvent> events = checkedEvents();
+ assertThat(events).hasSize(1);
+ assertGeneratedId(events.get(0).getId());
+ }
+
+ @Test
+ void aMapBodyKeepsItsId() {
+ template.sendBody("direct:put", Map.of("id", "order-123", "eventData",
Map.of("orderId", "123")));
+
+ List<CloudEvent> events = checkedEvents();
+ assertThat(events).hasSize(1);
+ assertThat(events.get(0).getId()).isEqualTo("order-123");
+ }
+
+ @Test
+ void eachEventOfABatchGetsAnIdOfItsOwn() {
+ template.sendBody("direct:put", List.of(JSON, JSON,
Map.of("eventData", Map.of("orderId", "456"))));
+
+ List<CloudEvent> events = checkedEvents();
+ assertThat(events).hasSize(3);
+
assertThat(events).extracting(CloudEvent::getId).allSatisfy(PutEventsEventIdTest::assertGeneratedId)
+ .doesNotHaveDuplicates();
+ }
+
+ @Test
+ void aBlankIdGetsAnIdWhenValidateEventSpecIsFalse() {
+ template.sendBodyAndHeader("direct:put", Map.of("id", " ",
"eventData", Map.of("orderId", "123")),
+ AlibabaEventBridgeHeaders.VALIDATE_EVENT_SPEC, false);
+
+ List<CloudEvent> events = checkedEvents();
+ assertThat(events).hasSize(1);
+ assertGeneratedId(events.get(0).getId());
+ }
+
+ @Test
+ void aBlankIdStillFailsWhenValidateEventSpecIsTrue() {
+ Map<String, Object> body = Map.of("id", " ", "eventData",
Map.of("orderId", "123"));
+
+ assertThatThrownBy(() -> template.sendBody("direct:put", body))
+ .isInstanceOf(CamelExecutionException.class)
+ .hasCauseInstanceOf(IllegalArgumentException.class)
+ .cause().hasMessage("Event 'id' cannot be blank when
provided");
+
+ verify(eventBridgeClient, never()).doRequest(any(), any(), any(),
any(), any(), any(), any());
+ }
+
+ @Test
+ void aCloudEventBodyIsSentAsItIs() {
+ CloudEvent cloudEvent = EventBuilder.builder()
+ .withId("event-1")
+
.withSource(URI.create(testConfiguration.getProperty("eventSource")))
+ .withType(testConfiguration.getProperty("eventType"))
+
.withAliyunEventBus(testConfiguration.getProperty("eventBusName"))
+ .withJsonStringData(JSON)
+ .build();
+
+ template.sendBody("direct:put", cloudEvent);
+
+ List<CloudEvent> events = checkedEvents();
+ assertThat(events).singleElement().isSameAs(cloudEvent);
+ assertThat(cloudEvent.getId()).isEqualTo("event-1");
+ }
+
+ @Test
+ void aCloudEventBodyWithoutAnIdIsNotGivenOne() {
+ CloudEvent cloudEvent = EventBuilder.builder()
+
.withSource(URI.create(testConfiguration.getProperty("eventSource")))
+ .withType(testConfiguration.getProperty("eventType"))
+
.withAliyunEventBus(testConfiguration.getProperty("eventBusName"))
+ .withJsonStringData(JSON)
+ .build();
+
+ assertThatThrownBy(() -> template.sendBody("direct:put", cloudEvent))
+ .isInstanceOf(CamelExecutionException.class)
+ .hasRootCauseMessage("Field id is required");
+
+ assertThat(cloudEvent.getId()).isNull();
+ verify(eventBridgeClient, never()).doRequest(any(), any(), any(),
any(), any(), any(), any());
+ }
+
+ /**
+ * A generated id is a UUID, as the doc page says.
+ */
+ private static void assertGeneratedId(String id) {
+ assertThat(id).isNotBlank();
+ assertThat(UUID.fromString(id)).hasToString(id);
+ }
+
+ /**
+ * The events of the one putEvents call, which the SDK client checked
before it called doRequest once.
+ */
+ @SuppressWarnings("unchecked")
+ private List<CloudEvent> checkedEvents() {
+ verify(eventBridgeClient).doRequest(eq("putEvents"), any(), any(),
any(), any(), any(), any());
+ ArgumentCaptor<List<CloudEvent>> events =
ArgumentCaptor.forClass(List.class);
+ verify(eventBridgeClient).putEvents(events.capture());
+ return events.getValue();
+ }
+
+ /**
+ * A real client, so that putEvents checks the events as it does in
production. Only doRequest, which would send the
+ * request, is replaced, so the endpoint below is never contacted.
+ */
+ private EventBridgeClient clientThatSendsNothing() {
+ Config config = new Config();
+ config.setAccessKeyId(testConfiguration.getProperty("accessKey"));
+ config.setAccessKeySecret(testConfiguration.getProperty("secretKey"));
+ config.setEndpoint("eventbridge.invalid");
+ EventBridgeClient client = spy(new EventBridgeClient(config));
+ doReturn(Map.of("RequestId", "req-eb-id", "FailedEntryCount", 0))
+ .when(client).doRequest(any(), any(), any(), any(), any(),
any(), any());
+ return client;
+ }
+}