[ 
https://issues.apache.org/jira/browse/BEAM-11411?focusedWorklogId=535345&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-535345
 ]

ASF GitHub Bot logged work on BEAM-11411:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 13/Jan/21 12:23
            Start Date: 13/Jan/21 12:23
    Worklog Time Spent: 10m 
      Work Description: ramazan-yapparov commented on a change in pull request 
#13636:
URL: https://github.com/apache/beam/pull/13636#discussion_r556481215



##########
File path: 
examples/java/src/test/java/org/apache/beam/examples/complete/kafkatopubsub/KafkaToPubsubE2ETest.java
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.beam.examples.complete.kafkatopubsub;
+
+import static 
org.apache.beam.examples.complete.kafkatopubsub.transforms.FormatTransform.readFromKafka;
+
+import com.google.auth.Credentials;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Supplier;
+import org.apache.beam.examples.complete.kafkatopubsub.utils.RunKafkaContainer;
+import org.apache.beam.runners.direct.DirectOptions;
+import org.apache.beam.sdk.PipelineResult;
+import org.apache.beam.sdk.extensions.gcp.auth.NoopCredentialFactory;
+import org.apache.beam.sdk.extensions.gcp.options.GcpOptions;
+import org.apache.beam.sdk.io.gcp.pubsub.PubsubClient;
+import org.apache.beam.sdk.io.gcp.pubsub.PubsubIO;
+import org.apache.beam.sdk.io.gcp.pubsub.PubsubJsonClient;
+import org.apache.beam.sdk.io.gcp.pubsub.PubsubOptions;
+import org.apache.beam.sdk.io.gcp.pubsub.TestPubsubSignal;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.transforms.Values;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.joda.time.Duration;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.testcontainers.containers.PubSubEmulatorContainer;
+import org.testcontainers.utility.DockerImageName;
+
+/** E2E test for {@link KafkaToPubsub} pipeline. */
+public class KafkaToPubsubE2ETest {
+
+  @Rule public final transient TestPipeline pipeline = 
TestPipeline.fromOptions(OPTIONS);
+  @Rule public transient TestPubsubSignal signal = 
TestPubsubSignal.fromOptions(OPTIONS);
+
+  private static final String PUBSUB_EMULATOR_IMAGE =
+      "gcr.io/google.com/cloudsdktool/cloud-sdk:316.0.0-emulators";
+  private static final String PUBSUB_MESSAGE = "test pubsub message";
+  private static final String PROJECT_ID = "try-kafka-pubsub";
+  private static final String TOPIC_NAME = "listen-to-kafka";
+  private static final PubsubClient.TopicPath TOPIC_PATH =
+      PubsubClient.topicPathFromName(PROJECT_ID, TOPIC_NAME);
+  private static final PipelineOptions OPTIONS = 
TestPipeline.testingPipelineOptions();
+
+  @BeforeClass
+  public static void beforeClass() throws Exception {
+    Credentials credentials = 
NoopCredentialFactory.fromOptions(OPTIONS).getCredential();
+    OPTIONS.as(GcpOptions.class).setGcpCredential(credentials);
+    OPTIONS.as(GcpOptions.class).setProject(PROJECT_ID);
+    setupPubsubContainer(OPTIONS.as(PubsubOptions.class));
+    createPubsubTopicForTest(OPTIONS.as(PubsubOptions.class));
+  }
+
+  @Test
+  public void testKafkaToPubsubE2E() throws IOException {
+    pipeline.getOptions().as(DirectOptions.class).setBlockOnRun(false);
+
+    RunKafkaContainer rkc = new RunKafkaContainer(PUBSUB_MESSAGE);
+    String bootstrapServer = rkc.getBootstrapServer();
+    String[] kafkaTopicsList = new String[] {rkc.getTopicName()};
+
+    String pubsubTopicPath = TOPIC_PATH.getPath();
+
+    Map<String, Object> kafkaConfig = new HashMap<>();
+    Map<String, String> sslConfig = new HashMap<>();
+
+    PCollection<KV<String, String>> readStrings =
+        pipeline.apply(
+            "readFromKafka",
+            readFromKafka(bootstrapServer, Arrays.asList(kafkaTopicsList), 
kafkaConfig, sslConfig));
+
+    PCollection<String> readFromPubsub =
+        readStrings
+            .apply(Values.create())
+            .apply("writeToPubSub", 
PubsubIO.writeStrings().to(pubsubTopicPath))
+            .getPipeline()
+            .apply("readFromPubsub", 
PubsubIO.readStrings().fromTopic(pubsubTopicPath));
+
+    readFromPubsub.apply(
+        "waitForTestMessage",
+        signal.signalSuccessWhen(
+            readFromPubsub.getCoder(),
+            input -> {
+              if (input == null) {
+                return false;
+              }
+              return input.stream().anyMatch(message -> 
Objects.equals(message, PUBSUB_MESSAGE));
+            }));
+
+    Supplier<Void> start = signal.waitForStart(Duration.standardSeconds(10));
+    pipeline.apply(signal.signalStart());
+    PipelineResult job = pipeline.run();
+    start.get();
+    signal.waitForSuccess(Duration.standardMinutes(2));
+    try {
+      job.cancel();
+    } catch (IOException | UnsupportedOperationException e) {
+      throw new AssertionError("Could not stop pipeline.", e);
+    }
+  }
+
+  private static void setupPubsubContainer(PubsubOptions options) {
+    PubSubEmulatorContainer emulator =
+        new 
PubSubEmulatorContainer(DockerImageName.parse(PUBSUB_EMULATOR_IMAGE));
+    emulator.start();
+    String pubsubUrl = emulator.getEmulatorEndpoint();
+    options.setPubsubRootUrl("http://"; + pubsubUrl);
+  }
+
+  private static void createPubsubTopicForTest(PubsubOptions options) {
+    try {
+      PubsubClient pubsubClient = PubsubJsonClient.FACTORY.newClient(null, 
null, options);
+      pubsubClient.createTopic(TOPIC_PATH);
+    } catch (Exception e) {
+      throw new RuntimeException(e);

Review comment:
       Thank you for the suggestion, changed to using `TestPubsub`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 535345)
    Time Spent: 50m  (was: 40m)

> Example of E2E test for Pub/Sub environment
> -------------------------------------------
>
>                 Key: BEAM-11411
>                 URL: https://issues.apache.org/jira/browse/BEAM-11411
>             Project: Beam
>          Issue Type: Improvement
>          Components: examples-java
>            Reporter: Ilya Kozyrev
>            Priority: P2
>          Time Spent: 50m
>  Remaining Estimate: 0h
>
> Kafka to Pub/Sub pipeline [example|https://github.com/apache/beam/pull/13112] 
> needs to have e2e tests. However, in Beam Testing Guide 
> [https://cwiki.apache.org/confluence/display/BEAM/Contribution+Testing+Guide] 
> , we have not found examples or documentation for e2e tests that require a 
> Pub/Sub environment can be set up.
> During the investigation and with the help of our reviewers we found possible 
> approaches for our issue. 
> For Pub/Sub, e2e test could use [Pub/Sub 
> emulator|https://cloud.google.com/pubsub/docs/emulator]
> These e2e test examples can be referenced in the 
> [https://cwiki.apache.org/confluence/display/BEAM/Contribution+Testing+Guide] 
> to serve examples of how customers may approach e2e testing of pipelines in 
> their environments.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to