<commit_before>package org.jboss.as.subsystem.test;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.ProcessType;
import org.jboss.as.controller.RunningMode;
import org.jboss.as.controller.capability.RuntimeCapability;
import org.jboss.as.controller.capability.registry.CapabilityScope;
import org.jboss.as.controller.capability.registry.RegistrationPoint;
import org.jboss.as.controller.capability.registry.RuntimeCapabilityRegistration;
import org.jboss.as.controller.capability.registry.RuntimeCapabilityRegistry;
import org.jboss.as.controller.extension.ExtensionRegistry;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.as.controller.registry.Resource;
import org.jboss.as.model.test.ModelTestModelDescriptionValidator.AttributeOrParameterArbitraryDescriptorValidator;
import org.jboss.as.subsystem.test.ModelDescriptionValidator.ValidationConfiguration;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;
import org.jboss.msc.service.ServiceTarget;

/**
 * Allows you to additionally initialize the service container and the model controller
 * beyond the subsystem being tested. Override this class to add behaviour.
 *
 * @author <a href="<EMAIL>"><NAME></a>
 */
public class AdditionalInitialization extends AdditionalParsers {
    public static final AdditionalInitialization MANAGEMENT = new ManagementAdditionalInitialization();

    public static final AdditionalInitialization ADMIN_ONLY_HC = new ManagementAdditionalInitialization() {
        @Override
        protected ProcessType getProcessType() {
            return ProcessType.HOST_CONTROLLER;
        }
    };

    public static class HostControllerAdditionalInitialization extends AdditionalInitialization implements Serializable {
        private static final long serialVersionUID = -509444465514822866L;

        @Override
        protected ProcessType getProcessType() {
            return ProcessType.HOST_CONTROLLER;
        }

        @Override
        protected RunningMode getRunningMode() {
            return RunningMode.ADMIN_ONLY;
        }

        @Override
        protected boolean isValidateOperations() {
            return true;
        }
    }

    /**
     * An {@code AdditionalInitialization} whose {@link #getRunningMode() running mode} is
     * {@link org.jboss.as.controller.RunningMode#ADMIN_ONLY}
     */
    public static class ManagementAdditionalInitialization extends AdditionalInitialization implements Serializable {
        private static final long serialVersionUID = -509444465514822866L;

        @Override
        protected RunningMode getRunningMode() {
            return RunningMode.ADMIN_ONLY;
        }
    }

    /**
     * Creates a {@link org.jboss.as.subsystem.test.AdditionalInitialization.ManagementAdditionalInitialization} with
     * the given {@link org.jboss.as.controller.capability.RuntimeCapability capabilities} registered, making it
     * possible for subsystems under test to require them. No runtime API will be available, but that should not
     * be needed for a {@link org.jboss.as.controller.RunningMode#ADMIN_ONLY} test.
     *
     * @param capabilities the capabilities
     * @return the additional initialization
     */
    public static AdditionalInitialization withCapabilities(final String... capabilities) {
        return new ManagementAdditionalInitialization() {

            @Override
            protected void initializeExtraSubystemsAndModel(ExtensionRegistry extensionRegistry, Resource rootResource, ManagementResourceRegistration rootRegistration, RuntimeCapabilityRegistry capabilityRegistry) {
                super.initializeExtraSubystemsAndModel(extensionRegistry, rootResource, rootRegistration, capabilityRegistry);
                registerCapabilities(capabilityRegistry, capabilities);
            }
        };
    }

    /**
     * Creates a {@link org.jboss.as.subsystem.test.AdditionalInitialization} with
     * the given {@link org.jboss.as.controller.capability.RuntimeCapability capabilities} registered, making it
     * possible for subsystems under test to require them. Any runtime API provided in the {@code capabilities}
     * parameter will be available.
     *
     * @param capabilities the capabilities; key is the name of the capability, value is its runtime API
     * @return the additional initialization
     */
    public static AdditionalInitialization withCapabilities(final Map<String, Object> capabilities) {
        return new AdditionalInitialization() {

            @Override
            protected void initializeExtraSubystemsAndModel(ExtensionRegistry extensionRegistry, Resource rootResource, ManagementResourceRegistration rootRegistration, RuntimeCapabilityRegistry capabilityRegistry) {
                super.initializeExtraSubystemsAndModel(extensionRegistry, rootResource, rootRegistration, capabilityRegistry);
                registerCapabilities(capabilityRegistry, capabilities);
            }
        };
    }

    public static final AttributeOrParameterArbitraryDescriptorValidator ARBITRARY_DESCRIPTOR_VALIDATOR = (ModelType currentType, ModelNode currentNode, String descriptor) -> null;

    /**
     * Creates a {@link org.jboss.as.subsystem.test.AdditionalInitialization} with arbitrary descriptors, making it
     * possible for subsystems under test to require them.
     *
     * @param address
     * @param operations list of operations the arbitrary descriptors will be used.
     * @param arbitraryDescriptors the arbitrary descriptors; key is the name of the descriptor, value is its value.
     * @return the additional initialization
     */
    public static AdditionalInitialization withArbitraryDescriptors(final ModelNode address, final List<String> operations, final Map<String, String> arbitraryDescriptors) {
        return new AdditionalInitialization() {
            @Override
            protected ValidationConfiguration getModelValidationConfiguration() {
                ValidationConfiguration config = super.getModelValidationConfiguration();
                arbitraryDescriptors.entrySet().stream().map((arbitraryDescriptor) -> {
                    config.registerAttributeArbitraryDescriptor(address, arbitraryDescriptor.getKey(), arbitraryDescriptor.getValue(), ARBITRARY_DESCRIPTOR_VALIDATOR);
                    return arbitraryDescriptor;
                }).forEach((arbitraryDescriptor) -> {
                    operations.stream().forEach((operation) -> {
                        config.registerArbitraryDescriptorForOperation(address, operation, arbitraryDescriptor.getValue(), ARBITRARY_DESCRIPTOR_VALIDATOR);
                    });
                });
                return config;
            }
        };
    }

    /**
     * Simple utility method to register a
     * {@link org.jboss.as.controller.capability.RuntimeCapability RuntimeCapability<Void>} for each of the given
     * capability names. They will be registered against
     * {@link CapabilityScope#GLOBAL} and with the root resource and no
     * specific attribute as their {@link org.jboss.as.controller.capability.registry.RegistrationPoint}.
     *
     * @param capabilityRegistry registry to use
     * @param capabilities names of the capabilities.
     */
    public static void registerCapabilities(RuntimeCapabilityRegistry capabilityRegistry, String... capabilities) {
        for (final String capabilityName : capabilities) {
            RuntimeCapability<Void> capability = RuntimeCapability.Builder.of(capabilityName).build();
            capabilityRegistry.registerCapability(new RuntimeCapabilityRegistration(capability, CapabilityScope.GLOBAL,
                    new RegistrationPoint(PathAddress.EMPTY_ADDRESS, null)));

        }
    }

    /**
     * Simple utility method to register a<commit_msg>[WFCORE-3455] Reduce lambda usage (subsystem-test)<commit_after>package org.jboss.as.subsystem.test;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.ProcessType;
import org.jboss.as.controller.RunningMode;
import org.jboss.as.controller.capability.RuntimeCapability;
import org.jboss.as.controller.capability.registry.CapabilityScope;
import org.jboss.as.controller.capability.registry.RegistrationPoint;
import org.jboss.as.controller.capability.registry.RuntimeCapabilityRegistration;
import org.jboss.as.controller.capability.registry.RuntimeCapabilityRegistry;
import org.jboss.as.controller.extension.ExtensionRegistry;
import org.jboss.as.controller.registry.ManagementResourceRegistration;
import org.jboss.as.controller.registry.Resource;
import org.jboss.as.model.test.ModelTestModelDescriptionValidator.AttributeOrParameterArbitraryDescriptorValidator;
import org.jboss.as.subsystem.test.ModelDescriptionValidator.ValidationConfiguration;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;
import org.jboss.msc.service.ServiceTarget;

/**
 * Allows you to additionally initialize the service container and the model controller
 * beyond the subsystem being tested. Override this class to add behaviour.
 *
 * @author <a href="<EMAIL>"><NAME></a>
 */
public class AdditionalInitialization extends AdditionalParsers {
    public static final AdditionalInitialization MANAGEMENT = new ManagementAdditionalInitialization();

    public static final AdditionalInitialization ADMIN_ONLY_HC = new ManagementAdditionalInitialization() {
        @Override
        protected ProcessType getProcessType() {
            return ProcessType.HOST_CONTROLLER;
        }
    };

    public static class HostControllerAdditionalInitialization extends AdditionalInitialization implements Serializable {
        private static final long serialVersionUID = -509444465514822866L;

        @Override
        protected ProcessType getProcessType() {
            return ProcessType.HOST_CONTROLLER;
        }

        @Override
        protected RunningMode getRunningMode() {
            return RunningMode.ADMIN_ONLY;
        }

        @Override
        protected boolean isValidateOperations() {
            return true;
        }
    }

    /**
     * An {@code AdditionalInitialization} whose {@link #getRunningMode() running mode} is
     * {@link org.jboss.as.controller.RunningMode#ADMIN_ONLY}
     */
    public static class ManagementAdditionalInitialization extends AdditionalInitialization implements Serializable {
        private static final long serialVersionUID = -509444465514822866L;

        @Override
        protected RunningMode getRunningMode() {
            return RunningMode.ADMIN_ONLY;
        }
    }

    /**
     * Creates a {@link org.jboss.as.subsystem.test.AdditionalInitialization.ManagementAdditionalInitialization} with
     * the given {@link org.jboss.as.controller.capability.RuntimeCapability capabilities} registered, making it
     * possible for subsystems under test to require them. No runtime API will be available, but that should not
     * be needed for a {@link org.jboss.as.controller.RunningMode#ADMIN_ONLY} test.
     *
     * @param capabilities the capabilities
     * @return the additional initialization
     */
    public static AdditionalInitialization withCapabilities(final String... capabilities) {
        return new ManagementAdditionalInitialization() {

            @Override
            protected void initializeExtraSubystemsAndModel(ExtensionRegistry extensionRegistry, Resource rootResource, ManagementResourceRegistration rootRegistration, RuntimeCapabilityRegistry capabilityRegistry) {
                super.initializeExtraSubystemsAndModel(extensionRegistry, rootResource, rootRegistration, capabilityRegistry);
                registerCapabilities(capabilityRegistry, capabilities);
            }
        };
    }

    /**
     * Creates a {@link org.jboss.as.subsystem.test.AdditionalInitialization} with
     * the given {@link org.jboss.as.controller.capability.RuntimeCapability capabilities} registered, making it
     * possible for subsystems under test to require them. Any runtime API provided in the {@code capabilities}
     * parameter will be available.
     *
     * @param capabilities the capabilities; key is the name of the capability, value is its runtime API
     * @return the additional initialization
     */
    public static AdditionalInitialization withCapabilities(final Map<String, Object> capabilities) {
        return new AdditionalInitialization() {

            @Override
            protected void initializeExtraSubystemsAndModel(ExtensionRegistry extensionRegistry, Resource rootResource, ManagementResourceRegistration rootRegistration, RuntimeCapabilityRegistry capabilityRegistry) {
                super.initializeExtraSubystemsAndModel(extensionRegistry, rootResource, rootRegistration, capabilityRegistry);
                registerCapabilities(capabilityRegistry, capabilities);
            }
        };
    }

    public static final AttributeOrParameterArbitraryDescriptorValidator ARBITRARY_DESCRIPTOR_VALIDATOR = (ModelType currentType, ModelNode currentNode, String descriptor) -> null;

    /**
     * Creates a {@link org.jboss.as.subsystem.test.AdditionalInitialization} with arbitrary descriptors, making it
     * possible for subsystems under test to require them.
     *
     * @param address
     * @param operations list of operations the arbitrary descriptors will be used.
     * @param arbitraryDescriptors the arbitrary descriptors; key is the name of the descriptor, value is its value.
     * @return the additional initialization
     */
    public static AdditionalInitialization withArbitraryDescriptors(final ModelNode address, final List<String> operations, final Map<String, String> arbitraryDescriptors) {
        return new AdditionalInitialization() {
            @Override
            protected ValidationConfiguration getModelValidationConfiguration() {
                ValidationConfiguration config = super.getModelValidationConfiguration();
                arbitraryDescriptors.entrySet().stream().map((arbitraryDescriptor) -> {
                    config.registerAttributeArbitraryDescriptor(address, arbitraryDescriptor.getKey(), arbitraryDescriptor.getValue(), ARBITRARY_DESCRIPTOR_VALIDATOR);
                    return arbitraryDescriptor;
                }).forEach((arbitraryDescriptor) -> {
                    for (String operation : operations) {
                        config.registerArbitraryDescriptorForOperation(address, operation, arbitraryDescriptor.getValue(), ARBITRARY_DESCRIPTOR_VALIDATOR);
                    }
                });
                return config;
            }
        };
    }

    /**
     * Simple utility method to register a
     * {@link org.jboss.as.controller.capability.RuntimeCapability RuntimeCapability<Void>} for each of the given
     * capability names. They will be registered against
     * {@link CapabilityScope#GLOBAL} and with the root resource and no
     * specific attribute as their {@link org.jboss.as.controller.capability.registry.RegistrationPoint}.
     *
     * @param capabilityRegistry registry to use
     * @param capabilities names of the capabilities.
     */
    public static void registerCapabilities(RuntimeCapabilityRegistry capabilityRegistry, String... capabilities) {
        for (final String capabilityName : capabilities) {
            RuntimeCapability<Void> capability = RuntimeCapability.Builder.of(capabilityName).build();
            capabilityRegistry.registerCapability(new RuntimeCapabilityRegistration(capability, CapabilityScope.GLOBAL,
                    new RegistrationPoint(PathAddress.EMPTY_ADDRESS, null)));

        }
    }

    /**
     * Simple utility method to register a
{'fraction_non_alphanumeric': 0.07132037364301944, 'fraction_numerical': 0.004796768492804847, 'mean_word_length': 3.5544696751940212, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6102021', 'n_tokens_mistral': 3789, 'n_tokens_neox': 3603, 'n_words': 966}