@Test
public void testTransferMetadata() throws Exception {

    User user1 = _userRepo.findOneByUsername("Username1");
    User user2 = _userRepo.findOneByUsername("uSername1");
    User user3 = _userRepo.findOneByUsername("usErname1");
    User user4 = _userRepo.findOneByUsername("useRname1");
    MergeUsersByUsernameDatabaseMigration migration = new MergeUsersByUsernameDatabaseMigration();
    List<User> duplicatedUserNames = _userRepo.findByUsernameIgnoreCase("username1");
    duplicatedUserNames.sort(Comparator.comparing(User::getProfile));
    User greatestProfileUser = duplicatedUserNames.get(0);
    final DataManager dataManager = _applicationContext.getBean(DataManager.class);
    final MetadataRepository metadataRepository = _applicationContext.getBean(MetadataRepository.class);
    migration.transferMetadata(_applicationContext, duplicatedUserNames, greatestProfileUser);
    for (Map.Entry<Integer, Integer> entry : metadataIdList.entrySet()) {
        Integer metadataId = entry.getKey();
        Integer groupId = entry.getValue();
        Metadata metadata = metadataRepository.findOne(metadataId);
        assertEquals((Integer) greatestProfileUser.getId(), metadata.getSourceInfo().getOwner());
        assertEquals(groupId, metadata.getSourceInfo().getGroupOwner());
    }
    assertEquals(0, metadataRepository.findAll((Specification<Metadata>)MetadataSpecs.isOwnedByUser(user1.getId())).size());
    assertEquals(0, metadataRepository.findAll((Specification<Metadata>)MetadataSpecs.isOwnedByUser(user2.getId())).size());
    assertEquals(0, metadataRepository.findAll((Specification<Metadata>)MetadataSpecs.isOwnedByUser(user3.getId())).size());
    assertEquals(6, metadataRepository.findAll((Specification<Metadata>)MetadataSpecs.isOwnedByUser(user4.getId())).size());
}