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);
});
}
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);
});
}
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);
});
}
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()));
}
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);
}
});
}
Aggregations