This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 4adc26ba2ffdc67211d4671db1cba440305ae675
Author: Raffaele Marcello <marcelloraffa...@gmail.com>
AuthorDate: Thu Mar 4 08:49:39 2021 +0100

    CAMEL-15963 Create a Google Cloud Functions component
---
 .../functions/GoogleCloudFunctionsProducer.java    |  47 +++++++-
 .../unit/GoogleCloudFunctionsComponentTest.java    | 128 +++++++++++++++++----
 .../src/test/resources/log4j.properties            |   2 +-
 3 files changed, 154 insertions(+), 23 deletions(-)

diff --git 
a/components/camel-google-functions/src/main/java/org/apache/camel/component/google/functions/GoogleCloudFunctionsProducer.java
 
b/components/camel-google-functions/src/main/java/org/apache/camel/component/google/functions/GoogleCloudFunctionsProducer.java
index 8abd299..581d6f6 100644
--- 
a/components/camel-google-functions/src/main/java/org/apache/camel/component/google/functions/GoogleCloudFunctionsProducer.java
+++ 
b/components/camel-google-functions/src/main/java/org/apache/camel/component/google/functions/GoogleCloudFunctionsProducer.java
@@ -20,7 +20,9 @@ import java.util.List;
 
 import com.google.api.client.util.Lists;
 import com.google.api.gax.rpc.ApiException;
+import com.google.cloud.functions.v1.CallFunctionResponse;
 import com.google.cloud.functions.v1.CloudFunction;
+import com.google.cloud.functions.v1.CloudFunctionName;
 import com.google.cloud.functions.v1.CloudFunctionsServiceClient;
 import 
com.google.cloud.functions.v1.CloudFunctionsServiceClient.ListFunctionsPagedResponse;
 import com.google.cloud.functions.v1.ListFunctionsRequest;
@@ -89,10 +91,51 @@ public class GoogleCloudFunctionsProducer extends 
DefaultProducer {
         }
     }
 
-    private void getFunction(CloudFunctionsServiceClient client, Exchange 
exchange) {
+    private void getFunction(CloudFunctionsServiceClient client, Exchange 
exchange) throws InvalidPayloadException {
+        if (getConfiguration().isPojoRequest()) {
+            Object payload = exchange.getIn().getMandatoryBody();
+            if (payload instanceof CloudFunctionName) {
+                CloudFunction result;
+                try {
+                    result = client.getFunction((CloudFunctionName) payload);
+                } catch (ApiException ae) {
+                    LOG.trace("getFunction command returned the error code 
{}", ae.getStatusCode());
+                    throw ae;
+                }
+                Message message = getMessageForResponse(exchange);
+                message.setBody(result);
+            }
+        } else {
+            CloudFunctionName cfName = 
CloudFunctionName.of(getConfiguration().getProject(), 
getConfiguration().getLocation(),
+                    getConfiguration().getFunctionName());
+            CloudFunction result = client.getFunction(cfName);
+            Message message = getMessageForResponse(exchange);
+            message.setBody(result);
+        }
     }
 
-    private void callFunction(CloudFunctionsServiceClient client, Exchange 
exchange) {
+    private void callFunction(CloudFunctionsServiceClient client, Exchange 
exchange) throws InvalidPayloadException {
+        String data = exchange.getIn().getBody(String.class);
+        if (getConfiguration().isPojoRequest()) {
+            Object payload = exchange.getIn().getMandatoryBody();
+            if (payload instanceof CloudFunctionName) {
+                CallFunctionResponse result;
+                try {
+                    result = client.callFunction((CloudFunctionName) payload, 
data);
+                } catch (ApiException ae) {
+                    LOG.trace("callFunction command returned the error code 
{}", ae.getStatusCode());
+                    throw ae;
+                }
+                Message message = getMessageForResponse(exchange);
+                message.setBody(result);
+            }
+        } else {
+            CloudFunctionName cfName = 
CloudFunctionName.of(getConfiguration().getProject(), 
getConfiguration().getLocation(),
+                    getConfiguration().getFunctionName());
+            CallFunctionResponse result = client.callFunction(cfName, data);
+            Message message = getMessageForResponse(exchange);
+            message.setBody(result);
+        }
     }
 
     private GoogleCloudFunctionsOperations determineOperation(Exchange 
exchange) {
diff --git 
a/components/camel-google-functions/src/test/java/org/apache/camel/component/google/functions/unit/GoogleCloudFunctionsComponentTest.java
 
b/components/camel-google-functions/src/test/java/org/apache/camel/component/google/functions/unit/GoogleCloudFunctionsComponentTest.java
index d7be5d1..75af885 100644
--- 
a/components/camel-google-functions/src/test/java/org/apache/camel/component/google/functions/unit/GoogleCloudFunctionsComponentTest.java
+++ 
b/components/camel-google-functions/src/test/java/org/apache/camel/component/google/functions/unit/GoogleCloudFunctionsComponentTest.java
@@ -17,10 +17,21 @@
 package org.apache.camel.component.google.functions.unit;
 
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
 
+import com.google.api.gax.grpc.GaxGrpcProperties;
+import com.google.api.gax.rpc.ApiClientHeaderProvider;
+import com.google.cloud.functions.v1.CallFunctionRequest;
+import com.google.cloud.functions.v1.CallFunctionResponse;
 import com.google.cloud.functions.v1.CloudFunction;
-import 
com.google.cloud.functions.v1.CloudFunctionsServiceClient.ListFunctionsPagedResponse;
+import com.google.cloud.functions.v1.CloudFunctionName;
+import com.google.cloud.functions.v1.CloudFunctionStatus;
+import com.google.cloud.functions.v1.GetFunctionRequest;
 import com.google.cloud.functions.v1.ListFunctionsResponse;
+import com.google.protobuf.AbstractMessage;
+import com.google.protobuf.Duration;
+import com.google.protobuf.Timestamp;
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
@@ -30,8 +41,9 @@ import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static 
org.apache.camel.component.google.functions.unit.GoogleCloudFunctionsBaseTest.mockCloudFunctionsService;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class GoogleCloudFunctionsComponentTest extends 
GoogleCloudFunctionsBaseTest {
 
@@ -40,34 +52,110 @@ public class GoogleCloudFunctionsComponentTest extends 
GoogleCloudFunctionsBaseT
     @EndpointInject("mock:result")
     private MockEndpoint mock;
 
+    private String project = "project123";
+    private String location = "location123";
+    private String functionName = "myCalmelFunction";
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:listFunctions")
+                        // 
.to("google-functions://myCamelFunction?client=#client&operation=listFunctions")
+                        .to("google-functions://" + functionName + "?project=" 
+ project + "&location=" + location
+                            + "&operation=listFunctions")
+                        .to("mock:result");
+                from("direct:getFunction")
+                        .to("google-functions://" + functionName + "?project=" 
+ project + "&location=" + location
+                            + "&operation=getFunction")
+                        .to("mock:result");
+                from("direct:callFunction")
+                        .to("google-functions://" + functionName + "?project=" 
+ project + "&location=" + location
+                            + "&operation=callFunction")
+                        .to("mock:result");
+            }
+        };
+    }
+
     @Test
     public void listFunctionsTest() throws Exception {
-
-        CloudFunction responsesElement = CloudFunction.newBuilder().build();
+        log.info("list function");
+        CloudFunction cf1 = CloudFunction.newBuilder().build();
+        CloudFunction cf2 = CloudFunction.newBuilder().build();
+        List<CloudFunction> cfList = Arrays.asList(cf1, cf2);
         ListFunctionsResponse expectedResponse = 
ListFunctionsResponse.newBuilder().setNextPageToken("")
-                .addAllFunctions(Arrays.asList(responsesElement)).build();
+                .addAllFunctions(cfList).build();
         mockCloudFunctionsService.addResponse(expectedResponse);
 
         Exchange exchange = template.send("direct:listFunctions", 
ExchangePattern.InOut, exc -> {
         });
-        log.info("body: " + exchange.getMessage().getBody());
-        ListFunctionsPagedResponse result = 
exchange.getMessage().getBody(ListFunctionsPagedResponse.class);
+        List<CloudFunction> result = exchange.getMessage().getBody(List.class);
         assertNotNull(result);
-        // assertEquals(1, result.getPage().);
-        // assertEquals("GetHelloWithName", 
result.functions().get(0).functionName());
+        assertEquals(cfList.size(), result.size());
+
+        for (int i = 0; i < result.size(); i++) {
+            assertEquals(expectedResponse.getFunctionsList().get(i), 
result.get(i));
+        }
+
     }
 
-    @Override
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        log.info("createRouteBuilder");
-        return new RouteBuilder() {
-            public void configure() {
+    @Test
+    public void getFunctionTest() throws Exception {
+        CloudFunction expectedResponse = CloudFunction.newBuilder()
+                .setName(CloudFunctionName.of(project, location, 
functionName).toString())
+                
.setDescription("description-1724546052").setStatus(CloudFunctionStatus.forNumber(0))
+                
.setEntryPoint("entryPoint-1979329474").setRuntime("runtime1550962648")
+                
.setTimeout(Duration.newBuilder().build()).setAvailableMemoryMb(1964533661)
+                
.setServiceAccountEmail("serviceAccountEmail1825953988").setUpdateTime(Timestamp.newBuilder().build())
+                .setVersionId(-670497310).putAllLabels(new HashMap<String, 
String>())
+                .putAllEnvironmentVariables(new HashMap<String, 
String>()).setNetwork("network1843485230")
+                
.setMaxInstances(-330682013).setVpcConnector("vpcConnector2101559652").setBuildId("buildId230943785")
+                .build();
+        mockCloudFunctionsService.addResponse(expectedResponse);
+        CloudFunctionName cfName = CloudFunctionName.of(project, location, 
functionName);
 
-                from("direct:listFunctions")
-                        // 
.to("google-functions://myCamelFunction?client=#client&operation=listFunctions")
-                        
.to("google-functions://myCamelFunction?project=proj1&location=loc1&operation=listFunctions")
-                        .to("mock:result");
-            }
-        };
+        Exchange exchange = template.send("direct:getFunction", 
ExchangePattern.InOut, exc -> {
+        });
+        CloudFunction actualResponse = 
exchange.getMessage().getBody(CloudFunction.class);
+        assertEquals(expectedResponse, actualResponse);
+
+        List<AbstractMessage> actualRequests = 
mockCloudFunctionsService.getRequests();
+        assertEquals(1, actualRequests.size());
+        GetFunctionRequest actualRequest = ((GetFunctionRequest) 
actualRequests.get(0));
+
+        assertEquals(cfName.toString(), actualRequest.getName());
+        
assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
+                GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
     }
+
+    @Test
+    public void callFunctionTest() throws Exception {
+        CallFunctionResponse expectedResponse = 
CallFunctionResponse.newBuilder()
+                .setExecutionId("executionId-454906285")
+                .setResult("result-934426595")
+                .setError("error96784904")
+                .build();
+        mockCloudFunctionsService.addResponse(expectedResponse);
+
+        CloudFunctionName name = CloudFunctionName.of(project, location, 
functionName);
+        String data = "data3076010";
+
+        Exchange exchange = template.send("direct:callFunction", 
ExchangePattern.InOut, exc -> {
+            exc.getIn().setBody(data);
+        });
+        CallFunctionResponse actualResponse = 
exchange.getMessage().getBody(CallFunctionResponse.class);
+        assertEquals(expectedResponse, actualResponse);
+
+        List<AbstractMessage> actualRequests = 
mockCloudFunctionsService.getRequests();
+        assertEquals(1, actualRequests.size());
+        CallFunctionRequest actualRequest = ((CallFunctionRequest) 
actualRequests.get(0));
+
+        assertEquals(name.toString(), actualRequest.getName());
+        assertEquals(data, actualRequest.getData());
+        assertTrue(
+                channelProvider.isHeaderSent(
+                        ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
+                        GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
+    }
+
 }
diff --git 
a/components/camel-google-functions/src/test/resources/log4j.properties 
b/components/camel-google-functions/src/test/resources/log4j.properties
index 1541092..23e3d45 100644
--- a/components/camel-google-functions/src/test/resources/log4j.properties
+++ b/components/camel-google-functions/src/test/resources/log4j.properties
@@ -28,4 +28,4 @@ appender.out.name = out
 appender.out.layout.type = PatternLayout
 appender.out.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
 rootLogger.level = INFO
-rootLogger.appenderRef.file.ref = file
+rootLogger.appenderRef.file.ref = file
\ No newline at end of file

Reply via email to