Skip to content

the bean

Ofer Skolsky edited this page Oct 5, 2020 · 1 revision

The bean that will be initialised with the configuration

CxConfigProvider is used to load a configuration from a YAML or JSON into a bean.

if we have the following configuration:

github:
  token: githubTokenFromApp
  configAsCode: cx.configuration
  
ast:
  apiUrl: \"http://this.is.just.a.test\"
  token: astToken
  preset: ${JAVA_HOME}
  incremental: false
  
path: appPath

and we would like to initialize the following bean:

@Getter
@Setter
public class AstConfigurationLoaderTestClass {

    String apiUrl;
    String token;
    String preset;
    @Optional
    boolean myParam;
    boolean incremental;
}

then after calling getConfiguration:

astConfigurationLoaderTestClass = configProvider.getConfiguration(APP_NAME, "ast", AstConfigurationLoaderTestClass.class);

the following will pass:

log.info("validating a normal String");
assertEquals("AST token value from configuration is not as expected", "astToken", astConfigurationLoaderTestClass.getToken());
log.info("validating a String with special characters");
assertEquals("AST API URI value from configuration is not as expected", "http://this.is.just.a.test", astConfigurationLoaderTestClass.getApiUrl());
log.info("validating a resolved value");
assertEquals("AST preset value from configuration is not as expected", System.getenv("JAVA_HOME"), astConfigurationLoaderTestClass.getPreset());
log.info("validating a boolean value");
assertFalse("AST incremental value from configuration is not as expected", astConfigurationLoaderTestClass.isIncremental());
log.info("validating missing Optional parameter");
assertFalse("AST myParam Optional parameter was true", astConfigurationLoaderTestClass.isMyParam());

important info

for every field in the bean that is not marked as Optional there must be a value in the configuration.
the configuration uses the empty constructor and then uses the setters of each field to set its value.
values are automaticaly converted to the appropriate type. if a value is escaped using ", before and after the value, then it will not be resolved the values (not escaped) that have the form ${xxx} will be resolved (from environment variables or within the configuration).
extra values in the configuration will be ignored.

breakdown of the values

all the values will be taken from the "ast" section.

in the bean in the config resault why
String apiUrl "http://this.is.just.a.test\" "http://this.is.just.a.test" escaped string can contain special chars
String token astToken "astToken" a simple String
String preset ${JAVA_HOME} System.getenv("JAVA_HOME") a string with a system variable
@Optional boolean myParam false missing Optional parameter, the default of boolean
boolean incremental false false boolean value from the config

for more examples, look at the feature files

Clone this wiki locally