This is an automated email from the ASF dual-hosted git repository. nfilotto pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-karaf.git
The following commit(s) were added to refs/heads/main by this push: new aca82e14 Ref #378: Added camel-google-pubsub integration test and fixed guava import (#379) aca82e14 is described below commit aca82e14aaa9c926c3a67db0f47cc05b88cccf84 Author: lopushen <lopus...@gmail.com> AuthorDate: Fri Jun 21 13:56:56 2024 +0300 Ref #378: Added camel-google-pubsub integration test and fixed guava import (#379) --- features/src/main/feature/camel-features.xml | 2 +- tests/features/camel-google-pubsub/pom.xml | 47 ++++++++ .../camel/test/CamelGooglePubsubRouteSupplier.java | 59 ++++++++++ .../karaf/camel/itest/CamelGooglePubsubITest.java | 126 +++++++++++++++++++++ tests/features/pom.xml | 1 + 5 files changed, 234 insertions(+), 1 deletion(-) diff --git a/features/src/main/feature/camel-features.xml b/features/src/main/feature/camel-features.xml index fda28bfc..23ef35f2 100644 --- a/features/src/main/feature/camel-features.xml +++ b/features/src/main/feature/camel-features.xml @@ -1065,7 +1065,7 @@ <bundle dependency='true'>wrap:mvn:com.google.auth/google-auth-library-credentials/${grpc-google-auth-library-version}</bundle> <bundle dependency='true'>wrap:mvn:io.grpc/grpc-api/${grpc-version}$${spi-consumer}</bundle> <bundle dependency='true'>mvn:org.threeten/threetenbp/${auto-detect-version}</bundle> - <bundle dependency='true'>wrap:mvn:com.google.http-client/google-http-client/${google-cloud-http-client-version}$overwrite=merge&Import-Package=com.google.common*;version="[32,33)",*;resolution:=optional</bundle> + <bundle dependency='true'>wrap:mvn:com.google.http-client/google-http-client/${google-cloud-http-client-version}$overwrite=merge&Import-Package=com.google.common*;version="[33,34)",*;resolution:=optional</bundle> <bundle dependency='true'>wrap:mvn:com.google.http-client/google-http-client-gson/${google-cloud-http-client-version}</bundle> <bundle dependency='true'>wrap:mvn:io.opencensus/opencensus-api/${auto-detect-version}</bundle> <bundle dependency='true'>wrap:mvn:io.opencensus/opencensus-contrib-http-util/${auto-detect-version}</bundle> diff --git a/tests/features/camel-google-pubsub/pom.xml b/tests/features/camel-google-pubsub/pom.xml new file mode 100644 index 00000000..1082c3b0 --- /dev/null +++ b/tests/features/camel-google-pubsub/pom.xml @@ -0,0 +1,47 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.camel.karaf</groupId> + <artifactId>camel-karaf-features-test</artifactId> + <version>4.6.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>camel-google-pubsub-test</artifactId> + <name>Apache Camel :: Karaf :: Tests :: Features :: Google PubSub</name> + + <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-google-pubsub</artifactId> + <version>${camel-version}</version> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>gcloud</artifactId> + <version>${testcontainers-version}</version> + <scope>test</scope> + </dependency> + </dependencies> +</project> \ No newline at end of file diff --git a/tests/features/camel-google-pubsub/src/main/java/org/apache/karaf/camel/test/CamelGooglePubsubRouteSupplier.java b/tests/features/camel-google-pubsub/src/main/java/org/apache/karaf/camel/test/CamelGooglePubsubRouteSupplier.java new file mode 100644 index 00000000..3aa7c9ee --- /dev/null +++ b/tests/features/camel-google-pubsub/src/main/java/org/apache/karaf/camel/test/CamelGooglePubsubRouteSupplier.java @@ -0,0 +1,59 @@ +/* + * 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.karaf.camel.test; + +import java.util.function.Function; + +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.google.pubsub.GooglePubsubComponent; +import org.apache.camel.model.RouteDefinition; +import org.apache.karaf.camel.itests.AbstractCamelSingleFeatureResultMockBasedRouteSupplier; +import org.apache.karaf.camel.itests.CamelRouteSupplier; +import org.osgi.service.component.annotations.Component; + +@Component(name = "karaf-camel-google-pubsub-test", immediate = true, service = CamelRouteSupplier.class) +public class CamelGooglePubsubRouteSupplier extends AbstractCamelSingleFeatureResultMockBasedRouteSupplier { + + private static final String TEST_PROJECT = System.getProperty("pubsub.project"); + private static final String TEST_HOST = System.getProperty("pubsub.host") + ":" + System.getProperty("pubsub.port"); + private static final String TEST_TOPIC = System.getProperty("pubsub.topic"); + private static final String TEST_SUBSCRIPTION = System.getProperty("pubsub.subscription"); + private static final String GOOGLE_PUB_SUB_MESSAGE = "Hello, Google Pub/Sub!"; + private static final String GOOGLE_PUBSUB_COMPONENT_ID = "google-pubsub"; + + @Override + public void configure(CamelContext camelContext) { + final GooglePubsubComponent googlePubsubComponent = new GooglePubsubComponent(); + googlePubsubComponent.setAuthenticate(false); + googlePubsubComponent.setEndpoint(TEST_HOST); + + camelContext.addComponent(GOOGLE_PUBSUB_COMPONENT_ID, googlePubsubComponent); + } + + @Override + protected void configureProducer(RouteBuilder builder, RouteDefinition producerRoute) { + producerRoute.setBody(builder.constant(GOOGLE_PUB_SUB_MESSAGE)) + .toF("%s:%s:%s", GOOGLE_PUBSUB_COMPONENT_ID, TEST_PROJECT, TEST_TOPIC) + .log("Sent message to PubSub: ${body}"); + } + + @Override + protected Function<RouteBuilder, RouteDefinition> consumerRoute() { + return builder -> builder.fromF("%s:%s:%s", GOOGLE_PUBSUB_COMPONENT_ID, TEST_PROJECT, TEST_SUBSCRIPTION) + .log("Received message from PubSub: ${body}"); + } +} \ No newline at end of file diff --git a/tests/features/camel-google-pubsub/src/test/java/org/apache/karaf/camel/itest/CamelGooglePubsubITest.java b/tests/features/camel-google-pubsub/src/test/java/org/apache/karaf/camel/itest/CamelGooglePubsubITest.java new file mode 100644 index 00000000..dc0c4dd4 --- /dev/null +++ b/tests/features/camel-google-pubsub/src/test/java/org/apache/karaf/camel/itest/CamelGooglePubsubITest.java @@ -0,0 +1,126 @@ +/* + * Licensed 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.karaf.camel.itest; + +import java.io.IOException; + +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.karaf.camel.itests.AbstractCamelSingleFeatureResultMockBasedRouteITest; +import org.apache.karaf.camel.itests.CamelKarafTestHint; +import org.apache.karaf.camel.itests.GenericContainerResource; +import org.apache.karaf.camel.itests.PaxExamWithExternalResource; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; +import org.ops4j.pax.exam.spi.reactors.PerClass; +import org.testcontainers.containers.PubSubEmulatorContainer; +import org.testcontainers.utility.DockerImageName; + +import com.google.api.gax.core.NoCredentialsProvider; +import com.google.api.gax.grpc.GrpcTransportChannel; +import com.google.api.gax.rpc.FixedTransportChannelProvider; +import com.google.api.gax.rpc.TransportChannelProvider; +import com.google.cloud.pubsub.v1.SubscriptionAdminClient; +import com.google.cloud.pubsub.v1.SubscriptionAdminSettings; +import com.google.cloud.pubsub.v1.TopicAdminClient; +import com.google.cloud.pubsub.v1.TopicAdminSettings; +import com.google.pubsub.v1.PushConfig; +import com.google.pubsub.v1.SubscriptionName; +import com.google.pubsub.v1.TopicName; + +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; + +@CamelKarafTestHint(externalResourceProvider = CamelGooglePubsubITest.ExternalResourceProviders.class) +@RunWith(PaxExamWithExternalResource.class) +@ExamReactorStrategy(PerClass.class) +public class CamelGooglePubsubITest extends AbstractCamelSingleFeatureResultMockBasedRouteITest { + + @Override + public void configureMock(MockEndpoint mock) { + mock.expectedBodiesReceived("Hello, Google Pub/Sub!"); + } + + @Test + public void testResultMock() throws Exception { + assertMockEndpointsSatisfied(); + } + + public static final class ExternalResourceProviders { + + private static final int PUB_SUB_PORT = 8085; + static final String TEST_PROJECT_ID = "test-project"; + static final String TEST_TOPIC = "test-topic"; + static final String TEST_SUBSCRIPTION = "test-subscription"; + + public static GenericContainerResource<PubSubEmulatorContainer> createPubsubContainer() { + final PubSubEmulatorContainer pubSubEmulatorContainer = new PubSubEmulatorContainer( + DockerImageName.parse("gcr.io/google.com/cloudsdktool/google-cloud-cli:441.0.0-emulators")); + + return new GenericContainerResource<>(pubSubEmulatorContainer, resource -> { + setUpEmulator(pubSubEmulatorContainer); + + resource.setProperty("pubsub.host", pubSubEmulatorContainer.getHost()); + resource.setProperty("pubsub.port", Integer.toString(pubSubEmulatorContainer.getMappedPort(PUB_SUB_PORT))); + resource.setProperty("pubsub.project", TEST_PROJECT_ID); + resource.setProperty("pubsub.topic", TEST_TOPIC); + resource.setProperty("pubsub.subscription", TEST_SUBSCRIPTION); + }); + } + + private static void createTopic(final TransportChannelProvider channelProvider, + final NoCredentialsProvider credentialsProvider) throws IOException { + final TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder() + .setTransportChannelProvider(channelProvider) + .setCredentialsProvider(credentialsProvider) + .build(); + try (final TopicAdminClient topicAdminClient = TopicAdminClient.create(topicAdminSettings)) { + final TopicName topicName = TopicName.of(TEST_PROJECT_ID, TEST_TOPIC); + topicAdminClient.createTopic(topicName); + } + } + + private static void createSubscription(final TransportChannelProvider channelProvider, + final NoCredentialsProvider credentialsProvider) throws IOException { + final SubscriptionAdminSettings subscriptionAdminSettings = SubscriptionAdminSettings.newBuilder() + .setTransportChannelProvider(channelProvider) + .setCredentialsProvider(credentialsProvider) + .build(); + + try (final SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create( + subscriptionAdminSettings)) { + final SubscriptionName subscriptionName = SubscriptionName.of(TEST_PROJECT_ID, TEST_SUBSCRIPTION); + subscriptionAdminClient.createSubscription(subscriptionName, TopicName.of(TEST_PROJECT_ID, TEST_TOPIC), + PushConfig.getDefaultInstance(), 10); + } + } + + static void setUpEmulator(final PubSubEmulatorContainer emulator) { + final String endpoint = emulator.getEmulatorEndpoint(); + final ManagedChannel channel = ManagedChannelBuilder.forTarget(endpoint).usePlaintext().build(); + try { + final TransportChannelProvider channelProvider = + FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel)); + final NoCredentialsProvider credentialsProvider = NoCredentialsProvider.create(); + + createTopic(channelProvider, credentialsProvider); + createSubscription(channelProvider, credentialsProvider); + } catch (final IOException e) { + throw new RuntimeException(e); + } finally { + channel.shutdown(); + } + } + } +} \ No newline at end of file diff --git a/tests/features/pom.xml b/tests/features/pom.xml index 746daae2..317ff48b 100644 --- a/tests/features/pom.xml +++ b/tests/features/pom.xml @@ -48,6 +48,7 @@ <module>camel-elasticsearch</module> <module>camel-fastjson</module> <module>camel-ftp</module> + <module>camel-google-pubsub</module> <module>camel-gson</module> <module>camel-hazelcast</module> <module>camel-http</module>