use of io.cdap.cdap.etl.mock.common.MockEmitter in project hydrator-plugins by cdapio.
the class EncoderTest method testBase64StringEncoder.
@Test
public void testBase64StringEncoder() throws Exception {
String test = "This is a test for testing base64 encoding";
Transform<StructuredRecord, StructuredRecord> transform = new Encoder(new Encoder.Config("a:STRING_BASE64", OUTPUTSTR.toString()));
TransformContext context = new MockTransformContext();
transform.initialize(context);
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(StructuredRecord.builder(INPUT).set("a", test).set("b", "2").set("c", "3").set("d", "4").set("e", "5").build(), emitter);
Base64 base64 = new Base64();
String expected = base64.encodeAsString(test.getBytes("UTF-8"));
String actual = emitter.getEmitted().get(0).get("a");
Assert.assertEquals(2, emitter.getEmitted().get(0).getSchema().getFields().size());
Assert.assertEquals(expected, actual);
Assert.assertEquals(0, context.getFailureCollector().getValidationFailures().size());
}
use of io.cdap.cdap.etl.mock.common.MockEmitter in project hydrator-plugins by cdapio.
the class JSONFormatterTest method testJSONFormatter.
@Test
public void testJSONFormatter() throws Exception {
JSONFormatter.Config config = new JSONFormatter.Config(OUTPUT1.toString());
Transform<StructuredRecord, StructuredRecord> transform = new JSONFormatter(config);
TransformContext context = new MockTransformContext();
transform.initialize(context);
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(StructuredRecord.builder(INPUT1).set("a", "1").set("b", "2").set("c", "3").set("d", "4").set("e", "5").build(), emitter);
Assert.assertEquals("{\"a\":\"1\",\"b\":\"2\",\"c\":\"3\",\"d\":\"4\",\"e\":\"5\"}", emitter.getEmitted().get(0).get("body"));
}
use of io.cdap.cdap.etl.mock.common.MockEmitter in project hydrator-plugins by cdapio.
the class JSONParserTest method testInvalidJsonPathForNonNullableSchema.
@Test
public void testInvalidJsonPathForNonNullableSchema() throws Exception {
final String[] jsonPaths = { "expensive:$.expensive", "bicycle_color:$.store.bicycle.color", "bicycle_price:$.store.bicycle.price", "window:$.store.window" };
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
JSONParser.Config config = new JSONParser.Config("body", Joiner.on(",").join(jsonPaths), OUTPUT4.toString());
Transform<StructuredRecord, StructuredRecord> transform = new JSONParser(config);
MockPipelineConfigurer mockPipelineConfigurer = new MockPipelineConfigurer(INPUT1);
transform.configurePipeline(mockPipelineConfigurer);
TransformContext context = new MockTransformContext();
transform.initialize(context);
transform.transform(StructuredRecord.builder(INPUT1).set("body", json).build(), emitter);
Assert.assertEquals(0, emitter.getEmitted().size());
}
use of io.cdap.cdap.etl.mock.common.MockEmitter in project hydrator-plugins by cdapio.
the class XMLMultiParserTest method testErrorDatasetForInvalidXml.
@Test
public void testErrorDatasetForInvalidXml() throws Exception {
// String field, String encoding, String xPath, String schema
Schema schema = Schema.recordOf("record", Schema.Field.of("id", Schema.of(Schema.Type.LONG)), Schema.Field.of("name", Schema.of(Schema.Type.STRING)), Schema.Field.of("price", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("desc", Schema.nullableOf(Schema.of(Schema.Type.STRING))));
XMLMultiParser.Config config = new XMLMultiParser.Config("body", "UTF-8", "/items/item", schema.toString());
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
Schema inputSchema = Schema.recordOf("input", Schema.Field.of("body", Schema.of(Schema.Type.STRING)));
StructuredRecord input = StructuredRecord.builder(inputSchema).set("body", "<items>" + "<item><id>0</id><name>Burrito</name><price>7.77</price><desc>delicious</desc></item>" + "<item><id>100</id><name>Tortilla Chips</name><price>0.99</price></item>" + "<item><id>200</id><name>Water</name><price>2.99</price>" + "</items>").build();
XMLMultiParser parser = new XMLMultiParser(config);
parser.initialize(new MockTransformContext("stage"));
parser.transform(input, emitter);
Assert.assertEquals(0, emitter.getEmitted().size());
Assert.assertEquals(1, emitter.getErrors().size());
InvalidEntry<StructuredRecord> invalidEntry = emitter.getErrors().get(0);
Assert.assertEquals(31, invalidEntry.getErrorCode());
Assert.assertEquals(input, invalidEntry.getInvalidRecord());
}
use of io.cdap.cdap.etl.mock.common.MockEmitter in project hydrator-plugins by cdapio.
the class XMLMultiParserTest method testMultipleRecordsFromOne.
@Test
public void testMultipleRecordsFromOne() throws Exception {
// String field, String encoding, String xPath, String schema
Schema schema = Schema.recordOf("record", Schema.Field.of("id", Schema.of(Schema.Type.LONG)), Schema.Field.of("name", Schema.of(Schema.Type.STRING)), Schema.Field.of("price", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("desc", Schema.nullableOf(Schema.of(Schema.Type.STRING))));
XMLMultiParser.Config config = new XMLMultiParser.Config("body", "UTF-8", "/items/item", schema.toString());
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
Schema inputSchema = Schema.recordOf("input", Schema.Field.of("body", Schema.of(Schema.Type.STRING)));
StructuredRecord input = StructuredRecord.builder(inputSchema).set("body", "<items>" + "<item><id>0</id><name>Burrito</name><price>7.77</price><desc>delicious</desc></item>" + "<item><id>100</id><name>Tortilla Chips</name><price>0.99</price></item>" + "<item><id>200</id><name>Water</name><price>2.99</price></item>" + "</items>").build();
XMLMultiParser parser = new XMLMultiParser(config);
parser.initialize(new MockTransformContext("stage"));
parser.transform(input, emitter);
List<StructuredRecord> expected = ImmutableList.of(StructuredRecord.builder(schema).set("id", 0L).set("name", "Burrito").set("price", 7.77d).set("desc", "delicious").build(), StructuredRecord.builder(schema).set("id", 100L).set("name", "Tortilla Chips").set("price", 0.99d).build(), StructuredRecord.builder(schema).set("id", 200L).set("name", "Water").set("price", 2.99d).build());
Assert.assertEquals(expected, emitter.getEmitted());
}
Aggregations