You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pp...@apache.org on 2021/07/14 14:55:49 UTC

[camel-quarkus] 03/13: Test dynamicRouter() EIP DSL method #2628

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

ppalaga pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git

commit d60a4440b23efb852b5506424e652019b636a4f4
Author: Peter Palaga <pp...@redhat.com>
AuthorDate: Wed May 19 22:18:23 2021 +0200

    Test dynamicRouter() EIP DSL method #2628
---
 .../quarkus/component/bean/eip/EipResource.java    | 60 ++++++++++++++
 .../quarkus/component/bean/eip/EipRoutes.java      | 96 ++++++++++++++++++++++
 .../camel/quarkus/component/bean/EipIT.java}       | 29 +------
 .../camel/quarkus/component/bean/EipTest.java      | 89 ++++++++++++++++++++
 .../org/apache/camel/quarkus/eip/it/EipRoutes.java | 16 ++++
 .../org/apache/camel/quarkus/eip/it/EipTest.java   | 27 ++++++
 6 files changed, 292 insertions(+), 25 deletions(-)

diff --git a/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/eip/EipResource.java b/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/eip/EipResource.java
new file mode 100644
index 0000000..c9ae9ad
--- /dev/null
+++ b/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/eip/EipResource.java
@@ -0,0 +1,60 @@
+/*
+ * 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.quarkus.component.bean.eip;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ProducerTemplate;
+
+@Path("/bean/eip")
+@ApplicationScoped
+public class EipResource {
+
+    @Inject
+    ProducerTemplate producerTemplate;
+
+    @Inject
+    CamelContext context;
+
+    @Path("/route/{route}")
+    @POST
+    @Consumes(MediaType.TEXT_PLAIN)
+    @Produces(MediaType.TEXT_PLAIN)
+    public String route(String body, @PathParam("route") String route) {
+        return producerTemplate.requestBody("direct:" + route, body, String.class);
+    }
+
+    @Path("/result/{name}")
+    @Produces(MediaType.TEXT_PLAIN)
+    @GET
+    public String mock(@PathParam("name") String name, @PathParam("count") int count, @PathParam("timeout") int timeout) {
+        List<String> results = context.getRegistry().lookupByNameAndType(name, List.class);
+        return results.stream().collect(Collectors.joining(","));
+    }
+}
diff --git a/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/eip/EipRoutes.java b/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/eip/EipRoutes.java
new file mode 100644
index 0000000..f27e872
--- /dev/null
+++ b/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/eip/EipRoutes.java
@@ -0,0 +1,96 @@
+/*
+ * 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.quarkus.component.bean.eip;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.enterprise.inject.Produces;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
+import io.quarkus.runtime.annotations.RegisterForReflection;
+import org.apache.camel.Consume;
+import org.apache.camel.builder.RouteBuilder;
+
+public class EipRoutes extends RouteBuilder {
+
+    @Override
+    public void configure() {
+        from("direct:dynamicRouter")
+                .dynamicRouter(method("myDynamicRouter", "route"));
+
+    }
+
+    @Produces
+    @Singleton
+    @Named("dynamicRouterResult0")
+    List<String> dynamicRouterResult0() {
+        return new ArrayList<>();
+    }
+
+    @Produces
+    @Singleton
+    @Named("dynamicRouterResult1")
+    List<String> dynamicRouterResult1() {
+        return new ArrayList<>();
+    }
+
+    @Singleton
+    @Named("myDynamicRouter")
+    @RegisterForReflection
+    static class CustomDynamicRouter {
+        public String route(String body) {
+            try {
+                int val = Integer.parseInt(body);
+                return "bean:dynamicRouterResult" + (val % 2) + "?method=add";
+            } catch (NumberFormatException e) {
+                return null;
+            }
+        }
+    }
+
+    @RegisterForReflection(targets = ArrayList.class) // for dynamicRouterAnnotationResult
+    static class DynamicRouterWithAnnotation {
+
+        @Consume("direct:dynamicRouterAnnotation")
+        @org.apache.camel.DynamicRouter
+        public String route(String body) {
+            try {
+                int val = Integer.parseInt(body);
+                return "bean:dynamicRouterAnnotationResult" + (val % 2) + "?method=add";
+            } catch (NumberFormatException e) {
+                return null;
+            }
+        }
+    }
+
+    @Produces
+    @Singleton
+    @Named("dynamicRouterAnnotationResult0")
+    List<String> dynamicRouterResult2() {
+        return new ArrayList<>();
+    }
+
+    @Produces
+    @Singleton
+    @Named("dynamicRouterAnnotationResult1")
+    List<String> dynamicRouterResult3() {
+        return new ArrayList<>();
+    }
+
+}
diff --git a/integration-test-groups/foundation/eip/src/test/java/org/apache/camel/quarkus/eip/it/EipTest.java b/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/EipIT.java
similarity index 51%
copy from integration-test-groups/foundation/eip/src/test/java/org/apache/camel/quarkus/eip/it/EipTest.java
copy to integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/EipIT.java
index bfa129c..85cbc75 100644
--- a/integration-test-groups/foundation/eip/src/test/java/org/apache/camel/quarkus/eip/it/EipTest.java
+++ b/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/EipIT.java
@@ -14,32 +14,11 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.quarkus.eip.it;
+package org.apache.camel.quarkus.component.bean;
 
-import io.quarkus.test.junit.QuarkusTest;
-import io.restassured.RestAssured;
-import io.restassured.http.ContentType;
-import org.hamcrest.Matchers;
-import org.junit.jupiter.api.Test;
+import io.quarkus.test.junit.NativeImageTest;
 
-@QuarkusTest
-class EipTest {
-
-    @Test
-    public void claimCheckByHeader() {
-        RestAssured.given()
-                .contentType(ContentType.TEXT)
-                .body("Secret")
-                .queryParam("claimCheckId", "foo")
-                .post("/eip/route/claimCheckByHeader")
-                .then()
-                .statusCode(200);
-
-        RestAssured.get("/eip/mock/claimCheckByHeader/4/10000")
-                .then()
-                .statusCode(200)
-                .body(Matchers.is("Bye World,Secret,Hi World,Secret"));
-
-    }
+@NativeImageTest
+class EipIT extends EipTest {
 
 }
diff --git a/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/EipTest.java b/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/EipTest.java
new file mode 100644
index 0000000..ec7c863
--- /dev/null
+++ b/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/EipTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.quarkus.component.bean;
+
+import java.util.concurrent.TimeUnit;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.RestAssured;
+import io.restassured.http.ContentType;
+import org.awaitility.Awaitility;
+import org.hamcrest.Matchers;
+import org.junit.jupiter.api.Test;
+
+@QuarkusTest
+class EipTest {
+
+    @Test
+    public void dynamicRouter() {
+        for (int i = 0; i < 4; i++) {
+            RestAssured.given()
+                    .contentType(ContentType.TEXT)
+                    .body(String.valueOf(i))
+                    .post("/bean/eip/route/dynamicRouter")
+                    .then()
+                    .statusCode(200);
+        }
+
+        Awaitility.await().pollInterval(50, TimeUnit.MILLISECONDS).atMost(30, TimeUnit.SECONDS)
+                .until(
+                        () -> RestAssured.get("/bean/eip/result/dynamicRouterResult0")
+                                .then()
+                                .statusCode(200)
+                                .extract().body().asString(),
+                        Matchers.is("0,2"));
+
+        Awaitility.await().pollInterval(50, TimeUnit.MILLISECONDS).atMost(30, TimeUnit.SECONDS)
+                .until(
+                        () -> RestAssured.get("/bean/eip/result/dynamicRouterResult1")
+                                .then()
+                                .statusCode(200)
+                                .extract().body().asString(),
+                        Matchers.is("1,3"));
+
+    }
+
+    @Test
+    public void dynamicRouterAnnotation() {
+        for (int i = 4; i < 8; i++) {
+            RestAssured.given()
+                    .contentType(ContentType.TEXT)
+                    .body(String.valueOf(i))
+                    .post("/bean/eip/route/dynamicRouterAnnotation")
+                    .then()
+                    .statusCode(200);
+        }
+
+        Awaitility.await().pollInterval(50, TimeUnit.MILLISECONDS).atMost(30, TimeUnit.SECONDS)
+                .until(
+                        () -> RestAssured.get("/bean/eip/result/dynamicRouterAnnotationResult0")
+                                .then()
+                                .statusCode(200)
+                                .extract().body().asString(),
+                        Matchers.is("4,6"));
+
+        Awaitility.await().pollInterval(50, TimeUnit.MILLISECONDS).atMost(30, TimeUnit.SECONDS)
+                .until(
+                        () -> RestAssured.get("/bean/eip/result/dynamicRouterAnnotationResult1")
+                                .then()
+                                .statusCode(200)
+                                .extract().body().asString(),
+                        Matchers.is("5,7"));
+
+    }
+
+}
diff --git a/integration-test-groups/foundation/eip/src/main/java/org/apache/camel/quarkus/eip/it/EipRoutes.java b/integration-test-groups/foundation/eip/src/main/java/org/apache/camel/quarkus/eip/it/EipRoutes.java
index 7c1b078..0e2a6b0 100644
--- a/integration-test-groups/foundation/eip/src/main/java/org/apache/camel/quarkus/eip/it/EipRoutes.java
+++ b/integration-test-groups/foundation/eip/src/main/java/org/apache/camel/quarkus/eip/it/EipRoutes.java
@@ -16,8 +16,13 @@
  */
 package org.apache.camel.quarkus.eip.it;
 
+import javax.enterprise.inject.Produces;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.model.ClaimCheckOperation;
+import org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer;
 
 public class EipRoutes extends RouteBuilder {
 
@@ -33,5 +38,16 @@ public class EipRoutes extends RouteBuilder {
                 .to("mock:claimCheckByHeader")
                 .claimCheck(ClaimCheckOperation.Get, "${header.claimCheckId}")
                 .to("mock:claimCheckByHeader");
+
+        from("direct:customLoadBalancer")
+                .loadBalance().custom("roundRobin")
+                .to("mock:customLoadBalancer1", "mock:customLoadBalancer2");
+    }
+
+    @Produces
+    @Singleton
+    @Named("roundRobin")
+    RoundRobinLoadBalancer roundRobinLoadBalancer() {
+        return new RoundRobinLoadBalancer();
     }
 }
diff --git a/integration-test-groups/foundation/eip/src/test/java/org/apache/camel/quarkus/eip/it/EipTest.java b/integration-test-groups/foundation/eip/src/test/java/org/apache/camel/quarkus/eip/it/EipTest.java
index bfa129c..6c94d4d 100644
--- a/integration-test-groups/foundation/eip/src/test/java/org/apache/camel/quarkus/eip/it/EipTest.java
+++ b/integration-test-groups/foundation/eip/src/test/java/org/apache/camel/quarkus/eip/it/EipTest.java
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.quarkus.eip.it;
 
+import java.util.Arrays;
+import java.util.List;
+
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
 import io.restassured.http.ContentType;
@@ -42,4 +45,28 @@ class EipTest {
 
     }
 
+    @Test
+    public void customLoadBalancer() {
+        final List<String> messages = Arrays.asList("a", "b", "c", "d");
+        for (String msg : messages) {
+            RestAssured.given()
+                    .contentType(ContentType.TEXT)
+                    .body(msg)
+                    .post("/eip/route/customLoadBalancer")
+                    .then()
+                    .statusCode(200);
+        }
+
+        RestAssured.get("/eip/mock/customLoadBalancer1/2/10000")
+                .then()
+                .statusCode(200)
+                .body(Matchers.is("a,c"));
+
+        RestAssured.get("/eip/mock/customLoadBalancer2/2/10000")
+                .then()
+                .statusCode(200)
+                .body(Matchers.is("b,d"));
+
+    }
+
 }