UserPropertyTransformer extends AbstractPropertyTransformer<User> { @Override @SuppressWarnings( { "unchecked", "rawtypes" } ) public Object transform( Object o ) { if ( !(o instanceof User) ) { if ( o instanceof Collection ) { Collection collection = (Collection) o; if ( collection.isEmpty() ) { return o; } Object next = collection.iterator().next(); if ( !(next instanceof User) ) { return o; } Collection<UserDto> userDtoCollection = newCollectionInstance( collection.getClass() ); collection.forEach( user -> userDtoCollection.add( buildUserDto( (User) user ) ) ); return userDtoCollection; } return o; } return buildUserDto( (User) o ); } UserPropertyTransformer(); @Override @SuppressWarnings( { "unchecked", "rawtypes" } ) Object transform( Object o );  }
@Test public void testUserTransform() { User user = new User(); UserCredentials userCredentials = new UserCredentials(); userCredentials.setUuid( uuid ); userCredentials.setUser( user ); userCredentials.setUsername( "test" ); userCredentials.setUserInfo( user ); user.setUserCredentials( userCredentials ); user.setUser( user ); UserPropertyTransformer transformer = new UserPropertyTransformer(); UserPropertyTransformer.UserDto userDto = (UserPropertyTransformer.UserDto) transformer.transform( user ); assertEquals( user.getUid(), userDto.getId() ); assertEquals( "test", userDto.getUsername() ); }