From 232ff0eda993fe140945d83bc0ebaac77fb358ee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= <loikeseke@gmail.com>
Date: Wed, 31 Aug 2022 11:33:47 +0200
Subject: [PATCH] Add Google Cloud Functions integration tests

---
 .../funqy-google-cloud-functions/pom.xml      | 11 +++-
 .../src/main/resources/application.properties |  6 +-
 .../test/GreetingFunctionsCloudEventTest.java | 63 +++++++++++++++++++
 .../test/GreetingFunctionsPubsubTest.java     | 45 +++++++++++++
 .../test/GreetingFunctionsStorageTest.java    | 34 ++++++++++
 .../google-cloud-functions/pom.xml            | 12 ++++
 .../src/main/resources/application.properties |  6 +-
 .../BackgroundFunctionStorageTestCase.java    | 45 +++++++++++++
 .../test/CloudEventStorageTestCase.java       | 62 ++++++++++++++++++
 .../function/test/HttpFunctionTestCase.java   | 34 ++++++++++
 .../RawBackgroundFunctionPubSubTestCase.java  | 45 +++++++++++++
 11 files changed, 355 insertions(+), 8 deletions(-)
 create mode 100644 integration-tests/funqy-google-cloud-functions/src/test/java/io/quarkus/funqy/gcp/functions/test/GreetingFunctionsCloudEventTest.java
 create mode 100644 integration-tests/funqy-google-cloud-functions/src/test/java/io/quarkus/funqy/gcp/functions/test/GreetingFunctionsPubsubTest.java
 create mode 100644 integration-tests/funqy-google-cloud-functions/src/test/java/io/quarkus/funqy/gcp/functions/test/GreetingFunctionsStorageTest.java
 create mode 100644 integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/BackgroundFunctionStorageTestCase.java
 create mode 100644 integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/CloudEventStorageTestCase.java
 create mode 100644 integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/HttpFunctionTestCase.java
 create mode 100644 integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/RawBackgroundFunctionPubSubTestCase.java

diff --git a/integration-tests/funqy-google-cloud-functions/pom.xml b/integration-tests/funqy-google-cloud-functions/pom.xml
index 853e1fd395b95..2fbef78aa7d3a 100644
--- a/integration-tests/funqy-google-cloud-functions/pom.xml
+++ b/integration-tests/funqy-google-cloud-functions/pom.xml
@@ -17,15 +17,22 @@
             <groupId>io.quarkus</groupId>
             <artifactId>quarkus-funqy-google-cloud-functions</artifactId>
         </dependency>
+
         <!-- test dependencies -->
+        <dependency>
+            <groupId>io.rest-assured</groupId>
+            <artifactId>rest-assured</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>io.quarkus</groupId>
             <artifactId>quarkus-junit5</artifactId>
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>io.quarkus</groupId>
-            <artifactId>quarkus-junit5-internal</artifactId>
+            <groupId>com.google.cloud.functions.invoker</groupId>
+            <artifactId>java-function-invoker</artifactId>
+            <version>${gcf-invoker.version}</version>
             <scope>test</scope>
         </dependency>
 
diff --git a/integration-tests/funqy-google-cloud-functions/src/main/resources/application.properties b/integration-tests/funqy-google-cloud-functions/src/main/resources/application.properties
index 59d042700da56..b00e1aa57fb73 100644
--- a/integration-tests/funqy-google-cloud-functions/src/main/resources/application.properties
+++ b/integration-tests/funqy-google-cloud-functions/src/main/resources/application.properties
@@ -1,3 +1,3 @@
-#quarkus.funqy.export=helloGCSWorld
-#quarkus.funqy.export=helloPubSubWorld
-quarkus.funqy.export=helloCloudEvent
+quarkus.funqy.export=helloGCSWorld
+%pubsub.quarkus.funqy.export=helloPubSubWorld
+%cloud-event.quarkus.funqy.export=helloCloudEvent
diff --git a/integration-tests/funqy-google-cloud-functions/src/test/java/io/quarkus/funqy/gcp/functions/test/GreetingFunctionsCloudEventTest.java b/integration-tests/funqy-google-cloud-functions/src/test/java/io/quarkus/funqy/gcp/functions/test/GreetingFunctionsCloudEventTest.java
new file mode 100644
index 0000000000000..4606c2e0296b9
--- /dev/null
+++ b/integration-tests/funqy-google-cloud-functions/src/test/java/io/quarkus/funqy/gcp/functions/test/GreetingFunctionsCloudEventTest.java
@@ -0,0 +1,63 @@
+package io.quarkus.funqy.gcp.functions.test;
+
+import static io.restassured.RestAssured.given;
+
+import org.junit.jupiter.api.Test;
+
+import com.google.cloud.functions.invoker.runner.Invoker;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.TestProfile;
+
+@QuarkusTest
+@TestProfile(GreetingFunctionsCloudEventTest.CloudEventFunctionTestProfile.class)
+class GreetingFunctionsCloudEventTest {
+    @Test
+    public void test() throws Exception {
+        // start the invoker without joining to avoid blocking the thread
+        Invoker invoker = new Invoker(
+                8081,
+                "io.quarkus.funqy.gcp.functions.FunqyCloudEventsFunction",
+                "cloudevent",
+                Thread.currentThread().getContextClassLoader());
+        invoker.startTestServer();
+
+        // test the function using RestAssured
+        given()
+                .body("{\n" +
+                        "        \"bucket\": \"MY_BUCKET\",\n" +
+                        "        \"contentType\": \"text/plain\",\n" +
+                        "        \"kind\": \"storage#object\",\n" +
+                        "        \"md5Hash\": \"...\",\n" +
+                        "        \"metageneration\": \"1\",\n" +
+                        "        \"name\": \"MY_FILE.txt\",\n" +
+                        "        \"size\": \"352\",\n" +
+                        "        \"storageClass\": \"MULTI_REGIONAL\",\n" +
+                        "        \"timeCreated\": \"2020-04-23T07:38:57.230Z\",\n" +
+                        "        \"timeStorageClassUpdated\": \"2020-04-23T07:38:57.230Z\",\n" +
+                        "        \"updated\": \"2020-04-23T07:38:57.230Z\"\n" +
+                        "      }")
+                .header("ce-id", "123451234512345")
+                .header("ce-specversion", "1.0")
+                .header("ce-time", "2020-01-02T12:34:56.789Z")
+                .header("ce-type", "google.cloud.storage.object.v1.finalized")
+                .header("ce-source", "//storage.googleapis.com/projects/_/buckets/MY-BUCKET-NAME")
+                .header("ce-subject", "objects/MY_FILE.txt")
+                .when()
+                .post("http://localhost:8081")
+                .then()
+                .statusCode(200);
+
+        // stop the invoker
+        invoker.stopServer();
+    }
+
+    public static class CloudEventFunctionTestProfile implements QuarkusTestProfile {
+
+        @Override
+        public String getConfigProfile() {
+            return "cloud-event";
+        }
+    }
+}
diff --git a/integration-tests/funqy-google-cloud-functions/src/test/java/io/quarkus/funqy/gcp/functions/test/GreetingFunctionsPubsubTest.java b/integration-tests/funqy-google-cloud-functions/src/test/java/io/quarkus/funqy/gcp/functions/test/GreetingFunctionsPubsubTest.java
new file mode 100644
index 0000000000000..b6160e6bbf3a2
--- /dev/null
+++ b/integration-tests/funqy-google-cloud-functions/src/test/java/io/quarkus/funqy/gcp/functions/test/GreetingFunctionsPubsubTest.java
@@ -0,0 +1,45 @@
+package io.quarkus.funqy.gcp.functions.test;
+
+import static io.restassured.RestAssured.given;
+
+import org.junit.jupiter.api.Test;
+
+import com.google.cloud.functions.invoker.runner.Invoker;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.TestProfile;
+
+@QuarkusTest
+@TestProfile(GreetingFunctionsPubsubTest.PubsubFunctionTestProfile.class)
+class GreetingFunctionsPubsubTest {
+    @Test
+    public void test() throws Exception {
+        // start the invoker without joining to avoid blocking the thread
+        Invoker invoker = new Invoker(
+                8081,
+                "io.quarkus.funqy.gcp.functions.FunqyBackgroundFunction",
+                "event",
+                Thread.currentThread().getContextClassLoader());
+        invoker.startTestServer();
+
+        // test the function using RestAssured
+        given()
+                .body("{\"data\":{\"data\":\"world\"}}")
+                .when()
+                .post("http://localhost:8081")
+                .then()
+                .statusCode(200);
+
+        // stop the invoker
+        invoker.stopServer();
+    }
+
+    public static class PubsubFunctionTestProfile implements QuarkusTestProfile {
+
+        @Override
+        public String getConfigProfile() {
+            return "pubsub";
+        }
+    }
+}
diff --git a/integration-tests/funqy-google-cloud-functions/src/test/java/io/quarkus/funqy/gcp/functions/test/GreetingFunctionsStorageTest.java b/integration-tests/funqy-google-cloud-functions/src/test/java/io/quarkus/funqy/gcp/functions/test/GreetingFunctionsStorageTest.java
new file mode 100644
index 0000000000000..545eda2861e03
--- /dev/null
+++ b/integration-tests/funqy-google-cloud-functions/src/test/java/io/quarkus/funqy/gcp/functions/test/GreetingFunctionsStorageTest.java
@@ -0,0 +1,34 @@
+package io.quarkus.funqy.gcp.functions.test;
+
+import static io.restassured.RestAssured.given;
+
+import org.junit.jupiter.api.Test;
+
+import com.google.cloud.functions.invoker.runner.Invoker;
+
+import io.quarkus.test.junit.QuarkusTest;
+
+@QuarkusTest
+class GreetingFunctionsStorageTest {
+    @Test
+    public void test() throws Exception {
+        // start the invoker without joining to avoid blocking the thread
+        Invoker invoker = new Invoker(
+                8081,
+                "io.quarkus.funqy.gcp.functions.FunqyBackgroundFunction",
+                "event",
+                Thread.currentThread().getContextClassLoader());
+        invoker.startTestServer();
+
+        // test the function using RestAssured
+        given()
+                .body("{\"data\":{\"name\":\"hello.txt\"}}")
+                .when()
+                .post("http://localhost:8081")
+                .then()
+                .statusCode(200);
+
+        // stop the invoker
+        invoker.stopServer();
+    }
+}
diff --git a/integration-tests/google-cloud-functions/pom.xml b/integration-tests/google-cloud-functions/pom.xml
index 1f08f2ab19810..01691814496bf 100644
--- a/integration-tests/google-cloud-functions/pom.xml
+++ b/integration-tests/google-cloud-functions/pom.xml
@@ -17,12 +17,24 @@
             <groupId>io.quarkus</groupId>
             <artifactId>quarkus-google-cloud-functions</artifactId>
         </dependency>
+
         <!-- test dependencies -->
+        <dependency>
+            <groupId>io.rest-assured</groupId>
+            <artifactId>rest-assured</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>io.quarkus</groupId>
             <artifactId>quarkus-junit5</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.google.cloud.functions.invoker</groupId>
+            <artifactId>java-function-invoker</artifactId>
+            <version>${gcf-invoker.version}</version>
+            <scope>test</scope>
+        </dependency>
 
         <!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
         <dependency>
diff --git a/integration-tests/google-cloud-functions/src/main/resources/application.properties b/integration-tests/google-cloud-functions/src/main/resources/application.properties
index 97124e554911e..546cad99e5117 100644
--- a/integration-tests/google-cloud-functions/src/main/resources/application.properties
+++ b/integration-tests/google-cloud-functions/src/main/resources/application.properties
@@ -1,4 +1,4 @@
 quarkus.google-cloud-functions.function=httpTest
-#quarkus.google-cloud-functions.function=cloudEventTest
-#quarkus.google-cloud-functions.function=rawPubSubTest
-#quarkus.google-cloud-functions.function=storageTest
+%cloud-event.quarkus.google-cloud-functions.function=cloudEventTest
+%raw-background.quarkus.google-cloud-functions.function=rawPubSubTest
+%background.quarkus.google-cloud-functions.function=storageTest
diff --git a/integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/BackgroundFunctionStorageTestCase.java b/integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/BackgroundFunctionStorageTestCase.java
new file mode 100644
index 0000000000000..c5d644befc3f9
--- /dev/null
+++ b/integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/BackgroundFunctionStorageTestCase.java
@@ -0,0 +1,45 @@
+package io.quarkus.gcp.function.test;
+
+import static io.restassured.RestAssured.given;
+
+import org.junit.jupiter.api.Test;
+
+import com.google.cloud.functions.invoker.runner.Invoker;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.TestProfile;
+
+@QuarkusTest
+@TestProfile(BackgroundFunctionStorageTestCase.BackgroundFunctionTestProfile.class)
+class BackgroundFunctionStorageTestCase {
+    @Test
+    public void test() throws Exception {
+        // start the invoker without joining to avoid blocking the thread
+        Invoker invoker = new Invoker(
+                8081,
+                "io.quarkus.gcp.functions.QuarkusBackgroundFunction",
+                "event",
+                Thread.currentThread().getContextClassLoader());
+        invoker.startTestServer();
+
+        // test the function using RestAssured
+        given()
+                .body("{\"data\":{\"name\":\"hello.txt\"}}")
+                .when()
+                .post("http://localhost:8081")
+                .then()
+                .statusCode(200);
+
+        // stop the invoker
+        invoker.stopServer();
+    }
+
+    public static class BackgroundFunctionTestProfile implements QuarkusTestProfile {
+
+        @Override
+        public String getConfigProfile() {
+            return "background";
+        }
+    }
+}
diff --git a/integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/CloudEventStorageTestCase.java b/integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/CloudEventStorageTestCase.java
new file mode 100644
index 0000000000000..c16de6d017996
--- /dev/null
+++ b/integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/CloudEventStorageTestCase.java
@@ -0,0 +1,62 @@
+package io.quarkus.gcp.function.test;
+
+import static io.restassured.RestAssured.given;
+
+import org.junit.jupiter.api.Test;
+
+import com.google.cloud.functions.invoker.runner.Invoker;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.TestProfile;
+
+@QuarkusTest
+@TestProfile(CloudEventStorageTestCase.CloudEventFunctionTestProfile.class)
+class CloudEventStorageTestCase {
+    @Test
+    public void test() throws Exception {
+        // start the invoker without joining to avoid blocking the thread
+        Invoker invoker = new Invoker(
+                8081,
+                "io.quarkus.gcp.functions.QuarkusCloudEventsFunction",
+                "cloudevent",
+                Thread.currentThread().getContextClassLoader());
+        invoker.startTestServer();
+
+        // test the function using RestAssured
+        given()
+                .header("ce-specversion", "1.0")
+                .header("ce-id", "1234567890")
+                .header("ce-type", "google.cloud.storage.object.v1.finalized")
+                .header("ce-source", "//storage.googleapis.com/projects/_/buckets/MY-BUCKET-NAME")
+                .header("ce-subject", "objects/MY_FILE.txt")
+                .body("{\n" +
+                        "        \"bucket\": \"MY_BUCKET\",\n" +
+                        "        \"contentType\": \"text/plain\",\n" +
+                        "        \"kind\": \"storage#object\",\n" +
+                        "        \"md5Hash\": \"...\",\n" +
+                        "        \"metageneration\": \"1\",\n" +
+                        "        \"name\": \"MY_FILE.txt\",\n" +
+                        "        \"size\": \"352\",\n" +
+                        "        \"storageClass\": \"MULTI_REGIONAL\",\n" +
+                        "        \"timeCreated\": \"2020-04-23T07:38:57.230Z\",\n" +
+                        "        \"timeStorageClassUpdated\": \"2020-04-23T07:38:57.230Z\",\n" +
+                        "        \"updated\": \"2020-04-23T07:38:57.230Z\"\n" +
+                        "      }")
+                .when()
+                .post("http://localhost:8081")
+                .then()
+                .statusCode(200);
+
+        // stop the invoker
+        invoker.stopServer();
+    }
+
+    public static class CloudEventFunctionTestProfile implements QuarkusTestProfile {
+
+        @Override
+        public String getConfigProfile() {
+            return "cloud-event";
+        }
+    }
+}
diff --git a/integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/HttpFunctionTestCase.java b/integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/HttpFunctionTestCase.java
new file mode 100644
index 0000000000000..e7eff84c1c68f
--- /dev/null
+++ b/integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/HttpFunctionTestCase.java
@@ -0,0 +1,34 @@
+package io.quarkus.gcp.function.test;
+
+import static io.restassured.RestAssured.when;
+import static org.hamcrest.Matchers.is;
+
+import org.junit.jupiter.api.Test;
+
+import com.google.cloud.functions.invoker.runner.Invoker;
+
+import io.quarkus.test.junit.QuarkusTest;
+
+@QuarkusTest
+class HttpFunctionTestCase {
+    @Test
+    public void test() throws Exception {
+        // start the invoker without joining to avoid blocking the thread
+        Invoker invoker = new Invoker(
+                8081,
+                "io.quarkus.gcp.functions.QuarkusHttpFunction",
+                "http",
+                Thread.currentThread().getContextClassLoader());
+        invoker.startTestServer();
+
+        // test the function using RestAssured
+        when()
+                .get("http://localhost:8081")
+                .then()
+                .statusCode(200)
+                .body(is("Hello World!"));
+
+        // stop the invoker
+        invoker.stopServer();
+    }
+}
diff --git a/integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/RawBackgroundFunctionPubSubTestCase.java b/integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/RawBackgroundFunctionPubSubTestCase.java
new file mode 100644
index 0000000000000..4a414da66db36
--- /dev/null
+++ b/integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/RawBackgroundFunctionPubSubTestCase.java
@@ -0,0 +1,45 @@
+package io.quarkus.gcp.function.test;
+
+import static io.restassured.RestAssured.given;
+
+import org.junit.jupiter.api.Test;
+
+import com.google.cloud.functions.invoker.runner.Invoker;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.QuarkusTestProfile;
+import io.quarkus.test.junit.TestProfile;
+
+@QuarkusTest
+@TestProfile(RawBackgroundFunctionPubSubTestCase.RawBackgroundFunctionTestProfile.class)
+class RawBackgroundFunctionPubSubTestCase {
+    @Test
+    public void test() throws Exception {
+        // start the invoker without joining to avoid blocking the thread
+        Invoker invoker = new Invoker(
+                8081,
+                "io.quarkus.gcp.functions.QuarkusBackgroundFunction",
+                "event",
+                Thread.currentThread().getContextClassLoader());
+        invoker.startTestServer();
+
+        // test the function using RestAssured
+        given()
+                .body("{\"data\":{\"name\":\"hello.txt\"}}")
+                .when()
+                .post("http://localhost:8081")
+                .then()
+                .statusCode(200);
+
+        // stop the invoker
+        invoker.stopServer();
+    }
+
+    public static class RawBackgroundFunctionTestProfile implements QuarkusTestProfile {
+
+        @Override
+        public String getConfigProfile() {
+            return "raw-background";
+        }
+    }
+}