ThriftToDynamicProto { @SuppressWarnings("unchecked") public Message convert(T thriftObj) { return doConvert((TBase<?, ?>) Preconditions.checkNotNull(thriftObj, "Can not convert a null object")); } ThriftToDynamicProto(Class<T> thriftClass);  ThriftToDynamicProto(Class<T> thriftClass,
                              boolean supportNestedObjects,
                              boolean ignoreUnsupportedTypes);  ThriftToDynamicProto(Class<T> thriftClass, List<Pair<String, Type>> extraFields);  ThriftToDynamicProto(Class<T> thriftClass,
                              List<Pair<String, Type>> extraFields,
                              boolean supportNestedObjects,
                              boolean ignoreUnsupportedTypes); Message.Builder getBuilder(Class<? extends TBase<?, ?>> thriftClass); @SuppressWarnings("unchecked") Message convert(T thriftObj); @SuppressWarnings("unchecked") Message doConvert(TBase<?, F> thriftObj); FieldDescriptor getFieldDescriptor(Class<? extends TBase<?, ?>> thriftClass,
                                            String fieldName); Descriptors.FileDescriptor getFileDescriptor();  }
@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")); }
@Test public void testNestedStructsWhenEnabled() throws DescriptorValidationException { ThriftToDynamicProto<Person> thriftToProto = new ThriftToDynamicProto<Person>(Person.class, true, false); Person person = genPerson(); Message msg = thriftToProto.convert(person); assertEquals(person.id, Protobufs.getFieldByName(msg, "id")); assertEquals(person.email, Protobufs.getFieldByName(msg, "email")); compareName(person.name, (Message) Protobufs.getFieldByName(msg, "name")); comparePhoneNumbers(person.phones, (List) Protobufs.getFieldByName(msg, "phones")); }
@Test public void testNestedStructsWhenDisabled() throws DescriptorValidationException { ThriftToDynamicProto<Person> thriftToProto = new ThriftToDynamicProto<Person>(Person.class); Person person = genPerson(); Message msg = thriftToProto.convert(person); assertEquals(person.id, Protobufs.getFieldByName(msg, "id")); assertEquals(person.email, Protobufs.getFieldByName(msg, "email")); assertTrue(!Protobufs.hasFieldByName(msg, "name")); assertTrue(!Protobufs.hasFieldByName(msg, "phones")); }
@Test public void testPrimitiveListConversionWhenNestedStructsEnabled() throws DescriptorValidationException { ThriftToDynamicProto<PrimitiveListsStruct> thriftToProto = new ThriftToDynamicProto<PrimitiveListsStruct>(PrimitiveListsStruct.class, true, false); PrimitiveListsStruct listStruct = genPrimitiveListsStruct(); Message msg = thriftToProto.convert(listStruct); comparePrimitiveListStruct(listStruct, msg); }
@Test public void testPrimitiveListConversionWhenNestedStructsDisabled() throws DescriptorValidationException { ThriftToDynamicProto<PrimitiveListsStruct> thriftToProto = new ThriftToDynamicProto<PrimitiveListsStruct>(PrimitiveListsStruct.class); PrimitiveListsStruct listStruct = genPrimitiveListsStruct(); Message msg = thriftToProto.convert(listStruct); comparePrimitiveListStruct(listStruct, msg); }
@Test public void testPrimitiveSetConversionWhenNestedStructsDisabled() throws DescriptorValidationException { ThriftToDynamicProto<PrimitiveSetsStruct> thriftToProto = new ThriftToDynamicProto<PrimitiveSetsStruct>(PrimitiveSetsStruct.class); PrimitiveSetsStruct setsStruct = genPrimitiveSetsStruct(); Message msg = thriftToProto.convert(setsStruct); comparePrimitiveSetsStruct(setsStruct, msg); }
@Test public void testPrimitiveSetConversionWhenNestedStructsEnabled() throws DescriptorValidationException { ThriftToDynamicProto<PrimitiveSetsStruct> thriftToProto = new ThriftToDynamicProto<PrimitiveSetsStruct>(PrimitiveSetsStruct.class, true, false); PrimitiveSetsStruct setsStruct = genPrimitiveSetsStruct(); Message msg = thriftToProto.convert(setsStruct); comparePrimitiveSetsStruct(setsStruct, msg); }
@Test public void testMapConversionWhenNestedStructsEnabled() throws DescriptorValidationException { ThriftToDynamicProto<MapStruct> thriftToProto = new ThriftToDynamicProto<MapStruct>(MapStruct.class, true, false); MapStruct mapStruct = genMapStruct(); Message msg = thriftToProto.convert(mapStruct); Map<Integer, String> expected = mapStruct.entries; List<?> entries = (List)Protobufs.getFieldByName(msg, "entries"); Set<?> expectedKeys = Sets.newHashSet(expected.keySet()); for (Object entry : entries) { Message entryMsg = (Message) entry; Object key = Protobufs.getFieldByName(entryMsg, "key"); assertTrue(expectedKeys.remove(key)); Object value = Protobufs.getFieldByName(entryMsg, "value"); assertEquals(expected.get(key), value); } assertEquals(0, expectedKeys.size()); }
@Test public void testMapConversionWhenNestedStructsDisabled() throws DescriptorValidationException { ThriftToDynamicProto<MapStruct> thriftToProto = new ThriftToDynamicProto<MapStruct>(MapStruct.class); MapStruct mapStruct = genMapStruct(); Message msg = thriftToProto.convert(mapStruct); assertTrue(!Protobufs.hasFieldByName(msg, "entries")); }