Class Configuration


  • public final class Configuration
    extends Object

    HOCON Configuration tools to support unit testing

    HOCON configurations can be loaded and activated at any time :

          Map<String, String> substitutions = new HashMap<>();
          
          // example load / activate on this node
          //
          config = new Configuration("test1.conf").load().activate();
     

    and subsequently deactivated and removed :

          // example deactive / remove on this node
          //
          config.deactivate().remove();
     

    Substitution parameters can be specified :

          // example load / activate on this node
          //
          substitutions.put("param", "value");
          config = new Configuration("test1.conf", substitutions).load().activate();
     

    By default, the configuration is loaded on the local node only - to load on a different node or on the cluster, the serviceName can be specified :

          // example load / activate on a specific node or cluster
          //
          config = new Configuration("A.test-framework", "test2.conf", substitutions).load().activate();
          config.deactivate().remove();
          config = new Configuration("test-framework", "test3.conf", substitutions).load().activate();
          config.deactivate().remove();
     

    Test can call the deactiveRemoveAll function to automatically deactivate and remove configurations that are still loaded.

          // Deactivate configurations previous loaded
          //
          Configuration.deactiveAndRemoveAll();
     

    Note that there is no support for auto loading configurations - any configuration that is required for a test case must load and activate it using this API. This includes configurations used be any dependent components.

    • Constructor Detail

      • Configuration

        public Configuration​(String serviceName,
                             String resource,
                             Map<String,​String> substitutions)

        Create a configuration instance

        Parameters:
        serviceName - service name, can not be null
        resource - HOCON configuration resource to load, can not be null
        substitutions - Substitution variables, can not be null
      • Configuration

        public Configuration​(String serviceName,
                             String resource,
                             Map<String,​String> substitutions,
                             String substitutionResource)

        Create a configuration instance

        Parameters:
        serviceName - service name, can not be null
        resource - HOCON configuration resource to load, can not be null
        substitutions - Substitution variables, can not be null
        substitutionResource - Substitutions resource file, can not be null
      • Configuration

        public Configuration​(String resource,
                             Map<String,​String> substitutions)

        Create a configuration instance

        Parameters:
        resource - HOCON configuration resource to load, can not be null
        substitutions - Substitution variables, can not be null
      • Configuration

        public Configuration​(String resource)

        Create a configuration instance

        Parameters:
        resource - HOCON configuration resource to load, can not be null
    • Method Detail

      • forFile

        public static Configuration forFile​(String serviceName,
                                            String resource,
                                            Map<String,​String> substitutions)

        Create a configuration instance

        Parameters:
        serviceName - service name, can not be null
        resource - HOCON configuration resource to load, can not be null
        substitutions - Substitution variables, can not be null
        Returns:
        instance of Configuration
      • forFile

        public static Configuration forFile​(String serviceName,
                                            String resource,
                                            Map<String,​String> substitutions,
                                            String substitutionResource)

        Create a configuration instance

        Parameters:
        serviceName - service name, can not be null
        resource - HOCON configuration resource to load, can not be null
        substitutions - Substitution variables, can not be null
        substitutionResource - Substitutions resource file, can not be null
        Returns:
        instance of Configuration
      • forFile

        public static Configuration forFile​(String resource,
                                            Map<String,​String> substitutions)

        Create a configuration instance

        Parameters:
        resource - HOCON configuration resource to load, can not be null
        substitutions - Substitution variables, can not be null
        Returns:
        instance of Configuration
      • forFile

        public static Configuration forFile​(String resource)

        Create a configuration instance

        Parameters:
        resource - HOCON configuration resource to load, can not be null
        Returns:
        instance of Configuration
      • activate

        public Configuration activate()
                               throws ConfigurationException

        Activate this configuration

        Fails if configuration is not loaded

        This method can be run within or outside a transaction

        Returns:
        Config instance
        Throws:
        ConfigurationException - on configuration failure or already active
      • deactivate

        public Configuration deactivate()
                                 throws ConfigurationException

        Deactivate this configuration

        Fails if configuration is not active

        This method can be run within or outside a transaction

        Returns:
        Config instance
        Throws:
        ConfigurationException - on configuration failure or not active
      • remove

        public void remove()
                    throws ConfigurationException

        Remove this configuration

        Fails if configuration is still active

        This method can be run within or outside a transaction

        Throws:
        ConfigurationException - on configuration failure or still active
      • getLoadedConfigurations

        public static List<Configuration> getLoadedConfigurations()
        Get all loaded configurations
        Returns:
        list of loaded configurations
      • isActive

        public boolean isActive()

        Check if this configuration is activated

        This method can be run within or outside a transaction

        Returns:
        Return true if configuration is activated, false otherwise
      • isLoaded

        public boolean isLoaded()

        Check if this configuration is loaded

        This method can be run within or outside a transaction

        Returns:
        Return true if configuration is loaded, false otherwise
      • getName

        public Optional<String> getName()
        Get the currently loaded configuration name
        Returns:
        name
      • getVersion

        public Optional<String> getVersion()
        Get the currently loaded configuration version
        Returns:
        version
      • getType

        public Optional<String> getType()
        Get the currently loaded configuration type
        Returns:
        type
      • deactiveAndRemoveAll

        public static void deactiveAndRemoveAll()

        Deactivate and remove configurations in reverse order

        This method can be run within or outside a transaction

        Errors are ignored