IndexValidator implements ConfigurationValidator<Index> { @Override public void validate(CacheElementOperation operation, Index config) throws IllegalArgumentException { switch (operation) { case CREATE: if (config.getName() == null) { throw new IllegalArgumentException("Name is required."); } if (config.getExpression() == null) { throw new IllegalArgumentException("Expression is required."); } if (config.getRegionPath() == null) { throw new IllegalArgumentException("RegionPath is required."); } if (config.getIndexType() == IndexType.HASH_DEPRECATED) { throw new IllegalArgumentException("IndexType HASH is not allowed."); } break; case DELETE: if (config.getName() == null) { throw new IllegalArgumentException("Name is required."); } if (config.getRegionPath() == null) { throw new IllegalArgumentException("RegionPath is required."); } break; } }  @Override void validate(CacheElementOperation operation, Index config);  }
@Test public void validate_happy() { when(configurationPersistenceService.getCacheConfig(any(), anyBoolean())) .thenReturn(cacheConfig); when(cacheConfig.findRegionConfiguration(anyString())).thenReturn(regionConfig); indexValidator.validate(CacheElementOperation.CREATE, index); }
@Test public void validate_missingExpression() { index.setExpression(null); assertThatThrownBy(() -> indexValidator.validate(CacheElementOperation.CREATE, index)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("Expression is required"); }
@Test public void validate_hasRegionPath() { index.setRegionPath(null); assertSoftly(softly -> { softly.assertThatThrownBy(() -> indexValidator.validate(CacheElementOperation.CREATE, index)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("RegionPath is required"); softly.assertThatThrownBy(() -> indexValidator.validate(CacheElementOperation.DELETE, index)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("RegionPath is required"); }); }
@Test public void validate_HashTypeNotAllowed() { index.setIndexType(IndexType.HASH_DEPRECATED); assertThatThrownBy(() -> indexValidator.validate(CacheElementOperation.CREATE, index)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("IndexType HASH is not allowed"); }
@Test public void validate_IndexHasName() { index.setName(null); assertSoftly(softly -> { softly.assertThatThrownBy(() -> indexValidator.validate(CacheElementOperation.CREATE, index)) .isInstanceOf(IllegalArgumentException.class).hasMessageContaining("Name is required"); softly.assertThatThrownBy(() -> indexValidator.validate(CacheElementOperation.DELETE, index)) .isInstanceOf(IllegalArgumentException.class).hasMessageContaining("Name is required"); }); }