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/05/19 10:56:16 UTC
[camel-quarkus] branch main updated: Improve camel-quarkus-velocity test coverage #3790
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 8a159df5b5 Improve camel-quarkus-velocity test coverage #3790
8a159df5b5 is described below
commit 8a159df5b5ef06a136d63ef35272e87bf9f41908
Author: JiriOndrusek <on...@gmail.com>
AuthorDate: Tue May 17 13:03:49 2022 +0200
Improve camel-quarkus-velocity test coverage #3790
---
.../ROOT/pages/reference/extensions/velocity.adoc | 16 +++++++
.../velocity/runtime/src/main/doc/usage.adoc | 12 +++++
.../component/velocity/it/VelocityResource.java | 53 ++++++++++++++++++++++
.../src/main/resources/template/dynamicTemplate.vm | 17 +++++++
.../src/main/resources/template/velocityContext.vm | 17 +++++++
.../component/velocity/it/VelocityTest.java | 34 ++++++++++++++
6 files changed, 149 insertions(+)
diff --git a/docs/modules/ROOT/pages/reference/extensions/velocity.adoc b/docs/modules/ROOT/pages/reference/extensions/velocity.adoc
index 0ce3c95291..a16985261c 100644
--- a/docs/modules/ROOT/pages/reference/extensions/velocity.adoc
+++ b/docs/modules/ROOT/pages/reference/extensions/velocity.adoc
@@ -38,6 +38,22 @@ Or add the coordinates to your existing project:
Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.
+== Usage
+
+=== Custom body as domain object in the native mode
+
+When using a custom object as message body and referencing its properties in the template in the native mode, all the classes need to be registered for reflection (see the https://quarkus.io/guides/writing-native-applications-tips#register-reflection[documentation]).
+
+
+Example:
+[source,java]
+----
+@RegisterForReflection
+public interface CustomBody {
+}
+----
+
+
== allowContextMapAll option in native mode
The `allowContextMapAll` option is not supported in native mode as it requires reflective access to security sensitive camel core classes such as
diff --git a/extensions/velocity/runtime/src/main/doc/usage.adoc b/extensions/velocity/runtime/src/main/doc/usage.adoc
new file mode 100644
index 0000000000..4b691b8ba4
--- /dev/null
+++ b/extensions/velocity/runtime/src/main/doc/usage.adoc
@@ -0,0 +1,12 @@
+=== Custom body as domain object in the native mode
+
+When using a custom object as message body and referencing its properties in the template in the native mode, all the classes need to be registered for reflection (see the https://quarkus.io/guides/writing-native-applications-tips#register-reflection[documentation]).
+
+
+Example:
+[source,java]
+----
+@RegisterForReflection
+public interface CustomBody {
+}
+----
diff --git a/integration-tests/velocity/src/main/java/org/apache/camel/quarkus/component/velocity/it/VelocityResource.java b/integration-tests/velocity/src/main/java/org/apache/camel/quarkus/component/velocity/it/VelocityResource.java
index f3313deaf2..0be83294b0 100644
--- a/integration-tests/velocity/src/main/java/org/apache/camel/quarkus/component/velocity/it/VelocityResource.java
+++ b/integration-tests/velocity/src/main/java/org/apache/camel/quarkus/component/velocity/it/VelocityResource.java
@@ -32,8 +32,11 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.velocity.VelocityConstants;
+import org.apache.camel.util.CollectionHelper;
+import org.apache.velocity.VelocityContext;
import org.jboss.logging.Logger;
@Path("/velocity")
@@ -112,6 +115,34 @@ public class VelocityResource {
.build();
}
+ @Path("/velocityContext")
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response velocityContext(String msg, @QueryParam("name") String name, @QueryParam("name2") String name2,
+ @QueryParam("item") String item) throws Exception {
+
+ final Exchange ex = producerTemplate.request(
+ "velocity://template/velocityContext.vm?allowTemplateFromHeader=true&allowContextMapAll=true",
+ (Processor) exchange -> {
+ exchange.getIn().setBody("");
+ exchange.getIn().setHeader("name", name2);
+ Map<String, Object> variableMap = new HashMap<>();
+ variableMap.put("headers", CollectionHelper.mapOf("name", name));
+ variableMap.put("body", "Monday");
+ variableMap.put("properties", exchange.getProperties());
+ VelocityContext velocityContext1 = new VelocityContext(variableMap);
+ exchange.getIn().setHeader(VelocityConstants.VELOCITY_CONTEXT, velocityContext1);
+ exchange.setProperty("item", item);
+ });
+
+ return Response
+ .created(new URI("https://camel.apache.org/"))
+ .entity(CollectionHelper.mapOf("headers.name", ex.getMessage().getHeader("name"),
+ "result", ex.getMessage().getBody(String.class)))
+ .build();
+ }
+
@Path("/templateViaHeader")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@@ -136,6 +167,28 @@ public class VelocityResource {
.build();
}
+ @Path("/dynamicTemplate")
+ @POST
+ @Consumes(MediaType.TEXT_PLAIN)
+ @Produces(MediaType.TEXT_PLAIN)
+ public Response dynamicTemplate(String template, @QueryParam("body") String body, @QueryParam("item") String item,
+ @QueryParam("name") String name) throws Exception {
+ Map<String, Object> headers = new HashMap() {
+ {
+ put("item", item);
+ put("name", name);
+ put(VelocityConstants.VELOCITY_RESOURCE_URI, template);
+ }
+ };
+ final String response = producerTemplate.requestBodyAndHeaders("velocity::dummy?allowTemplateFromHeader=true", body,
+ headers,
+ String.class);
+ return Response
+ .created(new URI("https://camel.apache.org/"))
+ .entity(response)
+ .build();
+ }
+
@Path("/withProperties")
@POST
@Consumes(MediaType.TEXT_PLAIN)
diff --git a/integration-tests/velocity/src/main/resources/template/dynamicTemplate.vm b/integration-tests/velocity/src/main/resources/template/dynamicTemplate.vm
new file mode 100644
index 0000000000..516886defe
--- /dev/null
+++ b/integration-tests/velocity/src/main/resources/template/dynamicTemplate.vm
@@ -0,0 +1,17 @@
+#*
+ * 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.
+ *#
+Dear ${headers.name}. You ordered item ${headers.item} on ${body}.
\ No newline at end of file
diff --git a/integration-tests/velocity/src/main/resources/template/velocityContext.vm b/integration-tests/velocity/src/main/resources/template/velocityContext.vm
new file mode 100644
index 0000000000..97221233da
--- /dev/null
+++ b/integration-tests/velocity/src/main/resources/template/velocityContext.vm
@@ -0,0 +1,17 @@
+#*
+ * 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.
+ *#
+Dear ${headers.name}. You ordered item ${properties.item} on ${body}.
\ No newline at end of file
diff --git a/integration-tests/velocity/src/test/java/org/apache/camel/quarkus/component/velocity/it/VelocityTest.java b/integration-tests/velocity/src/test/java/org/apache/camel/quarkus/component/velocity/it/VelocityTest.java
index d425c58abc..3eed5bdc8b 100644
--- a/integration-tests/velocity/src/test/java/org/apache/camel/quarkus/component/velocity/it/VelocityTest.java
+++ b/integration-tests/velocity/src/test/java/org/apache/camel/quarkus/component/velocity/it/VelocityTest.java
@@ -138,6 +138,40 @@ class VelocityTest {
.body(equalTo("\nHi Sheldon from Earth 2"));
}
+ @Test
+ public void testVelocityContext() {
+ Map result = RestAssured.given()
+ .queryParam("name", "Sheldon")
+ .queryParam("name2", "Leonard")
+ .queryParam("item", "Earth 1")
+ .contentType(ContentType.JSON)
+ .body("Hello!")
+ .post("/velocity/velocityContext")
+ .then()
+ .statusCode(201)
+ .extract().as(Map.class);
+
+ assertTrue(result.containsKey("headers.name"));
+ assertEquals("Leonard", result.get("headers.name"));
+ assertTrue(result.containsKey("result"));
+ assertEquals("\nDear Sheldon. You ordered item Earth 1 on Monday.", result.get("result"));
+ }
+
+ @Test
+ public void testDynamicTemplates() {
+ RestAssured.given()
+ .queryParam("body", "Monaday")
+ .queryParam("name", "Sheldon")
+ .queryParam("item", "Camel in Action")
+ .contentType(ContentType.TEXT)
+ .body("template/dynamicTemplate.vm")
+ .post("/velocity/dynamicTemplate")
+ .then()
+ .statusCode(201)
+ .body(equalTo("\n" +
+ "Dear Sheldon. You ordered item Camel in Action on Monaday."));
+ }
+
@Test
public void testContentCacheFalse() throws Exception {
testContentCache(false);