From f94ce7bf71a032b746975340377a6dfe097feeae Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Wed, 22 Feb 2017 14:48:11 -0800 Subject: [PATCH 1/9] adding snippets for pubsub v0.9.2-alpha --- .../snippets/PublisherClientSnippets.java | 141 +++++++++++++ .../pubsub/snippets/PublisherSnippets.java | 34 ++-- .../snippets/SubscriberClientSnippets.java | 186 ++++++++++++++++++ 3 files changed, 346 insertions(+), 15 deletions(-) create mode 100644 google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java create mode 100644 google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java new file mode 100644 index 000000000000..578aa94e684d --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java @@ -0,0 +1,141 @@ +/* + * Copyright 2017 Google Inc. All Rights Reserved. + * + * 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 com.google.cloud.examples.pubsub.snippets; + +import com.google.cloud.Identity; +import com.google.cloud.Role; +import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers; +import com.google.cloud.pubsub.spi.v1.PublisherClient; +import com.google.iam.v1.Binding; +import com.google.iam.v1.Policy; +import com.google.iam.v1.TestIamPermissionsResponse; +import com.google.pubsub.v1.ListTopicsRequest; +import com.google.pubsub.v1.ProjectName; +import com.google.pubsub.v1.Topic; +import com.google.pubsub.v1.TopicName; +import java.util.LinkedList; +import java.util.List; + +/** This class contains a number of snippets for the {@link PublisherClient} interface. */ +public class PublisherClientSnippets { + + private final String projectId; + + public PublisherClientSnippets(String projectId) { + this.projectId = projectId; + } + + /** Example of creating a topic. */ + // [TARGET createTopic(TopicName)] + // [VARIABLE "my_topic_name"] + public Topic createTopic(String name) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + // [START createTopic] + TopicName topicName = TopicName.create(projectId, name); + Topic topic = publisherClient.createTopic(topicName); + // [END createTopic] + return topic; + } + } + + /** Example of listing topics, specifying the page size. */ + // [TARGET listTopics(int)] + public PagedResponseWrappers.ListTopicsPagedResponse listTopics() throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + // [START listTopics] + ListTopicsRequest listTopicsRequest = + ListTopicsRequest.newBuilder() + .setProjectWithProjectName(ProjectName.create(projectId)) + .setPageSize(100) + .build(); + PagedResponseWrappers.ListTopicsPagedResponse response = + publisherClient.listTopics(listTopicsRequest); + Iterable topics = response.iterateAllElements(); + for (Topic topic : topics) { + // do something with the topic + } + // [END listTopics] + return response; + } + } + + /** Example of deleting a topic. */ + // [TARGET deleteTopic(TopicName)] + // [VARIABLE "my_topic_name"] + public void deleteTopic(String name) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + TopicName topicName = TopicName.create(projectId, name); + // [START deleteTopic] + publisherClient.deleteTopic(topicName); + // [END deleteTopic] + } + } + + /** Example of getting a topic policy. */ + // [TARGET getTopicPolicy(TopicName)] + // [VARIABLE "my_topic_name"] + public Policy getTopicPolicy(String name) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + TopicName topicName = TopicName.create(projectId, name); + // [START getTopicPolicy] + Policy policy = publisherClient.getIamPolicy(topicName.toString()); + if (policy == null) { + // topic iam policy was not found + } + // [END getTopicPolicy] + return policy; + } + } + + /** Example of replacing a topic policy. */ + // [TARGET replaceTopicPolicy(TopicName)] + // [VARIABLE "my_topic_name"] + public Policy replaceTopicPolicy(String name) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + String topicName = TopicName.create(projectId, name).toString(); + // [START replaceTopicPolicy] + Policy policy = publisherClient.getIamPolicy(topicName); + // add role -> members binding + Binding binding = + Binding.newBuilder() + .setRole(Role.viewer().toString()) + .addMembers(Identity.allAuthenticatedUsers().toString()) + .build(); + // create updated policy + Policy updatedPolicy = Policy.newBuilder(policy).addBindings(binding).build(); + updatedPolicy = publisherClient.setIamPolicy(topicName, updatedPolicy); + // [END replaceTopicPolicy] + return updatedPolicy; + } + } + + /** Example of testing whether the caller has the provided permissions on a topic. */ + // [TARGET testTopicPermissions(TopicName)] + // [VARIABLE "my_topic_name"] + public TestIamPermissionsResponse testTopicPermissions(String name) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + TopicName topicName = TopicName.create(projectId, name); + // [START testTopicPermissions] + List permissions = new LinkedList<>(); + permissions.add("pubsub.topics.get"); + TestIamPermissionsResponse testedPermissions = + publisherClient.testIamPermissions(topicName.toString(), permissions); + // [END testTopicPermissions] + return testedPermissions; + } + } +} diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherSnippets.java index 7b8d5acf8fc8..9f10fd08e9f7 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherSnippets.java @@ -14,7 +14,13 @@ * limitations under the License. */ -package com.google.cloud.examples.pubsub; +/* + * EDITING INSTRUCTIONS + * This file is referenced in {@link Publisher} javadoc. + * Any change to this file should be reflected in PubSub's javadoc. + */ + +package com.google.cloud.examples.pubsub.snippets; import com.google.api.gax.core.RpcFuture; import com.google.api.gax.core.RpcFutureCallback; @@ -23,6 +29,7 @@ import com.google.pubsub.v1.PubsubMessage; import com.google.pubsub.v1.TopicName; +/** This class contains a number of snippets for the {@link Publisher} interface. */ public class PublisherSnippets { private final Publisher publisher; @@ -30,9 +37,7 @@ public PublisherSnippets(Publisher publisher) { this.publisher = publisher; } - /** - * Example of publishing a message. - */ + /** Example of publishing a message. */ // [TARGET publish(PubsubMessage)] // [VARIABLE "my_message"] public void publish(String message) { @@ -40,21 +45,20 @@ public void publish(String message) { ByteString data = ByteString.copyFromUtf8(message); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); RpcFuture messageIdFuture = publisher.publish(pubsubMessage); - messageIdFuture.addCallback(new RpcFutureCallback() { - public void onSuccess(String messageId) { - System.out.println("published with message id: " + messageId); - } + messageIdFuture.addCallback( + new RpcFutureCallback() { + public void onSuccess(String messageId) { + System.out.println("published with message id: " + messageId); + } - public void onFailure(Throwable t) { - System.out.println("failed to publish: " + t); - } - }); + public void onFailure(Throwable t) { + System.out.println("failed to publish: " + t); + } + }); // [END publish] } - /** - * Example of creating a {@code Publisher}. - */ + /** Example of creating a {@code Publisher}. */ // [TARGET newBuilder(TopicName)] // [VARIABLE "my_project"] // [VARIABLE "my_topic"] diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java new file mode 100644 index 000000000000..79c76ea2d96a --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java @@ -0,0 +1,186 @@ +/* + * Copyright 2017 Google Inc. All Rights Reserved. + * + * 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. + */ + +/** This class contains snippets for the {@link SubscriberClient} interface. */ + +package com.google.cloud.examples.pubsub.snippets; + +import com.google.cloud.Identity; +import com.google.cloud.Role; +import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers; +import com.google.cloud.pubsub.spi.v1.PublisherClient; +import com.google.cloud.pubsub.spi.v1.SubscriberClient; +import com.google.iam.v1.Binding; +import com.google.iam.v1.Policy; +import com.google.iam.v1.TestIamPermissionsResponse; +import com.google.pubsub.v1.ListSubscriptionsRequest; +import com.google.pubsub.v1.ProjectName; +import com.google.pubsub.v1.PullResponse; +import com.google.pubsub.v1.PushConfig; +import com.google.pubsub.v1.ReceivedMessage; +import com.google.pubsub.v1.Subscription; +import com.google.pubsub.v1.SubscriptionName; +import com.google.pubsub.v1.TopicName; +import edu.emory.mathcs.backport.java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +/** This class contains a number of snippets for the {@link SubscriberClient} interface. */ +public class SubscriberClientSnippets { + + private final String projectId; + + public SubscriberClientSnippets(String projectId) { + this.projectId = projectId; + } + + /** Example of creating a pull subscription for a topic. */ + // [TARGET createSubscription(String, String)] + // [VARIABLE "my_topic_name"] + // [VARIABLE "my_subscription_name"] + public Subscription createSubscription(String topic, String subscriptionId) throws Exception { + TopicName topicName = TopicName.create(projectId, topic); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); + try (SubscriberClient subscriberClient = SubscriberClient.create()) { + // [START createSubscription] + Subscription subscription = + subscriberClient.createSubscription( + subscriptionName, topicName, PushConfig.getDefaultInstance(), 0); + // [END createSubscription] + return subscription; + } + } + + /** Example of pulling a maximum number of messages from a subscription. */ + // [TARGET pull(String)] + // [VARIABLE "my_subscription_name"] + public void pull(String subscriptionId) throws Exception { + try (SubscriberClient subscriberClient = SubscriberClient.create()) { + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); + // [START pull] + PullResponse response = subscriberClient.pull(subscriptionName, true, 100); + for (ReceivedMessage message : response.getReceivedMessagesList()) { + // do something with message, then ack or nack + subscriberClient.acknowledge( + subscriptionName, Collections.singletonList(message.getAckId())); + } + // [END pull] + } + } + + /** Example of replacing the push configuration of a subscription, setting the push endpoint. */ + // [TARGET replacePushConfig(String, String)] + // [VARIABLE "my_subscription_name"] + // [VARIABLE "https://www.example.com/push"] + public void replacePushConfig(String subscriptionId, String endpoint) throws Exception { + try (SubscriberClient subscriberClient = SubscriberClient.create()) { + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); + // [START replacePushConfig] + PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build(); + subscriberClient.modifyPushConfig(subscriptionName, pushConfig); + // [END replacePushConfig] + } + } + + /** Example of listing subscriptions, specifying the page size. */ + // [TARGET listSubscriptions()] + public PagedResponseWrappers.ListSubscriptionsPagedResponse listSubscriptions() throws Exception { + try (SubscriberClient subscriberClient = SubscriberClient.create()) { + ProjectName projectName = ProjectName.create(projectId); + // [START listSubscriptions] + ListSubscriptionsRequest listSubscriptionsRequest = + ListSubscriptionsRequest.newBuilder() + .setProjectWithProjectName(projectName) + .setPageSize(100) + .build(); + PagedResponseWrappers.ListSubscriptionsPagedResponse response = + subscriberClient.listSubscriptions(listSubscriptionsRequest); + Iterable subscriptions = response.iterateAllElements(); + for (Subscription subscription : subscriptions) { + // do something with the subscription + } + // [END listSubscriptions] + return response; + } + } + + /** Example of deleting a subscription. */ + // [TARGET deleteSubscription(String)] + // [VARIABLE "my_subscription_name"] + public void deleteSubscription(String subscriptionId) throws Exception { + try (SubscriberClient subscriberClient = SubscriberClient.create()) { + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); + // [START deleteSubscription] + subscriberClient.deleteSubscription(subscriptionName); + // [END deleteSubscription] + } + } + + /** Example of getting a subscription policy. */ + // [TARGET getSubscriptionPolicy(String)] + // [VARIABLE "my_subscription_name"] + public Policy getSubscriptionPolicy(String subscriptionId) throws Exception { + try (SubscriberClient subscriberClient = SubscriberClient.create()) { + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); + // [START getSubscriptionPolicy] + Policy policy = subscriberClient.getIamPolicy(subscriptionName.toString()); + if (policy == null) { + // subscription was not found + } + // [END getSubscriptionPolicy] + return policy; + } + } + + /** Example of replacing a subscription policy. */ + // [TARGET replaceSubscriptionPolicy(String)] + // [VARIABLE "my_subscription_name"] + public Policy replaceSubscriptionPolicy(String subscriptionId) throws Exception { + try (SubscriberClient subscriberClient = SubscriberClient.create()) { + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); + // [START replaceSubscriptionPolicy] + Policy policy = subscriberClient.getIamPolicy(subscriptionName.toString()); + // Create a role => members binding + Binding binding = + Binding.newBuilder() + .setRole(Role.viewer().toString()) + .addMembers(Identity.allAuthenticatedUsers().toString()) + .build(); + //Update policy + Policy updatedPolicy = policy.toBuilder().addBindings(binding).build(); + + updatedPolicy = subscriberClient.setIamPolicy(subscriptionName.toString(), updatedPolicy); + // [END replaceSubscriptionPolicy] + return updatedPolicy; + } + } + + /** Example of testing whether the caller has the provided permissions on a subscription. */ + // [TARGET testSubscriptionPermissions(String)] + // [VARIABLE "my_subscription_name"] + public TestIamPermissionsResponse testSubscriptionPermissions(String name) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + TopicName topicName = TopicName.create(projectId, name); + // [START testSubscriptionPermissions] + List permissions = new LinkedList<>(); + permissions.add("pubsub.subscriptions.get"); + TestIamPermissionsResponse testedPermissions = + publisherClient.testIamPermissions(topicName.toString(), permissions); + // [END testSubscriptionPermissions] + return testedPermissions; + } + } +} From dc21583e4a7872539ed27109b362a5c468b43254 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 23 Feb 2017 14:00:06 -0800 Subject: [PATCH 2/9] pubsub snippets --- .../snippets/PublisherClientSnippets.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java index 578aa94e684d..4c8ad754bfaf 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java @@ -18,11 +18,14 @@ import com.google.cloud.Identity; import com.google.cloud.Role; -import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers; +import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse; +import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse; + import com.google.cloud.pubsub.spi.v1.PublisherClient; import com.google.iam.v1.Binding; import com.google.iam.v1.Policy; import com.google.iam.v1.TestIamPermissionsResponse; +import com.google.pubsub.v1.ListTopicSubscriptionsRequest; import com.google.pubsub.v1.ListTopicsRequest; import com.google.pubsub.v1.ProjectName; import com.google.pubsub.v1.Topic; @@ -52,9 +55,31 @@ public Topic createTopic(String name) throws Exception { } } + /** Example of listing topics, specifying the page size. */ + // [TARGET listTopicSubscriptions(int)] + public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String name) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + TopicName topicName = TopicName.create(projectId, name); + // [START listTopicSubscriptions] + ListTopicSubscriptionsRequest request = + ListTopicSubscriptionsRequest.newBuilder() + .setTopicWithTopicName(topicName) + .setPageSize(100) + .build(); + ListTopicSubscriptionsPagedResponse response = + publisherClient.listTopicSubscriptions(request); + Iterable subscriptions = response.iterateAllElements(); + for (String subscription : subscriptions) { + // do something with the subscription name + } + // [END listTopicSubscriptions] + return response; + } + } + /** Example of listing topics, specifying the page size. */ // [TARGET listTopics(int)] - public PagedResponseWrappers.ListTopicsPagedResponse listTopics() throws Exception { + public ListTopicsPagedResponse listTopics() throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START listTopics] ListTopicsRequest listTopicsRequest = @@ -62,7 +87,7 @@ public PagedResponseWrappers.ListTopicsPagedResponse listTopics() throws Excepti .setProjectWithProjectName(ProjectName.create(projectId)) .setPageSize(100) .build(); - PagedResponseWrappers.ListTopicsPagedResponse response = + ListTopicsPagedResponse response = publisherClient.listTopics(listTopicsRequest); Iterable topics = response.iterateAllElements(); for (Topic topic : topics) { From e653d3177b9e51a2da16b1c96ac7ebada604ab20 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Fri, 24 Feb 2017 16:05:20 -0800 Subject: [PATCH 3/9] adding tests for publisherclient, subscriberclient snippets --- .../snippets/PublisherClientSnippets.java | 122 ++++++---- .../snippets/SubscriberClientSnippets.java | 112 +++++---- .../examples/pubsub/snippets/Cleanup.java | 57 +++++ .../snippets/ITPublisherClientSnippets.java | 194 ++++++++++++++++ .../snippets/ITSubscriberClientSnippets.java | 217 ++++++++++++++++++ 5 files changed, 622 insertions(+), 80 deletions(-) create mode 100644 google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/Cleanup.java create mode 100644 google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java create mode 100644 google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java index 4c8ad754bfaf..8f671a126191 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java @@ -18,9 +18,8 @@ import com.google.cloud.Identity; import com.google.cloud.Role; -import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse; import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse; - +import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse; import com.google.cloud.pubsub.spi.v1.PublisherClient; import com.google.iam.v1.Binding; import com.google.iam.v1.Policy; @@ -43,27 +42,65 @@ public PublisherClientSnippets(String projectId) { } /** Example of creating a topic. */ - // [TARGET createTopic(TopicName)] - // [VARIABLE "my_topic_name"] - public Topic createTopic(String name) throws Exception { + public Topic createTopic(String topicName) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START createTopic] - TopicName topicName = TopicName.create(projectId, name); - Topic topic = publisherClient.createTopic(topicName); + TopicName formattedTopicName = TopicName.create(projectId, topicName); + Topic topic = publisherClient.createTopic(formattedTopicName); // [END createTopic] return topic; } } /** Example of listing topics, specifying the page size. */ - // [TARGET listTopicSubscriptions(int)] - public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String name) throws Exception { + public ListTopicsPagedResponse listTopics() throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - TopicName topicName = TopicName.create(projectId, name); + // [START listTopics] + ListTopicsRequest listTopicsRequest = + ListTopicsRequest.newBuilder() + .setProjectWithProjectName(ProjectName.create(projectId)) + .setPageSize(100) + .build(); + ListTopicsPagedResponse response = + publisherClient.listTopics(listTopicsRequest); + Iterable topics = response.iterateAllElements(); + for (Topic topic : topics) { + // do something with the topic + } + // [END listTopics] + return response; + } + } + + /** Example of listing topics, specifying the page size and a page token. */ + public ListTopicsPagedResponse listTopicsWithPageToken(String pageToken) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + // [START listTopicsWithPageToken] + ListTopicsRequest listTopicsRequest = + ListTopicsRequest.newBuilder() + .setProjectWithProjectName(ProjectName.create(projectId)) + .setPageSize(100) + .setPageToken(pageToken) + .build(); + ListTopicsPagedResponse response = + publisherClient.listTopics(listTopicsRequest); + Iterable topics = response.iterateAllElements(); + for (Topic topic : topics) { + // do something with the topic + } + // [END listTopicsWithPageToken] + return response; + } + } + + /** Example of listing topics for a subscription, specifying the page size. */ + public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicName) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + TopicName formattedTopicName = TopicName.create(projectId, topicName); // [START listTopicSubscriptions] ListTopicSubscriptionsRequest request = ListTopicSubscriptionsRequest.newBuilder() - .setTopicWithTopicName(topicName) + .setTopicWithTopicName(formattedTopicName) .setPageSize(100) .build(); ListTopicSubscriptionsPagedResponse response = @@ -77,42 +114,41 @@ public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String name) t } } - /** Example of listing topics, specifying the page size. */ - // [TARGET listTopics(int)] - public ListTopicsPagedResponse listTopics() throws Exception { + /** Example of listing topics for a subscription, specifying the page size and page token */ + public ListTopicSubscriptionsPagedResponse listTopicSubscriptionsWithPageToken( + String topicName, String pageToken) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - // [START listTopics] - ListTopicsRequest listTopicsRequest = - ListTopicsRequest.newBuilder() - .setProjectWithProjectName(ProjectName.create(projectId)) + TopicName formattedTopicName = TopicName.create(projectId, topicName); + // [START listTopicSubscriptionsWithPageToken] + ListTopicSubscriptionsRequest request = + ListTopicSubscriptionsRequest.newBuilder() + .setTopicWithTopicName(formattedTopicName) .setPageSize(100) + .setPageToken(pageToken) .build(); - ListTopicsPagedResponse response = - publisherClient.listTopics(listTopicsRequest); - Iterable topics = response.iterateAllElements(); - for (Topic topic : topics) { - // do something with the topic + ListTopicSubscriptionsPagedResponse response = + publisherClient.listTopicSubscriptions(request); + Iterable subscriptions = response.iterateAllElements(); + for (String subscription : subscriptions) { + // do something with the subscription name } - // [END listTopics] + // [END listTopicSubscriptionsWithPageToken] return response; } } /** Example of deleting a topic. */ - // [TARGET deleteTopic(TopicName)] - // [VARIABLE "my_topic_name"] - public void deleteTopic(String name) throws Exception { + public TopicName deleteTopic(String topicName) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - TopicName topicName = TopicName.create(projectId, name); + TopicName formattedTopicName = TopicName.create(projectId, topicName); // [START deleteTopic] - publisherClient.deleteTopic(topicName); + publisherClient.deleteTopic(formattedTopicName); // [END deleteTopic] + return formattedTopicName; } } /** Example of getting a topic policy. */ - // [TARGET getTopicPolicy(TopicName)] - // [VARIABLE "my_topic_name"] public Policy getTopicPolicy(String name) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { TopicName topicName = TopicName.create(projectId, name); @@ -127,8 +163,6 @@ public Policy getTopicPolicy(String name) throws Exception { } /** Example of replacing a topic policy. */ - // [TARGET replaceTopicPolicy(TopicName)] - // [VARIABLE "my_topic_name"] public Policy replaceTopicPolicy(String name) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { String topicName = TopicName.create(projectId, name).toString(); @@ -148,19 +182,29 @@ public Policy replaceTopicPolicy(String name) throws Exception { } } - /** Example of testing whether the caller has the provided permissions on a topic. */ - // [TARGET testTopicPermissions(TopicName)] - // [VARIABLE "my_topic_name"] - public TestIamPermissionsResponse testTopicPermissions(String name) throws Exception { + /** Example of testing whether the caller has the provided permissions on a topic. + * Only viewer, editor or admin/owner can view results of pubsub.topics.get */ + public TestIamPermissionsResponse testTopicPermissions(String topicName) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - TopicName topicName = TopicName.create(projectId, name); + TopicName formattedTopicName = TopicName.create(projectId, topicName); // [START testTopicPermissions] List permissions = new LinkedList<>(); permissions.add("pubsub.topics.get"); TestIamPermissionsResponse testedPermissions = - publisherClient.testIamPermissions(topicName.toString(), permissions); + publisherClient.testIamPermissions(formattedTopicName.toString(), permissions); // [END testTopicPermissions] return testedPermissions; } } + + /** Example of getting a topic. */ + public Topic getTopic(String topicName) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + // [START getTopic] + TopicName formattedTopicName = TopicName.create(projectId, topicName); + Topic topic = publisherClient.getTopic(formattedTopicName); + // [END createTopic] + return topic; + } + } } diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java index 79c76ea2d96a..03e45a27978f 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java @@ -21,6 +21,7 @@ import com.google.cloud.Identity; import com.google.cloud.Role; import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers; +import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListSubscriptionsPagedResponse; import com.google.cloud.pubsub.spi.v1.PublisherClient; import com.google.cloud.pubsub.spi.v1.SubscriberClient; import com.google.iam.v1.Binding; @@ -48,56 +49,51 @@ public SubscriberClientSnippets(String projectId) { } /** Example of creating a pull subscription for a topic. */ - // [TARGET createSubscription(String, String)] - // [VARIABLE "my_topic_name"] - // [VARIABLE "my_subscription_name"] - public Subscription createSubscription(String topic, String subscriptionId) throws Exception { + public Subscription createSubscription(String topic, String subscriptionName) throws Exception { TopicName topicName = TopicName.create(projectId, topic); - SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); + SubscriptionName formattedSubscriptionName = + SubscriptionName.create(projectId, subscriptionName); try (SubscriberClient subscriberClient = SubscriberClient.create()) { // [START createSubscription] Subscription subscription = subscriberClient.createSubscription( - subscriptionName, topicName, PushConfig.getDefaultInstance(), 0); + formattedSubscriptionName, topicName, PushConfig.getDefaultInstance(), 0); // [END createSubscription] return subscription; } } /** Example of pulling a maximum number of messages from a subscription. */ - // [TARGET pull(String)] - // [VARIABLE "my_subscription_name"] - public void pull(String subscriptionId) throws Exception { + public PullResponse pull(String subscriptionName) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { - SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); + SubscriptionName subcriptionFormattedName = + SubscriptionName.create(projectId, subscriptionName); // [START pull] - PullResponse response = subscriberClient.pull(subscriptionName, true, 100); + PullResponse response = subscriberClient.pull(subcriptionFormattedName, true, 100); for (ReceivedMessage message : response.getReceivedMessagesList()) { // do something with message, then ack or nack subscriberClient.acknowledge( - subscriptionName, Collections.singletonList(message.getAckId())); + subcriptionFormattedName, Collections.singletonList(message.getAckId())); } // [END pull] + return response; } } /** Example of replacing the push configuration of a subscription, setting the push endpoint. */ - // [TARGET replacePushConfig(String, String)] - // [VARIABLE "my_subscription_name"] - // [VARIABLE "https://www.example.com/push"] - public void replacePushConfig(String subscriptionId, String endpoint) throws Exception { + public void replacePushConfig(String subscriptionName, String endpoint) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { - SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); + SubscriptionName formattedSubscriptionName = + SubscriptionName.create(projectId, subscriptionName); // [START replacePushConfig] PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build(); - subscriberClient.modifyPushConfig(subscriptionName, pushConfig); + subscriberClient.modifyPushConfig(formattedSubscriptionName, pushConfig); // [END replacePushConfig] } } /** Example of listing subscriptions, specifying the page size. */ - // [TARGET listSubscriptions()] - public PagedResponseWrappers.ListSubscriptionsPagedResponse listSubscriptions() throws Exception { + public ListSubscriptionsPagedResponse listSubscriptions() throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { ProjectName projectName = ProjectName.create(projectId); // [START listSubscriptions] @@ -117,26 +113,48 @@ public PagedResponseWrappers.ListSubscriptionsPagedResponse listSubscriptions() } } + /** Example of listing subscriptions, specifying the page size and page token. */ + public ListSubscriptionsPagedResponse listSubscriptionsWithPageToken(String pageToken) + throws Exception { + try (SubscriberClient subscriberClient = SubscriberClient.create()) { + ProjectName projectName = ProjectName.create(projectId); + // [START listSubscriptionsWithPageToken] + ListSubscriptionsRequest listSubscriptionsRequest = + ListSubscriptionsRequest.newBuilder() + .setProjectWithProjectName(projectName) + .setPageSize(100) + .setPageToken(pageToken) + .build(); + PagedResponseWrappers.ListSubscriptionsPagedResponse response = + subscriberClient.listSubscriptions(listSubscriptionsRequest); + Iterable subscriptions = response.iterateAllElements(); + for (Subscription subscription : subscriptions) { + // do something with the subscription + } + // [END listSubscriptionsWithPageToken] + return response; + } + } + /** Example of deleting a subscription. */ - // [TARGET deleteSubscription(String)] - // [VARIABLE "my_subscription_name"] - public void deleteSubscription(String subscriptionId) throws Exception { + public SubscriptionName deleteSubscription(String subscriptionName) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { - SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); + SubscriptionName formattedSubscriptionName = + SubscriptionName.create(projectId, subscriptionName); // [START deleteSubscription] - subscriberClient.deleteSubscription(subscriptionName); + subscriberClient.deleteSubscription(formattedSubscriptionName); // [END deleteSubscription] + return formattedSubscriptionName; } } /** Example of getting a subscription policy. */ - // [TARGET getSubscriptionPolicy(String)] - // [VARIABLE "my_subscription_name"] - public Policy getSubscriptionPolicy(String subscriptionId) throws Exception { + public Policy getSubscriptionPolicy(String subscriptionName) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { - SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); + SubscriptionName formattedSubscriptionName = + SubscriptionName.create(projectId, subscriptionName); // [START getSubscriptionPolicy] - Policy policy = subscriberClient.getIamPolicy(subscriptionName.toString()); + Policy policy = subscriberClient.getIamPolicy(formattedSubscriptionName.toString()); if (policy == null) { // subscription was not found } @@ -146,13 +164,12 @@ public Policy getSubscriptionPolicy(String subscriptionId) throws Exception { } /** Example of replacing a subscription policy. */ - // [TARGET replaceSubscriptionPolicy(String)] - // [VARIABLE "my_subscription_name"] - public Policy replaceSubscriptionPolicy(String subscriptionId) throws Exception { + public Policy replaceSubscriptionPolicy(String subscriptionName) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { - SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); + SubscriptionName formattedSubscriptionName = + SubscriptionName.create(projectId, subscriptionName); // [START replaceSubscriptionPolicy] - Policy policy = subscriberClient.getIamPolicy(subscriptionName.toString()); + Policy policy = subscriberClient.getIamPolicy(formattedSubscriptionName.toString()); // Create a role => members binding Binding binding = Binding.newBuilder() @@ -162,25 +179,38 @@ public Policy replaceSubscriptionPolicy(String subscriptionId) throws Exception //Update policy Policy updatedPolicy = policy.toBuilder().addBindings(binding).build(); - updatedPolicy = subscriberClient.setIamPolicy(subscriptionName.toString(), updatedPolicy); + updatedPolicy = subscriberClient.setIamPolicy( + formattedSubscriptionName.toString(), updatedPolicy); // [END replaceSubscriptionPolicy] return updatedPolicy; } } /** Example of testing whether the caller has the provided permissions on a subscription. */ - // [TARGET testSubscriptionPermissions(String)] - // [VARIABLE "my_subscription_name"] - public TestIamPermissionsResponse testSubscriptionPermissions(String name) throws Exception { + public TestIamPermissionsResponse testSubscriptionPermissions(String subscriptionName) + throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - TopicName topicName = TopicName.create(projectId, name); + SubscriptionName formattedSubscriptionName = + SubscriptionName.create(projectId, subscriptionName); // [START testSubscriptionPermissions] List permissions = new LinkedList<>(); permissions.add("pubsub.subscriptions.get"); TestIamPermissionsResponse testedPermissions = - publisherClient.testIamPermissions(topicName.toString(), permissions); + publisherClient.testIamPermissions(formattedSubscriptionName.toString(), permissions); // [END testSubscriptionPermissions] return testedPermissions; } } + + /** Example of getting a subscription. */ + public Subscription getSubscription(String subscriptionName) throws Exception { + SubscriptionName formattedSubscriptionName = + SubscriptionName.create(projectId, subscriptionName); + try (SubscriberClient subscriberClient = SubscriberClient.create()) { + // [START getSubscription] + Subscription subscription = subscriberClient.getSubscription(formattedSubscriptionName); + // [END getSubscription] + return subscription; + } + } } diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/Cleanup.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/Cleanup.java new file mode 100644 index 000000000000..9e6e5de27560 --- /dev/null +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/Cleanup.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 Google Inc. All Rights Reserved. + * + * 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 com.google.cloud.examples.pubsub.snippets; + +import com.google.cloud.pubsub.spi.v1.PublisherClient; +import com.google.cloud.pubsub.spi.v1.SubscriberClient; +import com.google.pubsub.v1.SubscriptionName; +import com.google.pubsub.v1.TopicName; + +class Cleanup { + + protected static void deleteTestTopicsAndSubscriptions( + String projectId, String[] topics, String[] subscriptions) throws Exception { + deleteTestTopics(projectId, topics); + deleteTestSubscriptions(projectId, subscriptions); + } + + private static void deleteTestTopics(String projectId, String[] testTopics) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + for (String testTopic : testTopics) { + try { + publisherClient.deleteTopic(TopicName.create(projectId, testTopic)); + } catch (Exception e) { + //do nothing catch clause + } + } + } + } + + private static void deleteTestSubscriptions(String projectId, String[] subscriptions) + throws Exception { + try (SubscriberClient subscriberClient = SubscriberClient.create()) { + for (String name : subscriptions) { + try { + subscriberClient.deleteSubscription( + SubscriptionName.create(projectId, name)); + } catch (Exception e) { + //do nothing catch clause + } + } + } + } +} diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java new file mode 100644 index 000000000000..26a5e10c0d46 --- /dev/null +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java @@ -0,0 +1,194 @@ +/* + * Copyright 2017 Google Inc. All Rights Reserved. + * + * 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 com.google.cloud.examples.pubsub.snippets; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.api.gax.grpc.ApiException; +import com.google.cloud.Identity; +import com.google.cloud.Role; +import com.google.cloud.ServiceOptions; +import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse; +import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse; +import com.google.cloud.pubsub.spi.v1.SubscriberClient; +import com.google.common.collect.Iterables; +import com.google.iam.v1.Policy; +import com.google.iam.v1.TestIamPermissionsResponse; +import com.google.pubsub.v1.PushConfig; +import com.google.pubsub.v1.Subscription; +import com.google.pubsub.v1.SubscriptionName; +import com.google.pubsub.v1.Topic; +import com.google.pubsub.v1.TopicName; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; + +public class ITPublisherClientSnippets { + + private static final String NAME_SUFFIX = UUID.randomUUID().toString(); + + private static String projectId; + + private static PublisherClientSnippets publisherClientSnippets; + + private static String[] topics = { + formatForTest("topic-1"), + formatForTest("topic-2"), + }; + private static String[] subscriptions = { + formatForTest("subscription-1"), + formatForTest("subscription-2") + }; + + @Rule + public Timeout globalTimeout = Timeout.seconds(300); + + private static String formatForTest(String resourceName) { + return resourceName + "-" + NAME_SUFFIX; + } + + @BeforeClass + public static void beforeClass() { + projectId = ServiceOptions.getDefaultProjectId(); + publisherClientSnippets = new PublisherClientSnippets(projectId); + } + + @Before + public void setUp() throws Exception { + Cleanup.deleteTestTopicsAndSubscriptions(projectId, topics, subscriptions); + } + + + @Test + public void topicAddedIsSameAsRetrieved() throws Exception { + String topicName = topics[0]; + Topic topicAdded = publisherClientSnippets.createTopic(topicName); + assertNotNull(topicAdded); + Topic topicRetrieved = publisherClientSnippets.getTopic(topicName); + assertEquals(topicAdded, topicRetrieved); + } + + @Test + public void listTopicsRetreivesAddedTopics() throws Exception { + List addedTopics = new ArrayList<>(); + String topicName1 = topics[0]; + addedTopics.add(publisherClientSnippets.createTopic(topicName1)); + String topicName2 = topics[1]; + addedTopics.add(publisherClientSnippets.createTopic(topicName2)); + String pageToken; + boolean[] topicFound = {false, false}; + ListTopicsPagedResponse response = publisherClientSnippets.listTopics(); + + do { + assertNotNull(response); + pageToken = (String) response.getNextPageToken(); + Iterable topics = response.iterateAllElements(); + for (int i = 0; i < 2; i++) { + if (!topicFound[i]) { + topicFound[i] = Iterables.contains(topics, addedTopics.get(i)); + } + } + if (pageToken != null) { + response = publisherClientSnippets.listTopicsWithPageToken(pageToken); + } + } while (!(topicFound[0] && topicFound[1])); + } + + @Test + public void listTopicSubscriptionsRetrievesAddedSubscriptions() throws Exception { + List addedSubscriptions = new ArrayList<>(); + String topicName1 = topics[0]; + publisherClientSnippets.createTopic(topicName1); + String subscriptionName1 = subscriptions[0]; + String subscriptionName2 = subscriptions[1]; + addedSubscriptions.add(createSubscription(topicName1, subscriptionName1)); + addedSubscriptions.add(createSubscription(topicName1, subscriptionName2)); + + String pageToken; + boolean[] subFound = {false, false}; + + ListTopicSubscriptionsPagedResponse response = + publisherClientSnippets.listTopicSubscriptions(topicName1); + do{ + assertNotNull(response); + pageToken = (String) response.getNextPageToken(); + Iterable subscriptions = response.iterateAllElements(); + for (int i = 0; i < 2; i++) { + if (!subFound[i]) { + subFound[i] = Iterables.contains(subscriptions, addedSubscriptions.get(i)); + } + } + if (pageToken != null) { + response = publisherClientSnippets.listTopicSubscriptionsWithPageToken( + topicName1, pageToken); + } + } while (!(subFound[0] && subFound[1])); + } + + @Test(expected = ApiException.class) + public void deletedTopicIsNotRetrievableAndThrowsException() throws Exception { + String topicName = topics[0]; + Topic topicAdded = publisherClientSnippets.createTopic(topicName); + assertNotNull(topicAdded); + TopicName formattedName = publisherClientSnippets.deleteTopic(topicName); + assertNotNull(formattedName); + publisherClientSnippets.getTopic(topicName); + } + + @Test + public void topicPolicyIsCorrectlyRetrieved() throws Exception { + String topicName = topics[0]; + publisherClientSnippets.createTopic(topicName); + Policy policy = publisherClientSnippets.getTopicPolicy(topicName); + assertNotNull(policy); + } + + @Test + public void replaceTopicPolicyAndTestPermissionsIsSuccessful() throws Exception { + String topicName = topics[0]; + publisherClientSnippets.createTopic(topicName); + Policy policy = publisherClientSnippets.replaceTopicPolicy(topicName); + assertNotNull(policy.getBindingsCount()); + assertTrue(policy.getBindings(0).getRole().equalsIgnoreCase(Role.viewer().toString())); + assertTrue(policy.getBindings(0).getMembers(0) + .equalsIgnoreCase(Identity.allAuthenticatedUsers().toString())); + TestIamPermissionsResponse response = publisherClientSnippets.testTopicPermissions(topicName); + assertTrue(response.getPermissionsList().contains("pubsub.topics.get")); + } + + private String createSubscription(String topic, String subscriptionName) throws Exception { + try (SubscriberClient subscriberClient = SubscriberClient.create()) { + Subscription subscription = subscriberClient.createSubscription( + SubscriptionName.create(projectId, subscriptionName), + TopicName.create(projectId, topic), PushConfig.getDefaultInstance(), 0); + return subscription.getName(); + } + } + + @After + public void tearDown() throws Exception { + Cleanup.deleteTestTopicsAndSubscriptions(projectId, topics, subscriptions); + } +} diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java new file mode 100644 index 000000000000..c666b0edb3a7 --- /dev/null +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java @@ -0,0 +1,217 @@ +/* + * Copyright 2017 Google Inc. All Rights Reserved. + * + * 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 com.google.cloud.examples.pubsub.snippets; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.api.gax.grpc.ApiException; +import com.google.cloud.Identity; +import com.google.cloud.Role; +import com.google.cloud.ServiceOptions; +import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListSubscriptionsPagedResponse; +import com.google.cloud.pubsub.spi.v1.Publisher; +import com.google.cloud.pubsub.spi.v1.PublisherClient; +import com.google.common.collect.Iterables; +import com.google.iam.v1.Policy; +import com.google.iam.v1.TestIamPermissionsResponse; +import com.google.protobuf.ByteString; +import com.google.pubsub.v1.PubsubMessage; +import com.google.pubsub.v1.PullResponse; +import com.google.pubsub.v1.ReceivedMessage; +import com.google.pubsub.v1.Subscription; +import com.google.pubsub.v1.TopicName; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; + +public class ITSubscriberClientSnippets { + + private static final String NAME_SUFFIX = UUID.randomUUID().toString(); + + private static String projectId; + + private static SubscriberClientSnippets subscriberClientSnippets; + + private static String[] topics = { + formatForTest("topic-1"), + formatForTest("topic-2"), + }; + private static String[] subscriptions = { + formatForTest("subscription-1"), + formatForTest("subscription-2") + }; + + @Rule + public Timeout globalTimeout = Timeout.seconds(300); + + private static String formatForTest(String resourceName) { + return resourceName + "-" + NAME_SUFFIX; + } + + @BeforeClass + public static void beforeClass() { + projectId = ServiceOptions.getDefaultProjectId(); + subscriberClientSnippets = new SubscriberClientSnippets(projectId); + } + + @Before + public void setUp() throws Exception { + Cleanup.deleteTestTopicsAndSubscriptions(projectId, topics, subscriptions); + } + + private Subscription createSubscription(String topicName, String subscriptionName) + throws Exception { + createTopic(topicName); + Subscription subscription = + subscriberClientSnippets.createSubscription(topicName, subscriptionName); + assertNotNull(subscription); + Subscription retrievedSubscription = subscriberClientSnippets.getSubscription(subscriptionName); + assertNotNull(retrievedSubscription); + assertEquals(subscription.getName(), retrievedSubscription.getName()); + return subscription; + } + + @Test + public void publishAndPullMessagesIsSuccessful() throws Exception { + String topicName = topics[0]; + String subscriptionName = subscriptions[0]; + createSubscription(topicName, subscriptionName); + Set messages = publishMessages(topicName, 5); + //pulls max 100 messages + PullResponse response = subscriberClientSnippets.pull(subscriptionName); + assertNotNull(response); + //remove messages that match sent + for (ReceivedMessage receivedMessage : response.getReceivedMessagesList()) { + String message = receivedMessage.getMessage().getData().toStringUtf8(); + if (messages.contains(message)) { + messages.remove(message); + } + } + //all messages published were received + assertTrue(messages.isEmpty()); + } + + @Test + public void replacePushConfigIsSuccessful() throws Exception { + String topicName = topics[0]; + String subscriptionName = subscriptions[0]; + createSubscription(topicName, subscriptionName); + String endpoint = "https://" + projectId + ".appspot.com/push"; + subscriberClientSnippets.replacePushConfig(subscriptionName, endpoint); + Subscription subscription = subscriberClientSnippets.getSubscription(subscriptionName); + assertNotNull(subscription.getPushConfig()); + assertEquals(subscription.getPushConfig().getPushEndpoint(), endpoint); + } + + @Test + public void listSubscriptionsRetrievesAllAddedSubscriptions() throws Exception { + List addedSubscriptions = new ArrayList<>(); + String topicName1 = topics[0]; + String subscriptionName1 = subscriptions[0]; + String topicName2 = topics[1]; + String subscriptionName2 = subscriptions[1]; + addedSubscriptions.add(createSubscription(topicName1, subscriptionName1)); + addedSubscriptions.add(createSubscription(topicName2, subscriptionName2)); + + String pageToken; + boolean[] subFound = {false, false}; + ListSubscriptionsPagedResponse response = subscriberClientSnippets.listSubscriptions(); + do { + assertNotNull(response); + pageToken = (String) response.getNextPageToken(); + Iterable subscriptions = response.iterateAllElements(); + for (int i = 0; i < 2; i++) { + if (!subFound[i]) { + subFound[i] = Iterables.contains(subscriptions, addedSubscriptions.get(i)); + } + } + if (pageToken != null) { + response = subscriberClientSnippets.listSubscriptionsWithPageToken(pageToken); + } + } while (!(subFound[0] && subFound[1])); + } + + @Test(expected = ApiException.class) + public void deleteSubscriptionThrowsExceptionWhenRetrieved() throws Exception { + String topicName = topics[0]; + String subscriptionName = subscriptions[0]; + createSubscription(topicName, subscriptionName); + subscriberClientSnippets.deleteSubscription(subscriptionName); + //expected to throw exception on retrieval + subscriberClientSnippets.getSubscription(subscriptionName); + } + + @Test + public void subscriptionHasValidIamPolicy() throws Exception { + String topicName = topics[0]; + String subscriptionName = subscriptions[0]; + createSubscription(topicName, subscriptionName); + Policy policy = subscriberClientSnippets.getSubscriptionPolicy(subscriptionName); + assertNotNull(policy); + } + + @Test + public void replaceSubscriptionPolicyAndTestPermissionsIsSuccessful() throws Exception { + String topicName = topics[0]; + String subscriptionName = subscriptions[0]; + createSubscription(topicName, subscriptionName); + Policy policy = subscriberClientSnippets.replaceSubscriptionPolicy(subscriptionName); + assertNotNull(policy.getBindingsCount()); + assertTrue(policy.getBindings(0).getRole().equalsIgnoreCase(Role.viewer().toString())); + assertTrue(policy.getBindings(0).getMembers(0) + .equalsIgnoreCase(Identity.allAuthenticatedUsers().toString())); + TestIamPermissionsResponse response = + subscriberClientSnippets.testSubscriptionPermissions(subscriptionName); + assertTrue(response.getPermissionsList().contains("pubsub.subscriptions.get")); + } + + + + private void createTopic(String name) throws Exception { + try (PublisherClient publisherClient = PublisherClient.create()) { + publisherClient.createTopic(TopicName.create(projectId, name)); + } + } + + private Set publishMessages(String topicName, int numMessages) throws Exception { + Set messages = new HashSet<>(); + Publisher publisher = Publisher.newBuilder(TopicName.create(projectId, topicName)).build(); + for (int i = 1; i<= numMessages; i++) { + String message = formatForTest("message-" + String.valueOf(i)); + PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData( + ByteString.copyFromUtf8(message)).build(); + publisher.publish(pubsubMessage); + messages.add(message); + } + return messages; + } + + @After + public void tearDown() throws Exception { + Cleanup.deleteTestTopicsAndSubscriptions(projectId, topics, subscriptions); + } +} From 67d5dd5bc24eab4dcde180de14c2b4fe415dc647 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Fri, 24 Feb 2017 16:36:46 -0800 Subject: [PATCH 4/9] header updates --- .../examples/pubsub/snippets/MessageReceiverSnippets.java | 6 ++++-- .../cloud/examples/pubsub/snippets/PublisherSnippets.java | 6 +++--- .../cloud/examples/pubsub/snippets/SubscriberSnippets.java | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/MessageReceiverSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/MessageReceiverSnippets.java index cfedfb8cea42..160980558e92 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/MessageReceiverSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/MessageReceiverSnippets.java @@ -16,8 +16,8 @@ /* * EDITING INSTRUCTIONS - * This file is referenced in Subscriber's javadoc. Any change to this file should be reflected in - * PubSub's javadoc. + * This file is referenced in MessageReceiver's javadoc. + * Any change to this file should be reflected in MessageReceiver's javadoc. */ package com.google.cloud.examples.pubsub.snippets; @@ -28,6 +28,8 @@ import com.google.pubsub.v1.PubsubMessage; import java.util.concurrent.BlockingQueue; +/** This class contains snippets for the {@link MessageReceiver} interface. */ + public class MessageReceiverSnippets { private final BlockingQueue blockingQueue; diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherSnippets.java index 9f10fd08e9f7..997ca563f085 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherSnippets.java @@ -16,8 +16,8 @@ /* * EDITING INSTRUCTIONS - * This file is referenced in {@link Publisher} javadoc. - * Any change to this file should be reflected in PubSub's javadoc. + * This file is referenced in Publisher's javadoc. Any change to this file should be reflected in + * Publisher's javadoc. */ package com.google.cloud.examples.pubsub.snippets; @@ -29,7 +29,7 @@ import com.google.pubsub.v1.PubsubMessage; import com.google.pubsub.v1.TopicName; -/** This class contains a number of snippets for the {@link Publisher} interface. */ +/** This class contains snippets for the {@link Publisher} interface. */ public class PublisherSnippets { private final Publisher publisher; diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberSnippets.java index 13bc5a6c6cdd..581f66efcc4d 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberSnippets.java @@ -17,7 +17,7 @@ /* * EDITING INSTRUCTIONS * This file is referenced in Subscriber's javadoc. Any change to this file should be reflected in - * PubSub's javadoc. + * Subscriber's javadoc. */ package com.google.cloud.examples.pubsub.snippets; @@ -28,6 +28,7 @@ import com.google.pubsub.v1.SubscriptionName; import java.util.concurrent.Executor; +/** This class contains snippets for the {@link Subscriber} interface. */ public class SubscriberSnippets { private final SubscriptionName subscription; From 5da936adf0f62dbd716e98c404e7bf68cd46f6c0 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Fri, 24 Feb 2017 16:45:27 -0800 Subject: [PATCH 5/9] string concatenation, whitespace fix --- .../examples/pubsub/snippets/ITSubscriberClientSnippets.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java index c666b0edb3a7..2056503ad0fe 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java @@ -189,8 +189,6 @@ public void replaceSubscriptionPolicyAndTestPermissionsIsSuccessful() throws Exc assertTrue(response.getPermissionsList().contains("pubsub.subscriptions.get")); } - - private void createTopic(String name) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { publisherClient.createTopic(TopicName.create(projectId, name)); @@ -201,7 +199,7 @@ private Set publishMessages(String topicName, int numMessages) throws Ex Set messages = new HashSet<>(); Publisher publisher = Publisher.newBuilder(TopicName.create(projectId, topicName)).build(); for (int i = 1; i<= numMessages; i++) { - String message = formatForTest("message-" + String.valueOf(i)); + String message = formatForTest("message-" + i); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData( ByteString.copyFromUtf8(message)).build(); publisher.publish(pubsubMessage); From e053154de274198479cda5f693b7b11128f673e7 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Mon, 27 Feb 2017 15:46:47 -0800 Subject: [PATCH 6/9] pubsub snippet updates --- .../snippets/PublisherClientSnippets.java | 98 +++++-------------- .../snippets/SubscriberClientSnippets.java | 98 ++++++------------- .../examples/pubsub/snippets/Cleanup.java | 6 +- .../snippets/ITPublisherClientSnippets.java | 45 ++++----- .../snippets/ITSubscriberClientSnippets.java | 16 +-- 5 files changed, 85 insertions(+), 178 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java index 8f671a126191..2b37d2e9d158 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java @@ -42,27 +42,25 @@ public PublisherClientSnippets(String projectId) { } /** Example of creating a topic. */ - public Topic createTopic(String topicName) throws Exception { + public Topic createTopic(String myTopic) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START createTopic] - TopicName formattedTopicName = TopicName.create(projectId, topicName); - Topic topic = publisherClient.createTopic(formattedTopicName); + TopicName topicName = TopicName.create(projectId, myTopic); + Topic topic = publisherClient.createTopic(topicName); // [END createTopic] return topic; } } - /** Example of listing topics, specifying the page size. */ + /** Example of listing topics. */ public ListTopicsPagedResponse listTopics() throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START listTopics] ListTopicsRequest listTopicsRequest = ListTopicsRequest.newBuilder() .setProjectWithProjectName(ProjectName.create(projectId)) - .setPageSize(100) .build(); - ListTopicsPagedResponse response = - publisherClient.listTopics(listTopicsRequest); + ListTopicsPagedResponse response = publisherClient.listTopics(listTopicsRequest); Iterable topics = response.iterateAllElements(); for (Topic topic : topics) { // do something with the topic @@ -72,41 +70,20 @@ public ListTopicsPagedResponse listTopics() throws Exception { } } - /** Example of listing topics, specifying the page size and a page token. */ - public ListTopicsPagedResponse listTopicsWithPageToken(String pageToken) throws Exception { + /** Example of listing topics for a subscription. */ + public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String myTopic) + throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - // [START listTopicsWithPageToken] - ListTopicsRequest listTopicsRequest = - ListTopicsRequest.newBuilder() - .setProjectWithProjectName(ProjectName.create(projectId)) - .setPageSize(100) - .setPageToken(pageToken) - .build(); - ListTopicsPagedResponse response = - publisherClient.listTopics(listTopicsRequest); - Iterable topics = response.iterateAllElements(); - for (Topic topic : topics) { - // do something with the topic - } - // [END listTopicsWithPageToken] - return response; - } - } - - /** Example of listing topics for a subscription, specifying the page size. */ - public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicName) throws Exception { - try (PublisherClient publisherClient = PublisherClient.create()) { - TopicName formattedTopicName = TopicName.create(projectId, topicName); // [START listTopicSubscriptions] + TopicName topicName = TopicName.create(projectId, myTopic); ListTopicSubscriptionsRequest request = ListTopicSubscriptionsRequest.newBuilder() - .setTopicWithTopicName(formattedTopicName) - .setPageSize(100) + .setTopicWithTopicName(topicName) .build(); ListTopicSubscriptionsPagedResponse response = publisherClient.listTopicSubscriptions(request); - Iterable subscriptions = response.iterateAllElements(); - for (String subscription : subscriptions) { + Iterable subscriptionNames = response.iterateAllElements(); + for (String subscriptionName : subscriptionNames) { // do something with the subscription name } // [END listTopicSubscriptions] @@ -114,45 +91,22 @@ public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicNa } } - /** Example of listing topics for a subscription, specifying the page size and page token */ - public ListTopicSubscriptionsPagedResponse listTopicSubscriptionsWithPageToken( - String topicName, String pageToken) throws Exception { - try (PublisherClient publisherClient = PublisherClient.create()) { - TopicName formattedTopicName = TopicName.create(projectId, topicName); - // [START listTopicSubscriptionsWithPageToken] - ListTopicSubscriptionsRequest request = - ListTopicSubscriptionsRequest.newBuilder() - .setTopicWithTopicName(formattedTopicName) - .setPageSize(100) - .setPageToken(pageToken) - .build(); - ListTopicSubscriptionsPagedResponse response = - publisherClient.listTopicSubscriptions(request); - Iterable subscriptions = response.iterateAllElements(); - for (String subscription : subscriptions) { - // do something with the subscription name - } - // [END listTopicSubscriptionsWithPageToken] - return response; - } - } - /** Example of deleting a topic. */ - public TopicName deleteTopic(String topicName) throws Exception { + public TopicName deleteTopic(String myTopic) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - TopicName formattedTopicName = TopicName.create(projectId, topicName); // [START deleteTopic] - publisherClient.deleteTopic(formattedTopicName); + TopicName topicName = TopicName.create(projectId, myTopic); + publisherClient.deleteTopic(topicName); // [END deleteTopic] - return formattedTopicName; + return topicName; } } /** Example of getting a topic policy. */ - public Policy getTopicPolicy(String name) throws Exception { + public Policy getTopicPolicy(String myTopic) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - TopicName topicName = TopicName.create(projectId, name); // [START getTopicPolicy] + TopicName topicName = TopicName.create(projectId, myTopic); Policy policy = publisherClient.getIamPolicy(topicName.toString()); if (policy == null) { // topic iam policy was not found @@ -163,10 +117,10 @@ public Policy getTopicPolicy(String name) throws Exception { } /** Example of replacing a topic policy. */ - public Policy replaceTopicPolicy(String name) throws Exception { + public Policy replaceTopicPolicy(String myTopic) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - String topicName = TopicName.create(projectId, name).toString(); // [START replaceTopicPolicy] + String topicName = TopicName.create(projectId, myTopic).toString(); Policy policy = publisherClient.getIamPolicy(topicName); // add role -> members binding Binding binding = @@ -184,25 +138,25 @@ public Policy replaceTopicPolicy(String name) throws Exception { /** Example of testing whether the caller has the provided permissions on a topic. * Only viewer, editor or admin/owner can view results of pubsub.topics.get */ - public TestIamPermissionsResponse testTopicPermissions(String topicName) throws Exception { + public TestIamPermissionsResponse testTopicPermissions(String myTopic) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - TopicName formattedTopicName = TopicName.create(projectId, topicName); // [START testTopicPermissions] List permissions = new LinkedList<>(); permissions.add("pubsub.topics.get"); + TopicName topicName = TopicName.create(projectId, myTopic); TestIamPermissionsResponse testedPermissions = - publisherClient.testIamPermissions(formattedTopicName.toString(), permissions); + publisherClient.testIamPermissions(topicName.toString(), permissions); // [END testTopicPermissions] return testedPermissions; } } /** Example of getting a topic. */ - public Topic getTopic(String topicName) throws Exception { + public Topic getTopic(String myTopic) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START getTopic] - TopicName formattedTopicName = TopicName.create(projectId, topicName); - Topic topic = publisherClient.getTopic(formattedTopicName); + TopicName topicName = TopicName.create(projectId, myTopic); + Topic topic = publisherClient.getTopic(topicName); // [END createTopic] return topic; } diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java index 03e45a27978f..44153462fe6c 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java @@ -20,7 +20,6 @@ import com.google.cloud.Identity; import com.google.cloud.Role; -import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers; import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListSubscriptionsPagedResponse; import com.google.cloud.pubsub.spi.v1.PublisherClient; import com.google.cloud.pubsub.spi.v1.SubscriberClient; @@ -49,31 +48,30 @@ public SubscriberClientSnippets(String projectId) { } /** Example of creating a pull subscription for a topic. */ - public Subscription createSubscription(String topic, String subscriptionName) throws Exception { - TopicName topicName = TopicName.create(projectId, topic); - SubscriptionName formattedSubscriptionName = - SubscriptionName.create(projectId, subscriptionName); + public Subscription createSubscription(String topic, String mySubscription) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { // [START createSubscription] + TopicName topicName = TopicName.create(projectId, topic); + SubscriptionName subscriptionName = + SubscriptionName.create(projectId, mySubscription); Subscription subscription = subscriberClient.createSubscription( - formattedSubscriptionName, topicName, PushConfig.getDefaultInstance(), 0); + subscriptionName, topicName, PushConfig.getDefaultInstance(), 0); // [END createSubscription] return subscription; } } /** Example of pulling a maximum number of messages from a subscription. */ - public PullResponse pull(String subscriptionName) throws Exception { + public PullResponse pull(String mySubscription) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { - SubscriptionName subcriptionFormattedName = - SubscriptionName.create(projectId, subscriptionName); // [START pull] - PullResponse response = subscriberClient.pull(subcriptionFormattedName, true, 100); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); + PullResponse response = subscriberClient.pull(subscriptionName, true, 100); for (ReceivedMessage message : response.getReceivedMessagesList()) { // do something with message, then ack or nack subscriberClient.acknowledge( - subcriptionFormattedName, Collections.singletonList(message.getAckId())); + subscriptionName, Collections.singletonList(message.getAckId())); } // [END pull] return response; @@ -81,28 +79,25 @@ public PullResponse pull(String subscriptionName) throws Exception { } /** Example of replacing the push configuration of a subscription, setting the push endpoint. */ - public void replacePushConfig(String subscriptionName, String endpoint) throws Exception { + public void replacePushConfig(String mySubscription, String endpoint) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { - SubscriptionName formattedSubscriptionName = - SubscriptionName.create(projectId, subscriptionName); // [START replacePushConfig] + SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build(); - subscriberClient.modifyPushConfig(formattedSubscriptionName, pushConfig); + subscriberClient.modifyPushConfig(subscriptionName, pushConfig); // [END replacePushConfig] } } - /** Example of listing subscriptions, specifying the page size. */ + /** Example of listing subscriptions. */ public ListSubscriptionsPagedResponse listSubscriptions() throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { - ProjectName projectName = ProjectName.create(projectId); // [START listSubscriptions] ListSubscriptionsRequest listSubscriptionsRequest = ListSubscriptionsRequest.newBuilder() - .setProjectWithProjectName(projectName) - .setPageSize(100) + .setProjectWithProjectName(ProjectName.create(projectId)) .build(); - PagedResponseWrappers.ListSubscriptionsPagedResponse response = + ListSubscriptionsPagedResponse response = subscriberClient.listSubscriptions(listSubscriptionsRequest); Iterable subscriptions = response.iterateAllElements(); for (Subscription subscription : subscriptions) { @@ -113,48 +108,23 @@ public ListSubscriptionsPagedResponse listSubscriptions() throws Exception { } } - /** Example of listing subscriptions, specifying the page size and page token. */ - public ListSubscriptionsPagedResponse listSubscriptionsWithPageToken(String pageToken) - throws Exception { - try (SubscriberClient subscriberClient = SubscriberClient.create()) { - ProjectName projectName = ProjectName.create(projectId); - // [START listSubscriptionsWithPageToken] - ListSubscriptionsRequest listSubscriptionsRequest = - ListSubscriptionsRequest.newBuilder() - .setProjectWithProjectName(projectName) - .setPageSize(100) - .setPageToken(pageToken) - .build(); - PagedResponseWrappers.ListSubscriptionsPagedResponse response = - subscriberClient.listSubscriptions(listSubscriptionsRequest); - Iterable subscriptions = response.iterateAllElements(); - for (Subscription subscription : subscriptions) { - // do something with the subscription - } - // [END listSubscriptionsWithPageToken] - return response; - } - } - /** Example of deleting a subscription. */ - public SubscriptionName deleteSubscription(String subscriptionName) throws Exception { + public SubscriptionName deleteSubscription(String mySubscription) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { - SubscriptionName formattedSubscriptionName = - SubscriptionName.create(projectId, subscriptionName); // [START deleteSubscription] - subscriberClient.deleteSubscription(formattedSubscriptionName); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); + subscriberClient.deleteSubscription(subscriptionName); // [END deleteSubscription] - return formattedSubscriptionName; + return subscriptionName; } } /** Example of getting a subscription policy. */ - public Policy getSubscriptionPolicy(String subscriptionName) throws Exception { + public Policy getSubscriptionPolicy(String mySubscription) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { - SubscriptionName formattedSubscriptionName = - SubscriptionName.create(projectId, subscriptionName); // [START getSubscriptionPolicy] - Policy policy = subscriberClient.getIamPolicy(formattedSubscriptionName.toString()); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); + Policy policy = subscriberClient.getIamPolicy(subscriptionName.toString()); if (policy == null) { // subscription was not found } @@ -164,12 +134,11 @@ public Policy getSubscriptionPolicy(String subscriptionName) throws Exception { } /** Example of replacing a subscription policy. */ - public Policy replaceSubscriptionPolicy(String subscriptionName) throws Exception { + public Policy replaceSubscriptionPolicy(String mySubscription) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { - SubscriptionName formattedSubscriptionName = - SubscriptionName.create(projectId, subscriptionName); // [START replaceSubscriptionPolicy] - Policy policy = subscriberClient.getIamPolicy(formattedSubscriptionName.toString()); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); + Policy policy = subscriberClient.getIamPolicy(subscriptionName.toString()); // Create a role => members binding Binding binding = Binding.newBuilder() @@ -179,36 +148,33 @@ public Policy replaceSubscriptionPolicy(String subscriptionName) throws Exceptio //Update policy Policy updatedPolicy = policy.toBuilder().addBindings(binding).build(); - updatedPolicy = subscriberClient.setIamPolicy( - formattedSubscriptionName.toString(), updatedPolicy); + updatedPolicy = subscriberClient.setIamPolicy(subscriptionName.toString(), updatedPolicy); // [END replaceSubscriptionPolicy] return updatedPolicy; } } /** Example of testing whether the caller has the provided permissions on a subscription. */ - public TestIamPermissionsResponse testSubscriptionPermissions(String subscriptionName) + public TestIamPermissionsResponse testSubscriptionPermissions(String mySubscription) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - SubscriptionName formattedSubscriptionName = - SubscriptionName.create(projectId, subscriptionName); // [START testSubscriptionPermissions] List permissions = new LinkedList<>(); permissions.add("pubsub.subscriptions.get"); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); TestIamPermissionsResponse testedPermissions = - publisherClient.testIamPermissions(formattedSubscriptionName.toString(), permissions); + publisherClient.testIamPermissions(subscriptionName.toString(), permissions); // [END testSubscriptionPermissions] return testedPermissions; } } /** Example of getting a subscription. */ - public Subscription getSubscription(String subscriptionName) throws Exception { - SubscriptionName formattedSubscriptionName = - SubscriptionName.create(projectId, subscriptionName); + public Subscription getSubscription(String mySubscription) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { // [START getSubscription] - Subscription subscription = subscriberClient.getSubscription(formattedSubscriptionName); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); + Subscription subscription = subscriberClient.getSubscription(subscriptionName); // [END getSubscription] return subscription; } diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/Cleanup.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/Cleanup.java index 9e6e5de27560..fccdf51824ca 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/Cleanup.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/Cleanup.java @@ -31,9 +31,10 @@ protected static void deleteTestTopicsAndSubscriptions( private static void deleteTestTopics(String projectId, String[] testTopics) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - for (String testTopic : testTopics) { + for (String name : testTopics) { try { - publisherClient.deleteTopic(TopicName.create(projectId, testTopic)); + publisherClient.deleteTopic(TopicName.create(projectId, name)); + System.out.println("Topic deleted : " + name); } catch (Exception e) { //do nothing catch clause } @@ -48,6 +49,7 @@ private static void deleteTestSubscriptions(String projectId, String[] subscript try { subscriberClient.deleteSubscription( SubscriptionName.create(projectId, name)); + System.out.println("Subscription deleted : " + name); } catch (Exception e) { //do nothing catch clause } diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java index 26a5e10c0d46..5fcd2bc20032 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java @@ -97,23 +97,19 @@ public void listTopicsRetreivesAddedTopics() throws Exception { addedTopics.add(publisherClientSnippets.createTopic(topicName1)); String topicName2 = topics[1]; addedTopics.add(publisherClientSnippets.createTopic(topicName2)); - String pageToken; + boolean[] topicFound = {false, false}; ListTopicsPagedResponse response = publisherClientSnippets.listTopics(); - do { - assertNotNull(response); - pageToken = (String) response.getNextPageToken(); - Iterable topics = response.iterateAllElements(); - for (int i = 0; i < 2; i++) { - if (!topicFound[i]) { - topicFound[i] = Iterables.contains(topics, addedTopics.get(i)); - } - } - if (pageToken != null) { - response = publisherClientSnippets.listTopicsWithPageToken(pageToken); + assertNotNull(response); + Iterable topics = response.iterateAllElements(); + for (int i = 0; i < 2; i++) { + if (!topicFound[i]) { + topicFound[i] = Iterables.contains(topics, addedTopics.get(i)); } - } while (!(topicFound[0] && topicFound[1])); + } + + assertTrue(topicFound[0] && topicFound[1]); } @Test @@ -126,25 +122,20 @@ public void listTopicSubscriptionsRetrievesAddedSubscriptions() throws Exception addedSubscriptions.add(createSubscription(topicName1, subscriptionName1)); addedSubscriptions.add(createSubscription(topicName1, subscriptionName2)); - String pageToken; boolean[] subFound = {false, false}; ListTopicSubscriptionsPagedResponse response = publisherClientSnippets.listTopicSubscriptions(topicName1); - do{ - assertNotNull(response); - pageToken = (String) response.getNextPageToken(); - Iterable subscriptions = response.iterateAllElements(); - for (int i = 0; i < 2; i++) { - if (!subFound[i]) { - subFound[i] = Iterables.contains(subscriptions, addedSubscriptions.get(i)); - } - } - if (pageToken != null) { - response = publisherClientSnippets.listTopicSubscriptionsWithPageToken( - topicName1, pageToken); + + + assertNotNull(response); + Iterable subscriptions = response.iterateAllElements(); + for (int i = 0; i < 2; i++) { + if (!subFound[i]) { + subFound[i] = Iterables.contains(subscriptions, addedSubscriptions.get(i)); } - } while (!(subFound[0] && subFound[1])); + } + assertTrue(subFound[0] && subFound[1]); } @Test(expected = ApiException.class) diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java index 2056503ad0fe..d7b24f86c9fc 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java @@ -137,22 +137,16 @@ public void listSubscriptionsRetrievesAllAddedSubscriptions() throws Exception { addedSubscriptions.add(createSubscription(topicName1, subscriptionName1)); addedSubscriptions.add(createSubscription(topicName2, subscriptionName2)); - String pageToken; boolean[] subFound = {false, false}; ListSubscriptionsPagedResponse response = subscriberClientSnippets.listSubscriptions(); - do { - assertNotNull(response); - pageToken = (String) response.getNextPageToken(); - Iterable subscriptions = response.iterateAllElements(); - for (int i = 0; i < 2; i++) { + assertNotNull(response); + Iterable subscriptions = response.iterateAllElements(); + for (int i = 0; i < 2; i++) { if (!subFound[i]) { subFound[i] = Iterables.contains(subscriptions, addedSubscriptions.get(i)); } - } - if (pageToken != null) { - response = subscriberClientSnippets.listSubscriptionsWithPageToken(pageToken); - } - } while (!(subFound[0] && subFound[1])); + } + assertTrue(subFound[0] && subFound[1]); } @Test(expected = ApiException.class) From 59ccf27d119222c3e2ededda203566209c4ded7c Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Mon, 27 Feb 2017 17:07:56 -0800 Subject: [PATCH 7/9] code review fixes --- .../snippets/PublisherClientSnippets.java | 28 ++++++++-------- .../snippets/SubscriberClientSnippets.java | 32 +++++++++---------- .../examples/pubsub/snippets/Cleanup.java | 12 +++---- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java index 2b37d2e9d158..2eaf88022acf 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java @@ -42,10 +42,10 @@ public PublisherClientSnippets(String projectId) { } /** Example of creating a topic. */ - public Topic createTopic(String myTopic) throws Exception { + public Topic createTopic(String topicId) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START createTopic] - TopicName topicName = TopicName.create(projectId, myTopic); + TopicName topicName = TopicName.create(projectId, topicId); Topic topic = publisherClient.createTopic(topicName); // [END createTopic] return topic; @@ -71,11 +71,11 @@ public ListTopicsPagedResponse listTopics() throws Exception { } /** Example of listing topics for a subscription. */ - public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String myTopic) + public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicId) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START listTopicSubscriptions] - TopicName topicName = TopicName.create(projectId, myTopic); + TopicName topicName = TopicName.create(projectId, topicId); ListTopicSubscriptionsRequest request = ListTopicSubscriptionsRequest.newBuilder() .setTopicWithTopicName(topicName) @@ -92,10 +92,10 @@ public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String myTopic } /** Example of deleting a topic. */ - public TopicName deleteTopic(String myTopic) throws Exception { + public TopicName deleteTopic(String topicId) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START deleteTopic] - TopicName topicName = TopicName.create(projectId, myTopic); + TopicName topicName = TopicName.create(projectId, topicId); publisherClient.deleteTopic(topicName); // [END deleteTopic] return topicName; @@ -103,10 +103,10 @@ public TopicName deleteTopic(String myTopic) throws Exception { } /** Example of getting a topic policy. */ - public Policy getTopicPolicy(String myTopic) throws Exception { + public Policy getTopicPolicy(String topicId) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START getTopicPolicy] - TopicName topicName = TopicName.create(projectId, myTopic); + TopicName topicName = TopicName.create(projectId, topicId); Policy policy = publisherClient.getIamPolicy(topicName.toString()); if (policy == null) { // topic iam policy was not found @@ -117,10 +117,10 @@ public Policy getTopicPolicy(String myTopic) throws Exception { } /** Example of replacing a topic policy. */ - public Policy replaceTopicPolicy(String myTopic) throws Exception { + public Policy replaceTopicPolicy(String topicId) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START replaceTopicPolicy] - String topicName = TopicName.create(projectId, myTopic).toString(); + String topicName = TopicName.create(projectId, topicId).toString(); Policy policy = publisherClient.getIamPolicy(topicName); // add role -> members binding Binding binding = @@ -138,12 +138,12 @@ public Policy replaceTopicPolicy(String myTopic) throws Exception { /** Example of testing whether the caller has the provided permissions on a topic. * Only viewer, editor or admin/owner can view results of pubsub.topics.get */ - public TestIamPermissionsResponse testTopicPermissions(String myTopic) throws Exception { + public TestIamPermissionsResponse testTopicPermissions(String topicId) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START testTopicPermissions] List permissions = new LinkedList<>(); permissions.add("pubsub.topics.get"); - TopicName topicName = TopicName.create(projectId, myTopic); + TopicName topicName = TopicName.create(projectId, topicId); TestIamPermissionsResponse testedPermissions = publisherClient.testIamPermissions(topicName.toString(), permissions); // [END testTopicPermissions] @@ -152,10 +152,10 @@ public TestIamPermissionsResponse testTopicPermissions(String myTopic) throws Ex } /** Example of getting a topic. */ - public Topic getTopic(String myTopic) throws Exception { + public Topic getTopic(String topicId) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START getTopic] - TopicName topicName = TopicName.create(projectId, myTopic); + TopicName topicName = TopicName.create(projectId, topicId); Topic topic = publisherClient.getTopic(topicName); // [END createTopic] return topic; diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java index 44153462fe6c..eff3a7f206e5 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java @@ -48,12 +48,12 @@ public SubscriberClientSnippets(String projectId) { } /** Example of creating a pull subscription for a topic. */ - public Subscription createSubscription(String topic, String mySubscription) throws Exception { + public Subscription createSubscription(String topic, String subscriptionId) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { // [START createSubscription] TopicName topicName = TopicName.create(projectId, topic); SubscriptionName subscriptionName = - SubscriptionName.create(projectId, mySubscription); + SubscriptionName.create(projectId, subscriptionId); Subscription subscription = subscriberClient.createSubscription( subscriptionName, topicName, PushConfig.getDefaultInstance(), 0); @@ -63,10 +63,10 @@ public Subscription createSubscription(String topic, String mySubscription) thro } /** Example of pulling a maximum number of messages from a subscription. */ - public PullResponse pull(String mySubscription) throws Exception { + public PullResponse pull(String subscriptionId) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { // [START pull] - SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); PullResponse response = subscriberClient.pull(subscriptionName, true, 100); for (ReceivedMessage message : response.getReceivedMessagesList()) { // do something with message, then ack or nack @@ -79,10 +79,10 @@ public PullResponse pull(String mySubscription) throws Exception { } /** Example of replacing the push configuration of a subscription, setting the push endpoint. */ - public void replacePushConfig(String mySubscription, String endpoint) throws Exception { + public void replacePushConfig(String subscriptionId, String endpoint) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { // [START replacePushConfig] - SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build(); subscriberClient.modifyPushConfig(subscriptionName, pushConfig); // [END replacePushConfig] @@ -109,10 +109,10 @@ public ListSubscriptionsPagedResponse listSubscriptions() throws Exception { } /** Example of deleting a subscription. */ - public SubscriptionName deleteSubscription(String mySubscription) throws Exception { + public SubscriptionName deleteSubscription(String subscriptionId) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { // [START deleteSubscription] - SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); subscriberClient.deleteSubscription(subscriptionName); // [END deleteSubscription] return subscriptionName; @@ -120,10 +120,10 @@ public SubscriptionName deleteSubscription(String mySubscription) throws Excepti } /** Example of getting a subscription policy. */ - public Policy getSubscriptionPolicy(String mySubscription) throws Exception { + public Policy getSubscriptionPolicy(String subscriptionId) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { // [START getSubscriptionPolicy] - SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); Policy policy = subscriberClient.getIamPolicy(subscriptionName.toString()); if (policy == null) { // subscription was not found @@ -134,10 +134,10 @@ public Policy getSubscriptionPolicy(String mySubscription) throws Exception { } /** Example of replacing a subscription policy. */ - public Policy replaceSubscriptionPolicy(String mySubscription) throws Exception { + public Policy replaceSubscriptionPolicy(String subscriptionId) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { // [START replaceSubscriptionPolicy] - SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); Policy policy = subscriberClient.getIamPolicy(subscriptionName.toString()); // Create a role => members binding Binding binding = @@ -155,13 +155,13 @@ public Policy replaceSubscriptionPolicy(String mySubscription) throws Exception } /** Example of testing whether the caller has the provided permissions on a subscription. */ - public TestIamPermissionsResponse testSubscriptionPermissions(String mySubscription) + public TestIamPermissionsResponse testSubscriptionPermissions(String subscriptionId) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { // [START testSubscriptionPermissions] List permissions = new LinkedList<>(); permissions.add("pubsub.subscriptions.get"); - SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); TestIamPermissionsResponse testedPermissions = publisherClient.testIamPermissions(subscriptionName.toString(), permissions); // [END testSubscriptionPermissions] @@ -170,10 +170,10 @@ public TestIamPermissionsResponse testSubscriptionPermissions(String mySubscript } /** Example of getting a subscription. */ - public Subscription getSubscription(String mySubscription) throws Exception { + public Subscription getSubscription(String subscriptionId) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { // [START getSubscription] - SubscriptionName subscriptionName = SubscriptionName.create(projectId, mySubscription); + SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId); Subscription subscription = subscriberClient.getSubscription(subscriptionName); // [END getSubscription] return subscription; diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/Cleanup.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/Cleanup.java index fccdf51824ca..c9911fd392a3 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/Cleanup.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/Cleanup.java @@ -31,10 +31,10 @@ protected static void deleteTestTopicsAndSubscriptions( private static void deleteTestTopics(String projectId, String[] testTopics) throws Exception { try (PublisherClient publisherClient = PublisherClient.create()) { - for (String name : testTopics) { + for (String topicId : testTopics) { try { - publisherClient.deleteTopic(TopicName.create(projectId, name)); - System.out.println("Topic deleted : " + name); + publisherClient.deleteTopic(TopicName.create(projectId, topicId)); + System.out.println("Topic deleted : " + topicId); } catch (Exception e) { //do nothing catch clause } @@ -45,11 +45,11 @@ private static void deleteTestTopics(String projectId, String[] testTopics) thro private static void deleteTestSubscriptions(String projectId, String[] subscriptions) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { - for (String name : subscriptions) { + for (String subscriptionId : subscriptions) { try { subscriberClient.deleteSubscription( - SubscriptionName.create(projectId, name)); - System.out.println("Subscription deleted : " + name); + SubscriptionName.create(projectId, subscriptionId)); + System.out.println("Subscription deleted : " + subscriptionId); } catch (Exception e) { //do nothing catch clause } From b2f6d649d26f0e173ad93320d7129694891d3c1a Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 2 Mar 2017 08:28:16 -0800 Subject: [PATCH 8/9] removing ARR retrieving default project id : moving to snippet class --- .../pubsub/snippets/PublisherClientSnippets.java | 11 ++++++++--- .../pubsub/snippets/SubscriberClientSnippets.java | 11 ++++++++--- .../pubsub/snippets/ITPublisherClientSnippets.java | 8 +++----- .../pubsub/snippets/ITSubscriberClientSnippets.java | 7 +++---- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java index 2eaf88022acf..936bbc043441 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Google Inc. All Rights Reserved. + * Copyright 2017 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ import com.google.cloud.Identity; import com.google.cloud.Role; +import com.google.cloud.ServiceOptions; import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse; import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse; import com.google.cloud.pubsub.spi.v1.PublisherClient; @@ -37,8 +38,12 @@ public class PublisherClientSnippets { private final String projectId; - public PublisherClientSnippets(String projectId) { - this.projectId = projectId; + public PublisherClientSnippets() { + this.projectId = ServiceOptions.getDefaultProjectId(); + } + + public String getProjectId() { + return projectId; } /** Example of creating a topic. */ diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java index eff3a7f206e5..e30ff77f5d28 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Google Inc. All Rights Reserved. + * Copyright 2017 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import com.google.cloud.Identity; import com.google.cloud.Role; +import com.google.cloud.ServiceOptions; import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListSubscriptionsPagedResponse; import com.google.cloud.pubsub.spi.v1.PublisherClient; import com.google.cloud.pubsub.spi.v1.SubscriberClient; @@ -43,10 +44,14 @@ public class SubscriberClientSnippets { private final String projectId; - public SubscriberClientSnippets(String projectId) { - this.projectId = projectId; + public SubscriberClientSnippets() { + this.projectId = ServiceOptions.getDefaultProjectId(); } + public String getProjectId() { + return projectId; + } + /** Example of creating a pull subscription for a topic. */ public Subscription createSubscription(String topic, String subscriptionId) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java index 5fcd2bc20032..d2d46afbac34 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Google Inc. All Rights Reserved. + * Copyright 2017 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ import com.google.api.gax.grpc.ApiException; import com.google.cloud.Identity; import com.google.cloud.Role; -import com.google.cloud.ServiceOptions; import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse; import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse; import com.google.cloud.pubsub.spi.v1.SubscriberClient; @@ -71,8 +70,8 @@ private static String formatForTest(String resourceName) { @BeforeClass public static void beforeClass() { - projectId = ServiceOptions.getDefaultProjectId(); - publisherClientSnippets = new PublisherClientSnippets(projectId); + publisherClientSnippets = new PublisherClientSnippets(); + projectId = publisherClientSnippets.getProjectId(); } @Before @@ -80,7 +79,6 @@ public void setUp() throws Exception { Cleanup.deleteTestTopicsAndSubscriptions(projectId, topics, subscriptions); } - @Test public void topicAddedIsSameAsRetrieved() throws Exception { String topicName = topics[0]; diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java index d7b24f86c9fc..763dcd6ccae6 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Google Inc. All Rights Reserved. + * Copyright 2017 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ import com.google.api.gax.grpc.ApiException; import com.google.cloud.Identity; import com.google.cloud.Role; -import com.google.cloud.ServiceOptions; import com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListSubscriptionsPagedResponse; import com.google.cloud.pubsub.spi.v1.Publisher; import com.google.cloud.pubsub.spi.v1.PublisherClient; @@ -74,8 +73,8 @@ private static String formatForTest(String resourceName) { @BeforeClass public static void beforeClass() { - projectId = ServiceOptions.getDefaultProjectId(); - subscriberClientSnippets = new SubscriberClientSnippets(projectId); + subscriberClientSnippets = new SubscriberClientSnippets(); + projectId = subscriberClientSnippets.getProjectId(); } @Before From 3238290600de9625fdf0e7251d83c61e376365da Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 2 Mar 2017 08:30:44 -0800 Subject: [PATCH 9/9] ARR back in, checkstyle audit requires header --- .../examples/pubsub/snippets/PublisherClientSnippets.java | 2 +- .../examples/pubsub/snippets/SubscriberClientSnippets.java | 4 ++-- .../examples/pubsub/snippets/ITPublisherClientSnippets.java | 2 +- .../examples/pubsub/snippets/ITSubscriberClientSnippets.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java index 936bbc043441..9737bf4a3087 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PublisherClientSnippets.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Google Inc. + * Copyright 2017 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java index e30ff77f5d28..bee70a1a118b 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriberClientSnippets.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Google Inc. + * Copyright 2017 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,7 +51,7 @@ public SubscriberClientSnippets() { public String getProjectId() { return projectId; } - + /** Example of creating a pull subscription for a topic. */ public Subscription createSubscription(String topic, String subscriptionId) throws Exception { try (SubscriberClient subscriberClient = SubscriberClient.create()) { diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java index d2d46afbac34..d607a1568d45 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPublisherClientSnippets.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Google Inc. + * Copyright 2017 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java index 763dcd6ccae6..50fc5b52ac93 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriberClientSnippets.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Google Inc. + * Copyright 2017 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.