Search in sources :

Example 1 with OneOfEach

use of thrift.test.OneOfEach in project elephant-bird by twitter.

the class TestThriftToPig method tupleTest.

private void tupleTest(TestType type) throws Exception {
    OneOfEach ooe = Fixtures.oneOfEach;
    Nesting n = Fixtures.nesting;
    ThriftConverter<HolyMoley> hmConverter = ThriftConverter.newInstance(HolyMoley.class);
    // use a deserialized hm object so that hm.contains HashSet iteration is a bit more predictable
    HolyMoley hm = hmConverter.fromBytes(hmConverter.toBytes(Fixtures.holyMoley));
    assertEquals("1-0-35-27000-16777216-6000000000-3.141592653589793-JSON THIS! \"-" + ooe.zomg_unicode + "-0-base64-{(1),(2),(3)}-{(1),(2),(3)}-{(1),(2),(3)}", toTuple(type, ooe).toDelimitedString("-"));
    assertEquals("(31337,I am a bonk... xor!)-(1,0,35,27000,16777216,6000000000,3.141592653589793,JSON THIS! \"," + n.my_ooe.zomg_unicode + ",0,base64,{(1),(2),(3)},{(1),(2),(3)},{(1),(2),(3)})", toTuple(type, n).toDelimitedString("-"));
    assertEquals("{(1,0,34,27000,16777216,6000000000,3.141592653589793,JSON THIS! \"," + ooe.zomg_unicode + ",0,base64,{(1),(2),(3)},{(1),(2),(3)},{(1),(2),(3)}),(1,0,35,27000,16777216,6000000000,3.141592653589793,JSON THIS! \"," + ooe.zomg_unicode + ",0,base64,{(1),(2),(3)},{(1),(2),(3)},{(1),(2),(3)})}-{({}),({(and a one),(and a two)}),({(then a one, two),(three!),(FOUR!!)})}-{zero={}, three={}, two={(1,Wait.),(2,What?)}}", (toTuple(type, hm).toDelimitedString("-")));
    // Test null fields. Pick the fields that have defaults of null
    // so that extra round of seralization and deserialization does not affect it.
    OneOfEach mostly_ooe = new OneOfEach(ooe);
    mostly_ooe.setBase64((ByteBuffer) null);
    mostly_ooe.setZomg_unicode(null);
    assertEquals("1-0-35-27000-16777216-6000000000-3.141592653589793-JSON THIS! \"--0--{(1),(2),(3)}-{(1),(2),(3)}-{(1),(2),(3)}", toTuple(type, mostly_ooe).toDelimitedString("-"));
    Nesting n2 = new Nesting(n);
    n2.getMy_bonk().setMessage(null);
    n2.setMy_ooe(mostly_ooe);
    assertEquals("(31337,)-(1,0,35,27000,16777216,6000000000,3.141592653589793,JSON THIS! \",,0,,{(1),(2),(3)},{(1),(2),(3)},{(1),(2),(3)})", toTuple(type, n2).toDelimitedString("-"));
    // test enum.
    ThriftToPig.setConversionProperties(new Configuration(false));
    PhoneNumber ph = new PhoneNumber();
    ph.setNumber("415-555-5555");
    ph.setType(PhoneType.HOME);
    assertEquals("415-555-5555,HOME", toTuple(type, ph).toDelimitedString(","));
    Person person = new Person(new Name("bob", "jenkins"), 42, "foo@bar.com", Lists.newArrayList(ph));
    assertEquals("(bob,jenkins),42,foo@bar.com,{(415-555-5555,HOME)}", toTuple(type, person).toDelimitedString(","));
    // test Enum map
    TestPerson testPerson = new TestPerson(new TestName("bob", "jenkins"), ImmutableMap.of(TestPhoneType.HOME, "408-555-5555", TestPhoneType.MOBILE, "650-555-5555", TestPhoneType.WORK, "415-555-5555"));
    String tupleString = toTuple(type, testPerson).toDelimitedString("-");
    assertTrue(tupleString.equals("(bob,jenkins)-{MOBILE=650-555-5555, WORK=415-555-5555, HOME=408-555-5555}") || tupleString.equals("(bob,jenkins)-{MOBILE=650-555-5555, HOME=408-555-5555, WORK=415-555-5555}"));
    // Test Union:
    TestUnion unionInt = new TestUnion();
    unionInt.setI32Type(10);
    assertEquals(",10,,,", toTuple(type, unionInt).toDelimitedString(","));
    TestUnion unionStr = new TestUnion();
    // is overridden below.
    unionStr.setI32Type(-1);
    unionStr.setStringType("abcd");
    assertEquals("abcd,,,,", toTuple(type, unionStr).toDelimitedString(","));
}
Also used : Nesting(thrift.test.Nesting) TestName(com.twitter.elephantbird.thrift.test.TestName) HolyMoley(thrift.test.HolyMoley) Configuration(org.apache.hadoop.conf.Configuration) TestUnion(com.twitter.elephantbird.thrift.test.TestUnion) PhoneNumber(com.twitter.elephantbird.thrift.test.PhoneNumber) TestPerson(com.twitter.elephantbird.thrift.test.TestPerson) Person(com.twitter.elephantbird.thrift.test.Person) TestPerson(com.twitter.elephantbird.thrift.test.TestPerson) OneOfEach(thrift.test.OneOfEach) TestName(com.twitter.elephantbird.thrift.test.TestName) Name(com.twitter.elephantbird.thrift.test.Name)

Example 2 with OneOfEach

use of thrift.test.OneOfEach in project elephant-bird by twitter.

the class TestThriftToProto method testThriftToProto.

@Test
public void testThriftToProto() throws TException, IOException {
    OneOfEach ooe = Fixtures.oneOfEach;
    ThriftToProto<OneOfEach, ThriftFixtures.OneOfEach> thriftToProto = ThriftToProto.newInstance(ooe, ThriftFixtures.OneOfEach.newBuilder().build());
    ThriftFixtures.OneOfEach proto = thriftToProto.convert(ooe);
    assertEquals(ooe.im_true, proto.getImTrue());
    assertEquals(ooe.im_false, proto.getImFalse());
    assertEquals(ooe.a_bite, proto.getABite());
    assertEquals(ooe.integer16, proto.getInteger16());
    assertEquals(ooe.integer32, proto.getInteger32());
    assertEquals(ooe.integer64, proto.getInteger64());
    assertEquals(ooe.double_precision, proto.getDoublePrecision(), 0.00001);
    assertEquals(ooe.some_characters, proto.getSomeCharacters());
    assertEquals(ooe.zomg_unicode, proto.getZomgUnicode());
    assertEquals(ooe.what_who, proto.getWhatWho());
    assertEquals(new String(ooe.getBase64(), "UTF-8"), proto.getBase64().toStringUtf8());
}
Also used : ThriftFixtures(com.twitter.elephantbird.examples.proto.ThriftFixtures) OneOfEach(thrift.test.OneOfEach) Test(org.junit.Test)

Example 3 with OneOfEach

use of thrift.test.OneOfEach in project parquet-mr by apache.

the class TestProtocolReadToWrite method testIncompatibleSchemaRecord.

@Test
public void testIncompatibleSchemaRecord() throws Exception {
    // handler will rethrow the exception for verifying purpose
    CountingErrorHandler countingHandler = new CountingErrorHandler();
    BufferedProtocolReadToWrite p = new BufferedProtocolReadToWrite(ThriftSchemaConverter.toStructType(AddressBook.class), countingHandler);
    final ByteArrayOutputStream in = new ByteArrayOutputStream();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    OneOfEach a = new OneOfEach(true, false, (byte) 8, (short) 16, (int) 32, (long) 64, (double) 1234, "string", "å", false, ByteBuffer.wrap("a".getBytes()), new ArrayList<Byte>(), new ArrayList<Short>(), new ArrayList<Long>());
    a.write(protocol(in));
    try {
        p.readOne(protocol(new ByteArrayInputStream(in.toByteArray())), protocol(out));
        fail("this should throw");
    } catch (SkippableException e) {
        Throwable cause = e.getCause();
        assertTrue(cause instanceof DecodingSchemaMismatchException);
        assertTrue(cause.getMessage().contains("the data type does not match the expected thrift structure"));
        assertTrue(cause.getMessage().contains("got BOOL"));
    }
    assertEquals(0, countingHandler.recordCountOfMissingFields);
    assertEquals(0, countingHandler.fieldIgnoredCount);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) OneOfEach(thrift.test.OneOfEach) Test(org.junit.Test)

Example 4 with OneOfEach

use of thrift.test.OneOfEach in project parquet-mr by apache.

the class TestProtocolReadToWrite method testOneOfEach.

@Test
public void testOneOfEach() throws Exception {
    OneOfEach a = new OneOfEach(true, false, (byte) 8, (short) 16, (int) 32, (long) 64, (double) 1234, "string", "å", false, ByteBuffer.wrap("a".getBytes()), new ArrayList<Byte>(), new ArrayList<Short>(), new ArrayList<Long>());
    writeReadCompare(a);
}
Also used : OneOfEach(thrift.test.OneOfEach) Test(org.junit.Test)

Example 5 with OneOfEach

use of thrift.test.OneOfEach in project parquet-mr by apache.

the class TestParquetWriteProtocol method testOneOfEach.

@Test
public void testOneOfEach() throws TException {
    String[] expectations = { "startMessage()", "startField(im_true, 0)", "addInt(1)", "endField(im_true, 0)", "startField(im_false, 1)", "addInt(0)", "endField(im_false, 1)", "startField(a_bite, 2)", "addInt(8)", "endField(a_bite, 2)", "startField(integer16, 3)", "addInt(16)", "endField(integer16, 3)", "startField(integer32, 4)", "addInt(32)", "endField(integer32, 4)", "startField(integer64, 5)", "addLong(64)", "endField(integer64, 5)", "startField(double_precision, 6)", "addDouble(1234.0)", "endField(double_precision, 6)", "startField(some_characters, 7)", "addBinary(string)", "endField(some_characters, 7)", "startField(zomg_unicode, 8)", "addBinary(å)", "endField(zomg_unicode, 8)", "startField(what_who, 9)", "addInt(0)", "endField(what_who, 9)", "startField(base64, 10)", "addBinary(a)", "endField(base64, 10)", "startField(byte_list, 11)", "startGroup()", "endGroup()", "endField(byte_list, 11)", "startField(i16_list, 12)", "startGroup()", "endGroup()", "endField(i16_list, 12)", "startField(i64_list, 13)", "startGroup()", "endGroup()", "endField(i64_list, 13)", "endMessage()" };
    OneOfEach a = new OneOfEach(true, false, (byte) 8, (short) 16, (int) 32, (long) 64, (double) 1234, "string", "å", false, ByteBuffer.wrap("a".getBytes()), new ArrayList<Byte>(), new ArrayList<Short>(), new ArrayList<Long>());
    validatePig(expectations, a);
    String[] thriftExpectations = Arrays.copyOf(expectations, expectations.length, String[].class);
    // Elephant bird maps booleans to int
    thriftExpectations[2] = "addBoolean(true)";
    thriftExpectations[5] = "addBoolean(false)";
    thriftExpectations[29] = "addBoolean(false)";
    validateThrift(thriftExpectations, a);
}
Also used : OneOfEach(thrift.test.OneOfEach) Test(org.junit.Test)

Aggregations

OneOfEach (thrift.test.OneOfEach)7 Test (org.junit.Test)6 ThriftFixtures (com.twitter.elephantbird.examples.proto.ThriftFixtures)1 Name (com.twitter.elephantbird.thrift.test.Name)1 Person (com.twitter.elephantbird.thrift.test.Person)1 PhoneNumber (com.twitter.elephantbird.thrift.test.PhoneNumber)1 TestName (com.twitter.elephantbird.thrift.test.TestName)1 TestPerson (com.twitter.elephantbird.thrift.test.TestPerson)1 TestUnion (com.twitter.elephantbird.thrift.test.TestUnion)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 Configuration (org.apache.hadoop.conf.Configuration)1 HolyMoley (thrift.test.HolyMoley)1 Nesting (thrift.test.Nesting)1