Search in sources :

Example 11 with PubsubMessage

use of com.google.pubsub.v1.PubsubMessage in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientTest method publishTest.

@Test
@SuppressWarnings("all")
public void publishTest() {
    String messageIdsElement = "messageIdsElement-744837059";
    List<String> messageIds = Arrays.asList(messageIdsElement);
    PublishResponse expectedResponse = PublishResponse.newBuilder().addAllMessageIds(messageIds).build();
    mockPublisher.addResponse(expectedResponse);
    TopicName topic = TopicName.create("[PROJECT]", "[TOPIC]");
    ByteString data = ByteString.copyFromUtf8("-86");
    PubsubMessage messagesElement = PubsubMessage.newBuilder().setData(data).build();
    List<PubsubMessage> messages = Arrays.asList(messagesElement);
    PublishResponse actualResponse = client.publish(topic, messages);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockPublisher.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    PublishRequest actualRequest = (PublishRequest) actualRequests.get(0);
    Assert.assertEquals(topic, actualRequest.getTopicAsTopicName());
    Assert.assertEquals(messages, actualRequest.getMessagesList());
}
Also used : PublishResponse(com.google.pubsub.v1.PublishResponse) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) PublishRequest(com.google.pubsub.v1.PublishRequest) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName) Test(org.junit.Test)

Example 12 with PubsubMessage

use of com.google.pubsub.v1.PubsubMessage in project google-cloud-java by GoogleCloudPlatform.

the class TopicAdminClientTest method publishExceptionTest.

@Test
@SuppressWarnings("all")
public void publishExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockPublisher.addException(exception);
    try {
        TopicName topic = TopicName.create("[PROJECT]", "[TOPIC]");
        ByteString data = ByteString.copyFromUtf8("-86");
        PubsubMessage messagesElement = PubsubMessage.newBuilder().setData(data).build();
        List<PubsubMessage> messages = Arrays.asList(messagesElement);
        client.publish(topic, messages);
        Assert.fail("No exception raised");
    } catch (ApiException e) {
        Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
    }
}
Also used : ByteString(com.google.protobuf.ByteString) StatusRuntimeException(io.grpc.StatusRuntimeException) PubsubMessage(com.google.pubsub.v1.PubsubMessage) TopicName(com.google.pubsub.v1.TopicName) ApiException(com.google.api.gax.grpc.ApiException) Test(org.junit.Test)

Aggregations

PubsubMessage (com.google.pubsub.v1.PubsubMessage)12 ByteString (com.google.protobuf.ByteString)7 TopicName (com.google.pubsub.v1.TopicName)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 AckReplyConsumer (com.google.cloud.pubsub.spi.v1.AckReplyConsumer)3 MessageReceiver (com.google.cloud.pubsub.spi.v1.MessageReceiver)3 Publisher (com.google.cloud.pubsub.spi.v1.Publisher)3 Subscriber (com.google.cloud.pubsub.spi.v1.Subscriber)3 SubscriptionName (com.google.pubsub.v1.SubscriptionName)3 Timestamp (com.google.protobuf.Timestamp)2 PublishRequest (com.google.pubsub.v1.PublishRequest)2 PublishResponse (com.google.pubsub.v1.PublishResponse)2 PullRequest (com.google.pubsub.v1.PullRequest)2 PullResponse (com.google.pubsub.v1.PullResponse)2 ReceivedMessage (com.google.pubsub.v1.ReceivedMessage)2 Server (io.grpc.Server)2 StreamObserver (io.grpc.stub.StreamObserver)2 ApiFuture (com.google.api.core.ApiFuture)1 FlowControlException (com.google.api.gax.batching.FlowController.FlowControlException)1