Search in sources :

Example 1 with PubSubAdmin

use of org.springframework.cloud.gcp.pubsub.PubSubAdmin in project spring-cloud-gcp by spring-cloud.

the class PubSubTemplateIntegrationTests method testPubSubTemplateLoadsMessageConverter.

@Test
public void testPubSubTemplateLoadsMessageConverter() {
    this.contextRunner.withUserConfiguration(JsonPayloadTestConfiguration.class).run((context) -> {
        PubSubAdmin pubSubAdmin = context.getBean(PubSubAdmin.class);
        PubSubTemplate pubSubTemplate = context.getBean(PubSubTemplate.class);
        String topicName = "json-payload-topic" + UUID.randomUUID();
        String subscriptionName = "json-payload-subscription" + UUID.randomUUID();
        pubSubAdmin.createTopic(topicName);
        pubSubAdmin.createSubscription(subscriptionName, topicName, 10);
        TestUser user = new TestUser("John", "password");
        pubSubTemplate.publish(topicName, user);
        await().atMost(Duration.TEN_SECONDS).untilAsserted(() -> {
            List<ConvertedAcknowledgeablePubsubMessage<TestUser>> messages = pubSubTemplate.pullAndConvert(subscriptionName, 1, true, TestUser.class);
            assertThat(messages).hasSize(1);
            TestUser receivedTestUser = messages.get(0).getPayload();
            assertThat(receivedTestUser.username).isEqualTo("John");
            assertThat(receivedTestUser.password).isEqualTo("password");
        });
        pubSubAdmin.deleteSubscription(subscriptionName);
        pubSubAdmin.deleteTopic(topicName);
    });
}
Also used : ConvertedAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage) ByteString(com.google.protobuf.ByteString) PubSubTemplate(org.springframework.cloud.gcp.pubsub.core.PubSubTemplate) PubSubAdmin(org.springframework.cloud.gcp.pubsub.PubSubAdmin) Test(org.junit.Test)

Example 2 with PubSubAdmin

use of org.springframework.cloud.gcp.pubsub.PubSubAdmin in project spring-cloud-gcp by spring-cloud.

the class PubSubTemplateIntegrationTests method testCreatePublishPullNextAndDelete.

@Test
public void testCreatePublishPullNextAndDelete() {
    this.contextRunner.run((context) -> {
        PubSubAdmin pubSubAdmin = context.getBean(PubSubAdmin.class);
        PubSubTemplate pubSubTemplate = context.getBean(PubSubTemplate.class);
        String topicName = "tarkus_" + UUID.randomUUID();
        String subscriptionName = "zatoichi_" + UUID.randomUUID();
        assertThat(pubSubAdmin.getTopic(topicName)).isNull();
        assertThat(pubSubAdmin.getSubscription(subscriptionName)).isNull();
        pubSubAdmin.createTopic(topicName);
        pubSubAdmin.createSubscription(subscriptionName, topicName);
        Map<String, String> headers = new HashMap<>();
        headers.put("cactuar", "tonberry");
        headers.put("fujin", "raijin");
        pubSubTemplate.publish(topicName, "tatatatata", headers).get();
        // get message
        AtomicReference<PubsubMessage> pubsubMessageRef = new AtomicReference<>();
        Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> {
            pubsubMessageRef.set(pubSubTemplate.pullNext(subscriptionName));
            return pubsubMessageRef.get() != null;
        });
        PubsubMessage pubsubMessage = pubsubMessageRef.get();
        assertThat(pubsubMessage.getData()).isEqualTo(ByteString.copyFromUtf8("tatatatata"));
        assertThat(pubsubMessage.getAttributesCount()).isEqualTo(2);
        assertThat(pubsubMessage.getAttributesOrThrow("cactuar")).isEqualTo("tonberry");
        assertThat(pubsubMessage.getAttributesOrThrow("fujin")).isEqualTo("raijin");
        assertThat(pubSubAdmin.getTopic(topicName)).isNotNull();
        assertThat(pubSubAdmin.getSubscription(subscriptionName)).isNotNull();
        assertThat(pubSubAdmin.listTopics().stream().filter((topic) -> topic.getName().endsWith(topicName)).toArray().length).isEqualTo(1);
        assertThat(pubSubAdmin.listSubscriptions().stream().filter((subscription) -> subscription.getName().endsWith(subscriptionName)).toArray().length).isEqualTo(1);
        pubSubAdmin.deleteSubscription(subscriptionName);
        pubSubAdmin.deleteTopic(topicName);
        assertThat(pubSubAdmin.getTopic(topicName)).isNull();
        assertThat(pubSubAdmin.getSubscription(subscriptionName)).isNull();
        assertThat(pubSubAdmin.listTopics().stream().filter((topic) -> topic.getName().endsWith(topicName)).toArray().length).isEqualTo(0);
        assertThat(pubSubAdmin.listSubscriptions().stream().filter((subscription) -> subscription.getName().endsWith(subscriptionName)).toArray().length).isEqualTo(0);
    });
}
Also used : HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteString(com.google.protobuf.ByteString) PubSubTemplate(org.springframework.cloud.gcp.pubsub.core.PubSubTemplate) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ConvertedAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage) AcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage) PubSubAdmin(org.springframework.cloud.gcp.pubsub.PubSubAdmin) Test(org.junit.Test)

Example 3 with PubSubAdmin

use of org.springframework.cloud.gcp.pubsub.PubSubAdmin in project spring-cloud-gcp by spring-cloud.

the class PubSubTemplateIntegrationTests method testPullAndAck.

@Test
public void testPullAndAck() {
    this.contextRunner.run((context) -> {
        PubSubAdmin pubSubAdmin = context.getBean(PubSubAdmin.class);
        String topicName = "peel-the-paint" + UUID.randomUUID();
        String subscriptionName = "i-lost-my-head" + UUID.randomUUID();
        pubSubAdmin.createTopic(topicName);
        pubSubAdmin.createSubscription(subscriptionName, topicName, 10);
        PubSubTemplate pubSubTemplate = context.getBean(PubSubTemplate.class);
        List<Future<String>> futures = new ArrayList<>();
        futures.add(pubSubTemplate.publish(topicName, "message1"));
        futures.add(pubSubTemplate.publish(topicName, "message2"));
        futures.add(pubSubTemplate.publish(topicName, "message3"));
        futures.parallelStream().forEach((f) -> {
            try {
                f.get(5, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException ex) {
                LOGGER.error(ex);
                Thread.currentThread().interrupt();
            }
        });
        List<AcknowledgeablePubsubMessage> ackableMessages = new ArrayList<>();
        Set<String> messagesSet = new HashSet<>();
        for (int i = 0; i < 5 && messagesSet.size() < 3; i++) {
            List<AcknowledgeablePubsubMessage> newMessages = pubSubTemplate.pull(subscriptionName, 4, false);
            ackableMessages.addAll(newMessages);
            messagesSet.addAll(newMessages.stream().map((message) -> message.getPubsubMessage().getData().toStringUtf8()).collect(Collectors.toList()));
        }
        assertThat(messagesSet.size()).as("check that we received all the messages").isEqualTo(3);
        ackableMessages.forEach((message) -> {
            try {
                if (message.getPubsubMessage().getData().toStringUtf8().equals("message1")) {
                    // sync call
                    message.ack().get();
                } else {
                    // sync call
                    message.nack().get();
                }
            } catch (InterruptedException | ExecutionException ex) {
                LOGGER.error(ex);
                Thread.currentThread().interrupt();
            }
        });
        Thread.sleep(11_000);
        // pull the 2 nacked messages with retries for up to 10s
        int messagesCount = 0;
        int tries = 100;
        while (messagesCount < 2 && tries > 0) {
            Thread.sleep(100);
            ackableMessages = pubSubTemplate.pull(subscriptionName, 4, true);
            ackableMessages.forEach((m) -> m.ack());
            messagesCount += ackableMessages.size();
            tries--;
        }
        assertThat(messagesCount).as("check that we get both nacked messages back").isEqualTo(2);
        pubSubAdmin.deleteSubscription(subscriptionName);
        pubSubAdmin.deleteTopic(topicName);
    });
}
Also used : ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) ConvertedAcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage) AcknowledgeablePubsubMessage(org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage) PubSubTemplate(org.springframework.cloud.gcp.pubsub.core.PubSubTemplate) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) PubSubAdmin(org.springframework.cloud.gcp.pubsub.PubSubAdmin) TimeoutException(java.util.concurrent.TimeoutException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with PubSubAdmin

use of org.springframework.cloud.gcp.pubsub.PubSubAdmin in project spring-cloud-gcp by spring-cloud.

the class PubSubChannelAdaptersIntegrationTests method enableTests.

@BeforeClass
public static void enableTests() throws IOException {
    assumeThat(System.getProperty("it.pubsub")).isEqualTo("true");
    pubSubAdmin = new PubSubAdmin(new DefaultGcpProjectIdProvider(), new DefaultCredentialsProvider(() -> new Credentials()));
}
Also used : DefaultCredentialsProvider(org.springframework.cloud.gcp.core.DefaultCredentialsProvider) Credentials(org.springframework.cloud.gcp.core.Credentials) PubSubAdmin(org.springframework.cloud.gcp.pubsub.PubSubAdmin) DefaultGcpProjectIdProvider(org.springframework.cloud.gcp.core.DefaultGcpProjectIdProvider) BeforeClass(org.junit.BeforeClass)

Example 5 with PubSubAdmin

use of org.springframework.cloud.gcp.pubsub.PubSubAdmin in project spring-cloud-gcp by spring-cloud.

the class PubSubTemplateDocumentationTests method pubSubTest.

private void pubSubTest(PubSubTest pubSubTest, Class... configClass) {
    ApplicationContextRunner contextRunner = configClass.length == 0 ? this.contextRunner : this.contextRunner.withUserConfiguration(configClass[0]);
    contextRunner.run((context) -> {
        PubSubAdmin pubSubAdmin = context.getBean(PubSubAdmin.class);
        PubSubTemplate pubSubTemplate = context.getBean(PubSubTemplate.class);
        String subscriptionName = "test_subscription_" + UUID.randomUUID();
        String topicName = "test_topic_" + UUID.randomUUID();
        try {
            assertThat(pubSubAdmin.getTopic(topicName)).isNull();
            assertThat(pubSubAdmin.getSubscription(subscriptionName)).isNull();
            // tag::create_topic[]
            pubSubAdmin.createTopic(topicName);
            // end::create_topic[]
            // tag::create_subscription[]
            pubSubAdmin.createSubscription(subscriptionName, topicName);
            // end::create_subscription[]
            pubSubTest.run(pubSubTemplate, subscriptionName, topicName);
        } finally {
            // tag::list_subscriptions[]
            List<String> subscriptions = pubSubAdmin.listSubscriptions().stream().map(Subscription::getName).collect(Collectors.toList());
            // end::list_subscriptions[]
            // tag::list_topics[]
            List<String> topics = pubSubAdmin.listTopics().stream().map(Topic::getName).collect(Collectors.toList());
            // end::list_topics[]
            pubSubAdmin.deleteSubscription(subscriptionName);
            pubSubAdmin.deleteTopic(topicName);
            assertThat(subscriptions.stream().map(this::getLastPart)).contains(subscriptionName);
            assertThat(topics.stream().map(this::getLastPart)).contains(topicName);
        }
    });
}
Also used : ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) ByteString(com.google.protobuf.ByteString) PubSubTemplate(org.springframework.cloud.gcp.pubsub.core.PubSubTemplate) PubSubAdmin(org.springframework.cloud.gcp.pubsub.PubSubAdmin)

Aggregations

PubSubAdmin (org.springframework.cloud.gcp.pubsub.PubSubAdmin)5 ByteString (com.google.protobuf.ByteString)4 PubSubTemplate (org.springframework.cloud.gcp.pubsub.core.PubSubTemplate)4 Test (org.junit.Test)3 ConvertedAcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.converter.ConvertedAcknowledgeablePubsubMessage)3 AcknowledgeablePubsubMessage (org.springframework.cloud.gcp.pubsub.support.AcknowledgeablePubsubMessage)2 PubsubMessage (com.google.pubsub.v1.PubsubMessage)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BeforeClass (org.junit.BeforeClass)1 ApplicationContextRunner (org.springframework.boot.test.context.runner.ApplicationContextRunner)1 Credentials (org.springframework.cloud.gcp.core.Credentials)1 DefaultCredentialsProvider (org.springframework.cloud.gcp.core.DefaultCredentialsProvider)1 DefaultGcpProjectIdProvider (org.springframework.cloud.gcp.core.DefaultGcpProjectIdProvider)1