use of com.google.cloud.teleport.v2.transforms.BigQueryConverters.FailsafeJsonToTableRow in project DataflowTemplates by GoogleCloudPlatform.
the class BigQueryConvertersTest method testFailsafeJsonToTableRowValidInput.
/**
* Tests the {@link BigQueryConverters.FailsafeJsonToTableRow} transform with good input.
*/
@Test
@Category(NeedsRunner.class)
public void testFailsafeJsonToTableRowValidInput() {
// Test input
final String payload = "{\"ticker\": \"GOOGL\", \"price\": 1006.94}";
final Map<String, String> attributes = ImmutableMap.of("id", "0xDb12", "type", "stock");
final PubsubMessage message = new PubsubMessage(payload.getBytes(), attributes);
final FailsafeElement<PubsubMessage, String> input = FailsafeElement.of(message, payload);
// Expected Output
TableRow expectedRow = new TableRow().set("ticker", "GOOGL").set("price", 1006.94);
// Register the coder for the pipeline. This prevents having to invoke .setCoder() on
// many transforms.
FailsafeElementCoder<PubsubMessage, String> coder = FailsafeElementCoder.of(PubsubMessageWithAttributesCoder.of(), StringUtf8Coder.of());
CoderRegistry coderRegistry = pipeline.getCoderRegistry();
coderRegistry.registerCoderForType(coder.getEncodedTypeDescriptor(), coder);
// Build the pipeline
PCollectionTuple output = pipeline.apply("CreateInput", Create.of(input).withCoder(coder)).apply("JsonToTableRow", FailsafeJsonToTableRow.<PubsubMessage>newBuilder().setSuccessTag(TABLE_ROW_TAG).setFailureTag(FAILSAFE_ELM_TAG).build());
// Assert
PAssert.that(output.get(TABLE_ROW_TAG)).containsInAnyOrder(expectedRow);
PAssert.that(output.get(FAILSAFE_ELM_TAG)).empty();
// Execute the test
pipeline.run();
}
Aggregations