use of io.cdap.cdap.etl.mock.transform.MockTransformContext 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.transform.MockTransformContext 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.transform.MockTransformContext 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.transform.MockTransformContext 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());
}
use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class XMLParserTest method testIgnoreError.
@Test
public void testIgnoreError() throws Exception {
XMLParser.Config config = new XMLParser.Config("body", "ISO-8859-1", "title:/book/title,author:/book/author," + "year:/book/year", "title:string,author:string,year:int", "Ignore error and continue");
Transform<StructuredRecord, StructuredRecord> transform = new XMLParser(config);
transform.initialize(new MockTransformContext());
List<StructuredRecord> input = ImmutableList.of(StructuredRecord.builder(INPUT).set("offset", 1).set("body", "<book category=\"COOKING\"><title lang=\"en\">Everyday Italian</title>" + "<author>Giada De Laurentiis</author><year>null</year><price>30.00</price></book>").build(), StructuredRecord.builder(INPUT).set("offset", 2).set("body", "<book category=\"children\"><title lang=\"en\">Harry Potter</title>" + "<author>J K. Rowling</author><year>2005</year><price>49.99</price></book>").build());
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
for (StructuredRecord record : input) {
transform.transform(record, emitter);
}
Assert.assertEquals(1, emitter.getEmitted().size());
Assert.assertEquals(0, emitter.getErrors().size());
StructuredRecord record = emitter.getEmitted().get(0);
Assert.assertEquals("Harry Potter", record.get("title"));
Assert.assertEquals("J K. Rowling", record.get("author"));
Assert.assertEquals(2005, record.<Integer>get("year").intValue());
}
Aggregations