Search in sources :

Example 1 with ValidationContext

use of org.apache.ignite.configuration.validation.ValidationContext 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 ValidationContext

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

the class ValidationUtilTest method validateLeafNode.

@Test
public void validateLeafNode() {
    var rootsNode = new SuperRoot(key -> null, Map.of(ValidatedRootConfiguration.KEY, root));
    Validator<LeafValidation, String> validator = new Validator<>() {

        @Override
        public void validate(LeafValidation annotation, ValidationContext<String> ctx) {
            assertEquals("root.child.str", ctx.currentKey());
            assertEquals("foo", ctx.getOldValue());
            assertEquals("foo", ctx.getNewValue());
            ctx.addIssue(new ValidationIssue("bar"));
        }
    };
    Map<Class<? extends Annotation>, Set<Validator<?, ?>>> validators = Map.of(LeafValidation.class, Set.of(validator));
    List<ValidationIssue> issues = ValidationUtil.validate(rootsNode, rootsNode, null, new HashMap<>(), validators);
    assertEquals(1, issues.size());
    assertEquals("bar", issues.get(0).message());
}
Also used : Set(java.util.Set) SuperRoot(org.apache.ignite.internal.configuration.SuperRoot) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) Annotation(java.lang.annotation.Annotation) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) Validator(org.apache.ignite.configuration.validation.Validator) Test(org.junit.jupiter.api.Test)

Example 3 with ValidationContext

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

the class ValidationUtilTest method validateInnerNode.

@Test
public void validateInnerNode() throws Exception {
    var rootsNode = new SuperRoot(key -> null, Map.of(ValidatedRootConfiguration.KEY, root));
    Validator<InnerValidation, ValidatedChildView> validator = new Validator<>() {

        @Override
        public void validate(InnerValidation annotation, ValidationContext<ValidatedChildView> ctx) {
            assertEquals("root.child", ctx.currentKey());
            assertEquals("foo", ctx.getOldValue().str());
            assertEquals("foo", ctx.getNewValue().str());
            ctx.addIssue(new ValidationIssue("bar"));
        }
    };
    Map<Class<? extends Annotation>, Set<Validator<?, ?>>> validators = Map.of(InnerValidation.class, Set.of(validator));
    List<ValidationIssue> issues = ValidationUtil.validate(rootsNode, rootsNode, null, new HashMap<>(), validators);
    assertEquals(1, issues.size());
    assertEquals("bar", issues.get(0).message());
}
Also used : Set(java.util.Set) SuperRoot(org.apache.ignite.internal.configuration.SuperRoot) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) Annotation(java.lang.annotation.Annotation) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) Validator(org.apache.ignite.configuration.validation.Validator) Test(org.junit.jupiter.api.Test)

Example 4 with ValidationContext

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

the class ConfigurationChangerTest method testModifiedFromAnotherStorageWithIncompatibleChanges.

/**
 * Test that subsequent change of configuration is failed if changes are incompatible.
 */
@Test
public void testModifiedFromAnotherStorageWithIncompatibleChanges() throws Exception {
    ConfigurationChanger changer1 = createChanger(KEY);
    changer1.start();
    Validator<MaybeInvalid, Object> validator = new Validator<>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void validate(MaybeInvalid annotation, ValidationContext<Object> ctx) {
            ctx.addIssue(new ValidationIssue("foo"));
        }
    };
    ConfigurationChanger changer2 = new TestConfigurationChanger(cgen, List.of(KEY), Map.of(MaybeInvalid.class, Set.of(validator)), storage, List.of(), List.of());
    changer2.start();
    changer1.change(source(KEY, (FirstChange parent) -> parent.changeChild(change -> change.changeIntCfg(1).changeStrCfg("1")).changeElements(change -> change.create("a", element -> element.changeStrCfg("1"))))).get(1, SECONDS);
    assertThrows(ExecutionException.class, () -> changer2.change(source(KEY, (FirstChange parent) -> parent.changeChild(change -> change.changeIntCfg(2).changeStrCfg("2")).changeElements(change -> change.create("a", element -> element.changeStrCfg("2")).create("b", element -> element.changeStrCfg("2"))))).get(1, SECONDS));
    FirstView newRoot = (FirstView) changer2.getRootNode(KEY);
    assertEquals(1, newRoot.child().intCfg());
    assertEquals("1", newRoot.child().strCfg());
    assertEquals("1", newRoot.elements().get("a").strCfg());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) Data(org.apache.ignite.internal.configuration.storage.Data) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) TimeoutException(java.util.concurrent.TimeoutException) ConstructableTreeNode(org.apache.ignite.internal.configuration.tree.ConstructableTreeNode) CompletableFuture(java.util.concurrent.CompletableFuture) ConfigurationStorage(org.apache.ignite.internal.configuration.storage.ConfigurationStorage) Validator(org.apache.ignite.configuration.validation.Validator) Retention(java.lang.annotation.Retention) NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) Immutable(org.apache.ignite.configuration.validation.Immutable) AfterAll(org.junit.jupiter.api.AfterAll) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyPathNode(org.apache.ignite.internal.configuration.direct.KeyPathNode) Map(java.util.Map) KEY(org.apache.ignite.internal.configuration.FirstConfiguration.KEY) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) FIELD(java.lang.annotation.ElementType.FIELD) ConfigurationRoot(org.apache.ignite.configuration.annotation.ConfigurationRoot) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) NamedListView(org.apache.ignite.configuration.NamedListView) Set(java.util.Set) Target(java.lang.annotation.Target) ConfigurationChangeException(org.apache.ignite.configuration.ConfigurationChangeException) RootKey(org.apache.ignite.configuration.RootKey) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) Serializable(java.io.Serializable) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) ConfigValue(org.apache.ignite.configuration.annotation.ConfigValue) List(java.util.List) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) LOCAL(org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL) Config(org.apache.ignite.configuration.annotation.Config) TestConfigurationStorage(org.apache.ignite.internal.configuration.storage.TestConfigurationStorage) ConfigurationSource(org.apache.ignite.internal.configuration.tree.ConfigurationSource) Matchers.containsString(org.hamcrest.Matchers.containsString) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Value(org.apache.ignite.configuration.annotation.Value) ConfigurationAsmGenerator(org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) Validator(org.apache.ignite.configuration.validation.Validator) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) Test(org.junit.jupiter.api.Test)

Example 5 with ValidationContext

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

the class ValidationUtilTest method validateNamedListNode.

@Test
public void validateNamedListNode() throws Exception {
    var rootsNode = new SuperRoot(key -> null, Map.of(ValidatedRootConfiguration.KEY, root));
    Validator<NamedListValidation, NamedListView<?>> validator = new Validator<>() {

        @Override
        public void validate(NamedListValidation annotation, ValidationContext<NamedListView<?>> ctx) {
            assertEquals("root.elements", ctx.currentKey());
            assertEquals(List.of(), ctx.getOldValue().namedListKeys());
            assertEquals(List.of(), ctx.getNewValue().namedListKeys());
            ctx.addIssue(new ValidationIssue("bar"));
        }
    };
    Map<Class<? extends Annotation>, Set<Validator<?, ?>>> validators = Map.of(NamedListValidation.class, Set.of(validator));
    List<ValidationIssue> issues = ValidationUtil.validate(rootsNode, rootsNode, null, new HashMap<>(), validators);
    assertEquals(1, issues.size());
    assertEquals("bar", issues.get(0).message());
}
Also used : Set(java.util.Set) SuperRoot(org.apache.ignite.internal.configuration.SuperRoot) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) Annotation(java.lang.annotation.Annotation) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) NamedListView(org.apache.ignite.configuration.NamedListView) Validator(org.apache.ignite.configuration.validation.Validator) Test(org.junit.jupiter.api.Test)

Aggregations

ValidationContext (org.apache.ignite.configuration.validation.ValidationContext)7 ValidationIssue (org.apache.ignite.configuration.validation.ValidationIssue)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 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 DataStorageView (org.apache.ignite.configuration.schemas.store.DataStorageView)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 PartialIndexConfigurationSchema (org.apache.ignite.configuration.schemas.table.PartialIndexConfigurationSchema)2