Search in sources :

Example 1 with ValidationIssue

use of org.apache.ignite.configuration.validation.ValidationIssue in project ignite-3 by apache.

the class ConfigurationPresentationTest method beforeAll.

/**
 * Before all.
 */
@BeforeAll
static void beforeAll() {
    Validator<Value, Object> validator = new Validator<>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void validate(Value annotation, ValidationContext<Object> ctx) {
            if (Objects.equals("error", ctx.getNewValue())) {
                ctx.addIssue(new ValidationIssue("Error word"));
            }
        }
    };
    cfgRegistry = new ConfigurationRegistry(List.of(TestRootConfiguration.KEY), Map.of(Value.class, Set.of(validator)), new TestConfigurationStorage(LOCAL), List.of(), List.of());
    cfgRegistry.start();
    cfgPresentation = new HoconPresentation(cfgRegistry);
    cfg = cfgRegistry.getConfiguration(TestRootConfiguration.KEY);
}
Also used : TestConfigurationStorage(org.apache.ignite.internal.configuration.storage.TestConfigurationStorage) Matchers.nullValue(org.hamcrest.Matchers.nullValue) ConfigValue(org.apache.ignite.configuration.annotation.ConfigValue) Value(org.apache.ignite.configuration.annotation.Value) HoconPresentation(org.apache.ignite.internal.configuration.rest.presentation.hocon.HoconPresentation) ConfigurationRegistry(org.apache.ignite.internal.configuration.ConfigurationRegistry) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) Validator(org.apache.ignite.configuration.validation.Validator) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 2 with ValidationIssue

use of org.apache.ignite.configuration.validation.ValidationIssue in project ignite-3 by apache.

the class ImmutableValidator method validate.

/**
 * {@inheritDoc}
 */
@Override
public void validate(Immutable annotation, ValidationContext<Object> ctx) {
    Object oldValue = ctx.getOldValue();
    Object newValue = ctx.getNewValue();
    if (oldValue != null && !Objects.deepEquals(oldValue, newValue)) {
        ctx.addIssue(new ValidationIssue("'" + ctx.currentKey() + "' configuration value is immutable and cannot be updated [curVal=" + oldValue + ", newVal=" + newValue + ']'));
    }
}
Also used : ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue)

Example 3 with ValidationIssue

use of org.apache.ignite.configuration.validation.ValidationIssue in project ignite-3 by apache.

the class OneOfValidator method validate.

/**
 * {@inheritDoc}
 */
@Override
public void validate(OneOf annotation, ValidationContext<String> ctx) {
    String value = ctx.getNewValue();
    boolean caseSensitive = annotation.caseSensitive();
    for (String exp : annotation.value()) {
        if (caseSensitive ? exp.equals(value) : exp.equalsIgnoreCase(value)) {
            return;
        }
    }
    String message = "'" + ctx.currentKey() + "' configuration value must be one of " + Arrays.toString(annotation.value()) + (caseSensitive ? " (case sensitive)" : " (case insensitive)");
    ctx.addIssue(new ValidationIssue(message));
}
Also used : ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue)

Example 4 with ValidationIssue

use of org.apache.ignite.configuration.validation.ValidationIssue in project ignite-3 by apache.

the class TableValidatorImpl method validate.

/**
 * {@inheritDoc}
 */
@Override
public void validate(TableValidator annotation, ValidationContext<NamedListView<TableView>> ctx) {
    NamedListView<TableView> oldTables = ctx.getOldValue();
    NamedListView<TableView> newTables = ctx.getNewValue();
    for (String tableName : newTables.namedListKeys()) {
        TableView newTable = newTables.get(tableName);
        try {
            TableDefinitionImpl tbl = SchemaConfigurationConverter.convert(newTable);
            assert !tbl.keyColumns().isEmpty();
            assert !tbl.colocationColumns().isEmpty();
            TableDefinitionBuilderImpl.validateIndices(tbl.indices(), tbl.columns(), tbl.colocationColumns());
        } catch (IllegalArgumentException e) {
            ctx.addIssue(new ValidationIssue("Validator works success by key " + ctx.currentKey() + ". Found " + newTable.columns().size() + " columns"));
        }
        validateDataRegion(oldTables == null ? null : oldTables.get(tableName), newTable, ctx);
        validateNamedListKeys(newTable, ctx);
    }
}
Also used : TableDefinitionImpl(org.apache.ignite.internal.schema.definition.TableDefinitionImpl) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) TableView(org.apache.ignite.configuration.schemas.table.TableView)

Example 5 with ValidationIssue

use of org.apache.ignite.configuration.validation.ValidationIssue in project ignite-3 by apache.

the class OneOfValidatorTest method testValidate.

/**
 * Test.
 */
@ParameterizedTest
@ValueSource(booleans = { false, true })
public void testValidate(boolean caseSensitive) {
    // Prepare mocked annotation instance.
    OneOf oneOfAnnotation = mock(OneOf.class);
    when(oneOfAnnotation.value()).thenReturn(new String[] { "foo", "bar" });
    when(oneOfAnnotation.caseSensitive()).thenReturn(caseSensitive);
    // Prepare mocked validation context.
    ValidationContext<String> ctx = mock(ValidationContext.class);
    when(ctx.currentKey()).thenReturn("x");
    when(ctx.getNewValue()).thenReturn("foo", "Bar", "no");
    // Prepare issues captor.
    ArgumentCaptor<ValidationIssue> issuesCaptor = ArgumentCaptor.forClass(ValidationIssue.class);
    doNothing().when(ctx).addIssue(issuesCaptor.capture());
    // Instantiate validator.
    OneOfValidator oneOfValidator = new OneOfValidator();
    // Assert that valid value produces no issues.
    oneOfValidator.validate(oneOfAnnotation, ctx);
    assertThat(issuesCaptor.getAllValues(), is(empty()));
    // Assert that case sencitivity affects validation.
    oneOfValidator.validate(oneOfAnnotation, ctx);
    if (caseSensitive) {
        assertThat(issuesCaptor.getValue().message(), is("'x' configuration value must be one of [foo, bar] (case sensitive)"));
    } else {
        assertThat(issuesCaptor.getAllValues(), is(empty()));
    }
    // Assert that unacceptable value produces validation issue.
    oneOfValidator.validate(oneOfAnnotation, ctx);
    if (caseSensitive) {
        assertThat(issuesCaptor.getValue().message(), is("'x' configuration value must be one of [foo, bar] (case sensitive)"));
    } else {
        assertThat(issuesCaptor.getValue().message(), is("'x' configuration value must be one of [foo, bar] (case insensitive)"));
    }
}
Also used : OneOf(org.apache.ignite.configuration.validation.OneOf) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ValidationIssue (org.apache.ignite.configuration.validation.ValidationIssue)15 ValidationContext (org.apache.ignite.configuration.validation.ValidationContext)7 Test (org.junit.jupiter.api.Test)6 Validator (org.apache.ignite.configuration.validation.Validator)5 Set (java.util.Set)4 NamedListView (org.apache.ignite.configuration.NamedListView)4 Annotation (java.lang.annotation.Annotation)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 DataStorageView (org.apache.ignite.configuration.schemas.store.DataStorageView)3 TableView (org.apache.ignite.configuration.schemas.table.TableView)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Matchers.nullValue (org.hamcrest.Matchers.nullValue)3 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)3 TimeUnit (java.util.concurrent.TimeUnit)2 DataStorageConfiguration (org.apache.ignite.configuration.schemas.store.DataStorageConfiguration)2 PageMemoryDataRegionConfigurationSchema (org.apache.ignite.configuration.schemas.store.PageMemoryDataRegionConfigurationSchema)2 RocksDbDataRegionConfigurationSchema (org.apache.ignite.configuration.schemas.store.RocksDbDataRegionConfigurationSchema)2 UnsafeMemoryAllocatorConfigurationSchema (org.apache.ignite.configuration.schemas.store.UnsafeMemoryAllocatorConfigurationSchema)2 HashIndexChange (org.apache.ignite.configuration.schemas.table.HashIndexChange)2 HashIndexConfigurationSchema (org.apache.ignite.configuration.schemas.table.HashIndexConfigurationSchema)2