MemberValidator { public void validateCreate(AbstractConfiguration config, CacheConfigurationManager manager) { Map<String, AbstractConfiguration> existingElementsAndTheirGroups = findCacheElement(config, manager); if (existingElementsAndTheirGroups.size() == 0) { return; } if (!(config instanceof GroupableConfiguration)) { throw new EntityExistsException( config.getClass().getSimpleName() + " '" + config.getId() + "' already exists"); } String configGroup = AbstractConfiguration.getGroupName(config.getGroup()); if (existingElementsAndTheirGroups.keySet().contains(configGroup)) { throw new EntityExistsException( config.getClass().getSimpleName() + " '" + config.getId() + "' already exists in group " + configGroup); } String[] groups = existingElementsAndTheirGroups.keySet().toArray(new String[0]); Set<DistributedMember> membersOfExistingGroups = findServers(groups); Set<DistributedMember> membersOfNewGroup = findServers(config.getGroup()); Set<DistributedMember> intersection = new HashSet<>(membersOfExistingGroups); intersection.retainAll(membersOfNewGroup); if (intersection.size() > 0) { String members = intersection.stream().map(DistributedMember::getName).collect(Collectors.joining(", ")); throw new EntityExistsException( config.getClass().getSimpleName() + " '" + config.getId() + "' already exists on member(s) " + members + "."); } for (Map.Entry<String, AbstractConfiguration> existing : existingElementsAndTheirGroups .entrySet()) { manager.checkCompatibility(config, existing.getKey(), existing.getValue()); } } MemberValidator(InternalCache cache, ConfigurationPersistenceService persistenceService); void validateCreate(AbstractConfiguration config, CacheConfigurationManager manager); String[] findGroupsWithThisElement(AbstractConfiguration config,
      CacheConfigurationManager manager); Map<String, AbstractConfiguration> findCacheElement(AbstractConfiguration config,
      CacheConfigurationManager manager); Set<String> findGroups(String regionName); Set<DistributedMember> findServers(String... groups); Set<DistributedMember> findServers(AbstractConfiguration configuration); Set<DistributedMember> findMembers(String id, String... groups); Set<DistributedMember> findMembers(boolean includeLocators, String... groups);  }
@Test public void validateCreate1() { cacheConfig.getRegions().add(xmlRegionConfig); when(service.getCacheConfig("cluster", true)).thenReturn(cacheConfig); regionConfig.setGroup("group1"); assertThatThrownBy(() -> validator.validateCreate(regionConfig, regionManager)) .isInstanceOf(EntityExistsException.class) .hasMessageContaining("member4").hasMessageContaining("member2") .hasMessageContaining("already exists on member(s)"); }
@Test public void validateCreate2() { cacheConfig.getRegions().add(xmlRegionConfig); when(service.getCacheConfig("group1", true)).thenReturn(cacheConfig); regionConfig.setGroup("group2"); assertThatThrownBy(() -> validator.validateCreate(regionConfig, regionManager)) .isInstanceOf(EntityExistsException.class) .hasMessageContaining("already exists on member(s) member4."); }
@Test public void validateCreate4() { cacheConfig.getRegions().add(xmlRegionConfig); when(service.getCacheConfig("group1", true)).thenReturn(cacheConfig); regionConfig.setGroup("group3"); validator.validateCreate(regionConfig, regionManager); }