Search in sources :

Example 26 with MockTransformContext

use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.

the class ProjectionTransformTest method testInvalidSyntax.

@Test(expected = IllegalArgumentException.class)
public void testInvalidSyntax() throws Exception {
    ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, "x,y", null, null);
    Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
    TransformContext transformContext = new MockTransformContext();
    transform.initialize(transformContext);
}
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) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 27 with MockTransformContext

use of io.cdap.cdap.etl.mock.transform.MockTransformContext 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());
}
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 28 with MockTransformContext

use of io.cdap.cdap.etl.mock.transform.MockTransformContext 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"));
}
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 29 with MockTransformContext

use of io.cdap.cdap.etl.mock.transform.MockTransformContext 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));
    }
}
Also used : MockTransformContext(io.cdap.cdap.etl.mock.transform.MockTransformContext) ValidationException(io.cdap.cdap.etl.api.validation.ValidationException) 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) Cause(io.cdap.cdap.etl.api.validation.ValidationFailure.Cause) StructuredRecord(io.cdap.cdap.api.data.format.StructuredRecord) Test(org.junit.Test)

Example 30 with MockTransformContext

use of io.cdap.cdap.etl.mock.transform.MockTransformContext 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)

Aggregations

StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)59 MockTransformContext (io.cdap.cdap.etl.mock.transform.MockTransformContext)59 Test (org.junit.Test)58 MockEmitter (io.cdap.cdap.etl.mock.common.MockEmitter)52 TransformContext (io.cdap.cdap.etl.api.TransformContext)35 Schema (io.cdap.cdap.api.data.schema.Schema)27 LookupConfig (io.cdap.cdap.etl.api.LookupConfig)7 LookupTableConfig (io.cdap.cdap.etl.api.LookupTableConfig)7 ValidationException (io.cdap.cdap.etl.api.validation.ValidationException)4 Base32 (org.apache.commons.codec.binary.Base32)4 Base64 (org.apache.commons.codec.binary.Base64)4 Cause (io.cdap.cdap.etl.api.validation.ValidationFailure.Cause)3 MockMultiOutputEmitter (io.cdap.cdap.etl.mock.common.MockMultiOutputEmitter)3 MockPipelineConfigurer (io.cdap.cdap.etl.mock.common.MockPipelineConfigurer)3 HashMap (java.util.HashMap)2 Hex (org.apache.commons.codec.binary.Hex)2 ImmutableList (com.google.common.collect.ImmutableList)1 MockLookupProvider (io.cdap.cdap.etl.mock.common.MockLookupProvider)1 List (java.util.List)1