/** * Create a random subscription for {@code topic}. Return the {@link SubscriptionPath}. It is the * responsibility of the caller to later delete the subscription. */ public SubscriptionPath createRandomSubscription( ProjectPath project, TopicPath topic, int ackDeadlineSeconds) throws IOException { // Create a randomized subscription derived from the topic name. String subscriptionName = topic.getName() + "_beam_" + ThreadLocalRandom.current().nextLong(); SubscriptionPath subscription = PubsubClient.subscriptionPathFromName(project.getId(), subscriptionName); createSubscription(topic, subscription, ackDeadlineSeconds); return subscription; }
@Override public List<SubscriptionPath> listSubscriptions(ProjectPath project, TopicPath topic) throws IOException { Subscriptions.List request = pubsub.projects().subscriptions().list(project.getPath()); ListSubscriptionsResponse response = request.execute(); if (response.getSubscriptions() == null || response.getSubscriptions().isEmpty()) { return ImmutableList.of(); } List<SubscriptionPath> subscriptions = new ArrayList<>(response.getSubscriptions().size()); while (true) { for (Subscription subscription : response.getSubscriptions()) { if (subscription.getTopic().equals(topic.getPath())) { subscriptions.add(subscriptionPathFromPath(subscription.getName())); } } if (Strings.isNullOrEmpty(response.getNextPageToken())) { break; } request.setPageToken(response.getNextPageToken()); response = request.execute(); } return subscriptions; }
public static ProjectPath projectPathFromId(String projectId) { return new ProjectPath(String.format("projects/%s", projectId)); }
@Override public List<SubscriptionPath> listSubscriptions(ProjectPath project, TopicPath topic) throws IOException { ListSubscriptionsRequest.Builder request = ListSubscriptionsRequest.newBuilder() .setProject(project.getPath()) .setPageSize(LIST_BATCH_SIZE); ListSubscriptionsResponse response = subscriberStub().listSubscriptions(request.build()); if (response.getSubscriptionsCount() == 0) { return ImmutableList.of(); } List<SubscriptionPath> subscriptions = new ArrayList<>(response.getSubscriptionsCount()); while (true) { for (Subscription subscription : response.getSubscriptionsList()) { if (subscription.getTopic().equals(topic.getPath())) { subscriptions.add(subscriptionPathFromPath(subscription.getName())); } } if (response.getNextPageToken().isEmpty()) { break; } request.setPageToken(response.getNextPageToken()); response = subscriberStub().listSubscriptions(request.build()); } return subscriptions; }
@Override public List<TopicPath> listTopics(ProjectPath project) throws IOException { ListTopicsRequest.Builder request = ListTopicsRequest.newBuilder().setProject(project.getPath()).setPageSize(LIST_BATCH_SIZE); ListTopicsResponse response = publisherStub().listTopics(request.build()); if (response.getTopicsCount() == 0) { return ImmutableList.of(); } List<TopicPath> topics = new ArrayList<>(response.getTopicsCount()); while (true) { for (Topic topic : response.getTopicsList()) { topics.add(topicPathFromPath(topic.getName())); } if (response.getNextPageToken().isEmpty()) { break; } request.setPageToken(response.getNextPageToken()); response = publisherStub().listTopics(request.build()); } return topics; }
@Override public List<TopicPath> listTopics(ProjectPath project) throws IOException { Topics.List request = pubsub.projects().topics().list(project.getPath()); ListTopicsResponse response = request.execute(); if (response.getTopics() == null || response.getTopics().isEmpty()) { return ImmutableList.of(); } List<TopicPath> topics = new ArrayList<>(response.getTopics().size()); while (true) { for (Topic topic : response.getTopics()) { topics.add(topicPathFromPath(topic.getName())); } if (Strings.isNullOrEmpty(response.getNextPageToken())) { break; } request.setPageToken(response.getNextPageToken()); response = request.execute(); } return topics; }
public static ProjectPath projectPathFromPath(String path) { return new ProjectPath(path); }
@Test public void listTopics() throws Exception { ListTopicsResponse expectedResponse1 = new ListTopicsResponse(); expectedResponse1.setTopics(Collections.singletonList(buildTopic(1))); expectedResponse1.setNextPageToken("AVgJH3Z7aHxiDBs"); ListTopicsResponse expectedResponse2 = new ListTopicsResponse(); expectedResponse2.setTopics(Collections.singletonList(buildTopic(2))); Topics.List request = mockPubsub.projects().topics().list(PROJECT.getPath()); when((Object) (request.execute())).thenReturn(expectedResponse1, expectedResponse2); List<TopicPath> topicPaths = client.listTopics(PROJECT); assertEquals(2, topicPaths.size()); }
@Override public String toString() { return getPath(); } }
@Test public void listSubscriptions() throws Exception { ListSubscriptionsResponse expectedResponse1 = new ListSubscriptionsResponse(); expectedResponse1.setSubscriptions(Collections.singletonList(buildSubscription(1))); expectedResponse1.setNextPageToken("AVgJH3Z7aHxiDBs"); ListSubscriptionsResponse expectedResponse2 = new ListSubscriptionsResponse(); expectedResponse2.setSubscriptions(Collections.singletonList(buildSubscription(2))); Subscriptions.List request = mockPubsub.projects().subscriptions().list(PROJECT.getPath()); when((Object) (request.execute())).thenReturn(expectedResponse1, expectedResponse2); final TopicPath topic101 = PubsubClient.topicPathFromName("testProject", "Topic2"); List<SubscriptionPath> subscriptionPaths = client.listSubscriptions(PROJECT, topic101); assertEquals(1, subscriptionPaths.size()); }
private static Topic buildTopic(int i) { Topic topic = new Topic(); topic.setName(PubsubClient.topicPathFromName(PROJECT.getId(), "Topic" + i).getPath()); return topic; }
@Test public void projectPathFromIdWellFormed() { ProjectPath path = PubsubClient.projectPathFromId("test"); assertEquals("projects/test", path.getPath()); }
private static Subscription buildSubscription(int i) { Subscription subscription = new Subscription(); subscription.setName( PubsubClient.subscriptionPathFromName(PROJECT.getId(), "Subscription" + i).getPath()); subscription.setTopic(PubsubClient.topicPathFromName(PROJECT.getId(), "Topic" + i).getPath()); return subscription; } }