You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2022/11/10 13:49:49 UTC

[camel-quarkus] branch main updated: controlbus: Added language tests (fixes #4008)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 397b500081 controlbus: Added language tests (fixes #4008)
397b500081 is described below

commit 397b50008164a4f0e1728df40cfe3b72cc8003c0
Author: Darren Coleman <dc...@redhat.com>
AuthorDate: Tue Nov 8 14:21:00 2022 +0000

    controlbus: Added language tests (fixes #4008)
---
 .../pages/reference/extensions/controlbus.adoc     | 48 +++++++++++-
 .../controlbus/runtime/src/main/doc/usage.adoc     | 46 +++++++++++-
 .../foundation/controlbus/pom.xml                  | 17 +++++
 .../component/controlbus/it/ControlbusBean.java    | 40 ++++++++++
 .../controlbus/it/ControlbusLanguageResource.java  | 86 ++++++++++++++++++++++
 .../controlbus/it/ControlbusLanguageRoute.java     | 45 +++++++++++
 .../controlbus/it/ControlbusLanguageIT.java        | 24 ++++++
 .../controlbus/it/ControlbusLanguageTest.java      | 54 ++++++++++++++
 8 files changed, 357 insertions(+), 3 deletions(-)

diff --git a/docs/modules/ROOT/pages/reference/extensions/controlbus.adoc b/docs/modules/ROOT/pages/reference/extensions/controlbus.adoc
index 2791345494..191182d933 100644
--- a/docs/modules/ROOT/pages/reference/extensions/controlbus.adoc
+++ b/docs/modules/ROOT/pages/reference/extensions/controlbus.adoc
@@ -47,8 +47,11 @@ endif::[]
 
 [id="extensions-controlbus-usage"]
 == Usage
+[id="extensions-controlbus-usage-actions"]
+=== Actions
+
 [id="extensions-controlbus-usage-statistics"]
-=== Statistics
+==== Statistics
 
 When using the `stats` command endpoint, the `camel-quarkus-management` extension must be added as a project dependency to enable JMX. Maven users will have to add the following to their `pom.xml`:
 
@@ -61,6 +64,49 @@ When using the `stats` command endpoint, the `camel-quarkus-management` extensio
 ----
 
 
+[id="extensions-controlbus-usage-languages"]
+=== Languages
+
+[id="extensions-controlbus-usage-bean"]
+==== Bean
+
+The Bean language can be used to invoke a method on a Bean to control the state of routes. The `org.apache.camel.quarkus:camel-quarkus-bean` extension must be added to the classpath. Maven users must add the following dependency to the POM:
+
+[source,xml]
+----
+<dependency>
+    <groupId>org.apache.camel.quarkus</groupId>
+    <artifactId>camel-quarkus-bean</artifactId>
+</dependency>
+----
+
+In native mode, the Bean class must be annotated with `@RegisterForReflection`.
+
+[id="extensions-controlbus-usage-simple"]
+==== Simple
+
+The Simple language can be used to control the state of routes. The following example uses a `ProducerTemplate` to stop a route with the id `foo`:
+
+[source,java]
+----
+template.sendBody(
+    "controlbus:language:simple", 
+    "${camelContext.getRouteController().stopRoute('foo')}"
+);
+----
+
+To use the OGNL notation, the `org.apache.camel.quarkus:camel-quarkus-bean` extension must be added as a dependency.
+
+In native mode, the classes used in the OGNL notation must be registered for reflection. In the above code snippet, the `org.apache.camel.spi.RouteController` class returned from `camelContext.getRouteController()` must be registered. As this is a third-party class, it cannot be annotated with `@RegisterForReflection` directly - instead you can annotate a different class and specifying the target classes to register. For example, the class defining the Camel routes could be annotated wit [...]
+
+Alternatively, add the following line to your `src/main/resources/application.properties`:
+
+[source,properties]
+----
+quarkus.camel.native.reflection.include-patterns = org.apache.camel.spi.RouteController
+----
+
+
 [id="extensions-controlbus-camel-quarkus-limitations"]
 == Camel Quarkus limitations
 
diff --git a/extensions/controlbus/runtime/src/main/doc/usage.adoc b/extensions/controlbus/runtime/src/main/doc/usage.adoc
index cbe7cdd4c9..39d6a7df72 100644
--- a/extensions/controlbus/runtime/src/main/doc/usage.adoc
+++ b/extensions/controlbus/runtime/src/main/doc/usage.adoc
@@ -1,4 +1,6 @@
-=== Statistics
+=== Actions
+
+==== Statistics
 
 When using the `stats` command endpoint, the `camel-quarkus-management` extension must be added as a project dependency to enable JMX. Maven users will have to add the following to their `pom.xml`:
 
@@ -8,4 +10,44 @@ When using the `stats` command endpoint, the `camel-quarkus-management` extensio
     <groupId>org.apache.camel.quarkus</groupId>
     <artifactId>camel-quarkus-management</artifactId>
 </dependency>
-----
\ No newline at end of file
+----
+
+
+=== Languages
+
+==== Bean
+
+The Bean language can be used to invoke a method on a Bean to control the state of routes. The `org.apache.camel.quarkus:camel-quarkus-bean` extension must be added to the classpath. Maven users must add the following dependency to the POM:
+
+[source,xml]
+----
+<dependency>
+    <groupId>org.apache.camel.quarkus</groupId>
+    <artifactId>camel-quarkus-bean</artifactId>
+</dependency>
+----
+
+In native mode, the Bean class must be annotated with `@RegisterForReflection`.
+
+==== Simple
+
+The Simple language can be used to control the state of routes. The following example uses a `ProducerTemplate` to stop a route with the id `foo`:
+
+[source,java]
+----
+template.sendBody(
+    "controlbus:language:simple", 
+    "${camelContext.getRouteController().stopRoute('foo')}"
+);
+----
+
+To use the OGNL notation, the `org.apache.camel.quarkus:camel-quarkus-bean` extension must be added as a dependency.
+
+In native mode, the classes used in the OGNL notation must be registered for reflection. In the above code snippet, the `org.apache.camel.spi.RouteController` class returned from `camelContext.getRouteController()` must be registered. As this is a third-party class, it cannot be annotated with `@RegisterForReflection` directly - instead you can annotate a different class and specifying the target classes to register. For example, the class defining the Camel routes could be annotated wit [...]
+
+Alternatively, add the following line to your `src/main/resources/application.properties`:
+
+[source,properties]
+----
+quarkus.camel.native.reflection.include-patterns = org.apache.camel.spi.RouteController
+----
diff --git a/integration-test-groups/foundation/controlbus/pom.xml b/integration-test-groups/foundation/controlbus/pom.xml
index d9fd8bf90c..800f4e274c 100644
--- a/integration-test-groups/foundation/controlbus/pom.xml
+++ b/integration-test-groups/foundation/controlbus/pom.xml
@@ -31,6 +31,10 @@
     <description>Integration tests for Camel Quarkus Control Bus extension</description>
 
     <dependencies>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-bean</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.apache.camel.quarkus</groupId>
             <artifactId>camel-quarkus-controlbus</artifactId>
@@ -102,6 +106,19 @@
             </activation>
             <dependencies>
                 <!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
+                <dependency>
+                    <groupId>org.apache.camel.quarkus</groupId>
+                    <artifactId>camel-quarkus-bean-deployment</artifactId>
+                    <version>${project.version}</version>
+                    <type>pom</type>
+                    <scope>test</scope>
+                    <exclusions>
+                        <exclusion>
+                            <groupId>*</groupId>
+                            <artifactId>*</artifactId>
+                        </exclusion>
+                    </exclusions>
+                </dependency>
                 <dependency>
                     <groupId>org.apache.camel.quarkus</groupId>
                     <artifactId>camel-quarkus-controlbus-deployment</artifactId>
diff --git a/integration-test-groups/foundation/controlbus/src/main/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusBean.java b/integration-test-groups/foundation/controlbus/src/main/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusBean.java
new file mode 100644
index 0000000000..4d7db4000e
--- /dev/null
+++ b/integration-test-groups/foundation/controlbus/src/main/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusBean.java
@@ -0,0 +1,40 @@
+/*
+ * 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.controlbus.it;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Named;
+
+import io.quarkus.runtime.annotations.RegisterForReflection;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Header;
+
+@ApplicationScoped
+@Named("controlbus-bean")
+@RegisterForReflection(fields = false)
+public class ControlbusBean {
+
+    private CamelContext context;
+
+    ControlbusBean(CamelContext context) {
+        this.context = context;
+    }
+
+    public void stopRoute(@Header("routeId") String routeId) throws Exception {
+        context.getRouteController().stopRoute(routeId);
+    }
+}
diff --git a/integration-test-groups/foundation/controlbus/src/main/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusLanguageResource.java b/integration-test-groups/foundation/controlbus/src/main/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusLanguageResource.java
new file mode 100644
index 0000000000..1f57cee344
--- /dev/null
+++ b/integration-test-groups/foundation/controlbus/src/main/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusLanguageResource.java
@@ -0,0 +1,86 @@
+/*
+ * 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.controlbus.it;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.camel.ProducerTemplate;
+
+@Path("/controlbus/language")
+@ApplicationScoped
+public class ControlbusLanguageResource {
+
+    private static final String CONTROL_ROUTE_ID = "lang-control";
+
+    @Inject
+    ProducerTemplate producerTemplate;
+
+    @Path("/status")
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public String status() throws Exception {
+        return producerTemplate.requestBody(
+                String.format("controlbus:route?routeId=%s&action=status", CONTROL_ROUTE_ID),
+                null, String.class);
+    }
+
+    @Path("/start")
+    @POST
+    public void start() throws Exception {
+        producerTemplate.sendBody(String.format("controlbus:route?routeId=%s&action=start", CONTROL_ROUTE_ID), null);
+    }
+
+    @Path("/simple")
+    @POST
+    public void simple() throws Exception {
+        producerTemplate.sendBody(
+                "controlbus:language:simple",
+                String.format("${camelContext.getRouteController().stopRoute('%s')}", CONTROL_ROUTE_ID));
+    }
+
+    @Path("/bean")
+    @POST
+    public void bean() throws Exception {
+        producerTemplate.sendBodyAndHeader(
+                "controlbus:language:bean",
+                "controlbus-bean?method=stopRoute",
+                "routeId",
+                CONTROL_ROUTE_ID);
+    }
+
+    @Path("/header")
+    @POST
+    public void header() throws Exception {
+        producerTemplate.sendBody(
+                "direct:header",
+                "action");
+    }
+
+    @Path("/exchangeProperty")
+    @POST
+    public void exchangeProperty() throws Exception {
+        producerTemplate.sendBody(
+                "direct:exchangeProperty",
+                "action");
+    }
+}
diff --git a/integration-test-groups/foundation/controlbus/src/main/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusLanguageRoute.java b/integration-test-groups/foundation/controlbus/src/main/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusLanguageRoute.java
new file mode 100644
index 0000000000..b566b1c3bc
--- /dev/null
+++ b/integration-test-groups/foundation/controlbus/src/main/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusLanguageRoute.java
@@ -0,0 +1,45 @@
+/*
+ * 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.controlbus.it;
+
+import javax.enterprise.context.ApplicationScoped;
+
+import io.quarkus.runtime.annotations.RegisterForReflection;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.RouteController;
+
+@ApplicationScoped
+@RegisterForReflection(targets = { RouteController.class })
+public class ControlbusLanguageRoute extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        from("direct:lang-control")
+                .routeId("lang-control")
+                .log("control: ${body}");
+
+        from("direct:header")
+                .setHeader("action", constant("stop"))
+                .to("controlbus:language:header")
+                .toD("controlbus:route?routeId=lang-control&action=${body}");
+
+        from("direct:exchangeProperty")
+                .setProperty("action", constant("stop"))
+                .to("controlbus:language:exchangeProperty")
+                .toD("controlbus:route?routeId=lang-control&action=${body}");
+    }
+}
diff --git a/integration-test-groups/foundation/controlbus/src/test/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusLanguageIT.java b/integration-test-groups/foundation/controlbus/src/test/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusLanguageIT.java
new file mode 100644
index 0000000000..50a18816d4
--- /dev/null
+++ b/integration-test-groups/foundation/controlbus/src/test/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusLanguageIT.java
@@ -0,0 +1,24 @@
+/*
+ * 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.controlbus.it;
+
+import io.quarkus.test.junit.QuarkusIntegrationTest;
+
+@QuarkusIntegrationTest
+class ControlbusLanguageIT extends ControlbusLanguageTest {
+
+}
diff --git a/integration-test-groups/foundation/controlbus/src/test/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusLanguageTest.java b/integration-test-groups/foundation/controlbus/src/test/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusLanguageTest.java
new file mode 100644
index 0000000000..ac3962bc7e
--- /dev/null
+++ b/integration-test-groups/foundation/controlbus/src/test/java/org/apache/camel/quarkus/component/controlbus/it/ControlbusLanguageTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.controlbus.it;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.RestAssured;
+import io.restassured.http.ContentType;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+
+@QuarkusTest
+class ControlbusLanguageTest {
+
+    @BeforeEach
+    public void startRoute() {
+        String status = RestAssured.get("/controlbus/language/status").asString();
+        if ("Stopped".equals(status)) {
+            RestAssured.post("/controlbus/language/start");
+        }
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = { "simple", "bean", "header", "exchangeProperty" })
+    public void testLanguage(String language) {
+        RestAssured.given()
+                .contentType(ContentType.TEXT).get("/controlbus/language/status")
+                .then().body(equalTo("Started"));
+
+        RestAssured.given()
+                .contentType(ContentType.TEXT).post("/controlbus/language/" + language)
+                .then().statusCode(204);
+
+        RestAssured.given()
+                .contentType(ContentType.TEXT).get("/controlbus/language/status")
+                .then().body(equalTo("Stopped"));
+    }
+}