@Test
public void testConsolidateUserRoles() {

  final EasyMockSupport ems = new EasyMockSupport();
  ResourceTypeEntity resourceTypeAmbari = ems.createMock(ResourceTypeEntity.class);
  expect(resourceTypeAmbari.getName()).andReturn(ResourceType.AMBARI.name()).anyTimes();
  ResourceTypeEntity resourceTypeCluster = ems.createMock(ResourceTypeEntity.class);
  expect(resourceTypeCluster.getName()).andReturn(ResourceType.CLUSTER.name()).anyTimes();
  ResourceEntity resourceAmbari = ems.createMock(ResourceEntity.class);
  expect(resourceAmbari.getResourceType()).andReturn(resourceTypeAmbari).anyTimes();
  ResourceEntity resourceC1 = ems.createMock(ResourceEntity.class);
  expect(resourceC1.getResourceType()).andReturn(resourceTypeCluster).anyTimes();
  expect(resourceC1.getId()).andReturn(1L).anyTimes();
  ResourceEntity resourceC2 = ems.createMock(ResourceEntity.class);
  expect(resourceC2.getResourceType()).andReturn(resourceTypeCluster).anyTimes();
  expect(resourceC2.getId()).andReturn(2L).anyTimes();
  PermissionEntity permissionAmbariAdministrator = ems.createMock(PermissionEntity.class);
  expect(permissionAmbariAdministrator.getPermissionName()).andReturn("AMBARI.ADMINISTRATOR").anyTimes();
  PermissionEntity permissionClusterUser = ems.createMock(PermissionEntity.class);
  expect(permissionClusterUser.getPermissionName()).andReturn("CLUSTER.USER").anyTimes();
  PermissionEntity permissionClusterOperator = ems.createMock(PermissionEntity.class);
  expect(permissionClusterOperator.getPermissionName()).andReturn("CLUSTER.OPERATOR").anyTimes();
  PrivilegeEntity privilegeAdmin = ems.createMock(PrivilegeEntity.class);
  expect(privilegeAdmin.getResource()).andReturn(resourceAmbari).anyTimes();
  expect(privilegeAdmin.getPermission()).andReturn(permissionAmbariAdministrator).anyTimes();
  PrivilegeEntity privilegeClusterUserC1 = ems.createMock(PrivilegeEntity.class);
  expect(privilegeClusterUserC1.getResource()).andReturn(resourceC1).anyTimes();
  expect(privilegeClusterUserC1.getPermission()).andReturn(permissionClusterUser).anyTimes();
  PrivilegeEntity privilegeClusterOperatorC1 = ems.createMock(PrivilegeEntity.class);
  expect(privilegeClusterOperatorC1.getResource()).andReturn(resourceC1).anyTimes();
  expect(privilegeClusterOperatorC1.getPermission()).andReturn(permissionClusterOperator).anyTimes();
  PrivilegeEntity privilegeClusterUserC2 = ems.createMock(PrivilegeEntity.class);
  expect(privilegeClusterUserC2.getResource()).andReturn(resourceC2).anyTimes();
  expect(privilegeClusterUserC2.getPermission()).andReturn(permissionClusterUser).anyTimes();
  PrivilegeEntity privilegeClusterOperatorC2 = ems.createMock(PrivilegeEntity.class);
  expect(privilegeClusterOperatorC2.getResource()).andReturn(resourceC2).anyTimes();
  expect(privilegeClusterOperatorC2.getPermission()).andReturn(permissionClusterOperator).anyTimes();
  PrincipalEntity principalAdministratorOnly = ems.createStrictMock(PrincipalEntity.class);
  expect(principalAdministratorOnly.getPrivileges())
      .andReturn(new HashSet<PrivilegeEntity>(Arrays.asList(privilegeAdmin)))
      .once();
  PrincipalEntity principalNonAdminSingleRoleSingleCluster = ems.createStrictMock(PrincipalEntity.class);
  expect(principalNonAdminSingleRoleSingleCluster.getPrivileges())
      .andReturn(new HashSet<PrivilegeEntity>(Arrays.asList(privilegeClusterUserC1)))
      .once();
  PrincipalEntity principalNonAdminMultipleRolesSingleCluster = ems.createStrictMock(PrincipalEntity.class);
  expect(principalNonAdminMultipleRolesSingleCluster.getPrivileges())
      .andReturn(new HashSet<PrivilegeEntity>(Arrays.asList(privilegeClusterUserC1, privilegeClusterOperatorC1)))
      .once();
  PrincipalEntity principalNonAdminMultipleRolesMultipleClusters = ems.createStrictMock(PrincipalEntity.class);
  expect(principalNonAdminMultipleRolesMultipleClusters.getPrivileges())
      .andReturn(new HashSet<PrivilegeEntity>(Arrays.asList(privilegeClusterUserC1, privilegeClusterOperatorC1,
          privilegeClusterUserC2, privilegeClusterOperatorC2)))
      .once();
  PrincipalEntity principalAdminSingleRoleSingleCluster = ems.createStrictMock(PrincipalEntity.class);
  expect(principalAdminSingleRoleSingleCluster.getPrivileges())
      .andReturn(new HashSet<PrivilegeEntity>(Arrays.asList(privilegeAdmin, privilegeClusterOperatorC1)))
      .once();
  PrincipalEntity principalAdminMultipleRolesSingleCluster = ems.createStrictMock(PrincipalEntity.class);
  expect(principalAdminMultipleRolesSingleCluster.getPrivileges())
      .andReturn(new HashSet<PrivilegeEntity>(Arrays.asList(privilegeAdmin, privilegeClusterUserC1, privilegeClusterOperatorC1)))
      .once();
  PrincipalEntity principalAdminMultipleRolesMultipleClusters = ems.createStrictMock(PrincipalEntity.class);
  expect(principalAdminMultipleRolesMultipleClusters.getPrivileges())
      .andReturn(new HashSet<PrivilegeEntity>(Arrays.asList(privilegeAdmin, privilegeClusterUserC1,
          privilegeClusterOperatorC1, privilegeClusterUserC2, privilegeClusterOperatorC2)))
      .once();
  UserEntity userAdministratorOnly = ems.createStrictMock(UserEntity.class);
  expect(userAdministratorOnly.getPrincipal()).andReturn(principalAdministratorOnly).once();
  expect(userAdministratorOnly.getUserName()).andReturn("userAdministratorOnly").anyTimes();
  UserEntity userNonAdminSingleRoleSingleCluster = ems.createStrictMock(UserEntity.class);
  expect(userNonAdminSingleRoleSingleCluster.getPrincipal()).andReturn(principalNonAdminSingleRoleSingleCluster).once();
  expect(userNonAdminSingleRoleSingleCluster.getUserName()).andReturn("userNonAdminSingleRoleSingleCluster").anyTimes();
  UserEntity userNonAdminMultipleRolesSingleCluster = ems.createStrictMock(UserEntity.class);
  expect(userNonAdminMultipleRolesSingleCluster.getPrincipal()).andReturn(principalNonAdminMultipleRolesSingleCluster).once();
  expect(userNonAdminMultipleRolesSingleCluster.getUserName()).andReturn("userNonAdminMultipleRolesSingleCluster").anyTimes();
  UserEntity userNonAdminMultipleRolesMultipleClusters = ems.createStrictMock(UserEntity.class);
  expect(userNonAdminMultipleRolesMultipleClusters.getPrincipal()).andReturn(principalNonAdminMultipleRolesMultipleClusters).once();
  expect(userNonAdminMultipleRolesMultipleClusters.getUserName()).andReturn("userNonAdminMultipleRolesMultipleClusters").anyTimes();
  UserEntity userAdminSingleRoleSingleCluster = ems.createStrictMock(UserEntity.class);
  expect(userAdminSingleRoleSingleCluster.getPrincipal()).andReturn(principalAdminSingleRoleSingleCluster).once();
  expect(userAdminSingleRoleSingleCluster.getUserName()).andReturn("userAdminSingleRoleSingleCluster").anyTimes();
  UserEntity userAdminMultipleRolesSingleCluster = ems.createStrictMock(UserEntity.class);
  expect(userAdminMultipleRolesSingleCluster.getPrincipal()).andReturn(principalAdminMultipleRolesSingleCluster).once();
  expect(userAdminMultipleRolesSingleCluster.getUserName()).andReturn("userAdminMultipleRolesSingleCluster").anyTimes();
  UserEntity userAdminMultipleRolesMultipleClusters = ems.createStrictMock(UserEntity.class);
  expect(userAdminMultipleRolesMultipleClusters.getPrincipal()).andReturn(principalAdminMultipleRolesMultipleClusters).once();
  expect(userAdminMultipleRolesMultipleClusters.getUserName()).andReturn("userAdminMultipleRolesMultipleClusters").anyTimes();
  final UserDAO userDAO = ems.createStrictMock(UserDAO.class);
  expect(userDAO.findAll())
      .andReturn(Arrays.asList(
          userAdministratorOnly,
          userNonAdminSingleRoleSingleCluster,
          userNonAdminMultipleRolesSingleCluster,
          userNonAdminMultipleRolesMultipleClusters,
          userAdminSingleRoleSingleCluster,
          userAdminMultipleRolesSingleCluster,
          userAdminMultipleRolesMultipleClusters))
      .once();
  final PrivilegeDAO privilegeDAO = ems.createMock(PrivilegeDAO.class);
  // principalNonAdminMultipleRolesSingleCluster
  privilegeDAO.remove(privilegeClusterUserC1);
  expectLastCall().once();
  // principalNonAdminMultipleRolesMultipleClusters
  privilegeDAO.remove(privilegeClusterUserC1);
  expectLastCall().once();
  privilegeDAO.remove(privilegeClusterUserC2);
  expectLastCall().once();
  // principalAdminSingleRoleSingleCluster
  privilegeDAO.remove(privilegeClusterOperatorC1);
  expectLastCall().once();
  // principalAdminMultipleRolesSingleCluster
  privilegeDAO.remove(privilegeClusterUserC1);
  expectLastCall().once();
  privilegeDAO.remove(privilegeClusterOperatorC1);
  expectLastCall().once();
  // principalAdminMultipleRolesMultipleClusters
  privilegeDAO.remove(privilegeClusterUserC1);
  expectLastCall().once();
  privilegeDAO.remove(privilegeClusterOperatorC1);
  expectLastCall().once();
  privilegeDAO.remove(privilegeClusterUserC2);
  expectLastCall().once();
  privilegeDAO.remove(privilegeClusterOperatorC2);
  expectLastCall().once();
  ClusterEntity clusterC1 = ems.createStrictMock(ClusterEntity.class);
  expect(clusterC1.getClusterName()).andReturn("c1").anyTimes();
  ClusterEntity clusterC2 = ems.createStrictMock(ClusterEntity.class);
  expect(clusterC2.getClusterName()).andReturn("c2").anyTimes();
  final ClusterDAO clusterDAO = ems.createMock(ClusterDAO.class);
  expect(clusterDAO.findByResourceId(1L)).andReturn(clusterC1).anyTimes();
  expect(clusterDAO.findByResourceId(2L)).andReturn(clusterC2).anyTimes();
  final Injector injector = Guice.createInjector(new AbstractModule() {
    @Override
    protected void configure() {
      bind(EntityManager.class).toInstance(ems.createNiceMock(EntityManager.class));
      bind(UserDAO.class).toInstance(userDAO);
      bind(PrivilegeDAO.class).toInstance(privilegeDAO);
      bind(ClusterDAO.class).toInstance(clusterDAO);
      bind(DBAccessor.class).toInstance(ems.createNiceMock(DBAccessor.class));
      bind(OsFamily.class).toInstance(ems.createNiceMock(OsFamily.class));
      bind(PasswordEncoder.class).toInstance(createNiceMock(PasswordEncoder.class));
      bind(HookContextFactory.class).toInstance(createMock(HookContextFactory.class));
      bind(HookService.class).toInstance(createMock(HookService.class));
    }
  });
  ems.replayAll();
  injector.getInstance(UpgradeCatalog240.class).consolidateUserRoles();
  ems.verifyAll();
}