use of org.springframework.cloud.gcp.pubsub.core.PubSubTemplate in project spring-cloud-gcp by spring-cloud.
the class PubSubTemplateDocumentationTests method testCreatePublishPullNextAndDelete.
@Test
public void testCreatePublishPullNextAndDelete() {
pubSubTest((PubSubTemplate pubSubTemplate, String subscriptionName, String topicName) -> {
// tag::publish[]
Map<String, String> headers = Collections.singletonMap("key1", "val1");
pubSubTemplate.publish(topicName, "message", headers).get();
// end::publish[]
PubsubMessage pubsubMessage = pubSubTemplate.pullNext(subscriptionName);
assertThat(pubsubMessage.getData()).isEqualTo(ByteString.copyFromUtf8("message"));
assertThat(pubsubMessage.getAttributesCount()).isEqualTo(1);
assertThat(pubsubMessage.getAttributesOrThrow("key1")).isEqualTo("val1");
});
}
use of org.springframework.cloud.gcp.pubsub.core.PubSubTemplate 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);
}
});
}
Aggregations