Search in sources :

Example 11 with MockEmitter

use of io.cdap.cdap.etl.mock.common.MockEmitter 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 12 with MockEmitter

use of io.cdap.cdap.etl.mock.common.MockEmitter in project hydrator-plugins by cdapio.

the class CompressorTest method testSnappyCompress.

@Test
public void testSnappyCompress() throws Exception {
    Transform<StructuredRecord, StructuredRecord> transform = new Compressor(new Compressor.Config("a:SNAPPY", OUTPUT.toString()));
    MockTransformContext context = new MockTransformContext();
    transform.initialize(context);
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(StructuredRecord.builder(INPUT).set("a", "This is a test for testing snappy compression").set("b", "2").set("c", "3").set("d", "4").set("e", "5").build(), emitter);
    byte[] expected = Snappy.compress("This is a test for testing snappy compression".getBytes());
    byte[] actual = emitter.getEmitted().get(0).get("a");
    Assert.assertEquals(2, emitter.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertArrayEquals(expected, actual);
}
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) Test(org.junit.Test)

Example 13 with MockEmitter

use of io.cdap.cdap.etl.mock.common.MockEmitter 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 14 with MockEmitter

use of io.cdap.cdap.etl.mock.common.MockEmitter 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 15 with MockEmitter

use of io.cdap.cdap.etl.mock.common.MockEmitter 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)

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