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 2024/03/05 15:27:56 UTC

(camel-quarkus) 04/04: Add additional Servlet test coverage

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

commit f7abb3e9827cd03da6f83bb403a0855ba494cd11
Author: James Netherton <ja...@gmail.com>
AuthorDate: Tue Mar 5 09:48:35 2024 +0000

    Add additional Servlet test coverage
---
 .../quarkus/component/servlet/CamelRoute.java      | 26 +++++-
 .../quarkus/component/servlet/CustomException.java | 28 +++++++
 .../src/main/resources/application.properties      |  6 ++
 .../component/servlet/CamelServletTest.java        | 97 ++++++++++++++++++++--
 4 files changed, 148 insertions(+), 9 deletions(-)

diff --git a/integration-tests/servlet/src/main/java/org/apache/camel/quarkus/component/servlet/CamelRoute.java b/integration-tests/servlet/src/main/java/org/apache/camel/quarkus/component/servlet/CamelRoute.java
index 6c8c117c35..2ca381f295 100644
--- a/integration-tests/servlet/src/main/java/org/apache/camel/quarkus/component/servlet/CamelRoute.java
+++ b/integration-tests/servlet/src/main/java/org/apache/camel/quarkus/component/servlet/CamelRoute.java
@@ -44,10 +44,34 @@ public class CamelRoute extends RouteBuilder {
                 .to("direct:echoMethodPath")
 
                 .post("/rest-post")
+                .to("direct:echoMethodPath")
+
+                .put("/rest-put")
+                .to("direct:echoMethodPath")
+
+                .patch("/rest-patch")
+                .to("direct:echoMethodPath")
+
+                .delete("/rest-delete")
+                .to("direct:echoMethodPath")
+
+                .head("/rest-head")
                 .to("direct:echoMethodPath");
 
         from("servlet://hello?matchOnUriPrefix=true")
-                .setBody(constant("GET: /hello"));
+                .to("direct:echoMethodPath");
+
+        from("servlet://options?servletName=options-method-servlet&optionsEnabled=true")
+                .to("direct:echoMethodPath");
+
+        from("servlet://trace?servletName=trace-method-servlet&traceEnabled=true")
+                .to("direct:echoMethodPath");
+
+        from("servlet://transfer/exception?transferException=true&muteException=false")
+                .throwException(new CustomException());
+
+        from("servlet://params")
+                .setBody().simple("${header.prefix} ${header.suffix}");
 
         from("servlet://configuration")
                 .process(servletConfigInfoProcessor);
diff --git a/integration-tests/servlet/src/main/java/org/apache/camel/quarkus/component/servlet/CustomException.java b/integration-tests/servlet/src/main/java/org/apache/camel/quarkus/component/servlet/CustomException.java
new file mode 100644
index 0000000000..33fb3db78d
--- /dev/null
+++ b/integration-tests/servlet/src/main/java/org/apache/camel/quarkus/component/servlet/CustomException.java
@@ -0,0 +1,28 @@
+/*
+ * 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.servlet;
+
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+@RegisterForReflection(serialization = true)
+public class CustomException extends Exception {
+    public static final String MESSAGE = "Something went wrong";
+
+    public CustomException() {
+        super(MESSAGE);
+    }
+}
diff --git a/integration-tests/servlet/src/main/resources/application.properties b/integration-tests/servlet/src/main/resources/application.properties
index 06920af73f..76be5f65b3 100644
--- a/integration-tests/servlet/src/main/resources/application.properties
+++ b/integration-tests/servlet/src/main/resources/application.properties
@@ -15,6 +15,9 @@
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
 
+# For transferException
+quarkus.camel.native.reflection.serialization-enabled=true
+
 # Default servlet configuration
 quarkus.camel.servlet.url-patterns=/folder-1/*,/folder-2/*,/debug/*
 
@@ -51,3 +54,6 @@ quarkus.camel.servlet.custom-executor-servlet.url-patterns=/custom-executor/*
 quarkus.camel.servlet.custom-executor-servlet.async=true
 quarkus.camel.servlet.custom-executor-servlet.executor-ref=customServletExecutor
 
+# Servlet to allow OPTIONS & TRACE
+quarkus.camel.servlet.options-method-servlet.url-patterns=/method-options/*
+quarkus.camel.servlet.trace-method-servlet.url-patterns=/method-trace/*
diff --git a/integration-tests/servlet/src/test/java/org/apache/camel/quarkus/component/servlet/CamelServletTest.java b/integration-tests/servlet/src/test/java/org/apache/camel/quarkus/component/servlet/CamelServletTest.java
index 73bdd78dbe..b87966872b 100644
--- a/integration-tests/servlet/src/test/java/org/apache/camel/quarkus/component/servlet/CamelServletTest.java
+++ b/integration-tests/servlet/src/test/java/org/apache/camel/quarkus/component/servlet/CamelServletTest.java
@@ -16,11 +16,18 @@
  */
 package org.apache.camel.quarkus.component.servlet;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
+import org.apache.camel.CamelContext;
+import org.apache.camel.http.common.HttpHelper;
+import org.apache.camel.impl.DefaultCamelContext;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 import static org.hamcrest.Matchers.anEmptyMap;
 import static org.hamcrest.Matchers.is;
@@ -28,6 +35,9 @@ import static org.hamcrest.Matchers.nullValue;
 import static org.hamcrest.Matchers.oneOf;
 import static org.hamcrest.Matchers.startsWith;
 import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 @QuarkusTest
 public class CamelServletTest {
@@ -42,22 +52,72 @@ public class CamelServletTest {
                         "loadOnStartup", nullValue());
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = { "GET", "POST", "PUT", "PATCH", "DELETE", "HEAD" })
+    public void multiplePaths(String method) {
+        String lowercaseMethod = method.toLowerCase();
+        String restResponse = method.equals("HEAD") ? "" : method + ": /rest-" + lowercaseMethod;
+        RestAssured.given()
+                .request(method, "/folder-1/rest-" + lowercaseMethod)
+                .then()
+                .statusCode(200)
+                .body(equalTo(restResponse));
+
+        RestAssured.given()
+                .request(method, "/folder-2/rest-" + lowercaseMethod)
+                .then()
+                .statusCode(200)
+                .body(equalTo(restResponse));
+
+        String helloResponse = method.equals("HEAD") ? "" : method + ": /hello";
+        RestAssured.given()
+                .request(method, "/folder-1/hello")
+                .then()
+                .statusCode(200)
+                .body(equalTo(helloResponse));
+
+        RestAssured.given()
+                .request(method, "/folder-2/hello")
+                .then()
+                .statusCode(200)
+                .body(equalTo(helloResponse));
+    }
+
     @Test
-    public void multiplePaths() {
-        RestAssured.get("/folder-1/rest-get").then().body(equalTo("GET: /rest-get"));
-        RestAssured.get("/folder-2/rest-get").then().body(equalTo("GET: /rest-get"));
-        RestAssured.post("/folder-1/rest-post").then().body(equalTo("POST: /rest-post"));
-        RestAssured.post("/folder-2/rest-post").then().body(equalTo("POST: /rest-post"));
-        RestAssured.get("/folder-1/hello").then().body(equalTo("GET: /hello"));
-        RestAssured.get("/folder-2/hello").then().body(equalTo("GET: /hello"));
+    public void options() {
+        RestAssured.given()
+                .request("OPTIONS", "/method-options/options")
+                .then()
+                .statusCode(200)
+                .body(equalTo("OPTIONS: /options"));
+    }
+
+    @Test
+    public void trace() {
+        RestAssured.given()
+                .request("TRACE", "/method-trace/trace")
+                .then()
+                .statusCode(200)
+                .body(equalTo("TRACE: /trace"));
+    }
+
+    @Test
+    public void requestParameters() {
+        RestAssured.given()
+                .formParam("prefix", "Hello")
+                .queryParam("suffix", "World")
+                .post("/folder-1/params")
+                .then()
+                .statusCode(200)
+                .body(equalTo("Hello World"));
     }
 
     @Test
     public void namedWithServletClass() {
         RestAssured.get("/my-custom-folder/custom")
                 .then()
+                .statusCode(200)
                 .body(equalTo("GET: /custom"))
-                .and()
                 .header("x-servlet-class-name", CustomServlet.class.getName());
     }
 
@@ -142,4 +202,25 @@ public class CamelServletTest {
                         "initParams.async", equalTo("true"),
                         "loadOnStartup", nullValue());
     }
+
+    @Test
+    public void transferException() throws IOException, ClassNotFoundException {
+        InputStream response = RestAssured.given()
+                .get("/folder-1/transfer/exception")
+                .then()
+                .statusCode(500)
+                .contentType("application/x-java-serialized-object")
+                .extract()
+                .body()
+                .asInputStream();
+
+        // The CamelContext instance is only needed to ensure the correct ClassLoader is used for deserialization
+        CamelContext context = new DefaultCamelContext();
+        context.setApplicationContextClassLoader(Thread.currentThread().getContextClassLoader());
+        Object exception = HttpHelper.deserializeJavaObjectFromStream(response, context);
+        assertNotNull(exception);
+
+        CustomException cause = assertInstanceOf(CustomException.class, exception);
+        assertEquals(CustomException.MESSAGE, cause.getMessage());
+    }
 }