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