Search in sources :

Example 26 with PubsubMessage

use of com.google.pubsub.v1.PubsubMessage in project spring-cloud-gcp by spring-cloud.

the class SimplePubSubMessageConverterTests method testNullHeaders.

@Test
public void testNullHeaders() {
    SimplePubSubMessageConverter converter = new SimplePubSubMessageConverter();
    PubsubMessage pubsubMessage = converter.toPubSubMessage(TEST_STRING, null);
    assertThat(pubsubMessage.getData().toString(Charset.defaultCharset())).isEqualTo(TEST_STRING);
    assertThat(pubsubMessage.getAttributesMap()).isEqualTo(new HashMap<>());
}
Also used : PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.Test)

Example 27 with PubsubMessage

use of com.google.pubsub.v1.PubsubMessage in project spring-cloud-gcp by spring-cloud.

the class PubSubSubscriberTemplate method pullAndAckAsync.

@Override
public ListenableFuture<List<PubsubMessage>> pullAndAckAsync(String subscription, Integer maxMessages, Boolean returnImmediately) {
    PullRequest pullRequest = this.subscriberFactory.createPullRequest(subscription, maxMessages, returnImmediately);
    final SettableListenableFuture<List<PubsubMessage>> settableFuture = new SettableListenableFuture<>();
    this.pullAsync(pullRequest).addCallback(ackableMessages -> {
        if (!ackableMessages.isEmpty()) {
            ack(ackableMessages);
        }
        List<PubsubMessage> messages = ackableMessages.stream().map(AcknowledgeablePubsubMessage::getPubsubMessage).collect(Collectors.toList());
        settableFuture.set(messages);
    }, settableFuture::setException);
    return settableFuture;
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) PullRequest(com.google.pubsub.v1.PullRequest) List(java.util.List) BasicAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ConvertedAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage) AcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage) ConvertedBasicAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedBasicAcknowledgeablePubsubMessage)

Example 28 with PubsubMessage

use of com.google.pubsub.v1.PubsubMessage in project spring-cloud-gcp by spring-cloud.

the class PubSubSubscriberTemplate method pullAndAck.

@Override
public List<PubsubMessage> pullAndAck(String subscription, Integer maxMessages, Boolean returnImmediately) {
    PullRequest pullRequest = this.subscriberFactory.createPullRequest(subscription, maxMessages, returnImmediately);
    List<AcknowledgeablePubsubMessage> ackableMessages = pull(pullRequest);
    if (!ackableMessages.isEmpty()) {
        ack(ackableMessages);
    }
    return ackableMessages.stream().map(AcknowledgeablePubsubMessage::getPubsubMessage).collect(Collectors.toList());
}
Also used : PullRequest(com.google.pubsub.v1.PullRequest) BasicAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage) ConvertedAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage) AcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage) ConvertedBasicAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedBasicAcknowledgeablePubsubMessage)

Example 29 with PubsubMessage

use of com.google.pubsub.v1.PubsubMessage in project spring-cloud-gcp by spring-cloud.

the class PubSubTemplateTests method testPublish_Object.

@Test
public void testPublish_Object() throws IOException {
    AllowedPayload allowedPayload = new AllowedPayload();
    allowedPayload.name = "allowed";
    allowedPayload.value = 12345;
    PubSubPublisherTemplate pubSubPublisherTemplate = spy(createPublisherTemplate());
    doAnswer((invocation) -> {
        PubsubMessage message = invocation.getArgument(1);
        assertThat(message.getData().toStringUtf8()).isEqualTo("{\"@class\":" + "\"org.springframework.cloud.gcp.pubsub.core.test.allowed.AllowedPayload\"" + ",\"name\":\"allowed\",\"value\":12345}");
        return null;
    }).when(pubSubPublisherTemplate).publish(eq("test"), any());
    pubSubPublisherTemplate.publish("test", allowedPayload);
    verify(pubSubPublisherTemplate, times(1)).publish(eq("test"), isA(PubsubMessage.class));
}
Also used : PubSubPublisherTemplate(org.springframework.cloud.gcp.pubsub.core.publisher.PubSubPublisherTemplate) AllowedPayload(org.springframework.cloud.gcp.pubsub.core.test.allowed.AllowedPayload) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.Test)

Example 30 with PubsubMessage

use of com.google.pubsub.v1.PubsubMessage in project spring-cloud-gcp by spring-cloud.

the class JacksonPubSubMessageConverterTests method testString.

@Test
public void testString() throws JSONException {
    String str = "test 123";
    PubsubMessage pubsubMessage = this.converter.toPubSubMessage(str, null);
    JSONAssert.assertEquals("\"test 123\"", pubsubMessage.getData().toStringUtf8(), true);
    Object o = this.converter.fromPubSubMessage(pubsubMessage, String.class);
    assertThat(o).as("verify that deserialized object is equal to the original one").isEqualTo(str);
}
Also used : PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.Test)

Aggregations

PubsubMessage (com.google.pubsub.v1.PubsubMessage)51 ByteString (com.google.protobuf.ByteString)28 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)15 Subscriber (com.google.cloud.pubsub.v1.Subscriber)12 List (java.util.List)12 AcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage)12 Publisher (com.google.cloud.pubsub.v1.Publisher)11 ConvertedAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage)11 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)10 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)9 BasicAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.BasicAcknowledgeablePubsubMessage)9 ConvertedBasicAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.converter.ConvertedBasicAcknowledgeablePubsubMessage)9 SettableApiFuture (com.google.api.core.SettableApiFuture)8 ServiceOptions (com.google.cloud.ServiceOptions)8 DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)8 Action (com.google.privacy.dlp.v2.Action)8 BigQueryTable (com.google.privacy.dlp.v2.BigQueryTable)8 CreateDlpJobRequest (com.google.privacy.dlp.v2.CreateDlpJobRequest)8 DlpJob (com.google.privacy.dlp.v2.DlpJob)8