|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.dataflow; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertWithMessage; |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | + |
| 22 | +import com.google.cloud.pubsub.v1.AckReplyConsumer; |
| 23 | +import com.google.cloud.pubsub.v1.MessageReceiver; |
| 24 | +import com.google.cloud.pubsub.v1.Subscriber; |
| 25 | +import com.google.cloud.pubsub.v1.SubscriptionAdminClient; |
| 26 | +import com.google.cloud.pubsub.v1.TopicAdminClient; |
| 27 | +import com.google.pubsub.v1.ProjectSubscriptionName; |
| 28 | +import com.google.pubsub.v1.PubsubMessage; |
| 29 | +import com.google.pubsub.v1.PushConfig; |
| 30 | +import com.google.pubsub.v1.SubscriptionName; |
| 31 | +import com.google.pubsub.v1.TopicName; |
| 32 | +import java.io.ByteArrayOutputStream; |
| 33 | +import java.io.PrintStream; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.UUID; |
| 36 | +import java.util.concurrent.ConcurrentHashMap; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | +import java.util.concurrent.TimeoutException; |
| 39 | +import org.junit.After; |
| 40 | +import org.junit.Before; |
| 41 | +import org.junit.Test; |
| 42 | +import org.junit.runner.RunWith; |
| 43 | +import org.junit.runners.JUnit4; |
| 44 | + |
| 45 | +@RunWith(JUnit4.class) |
| 46 | +public class PubSubWriteIT { |
| 47 | + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 48 | + |
| 49 | + private ByteArrayOutputStream bout; |
| 50 | + private PrintStream out; |
| 51 | + private String topicId; |
| 52 | + private String subscriptionId; |
| 53 | + TopicAdminClient topicAdminClient; |
| 54 | + SubscriptionAdminClient subscriptionAdminClient; |
| 55 | + |
| 56 | + // Check if the required environment variables are set. |
| 57 | + public static void requireEnvVar(String envVarName) { |
| 58 | + assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) |
| 59 | + .that(System.getenv(envVarName)).isNotEmpty(); |
| 60 | + } |
| 61 | + |
| 62 | + @Before |
| 63 | + public void setUp() throws Exception { |
| 64 | + requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 65 | + |
| 66 | + bout = new ByteArrayOutputStream(); |
| 67 | + out = new PrintStream(bout); |
| 68 | + System.setOut(out); |
| 69 | + |
| 70 | + topicId = "test_topic_" + UUID.randomUUID().toString().substring(0, 8); |
| 71 | + subscriptionId = topicId + "-sub"; |
| 72 | + |
| 73 | + TopicName topicName = TopicName.of(PROJECT_ID, topicId); |
| 74 | + topicAdminClient = TopicAdminClient.create(); |
| 75 | + topicAdminClient.createTopic(topicName); |
| 76 | + |
| 77 | + SubscriptionName subscriptionName = SubscriptionName.of(PROJECT_ID, subscriptionId); |
| 78 | + subscriptionAdminClient = SubscriptionAdminClient.create(); |
| 79 | + subscriptionAdminClient.createSubscription(subscriptionName, topicName, |
| 80 | + PushConfig.getDefaultInstance(), 120); |
| 81 | + } |
| 82 | + |
| 83 | + @After |
| 84 | + public void tearDown() { |
| 85 | + subscriptionAdminClient.deleteSubscription(SubscriptionName.of(PROJECT_ID, subscriptionId)); |
| 86 | + topicAdminClient.deleteTopic(TopicName.of(PROJECT_ID, topicId)); |
| 87 | + System.setOut(null); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testPubSubWriteWithAttributes() throws Exception { |
| 92 | + |
| 93 | + Map<String, PubsubMessage> messages = new ConcurrentHashMap<>(); |
| 94 | + |
| 95 | + PubSubWriteWithAttributes.main( |
| 96 | + new String[] { |
| 97 | + "--runner=DirectRunner", |
| 98 | + "--topic=" + String.format("projects/%s/topics/%s", PROJECT_ID, topicId) |
| 99 | + }); |
| 100 | + |
| 101 | + MessageReceiver receiver = |
| 102 | + (PubsubMessage message, AckReplyConsumer consumer) -> { |
| 103 | + // Store in a map by message ID, which are guaranteed to be unique within a topic. |
| 104 | + messages.put(message.getMessageId(), message); |
| 105 | + consumer.ack(); |
| 106 | + }; |
| 107 | + |
| 108 | + // Verify that the pipeline wrote messages to Pub/Sub |
| 109 | + Subscriber subscriber = null; |
| 110 | + try { |
| 111 | + ProjectSubscriptionName subscriptionName = |
| 112 | + ProjectSubscriptionName.of(PROJECT_ID, subscriptionId); |
| 113 | + |
| 114 | + subscriber = Subscriber.newBuilder(subscriptionName, receiver).build(); |
| 115 | + subscriber.startAsync().awaitRunning(); |
| 116 | + subscriber.awaitTerminated(30, TimeUnit.SECONDS); |
| 117 | + } catch (TimeoutException timeoutException) { |
| 118 | + subscriber.stopAsync(); |
| 119 | + } |
| 120 | + assertEquals(4, messages.size()); |
| 121 | + for (Map.Entry<String, PubsubMessage> item : messages.entrySet()) { |
| 122 | + assertEquals(2, item.getValue().getAttributesCount()); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments