Skip to content

Commit 4bbe6b7

Browse files
authored
Add snippets to Topic's javadoc, TopicSnippets class and tests (#1245)
1 parent 05a2a32 commit 4bbe6b7

File tree

3 files changed

+617
-0
lines changed

3 files changed

+617
-0
lines changed
Lines changed: 332 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,332 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in Topic's javadoc. Any change to this file should be reflected in
20+
* Topic's javadoc.
21+
*/
22+
23+
package com.google.cloud.examples.pubsub.snippets;
24+
25+
import com.google.cloud.AsyncPage;
26+
import com.google.cloud.Identity;
27+
import com.google.cloud.Page;
28+
import com.google.cloud.Policy;
29+
import com.google.cloud.Role;
30+
import com.google.cloud.pubsub.Message;
31+
import com.google.cloud.pubsub.PubSub.ListOption;
32+
import com.google.cloud.pubsub.SubscriptionId;
33+
import com.google.cloud.pubsub.Topic;
34+
35+
import java.util.Iterator;
36+
import java.util.LinkedList;
37+
import java.util.List;
38+
import java.util.concurrent.ExecutionException;
39+
import java.util.concurrent.Future;
40+
41+
/**
42+
* This class contains a number of snippets for the {@link Topic} class.
43+
*/
44+
public class TopicSnippets {
45+
46+
private final Topic topic;
47+
48+
public TopicSnippets(Topic topic) {
49+
this.topic = topic;
50+
}
51+
52+
/**
53+
* Example of getting the topic's latest information.
54+
*/
55+
// [TARGET reload()]
56+
public Topic reload() {
57+
// [START reload]
58+
Topic latestTopic = topic.reload();
59+
if (latestTopic == null) {
60+
// the topic was not found
61+
}
62+
// [END reload]
63+
return latestTopic;
64+
}
65+
66+
/**
67+
* Example of asynchronously getting the topic's latest information.
68+
*/
69+
// [TARGET reloadAsync()]
70+
public Topic reloadAsync() throws ExecutionException, InterruptedException {
71+
// [START reloadAsync]
72+
Future<Topic> future = topic.reloadAsync();
73+
// ...
74+
Topic latestTopic = future.get();
75+
if (latestTopic == null) {
76+
// the topic was not found
77+
}
78+
// [END reloadAsync]
79+
return latestTopic;
80+
}
81+
82+
/**
83+
* Example of deleting the topic.
84+
*/
85+
// [TARGET delete()]
86+
public boolean delete() {
87+
// [START delete]
88+
boolean deleted = topic.delete();
89+
if (deleted) {
90+
// the topic was deleted
91+
} else {
92+
// the topic was not found
93+
}
94+
// [END delete]
95+
return deleted;
96+
}
97+
98+
/**
99+
* Example of asynchronously deleting the topic.
100+
*/
101+
// [TARGET deleteAsync()]
102+
public boolean deleteAsync() throws ExecutionException, InterruptedException {
103+
// [START deleteAsync]
104+
Future<Boolean> future = topic.deleteAsync();
105+
// ...
106+
boolean deleted = future.get();
107+
if (deleted) {
108+
// the topic was deleted
109+
} else {
110+
// the topic was not found
111+
}
112+
// [END deleteAsync]
113+
return deleted;
114+
}
115+
116+
/**
117+
* Example of publishing one message to the topic.
118+
*/
119+
// [TARGET publish(Message)]
120+
public String publishOneMessage() {
121+
// [START publishOneMessage]
122+
Message message = Message.of("payload");
123+
String messageId = topic.publish(message);
124+
// [END publishOneMessage]
125+
return messageId;
126+
}
127+
128+
/**
129+
* Example of asynchronously publishing one message to the topic.
130+
*/
131+
// [TARGET publishAsync(Message)]
132+
public String publishOneMessageAsync()
133+
throws ExecutionException, InterruptedException {
134+
// [START publishOneMessageAsync]
135+
Message message = Message.of("payload");
136+
Future<String> future = topic.publishAsync(message);
137+
// ...
138+
String messageId = future.get();
139+
// [END publishOneMessageAsync]
140+
return messageId;
141+
}
142+
143+
144+
/**
145+
* Example of publishing a list of messages to the topic.
146+
*/
147+
// [TARGET publish(Iterable)]
148+
public List<String> publishMessageList() {
149+
// [START publishMessageList]
150+
List<Message> messages = new LinkedList<>();
151+
messages.add(Message.of("payload1"));
152+
messages.add(Message.of("payload2"));
153+
List<String> messageIds = topic.publish(messages);
154+
// [END publishMessageList]
155+
return messageIds;
156+
}
157+
158+
/**
159+
* Example of asynchronously publishing a list of messages to the topic.
160+
*/
161+
// [TARGET publishAsync(Iterable)]
162+
public List<String> publishMessageListAsync()
163+
throws ExecutionException, InterruptedException {
164+
// [START publishMessageListAsync]
165+
List<Message> messages = new LinkedList<>();
166+
messages.add(Message.of("payload1"));
167+
messages.add(Message.of("payload2"));
168+
Future<List<String>> future = topic.publishAsync(messages);
169+
// ...
170+
List<String> messageIds = future.get();
171+
// [END publishMessageListAsync]
172+
return messageIds;
173+
}
174+
175+
/**
176+
* Example of publishing some messages to the topic.
177+
*/
178+
// [TARGET publish(Message, Message...)]
179+
public List<String> publishMessages() {
180+
// [START publishMessages]
181+
Message message1 = Message.of("payload1");
182+
Message message2 = Message.of("payload2");
183+
List<String> messageIds = topic.publish(message1, message2);
184+
// [END publishMessages]
185+
return messageIds;
186+
}
187+
188+
/**
189+
* Example of asynchronously publishing some messages to the topic.
190+
*/
191+
// [TARGET publishAsync(Message, Message...)]
192+
public List<String> publishMessagesAsync()
193+
throws ExecutionException, InterruptedException {
194+
// [START publishMessagesAsync]
195+
Message message1 = Message.of("payload1");
196+
Message message2 = Message.of("payload2");
197+
Future<List<String>> future = topic.publishAsync(message1, message2);
198+
// ...
199+
List<String> messageIds = future.get();
200+
// [END publishMessagesAsync]
201+
return messageIds;
202+
}
203+
204+
/**
205+
* Example of listing subscriptions for the topic, specifying the page size.
206+
*/
207+
// [TARGET listSubscriptions(ListOption...)]
208+
public Page<SubscriptionId> listSubscriptionsForTopic() {
209+
// [START listSubscriptionsForTopic]
210+
Page<SubscriptionId> subscriptions = topic.listSubscriptions(ListOption.pageSize(100));
211+
Iterator<SubscriptionId> subscriptionIterator = subscriptions.iterateAll();
212+
while (subscriptionIterator.hasNext()) {
213+
SubscriptionId subscription = subscriptionIterator.next();
214+
// do something with the subscription identity
215+
}
216+
// [END listSubscriptionsForTopic]
217+
return subscriptions;
218+
}
219+
220+
/**
221+
* Example of asynchronously listing subscriptions for the topic, specifying the page size.
222+
*/
223+
// [TARGET listSubscriptionsAsync(ListOption...)]
224+
public Page<SubscriptionId> listSubscriptionsForTopicAsync()
225+
throws ExecutionException, InterruptedException {
226+
// [START listSubscriptionsForTopicAsync]
227+
Future<AsyncPage<SubscriptionId>> future =
228+
topic.listSubscriptionsAsync(ListOption.pageSize(100));
229+
// ...
230+
AsyncPage<SubscriptionId> subscriptions = future.get();
231+
Iterator<SubscriptionId> subscriptionIterator = subscriptions.iterateAll();
232+
while (subscriptionIterator.hasNext()) {
233+
SubscriptionId subscription = subscriptionIterator.next();
234+
// do something with the subscription identity
235+
}
236+
// [END listSubscriptionsForTopicAsync]
237+
return subscriptions;
238+
}
239+
240+
/**
241+
* Example of getting the topic's policy.
242+
*/
243+
// [TARGET getPolicy()]
244+
public Policy getPolicy() {
245+
// [START getPolicy]
246+
Policy policy = topic.getPolicy();
247+
if (policy == null) {
248+
// topic was not found
249+
}
250+
// [END getPolicy]
251+
return policy;
252+
}
253+
254+
/**
255+
* Example of asynchronously getting the topic's policy.
256+
*/
257+
// [TARGET getPolicyAsync()]
258+
public Policy getPolicyAsync() throws ExecutionException, InterruptedException {
259+
// [START getPolicyAsync]
260+
Future<Policy> future = topic.getPolicyAsync();
261+
// ...
262+
Policy policy = future.get();
263+
if (policy == null) {
264+
// topic was not found
265+
}
266+
// [END getPolicyAsync]
267+
return policy;
268+
}
269+
270+
/**
271+
* Example of replacing the topic's policy.
272+
*/
273+
// [TARGET replacePolicy(Policy)]
274+
public Policy replacePolicy() {
275+
// [START replacePolicy]
276+
Policy policy = topic.getPolicy();
277+
Policy updatedPolicy = policy.toBuilder()
278+
.addIdentity(Role.viewer(), Identity.allAuthenticatedUsers())
279+
.build();
280+
updatedPolicy = topic.replacePolicy(updatedPolicy);
281+
// [END replacePolicy]
282+
return updatedPolicy;
283+
}
284+
285+
/**
286+
* Example of asynchronously replacing the topic's policy.
287+
*/
288+
// [TARGET replacePolicyAsync(Policy)]
289+
public Policy replacePolicyAsync()
290+
throws ExecutionException, InterruptedException {
291+
// [START replacePolicyAsync]
292+
Policy policy = topic.getPolicy();
293+
Policy updatedPolicy = policy.toBuilder()
294+
.addIdentity(Role.viewer(), Identity.allAuthenticatedUsers())
295+
.build();
296+
Future<Policy> future = topic.replacePolicyAsync(updatedPolicy);
297+
// ...
298+
updatedPolicy = future.get();
299+
// [END replacePolicyAsync]
300+
return updatedPolicy;
301+
}
302+
303+
/**
304+
* Example of testing whether the caller has the provided permissions on the topic.
305+
*/
306+
// [TARGET testPermissions(List)]
307+
public List<Boolean> testPermissions() {
308+
// [START testPermissions]
309+
List<String> permissions = new LinkedList<>();
310+
permissions.add("pubsub.topics.get");
311+
List<Boolean> testedPermissions = topic.testPermissions(permissions);
312+
// [END testPermissions]
313+
return testedPermissions;
314+
}
315+
316+
/**
317+
* Example of asynchronously testing whether the caller has the provided permissions on the
318+
* topic.
319+
*/
320+
// [TARGET testPermissionsAsync(List)]
321+
public List<Boolean> testPermissionsAsync()
322+
throws ExecutionException, InterruptedException {
323+
// [START testPermissionsAsync]
324+
List<String> permissions = new LinkedList<>();
325+
permissions.add("pubsub.topics.get");
326+
Future<List<Boolean>> future = topic.testPermissionsAsync(permissions);
327+
// ...
328+
List<Boolean> testedPermissions = future.get();
329+
// [END testPermissionsAsync]
330+
return testedPermissions;
331+
}
332+
}

0 commit comments

Comments
 (0)