Search in sources :

Example 21 with MockEmitter

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

the class JavaScriptTransformTest method testEmitAlerts.

@Test
public void testEmitAlerts() throws Exception {
    JavaScriptTransform.Config config = new JavaScriptTransform.Config("function transform(input, emitter, context) { " + "emitter.emitAlert({'x': input.x + ''});" + " }", null, null);
    JavaScriptTransform transform = new JavaScriptTransform(config);
    MockTransformContext transformContext = new MockTransformContext();
    transform.initialize(transformContext);
    Schema schema = Schema.recordOf("x", Schema.Field.of("x", Schema.of(Schema.Type.INT)));
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(StructuredRecord.builder(schema).set("x", 2).build(), emitter);
    Assert.assertEquals(ImmutableList.of(ImmutableMap.of("x", "2")), emitter.getAlerts());
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) LookupConfig(io.cdap.cdap.etl.api.LookupConfig) LookupTableConfig(io.cdap.cdap.etl.api.LookupTableConfig) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 22 with MockEmitter

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

the class JavaScriptTransformTest method testDropAndRename.

@Test
public void testDropAndRename() throws Exception {
    Schema outputSchema = Schema.recordOf("smallerSchema", Schema.Field.of("x", Schema.of(Schema.Type.INT)), Schema.Field.of("y", Schema.of(Schema.Type.LONG)));
    JavaScriptTransform.Config config = new JavaScriptTransform.Config("function transform(input, emitter, context) { var returnMap = {}; " + "returnMap['x'] = input.intField; " + "returnMap['y'] = input.longField; " + "emitter.emit(returnMap); }", outputSchema.toString(), null);
    Transform<StructuredRecord, StructuredRecord> transform = new JavaScriptTransform(config);
    transform.initialize(new MockTransformContext());
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(RECORD1, emitter);
    StructuredRecord output = emitter.getEmitted().get(0);
    Assert.assertEquals(outputSchema, output.getSchema());
    Assert.assertEquals(28, output.<Integer>get("x").intValue());
    Assert.assertEquals(99L, output.<Long>get("y").longValue());
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) LookupConfig(io.cdap.cdap.etl.api.LookupConfig) LookupTableConfig(io.cdap.cdap.etl.api.LookupTableConfig) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) Schema(io.cdap.cdap.api.data.schema.Schema) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 23 with MockEmitter

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

the class JavaScriptTransformTest method testLookup.

@Test
public void testLookup() throws Exception {
    JavaScriptTransform.Config config = new JavaScriptTransform.Config("function transform(x, emitter, ctx) { " + "var single = ctx.getLookup('purchases').lookup('abc');" + "var batch = ctx.getLookup('purchases').lookup(['abc', 'sdf']);" + "x.stringField = '1_' + single + ' 2_' + batch['abc'] + batch['sdf'] + '::' + batch.abc;" + "emitter.emit(x);" + "}", null, new LookupConfig(ImmutableMap.of("purchases", new LookupTableConfig(LookupTableConfig.TableType.DATASET))));
    Transform<StructuredRecord, StructuredRecord> transform = new JavaScriptTransform(config);
    transform.initialize(new MockTransformContext("somestage", new HashMap<String, String>(), new MockLookupProvider(TEST_LOOKUP)));
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    transform.transform(STRING_RECORD, emitter);
    StructuredRecord output = emitter.getEmitted().get(0);
    // check record1
    Assert.assertEquals(STRING_SCHEMA, output.getSchema());
    Assert.assertEquals("1_abc 2_abcsdf::abc", output.get("stringField"));
}
Also used : LookupTableConfig(io.cdap.cdap.etl.api.LookupTableConfig) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) HashMap(java.util.HashMap) LookupConfig(io.cdap.cdap.etl.api.LookupConfig) LookupTableConfig(io.cdap.cdap.etl.api.LookupTableConfig) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) LookupConfig(io.cdap.cdap.etl.api.LookupConfig) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) MockLookupProvider(io.cdap.cdap.etl.mock.common.MockLookupProvider) Test(org.junit.Test)

Example 24 with MockEmitter

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

the class LogParserTransformTest method testErrorDatasetForInvalidCLFLog.

@Test
public void testErrorDatasetForInvalidCLFLog() throws Exception {
    StructuredRecord record = StructuredRecord.builder(BYTE_SCHEMA).set("body", ByteBuffer.wrap((Bytes.toBytes("127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] " + "\"GET /apache_pb.gif HTTP/1.0\" 200 2326 " + "\"http://www.example.com/start.html\" \"Mozilla/5.0 " + "")))).build();
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    CLF_TRANSFORM.transform(record, emitter);
    Assert.assertEquals(0, emitter.getEmitted().size());
    Assert.assertEquals(1, emitter.getErrors().size());
    InvalidEntry<StructuredRecord> invalidEntry = emitter.getErrors().get(0);
    Assert.assertEquals(31, invalidEntry.getErrorCode());
    Assert.assertEquals("Error Message", "Couldn't parse log, because the log did not match the CLF format.", invalidEntry.getErrorMsg());
    Assert.assertEquals("Error Record", record, invalidEntry.getInvalidRecord());
}
Also used : MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 25 with MockEmitter

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

the class LogParserTransformTest method testCLFLogTransform.

@Test
public void testCLFLogTransform() throws Exception {
    StructuredRecord record = StructuredRecord.builder(BYTE_SCHEMA).set("body", ByteBuffer.wrap((Bytes.toBytes("127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] " + "\"GET /apache_pb.gif HTTP/1.0\" 200 2326 " + "\"http://www.example.com/start.html\" \"Mozilla/5.0 " + "(Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36\"")))).build();
    MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
    CLF_TRANSFORM.transform(record, emitter);
    StructuredRecord output = emitter.getEmitted().get(0);
    Assert.assertEquals("/apache_pb.gif", output.get("uri"));
    Assert.assertEquals("127.0.0.1", output.get("ip"));
    Assert.assertEquals("Chrome", output.get("browser"));
    Assert.assertEquals("Personal computer", output.get("device"));
    Assert.assertEquals(200, output.<Integer>get("httpStatus").intValue());
    Assert.assertEquals(971211336000L, output.<Long>get("ts").longValue());
}
Also used : MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) 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