Search in sources :

Example 1 with ListTopicsPagedResponse

use of com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientSnippets method listTopics.

/** Example of listing topics.  */
public ListTopicsPagedResponse listTopics() throws Exception {
    // [START pubsub_list_topics]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        ListTopicsRequest listTopicsRequest = ListTopicsRequest.newBuilder().setProjectWithProjectName(ProjectName.create(projectId)).build();
        ListTopicsPagedResponse response = topicAdminClient.listTopics(listTopicsRequest);
        Iterable<Topic> topics = response.iterateAll();
        for (Topic topic : topics) {
        // do something with the topic
        }
        return response;
    }
// [END pubsub_list_topics]
}
Also used : ListTopicsPagedResponse(com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) ListTopicsRequest(com.google.pubsub.v1.ListTopicsRequest) Topic(com.google.pubsub.v1.Topic)

Example 2 with ListTopicsPagedResponse

use of com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse in project google-cloud-java by GoogleCloudPlatform.

the class ITTopicAdminClientSnippets method listTopicsRetreivesAddedTopics.

@Test
public void listTopicsRetreivesAddedTopics() throws Exception {
    List<Topic> addedTopics = new ArrayList<>();
    String topicName1 = topics[0];
    addedTopics.add(topicAdminClientSnippets.createTopic(topicName1));
    String topicName2 = topics[1];
    addedTopics.add(topicAdminClientSnippets.createTopic(topicName2));
    boolean[] topicFound = { false, false };
    ListTopicsPagedResponse response = topicAdminClientSnippets.listTopics();
    assertNotNull(response);
    Iterable<Topic> topics = response.iterateAll();
    for (int i = 0; i < 2; i++) {
        if (!topicFound[i]) {
            topicFound[i] = Iterables.contains(topics, addedTopics.get(i));
        }
    }
    assertTrue(topicFound[0] && topicFound[1]);
}
Also used : ListTopicsPagedResponse(com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse) ArrayList(java.util.ArrayList) Topic(com.google.pubsub.v1.Topic) Test(org.junit.Test)

Example 3 with ListTopicsPagedResponse

use of com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientTest method listTopicsTest.

@Test
@SuppressWarnings("all")
public void listTopicsTest() {
    String nextPageToken = "";
    Topic topicsElement = Topic.newBuilder().build();
    List<Topic> topics = Arrays.asList(topicsElement);
    ListTopicsResponse expectedResponse = ListTopicsResponse.newBuilder().setNextPageToken(nextPageToken).addAllTopics(topics).build();
    mockPublisher.addResponse(expectedResponse);
    ProjectName project = ProjectName.create("[PROJECT]");
    ListTopicsPagedResponse pagedListResponse = client.listTopics(project);
    List<Topic> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getTopicsList().get(0), resources.get(0));
    List<GeneratedMessageV3> actualRequests = mockPublisher.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListTopicsRequest actualRequest = (ListTopicsRequest) actualRequests.get(0);
    Assert.assertEquals(project, actualRequest.getProjectAsProjectName());
}
Also used : ListTopicsPagedResponse(com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse) ProjectName(com.google.pubsub.v1.ProjectName) ListTopicsRequest(com.google.pubsub.v1.ListTopicsRequest) ByteString(com.google.protobuf.ByteString) Topic(com.google.pubsub.v1.Topic) ListTopicsResponse(com.google.pubsub.v1.ListTopicsResponse) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Aggregations

ListTopicsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicsPagedResponse)3 Topic (com.google.pubsub.v1.Topic)3 ListTopicsRequest (com.google.pubsub.v1.ListTopicsRequest)2 Test (org.junit.Test)2 TopicAdminClient (com.google.cloud.pubsub.spi.v1.TopicAdminClient)1 ByteString (com.google.protobuf.ByteString)1 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)1 ListTopicsResponse (com.google.pubsub.v1.ListTopicsResponse)1 ProjectName (com.google.pubsub.v1.ProjectName)1 ArrayList (java.util.ArrayList)1