You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/07/31 08:34:54 UTC

[camel] branch main updated: CAMEL-19675: added test case for aggregation strategy with attachment (#10895)

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

orpiske 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 4703fe669da CAMEL-19675: added test case for aggregation strategy with attachment (#10895)
4703fe669da is described below

commit 4703fe669da87748b9cd6fe2e948fc6a4cba9844
Author: rvanderhallen <54...@users.noreply.github.com>
AuthorDate: Mon Jul 31 10:34:48 2023 +0200

    CAMEL-19675: added test case for aggregation strategy with attachment (#10895)
    
    Co-authored-by: Robin Vanderhallen <ro...@aviobook.aero>
---
 .../MulticastAggregationStrategyTest.java          | 65 ++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/components/camel-attachments/src/test/java/org/apache/camel/attachment/MulticastAggregationStrategyTest.java b/components/camel-attachments/src/test/java/org/apache/camel/attachment/MulticastAggregationStrategyTest.java
new file mode 100644
index 00000000000..85c134bc5fd
--- /dev/null
+++ b/components/camel-attachments/src/test/java/org/apache/camel/attachment/MulticastAggregationStrategyTest.java
@@ -0,0 +1,65 @@
+package org.apache.camel.attachment;
+
+import jakarta.activation.DataHandler;
+import org.apache.camel.AggregationStrategy;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.Builder;
+import org.apache.camel.builder.ExchangeBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MulticastAggregationStrategyTest extends CamelTestSupport {
+
+    @Test
+    void testAggregationStrategyWithAttachment() {
+        Exchange exchange = ExchangeBuilder.anExchange(new DefaultCamelContext()).build();
+        
+        template.send("direct:start", exchange);
+
+        AttachmentMessage msg = exchange.getMessage(AttachmentMessage.class);
+        
+        assertTrue(msg.hasAttachments());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start")
+                    .multicast(new AttachmentMessageAggregationStrategy()).stopOnException()
+                        .to("direct:setBody", "direct:setAttachment")
+                    .end();
+
+                from("direct:setBody")
+                    .setBody(Builder.constant("body"));
+
+                from("direct:setAttachment")
+                    .setBody(Builder.constant("attachment".getBytes()));
+            }
+        };
+    }
+    
+    private static class AttachmentMessageAggregationStrategy implements AggregationStrategy {
+
+        @Override
+        public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
+            if (oldExchange == null) {
+                return newExchange;
+            }
+
+            if (newExchange.getIn().getBody() instanceof String) {
+                oldExchange.getMessage().setBody(newExchange.getIn().getBody());
+            } else {
+                byte[] data = newExchange.getIn().getBody(byte[].class);
+                oldExchange.getMessage(AttachmentMessage.class).addAttachment("attachment", new DataHandler(data, "text/plain"));
+            }
+
+            return oldExchange;
+        }
+    }
+}