You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/09/10 06:20:07 UTC

[GitHub] [ignite-3] ibessonov opened a new pull request #329: IGNITE-15486 JUnit configuration extension implemented

ibessonov opened a new pull request #329:
URL: https://github.com/apache/ignite-3/pull/329


   https://issues.apache.org/jira/browse/IGNITE-15486


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite-3] ibessonov commented on a change in pull request #329: IGNITE-15486 JUnit configuration extension implemented

Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #329:
URL: https://github.com/apache/ignite-3/pull/329#discussion_r705996302



##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);

Review comment:
       done

##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean supportsParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        return parameterContext.isAnnotated(InjectConfiguration.class)
+            && supportType(parameterContext.getParameter().getType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object resolveParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        Parameter parameter = parameterContext.getParameter();
+
+        var cgen = (ConfigurationAsmGenerator)extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(CGEN_KEY);

Review comment:
       done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite-3] ibessonov merged pull request #329: IGNITE-15486 JUnit configuration extension implemented

Posted by GitBox <gi...@apache.org>.
ibessonov merged pull request #329:
URL: https://github.com/apache/ignite-3/pull/329


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite-3] tkalkirill commented on a change in pull request #329: IGNITE-15486 JUnit configuration extension implemented

Posted by GitBox <gi...@apache.org>.
tkalkirill commented on a change in pull request #329:
URL: https://github.com/apache/ignite-3/pull/329#discussion_r705963208



##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtensionTest.java
##########
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.util.concurrent.ExecutionException;
+import org.apache.ignite.internal.configuration.sample.DiscoveryConfiguration;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Test basic scenarios of {@link ConfigurationExtension}

Review comment:
       ```suggestion
    * Test basic scenarios of {@link ConfigurationExtension}.
   ```

##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean supportsParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        return parameterContext.isAnnotated(InjectConfiguration.class)
+            && supportType(parameterContext.getParameter().getType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object resolveParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        Parameter parameter = parameterContext.getParameter();
+
+        var cgen = (ConfigurationAsmGenerator)extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(CGEN_KEY);
+
+        try {
+            return cfgValue(parameter.getType(), parameter.getAnnotation(InjectConfiguration.class), cgen);
+        }
+        catch (ClassNotFoundException classNotFoundException) {
+            throw new ParameterResolutionException(
+                "Cannot find a configuration schema class that matches " + parameter.getType().getCanonicalName(),
+                classNotFoundException
+            );
+        }
+    }
+
+    /**
+     * Instantiates configuration instance for injection.
+     *
+     * @param type Type of the field or parameter. Class name must end with {@code Configuration}.
+     * @param annotation Annotation present on the field or parameter.
+     * @param cgen Runtime code generator associated with the extension instance.
+     * @return Mock configuration instance.
+     * @throws ClassNotFoundException If corresponding configuration schema class is not found.
+     * @see #supportType(Class)
+     */
+    private static Object cfgValue(Class<?> type, InjectConfiguration annotation, ConfigurationAsmGenerator cgen)

Review comment:
       ```suggestion
       private static Object cfgValue(
       Class<?> type, 
       InjectConfiguration annotation, 
       ConfigurationAsmGenerator cgen
       )
   ```

##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean supportsParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        return parameterContext.isAnnotated(InjectConfiguration.class)
+            && supportType(parameterContext.getParameter().getType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object resolveParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        Parameter parameter = parameterContext.getParameter();
+
+        var cgen = (ConfigurationAsmGenerator)extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(CGEN_KEY);
+
+        try {
+            return cfgValue(parameter.getType(), parameter.getAnnotation(InjectConfiguration.class), cgen);
+        }
+        catch (ClassNotFoundException classNotFoundException) {
+            throw new ParameterResolutionException(
+                "Cannot find a configuration schema class that matches " + parameter.getType().getCanonicalName(),
+                classNotFoundException
+            );
+        }
+    }
+
+    /**
+     * Instantiates configuration instance for injection.
+     *
+     * @param type Type of the field or parameter. Class name must end with {@code Configuration}.
+     * @param annotation Annotation present on the field or parameter.
+     * @param cgen Runtime code generator associated with the extension instance.
+     * @return Mock configuration instance.
+     * @throws ClassNotFoundException If corresponding configuration schema class is not found.
+     * @see #supportType(Class)
+     */
+    private static Object cfgValue(Class<?> type, InjectConfiguration annotation, ConfigurationAsmGenerator cgen)
+        throws ClassNotFoundException
+    {
+        // Trying to find a schema class using configuration naming convention. This code won't work for inner Java
+        // classes, extension is designed to mock actual configurations from public API to configure Ignite components.
+        Class<?> schemaClass = Class.forName(type.getCanonicalName() + "Schema");
+
+        // Internal configuration extensions are not yet supported. This will probably be changed in the future.
+        cgen.compileRootSchema(schemaClass, Map.of());
+
+        // RootKey must be mocked, there's no way to instantiate it using a public constructor.
+        RootKey rootKey = mock(RootKey.class);
+
+        when(rootKey.key()).thenReturn("mock");
+        when(rootKey.type()).thenReturn(LOCAL);
+        when(rootKey.schemaClass()).thenReturn(schemaClass);
+        when(rootKey.internal()).thenReturn(false);
+
+        SuperRoot superRoot = new SuperRoot(s -> new RootInnerNode(rootKey, cgen.instantiateNode(schemaClass)));
+
+        ConfigObject hoconCfg = ConfigFactory.parseString(annotation.value()).root();
+
+        HoconConverter.hoconSource(hoconCfg).descend(superRoot);
+
+        ConfigurationUtil.addDefaults(superRoot);
+
+        // Reference to the super root is required to make DynamicConfigurationChanger#change method atomic.
+        var superRootRef = new AtomicReference<>(superRoot);
+
+        return cgen.instantiateCfg(rootKey, new DynamicConfigurationChanger() {
+            /** {@inheritDoc} */
+            @Override public CompletableFuture<Void> change(ConfigurationSource change) {
+                while (true) {
+                    SuperRoot sr = superRootRef.get();
+
+                    SuperRoot copy = sr.copy();
+
+                    change.descend(copy);
+
+                    if (superRootRef.compareAndSet(sr, copy))
+                        return completedFuture(null);
+                }
+            }
+
+            /** {@inheritDoc} */
+            @Override public InnerNode getRootNode(RootKey<?, ?> rk) {
+                return superRootRef.get().getRoot(rk);
+            }
+        });
+    }
+
+    /**
+     * Looks for the annotated field inside the given test class.
+     *
+     * @return Annotated fields.
+     */
+    private static List<Field> getMatchingFields(Class<?> testClass) {
+        List<Field> fields = AnnotationSupport.findAnnotatedFields(
+            testClass,
+            InjectConfiguration.class,
+            field -> supportType(field.getType()),
+            HierarchyTraversalMode.TOP_DOWN
+        );
+
+        return fields;
+    }
+
+    /**
+     * Checks that instance of the gicen class can be injected by the extension.

Review comment:
       ```suggestion
        * Checks that instance of the given class can be injected by the extension.
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite-3] sashapolo commented on a change in pull request #329: IGNITE-15486 JUnit configuration extension implemented

Posted by GitBox <gi...@apache.org>.
sashapolo commented on a change in pull request #329:
URL: https://github.com/apache/ignite-3/pull/329#discussion_r705978615



##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/DynamicConfigurationChanger.java
##########
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration;
+
+import java.util.concurrent.CompletableFuture;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+
+/**
+ * Interface to provide configuration access to {@link DynamicConfiguration}, {@link NamedListConfiguration} and

Review comment:
       `provide configuration access` - what does this sentence mean?

##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/DynamicConfigurationChanger.java
##########
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration;
+
+import java.util.concurrent.CompletableFuture;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+
+/**
+ * Interface to provide configuration access to {@link DynamicConfiguration}, {@link NamedListConfiguration} and
+ * {@link DynamicProperty}.
+ */
+public interface DynamicConfigurationChanger {
+    /**
+     * Changes the configuration.
+     *
+     * @param source Configuration source to create patch from.
+     * @return Future that is completed on change completion.
+     */
+    CompletableFuture<Void> change(ConfigurationSource source);
+
+    /**
+     * Get root node by root key. Subject to revisiting.

Review comment:
       Subject to revisiting?

##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean supportsParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        return parameterContext.isAnnotated(InjectConfiguration.class)
+            && supportType(parameterContext.getParameter().getType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object resolveParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        Parameter parameter = parameterContext.getParameter();
+
+        var cgen = (ConfigurationAsmGenerator)extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(CGEN_KEY);

Review comment:
       also, this line is too long

##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean supportsParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        return parameterContext.isAnnotated(InjectConfiguration.class)
+            && supportType(parameterContext.getParameter().getType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object resolveParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        Parameter parameter = parameterContext.getParameter();
+
+        var cgen = (ConfigurationAsmGenerator)extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(CGEN_KEY);

Review comment:
       there's a `get(Object, Class)` method that you can use here

##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean supportsParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        return parameterContext.isAnnotated(InjectConfiguration.class)
+            && supportType(parameterContext.getParameter().getType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object resolveParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        Parameter parameter = parameterContext.getParameter();
+
+        var cgen = (ConfigurationAsmGenerator)extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(CGEN_KEY);
+
+        try {
+            return cfgValue(parameter.getType(), parameter.getAnnotation(InjectConfiguration.class), cgen);
+        }
+        catch (ClassNotFoundException classNotFoundException) {
+            throw new ParameterResolutionException(
+                "Cannot find a configuration schema class that matches " + parameter.getType().getCanonicalName(),
+                classNotFoundException
+            );
+        }
+    }
+
+    /**
+     * Instantiates configuration instance for injection.

Review comment:
       ```suggestion
        * Instantiates a configuration instance for injection.
   ```

##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);

Review comment:
       I'm not sure this is the correct namespace to use, as it is shared by all extensions

##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean supportsParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        return parameterContext.isAnnotated(InjectConfiguration.class)
+            && supportType(parameterContext.getParameter().getType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object resolveParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        Parameter parameter = parameterContext.getParameter();
+
+        var cgen = (ConfigurationAsmGenerator)extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(CGEN_KEY);
+
+        try {
+            return cfgValue(parameter.getType(), parameter.getAnnotation(InjectConfiguration.class), cgen);
+        }
+        catch (ClassNotFoundException classNotFoundException) {
+            throw new ParameterResolutionException(
+                "Cannot find a configuration schema class that matches " + parameter.getType().getCanonicalName(),
+                classNotFoundException
+            );
+        }
+    }
+
+    /**
+     * Instantiates configuration instance for injection.
+     *
+     * @param type Type of the field or parameter. Class name must end with {@code Configuration}.
+     * @param annotation Annotation present on the field or parameter.
+     * @param cgen Runtime code generator associated with the extension instance.
+     * @return Mock configuration instance.
+     * @throws ClassNotFoundException If corresponding configuration schema class is not found.
+     * @see #supportType(Class)
+     */
+    private static Object cfgValue(
+        Class<?> type,
+        InjectConfiguration annotation,
+        ConfigurationAsmGenerator cgen
+    ) throws ClassNotFoundException {
+        // Trying to find a schema class using configuration naming convention. This code won't work for inner Java
+        // classes, extension is designed to mock actual configurations from public API to configure Ignite components.
+        Class<?> schemaClass = Class.forName(type.getCanonicalName() + "Schema");
+
+        // Internal configuration extensions are not yet supported. This will probably be changed in the future.
+        cgen.compileRootSchema(schemaClass, Map.of());
+
+        // RootKey must be mocked, there's no way to instantiate it using a public constructor.
+        RootKey rootKey = mock(RootKey.class);
+
+        when(rootKey.key()).thenReturn("mock");
+        when(rootKey.type()).thenReturn(LOCAL);
+        when(rootKey.schemaClass()).thenReturn(schemaClass);
+        when(rootKey.internal()).thenReturn(false);
+
+        SuperRoot superRoot = new SuperRoot(s -> new RootInnerNode(rootKey, cgen.instantiateNode(schemaClass)));
+
+        ConfigObject hoconCfg = ConfigFactory.parseString(annotation.value()).root();
+
+        HoconConverter.hoconSource(hoconCfg).descend(superRoot);
+
+        ConfigurationUtil.addDefaults(superRoot);
+
+        // Reference to the super root is required to make DynamicConfigurationChanger#change method atomic.
+        var superRootRef = new AtomicReference<>(superRoot);
+
+        return cgen.instantiateCfg(rootKey, new DynamicConfigurationChanger() {
+            /** {@inheritDoc} */
+            @Override public CompletableFuture<Void> change(ConfigurationSource change) {
+                while (true) {
+                    SuperRoot sr = superRootRef.get();
+
+                    SuperRoot copy = sr.copy();
+
+                    change.descend(copy);
+
+                    if (superRootRef.compareAndSet(sr, copy))
+                        return completedFuture(null);
+                }
+            }
+
+            /** {@inheritDoc} */
+            @Override public InnerNode getRootNode(RootKey<?, ?> rk) {
+                return superRootRef.get().getRoot(rk);
+            }
+        });
+    }
+
+    /**
+     * Looks for the annotated field inside the given test class.
+     *
+     * @return Annotated fields.
+     */
+    private static List<Field> getMatchingFields(Class<?> testClass) {
+        List<Field> fields = AnnotationSupport.findAnnotatedFields(

Review comment:
       ```suggestion
           return AnnotationSupport.findAnnotatedFields(
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite-3] ibessonov commented on a change in pull request #329: IGNITE-15486 JUnit configuration extension implemented

Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #329:
URL: https://github.com/apache/ignite-3/pull/329#discussion_r705994363



##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/DynamicConfigurationChanger.java
##########
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration;
+
+import java.util.concurrent.CompletableFuture;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+
+/**
+ * Interface to provide configuration access to {@link DynamicConfiguration}, {@link NamedListConfiguration} and

Review comment:
       Added a few words




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite-3] ibessonov commented on a change in pull request #329: IGNITE-15486 JUnit configuration extension implemented

Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #329:
URL: https://github.com/apache/ignite-3/pull/329#discussion_r705980971



##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean supportsParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        return parameterContext.isAnnotated(InjectConfiguration.class)
+            && supportType(parameterContext.getParameter().getType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object resolveParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        Parameter parameter = parameterContext.getParameter();
+
+        var cgen = (ConfigurationAsmGenerator)extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(CGEN_KEY);
+
+        try {
+            return cfgValue(parameter.getType(), parameter.getAnnotation(InjectConfiguration.class), cgen);
+        }
+        catch (ClassNotFoundException classNotFoundException) {
+            throw new ParameterResolutionException(
+                "Cannot find a configuration schema class that matches " + parameter.getType().getCanonicalName(),
+                classNotFoundException
+            );
+        }
+    }
+
+    /**
+     * Instantiates configuration instance for injection.
+     *
+     * @param type Type of the field or parameter. Class name must end with {@code Configuration}.
+     * @param annotation Annotation present on the field or parameter.
+     * @param cgen Runtime code generator associated with the extension instance.
+     * @return Mock configuration instance.
+     * @throws ClassNotFoundException If corresponding configuration schema class is not found.
+     * @see #supportType(Class)
+     */
+    private static Object cfgValue(Class<?> type, InjectConfiguration annotation, ConfigurationAsmGenerator cgen)

Review comment:
       Formatted manually




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite-3] ibessonov commented on a change in pull request #329: IGNITE-15486 JUnit configuration extension implemented

Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #329:
URL: https://github.com/apache/ignite-3/pull/329#discussion_r705996486



##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,193 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean supportsParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        return parameterContext.isAnnotated(InjectConfiguration.class)
+            && supportType(parameterContext.getParameter().getType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object resolveParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        Parameter parameter = parameterContext.getParameter();
+
+        var cgen = (ConfigurationAsmGenerator)extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(CGEN_KEY);
+
+        try {
+            return cfgValue(parameter.getType(), parameter.getAnnotation(InjectConfiguration.class), cgen);
+        }
+        catch (ClassNotFoundException classNotFoundException) {
+            throw new ParameterResolutionException(
+                "Cannot find a configuration schema class that matches " + parameter.getType().getCanonicalName(),
+                classNotFoundException
+            );
+        }
+    }
+
+    /**
+     * Instantiates configuration instance for injection.

Review comment:
       done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite-3] ibessonov commented on a change in pull request #329: IGNITE-15486 JUnit configuration extension implemented

Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #329:
URL: https://github.com/apache/ignite-3/pull/329#discussion_r705994218



##########
File path: modules/configuration/src/main/java/org/apache/ignite/internal/configuration/DynamicConfigurationChanger.java
##########
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration;
+
+import java.util.concurrent.CompletableFuture;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+
+/**
+ * Interface to provide configuration access to {@link DynamicConfiguration}, {@link NamedListConfiguration} and
+ * {@link DynamicProperty}.
+ */
+public interface DynamicConfigurationChanger {
+    /**
+     * Changes the configuration.
+     *
+     * @param source Configuration source to create patch from.
+     * @return Future that is completed on change completion.
+     */
+    CompletableFuture<Void> change(ConfigurationSource source);
+
+    /**
+     * Get root node by root key. Subject to revisiting.

Review comment:
       Removed that line




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite-3] ibessonov commented on a change in pull request #329: IGNITE-15486 JUnit configuration extension implemented

Posted by GitBox <gi...@apache.org>.
ibessonov commented on a change in pull request #329:
URL: https://github.com/apache/ignite-3/pull/329#discussion_r705996830



##########
File path: modules/configuration/src/test/java/org/apache/ignite/internal/configuration/testframework/ConfigurationExtension.java
##########
@@ -0,0 +1,195 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.configuration.testframework;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Parameter;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import com.typesafe.config.ConfigFactory;
+import com.typesafe.config.ConfigObject;
+import org.apache.ignite.configuration.RootKey;
+import org.apache.ignite.internal.configuration.DynamicConfigurationChanger;
+import org.apache.ignite.internal.configuration.RootInnerNode;
+import org.apache.ignite.internal.configuration.SuperRoot;
+import org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator;
+import org.apache.ignite.internal.configuration.hocon.HoconConverter;
+import org.apache.ignite.internal.configuration.tree.ConfigurationSource;
+import org.apache.ignite.internal.configuration.tree.InnerNode;
+import org.apache.ignite.internal.configuration.util.ConfigurationUtil;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * JUnit extension to inject configuration instances into test classes.
+ *
+ * @see InjectConfiguration
+ */
+public class ConfigurationExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {
+    /** Key to store {@link ConfigurationAsmGenerator} in {@link ExtensionContext.Store}. */
+    private static final Object CGEN_KEY = new Object();
+
+    /** {@inheritDoc} */
+    @Override public void beforeEach(ExtensionContext context) throws Exception {
+        ConfigurationAsmGenerator cgen = new ConfigurationAsmGenerator();
+
+        context.getStore(ExtensionContext.Namespace.GLOBAL).put(CGEN_KEY, cgen);
+
+        Object testInstance = context.getRequiredTestInstance();
+
+        for (Field field : getMatchingFields(testInstance.getClass())) {
+            field.setAccessible(true);
+
+            field.set(testInstance, cfgValue(field.getType(), field.getAnnotation(InjectConfiguration.class), cgen));
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void afterEach(ExtensionContext context) throws Exception {
+        context.getStore(ExtensionContext.Namespace.GLOBAL).remove(CGEN_KEY);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean supportsParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        return parameterContext.isAnnotated(InjectConfiguration.class)
+            && supportType(parameterContext.getParameter().getType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object resolveParameter(
+        ParameterContext parameterContext, ExtensionContext extensionContext
+    ) throws ParameterResolutionException {
+        Parameter parameter = parameterContext.getParameter();
+
+        var cgen = (ConfigurationAsmGenerator)extensionContext.getStore(ExtensionContext.Namespace.GLOBAL).get(CGEN_KEY);
+
+        try {
+            return cfgValue(parameter.getType(), parameter.getAnnotation(InjectConfiguration.class), cgen);
+        }
+        catch (ClassNotFoundException classNotFoundException) {
+            throw new ParameterResolutionException(
+                "Cannot find a configuration schema class that matches " + parameter.getType().getCanonicalName(),
+                classNotFoundException
+            );
+        }
+    }
+
+    /**
+     * Instantiates configuration instance for injection.
+     *
+     * @param type Type of the field or parameter. Class name must end with {@code Configuration}.
+     * @param annotation Annotation present on the field or parameter.
+     * @param cgen Runtime code generator associated with the extension instance.
+     * @return Mock configuration instance.
+     * @throws ClassNotFoundException If corresponding configuration schema class is not found.
+     * @see #supportType(Class)
+     */
+    private static Object cfgValue(
+        Class<?> type,
+        InjectConfiguration annotation,
+        ConfigurationAsmGenerator cgen
+    ) throws ClassNotFoundException {
+        // Trying to find a schema class using configuration naming convention. This code won't work for inner Java
+        // classes, extension is designed to mock actual configurations from public API to configure Ignite components.
+        Class<?> schemaClass = Class.forName(type.getCanonicalName() + "Schema");
+
+        // Internal configuration extensions are not yet supported. This will probably be changed in the future.
+        cgen.compileRootSchema(schemaClass, Map.of());
+
+        // RootKey must be mocked, there's no way to instantiate it using a public constructor.
+        RootKey rootKey = mock(RootKey.class);
+
+        when(rootKey.key()).thenReturn("mock");
+        when(rootKey.type()).thenReturn(LOCAL);
+        when(rootKey.schemaClass()).thenReturn(schemaClass);
+        when(rootKey.internal()).thenReturn(false);
+
+        SuperRoot superRoot = new SuperRoot(s -> new RootInnerNode(rootKey, cgen.instantiateNode(schemaClass)));
+
+        ConfigObject hoconCfg = ConfigFactory.parseString(annotation.value()).root();
+
+        HoconConverter.hoconSource(hoconCfg).descend(superRoot);
+
+        ConfigurationUtil.addDefaults(superRoot);
+
+        // Reference to the super root is required to make DynamicConfigurationChanger#change method atomic.
+        var superRootRef = new AtomicReference<>(superRoot);
+
+        return cgen.instantiateCfg(rootKey, new DynamicConfigurationChanger() {
+            /** {@inheritDoc} */
+            @Override public CompletableFuture<Void> change(ConfigurationSource change) {
+                while (true) {
+                    SuperRoot sr = superRootRef.get();
+
+                    SuperRoot copy = sr.copy();
+
+                    change.descend(copy);
+
+                    if (superRootRef.compareAndSet(sr, copy))
+                        return completedFuture(null);
+                }
+            }
+
+            /** {@inheritDoc} */
+            @Override public InnerNode getRootNode(RootKey<?, ?> rk) {
+                return superRootRef.get().getRoot(rk);
+            }
+        });
+    }
+
+    /**
+     * Looks for the annotated field inside the given test class.
+     *
+     * @return Annotated fields.
+     */
+    private static List<Field> getMatchingFields(Class<?> testClass) {
+        List<Field> fields = AnnotationSupport.findAnnotatedFields(

Review comment:
       sure




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org