This is an automated email from the ASF dual-hosted git repository. ppalaga pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
The following commit(s) were added to refs/heads/master by this push: new c48e2ba Test the Freemarker extension properly, although only in JVM mode for now c48e2ba is described below commit c48e2bace0cd6d2c3494a513de91185f99e63829 Author: Peter Palaga <ppal...@redhat.com> AuthorDate: Wed Nov 25 10:28:12 2020 +0100 Test the Freemarker extension properly, although only in JVM mode for now The tests are adapted from https://github.com/apache/camel-quarkus/pull/835 by @carlosthe19916 --- .../freemarker/it/FreemarkerResource.java | 122 ++++++++++++++++++--- .../quarkus/component/freemarker/it/MyPerson.java} | 36 ++++-- .../src/main/resources/AppleTemplate.ftl | 19 ++++ .../src/main/resources/application.properties | 21 ++++ .../subfolder/templates/BodyAsDomainObject.ftl | 20 ++++ .../apache/camel/component/freemarker/example.ftl | 19 ++++ .../component/freemarker/it/FreemarkerTest.java | 56 +++++++++- 7 files changed, 263 insertions(+), 30 deletions(-) diff --git a/extensions-jvm/freemarker/integration-test/src/main/java/org/apache/camel/quarkus/component/freemarker/it/FreemarkerResource.java b/extensions-jvm/freemarker/integration-test/src/main/java/org/apache/camel/quarkus/component/freemarker/it/FreemarkerResource.java index 845b95f..d24527b 100644 --- a/extensions-jvm/freemarker/integration-test/src/main/java/org/apache/camel/quarkus/component/freemarker/it/FreemarkerResource.java +++ b/extensions-jvm/freemarker/integration-test/src/main/java/org/apache/camel/quarkus/component/freemarker/it/FreemarkerResource.java @@ -16,36 +16,128 @@ */ package org.apache.camel.quarkus.component.freemarker.it; +import java.util.HashMap; +import java.util.Map; + 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 javax.ws.rs.core.Response; -import org.apache.camel.CamelContext; -import org.jboss.logging.Logger; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.freemarker.FreemarkerConstants; @Path("/freemarker") @ApplicationScoped public class FreemarkerResource { - private static final Logger LOG = Logger.getLogger(FreemarkerResource.class); - - private static final String COMPONENT_FREEMARKER = "freemarker"; @Inject - CamelContext context; + ProducerTemplate producerTemplate; + + @Path("/freemarkerLetter") + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.TEXT_PLAIN) + public String freemarkerLetter() throws Exception { + return producerTemplate.request( + "freemarker:org/apache/camel/component/freemarker/example.ftl?allowContextMapAll=true", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody("Monday"); + exchange.getIn().setHeader("name", "Christian"); + exchange.setProperty("item", "7"); + } + }) + .getMessage().getBody(String.class); + } + + @Path("/freemarkerDataModel") + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.TEXT_PLAIN) + public String freemarkerDataModel() throws Exception { + return producerTemplate.request( + "freemarker:org/apache/camel/component/freemarker/example.ftl?allowTemplateFromHeader=true&allowContextMapAll=true", + new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody(""); + exchange.getIn().setHeader("name", "Christian"); + Map<String, Object> variableMap = new HashMap<>(); + Map<String, Object> headersMap = new HashMap<>(); + headersMap.put("name", "Willem"); + variableMap.put("headers", headersMap); + variableMap.put("body", "Monday"); + variableMap.put("exchange", exchange); + exchange.getIn().setHeader(FreemarkerConstants.FREEMARKER_DATA_MODEL, variableMap); + exchange.setProperty("item", "7"); + } + }) + .getMessage().getBody(String.class); + } + + @Path("/valuesInProperties") + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.TEXT_PLAIN) + public String valuesInProperties() throws Exception { + return producerTemplate.request( + "freemarker:dummy?allowTemplateFromHeader=true&allowContextMapAll=true", + exchange1 -> { + exchange1.getIn().setHeader( + FreemarkerConstants.FREEMARKER_TEMPLATE, + "Dear ${exchange.properties.name}. You ordered item ${exchange.properties.item}."); + exchange1.setProperty("name", "Christian"); + exchange1.setProperty("item", "7"); + }) + .getMessage().getBody(String.class); + } + + @Path("/templateInHeader") + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.TEXT_PLAIN) + public String templateInHeader() throws Exception { + final Map<String, Object> headers = new HashMap<>(); + headers.put(FreemarkerConstants.FREEMARKER_TEMPLATE, "<hello>${headers.cheese}</hello>"); + headers.put("cheese", "foo"); + return producerTemplate.requestBodyAndHeaders("freemarker://dummy?allowContextMapAll=true", headers, null, + String.class); + } - @Path("/load/component/freemarker") + @Path("/bodyAsDomainObject/{firstName}/{lastName}") @GET + @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.TEXT_PLAIN) - public Response loadComponentFreemarker() throws Exception { - /* This is an autogenerated test */ - if (context.getComponent(COMPONENT_FREEMARKER) != null) { - return Response.ok().build(); - } - LOG.warnf("Could not load [%s] from the Camel context", COMPONENT_FREEMARKER); - return Response.status(500, COMPONENT_FREEMARKER + " could not be loaded from the Camel context").build(); + public String bodyAsDomainObject(@PathParam("firstName") String firstName, @PathParam("lastName") String lastName) + throws Exception { + return producerTemplate.requestBody("freemarker:folder/subfolder/templates/BodyAsDomainObject.ftl", + new MyPerson(firstName, lastName), + String.class); } + + @Path("/apple") + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.TEXT_PLAIN) + public String apple() throws Exception { + return producerTemplate + .request("freemarker:AppleTemplate.ftl?allowContextMapAll=true", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody("Orange"); + exchange.getIn().setHeader("color", "orange"); + exchange.setProperty("price", "7"); + } + }) + .getMessage().getBody(String.class); + } + } diff --git a/extensions-jvm/freemarker/integration-test/src/test/java/org/apache/camel/quarkus/component/freemarker/it/FreemarkerTest.java b/extensions-jvm/freemarker/integration-test/src/main/java/org/apache/camel/quarkus/component/freemarker/it/MyPerson.java similarity index 55% copy from extensions-jvm/freemarker/integration-test/src/test/java/org/apache/camel/quarkus/component/freemarker/it/FreemarkerTest.java copy to extensions-jvm/freemarker/integration-test/src/main/java/org/apache/camel/quarkus/component/freemarker/it/MyPerson.java index 58fbdc5..1cf3731 100644 --- a/extensions-jvm/freemarker/integration-test/src/test/java/org/apache/camel/quarkus/component/freemarker/it/FreemarkerTest.java +++ b/extensions-jvm/freemarker/integration-test/src/main/java/org/apache/camel/quarkus/component/freemarker/it/MyPerson.java @@ -16,19 +16,33 @@ */ package org.apache.camel.quarkus.component.freemarker.it; -import io.quarkus.test.junit.QuarkusTest; -import io.restassured.RestAssured; -import org.junit.jupiter.api.Test; +import io.quarkus.runtime.annotations.RegisterForReflection; -@QuarkusTest -class FreemarkerTest { +@RegisterForReflection +public class MyPerson { + public MyPerson(String givenName, String familyName) { + this.givenName = givenName; + this.familyName = familyName; + } + + private final String givenName; + private final String familyName; - @Test - public void loadComponentFreemarker() { - /* A simple autogenerated test */ - RestAssured.get("/freemarker/load/component/freemarker") - .then() - .statusCode(200); + public String getGivenName() { + return givenName; } + public String getFamilyName() { + return familyName; + } + + @Override + public String toString() { + return "MyPerson{" + + "givenName='" + + givenName + '\'' + + ", familyName='" + + familyName + '\'' + + '}'; + } } diff --git a/extensions-jvm/freemarker/integration-test/src/main/resources/AppleTemplate.ftl b/extensions-jvm/freemarker/integration-test/src/main/resources/AppleTemplate.ftl new file mode 100644 index 0000000..15c03d3 --- /dev/null +++ b/extensions-jvm/freemarker/integration-test/src/main/resources/AppleTemplate.ftl @@ -0,0 +1,19 @@ +<#-- + + 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. + +--> +I am a ${body} whose color is ${headers.color}. I cost ${exchange.properties.price}. \ No newline at end of file diff --git a/extensions-jvm/freemarker/integration-test/src/main/resources/application.properties b/extensions-jvm/freemarker/integration-test/src/main/resources/application.properties new file mode 100644 index 0000000..5e200d3 --- /dev/null +++ b/extensions-jvm/freemarker/integration-test/src/main/resources/application.properties @@ -0,0 +1,21 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +# +# Quarkus +# +quarkus.freemarker.resource-paths=classpath: +quarkus.freemarker.default-encoding=UTF-8 diff --git a/extensions-jvm/freemarker/integration-test/src/main/resources/folder/subfolder/templates/BodyAsDomainObject.ftl b/extensions-jvm/freemarker/integration-test/src/main/resources/folder/subfolder/templates/BodyAsDomainObject.ftl new file mode 100644 index 0000000..7648c82 --- /dev/null +++ b/extensions-jvm/freemarker/integration-test/src/main/resources/folder/subfolder/templates/BodyAsDomainObject.ftl @@ -0,0 +1,20 @@ +<#-- + + 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. + +--> +Hi ${body.givenName} how are you? Its a nice day. +Give my regards to the family ${body.familyName}. \ No newline at end of file diff --git a/extensions-jvm/freemarker/integration-test/src/main/resources/org/apache/camel/component/freemarker/example.ftl b/extensions-jvm/freemarker/integration-test/src/main/resources/org/apache/camel/component/freemarker/example.ftl new file mode 100644 index 0000000..b7e6e72 --- /dev/null +++ b/extensions-jvm/freemarker/integration-test/src/main/resources/org/apache/camel/component/freemarker/example.ftl @@ -0,0 +1,19 @@ +<#-- + + 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 ${exchange.properties.item} on ${body}. \ No newline at end of file diff --git a/extensions-jvm/freemarker/integration-test/src/test/java/org/apache/camel/quarkus/component/freemarker/it/FreemarkerTest.java b/extensions-jvm/freemarker/integration-test/src/test/java/org/apache/camel/quarkus/component/freemarker/it/FreemarkerTest.java index 58fbdc5..4cb258f 100644 --- a/extensions-jvm/freemarker/integration-test/src/test/java/org/apache/camel/quarkus/component/freemarker/it/FreemarkerTest.java +++ b/extensions-jvm/freemarker/integration-test/src/test/java/org/apache/camel/quarkus/component/freemarker/it/FreemarkerTest.java @@ -18,17 +18,65 @@ package org.apache.camel.quarkus.component.freemarker.it; import io.quarkus.test.junit.QuarkusTest; import io.restassured.RestAssured; +import io.restassured.http.ContentType; import org.junit.jupiter.api.Test; +import static org.hamcrest.Matchers.equalTo; + @QuarkusTest class FreemarkerTest { @Test - public void loadComponentFreemarker() { - /* A simple autogenerated test */ - RestAssured.get("/freemarker/load/component/freemarker") + public void freemarkerLetter() { + RestAssured.given() + .contentType(ContentType.JSON) + .accept(ContentType.TEXT) + .post("/freemarker/freemarkerLetter") + .then() + .statusCode(200) + .body(equalTo("Dear Christian. You ordered item 7 on Monday.")); + } + + @Test + public void freemarkerDataModel() { + RestAssured.given() + .contentType(ContentType.JSON) + .accept(ContentType.TEXT) + .post("/freemarker/freemarkerDataModel") + .then() + .statusCode(200) + .body(equalTo("Dear Willem. You ordered item 7 on Monday.")); + } + + @Test + public void valuesInProperties() { + RestAssured.given() + .contentType(ContentType.JSON) + .accept(ContentType.TEXT) + .post("/freemarker/valuesInProperties") + .then() + .statusCode(200) + .body(equalTo("Dear Christian. You ordered item 7.")); + } + + @Test + public void bodyAsDomainObject() { + RestAssured.given() + .accept(ContentType.TEXT) + .get("/freemarker/bodyAsDomainObject/{firstName}/{lastName}", "Claus", "Ibsen") .then() - .statusCode(200); + .statusCode(200) + .body(equalTo("Hi Claus how are you? Its a nice day.\nGive my regards to the family Ibsen.")); } + @Test + public void apple() { + RestAssured.given() + .contentType(ContentType.JSON) + .accept(ContentType.TEXT) + .post("/freemarker/apple") + .then() + .statusCode(200) + .body(equalTo("I am a Orange whose color is orange. I cost 7.")); + } }