Search in sources :

Example 56 with MockEmitter

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());
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) Base64(org.apache.commons.codec.binary.Base64) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 57 with MockEmitter

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"));
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 58 with MockEmitter

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());
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockPipelineConfigurer(io.cdap.cdap.etl.mock.common.MockPipelineConfigurer) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 59 with MockEmitter

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());
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 60 with MockEmitter

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());
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Aggregations

StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)62 MockEmitter (io.cdap.cdap.etl.mock.common.MockEmitter)62 Test (org.junit.Test)61 MockTransformContext (io.cdap.cdap.etl.mock.transform.MockTransformContext)52 TransformContext (io.cdap.cdap.etl.api.TransformContext)30 Schema (io.cdap.cdap.api.data.schema.Schema)25 LookupConfig (io.cdap.cdap.etl.api.LookupConfig)7 LookupTableConfig (io.cdap.cdap.etl.api.LookupTableConfig)7 MockPipelineConfigurer (io.cdap.cdap.etl.mock.common.MockPipelineConfigurer)4 Base32 (org.apache.commons.codec.binary.Base32)4 Base64 (org.apache.commons.codec.binary.Base64)4 KeyValue (io.cdap.cdap.api.dataset.lib.KeyValue)3 Hex (org.apache.commons.codec.binary.Hex)2 ValidationException (io.cdap.cdap.etl.api.validation.ValidationException)1 Cause (io.cdap.cdap.etl.api.validation.ValidationFailure.Cause)1 MockLookupProvider (io.cdap.cdap.etl.mock.common.MockLookupProvider)1 HashMap (java.util.HashMap)1