You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2020/09/23 10:20:25 UTC

[GitHub] [camel-quarkus] ppalaga commented on a change in pull request #1804: Velocity Support #837

ppalaga commented on a change in pull request #1804:
URL: https://github.com/apache/camel-quarkus/pull/1804#discussion_r493414845



##########
File path: integration-tests/velocity/src/test/java/org/apache/camel/quarkus/component/velocity/it/VelocityTest.java
##########
@@ -0,0 +1,205 @@
+/*
+ * 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.velocity.it;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Map;
+
+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.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@QuarkusTest
+class VelocityTest {
+
+    public static final String OLD_BODY = "old_body";
+    public static final String BODY = "bar";
+    public static final String NEW_BODY = "new_body";
+    public static final String MSG = "\nDear Sheldon\n" +
+            "\n" +
+            "Thanks for the order of Camel in Action.\n" +
+            "\n" +
+            "Regards Apache Camel Riders Bookstore\n" +
+            "PS: Next beer is on me";
+
+    @Test
+    public void testTemplateViaFile() throws IOException {
+        File template = createFile("velocity_test", "Hi ${headers.name}. You have got ${headers.item}. ${body}");
+        RestAssured.given() //

Review comment:
       Couldn't we remove the trailing empty commets?

##########
File path: integration-tests/velocity/src/test/java/org/apache/camel/quarkus/component/velocity/it/VelocityTest.java
##########
@@ -0,0 +1,205 @@
+/*
+ * 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.velocity.it;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Map;
+
+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.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@QuarkusTest
+class VelocityTest {
+
+    public static final String OLD_BODY = "old_body";
+    public static final String BODY = "bar";
+    public static final String NEW_BODY = "new_body";
+    public static final String MSG = "\nDear Sheldon\n" +
+            "\n" +
+            "Thanks for the order of Camel in Action.\n" +
+            "\n" +
+            "Regards Apache Camel Riders Bookstore\n" +
+            "PS: Next beer is on me";
+
+    @Test
+    public void testTemplateViaFile() throws IOException {
+        File template = createFile("velocity_test", "Hi ${headers.name}. You have got ${headers.item}. ${body}");
+        RestAssured.given() //
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "Camel in Action")
+                .queryParam("template", "file:/" + template.getPath())
+                .contentType(ContentType.TEXT)
+                .body("PS: Next beer is on me")
+                .post("/velocity/template") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("Hi Sheldon. You have got Camel in Action. PS: Next beer is on me"));
+    }
+
+    @Test
+    public void testTemplateViaClasspath() {
+        RestAssured.given() //
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "Camel in Action")
+                .queryParam("template", "//template/letter.vm")
+                .contentType(ContentType.TEXT)
+                .body("PS: Next beer is on me")
+                .post("/velocity/template") //
+                .then()
+                .statusCode(201)
+                .body(equalTo(MSG));
+    }
+
+    @Test
+    public void testTemplateViaClasspathWithProperties() {
+        //class loader is forbidden by properties, response should fail
+        RestAssured.given() //
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "Camel in Action")
+                .queryParam("template", "//template/template.vm")
+                .queryParam("propertiesFile", "/template/velocity.properties")
+                .queryParam("expectFailure", "true")
+                .contentType(ContentType.TEXT)
+                .body("PS: Next beer is on me")
+                .post("/velocity/template") //
+                .then()
+                .statusCode(500)
+                .body(containsString("Exception"));
+    }
+
+    @Test
+    public void testTemplateViaHeader() {
+        RestAssured.given() //
+                .queryParam("body", "PS: Next beer is on me.")
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "Camel in Action")
+                .contentType(ContentType.TEXT)
+                .body("Hi ${headers.name}. Thanks for ${headers.item}. ${body}")
+                .post("/velocity/templateViaHeader") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("Hi Sheldon. Thanks for Camel in Action. PS: Next beer is on me."));
+    }
+
+    @Test
+    public void testSupplementalContext() {
+        final String template = "#set( $headers.body = ${body} )\n#set( $headers['in.body'] = $in.body )\n" + BODY;
+        Map result = RestAssured.given() //
+                .queryParam("body", OLD_BODY)
+                .queryParam("supplementalBody", NEW_BODY)
+                .contentType(ContentType.TEXT)
+                .body(template)
+                .post("/velocity/supplementalContext") //
+                .then()
+                .statusCode(200)
+                .extract().as(Map.class);
+
+        assertTrue(result.containsKey("in.body"));
+        assertEquals(OLD_BODY, result.get("in.body"));
+        assertTrue(result.containsKey("result_value"));
+        assertEquals(BODY, result.get("result_value"));
+        assertTrue(result.containsKey("body"));
+        assertEquals(NEW_BODY, result.get("body"));
+    }
+
+    @Test
+    public void testBodyAsDomainObject() {
+        RestAssured.given()
+                .queryParam("name", "Sheldon")
+                .queryParam("country", "Earth 1")
+                .contentType(ContentType.JSON)
+                .body(new Person("Sheldon", "Earth 2"))
+                .post("/velocity/bodyAsDomainObject") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("\nHi Sheldon from Earth 2"));
+    }
+
+    @Test
+    public void testValuesInProperties() {
+        RestAssured.given() //
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "1")
+                .contentType(ContentType.TEXT)
+                .body("Dear ${exchange.properties.name}. You ordered item ${exchange.properties.item}.")
+                .post("/velocity/withProperties") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("Dear Sheldon. You ordered item 1."));
+    }
+
+    @Test
+    public void testContentCacheFalse() throws Exception {
+        testContentCache(false);
+    }
+
+    @Test
+    public void testContentCacheTrue() throws Exception {
+        testContentCache(true);
+    }
+
+    private void testContentCache(boolean useContentCache) throws Exception {
+        File template = createFile("velocity_test", "Hi ${body}");
+
+        RestAssured.given()
+                .queryParam("template", "file:/" + template.getPath())
+                //                .queryParam("loaderCache", "false")

Review comment:
       Can we remove this line?

##########
File path: integration-tests/velocity/src/test/java/org/apache/camel/quarkus/component/velocity/it/VelocityTest.java
##########
@@ -0,0 +1,205 @@
+/*
+ * 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.velocity.it;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Map;
+
+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.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@QuarkusTest
+class VelocityTest {
+
+    public static final String OLD_BODY = "old_body";
+    public static final String BODY = "bar";
+    public static final String NEW_BODY = "new_body";
+    public static final String MSG = "\nDear Sheldon\n" +
+            "\n" +
+            "Thanks for the order of Camel in Action.\n" +
+            "\n" +
+            "Regards Apache Camel Riders Bookstore\n" +
+            "PS: Next beer is on me";
+
+    @Test
+    public void testTemplateViaFile() throws IOException {
+        File template = createFile("velocity_test", "Hi ${headers.name}. You have got ${headers.item}. ${body}");
+        RestAssured.given() //
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "Camel in Action")
+                .queryParam("template", "file:/" + template.getPath())
+                .contentType(ContentType.TEXT)
+                .body("PS: Next beer is on me")
+                .post("/velocity/template") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("Hi Sheldon. You have got Camel in Action. PS: Next beer is on me"));
+    }
+
+    @Test
+    public void testTemplateViaClasspath() {
+        RestAssured.given() //
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "Camel in Action")
+                .queryParam("template", "//template/letter.vm")
+                .contentType(ContentType.TEXT)
+                .body("PS: Next beer is on me")
+                .post("/velocity/template") //
+                .then()
+                .statusCode(201)
+                .body(equalTo(MSG));
+    }
+
+    @Test
+    public void testTemplateViaClasspathWithProperties() {
+        //class loader is forbidden by properties, response should fail
+        RestAssured.given() //
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "Camel in Action")
+                .queryParam("template", "//template/template.vm")
+                .queryParam("propertiesFile", "/template/velocity.properties")
+                .queryParam("expectFailure", "true")
+                .contentType(ContentType.TEXT)
+                .body("PS: Next beer is on me")
+                .post("/velocity/template") //
+                .then()
+                .statusCode(500)
+                .body(containsString("Exception"));
+    }
+
+    @Test
+    public void testTemplateViaHeader() {
+        RestAssured.given() //
+                .queryParam("body", "PS: Next beer is on me.")
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "Camel in Action")
+                .contentType(ContentType.TEXT)
+                .body("Hi ${headers.name}. Thanks for ${headers.item}. ${body}")
+                .post("/velocity/templateViaHeader") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("Hi Sheldon. Thanks for Camel in Action. PS: Next beer is on me."));
+    }
+
+    @Test
+    public void testSupplementalContext() {
+        final String template = "#set( $headers.body = ${body} )\n#set( $headers['in.body'] = $in.body )\n" + BODY;
+        Map result = RestAssured.given() //
+                .queryParam("body", OLD_BODY)
+                .queryParam("supplementalBody", NEW_BODY)
+                .contentType(ContentType.TEXT)
+                .body(template)
+                .post("/velocity/supplementalContext") //
+                .then()
+                .statusCode(200)
+                .extract().as(Map.class);
+
+        assertTrue(result.containsKey("in.body"));
+        assertEquals(OLD_BODY, result.get("in.body"));
+        assertTrue(result.containsKey("result_value"));
+        assertEquals(BODY, result.get("result_value"));
+        assertTrue(result.containsKey("body"));
+        assertEquals(NEW_BODY, result.get("body"));
+    }
+
+    @Test
+    public void testBodyAsDomainObject() {
+        RestAssured.given()
+                .queryParam("name", "Sheldon")
+                .queryParam("country", "Earth 1")
+                .contentType(ContentType.JSON)
+                .body(new Person("Sheldon", "Earth 2"))
+                .post("/velocity/bodyAsDomainObject") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("\nHi Sheldon from Earth 2"));
+    }
+
+    @Test
+    public void testValuesInProperties() {
+        RestAssured.given() //
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "1")
+                .contentType(ContentType.TEXT)
+                .body("Dear ${exchange.properties.name}. You ordered item ${exchange.properties.item}.")
+                .post("/velocity/withProperties") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("Dear Sheldon. You ordered item 1."));
+    }
+
+    @Test
+    public void testContentCacheFalse() throws Exception {
+        testContentCache(false);
+    }
+
+    @Test
+    public void testContentCacheTrue() throws Exception {
+        testContentCache(true);
+    }
+
+    private void testContentCache(boolean useContentCache) throws Exception {
+        File template = createFile("velocity_test", "Hi ${body}");
+
+        RestAssured.given()
+                .queryParam("template", "file:/" + template.getPath())
+                //                .queryParam("loaderCache", "false")
+                .queryParam("contentCache", useContentCache)
+                .contentType(ContentType.TEXT)
+                .body("Sheldon")
+                .post("/velocity/template") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("Hi Sheldon"));
+
+        //override file
+        Files.write(Paths.get(template.getPath()), "Bye ${body}".getBytes());
+
+        RestAssured.given() //
+                .queryParam("template", "file:/" + template.getPath())
+                //                .queryParam("loaderCache", "false")
+                .queryParam("contentCache", useContentCache)
+                .contentType(ContentType.TEXT)
+                .body("Sheldon")
+                .post("/velocity/template") //
+                .then()
+
+                .statusCode(201)
+                .body(equalTo(useContentCache ? "Hi Sheldon" : "Bye Sheldon"));
+    }
+
+    private File createFile(String fileName, String body) throws IOException {
+        File tmpFile = File.createTempFile(fileName, ".vm");
+        FileWriter writer = new FileWriter(tmpFile);
+        writer.write(body);
+        writer.close();

Review comment:
       If you use a writer, it needs to be enclosed in a try-with. You also implicitly use the current machines default encoding, which can be just anything - the test might fail on machines with exotic default encodings. Shorter & solving all named problems: 
   ```suggestion
           Files.write(tmpFile.toPath(), body.getBytes(StandardCharsets.UTF_8);
   ```

##########
File path: integration-tests/velocity/src/test/java/org/apache/camel/quarkus/component/velocity/it/VelocityTest.java
##########
@@ -0,0 +1,205 @@
+/*
+ * 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.velocity.it;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Map;
+
+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.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@QuarkusTest
+class VelocityTest {
+
+    public static final String OLD_BODY = "old_body";
+    public static final String BODY = "bar";
+    public static final String NEW_BODY = "new_body";
+    public static final String MSG = "\nDear Sheldon\n" +
+            "\n" +
+            "Thanks for the order of Camel in Action.\n" +
+            "\n" +
+            "Regards Apache Camel Riders Bookstore\n" +
+            "PS: Next beer is on me";
+
+    @Test
+    public void testTemplateViaFile() throws IOException {
+        File template = createFile("velocity_test", "Hi ${headers.name}. You have got ${headers.item}. ${body}");
+        RestAssured.given() //
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "Camel in Action")
+                .queryParam("template", "file:/" + template.getPath())
+                .contentType(ContentType.TEXT)
+                .body("PS: Next beer is on me")
+                .post("/velocity/template") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("Hi Sheldon. You have got Camel in Action. PS: Next beer is on me"));
+    }
+
+    @Test
+    public void testTemplateViaClasspath() {
+        RestAssured.given() //
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "Camel in Action")
+                .queryParam("template", "//template/letter.vm")
+                .contentType(ContentType.TEXT)
+                .body("PS: Next beer is on me")
+                .post("/velocity/template") //
+                .then()
+                .statusCode(201)
+                .body(equalTo(MSG));
+    }
+
+    @Test
+    public void testTemplateViaClasspathWithProperties() {
+        //class loader is forbidden by properties, response should fail
+        RestAssured.given() //
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "Camel in Action")
+                .queryParam("template", "//template/template.vm")
+                .queryParam("propertiesFile", "/template/velocity.properties")
+                .queryParam("expectFailure", "true")
+                .contentType(ContentType.TEXT)
+                .body("PS: Next beer is on me")
+                .post("/velocity/template") //
+                .then()
+                .statusCode(500)
+                .body(containsString("Exception"));
+    }
+
+    @Test
+    public void testTemplateViaHeader() {
+        RestAssured.given() //
+                .queryParam("body", "PS: Next beer is on me.")
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "Camel in Action")
+                .contentType(ContentType.TEXT)
+                .body("Hi ${headers.name}. Thanks for ${headers.item}. ${body}")
+                .post("/velocity/templateViaHeader") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("Hi Sheldon. Thanks for Camel in Action. PS: Next beer is on me."));
+    }
+
+    @Test
+    public void testSupplementalContext() {
+        final String template = "#set( $headers.body = ${body} )\n#set( $headers['in.body'] = $in.body )\n" + BODY;
+        Map result = RestAssured.given() //
+                .queryParam("body", OLD_BODY)
+                .queryParam("supplementalBody", NEW_BODY)
+                .contentType(ContentType.TEXT)
+                .body(template)
+                .post("/velocity/supplementalContext") //
+                .then()
+                .statusCode(200)
+                .extract().as(Map.class);
+
+        assertTrue(result.containsKey("in.body"));
+        assertEquals(OLD_BODY, result.get("in.body"));
+        assertTrue(result.containsKey("result_value"));
+        assertEquals(BODY, result.get("result_value"));
+        assertTrue(result.containsKey("body"));
+        assertEquals(NEW_BODY, result.get("body"));
+    }
+
+    @Test
+    public void testBodyAsDomainObject() {
+        RestAssured.given()
+                .queryParam("name", "Sheldon")
+                .queryParam("country", "Earth 1")
+                .contentType(ContentType.JSON)
+                .body(new Person("Sheldon", "Earth 2"))
+                .post("/velocity/bodyAsDomainObject") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("\nHi Sheldon from Earth 2"));
+    }
+
+    @Test
+    public void testValuesInProperties() {
+        RestAssured.given() //
+                .queryParam("name", "Sheldon")
+                .queryParam("item", "1")
+                .contentType(ContentType.TEXT)
+                .body("Dear ${exchange.properties.name}. You ordered item ${exchange.properties.item}.")
+                .post("/velocity/withProperties") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("Dear Sheldon. You ordered item 1."));
+    }
+
+    @Test
+    public void testContentCacheFalse() throws Exception {
+        testContentCache(false);
+    }
+
+    @Test
+    public void testContentCacheTrue() throws Exception {
+        testContentCache(true);
+    }
+
+    private void testContentCache(boolean useContentCache) throws Exception {
+        File template = createFile("velocity_test", "Hi ${body}");
+
+        RestAssured.given()
+                .queryParam("template", "file:/" + template.getPath())
+                //                .queryParam("loaderCache", "false")
+                .queryParam("contentCache", useContentCache)
+                .contentType(ContentType.TEXT)
+                .body("Sheldon")
+                .post("/velocity/template") //
+                .then()
+                .statusCode(201)
+                .body(equalTo("Hi Sheldon"));
+
+        //override file
+        Files.write(Paths.get(template.getPath()), "Bye ${body}".getBytes());
+
+        RestAssured.given() //
+                .queryParam("template", "file:/" + template.getPath())
+                //                .queryParam("loaderCache", "false")

Review comment:
       Can we remove this line?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org