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);
}
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);
}
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());
}
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());
}
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());
}
Aggregations