@Test
public void testUninstallViewInstance() throws Exception {

  ViewRegistry registry = ViewRegistry.getInstance();
  Configuration ambariConfig = new Configuration(new Properties());
  ViewConfig config = ViewConfigTest.getConfig(XML_VALID_INSTANCE);
  ViewEntity viewEntity = getViewEntity(config, ambariConfig, getClass().getClassLoader(), "");
  ViewInstanceEntity viewInstanceEntity = getViewInstanceEntity(viewEntity, config.getInstances().get(0));
  ResourceEntity resource = new ResourceEntity();
  resource.setId(3L);
  viewInstanceEntity.setResource(resource);
  PrivilegeEntity privilege1 = createNiceMock(PrivilegeEntity.class);
  PrivilegeEntity privilege2 = createNiceMock(PrivilegeEntity.class);
  List<PrivilegeEntity> privileges = Arrays.asList(privilege1, privilege2);
  PrincipalEntity principalEntity = createNiceMock(PrincipalEntity.class);
  expect(privilege1.getPrincipal()).andReturn(principalEntity);
  expect(privilege2.getPrincipal()).andReturn(principalEntity);
  principalEntity.removePrivilege(privilege1);
  principalEntity.removePrivilege(privilege2);
  expect(privilegeDAO.findByResourceId(3L)).andReturn(privileges);
  privilegeDAO.remove(privilege1);
  privilegeDAO.remove(privilege2);
  viewInstanceDAO.remove(viewInstanceEntity);
  handlerList.removeViewInstance(viewInstanceEntity);
  replay(viewInstanceDAO, privilegeDAO, handlerList, privilege1, privilege2, principalEntity);
  registry.addDefinition(viewEntity);
  registry.addInstanceDefinition(viewEntity, viewInstanceEntity);
  registry.uninstallViewInstance(viewInstanceEntity);
  Collection<ViewInstanceEntity> viewInstanceDefinitions = registry.getInstanceDefinitions(viewEntity);
  Assert.assertEquals(0, viewInstanceDefinitions.size());
  verify(viewInstanceDAO, privilegeDAO, handlerList, privilege1, privilege2, principalEntity);
}