PdxValidator implements ConfigurationValidator<Pdx> { @Override public void validate(CacheElementOperation operation, Pdx pdx) { AutoSerializer autoSerializer = pdx.getAutoSerializer(); if (autoSerializer != null) { List<String> patterns = autoSerializer.getPatterns(); switch (operation) { case CREATE: if (patterns == null || patterns.isEmpty()) { throw new IllegalArgumentException( "The autoSerializer must have at least one pattern."); } case UPDATE: if (patterns != null && patterns.isEmpty()) { throw new IllegalArgumentException( "The autoSerializer must have at least one pattern."); } } } }  @Override void validate(CacheElementOperation operation, Pdx pdx);  }
@Test public void validateForAnyOperation_acceptsDefaultPdx() { PdxValidator validator = new PdxValidator(); Pdx pdx = new Pdx(); Stream.of(CREATE, UPDATE, DELETE) .forEach(operation -> assertThatCode(() -> validator.validate(operation, pdx)) .as(operation.name()) .doesNotThrowAnyException()); }
@Test public void validateForCreate_throwsIfAutoSerializerHasNoPatterns() { PdxValidator validator = new PdxValidator(); Pdx pdx = new Pdx(); List<String> noPatterns = emptyList(); pdx.setAutoSerializer(new AutoSerializer(false, noPatterns)); assertThatThrownBy(() -> validator.validate(CREATE, pdx)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("at least one pattern"); }
@Test public void validateForCreate_throwsIfAutoSerializerHasNullPatterns() { PdxValidator validator = new PdxValidator(); Pdx pdx = new Pdx(); pdx.setAutoSerializer(new AutoSerializer(false, (List<String>) null)); assertThatThrownBy(() -> validator.validate(CREATE, pdx)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("at least one pattern"); }
@Test public void validateForDelete_acceptsAutoSerializerWithNoPatterns() { PdxValidator validator = new PdxValidator(); Pdx pdx = new Pdx(); List<String> noPatterns = emptyList(); pdx.setAutoSerializer(new AutoSerializer(false, noPatterns)); assertThatCode(() -> validator.validate(DELETE, pdx)) .doesNotThrowAnyException(); }
@Test public void validateForDelete_acceptsAutoSerializerWithNullPatterns() { PdxValidator validator = new PdxValidator(); Pdx pdx = new Pdx(); pdx.setAutoSerializer(new AutoSerializer(false, (List<String>) null)); assertThatCode(() -> validator.validate(DELETE, pdx)) .doesNotThrowAnyException(); }
@Test public void validateForUpdate_acceptsAutoSerializerWithNullPatterns() { PdxValidator validator = new PdxValidator(); Pdx pdx = new Pdx(); pdx.setAutoSerializer(new AutoSerializer(false, (List<String>) null)); assertThatCode(() -> validator.validate(UPDATE, pdx)) .doesNotThrowAnyException(); }
@Test public void validateForUpdate_throwsIfAutoSerializerHasNoPatterns() { PdxValidator validator = new PdxValidator(); Pdx pdx = new Pdx(); List<String> noPatterns = emptyList(); pdx.setAutoSerializer(new AutoSerializer(false, noPatterns)); assertThatThrownBy(() -> validator.validate(UPDATE, pdx)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("at least one pattern"); }