|
| 1 | +/* |
| 2 | + * Copyright 2021 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 demo; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static junit.framework.TestCase.assertNotNull; |
| 21 | + |
| 22 | +import com.google.api.gax.rpc.AlreadyExistsException; |
| 23 | +import com.google.cloud.pubsub.v1.SubscriptionAdminClient; |
| 24 | +import com.google.cloud.pubsub.v1.TopicAdminClient; |
| 25 | +import com.google.pubsub.v1.ProjectSubscriptionName; |
| 26 | +import com.google.pubsub.v1.Subscription; |
| 27 | +import com.google.pubsub.v1.TopicName; |
| 28 | +import java.io.ByteArrayOutputStream; |
| 29 | +import java.io.PrintStream; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.BeforeClass; |
| 34 | +import org.junit.Rule; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.rules.Timeout; |
| 37 | + |
| 38 | +public class PubSubApplicationIT { |
| 39 | + private ByteArrayOutputStream bout; |
| 40 | + private PrintStream out; |
| 41 | + |
| 42 | + private static final String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 43 | + private static final String topicOneId = "topic-one"; |
| 44 | + private static final String topicTwoId = "topic-two"; |
| 45 | + private static final String subscriptionOneId = "sub-one"; |
| 46 | + private static final String subscriptionTwoId = "sub-two"; |
| 47 | + |
| 48 | + private static void requireEnvVar(String varName) { |
| 49 | + assertNotNull( |
| 50 | + "Environment variable " + varName + " is required to perform these tests.", |
| 51 | + System.getenv(varName)); |
| 52 | + } |
| 53 | + |
| 54 | + @Rule public Timeout globalTimeout = Timeout.seconds(600); // 10 minute timeout |
| 55 | + |
| 56 | + @BeforeClass |
| 57 | + public static void checkRequirements() { |
| 58 | + requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 59 | + } |
| 60 | + |
| 61 | + @Before |
| 62 | + public void setUp() throws Exception { |
| 63 | + bout = new ByteArrayOutputStream(); |
| 64 | + out = new PrintStream(bout); |
| 65 | + System.setOut(out); |
| 66 | + |
| 67 | + try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) { |
| 68 | + try { |
| 69 | + topicAdminClient.createTopic(TopicName.of(projectId, topicOneId)); |
| 70 | + topicAdminClient.createTopic(TopicName.of(projectId, topicTwoId)); |
| 71 | + } catch (AlreadyExistsException ignore) { |
| 72 | + System.out.println("Using existing topics."); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) { |
| 77 | + Subscription subscriptionOne = |
| 78 | + Subscription.newBuilder() |
| 79 | + .setName(String.valueOf(ProjectSubscriptionName.of(projectId, subscriptionOneId))) |
| 80 | + .setTopic(String.valueOf(TopicName.of(projectId, topicOneId))) |
| 81 | + .build(); |
| 82 | + Subscription subscriptionTwo = |
| 83 | + Subscription.newBuilder() |
| 84 | + .setName(String.valueOf(ProjectSubscriptionName.of(projectId, subscriptionTwoId))) |
| 85 | + .setTopic(String.valueOf(TopicName.of(projectId, topicTwoId))) |
| 86 | + .build(); |
| 87 | + |
| 88 | + try { |
| 89 | + subscriptionAdminClient.createSubscription(subscriptionOne); |
| 90 | + subscriptionAdminClient.createSubscription(subscriptionTwo); |
| 91 | + } catch (AlreadyExistsException ignore) { |
| 92 | + System.out.println("Using existing subscriptions"); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @After |
| 98 | + public void tearDown() { |
| 99 | + // No need to clean up these pairs of topics and subscriptions. |
| 100 | + System.setOut(null); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testPubSubApplication() throws Exception { |
| 105 | + bout.reset(); |
| 106 | + |
| 107 | + demo.PubSubApplication.main(new String[] {}); |
| 108 | + |
| 109 | + TimeUnit.MINUTES.sleep(1); |
| 110 | + |
| 111 | + assertThat(bout.toString()).contains("Started PubSubApplication"); |
| 112 | + assertThat(bout.toString()).contains("Sending a message via the output binder to topic-one!"); |
| 113 | + assertThat(bout.toString()) |
| 114 | + .contains("Message arrived via an inbound channel adapter from sub-one!"); |
| 115 | + assertThat(bout.toString()) |
| 116 | + .contains("Message was sent via the outbound channel adapter to topic-two!"); |
| 117 | + assertThat(bout.toString()).contains("Message arrived via an input binder from topic-two!"); |
| 118 | + } |
| 119 | +} |
0 commit comments