Search in sources :

Example 1 with TransformContext

use of io.cdap.cdap.etl.api.TransformContext in project hydrator-plugins by cdapio.

the class EncoderTest method testHEXEncoder.

@Test
public void testHEXEncoder() throws Exception {
    String test = "This is a test for testing hex encoding";
    Transform<StructuredRecord, StructuredRecord> transform = new Encoder(new Encoder.Config("a:HEX", OUTPUT.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);
    Hex hex = new Hex();
    byte[] expected = hex.encode(test.getBytes("UTF-8"));
    byte[] actual = emitter.getEmitted().get(0).get("a");
    Assert.assertEquals(2, emitter.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertArrayEquals(expected, actual);
    Assert.assertEquals(0, context.getFailureCollector().getValidationFailures().size());
}
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) Hex(org.apache.commons.codec.binary.Hex) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 2 with TransformContext

use of io.cdap.cdap.etl.api.TransformContext in project hydrator-plugins by cdapio.

the class EncoderTest method testBase64Encoder.

@Test
public void testBase64Encoder() throws Exception {
    String test = "This is a test for testing base64 encoding";
    Transform<StructuredRecord, StructuredRecord> transform = new Encoder(new Encoder.Config("a:BASE64", OUTPUT.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();
    byte[] expected = base64.encode(test.getBytes("UTF-8"));
    byte[] actual = emitter.getEmitted().get(0).get("a");
    Assert.assertEquals(2, emitter.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertArrayEquals(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 3 with TransformContext

use of io.cdap.cdap.etl.api.TransformContext in project hydrator-plugins by cdapio.

the class JSONParserTest method testJsonParserConfig.

private void testJsonParserConfig(JSONParser.Config config) throws Exception {
    Transform<StructuredRecord, StructuredRecord> transform = new JSONParser(config);
    TransformContext context = new MockTransformContext();
    transform.initialize(context);
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(StructuredRecord.builder(INPUT1).set("body", "{\"a\": \"1\", \"b\": \"2\", \"c\" : \"3\", \"d\" : \"4\", \"e\" : \"5\" }").build(), emitter);
    Assert.assertEquals("1", emitter.getEmitted().get(0).get("a"));
    Assert.assertEquals("2", emitter.getEmitted().get(0).get("b"));
    Assert.assertEquals("3", emitter.getEmitted().get(0).get("c"));
    Assert.assertEquals("4", emitter.getEmitted().get(0).get("d"));
    Assert.assertEquals("5", emitter.getEmitted().get(0).get("e"));
}
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)

Example 4 with TransformContext

use of io.cdap.cdap.etl.api.TransformContext in project hydrator-plugins by cdapio.

the class JSONParserTest method testInvalidJsonPathForNullableSchema.

@Test
public void testInvalidJsonPathForNullableSchema() 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), OUTPUT5.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(10, emitter.getEmitted().get(0).<Integer>get("expensive").intValue());
    Assert.assertEquals("red", emitter.getEmitted().get(0).get("bicycle_color"));
    Assert.assertEquals(19.95d, emitter.getEmitted().get(0).get("bicycle_price"), 0.0001d);
    Assert.assertEquals(null, emitter.getEmitted().get(0).get("window"));
    final String[] jsonPaths2 = { "expensive:$['expensive']", "bicycle_color:$['store']['bicycle']['color']", "bicycle_price:$['store']['bicycle']['price']", "window:$['store']['window']" };
    emitter.clear();
    config = new JSONParser.Config("body", Joiner.on(",").join(jsonPaths2), OUTPUT5.toString());
    transform = new JSONParser(config);
    mockPipelineConfigurer = new MockPipelineConfigurer(INPUT1);
    transform.configurePipeline(mockPipelineConfigurer);
    context = new MockTransformContext();
    transform.initialize(context);
    transform.transform(StructuredRecord.builder(INPUT1).set("body", json).build(), emitter);
    Assert.assertEquals(10, emitter.getEmitted().get(0).<Integer>get("expensive").intValue());
    Assert.assertEquals("red", emitter.getEmitted().get(0).get("bicycle_color"));
    Assert.assertEquals(19.95d, emitter.getEmitted().get(0).get("bicycle_price"), 0.0001d);
    Assert.assertEquals(null, emitter.getEmitted().get(0).get("window"));
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) MockPipelineConfigurer(io.cdap.cdap.etl.mock.common.MockPipelineConfigurer) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) Test(org.junit.Test)

Example 5 with TransformContext

use of io.cdap.cdap.etl.api.TransformContext in project hydrator-plugins by cdapio.

the class JSONParserTest method testJSONParserProjections.

@Test
public void testJSONParserProjections() throws Exception {
    JSONParser.Config config = new JSONParser.Config("body", "", OUTPUT2.toString());
    Transform<StructuredRecord, StructuredRecord> transform = new JSONParser(config);
    TransformContext context = new MockTransformContext();
    transform.initialize(context);
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(StructuredRecord.builder(INPUT1).set("body", "{\"a\": \"1\", \"b\": \"2\", \"c\" : \"3\", \"d\" : \"4\", \"e\" : \"5\" }").build(), emitter);
    Assert.assertEquals("1", emitter.getEmitted().get(0).get("a"));
    Assert.assertEquals("2", emitter.getEmitted().get(0).get("b"));
    Assert.assertEquals("5", emitter.getEmitted().get(0).get("e"));
}
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)

Aggregations

TransformContext (io.cdap.cdap.etl.api.TransformContext)37 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)35 MockTransformContext (io.cdap.cdap.etl.mock.transform.MockTransformContext)35 Test (org.junit.Test)34 MockEmitter (io.cdap.cdap.etl.mock.common.MockEmitter)30 Schema (io.cdap.cdap.api.data.schema.Schema)15 ValidationException (io.cdap.cdap.etl.api.validation.ValidationException)4 Base32 (org.apache.commons.codec.binary.Base32)4 Base64 (org.apache.commons.codec.binary.Base64)4 Cause (io.cdap.cdap.etl.api.validation.ValidationFailure.Cause)3 MockPipelineConfigurer (io.cdap.cdap.etl.mock.common.MockPipelineConfigurer)3 StageMetrics (io.cdap.cdap.etl.api.StageMetrics)2 DefaultStageMetrics (io.cdap.cdap.etl.common.DefaultStageMetrics)2 NoopStageStatisticsCollector (io.cdap.cdap.etl.common.NoopStageStatisticsCollector)2 StageStatisticsCollector (io.cdap.cdap.etl.common.StageStatisticsCollector)2 TrackedMultiOutputTransform (io.cdap.cdap.etl.common.TrackedMultiOutputTransform)2 Hex (org.apache.commons.codec.binary.Hex)2 ImmutableList (com.google.common.collect.ImmutableList)1 MockMultiOutputEmitter (io.cdap.cdap.etl.mock.common.MockMultiOutputEmitter)1 HashMap (java.util.HashMap)1