Search in sources :

Example 31 with TransformContext

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

the class DecoderTest method testHexDecoder.

@Test
public void testHexDecoder() throws Exception {
    String test = "This is a test for testing hex decoding";
    Transform<StructuredRecord, StructuredRecord> encoder = new Encoder(new Encoder.Config("a:HEX", 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);
    Hex hex = new Hex();
    byte[] expected = hex.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:HEX", 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) Hex(org.apache.commons.codec.binary.Hex) Test(org.junit.Test)

Example 32 with TransformContext

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

the class EncoderTest method testBase32Encoder.

@Test
public void testBase32Encoder() throws Exception {
    String test = "This is a test for testing base32 encoding";
    Transform<StructuredRecord, StructuredRecord> transform = new Encoder(new Encoder.Config("a:BASE32", 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);
    Base32 base32 = new Base32();
    byte[] expected = base32.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) Base32(org.apache.commons.codec.binary.Base32) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 33 with TransformContext

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

the class EncoderTest method testStringBase32Encoder.

@Test
public void testStringBase32Encoder() throws Exception {
    String test = "This is a test for testing base32 encoding";
    Transform<StructuredRecord, StructuredRecord> transform = new Encoder(new Encoder.Config("a:BASE32", 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);
    Base32 base32 = new Base32();
    String expected = base32.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) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) Base32(org.apache.commons.codec.binary.Base32) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 34 with TransformContext

use of io.cdap.cdap.etl.api.TransformContext 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 35 with TransformContext

use of io.cdap.cdap.etl.api.TransformContext 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)

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