LocatorClusterManagementService implements ClusterManagementService { @Override public <T extends AbstractConfiguration<?>> ClusterManagementRealizationResult delete( T config) { CacheConfigurationManager configurationManager = (CacheConfigurationManager) getConfigurationManager(config); if (persistenceService == null) { return assertSuccessful(new ClusterManagementRealizationResult(StatusCode.ERROR, "Cluster configuration service needs to be enabled.")); } lockCMS(); try { try { commonValidator.validate(CacheElementOperation.DELETE, config); ConfigurationValidator validator = validators.get(config.getClass()); if (validator != null) { validator.validate(CacheElementOperation.DELETE, config); } } catch (IllegalArgumentException e) { raise(StatusCode.ILLEGAL_ARGUMENT, e); } String[] groupsWithThisElement = memberValidator.findGroupsWithThisElement(config, configurationManager); if (groupsWithThisElement.length == 0) { raise(StatusCode.ENTITY_NOT_FOUND, config.getClass().getSimpleName() + " '" + config.getId() + "' does not exist."); } ClusterManagementRealizationResult result = new ClusterManagementRealizationResult(); List<RealizationResult> functionResults = executeCacheRealizationFunction( config, CacheElementOperation.DELETE, memberValidator.findServers(groupsWithThisElement)); functionResults.forEach(result::addMemberStatus); if (result.getStatusCode() != StatusCode.OK) { result.setStatus(StatusCode.ERROR, "Failed to delete on all members."); return result; } List<String> updatedGroups = new ArrayList<>(); List<String> failedGroups = new ArrayList<>(); for (String finalGroup : groupsWithThisElement) { try { configurationManager.delete(config, finalGroup); updatedGroups.add(finalGroup); } catch (Exception e) { logger.error(e.getMessage(), e); failedGroups.add(finalGroup); } } setResultStatus(result, updatedGroups, failedGroups); return assertSuccessful(result); } finally { unlockCMS(); } } LocatorClusterManagementService(InternalCache cache,
      InternalConfigurationPersistenceService persistenceService); @VisibleForTesting  LocatorClusterManagementService(
      InternalCache cache,
      InternalConfigurationPersistenceService persistenceService,
      Map<Class, ConfigurationManager> managers,
      Map<Class, ConfigurationValidator> validators,
      MemberValidator memberValidator,
      CommonConfigurationValidator commonValidator,
      OperationManager operationManager); @Override ClusterManagementRealizationResult create(T config); @Override ClusterManagementRealizationResult delete(
      T config); @Override ClusterManagementRealizationResult update(
      T config); @Override ClusterManagementListResult<T, R> list(
      T filter); @Override ClusterManagementGetResult<T, R> get(
      T config); @Override ClusterManagementOperationResult<A, V> start(
      A op); @Override ClusterManagementListOperationsResult<A, V> list(
      A opType); @Override ClusterManagementOperationResult<A, V> get(
      A opType, String opId); @Override CompletableFuture<ClusterManagementOperationResult<A, V>> getFuture(
      A opType, String opId); boolean isConnected(); @Override void close(); @VisibleForTesting
  // the dlock service name used by the CMS
  static final String CMS_DLOCK_SERVICE_NAME; }
@Test public void lockAndUnlockCalledAtDeleteWithException() { try { service.delete(regionConfig); } catch (Exception ignore) { } verify(dLockService).lock(LocatorClusterManagementService.CMS_DLOCK_SERVICE_NAME, -1, -1); verify(dLockService).unlock(LocatorClusterManagementService.CMS_DLOCK_SERVICE_NAME); }
@Test public void delete_validatorIsCalledCorrectly() { doReturn(Collections.emptySet()).when(memberValidator).findServers(anyString()); doReturn(new String[] {"cluster"}).when(memberValidator).findGroupsWithThisElement( regionConfig, regionManager); doNothing().when(persistenceService).updateCacheConfig(any(), any()); service.delete(regionConfig); verify(cacheElementValidator).validate(CacheElementOperation.DELETE, regionConfig); verify(regionValidator).validate(CacheElementOperation.DELETE, regionConfig); verify(memberValidator).findGroupsWithThisElement(regionConfig, regionManager); memberValidator.findServers("cluster"); }
@Test public void delete_unknownRegionFails() { Region config = new Region(); config.setName("unknown"); doReturn(new String[] {}).when(memberValidator).findGroupsWithThisElement(any(), any()); assertThatThrownBy(() -> service.delete(config)) .isInstanceOf(ClusterManagementException.class) .hasMessage("ENTITY_NOT_FOUND: Region 'unknown' does not exist."); }
@Test public void delete_usingGroupFails() { Region config = new Region(); config.setName("test"); config.setGroup("group1"); assertThatThrownBy(() -> service.delete(config)) .isInstanceOf(ClusterManagementException.class) .hasMessage("ILLEGAL_ARGUMENT: Group is an invalid option when deleting region."); }
@Test public void deleteWithNoMember() { doReturn(new String[] {"cluster"}).when(memberValidator).findGroupsWithThisElement(any(), any()); doReturn(Collections.emptySet()).when(memberValidator); memberValidator.findServers(); doReturn(null).when(persistenceService).getConfiguration(any()); org.apache.geode.cache.Region<String, Object> mockRegion = mock(org.apache.geode.cache.Region.class); doReturn(mockRegion).when(persistenceService).getConfigurationRegion(); ClusterManagementRealizationResult result = service.delete(regionConfig); verify(regionManager).delete(eq(regionConfig), any(CacheConfig.class)); assertThat(result.isSuccessful()).isTrue(); assertThat(result.getMemberStatuses()).hasSize(0); assertThat(result.getStatusMessage()) .contains("Successfully updated configuration for cluster"); }