You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2025/07/25 07:59:29 UTC

(camel) 01/03: Added Loop EIP test for Camel 22261 (#18714)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-4.10.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 498539e54dfc96dd4f3327f38e04dc9d4ad14efd
Author: lantzen <89...@users.noreply.github.com>
AuthorDate: Thu Jul 24 14:31:29 2025 +0200

    Added Loop EIP test for Camel 22261 (#18714)
    
    * CAMEL-22261 Reuse existing transaction if already transacted
    
    * CAMEL-22261 camel format of the code
    
    * CAMEL-22261 Added Loop EIP test
---
 ...rMultipleMessagesInTransactionWithLoopTest.java | 137 +++++++++++++++++++++
 1 file changed, 137 insertions(+)

diff --git a/components/camel-kafka/src/test/java/org/apache/camel/component/kafka/KafkaProducerMultipleMessagesInTransactionWithLoopTest.java b/components/camel-kafka/src/test/java/org/apache/camel/component/kafka/KafkaProducerMultipleMessagesInTransactionWithLoopTest.java
new file mode 100644
index 00000000000..c5aaa5a9e81
--- /dev/null
+++ b/components/camel-kafka/src/test/java/org/apache/camel/component/kafka/KafkaProducerMultipleMessagesInTransactionWithLoopTest.java
@@ -0,0 +1,137 @@
+/*
+ * 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.kafka;
+
+import java.util.Properties;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.kafka.clients.producer.MockProducer;
+import org.apache.kafka.common.serialization.StringSerializer;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.mockito.Mockito;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.mockito.ArgumentMatchers.any;
+
+@TestMethodOrder(MethodOrderer.MethodName.class)
+public class KafkaProducerMultipleMessagesInTransactionWithLoopTest extends CamelTestSupport {
+    @EndpointInject("mock:done")
+    protected MockEndpoint doneEndpoint;
+
+    private MockProducer<String, String> mockProducer
+            = new MockProducer<>(true, new StringSerializer(), new StringSerializer());
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+
+        KafkaClientFactory kcf = Mockito.mock(KafkaClientFactory.class);
+        Mockito.when(kcf.getProducer(any(Properties.class))).thenReturn(mockProducer);
+
+        KafkaComponent kafka = new KafkaComponent();
+        kafka.getConfiguration().setBrokers("broker1:1234,broker2:4567");
+        kafka.getConfiguration().setRecordMetadata(true);
+        kafka.setKafkaClientFactory(kcf);
+
+        context.addComponent("kafka", kafka);
+
+        return context;
+    }
+
+    /**
+     * In a Loop EIP sends messages with transactional.id to Kafka.
+     */
+    @Test
+    public void test01_HappyLoopPath() throws Exception {
+        int messageCount = 5;
+
+        doneEndpoint.expectedMessageCount(1);
+
+        template.sendBody("direct:loop", messageCount);
+
+        MockEndpoint.assertIsSatisfied(context);
+
+        assertEquals(messageCount, mockProducer.history().size());
+        assertEquals(1, mockProducer.commitCount());
+    }
+
+    /**
+     * Using the same route as in test01_HappyLoopPath but will throw a RuntimeException on the last iteration.
+     */
+    @Test
+    public void test02_OnExceptionWithLoop() throws Exception {
+        Exception exceptionCaught = null;
+        int throwExeptionOnIndex = 4;
+        try {
+            template.sendBodyAndHeader("direct:loop", throwExeptionOnIndex + 1, "ThrowExeptionOnIndex", throwExeptionOnIndex);
+        } catch (CamelExecutionException e) {
+            exceptionCaught = e;
+        }
+
+        assertInstanceOf(CamelExecutionException.class, exceptionCaught);
+        assertInstanceOf(RuntimeException.class, exceptionCaught.getCause());
+        assertEquals("Failing with Index: " + throwExeptionOnIndex, exceptionCaught.getCause().getMessage());
+
+        assertEquals(0, mockProducer.history().size());
+        assertEquals(0, mockProducer.commitCount());
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:loop")
+                        .id("loop")
+                        .setVariable("ThrowExeptionOnIndex",
+                                header("ThrowExeptionOnIndex").convertTo(Integer.class))
+                        .loop(body().convertTo(Integer.class))
+                        .choice().when(exchange -> {
+                            Integer throwExeptionOnIndex = exchange.getVariable("ThrowExeptionOnIndex", Integer.class);
+                            Integer camelLoopIndex = exchange.getProperty("CamelLoopIndex", Integer.class);
+
+                            if (null != throwExeptionOnIndex && throwExeptionOnIndex == camelLoopIndex) {
+                                return true;
+                            } else {
+                                System.out.printf(
+                                        "***** Sending message to Kafka from Loop exchange with id '%s' and UnitOfWork: %s%n",
+                                        exchange.getExchangeId(), exchange.getUnitOfWork().hashCode());
+
+                                return false;
+                            }
+                        })
+                        .throwException(RuntimeException.class, "Failing with Index: ${exchangeProperty.CamelLoopIndex}")
+                        .otherwise()
+                        .setBody(simple("test ${exchangeProperty.CamelLoopIndex}"))
+                        .to("kafka:loop?additional-properties[transactional.id]=1234&additional-properties[enable.idempotence]=true&additional-properties[retries]=5")
+                        .end() // .choice
+                        .end() // .loop
+                        .to("mock:done");
+            }
+        };
+    }
+}