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 0ac2afc  Expand REST test coverage
0ac2afc is described below

commit 0ac2afc97000b63c35a5bb370f2a1c190cd5d268
Author: James Netherton <[email protected]>
AuthorDate: Thu Apr 29 10:28:00 2021 +0100

    Expand REST test coverage
    
    * Usage of URI templates
    * RestBindingMode.json
    * CORS
    * Enabling client request validation
    * REST producer
    
    Fixes #2529
---
 integration-tests/rest/pom.xml                     | 51 +++++++++++
 .../camel/quarkus/component/rest/it/Person.java    | 68 +++++++++++++++
 .../quarkus/component/rest/it/RestResource.java    | 19 +++++
 .../quarkus/component/rest/it/RestRoutes.java      | 40 ++++++++-
 .../camel/quarkus/component/rest/it/RestTest.java  | 98 +++++++++++++++++++++-
 5 files changed, 269 insertions(+), 7 deletions(-)

diff --git a/integration-tests/rest/pom.xml b/integration-tests/rest/pom.xml
index def7c73..9c52dcb 100644
--- a/integration-tests/rest/pom.xml
+++ b/integration-tests/rest/pom.xml
@@ -32,6 +32,18 @@
     <dependencies>
         <dependency>
             <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-bean</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-http</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-jackson</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
             <artifactId>camel-quarkus-rest</artifactId>
         </dependency>
         <dependency>
@@ -54,6 +66,45 @@
         <!-- The following dependencies guarantee that this module is built 
after them. You can update them by running `mvn process-resources -Pformat -N` 
from the source tree root directory -->
         <dependency>
             <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-bean-deployment</artifactId>
+            <version>${project.version}</version>
+            <type>pom</type>
+            <scope>test</scope>
+            <exclusions>
+                <exclusion>
+                    <groupId>*</groupId>
+                    <artifactId>*</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-http-deployment</artifactId>
+            <version>${project.version}</version>
+            <type>pom</type>
+            <scope>test</scope>
+            <exclusions>
+                <exclusion>
+                    <groupId>*</groupId>
+                    <artifactId>*</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-jackson-deployment</artifactId>
+            <version>${project.version}</version>
+            <type>pom</type>
+            <scope>test</scope>
+            <exclusions>
+                <exclusion>
+                    <groupId>*</groupId>
+                    <artifactId>*</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
             <artifactId>camel-quarkus-rest-deployment</artifactId>
             <version>${project.version}</version>
             <type>pom</type>
diff --git 
a/integration-tests/rest/src/main/java/org/apache/camel/quarkus/component/rest/it/Person.java
 
b/integration-tests/rest/src/main/java/org/apache/camel/quarkus/component/rest/it/Person.java
new file mode 100644
index 0000000..7fa4ee4
--- /dev/null
+++ 
b/integration-tests/rest/src/main/java/org/apache/camel/quarkus/component/rest/it/Person.java
@@ -0,0 +1,68 @@
+/*
+ * 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.rest.it;
+
+import java.util.Objects;
+
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+@RegisterForReflection(fields = false)
+public class Person {
+
+    private String firstName;
+    private String lastName;
+    private int age;
+
+    public String getFirstName() {
+        return firstName;
+    }
+
+    public void setFirstName(String firstName) {
+        this.firstName = firstName;
+    }
+
+    public String getLastName() {
+        return lastName;
+    }
+
+    public void setLastName(String lastName) {
+        this.lastName = lastName;
+    }
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o)
+            return true;
+        if (o == null || getClass() != o.getClass())
+            return false;
+        Person person = (Person) o;
+        return age == person.age && Objects.equals(firstName, 
person.firstName) && Objects.equals(lastName, person.lastName);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(firstName, lastName, age);
+    }
+}
diff --git 
a/integration-tests/rest/src/main/java/org/apache/camel/quarkus/component/rest/it/RestResource.java
 
b/integration-tests/rest/src/main/java/org/apache/camel/quarkus/component/rest/it/RestResource.java
index b0e8f05..751c8e8 100644
--- 
a/integration-tests/rest/src/main/java/org/apache/camel/quarkus/component/rest/it/RestResource.java
+++ 
b/integration-tests/rest/src/main/java/org/apache/camel/quarkus/component/rest/it/RestResource.java
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.quarkus.component.rest.it;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
 import javax.json.Json;
@@ -23,10 +26,12 @@ import javax.json.JsonObject;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.ProducerTemplate;
 
 @Path("/rest")
 @ApplicationScoped
@@ -34,6 +39,9 @@ public class RestResource {
     @Inject
     CamelContext camelContext;
 
+    @Inject
+    ProducerTemplate producerTemplate;
+
     @Path("/inspect/configuration")
     @GET
     @Produces(MediaType.APPLICATION_JSON)
@@ -49,4 +57,15 @@ public class RestResource {
     public boolean lightweight() {
         return camelContext.adapt(ExtendedCamelContext.class).isLightweight();
     }
+
+    @Path("/invoke/route")
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public String restProducer(@QueryParam("port") int port) {
+        Map<String, Object> headers = new HashMap<>();
+        headers.put("messageStart", "Hello");
+        headers.put("messageEnd", "Invoked");
+        return producerTemplate.requestBodyAndHeaders(
+                
"rest:get:/rest/template/{messageStart}/{messageEnd}?host=localhost:" + port, 
null, headers, String.class);
+    }
 }
diff --git 
a/integration-tests/rest/src/main/java/org/apache/camel/quarkus/component/rest/it/RestRoutes.java
 
b/integration-tests/rest/src/main/java/org/apache/camel/quarkus/component/rest/it/RestRoutes.java
index f001379..4825053 100644
--- 
a/integration-tests/rest/src/main/java/org/apache/camel/quarkus/component/rest/it/RestRoutes.java
+++ 
b/integration-tests/rest/src/main/java/org/apache/camel/quarkus/component/rest/it/RestRoutes.java
@@ -16,23 +16,57 @@
  */
 package org.apache.camel.quarkus.component.rest.it;
 
+import javax.ws.rs.core.MediaType;
+
+import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.rest.RestBindingMode;
+import org.apache.camel.model.rest.RestParamType;
 
 public class RestRoutes extends RouteBuilder {
 
     @Override
     public void configure() {
-        rest()
+        restConfiguration()
+                .enableCORS(true)
+                .corsAllowCredentials(true)
+                .corsHeaderProperty("Access-Control-Allow-Methods", "GET, 
POST");
 
-                .get("/rest/get")
+        rest("/rest")
+                .get("/get")
                 .route()
                 .setBody(constant("GET: /rest/get"))
                 .endRest()
 
-                .post("/rest/post")
+                .post("/post")
                 .consumes("text/plain").produces("text/plain")
                 .route()
                 .setBody(constant("POST: /rest/post"))
+                .endRest()
+
+                .post("/validation")
+                .clientRequestValidation(true)
+                
.param().name("messageStart").type(RestParamType.query).required(true).endParam()
+                // https://issues.apache.org/jira/browse/CAMEL-16560
+                // 
.param().name("messageEnd").type(RestParamType.body).required(true).endParam()
+                
.param().name("messageEnd").type(RestParamType.header).required(true).endParam()
+                
.param().name("unused").type(RestParamType.formData).required(false).endParam()
+                .route()
+                .setBody(simple("${header.messageStart} ${header.messageEnd}"))
+                .endRest()
+
+                .get("/template/{messageStart}/{messageEnd}")
+                .route()
+                .setBody(simple("${header.messageStart} ${header.messageEnd}"))
+                .endRest()
+
+                .post("/pojo/binding")
+                .bindingMode(RestBindingMode.json)
+                .type(Person.class)
+                .produces(MediaType.TEXT_PLAIN)
+                .route()
+                .setBody(simple("Name: ${body.firstName} ${body.lastName}, 
Age: ${body.age}"))
+                .setHeader(Exchange.CONTENT_TYPE, constant("text/plain"))
                 .endRest();
     }
 }
diff --git 
a/integration-tests/rest/src/test/java/org/apache/camel/quarkus/component/rest/it/RestTest.java
 
b/integration-tests/rest/src/test/java/org/apache/camel/quarkus/component/rest/it/RestTest.java
index f149919..9bdd587 100644
--- 
a/integration-tests/rest/src/test/java/org/apache/camel/quarkus/component/rest/it/RestTest.java
+++ 
b/integration-tests/rest/src/test/java/org/apache/camel/quarkus/component/rest/it/RestTest.java
@@ -18,14 +18,17 @@ package org.apache.camel.quarkus.component.rest.it;
 
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
+import io.restassured.http.ContentType;
 import org.apache.camel.component.platform.http.PlatformHttpConstants;
 import org.junit.jupiter.api.Test;
 
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.matchesPattern;
 
 @QuarkusTest
 class RestTest {
+
     @Test
     public void inspectConfiguration() {
         RestAssured.when()
@@ -36,13 +39,100 @@ class RestTest {
     }
 
     @Test
-    public void rest() throws Throwable {
+    public void rest() {
         RestAssured.get("/rest/get")
-                .then().body(equalTo("GET: /rest/get"));
+                .then()
+                .header("Access-Control-Allow-Headers", 
matchesPattern(".*Access-Control.*"))
+                .header("Access-Control-Allow-Methods", matchesPattern("GET, 
POST"))
+                .header("Access-Control-Allow-Credentials", equalTo("true"))
+                .body(equalTo("GET: /rest/get"));
+
         RestAssured.given()
-                .contentType("text/plain")
+                .contentType(ContentType.TEXT)
                 .post("/rest/post")
-                .then().body(equalTo("POST: /rest/post"));
+                .then()
+                .statusCode(200)
+                .body(equalTo("POST: /rest/post"));
+    }
+
+    @Test
+    public void pathTemplate() {
+        RestAssured.given()
+                .contentType(ContentType.TEXT)
+                .get("/rest/template/Hello/World")
+                .then()
+                .statusCode(200)
+                .body(equalTo("Hello World"));
+    }
+
+    @Test
+    public void requestValidation() {
+        RestAssured.given()
+                .contentType(ContentType.TEXT)
+                .header("messageEnd", "World")
+                .post("/rest/validation")
+                .then()
+                .statusCode(400)
+                .body(equalTo("Some of the required query parameters are 
missing."));
+
+        // TODO: Enable this - 
https://issues.apache.org/jira/browse/CAMEL-16560
+        //        RestAssured.given()
+        //                .contentType(ContentType.TEXT)
+        //                .queryParam("messageStart", "Hello")
+        //                .post("/rest/validation")
+        //                .then()
+        //                .statusCode(400)
+        //                .body(equalTo("The request body is missing."));
+        //
+
+        RestAssured.given()
+                .contentType(ContentType.TEXT)
+                .queryParam("messageStart", "Hello")
+                .post("/rest/validation")
+                .then()
+                .statusCode(400)
+                .body(equalTo("Some of the required HTTP headers are 
missing."));
+
+        RestAssured.given()
+                .contentType(ContentType.TEXT)
+                .queryParam("messageStart", "Hello")
+                .header("messageEnd", "World")
+                .post("/rest/validation")
+                .then()
+                .statusCode(200)
+                .body(equalTo("Hello World"));
+    }
+
+    @Test
+    public void jsonBinding() {
+        Person person = new Person();
+        person.setFirstName("John");
+        person.setLastName("Doe");
+        person.setAge(64);
+
+        String result = String.format(
+                "Name: %s %s, Age: %d",
+                person.getFirstName(),
+                person.getLastName(),
+                person.getAge());
+
+        RestAssured.given()
+                .contentType(ContentType.JSON)
+                .body(person)
+                .post("/rest/pojo/binding")
+                .then()
+                .statusCode(200)
+                .body(equalTo(result));
+    }
+
+    @Test
+    public void testRestProducer() {
+        RestAssured.given()
+                .queryParam("port", RestAssured.port)
+                .get("/rest/invoke/route")
+                .then()
+                .statusCode(200)
+                .body(equalTo("Hello Invoked"));
     }
 
     @Test

Reply via email to