use of com.google.cloud.teleport.v2.coders.FailsafeElementCoder in project DataflowTemplates by GoogleCloudPlatform.
the class CsvConvertersTest method testLineToFailsafeJsonNoHeadersUdfDeadletter.
/**
* Tests {@link CsvConverters.LineToFailsafeJson} converts a line to a {@link FailsafeElement}
* correctly using a Javascript Udf. Udf processing is handled by {@link
* JavascriptTextTransformer}. Should output record to deadletter table tag.
*/
@Test
public void testLineToFailsafeJsonNoHeadersUdfDeadletter() {
FailsafeElementCoder<String, String> coder = FAILSAFE_ELEMENT_CODER;
CoderRegistry coderRegistry = pipeline.getCoderRegistry();
coderRegistry.registerCoderForType(coder.getEncodedTypeDescriptor(), coder);
PCollection<String> lines = pipeline.apply(Create.of(BAD_JSON_STRING_RECORD).withCoder(StringUtf8Coder.of()));
PCollectionTuple linesTuple = PCollectionTuple.of(CSV_LINES, lines);
CsvConverters.CsvPipelineOptions options = PipelineOptionsFactory.create().as(CsvConverters.CsvPipelineOptions.class);
options.setDelimiter(",");
options.setJavascriptTextTransformGcsPath(SCRIPT_PARSE_EXCEPTION_FILE_PATH);
options.setJavascriptTextTransformFunctionName("transform");
PCollectionTuple failsafe = linesTuple.apply("TestLineToFailsafeJsonNoHeadersUdfBad", CsvConverters.LineToFailsafeJson.newBuilder().setDelimiter(options.getDelimiter()).setUdfFileSystemPath(options.getJavascriptTextTransformGcsPath()).setUdfFunctionName(options.getJavascriptTextTransformFunctionName()).setJsonSchemaPath(options.getJsonSchemaPath()).setJsonSchemaPath(null).setHeaderTag(CSV_HEADERS).setLineTag(CSV_LINES).setUdfOutputTag(PROCESSING_OUT).setUdfDeadletterTag(PROCESSING_DEADLETTER_OUT).build());
PAssert.that(failsafe.get(PROCESSING_OUT)).empty();
PAssert.that(failsafe.get(PROCESSING_DEADLETTER_OUT)).satisfies(collection -> {
FailsafeElement result = collection.iterator().next();
assertThat(result.getPayload(), is(equalTo(BAD_JSON_STRING_RECORD)));
return null;
});
pipeline.run();
}
Aggregations