Skip to content

Standard Format for Storing and using Configuration Information

Nick Benik edited this page Aug 13, 2019 · 1 revision

In the situation that one or more tests require specific static configuration information, that configuration information is to be managed following the herein described standards.

Definition of configuration information in the root pic-sure-api POM

All configuration information that may be needed by unit tests, integration tests, etc should be defined as properties in the root POM file as shown HERE.

Test Code: Using a testing.properties file to pull values from POM properties

The following code is used in the POM file to define a testing.properties file that should be used to pass any and all configuration information to integration tests.

  <phase>generate-sources</phase>
  <goals>
    <goal>copy-resources</goal>
  </goals>
  <configuration>
    <outputDirectory>${project.build.directory}/test-classes</outputDirectory>
    <resources>
      <resource>
        <directory>src/main/resources/testingConfiguration</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
  </configuration>

Within a src\main\resources\testingConfiguration folder will be a file called testing.properties. This file will be copied to target\test-classes\testing.properties and will have values injected from the POM file. The format of the testing.properties file is as follows.

picsure_url = ${picsure.cfg.endpoint_url}
picsure_client_secret = ${picsure.cfg.client_secret}
picsure_user_id_claim = ${picsure.cfg.user_id_claim}
picsure_roles_claim = ${picsure.cfg.roles_claim}
picsure_verify_user_method = ${picsure.cfg.verify_user_method}
picsure_token_introspection_url = ${picsure.cfg.token_introspection_url}
picsure_token_introspection_token = ${picsure.cfg.token_introspection_token}

An example code snipped that shows how an integration test would use the values in the testing.property file is:

public class SomeTestIT {
  protected static String CFG_PICSURE_URL;
  protected static String CFG_PICSURE_CLIENT_SECRET;

  @BeforeClass
  public static void beforeClass() {
    InputStream testConfiguration = SomeTestIT.class.getClassLoader().getResourceAsStream("testing.properties");
    Properties testProperties = new Properties();
    try {
      testProperties.load(testConfiguration);
      CFG_PICSURE_URL           = testProperties.getProperties("picsure_url");
      CFG_PICSURE_CLIENT_SECRET = testProperties.getProperties("picsure_client_secret");
    } catch (Exception e) {
      // TODO: Handle error(s)
    }
  }
}

Code Under Test: Using JNDI as source of configuration information

Configuration variables are exposed via JNDI through configuration of Wildfly's standalone.xml file. An example of how to do this in standalone.xml is as follows:

<subsystem xmlns="urn:jboss:domain:naming:2.0">
  <bindings>
    <!-- THESE ARE USED BY THE PIC-SURE API WAR -->
    <simple name="java:global/picsure_url" value="${picsure.cfg.endpoint_url}"/>
    <simple name="java:global/picsure_client_secret" value="${picsure.cfg.client_secret}"/>
    <simple name="java:global/picsure_user_id_claim" value="${picsure.cfg.user_id_claim}"/>
    <simple name="java:global/picsure_roles_claim" value="${picsure.cfg.roles_claim}"/>
    <simple name="java:global/picsure_verify_user_method" value="${picsure.cfg.verify_user_method}"/>
    <simple name="java:global/picsure_token_introspection_url" value="${picsure.cfg.token_introspection_url}"/>
    <simple name="java:global/picsure_token_introspection_token" value="${picsure.cfg.token_introspection_token}"/>
    <!-- THE BELOW SETTINGS ARE USED *ONLY* BY INTEGRATION TESTING!!! -->
    <simple name="java:global/mockito_base_url" value="${mockito.cfg.base_url}"/>
    <simple name="java:global/irct_token" value="${irct.cfg.token}"/>
    <simple name="java:global/irct_endpoint_url" value="${irct.cfg.target_url}"/>
    <simple name="java:global/aggregate_rs_url" value="${aggregate.cfg.rs_url}"/>
  </bindings>
  <remote-naming/>
</subsystem>

Here is some example code for reading configuration variables using JNDI:

public class ProductionCode implements IResourceRS {
  protected static String CFG_PICSURE_URL;
  protected static String CFG_PICSURE_CLIENT_SECRET;

  @PostConstruct
  public void init() {
    try {
      InitialContext ctx = new InitialContext();
      CFG_PICSURE_URL           = (String) ctx.lookup("picsure_url");
      CFG_PICSURE_CLIENT_SECRET = (String) ctx.lookup("picsure_client_secret");
    } catch (NamingException e) {
      // TODO: Handle error(s)
    }
  }
}