Search in sources :

Example 1 with FailedStringToPubsubMessageFn

use of com.google.cloud.teleport.templates.common.ErrorConverters.FailedStringToPubsubMessageFn in project DataflowTemplates by GoogleCloudPlatform.

the class ErrorConvertersTest method testFailedStringToPubsubMessageFn.

/**
 * Test successful conversion of {@link FailsafeElement} records into {@link PubsubMessage} with
 * attributes.
 */
@Test
@Category(NeedsRunner.class)
public void testFailedStringToPubsubMessageFn() {
    String testMessage = "original-test-message";
    FailsafeElement<String, String> element = FailsafeElement.of(testMessage, testMessage);
    Instant expectedTimestamp = Instant.now();
    String errorMessage = "my-error-message";
    element.setErrorMessage(errorMessage);
    TimestampedValue<FailsafeElement<String, String>> input = TimestampedValue.of(element, expectedTimestamp);
    PCollection<PubsubMessage> pCollection = pipeline.apply(Create.timestamped(input).withCoder(FailsafeElementCoder.of(StringUtf8Coder.of(), StringUtf8Coder.of()))).apply(ParDo.of(new FailedStringToPubsubMessageFn()));
    String expectedTimestampString = FailedStringToPubsubMessageFn.TIMESTAMP_FORMATTER.print(expectedTimestamp.toDateTime(DateTimeZone.UTC));
    PAssert.that(pCollection).satisfies(collection -> {
        PubsubMessage actual = collection.iterator().next();
        assertThat(new String(actual.getPayload(), StandardCharsets.UTF_8), is(equalTo(testMessage)));
        assertThat(actual.getAttribute(FailedStringToPubsubMessageFn.ERROR_MESSAGE), is(equalTo(errorMessage)));
        assertThat(actual.getAttribute(FailedStringToPubsubMessageFn.TIMESTAMP), is(equalTo(expectedTimestampString)));
        return null;
    });
    pipeline.run();
}
Also used : Instant(org.joda.time.Instant) FailedStringToPubsubMessageFn(com.google.cloud.teleport.templates.common.ErrorConverters.FailedStringToPubsubMessageFn) PubsubMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubMessage) FailsafeElement(com.google.cloud.teleport.values.FailsafeElement) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

FailedStringToPubsubMessageFn (com.google.cloud.teleport.templates.common.ErrorConverters.FailedStringToPubsubMessageFn)1 FailsafeElement (com.google.cloud.teleport.values.FailsafeElement)1 PubsubMessage (org.apache.beam.sdk.io.gcp.pubsub.PubsubMessage)1 Instant (org.joda.time.Instant)1 Test (org.junit.Test)1 Category (org.junit.experimental.categories.Category)1