PubsubJsonClient extends PubsubClient { @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; } @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 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()); }