Search in sources :

Example 1 with ListTopicSubscriptionsPagedResponse

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

the class TopicAdminClientSnippets method listTopicSubscriptions.

/** Example of listing topics for a subscription. */
public ListTopicSubscriptionsPagedResponse listTopicSubscriptions(String topicId) throws Exception {
    // [START pubsub_list_topic_subscriptions]
    try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
        TopicName topicName = TopicName.create(projectId, topicId);
        ListTopicSubscriptionsRequest request = ListTopicSubscriptionsRequest.newBuilder().setTopicWithTopicName(topicName).build();
        ListTopicSubscriptionsPagedResponse response = topicAdminClient.listTopicSubscriptions(request);
        Iterable<String> subscriptionNames = response.iterateAll();
        for (String subscriptionName : subscriptionNames) {
        // do something with the subscription name
        }
        return response;
    }
// [END pubsub_list_topic_subscriptions]
}
Also used : ListTopicSubscriptionsPagedResponse(com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse) TopicAdminClient(com.google.cloud.pubsub.spi.v1.TopicAdminClient) ListTopicSubscriptionsRequest(com.google.pubsub.v1.ListTopicSubscriptionsRequest) TopicName(com.google.pubsub.v1.TopicName)

Example 2 with ListTopicSubscriptionsPagedResponse

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

the class TopicAdminClientTest method listTopicSubscriptionsTest.

@Test
@SuppressWarnings("all")
public void listTopicSubscriptionsTest() {
    String nextPageToken = "";
    SubscriptionName subscriptionsElement = SubscriptionName.create("[PROJECT]", "[SUBSCRIPTION]");
    List<SubscriptionName> subscriptions = Arrays.asList(subscriptionsElement);
    ListTopicSubscriptionsResponse expectedResponse = ListTopicSubscriptionsResponse.newBuilder().setNextPageToken(nextPageToken).addAllSubscriptionsWithSubscriptionNameList(subscriptions).build();
    mockPublisher.addResponse(expectedResponse);
    TopicName topic = TopicName.create("[PROJECT]", "[TOPIC]");
    ListTopicSubscriptionsPagedResponse pagedListResponse = client.listTopicSubscriptions(topic);
    List<String> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getSubscriptionsList().get(0), resources.get(0));
    List<SubscriptionName> resourceNames = Lists.newArrayList(pagedListResponse.iterateAllAsSubscriptionName());
    Assert.assertEquals(1, resourceNames.size());
    Assert.assertEquals(expectedResponse.getSubscriptionsListAsSubscriptionNameList().get(0), resourceNames.get(0));
    List<GeneratedMessageV3> actualRequests = mockPublisher.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListTopicSubscriptionsRequest actualRequest = (ListTopicSubscriptionsRequest) actualRequests.get(0);
    Assert.assertEquals(topic, actualRequest.getTopicAsTopicName());
}
Also used : ListTopicSubscriptionsPagedResponse(com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse) ListTopicSubscriptionsRequest(com.google.pubsub.v1.ListTopicSubscriptionsRequest) ByteString(com.google.protobuf.ByteString) SubscriptionName(com.google.pubsub.v1.SubscriptionName) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) ListTopicSubscriptionsResponse(com.google.pubsub.v1.ListTopicSubscriptionsResponse) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Example 3 with ListTopicSubscriptionsPagedResponse

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

the class ITTopicAdminClientSnippets method listTopicSubscriptionsRetrievesAddedSubscriptions.

@Test
public void listTopicSubscriptionsRetrievesAddedSubscriptions() throws Exception {
    List<String> addedSubscriptions = new ArrayList<>();
    String topicName1 = topics[0];
    topicAdminClientSnippets.createTopic(topicName1);
    String subscriptionName1 = subscriptions[0];
    String subscriptionName2 = subscriptions[1];
    addedSubscriptions.add(createSubscription(topicName1, subscriptionName1));
    addedSubscriptions.add(createSubscription(topicName1, subscriptionName2));
    boolean[] subFound = { false, false };
    ListTopicSubscriptionsPagedResponse response = topicAdminClientSnippets.listTopicSubscriptions(topicName1);
    assertNotNull(response);
    Iterable<String> subscriptions = response.iterateAll();
    for (int i = 0; i < 2; i++) {
        if (!subFound[i]) {
            subFound[i] = Iterables.contains(subscriptions, addedSubscriptions.get(i));
        }
    }
    assertTrue(subFound[0] && subFound[1]);
}
Also used : ListTopicSubscriptionsPagedResponse(com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ListTopicSubscriptionsPagedResponse (com.google.cloud.pubsub.spi.v1.PagedResponseWrappers.ListTopicSubscriptionsPagedResponse)3 ListTopicSubscriptionsRequest (com.google.pubsub.v1.ListTopicSubscriptionsRequest)2 TopicName (com.google.pubsub.v1.TopicName)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 ListTopicSubscriptionsResponse (com.google.pubsub.v1.ListTopicSubscriptionsResponse)1 SubscriptionName (com.google.pubsub.v1.SubscriptionName)1 ArrayList (java.util.ArrayList)1