@Test
public void testSetViewInstanceRoleAccess() throws Exception {

  final Map<String, PermissionEntity> permissions = new HashMap<>();
  permissions.put("CLUSTER.ADMINISTRATOR", TestAuthenticationFactory.createClusterAdministratorPermission());
  permissions.put("CLUSTER.OPERATOR", TestAuthenticationFactory.createClusterOperatorPermission());
  permissions.put("SERVICE.ADMINISTRATOR", TestAuthenticationFactory.createServiceAdministratorPermission());
  permissions.put("SERVICE.OPERATOR", TestAuthenticationFactory.createServiceOperatorPermission());
  permissions.put("CLUSTER.USER", TestAuthenticationFactory.createClusterUserPermission());
  PermissionEntity permissionViewUser = TestAuthenticationFactory.createViewUserPermission();
  ViewInstanceEntity viewInstanceEntity = ViewInstanceEntityTest.getViewInstanceEntity();
  ResourceEntity resourceEntity = viewInstanceEntity.getResource();
  // Expected PrivilegeEntity items to be created...
  Map<String, PrivilegeEntity> expectedPrivileges = new HashMap<>();
  for (Map.Entry<String, PermissionEntity> entry : permissions.entrySet()) {
    if(!entry.getKey().equals("CLUSTER.ADMINISTRATOR")) {
      expectedPrivileges.put(entry.getKey(), TestAuthenticationFactory.createPrivilegeEntity(resourceEntity, permissionViewUser, entry.getValue().getPrincipal()));
    }
  }
  Capture<PrivilegeEntity> captureCreatedPrivilegeEntity = Capture.newInstance(CaptureType.ALL);
  for (Map.Entry<String, PermissionEntity> entry : permissions.entrySet()) {
    expect(permissionDAO.findByName(entry.getKey())).andReturn(entry.getValue()).atLeastOnce();
  }
  expect(permissionDAO.findViewUsePermission()).andReturn(permissionViewUser).atLeastOnce();
  // The CLUSTER.ADMINISTRATOR privilege for this View instance already exists...
  expect(privilegeDAO.exists(EasyMock.anyObject(PrincipalEntity.class), eq(resourceEntity), eq(permissionViewUser)))
      .andAnswer(new IAnswer<Boolean>() {
        @Override
        public Boolean answer() throws Throwable {
          return EasyMock.getCurrentArguments()[0] == permissions.get("CLUSTER.ADMINISTRATOR").getPrincipal();
        }
      })
      .anyTimes();
  privilegeDAO.create(capture(captureCreatedPrivilegeEntity));
  expectLastCall().times(expectedPrivileges.size());
  replay(privilegeDAO, permissionDAO);
  ViewRegistry viewRegistry = ViewRegistry.getInstance();
  viewRegistry.setViewInstanceRoleAccess(viewInstanceEntity, permissions.keySet());
  verify(privilegeDAO, permissionDAO);
  Assert.assertTrue(expectedPrivileges.size() != permissions.size());
  Assert.assertTrue(captureCreatedPrivilegeEntity.hasCaptured());
  List<PrivilegeEntity> capturedValues = captureCreatedPrivilegeEntity.getValues();
  Assert.assertNotNull( capturedValues);
  Set<PrivilegeEntity> uniqueCapturedValues = new HashSet<>(capturedValues);
  Assert.assertEquals(expectedPrivileges.size(), uniqueCapturedValues.size());
  for(PrivilegeEntity capturedValue: uniqueCapturedValues) {
    Assert.assertTrue(expectedPrivileges.containsValue(capturedValue));
  }
}