PubsubJsonClient extends PubsubClient { @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; } @VisibleForTesting  PubsubJsonClient(
      @Nullable String timestampAttribute, @Nullable String idAttribute, Pubsub pubsub); @Override void close(); @Override int publish(TopicPath topic, List<OutgoingMessage> outgoingMessages); @Override @SuppressWarnings("ProtoFieldNullComparison") List<IncomingMessage> pull(
      long requestTimeMsSinceEpoch,
      SubscriptionPath subscription,
      int batchSize,
      boolean returnImmediately); @Override void acknowledge(SubscriptionPath subscription, List<String> ackIds); @Override void modifyAckDeadline(
      SubscriptionPath subscription, List<String> ackIds, int deadlineSeconds); @Override void createTopic(TopicPath topic); @Override void deleteTopic(TopicPath topic); @Override List<TopicPath> listTopics(ProjectPath project); @Override void createSubscription(
      TopicPath topic, SubscriptionPath subscription, int ackDeadlineSeconds); @Override void deleteSubscription(SubscriptionPath subscription); @Override List<SubscriptionPath> listSubscriptions(ProjectPath project, TopicPath topic); @Override int ackDeadlineSeconds(SubscriptionPath subscription); @Override boolean isEOF(); static final PubsubClientFactory FACTORY; }
@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()); }