RegionConfigValidator implements ConfigurationValidator<Region> { @Override public void validate(CacheElementOperation operation, Region config) throws IllegalArgumentException { switch (operation) { case UPDATE: case CREATE: validateCreate(config); break; case DELETE: validateDelete(config); default: } } RegionConfigValidator(InternalCache cache); @Override void validate(CacheElementOperation operation, Region config);  }
@Test public void checkSecurityForDiskAccess() { config.setName("regionName"); config.setType(RegionType.REPLICATE_PERSISTENT); validator.validate(CacheElementOperation.CREATE, config); verify(securityService).authorize(ResourcePermission.Resource.CLUSTER, ResourcePermission.Operation.WRITE, ResourcePermission.Target.DISK); assertThat(config.getType()).isEqualTo(RegionType.REPLICATE_PERSISTENT); }
@Test public void noChangesWhenTypeIsSet() { config.setName("regionName"); config.setType(RegionType.REPLICATE); validator.validate(CacheElementOperation.CREATE, config); verify(securityService, times(0)).authorize( any(ResourcePermission.Resource.class), any(ResourcePermission.Operation.class), any(ResourcePermission.Target.class)); assertThat(config.getType()).isEqualTo(RegionType.REPLICATE); }
@Test public void invalidName1() { config.setName("__test"); config.setType(RegionType.REPLICATE); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining("Region names may not begin with a double-underscore"); }
@Test public void invalidName2() { config.setName("a!&b"); config.setType(RegionType.REPLICATE); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining( "Region names may only be alphanumeric and may contain hyphens or underscores"); }
@Test public void missingType() { config.setName("test"); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining( "Region type is required."); }
@Test public void invalidType() { config.setName("test"); config.setType(RegionType.LEGACY); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining( "Region type is unsupported."); }
@Test public void invalidRedundancy() { config.setRedundantCopies(-1); config.setType(RegionType.PARTITION); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining( "redundantCopies cannot be less than 0 or greater than 3."); config.setRedundantCopies(4); config.setType(RegionType.PARTITION); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining( "redundantCopies cannot be less than 0 or greater than 3."); }
@Test public void replicateWithRedundancy() { config.setName("test"); config.setType(RegionType.REPLICATE); config.setRedundantCopies(2); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining( "redundantCopies can only be set with PARTITION regions."); }
@Test public void validateExpiration() { config.setName("test"); config.setType(RegionType.REPLICATE); config.addExpiry(null, null, null); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining( "Expiration type must be set."); config.getExpirations().clear(); config.addExpiry(Region.ExpirationType.ENTRY_IDLE_TIME, null, null); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining( "Expiration timeInSeconds must be greater than or equal to 0."); config.getExpirations().clear(); config.addExpiry(Region.ExpirationType.ENTRY_IDLE_TIME, -1, null); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining( "Expiration timeInSeconds must be greater than or equal to 0."); config.getExpirations().clear(); config.addExpiry(Region.ExpirationType.LEGACY, 100, null); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining( "Invalid Expiration type."); config.getExpirations().clear(); config.addExpiry(Region.ExpirationType.ENTRY_IDLE_TIME, 100, Region.ExpirationAction.LEGACY); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining( "Invalid Expiration action."); config.getExpirations().clear(); config.addExpiry(Region.ExpirationType.ENTRY_IDLE_TIME, 100, null); config.addExpiry(Region.ExpirationType.ENTRY_IDLE_TIME, 200, null); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)).isInstanceOf( IllegalArgumentException.class) .hasMessageContaining( "Can not have multiple " + Region.ExpirationType.ENTRY_IDLE_TIME.name()); config.getExpirations().clear(); config.addExpiry(Region.ExpirationType.ENTRY_IDLE_TIME, 100, null); config.addExpiry(Region.ExpirationType.ENTRY_TIME_TO_LIVE, 200, null); validator.validate(CacheElementOperation.CREATE, config); }
@Test public void evictionEmptyHappy() { config.setName("test"); config.setType(RegionType.REPLICATE); config.setEviction(eviction); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Eviction type must be set."); }
@Test public void evictionHeapHappy() { config.setName("test"); config.setType(RegionType.REPLICATE); eviction.setType(Region.EvictionType.HEAP_PERCENTAGE); config.setEviction(eviction); validator.validate(CacheElementOperation.CREATE, config); }
@Test public void evictionEntryCountHappy() { config.setName("test"); config.setType(RegionType.REPLICATE); config.setEviction(eviction); eviction.setEntryCount(10); validator.validate(CacheElementOperation.CREATE, config); }
@Test public void evictionEntryCountMissingCount() { config.setName("test"); config.setType(RegionType.REPLICATE); config.setEviction(eviction); eviction.setEntryCount(null); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("EntryCount must be set for: ENTRY_COUNT"); }
@Test public void evictionMaxMemoryMbHappy() { config.setName("test"); config.setType(RegionType.REPLICATE); config.setEviction(eviction); eviction.setMemorySizeMb(10); validator.validate(CacheElementOperation.CREATE, config); }
@Test public void evictionMaxMemoryMbMissingMemorySize() { config.setName("test"); config.setType(RegionType.REPLICATE); config.setEviction(eviction); eviction.setMemorySizeMb(null); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("MemorySizeMb must be set for: MEMORY_SIZE"); }
@Test public void objectSizerProvidedHappy() { config.setName("test"); config.setType(RegionType.REPLICATE); eviction.setObjectSizer(new ClassName("ObjectSizer")); eviction.setMemorySizeMb(10); config.setEviction(eviction); validator.validate(CacheElementOperation.CREATE, config); eviction = new Region.Eviction(); config.setEviction(eviction); eviction.setObjectSizer(new ClassName("ObjectSizer")); eviction.setType(Region.EvictionType.HEAP_PERCENTAGE); validator.validate(CacheElementOperation.CREATE, config); }
@Test public void objectSizerProvidedSad() { config.setName("test"); config.setType(RegionType.REPLICATE); config.setEviction(eviction); eviction.setEntryCount(10); eviction.setObjectSizer(new ClassName("ObjectSizer")); assertThatThrownBy(() -> validator.validate(CacheElementOperation.CREATE, config)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("ObjectSizer must not be set for: ENTRY_COUNT"); }