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 0b96e02be537ef9e874b31410a1dc59cea2c3367 Author: Raffaele Marcello <marcelloraffa...@gmail.com> AuthorDate: Wed Mar 10 09:28:42 2021 +0100 CAMEL-15963 Create a Google Cloud Functions component --- .../integration/ComplexIntegrationTest.java | 138 +++++++++++++++++++++ .../GoogleCloudFunctionsConfigurationTest.java | 64 ++++++++-- 2 files changed, 193 insertions(+), 9 deletions(-) diff --git a/components/camel-google-functions/src/test/java/org/apache/camel/component/google/functions/integration/ComplexIntegrationTest.java b/components/camel-google-functions/src/test/java/org/apache/camel/component/google/functions/integration/ComplexIntegrationTest.java new file mode 100644 index 0000000..3861550 --- /dev/null +++ b/components/camel-google-functions/src/test/java/org/apache/camel/component/google/functions/integration/ComplexIntegrationTest.java @@ -0,0 +1,138 @@ +/* + * 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.component.google.functions.integration; + +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +public class ComplexIntegrationTest extends CamelTestSupport { + + @EndpointInject + private ProducerTemplate template; + + @EndpointInject("mock:bucket1") + private MockEndpoint mockBucket1; + + @EndpointInject("mock:bucket2") + private MockEndpoint mockBucket2; + + @EndpointInject("mock:processed") + private MockEndpoint mockProcessed; + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // final int numberOfObjects = 3; + //final String serviceAccountKeyFile = "somefile.json"; + // final String serviceAccountKeyFile = System.getenv("GOOGLE_APPLICATION_CREDENTIALS"); + + // //upload 3 file into bucket1 + // byte[] payload = "Camel rocks!".getBytes(); + // ByteArrayInputStream bais = new ByteArrayInputStream(payload); + // from("timer:timer1?repeatCount=" + numberOfObjects) + // .process(exchange -> { + // String filename = "file_" + ((int) (Math.random() * 10000)) + ".txt"; + // exchange.getIn().setHeader(GoogleCloudStorageConstants.OBJECT_NAME, filename); + // exchange.getIn().setBody(bais); + // }) + // .to("google-storage://" + bucket1 + "?serviceAccountKey=" + serviceAccountKeyFile) + // .log("upload file object:${header.CamelGoogleCloudStorageObjectName}, body:${body}") + // .to("mock:bucket1"); + + // //poll from bucket1, moving processed into bucket_processed and deleting original + // from("google-storage://" + bucket1 + "?serviceAccountKey=" + serviceAccountKeyFile + // + "&moveAfterRead=true" + // + "&destinationBucket=" + bucket3 + // + "&autoCreateBucket=true" + // + "&deleteAfterRead=true" + // + "&includeBody=true") + // .log("consuming: ${header.CamelGoogleCloudStorageBucketName}/${header.CamelGoogleCloudStorageObjectName}") + // .to("direct:processed") + // .to("mock:processed"); + + // //upload these files to bucket2 + // from("direct:processed") + // .to("google-storage://" + bucket2 + "?serviceAccountKey=" + serviceAccountKeyFile) + // .log("uploaded file object:${header.CamelGoogleCloudStorageObjectName}, body:${body}") + // .process(exchange -> { + // exchange.getIn().setHeader(GoogleCloudStorageConstants.DOWNLOAD_LINK_EXPIRATION_TIME, 86400000L); //1 day + // }) + // .to("google-storage://" + bucket2 + "?serviceAccountKey=" + serviceAccountKeyFile + // + "&operation=createDownloadLink") + // .log("URL for ${header.CamelGoogleCloudStorageBucketName}/${header.CamelGoogleCloudStorageObjectName} =${body}") + // .to("mock:bucket2"); + + // //list all buckets + // from("timer:timer1?repeatCount=1&fixedRate=true&period=10000") + // .to("google-storage://" + bucket2 + "?serviceAccountKey=" + serviceAccountKeyFile + // + "&operation=listBuckets") + // .log("list buckets:${body}"); + + // //list all object of the bucket2 and send result to direct:moreinfo and direct:copy + // from("timer:timer1?repeatCount=1&fixedRate=true&period=10000") + // .to("google-storage://" + bucket2 + "?serviceAccountKey=" + serviceAccountKeyFile + // + "&operation=listObjects") + // .log("list " + bucket2 + " objects body:${body}") + // .split(bodyAs(List.class)) + // .log("splitted: ${body}") + // .multicast().to("direct:moreinfo", "direct:copy"); + + // from("direct:moreinfo") + // .process(exchange -> { + // Blob blob = exchange.getIn().getBody(Blob.class); + // String fileName = blob.getName(); + // exchange.getIn().setHeader(GoogleCloudStorageConstants.OBJECT_NAME, fileName); + // }) + // .to("google-storage://" + bucket2 + "?serviceAccountKey=" + serviceAccountKeyFile + // + "&operation=getObject") + // .log("get object bucket:${header.CamelGoogleCloudStorageBucketName} object:${header.CamelGoogleCloudStorageObjectName}, body:${body}"); + + // //copy object + // from("direct:copy") + // .process(exchange -> { + // Blob blob = exchange.getIn().getBody(Blob.class); + // String fileName = blob.getName(); + // String copyFileName = "copy_" + fileName; + // exchange.getIn().setHeader(GoogleCloudStorageConstants.OBJECT_NAME, fileName); + // exchange.getIn().setHeader(GoogleCloudStorageConstants.DESTINATION_BUCKET_NAME, bucket4); + // exchange.getIn().setHeader(GoogleCloudStorageConstants.DESTINATION_OBJECT_NAME, copyFileName); + // }) + // .to("google-storage://" + bucket2 + "?serviceAccountKey=" + serviceAccountKeyFile + // + "&operation=copyObject") + // .log("${body}"); + + } + }; + } + + @Test + public void sendIn() throws Exception { + mockBucket1.expectedMessageCount(3); + mockBucket2.expectedMessageCount(3); + mockProcessed.expectedMessageCount(3); + + Thread.sleep(10000); + assertMockEndpointsSatisfied(); + } + +} diff --git a/components/camel-google-functions/src/test/java/org/apache/camel/component/google/functions/unit/GoogleCloudFunctionsConfigurationTest.java b/components/camel-google-functions/src/test/java/org/apache/camel/component/google/functions/unit/GoogleCloudFunctionsConfigurationTest.java index 9a66af6..2704ed7 100644 --- a/components/camel-google-functions/src/test/java/org/apache/camel/component/google/functions/unit/GoogleCloudFunctionsConfigurationTest.java +++ b/components/camel-google-functions/src/test/java/org/apache/camel/component/google/functions/unit/GoogleCloudFunctionsConfigurationTest.java @@ -16,31 +16,77 @@ */ package org.apache.camel.component.google.functions.unit; +import java.util.Arrays; +import java.util.UUID; + +import com.google.api.gax.core.NoCredentialsProvider; +import com.google.api.gax.grpc.testing.LocalChannelProvider; +import com.google.api.gax.grpc.testing.MockGrpcService; +import com.google.api.gax.grpc.testing.MockServiceHelper; +import com.google.cloud.functions.v1.CloudFunctionsServiceClient; +import com.google.cloud.functions.v1.CloudFunctionsServiceSettings; import org.apache.camel.component.google.functions.GoogleCloudFunctionsComponent; import org.apache.camel.component.google.functions.GoogleCloudFunctionsEndpoint; +import org.apache.camel.component.google.functions.GoogleCloudFunctionsOperations; +import org.apache.camel.component.google.functions.mock.MockCloudFunctionsService; import org.apache.camel.test.junit5.CamelTestSupport; import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class GoogleCloudFunctionsConfigurationTest extends CamelTestSupport { - private static final Logger LOGGER = LoggerFactory.getLogger(GoogleCloudFunctionsConfigurationTest.class); - @Test - public void createEndpointWithMinimalConfiguration() throws Exception { + public void createEndpointConfiguration() throws Exception { final String functionName = "function1"; final String serviceAccountKeyFile = "somefile.json"; + final String project = "project123"; + final String location = "location123"; + final GoogleCloudFunctionsOperations operation = GoogleCloudFunctionsOperations.callFunction; + final boolean pojoRequest = false; - GoogleCloudFunctionsComponent component = context.getComponent("google-functions", GoogleCloudFunctionsComponent.class); - GoogleCloudFunctionsEndpoint endpoint = (GoogleCloudFunctionsEndpoint) component.createEndpoint( - String.format("google-functions://%s?serviceAccountKey=%s", functionName, serviceAccountKeyFile)); + GoogleCloudFunctionsComponent component = context.getComponent("google-functions", + GoogleCloudFunctionsComponent.class); + GoogleCloudFunctionsEndpoint endpoint = (GoogleCloudFunctionsEndpoint) component.createEndpoint(String.format( + "google-functions://%s?serviceAccountKey=%s&project=%s&location=%s&operation=%s&pojoRequest=%s", + functionName, serviceAccountKeyFile, project, location, operation.name(), pojoRequest)); assertEquals(endpoint.getConfiguration().getFunctionName(), functionName); assertEquals(endpoint.getConfiguration().getServiceAccountKey(), serviceAccountKeyFile); - LOGGER.info("OK"); + assertEquals(endpoint.getConfiguration().getProject(), project); + assertEquals(endpoint.getConfiguration().getLocation(), location); + assertEquals(endpoint.getConfiguration().getOperation(), operation); + assertEquals(endpoint.getConfiguration().isPojoRequest(), pojoRequest); + } + + @Test + public void createEndpointWithAutowireClient() throws Exception { + final String functionName = "function1"; + + // init mock + MockCloudFunctionsService mockCloudFunctionsService = new MockCloudFunctionsService(); + MockServiceHelper mockServiceHelper = new MockServiceHelper( + UUID.randomUUID().toString(), + Arrays.<MockGrpcService> asList(mockCloudFunctionsService)); + mockServiceHelper.start(); + LocalChannelProvider channelProvider = mockServiceHelper.createChannelProvider(); + CloudFunctionsServiceSettings settings = CloudFunctionsServiceSettings.newBuilder() + .setTransportChannelProvider(channelProvider).setCredentialsProvider(NoCredentialsProvider.create()) + .build(); + CloudFunctionsServiceClient clientMock = CloudFunctionsServiceClient.create(settings); + + context.getRegistry().bind("myClient", clientMock); + + GoogleCloudFunctionsComponent component = context.getComponent("google-functions", + GoogleCloudFunctionsComponent.class); + GoogleCloudFunctionsEndpoint endpoint = (GoogleCloudFunctionsEndpoint) component + .createEndpoint(String.format("google-functions://%s?client=#myClient", functionName)); + + assertEquals(endpoint.getConfiguration().getFunctionName(), functionName); + assertTrue(endpoint.getConfiguration().getClient() == clientMock); + + mockServiceHelper.stop(); } }