Search in sources :

Example 6 with TransformContext

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

the class JSONParserTest method testComplexJSONParsing.

@Test
public void testComplexJSONParsing() 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), OUTPUT3.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);
}
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 7 with TransformContext

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

the class DecoderTest method testBase32AsStringDecoder.

@Test
public void testBase32AsStringDecoder() throws Exception {
    String test = "This is a test for testing string base32 decoding";
    Transform<StructuredRecord, StructuredRecord> encoder = new Encoder(new Encoder.Config("a:STRING_BASE32", OUTPUT.toString()));
    TransformContext encoderContext = new MockTransformContext();
    encoder.initialize(encoderContext);
    MockEmitter<StructuredRecord> emitterEncoded = new MockEmitter<>();
    encoder.transform(StructuredRecord.builder(INPUT).set("a", test).set("b", "2").set("c", "3").set("d", "4").set("e", "5").build(), emitterEncoded);
    Base32 base32 = new Base32();
    byte[] expected = base32.encode(test.getBytes("UTF-8"));
    byte[] actual = emitterEncoded.getEmitted().get(0).get("a");
    Assert.assertEquals(2, emitterEncoded.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertArrayEquals(expected, actual);
    Transform<StructuredRecord, StructuredRecord> decoder = new Decoder(new Decoder.Config("a:STRING_BASE32", OUTPUTSTR.toString()));
    MockTransformContext context = new MockTransformContext();
    decoder.initialize(context);
    MockEmitter<StructuredRecord> emitterDecoded = new MockEmitter<>();
    decoder.transform(emitterEncoded.getEmitted().get(0), emitterDecoded);
    Assert.assertEquals(2, emitterDecoded.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertEquals(test, emitterDecoded.getEmitted().get(0).get("a"));
    Assert.assertEquals(0, encoderContext.getFailureCollector().getValidationFailures().size());
}
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) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) Base32(org.apache.commons.codec.binary.Base32) Test(org.junit.Test)

Example 8 with TransformContext

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

the class DecoderTest method testBase64AsStringDecoder.

@Test
public void testBase64AsStringDecoder() throws Exception {
    String test = "This is a test for testing string base64 decoding";
    Transform<StructuredRecord, StructuredRecord> encoder = new Encoder(new Encoder.Config("a:STRING_BASE64", OUTPUT.toString()));
    TransformContext encoderContext = new MockTransformContext();
    encoder.initialize(encoderContext);
    MockEmitter<StructuredRecord> emitterEncoded = new MockEmitter<>();
    encoder.transform(StructuredRecord.builder(INPUT).set("a", test).set("b", "2").set("c", "3").set("d", "4").set("e", "5").build(), emitterEncoded);
    Base64 base64 = new Base64();
    byte[] expected = base64.encode(test.getBytes("UTF-8"));
    byte[] actual = emitterEncoded.getEmitted().get(0).get("a");
    Assert.assertEquals(2, emitterEncoded.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertArrayEquals(expected, actual);
    Transform<StructuredRecord, StructuredRecord> decoder = new Decoder(new Decoder.Config("a:STRING_BASE64", OUTPUTSTR.toString()));
    MockTransformContext context = new MockTransformContext();
    decoder.initialize(context);
    MockEmitter<StructuredRecord> emitterDecoded = new MockEmitter<>();
    decoder.transform(emitterEncoded.getEmitted().get(0), emitterDecoded);
    Assert.assertEquals(2, emitterDecoded.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertEquals(test, emitterDecoded.getEmitted().get(0).get("a"));
    Assert.assertEquals(0, encoderContext.getFailureCollector().getValidationFailures().size());
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) Base64(org.apache.commons.codec.binary.Base64) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) Test(org.junit.Test)

Example 9 with TransformContext

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

the class DecoderTest method testBase64Decoder.

@Test
public void testBase64Decoder() throws Exception {
    String test = "This is a test for testing base64 decoding";
    Transform<StructuredRecord, StructuredRecord> encoder = new Encoder(new Encoder.Config("a:BASE64", OUTPUT.toString()));
    TransformContext encoderContext = new MockTransformContext();
    encoder.initialize(encoderContext);
    MockEmitter<StructuredRecord> emitterEncoded = new MockEmitter<>();
    encoder.transform(StructuredRecord.builder(INPUT).set("a", test).set("b", "2").set("c", "3").set("d", "4").set("e", "5").build(), emitterEncoded);
    Base64 base64 = new Base64();
    byte[] expected = base64.encode(test.getBytes("UTF-8"));
    byte[] actual = emitterEncoded.getEmitted().get(0).get("a");
    Assert.assertEquals(2, emitterEncoded.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertArrayEquals(expected, actual);
    Transform<StructuredRecord, StructuredRecord> decoder = new Decoder(new Decoder.Config("a:BASE64", OUTPUTSTR.toString()));
    MockTransformContext context = new MockTransformContext();
    decoder.initialize(context);
    MockEmitter<StructuredRecord> emitterDecoded = new MockEmitter<>();
    decoder.transform(emitterEncoded.getEmitted().get(0), emitterDecoded);
    Assert.assertEquals(2, emitterDecoded.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertEquals(test, emitterDecoded.getEmitted().get(0).get("a"));
    Assert.assertEquals(0, encoderContext.getFailureCollector().getValidationFailures().size());
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) Base64(org.apache.commons.codec.binary.Base64) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) Test(org.junit.Test)

Example 10 with TransformContext

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

the class ProjectionTransformTest method testSameFieldMultipleConverts.

@Test
public void testSameFieldMultipleConverts() throws Exception {
    ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "x:int,x:long", null);
    Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
    TransformContext transformContext = new MockTransformContext();
    try {
        transform.initialize(transformContext);
        Assert.fail();
    } catch (ValidationException e) {
        Assert.assertEquals(1, e.getFailures().size());
        Assert.assertEquals(1, e.getFailures().get(0).getCauses().size());
        Cause expectedCause = new Cause();
        expectedCause.addAttribute(CauseAttributes.STAGE_CONFIG, ProjectionTransform.ProjectionTransformConfig.CONVERT);
        expectedCause.addAttribute(STAGE, MOCK_STAGE);
        Assert.assertEquals(expectedCause, e.getFailures().get(0).getCauses().get(0));
    }
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) ValidationException(io.cdap.cdap.etl.api.validation.ValidationException) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) Cause(io.cdap.cdap.etl.api.validation.ValidationFailure.Cause) 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