@Test
public void testMergeGroups() {

    User user1 = _userRepo.findOneByUsername("Username1");
    User user2 = _userRepo.findOneByUsername("uSername1");
    User user3 = _userRepo.findOneByUsername("usErname1");
    User user4 = _userRepo.findOneByUsername("useRname1");
    Group group1 = _groupRepo.findByName("groupTest1");
    Group group2 = _groupRepo.findByName("groupTest2");
    MergeUsersByUsernameDatabaseMigration migration = new MergeUsersByUsernameDatabaseMigration();
    List<User> duplicatedUserNames = _userRepo.findByUsernameIgnoreCase("username1");
    duplicatedUserNames.sort(Comparator.comparing(User::getProfile));
    User greatestProfileUser = duplicatedUserNames.get(0);
    migration.mergeGroups(_applicationContext, duplicatedUserNames, greatestProfileUser);
    assertNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group1.getId())
        .setUserId(greatestProfileUser.getId()).setProfile(Profile.Administrator)));
    assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group1.getId())
        .setUserId(greatestProfileUser.getId()).setProfile(Profile.UserAdmin)));
    assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group1.getId())
        .setUserId(greatestProfileUser.getId()).setProfile(Profile.Reviewer)));
    assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group1.getId())
        .setUserId(greatestProfileUser.getId()).setProfile(Profile.Editor)));
    assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group1.getId())
        .setUserId(greatestProfileUser.getId()).setProfile(Profile.RegisteredUser)));
    assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group1.getId())
        .setUserId(greatestProfileUser.getId()).setProfile(Profile.Guest)));
    // Reviewer in group2
    assertNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group2.getId())
        .setUserId(greatestProfileUser.getId()).setProfile(Profile.Administrator)));
    assertNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group2.getId())
        .setUserId(greatestProfileUser.getId()).setProfile(Profile.UserAdmin)));
    assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group2.getId())
        .setUserId(greatestProfileUser.getId()).setProfile(Profile.Reviewer)));
    assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group2.getId())
        .setUserId(greatestProfileUser.getId()).setProfile(Profile.Editor)));
    assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group2.getId())
        .setUserId(greatestProfileUser.getId()).setProfile(Profile.RegisteredUser)));
    assertNotNull(_userGroupRepo.findOne(new UserGroupId().setGroupId(group2.getId())
        .setUserId(greatestProfileUser.getId()).setProfile(Profile.Guest)));
    assertEquals(0, _userGroupRepo.findAll(UserGroupSpecs.hasUserId(user1.getId())).size());
    assertEquals(0, _userGroupRepo.findAll(UserGroupSpecs.hasUserId(user2.getId())).size());
    assertEquals(0, _userGroupRepo.findAll(UserGroupSpecs.hasUserId(user3.getId())).size());
    assertEquals(9, _userGroupRepo.findAll(UserGroupSpecs.hasUserId(user4.getId())).size());
}