Skip to content
Permalink
Browse files
Improve camel-quarkus-velocity test coverage #3790
  • Loading branch information
JiriOndrusek authored and jamesnetherton committed May 19, 2022
1 parent dc8589d commit 8a159df5b5ef06a136d63ef35272e87bf9f41908
Showing 6 changed files with 149 additions and 0 deletions.
@@ -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
@@ -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 {
}
----
@@ -32,8 +32,11 @@
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 Response bodyAsDomainObject(Person person, @QueryParam("givenName") Strin
.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 Response templateViaHeader(String message, @QueryParam("body") String bod
.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)
@@ -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}.
@@ -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}.
@@ -138,6 +138,40 @@ public void testBodyAsDomainObject() {
.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);

0 comments on commit 8a159df

Please sign in to comment.