Search in sources :

Example 31 with MockEmitter

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

the class ProjectionTransformTest method testConvertFromBytes.

@Test
public void testConvertFromBytes() throws Exception {
    Schema schema = Schema.recordOf("record", 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)));
    StructuredRecord input = StructuredRecord.builder(schema).set("booleanField", Bytes.toBytes(true)).set("intField", Bytes.toBytes(28)).set("longField", Bytes.toBytes(99L)).set("floatField", Bytes.toBytes(2.71f)).set("doubleField", Bytes.toBytes(3.14)).set("bytesField", Bytes.toBytes("foo")).set("stringField", Bytes.toBytes("bar")).build();
    ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "booleanField:boolean,intField:int,longField:long,floatField:float," + "doubleField:double,bytesField:bytes,stringField:string", null);
    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(SIMPLE_TYPES_SCHEMA.getRecordName() + ".projected", SIMPLE_TYPES_SCHEMA.getFields());
    Assert.assertEquals(expectedSchema, output.getSchema());
    Assert.assertTrue((Boolean) output.get("booleanField"));
    Assert.assertEquals(28, output.<Integer>get("intField").intValue());
    Assert.assertEquals(99L, output.<Long>get("longField").longValue());
    Assert.assertTrue(Math.abs(2.71f - (Float) 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"));
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) Schema(io.cdap.cdap.api.data.schema.Schema) 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 32 with MockEmitter

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

the class ProjectionTransformTest method testConvertToString.

@Test
public void testConvertToString() throws Exception {
    ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "booleanField:string,intField:string,longField:string,floatField:string," + "doubleField:string,bytesField:string,stringField:string", 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.STRING)), Schema.Field.of("intField", Schema.of(Schema.Type.STRING)), Schema.Field.of("longField", Schema.of(Schema.Type.STRING)), Schema.Field.of("floatField", Schema.of(Schema.Type.STRING)), Schema.Field.of("doubleField", Schema.of(Schema.Type.STRING)), Schema.Field.of("bytesField", Schema.of(Schema.Type.STRING)), Schema.Field.of("stringField", Schema.of(Schema.Type.STRING)));
    Assert.assertEquals(expectedSchema, output.getSchema());
    Assert.assertEquals("true", output.get("booleanField"));
    Assert.assertEquals("28", output.get("intField"));
    Assert.assertEquals("99", output.get("longField"));
    Assert.assertEquals("2.71", output.get("floatField"));
    Assert.assertEquals("3.14", output.get("doubleField"));
    Assert.assertEquals("foo", output.get("bytesField"));
    Assert.assertEquals("bar", output.get("stringField"));
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) TransformContext(io.cdap.cdap.etl.api.TransformContext) MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) 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 33 with MockEmitter

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

the class DBSinkTest method testIdentity.

@Test
public void testIdentity() throws Exception {
    final DBSink.DBSinkConfig config = new DBSink.DBSinkConfig();
    config.columns = "ts,body";
    config.tableName = "foo";
    DBSink sink = new DBSink(config);
    sink.setColumns(ImmutableList.copyOf(Splitter.on(",").split(config.columns)));
    StructuredRecord input = StructuredRecord.builder(Schema.recordOf("foo", Schema.Field.of("ts", Schema.of(Schema.Type.LONG)), Schema.Field.of("body", Schema.of(Schema.Type.STRING)))).set("ts", 123).set("body", "sdfsdf").build();
    MockEmitter<KeyValue<DBRecord, NullWritable>> emitter = new MockEmitter<>();
    sink.transform(input, emitter);
    Assert.assertEquals(1, emitter.getEmitted().size());
    Assert.assertEquals(getRecordFields(input), getRecordFields(emitter.getEmitted().get(0).getKey().getRecord()));
}
Also used : KeyValue(io.cdap.cdap.api.dataset.lib.KeyValue) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 34 with MockEmitter

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

the class DBSinkTest method testFailure.

@Test
public void testFailure() throws Exception {
    final DBSink.DBSinkConfig config = new DBSink.DBSinkConfig();
    config.columns = "body,ts,missing";
    config.tableName = "foo";
    DBSink sink = new DBSink(config);
    sink.setColumns(ImmutableList.copyOf(Splitter.on(",").split(config.columns)));
    StructuredRecord input = StructuredRecord.builder(Schema.recordOf("foo", Schema.Field.of("ts", Schema.of(Schema.Type.LONG)), Schema.Field.of("headers", Schema.mapOf(Schema.of(Schema.Type.STRING), Schema.of(Schema.Type.STRING))), Schema.Field.of("body", Schema.of(Schema.Type.STRING)))).set("ts", 123).set("headers", ImmutableMap.of("hello", "zzz")).set("body", "sdfsdf").build();
    MockEmitter<KeyValue<DBRecord, NullWritable>> emitter = new MockEmitter<>();
    try {
        sink.transform(input, emitter);
        Assert.fail("Expected NullPointerException");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : KeyValue(io.cdap.cdap.api.dataset.lib.KeyValue) MockEmitter(io.cdap.cdap.etl.mock.common.MockEmitter) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 35 with MockEmitter

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

the class DBSinkTest method testDropColumn.

@Test
public void testDropColumn() throws Exception {
    final DBSink.DBSinkConfig config = new DBSink.DBSinkConfig();
    config.columns = "body,ts";
    config.tableName = "foo";
    DBSink sink = new DBSink(config);
    sink.setColumns(ImmutableList.copyOf(Splitter.on(",").split(config.columns)));
    StructuredRecord input = StructuredRecord.builder(Schema.recordOf("foo", Schema.Field.of("ts", Schema.of(Schema.Type.LONG)), Schema.Field.of("headers", Schema.mapOf(Schema.of(Schema.Type.STRING), Schema.of(Schema.Type.STRING))), Schema.Field.of("body", Schema.of(Schema.Type.STRING)))).set("ts", 123).set("headers", ImmutableMap.of("hello", "zzz")).set("body", "sdfsdf").build();
    StructuredRecord output = StructuredRecord.builder(Schema.recordOf("foo", Schema.Field.of("body", Schema.of(Schema.Type.STRING)), Schema.Field.of("ts", Schema.of(Schema.Type.LONG)))).set("body", "sdfsdf").set("ts", 123).build();
    MockEmitter<KeyValue<DBRecord, NullWritable>> emitter = new MockEmitter<>();
    sink.transform(input, emitter);
    Assert.assertEquals(1, emitter.getEmitted().size());
    Assert.assertEquals(getRecordFields(output), getRecordFields(emitter.getEmitted().get(0).getKey().getRecord()));
}
Also used : KeyValue(io.cdap.cdap.api.dataset.lib.KeyValue) 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