public class ServerImplTest extends TestCase {
public void testComplicatedDtoImpl() {
final String fooString = "Something";
final int intId = 1;
ComplicatedDtoImpl dtoImpl = ComplicatedDtoImpl.make();
dtoImpl.setIntId(intId);
dtoImpl.setSimpleEnum(SimpleEnum.TWO);
dtoImpl.getFooStrings().add(fooString);
dtoImpl.getNightmare();
dtoImpl.getArrayOfArrayOfEnum();
// Assume that SimpleDto works. Use it to test nested objects
SimpleDtoImpl simpleDto = SimpleDtoImpl.make();
simpleDto.setName(fooString);
simpleDto.setIDontStartWithGet(intId);
dtoImpl.getSimpleDtos().add(simpleDto);
dtoImpl.getMap().put(fooString, simpleDto);
// Check to make sure things are in a sane state.
ComplicatedDto dto = dtoImpl;
assertEquals(intId, dto.getIntId());
assertEquals(fooString, dto.getFooStrings().get(0));
// Should be reference equal initially.
assertEquals(dto.getSimpleDtos().get(0), simpleDto);
assertEquals(dto.getMap().get(fooString), simpleDto);
assertEquals(ComplicatedDto.TYPE, dto.getType());
// Make it json and pull it back out.
Gson gson = new Gson();
String serialized = dtoToJson(gson, dtoImpl);
ComplicatedDtoImpl deserialized = ComplicatedDtoImpl.fromJsonString(serialized);
// Test correctness of JSON serialization.
assertEquals(serialized, dtoImpl.toJson());
assertEquals(intId, deserialized.getIntId());
assertEquals(fooString, deserialized.getFooStrings().get(0));
assertEquals(ComplicatedDto.TYPE, deserialized.getType());
// Pull it out using the DTO's deserializer.
JsonElement jsonElement = new JsonParser().parse(serialized);
ComplicatedDtoImpl deserialized2 = ComplicatedDtoImpl.fromJsonElement(jsonElement);
assertEquals(intId, deserialized2.getIntId());
assertEquals(fooString, deserialized2.getFooStrings().get(0));
assertEquals(ComplicatedDto.TYPE, deserialized2.getType());
// Verify that the hasFields() are correct when using the DTO's deserializer.
assertTrue(deserialized2.hasIntId());
// Check that the SimpleDto object looks correct.
checkSimpleDto(dto.getSimpleDtos().get(0), simpleDto.getName(), simpleDto.iDontStartWithGet());
checkSimpleDto(dto.getMap().get(fooString), simpleDto.getName(), simpleDto.iDontStartWithGet());
}