@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); }
@Transactional public void uninstallViewInstance(ViewInstanceEntity instanceEntity) throws IllegalStateException { ViewEntity viewEntity = getDefinition(instanceEntity.getViewName()); if (viewEntity != null) { String instanceName = instanceEntity.getName(); String viewName = viewEntity.getCommonName(); String version = viewEntity.getVersion(); if (getInstanceDefinition(viewName, version, instanceName) != null) { if (instanceEntity.isXmlDriven()) { throw new IllegalStateException("View instances defined via xml can't be deleted through api requests"); } if (LOG.isDebugEnabled()) { LOG.debug("Deleting view instance " + viewName + "/" + version + "/" + instanceName); } List<PrivilegeEntity> instancePrivileges = privilegeDAO.findByResourceId(instanceEntity.getResource().getId()); for (PrivilegeEntity privilegeEntity : instancePrivileges) { removePrivilegeEntity(privilegeEntity); } instanceDAO.remove(instanceEntity); viewEntity.removeInstanceDefinition(instanceName); removeInstanceDefinition(viewEntity, instanceName); handlerList.removeViewInstance(instanceEntity); } } }
ViewRegistry { @Transactional public void uninstallViewInstance(ViewInstanceEntity instanceEntity) throws IllegalStateException { ViewEntity viewEntity = getDefinition(instanceEntity.getViewName()); if (viewEntity != null) { String instanceName = instanceEntity.getName(); String viewName = viewEntity.getCommonName(); String version = viewEntity.getVersion(); if (getInstanceDefinition(viewName, version, instanceName) != null) { if (instanceEntity.isXmlDriven()) { throw new IllegalStateException("View instances defined via xml can't be deleted through api requests"); } if (LOG.isDebugEnabled()) { LOG.debug("Deleting view instance " + viewName + "/" + version + "/" + instanceName); } List<PrivilegeEntity> instancePrivileges = privilegeDAO.findByResourceId(instanceEntity.getResource().getId()); for (PrivilegeEntity privilegeEntity : instancePrivileges) { removePrivilegeEntity(privilegeEntity); } instanceDAO.remove(instanceEntity); viewEntity.removeInstanceDefinition(instanceName); removeInstanceDefinition(viewEntity, instanceName); handlerList.removeViewInstance(instanceEntity); } } } }
ViewRegistry { @Transactional public void uninstallViewInstance(ViewInstanceEntity instanceEntity) throws IllegalStateException { ViewEntity viewEntity = getDefinition(instanceEntity.getViewName()); if (viewEntity != null) { String instanceName = instanceEntity.getName(); String viewName = viewEntity.getCommonName(); String version = viewEntity.getVersion(); if (getInstanceDefinition(viewName, version, instanceName) != null) { if (instanceEntity.isXmlDriven()) { throw new IllegalStateException("View instances defined via xml can't be deleted through api requests"); } if (LOG.isDebugEnabled()) { LOG.debug("Deleting view instance " + viewName + "/" + version + "/" + instanceName); } List<PrivilegeEntity> instancePrivileges = privilegeDAO.findByResourceId(instanceEntity.getResource().getId()); for (PrivilegeEntity privilegeEntity : instancePrivileges) { removePrivilegeEntity(privilegeEntity); } instanceDAO.remove(instanceEntity); viewEntity.removeInstanceDefinition(instanceName); removeInstanceDefinition(viewEntity, instanceName); handlerList.removeViewInstance(instanceEntity); } } } @Inject  ViewRegistry(AmbariEventPublisher publisher); }
ViewRegistry { @Transactional public void uninstallViewInstance(ViewInstanceEntity instanceEntity) throws IllegalStateException { ViewEntity viewEntity = getDefinition(instanceEntity.getViewName()); if (viewEntity != null) { String instanceName = instanceEntity.getName(); String viewName = viewEntity.getCommonName(); String version = viewEntity.getVersion(); if (getInstanceDefinition(viewName, version, instanceName) != null) { if (instanceEntity.isXmlDriven()) { throw new IllegalStateException("View instances defined via xml can't be deleted through api requests"); } if (LOG.isDebugEnabled()) { LOG.debug("Deleting view instance " + viewName + "/" + version + "/" + instanceName); } List<PrivilegeEntity> instancePrivileges = privilegeDAO.findByResourceId(instanceEntity.getResource().getId()); for (PrivilegeEntity privilegeEntity : instancePrivileges) { removePrivilegeEntity(privilegeEntity); } instanceDAO.remove(instanceEntity); viewEntity.removeInstanceDefinition(instanceName); removeInstanceDefinition(viewEntity, instanceName); handlerList.removeViewInstance(instanceEntity); } } } @Inject  ViewRegistry(AmbariEventPublisher publisher); static void main(String[] args); Collection<ViewEntity> getDefinitions(); ViewEntity getDefinition(String viewName, String version); ViewEntity getDefinition(ResourceTypeEntity resourceTypeEntity); void addDefinition(ViewEntity definition); Collection<ViewInstanceEntity> getInstanceDefinitions(ViewEntity definition); ViewInstanceEntity getInstanceDefinition(String viewName, String version, String instanceName); void addInstanceDefinition(ViewEntity definition, ViewInstanceEntity instanceDefinition); void removeInstanceDefinition(ViewEntity definition, String instanceName); static void initInstance(ViewRegistry singleton); static ViewRegistry getInstance(); Set<SubResourceDefinition> getSubResourceDefinitions(
      String viewName, String version); void readViewArchives(); void readViewArchives(String viewNameRegExp); boolean instanceExists(ViewInstanceEntity instanceEntity); void installViewInstance(ViewInstanceEntity instanceEntity); void updateViewInstance(ViewInstanceEntity instanceEntity); void updateView(ViewInstanceEntity instanceEntity); ViewInstanceEntity getViewInstanceEntity(String viewName, String instanceName); @Transactional void uninstallViewInstance(ViewInstanceEntity instanceEntity); @Transactional void removeInstanceData(ViewInstanceEntity instanceEntity, String key); @Transactional void copyPrivileges(ViewInstanceEntity sourceInstanceEntity,
                             ViewInstanceEntity targetInstanceEntity); void fireEvent(Event event); synchronized void registerListener(Listener listener, String viewName, String viewVersion); synchronized void unregisterListener(Listener listener, String viewName, String viewVersion); boolean hasPermission(PermissionEntity permissionEntity, ResourceEntity resourceEntity, String userName); boolean checkPermission(String viewName, String version, String instanceName, boolean readOnly); boolean checkPermission(ViewInstanceEntity instanceEntity, boolean readOnly); boolean checkAdmin(); boolean includeDefinition(ViewEntity definitionEntity); void setViewInstanceProperties(ViewInstanceEntity instanceEntity, Map<String, String> properties,
                                        ViewConfig viewConfig, ClassLoader classLoader); Cluster getCluster(ViewInstanceDefinition viewInstance); @Subscribe @AllowConcurrentEvents void onAmbariEvent(ServiceInstalledEvent event); @Subscribe void onClusterConfigFinishedEvent(ClusterConfigFinishedEvent event); void readViewArchive(Path path); }
ViewRegistry { @Transactional public void uninstallViewInstance(ViewInstanceEntity instanceEntity) throws IllegalStateException { ViewEntity viewEntity = getDefinition(instanceEntity.getViewName()); if (viewEntity != null) { String instanceName = instanceEntity.getName(); String viewName = viewEntity.getCommonName(); String version = viewEntity.getVersion(); if (getInstanceDefinition(viewName, version, instanceName) != null) { if (instanceEntity.isXmlDriven()) { throw new IllegalStateException("View instances defined via xml can't be deleted through api requests"); } if (LOG.isDebugEnabled()) { LOG.debug("Deleting view instance " + viewName + "/" + version + "/" + instanceName); } List<PrivilegeEntity> instancePrivileges = privilegeDAO.findByResourceId(instanceEntity.getResource().getId()); for (PrivilegeEntity privilegeEntity : instancePrivileges) { removePrivilegeEntity(privilegeEntity); } instanceDAO.remove(instanceEntity); viewEntity.removeInstanceDefinition(instanceName); removeInstanceDefinition(viewEntity, instanceName); handlerList.removeViewInstance(instanceEntity); } } } @Inject  ViewRegistry(AmbariEventPublisher publisher); static void main(String[] args); Collection<ViewEntity> getDefinitions(); ViewEntity getDefinition(String viewName, String version); ViewEntity getDefinition(ResourceTypeEntity resourceTypeEntity); void addDefinition(ViewEntity definition); Collection<ViewInstanceEntity> getInstanceDefinitions(ViewEntity definition); ViewInstanceEntity getInstanceDefinition(String viewName, String version, String instanceName); void addInstanceDefinition(ViewEntity definition, ViewInstanceEntity instanceDefinition); void removeInstanceDefinition(ViewEntity definition, String instanceName); static void initInstance(ViewRegistry singleton); static ViewRegistry getInstance(); Set<SubResourceDefinition> getSubResourceDefinitions(
      String viewName, String version); void readViewArchives(); void readViewArchives(String viewNameRegExp); boolean instanceExists(ViewInstanceEntity instanceEntity); void installViewInstance(ViewInstanceEntity instanceEntity); void updateViewInstance(ViewInstanceEntity instanceEntity); void updateView(ViewInstanceEntity instanceEntity); ViewInstanceEntity getViewInstanceEntity(String viewName, String instanceName); @Transactional void uninstallViewInstance(ViewInstanceEntity instanceEntity); @Transactional void removeInstanceData(ViewInstanceEntity instanceEntity, String key); @Transactional void copyPrivileges(ViewInstanceEntity sourceInstanceEntity,
                             ViewInstanceEntity targetInstanceEntity); void fireEvent(Event event); synchronized void registerListener(Listener listener, String viewName, String viewVersion); synchronized void unregisterListener(Listener listener, String viewName, String viewVersion); boolean hasPermission(PermissionEntity permissionEntity, ResourceEntity resourceEntity, String userName); boolean checkPermission(String viewName, String version, String instanceName, boolean readOnly); boolean checkPermission(ViewInstanceEntity instanceEntity, boolean readOnly); boolean checkAdmin(); boolean includeDefinition(ViewEntity definitionEntity); void setViewInstanceProperties(ViewInstanceEntity instanceEntity, Map<String, String> properties,
                                        ViewConfig viewConfig, ClassLoader classLoader); Cluster getCluster(ViewInstanceDefinition viewInstance); @Subscribe @AllowConcurrentEvents void onAmbariEvent(ServiceInstalledEvent event); @Subscribe void onClusterConfigFinishedEvent(ClusterConfigFinishedEvent event); void readViewArchive(Path path); static final String API_PREFIX; static final String DEFAULT_AUTO_INSTANCE_URL; }