use of com.twitter.elephantbird.thrift.test.PhoneNumber in project elephant-bird by twitter.
the class TestThriftToPig method testSetConversionProperties.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testSetConversionProperties() throws ExecException {
PhoneNumber pn = new PhoneNumber();
pn.setNumber("1234");
pn.setType(PhoneType.HOME);
ThriftToPig ttp = ThriftToPig.newInstance(PhoneNumber.class);
Tuple tuple = ttp.getPigTuple(pn);
assertEquals(DataType.CHARARRAY, tuple.getType(1));
assertEquals(PhoneType.HOME.toString(), tuple.get(1));
Configuration conf = new Configuration();
conf.setBoolean(ThriftToPig.USE_ENUM_ID_CONF_KEY, true);
ThriftToPig.setConversionProperties(conf);
tuple = ttp.getPigTuple(pn);
assertEquals(DataType.INTEGER, tuple.getType(1));
assertEquals(PhoneType.HOME.getValue(), tuple.get(1));
}
use of com.twitter.elephantbird.thrift.test.PhoneNumber 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(","));
}
use of com.twitter.elephantbird.thrift.test.PhoneNumber in project elephant-bird by twitter.
the class TestThriftToDynamicProto method comparePhoneNumbers.
private void comparePhoneNumbers(List<PhoneNumber> expected, List<?> actual) {
assertNotNull(actual);
assertEquals(expected.size(), actual.size());
for (int i = 0; i < expected.size(); i++) {
PhoneNumber number = expected.get(i);
Message msg = (Message) actual.get(i);
assertEquals(number.number, Protobufs.getFieldByName(msg, "number"));
assertEquals(number.type.toString(), Protobufs.getFieldByName(msg, "type"));
}
}
use of com.twitter.elephantbird.thrift.test.PhoneNumber in project elephant-bird by twitter.
the class TestThriftToDynamicProto method testSimpleStructConversion.
@Test
public void testSimpleStructConversion() throws DescriptorValidationException {
PhoneNumber thriftPhone = genPhoneNumber("123-34-5467", PhoneType.HOME);
ThriftToDynamicProto<PhoneNumber> thriftToProto = new ThriftToDynamicProto<PhoneNumber>(PhoneNumber.class);
Message msg = thriftToProto.convert(thriftPhone);
assertEquals(thriftPhone.number, Protobufs.getFieldByName(msg, "number"));
assertEquals(thriftPhone.type.toString(), Protobufs.getFieldByName(msg, "type"));
}
use of com.twitter.elephantbird.thrift.test.PhoneNumber in project elephant-bird by twitter.
the class TestThriftToDynamicProto method genPhoneNumber.
private PhoneNumber genPhoneNumber(String number, PhoneType type) {
PhoneNumber phoneNumber = new PhoneNumber(number);
phoneNumber.setType(type);
return phoneNumber;
}
Aggregations