use of com.google.api.services.pubsub.model.ReceivedMessage in project components by Talend.
the class PubSubOutputRuntimeTestIT method outputAvro.
private void outputAvro(Pipeline pipeline) throws IOException {
String testID = "avroBasicTest" + new Random().nextInt();
List<Person> expectedPersons = Person.genRandomList(testID, maxRecords);
List<String> expectedMessages = new ArrayList<>();
List<IndexedRecord> sendMessages = new ArrayList<>();
for (Person person : expectedPersons) {
expectedMessages.add(person.toAvroRecord().toString());
sendMessages.add(person.toAvroRecord());
}
PubSubOutputRuntime outputRuntime = new PubSubOutputRuntime();
outputRuntime.initialize(runtimeContainer, createOutput(createDatasetFromAvro(createDatastore(), topicName, Person.schema.toString())));
PCollection<IndexedRecord> output = (PCollection<IndexedRecord>) pipeline.apply(Create.of(sendMessages));
output.apply(outputRuntime);
pipeline.run().waitUntilFinish();
List<String> actual = new ArrayList<>();
while (true) {
List<ReceivedMessage> messages = client.pull(subscriptionName, maxRecords);
List<String> ackIds = new ArrayList<>();
for (ReceivedMessage message : messages) {
actual.add(Person.desFromAvroBytes(message.getMessage().decodeData()).toAvroRecord().toString());
ackIds.add(message.getAckId());
}
client.ack(subscriptionName, ackIds);
if (actual.size() >= maxRecords) {
break;
}
}
assertThat(actual, containsInAnyOrder(expectedMessages.toArray()));
}
use of com.google.api.services.pubsub.model.ReceivedMessage in project components by Talend.
the class PubSubOutputRuntimeTestIT method outputCsv.
private void outputCsv(Pipeline pipeline) throws IOException {
String testID = "csvBasicTest" + new Random().nextInt();
final String fieldDelimited = ";";
List<Person> expectedPersons = Person.genRandomList(testID, maxRecords);
List<String> expectedMessages = new ArrayList<>();
List<String[]> sendMessages = new ArrayList<>();
for (Person person : expectedPersons) {
expectedMessages.add(person.toCSV(fieldDelimited));
sendMessages.add(person.toCSV(fieldDelimited).split(fieldDelimited));
}
PubSubOutputRuntime outputRuntime = new PubSubOutputRuntime();
outputRuntime.initialize(runtimeContainer, createOutput(createDatasetFromCSV(createDatastore(), topicName, fieldDelimited)));
PCollection<IndexedRecord> records = (PCollection<IndexedRecord>) pipeline.apply(Create.of(sendMessages)).apply((PTransform) ConvertToIndexedRecord.of());
records.setCoder(LazyAvroCoder.of()).apply(outputRuntime);
pipeline.run().waitUntilFinish();
List<String> actual = new ArrayList<>();
while (true) {
List<ReceivedMessage> messages = client.pull(subscriptionName, maxRecords);
List<String> ackIds = new ArrayList<>();
for (ReceivedMessage message : messages) {
actual.add(new String(message.getMessage().decodeData()));
ackIds.add(message.getAckId());
}
client.ack(subscriptionName, ackIds);
if (actual.size() >= maxRecords) {
break;
}
}
assertThat(actual, containsInAnyOrder(expectedMessages.toArray()));
}
use of com.google.api.services.pubsub.model.ReceivedMessage in project beam by apache.
the class PubsubJsonClientTest method pullOneMessage.
@Test
public void pullOneMessage() throws IOException {
String expectedSubscription = SUBSCRIPTION.getPath();
PullRequest expectedRequest = new PullRequest().setReturnImmediately(true).setMaxMessages(10);
PubsubMessage expectedPubsubMessage = new PubsubMessage().setMessageId(MESSAGE_ID).encodeData(DATA.getBytes(StandardCharsets.UTF_8)).setPublishTime(String.valueOf(PUB_TIME)).setAttributes(ImmutableMap.of(TIMESTAMP_ATTRIBUTE, String.valueOf(MESSAGE_TIME), ID_ATTRIBUTE, RECORD_ID)).setOrderingKey(ORDERING_KEY);
ReceivedMessage expectedReceivedMessage = new ReceivedMessage().setMessage(expectedPubsubMessage).setAckId(ACK_ID);
PullResponse expectedResponse = new PullResponse().setReceivedMessages(ImmutableList.of(expectedReceivedMessage));
when((Object) mockPubsub.projects().subscriptions().pull(expectedSubscription, expectedRequest).execute()).thenReturn(expectedResponse);
List<IncomingMessage> acutalMessages = client.pull(REQ_TIME, SUBSCRIPTION, 10, true);
assertEquals(1, acutalMessages.size());
IncomingMessage actualMessage = acutalMessages.get(0);
assertEquals(ACK_ID, actualMessage.ackId());
assertEquals(DATA, actualMessage.message().getData().toStringUtf8());
assertEquals(RECORD_ID, actualMessage.recordId());
assertEquals(REQ_TIME, actualMessage.requestTimeMsSinceEpoch());
assertEquals(MESSAGE_TIME, actualMessage.timestampMsSinceEpoch());
assertEquals(ORDERING_KEY, actualMessage.message().getOrderingKey());
}
use of com.google.api.services.pubsub.model.ReceivedMessage in project beam by apache.
the class PubsubJsonClientTest method pullOneMessageEmptyAttributes.
@Test
public void pullOneMessageEmptyAttributes() throws IOException {
client = new PubsubJsonClient(null, null, mockPubsub);
String expectedSubscription = SUBSCRIPTION.getPath();
PullRequest expectedRequest = new PullRequest().setReturnImmediately(true).setMaxMessages(10);
PubsubMessage expectedPubsubMessage = new PubsubMessage().setMessageId(MESSAGE_ID).encodeData(DATA.getBytes(StandardCharsets.UTF_8)).setPublishTime(String.valueOf(PUB_TIME));
ReceivedMessage expectedReceivedMessage = new ReceivedMessage().setMessage(expectedPubsubMessage).setAckId(ACK_ID);
PullResponse expectedResponse = new PullResponse().setReceivedMessages(ImmutableList.of(expectedReceivedMessage));
when((Object) mockPubsub.projects().subscriptions().pull(expectedSubscription, expectedRequest).execute()).thenReturn(expectedResponse);
List<IncomingMessage> acutalMessages = client.pull(REQ_TIME, SUBSCRIPTION, 10, true);
assertEquals(1, acutalMessages.size());
IncomingMessage actualMessage = acutalMessages.get(0);
assertEquals(ACK_ID, actualMessage.ackId());
assertEquals(DATA, actualMessage.message().getData().toStringUtf8());
assertEquals(REQ_TIME, actualMessage.requestTimeMsSinceEpoch());
assertEquals(PUB_TIME, actualMessage.timestampMsSinceEpoch());
}
use of com.google.api.services.pubsub.model.ReceivedMessage in project components by Talend.
the class PubSubOutputRuntimeTestIT method createTopicSub.
private void createTopicSub(Pipeline pipeline) throws IOException {
String testID = "createTopicSubTest" + new Random().nextInt();
final String newTopicName = "tcomp-pubsub-createTopicSub" + uuid;
final String newSubName = "tcomp-pubsub-createTopicSub-sub" + uuid;
final String fieldDelimited = ";";
List<Person> expectedPersons = Person.genRandomList(testID, maxRecords);
List<String> expectedMessages = new ArrayList<>();
List<String[]> sendMessages = new ArrayList<>();
for (Person person : expectedPersons) {
expectedMessages.add(person.toCSV(fieldDelimited));
sendMessages.add(person.toCSV(fieldDelimited).split(fieldDelimited));
}
PubSubOutputRuntime outputRuntime = new PubSubOutputRuntime();
PubSubOutputProperties outputProperties = createOutput(addSubscriptionForDataset(createDatasetFromCSV(createDatastore(), newTopicName, fieldDelimited), newSubName));
outputProperties.topicOperation.setValue(PubSubOutputProperties.TopicOperation.CREATE_IF_NOT_EXISTS);
outputRuntime.initialize(runtimeContainer, outputProperties);
PCollection<IndexedRecord> records = (PCollection<IndexedRecord>) pipeline.apply(Create.of(sendMessages)).apply((PTransform) ConvertToIndexedRecord.of());
records.setCoder(LazyAvroCoder.of()).apply(outputRuntime);
pipeline.run().waitUntilFinish();
List<String> actual = new ArrayList<>();
while (true) {
List<ReceivedMessage> messages = client.pull(newSubName, maxRecords);
List<String> ackIds = new ArrayList<>();
for (ReceivedMessage message : messages) {
actual.add(new String(message.getMessage().decodeData()));
ackIds.add(message.getAckId());
}
client.ack(newSubName, ackIds);
if (actual.size() >= maxRecords) {
break;
}
}
client.deleteSubscription(newSubName);
client.deleteTopic(newTopicName);
assertThat(actual, containsInAnyOrder(expectedMessages.toArray()));
}
Aggregations