Search in sources :

Example 6 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class SerdeFieldsDescriptorTest method simpleTest.

@Test
public void simpleTest() {
    Map<String, Type> fieldToType = Maps.newHashMap();
    fieldToType.put("a", Type.INTEGER);
    fieldToType.put("b", Type.CHAR);
    FieldsDescriptor fd = new FieldsDescriptor(fieldToType);
    byte[] bytes = SerdeFieldsDescriptor.INSTANCE.serializeObject(fd);
    FieldsDescriptor newfd = (FieldsDescriptor) SerdeFieldsDescriptor.INSTANCE.deserializeObject(bytes, new MutableInt(0));
    Assert.assertEquals(fd, newfd);
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) MutableInt(org.apache.commons.lang3.mutable.MutableInt) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) Test(org.junit.Test)

Example 7 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class DimensionsEventTest method eventKeyEqualsHashCodeTest.

@Test
public void eventKeyEqualsHashCodeTest() {
    Map<String, Type> fieldToTypeA = Maps.newHashMap();
    fieldToTypeA.put("a", Type.LONG);
    fieldToTypeA.put("b", Type.STRING);
    FieldsDescriptor fdA = new FieldsDescriptor(fieldToTypeA);
    GPOMutable gpoA = new GPOMutable(fdA);
    gpoA.setField("a", 1L);
    gpoA.setField("b", "hello");
    EventKey eventKeyA = new EventKey(1, 1, 1, gpoA);
    Map<String, Type> fieldToTypeB = Maps.newHashMap();
    fieldToTypeB.put("a", Type.LONG);
    fieldToTypeB.put("b", Type.STRING);
    FieldsDescriptor fdB = new FieldsDescriptor(fieldToTypeB);
    GPOMutable gpoB = new GPOMutable(fdB);
    gpoB.setField("a", 1L);
    gpoB.setField("b", "hello");
    EventKey eventKeyB = new EventKey(1, 1, 1, gpoB);
    Assert.assertEquals("The two hashcodes should equal", eventKeyA.hashCode(), eventKeyB.hashCode());
    Assert.assertEquals("The two event keys should equal", eventKeyA, eventKeyB);
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) GPOMutable(org.apache.apex.malhar.lib.appdata.gpo.GPOMutable) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) EventKey(org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey) Test(org.junit.Test)

Example 8 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class GPOUtilsTest method validDeserializeTest.

@Test
public void validDeserializeTest() throws Exception {
    final Map<String, Type> fieldToType = Maps.newHashMap();
    final String tboolean = "tboolean";
    final Boolean tbooleanv = true;
    final String tchar = "tchar";
    final Character tcharv = 'A';
    final String tstring = "tstring";
    final String tstringv = "hello";
    final String tfloat = "tfloat";
    final Float tfloatv = 1.0f;
    final String tdouble = "tdouble";
    final Double tdoublev = 2.0;
    final String tbyte = "tbyte";
    final Byte tbytev = 50;
    final String tshort = "tshort";
    final Short tshortv = 1000;
    final String tinteger = "tinteger";
    final Integer tintegerv = 100000;
    final String tlong = "tlong";
    final Long tlongv = 10000000000L;
    fieldToType.put(tboolean, Type.BOOLEAN);
    fieldToType.put(tchar, Type.CHAR);
    fieldToType.put(tstring, Type.STRING);
    fieldToType.put(tfloat, Type.FLOAT);
    fieldToType.put(tdouble, Type.DOUBLE);
    fieldToType.put(tbyte, Type.BYTE);
    fieldToType.put(tshort, Type.SHORT);
    fieldToType.put(tinteger, Type.INTEGER);
    fieldToType.put(tlong, Type.LONG);
    JSONObject jo = new JSONObject();
    jo.put(tboolean, tbooleanv);
    jo.put(tchar, tcharv);
    jo.put(tstring, tstringv);
    jo.put(tfloat, tfloatv);
    jo.put(tdouble, tdoublev);
    jo.put(tbyte, tbytev);
    jo.put(tshort, tshortv);
    jo.put(tinteger, tintegerv);
    jo.put(tlong, tlongv);
    String json = jo.toString(2);
    logger.debug("Input json: {}", json);
    GPOMutable gpom = GPOUtils.deserialize(new FieldsDescriptor(fieldToType), jo);
    Assert.assertEquals("Results must equal", tbooleanv, gpom.getField(tboolean));
    Assert.assertEquals("Results must equal", tcharv, gpom.getField(tchar));
    Assert.assertEquals("Results must equal", tfloatv, gpom.getField(tfloat));
    Assert.assertEquals("Results must equal", tdoublev, gpom.getField(tdouble));
    Assert.assertEquals("Results must equal", tbytev, gpom.getField(tbyte));
    Assert.assertEquals("Results must equal", tshortv, gpom.getField(tshort));
    Assert.assertEquals("Results must equal", tintegerv, gpom.getField(tinteger));
    Assert.assertEquals("Results must equal", tlongv, gpom.getField(tlong));
}
Also used : FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) Type(org.apache.apex.malhar.lib.appdata.schemas.Type) JSONObject(org.codehaus.jettison.json.JSONObject) Test(org.junit.Test)

Example 9 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class GPOUtilsTest method simpleSerializeDeserializeTest.

@Test
public void simpleSerializeDeserializeTest() {
    final Map<String, Type> fieldToType = Maps.newHashMap();
    final String tboolean = "tboolean";
    final Boolean tbooleanv = true;
    final String tchar = "tchar";
    final Character tcharv = 'A';
    final String tstring = "tstring";
    final String tstringv = "hello";
    final String tfloat = "tfloat";
    final Float tfloatv = 1.0f;
    final String tdouble = "tdouble";
    final Double tdoublev = 2.0;
    final String tbyte = "tbyte";
    final Byte tbytev = 50;
    final String tshort = "tshort";
    final Short tshortv = 1000;
    final String tinteger = "tinteger";
    final Integer tintegerv = 100000;
    final String tlong = "tlong";
    final Long tlongv = 10000000000L;
    int totalBytes = // boolean
    1 + // char
    2 + 4 + // string
    tstringv.getBytes().length + // float
    4 + // double
    8 + // byte
    1 + // short
    2 + // int
    4 + // long
    8;
    logger.debug("Correct total bytes {}.", totalBytes);
    fieldToType.put(tboolean, Type.BOOLEAN);
    fieldToType.put(tchar, Type.CHAR);
    fieldToType.put(tstring, Type.STRING);
    fieldToType.put(tfloat, Type.FLOAT);
    fieldToType.put(tdouble, Type.DOUBLE);
    fieldToType.put(tbyte, Type.BYTE);
    fieldToType.put(tshort, Type.SHORT);
    fieldToType.put(tinteger, Type.INTEGER);
    fieldToType.put(tlong, Type.LONG);
    FieldsDescriptor fd = new FieldsDescriptor(fieldToType);
    GPOMutable gpo = new GPOMutable(fd);
    gpo.setFieldGeneric(tboolean, tbooleanv);
    gpo.setFieldGeneric(tchar, tcharv);
    gpo.setField(tstring, tstringv);
    gpo.setFieldGeneric(tfloat, tfloatv);
    gpo.setFieldGeneric(tdouble, tdoublev);
    gpo.setFieldGeneric(tbyte, tbytev);
    gpo.setFieldGeneric(tshort, tshortv);
    gpo.setFieldGeneric(tinteger, tintegerv);
    gpo.setFieldGeneric(tlong, tlongv);
    GPOByteArrayList byteArrayList = new GPOByteArrayList();
    byte[] gpoByte = GPOUtils.serialize(gpo, byteArrayList);
    logger.debug("GPO num bytes: {}", gpoByte.length);
    GPOMutable dgpo = GPOUtils.deserialize(fd, gpoByte, new MutableInt(0));
    Assert.assertEquals("Values must equal", tbooleanv, dgpo.getField(tboolean));
    Assert.assertEquals("Values must equal", tcharv, dgpo.getField(tchar));
    Assert.assertEquals("Values must equal", tstringv, dgpo.getField(tstring));
    Assert.assertEquals("Values must equal", tfloatv, dgpo.getField(tfloat));
    Assert.assertEquals("Values must equal", tdoublev, dgpo.getField(tdouble));
    Assert.assertEquals("Values must equal", tbytev, dgpo.getField(tbyte));
    Assert.assertEquals("Values must equal", tshortv, dgpo.getField(tshort));
    Assert.assertEquals("Values must equal", tintegerv, dgpo.getField(tinteger));
    Assert.assertEquals("Values must equal", tlongv, dgpo.getField(tlong));
}
Also used : FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) Type(org.apache.apex.malhar.lib.appdata.schemas.Type) MutableInt(org.apache.commons.lang3.mutable.MutableInt) Test(org.junit.Test)

Example 10 with FieldsDescriptor

use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.

the class GPOUtilsTest method testCornerCaseSerializationLegnth.

@Test
public void testCornerCaseSerializationLegnth() {
    final Map<String, Type> fieldToType = Maps.newHashMap();
    fieldToType.put("a", Type.OBJECT);
    fieldToType.put("b", Type.OBJECT);
    GPOMutable gpo = new GPOMutable(new FieldsDescriptor(fieldToType));
    int serializeLength = GPOUtils.serializedLength(gpo);
    Assert.assertEquals(0, serializeLength);
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) Test(org.junit.Test)

Aggregations

FieldsDescriptor (org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor)24 Type (org.apache.apex.malhar.lib.appdata.schemas.Type)19 Test (org.junit.Test)10 Map (java.util.Map)5 GPOMutable (org.apache.apex.malhar.lib.appdata.gpo.GPOMutable)4 MutableInt (org.apache.commons.lang3.mutable.MutableInt)4 JSONObject (org.codehaus.jettison.json.JSONObject)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 EventKey (org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey)2 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)1 Set (java.util.Set)1 Serde (org.apache.apex.malhar.lib.appdata.gpo.Serde)1 DimensionalConfigurationSchema (org.apache.apex.malhar.lib.appdata.schemas.DimensionalConfigurationSchema)1 Fields (org.apache.apex.malhar.lib.appdata.schemas.Fields)1