use of io.cdap.cdap.etl.mock.common.MockEmitter in project hydrator-plugins by cdapio.
the class LogParserTransformTest method testCloudfrontLogTransform.
@Test
public void testCloudfrontLogTransform() throws Exception {
String event = "2015-04-17\t13:35:48\tSFO20\t582123\t11.111.111.11\tGET\texample.cloudfront" + ".net\t/coopr-standalone-vm/0.9.8/coopr-standalone-vm-0.9.8.ova\t200\t-\tMozilla/5" + ".0%2520(compatible;%2520Yahoo!%2520Slurp;%2520http://help.yahoo.com/help/us/ysearch/slurp)" + "\t-\tError\tsCmB94WPP5v-QoCyn7Jz1ZLn0kBhzIEkqfFuX2Gh5oA1SA8dsLp-kw==\texample.co\thttp\t264\t0.984";
StructuredRecord record = StructuredRecord.builder(STRING_SCHEMA).set("body", event).build();
String comment = "#Fields: date time x-edge-location sc-bytes c-ip cs-method cs(Host) cs-uri-stem sc-status " + "cs(Referer) cs(User-Agent) cs-uri-query cs(Cookie) x-edge-result-type x-edge-request-id x-host-header " + "cs-protocol cs-bytes time-taken";
StructuredRecord commentRecord = StructuredRecord.builder(STRING_SCHEMA).set("body", comment).build();
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
CLOUDFRONT_TRANSFORM.transform(record, emitter);
StructuredRecord output = emitter.getEmitted().get(0);
Assert.assertEquals("/coopr-standalone-vm/0.9.8/coopr-standalone-vm-0.9.8.ova", output.get("uri"));
Assert.assertEquals("11.111.111.11", output.get("ip"));
Assert.assertEquals("unknown", output.get("browser"));
Assert.assertEquals("", output.get("device"));
Assert.assertEquals(200, output.<Integer>get("httpStatus").intValue());
Assert.assertEquals(1429277748000L, output.<Long>get("ts").longValue());
CLOUDFRONT_TRANSFORM.transform(commentRecord, emitter);
Assert.assertEquals(1, emitter.getEmitted().size());
}
use of io.cdap.cdap.etl.mock.common.MockEmitter in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testConvertToDouble.
@Test
public void testConvertToDouble() throws Exception {
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "intField:double,longField:double,floatField:double", null);
Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
TransformContext transformContext = new MockTransformContext();
transform.initialize(transformContext);
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(SIMPLE_TYPES_RECORD, emitter);
StructuredRecord output = emitter.getEmitted().get(0);
Schema expectedSchema = Schema.recordOf("record.projected", Schema.Field.of("booleanField", Schema.of(Schema.Type.BOOLEAN)), Schema.Field.of("intField", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("longField", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("floatField", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("doubleField", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("bytesField", Schema.of(Schema.Type.BYTES)), Schema.Field.of("stringField", Schema.of(Schema.Type.STRING)));
Assert.assertEquals(expectedSchema, output.getSchema());
Assert.assertTrue((Boolean) output.get("booleanField"));
Assert.assertEquals(28d, output.get("intField"), 0.0001d);
Assert.assertEquals(99d, output.get("longField"), 0.0001d);
Assert.assertTrue(Math.abs(2.71 - (Double) output.get("floatField")) < 0.000001);
Assert.assertTrue(Math.abs(3.14 - (Double) output.get("doubleField")) < 0.000001);
Assert.assertArrayEquals(Bytes.toBytes("foo"), (byte[]) output.get("bytesField"));
Assert.assertEquals("bar", output.get("stringField"));
}
use of io.cdap.cdap.etl.mock.common.MockEmitter in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testKeepFields.
@Test
public void testKeepFields() throws Exception {
Schema schema = Schema.recordOf("three", Schema.Field.of("x", Schema.of(Schema.Type.INT)), Schema.Field.of("y", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("z", Schema.arrayOf(Schema.of(Schema.Type.INT))));
StructuredRecord input = StructuredRecord.builder(schema).set("x", 1).set("y", 3.14).set("z", new int[] { 1, 2, 3 }).build();
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, null, "x");
Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
TransformContext transformContext = new MockTransformContext();
transform.initialize(transformContext);
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(input, emitter);
StructuredRecord output = emitter.getEmitted().get(0);
Schema expectedSchema = Schema.recordOf("three.projected", Schema.Field.of("x", Schema.of(Schema.Type.INT)));
Assert.assertEquals(expectedSchema, output.getSchema());
Assert.assertEquals(1, output.<Integer>get("x").intValue());
}
use of io.cdap.cdap.etl.mock.common.MockEmitter in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testConvertToBytes.
@Test
public void testConvertToBytes() throws Exception {
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "booleanField:bytes,intField:bytes,longField:bytes,floatField:bytes," + "doubleField:bytes,bytesField:bytes,stringField:bytes", null);
Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
TransformContext transformContext = new MockTransformContext();
transform.initialize(transformContext);
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(SIMPLE_TYPES_RECORD, emitter);
StructuredRecord output = emitter.getEmitted().get(0);
Schema expectedSchema = Schema.recordOf("record.projected", Schema.Field.of("booleanField", Schema.of(Schema.Type.BYTES)), Schema.Field.of("intField", Schema.of(Schema.Type.BYTES)), Schema.Field.of("longField", Schema.of(Schema.Type.BYTES)), Schema.Field.of("floatField", Schema.of(Schema.Type.BYTES)), Schema.Field.of("doubleField", Schema.of(Schema.Type.BYTES)), Schema.Field.of("bytesField", Schema.of(Schema.Type.BYTES)), Schema.Field.of("stringField", Schema.of(Schema.Type.BYTES)));
Assert.assertEquals(expectedSchema, output.getSchema());
Assert.assertArrayEquals(Bytes.toBytes(true), (byte[]) output.get("booleanField"));
Assert.assertArrayEquals(Bytes.toBytes(28), (byte[]) output.get("intField"));
Assert.assertArrayEquals(Bytes.toBytes(99L), (byte[]) output.get("longField"));
Assert.assertArrayEquals(Bytes.toBytes(2.71f), (byte[]) output.get("floatField"));
Assert.assertArrayEquals(Bytes.toBytes(3.14), (byte[]) output.get("doubleField"));
Assert.assertArrayEquals(Bytes.toBytes("foo"), (byte[]) output.get("bytesField"));
Assert.assertArrayEquals(Bytes.toBytes("bar"), (byte[]) output.get("stringField"));
}
use of io.cdap.cdap.etl.mock.common.MockEmitter in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testInvalidConversion.
@Test
public void testInvalidConversion() throws Exception {
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "x:int", null);
Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
TransformContext transformContext = new MockTransformContext();
try {
transform.initialize(transformContext);
Schema schema = Schema.recordOf("record", Schema.Field.of("x", Schema.of(Schema.Type.LONG)));
StructuredRecord input = StructuredRecord.builder(schema).set("x", 5L).build();
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(input, emitter);
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(CauseAttributes.CONFIG_ELEMENT, "x:int");
expectedCause.addAttribute(STAGE, MOCK_STAGE);
Assert.assertEquals(expectedCause, e.getFailures().get(0).getCauses().get(0));
}
}
Aggregations