EnvironmentVariableCsmConfigurationProvider implements CsmConfigurationProvider { @Override public CsmConfiguration getConfiguration() { String enabled = System.getenv(AWS_CSM_ENABLED_ENV_VAR); if (enabled == null) { throw new SdkClientException("Unable to load Client Side Monitoring configurations from" + " environment variables!"); } String host = System.getenv(AWS_CSM_HOST_ENV_VAR); host = host == null ? DEFAULT_AWS_CSM_HOST : host; String port = System.getenv(AWS_CSM_PORT_ENV_VAR); String clientId = System.getenv(AWS_CSM_CLIENT_ID_ENV_VAR); clientId = clientId == null ? DEFAULT_AWS_CSM_CLIENT_ID : clientId; try { int portNumber = port == null ? DEFAULT_AWS_CSM_PORT : Integer.parseInt(port); return CsmConfiguration.builder() .withEnabled(Boolean.parseBoolean(enabled)) .withHost(host) .withPort(portNumber) .withClientId(clientId) .build(); } catch (Exception e) { throw new SdkClientException("Unable to load Client Side Monitoring configurations from" + " environment variables!", e); } }  @Override CsmConfiguration getConfiguration();  }
@Test public void testCorrectlyResolvesConfiguration() { environmentVariableHelper.set(AWS_CSM_ENABLED_ENV_VAR, "true"); environmentVariableHelper.set(AWS_CSM_HOST_ENV_VAR, "bar"); environmentVariableHelper.set(AWS_CSM_PORT_ENV_VAR, "1234"); environmentVariableHelper.set(AWS_CSM_CLIENT_ID_ENV_VAR, "foo"); CsmConfiguration cfg = provider.getConfiguration(); assertEquals(CsmConfiguration.builder().withEnabled(true).withHost("bar").withPort(1234).withClientId("foo").build(), cfg); }
@Test(expected = SdkClientException.class) public void testThrowsSdkClientExceptionWhenVariablesNotPresent() { provider.getConfiguration(); }
@Test(expected = SdkClientException.class) public void testThrowsSdkClientExceptionWhenPortCannotBeParsed() { environmentVariableHelper.set(AWS_CSM_ENABLED_ENV_VAR, "true"); environmentVariableHelper.set(AWS_CSM_PORT_ENV_VAR, "onetwothreefour"); environmentVariableHelper.set(AWS_CSM_CLIENT_ID_ENV_VAR, "foo"); provider.getConfiguration(); }
@Test public void optionalValuesNotSet_shouldUseDefaultValues() { environmentVariableHelper.reset(); environmentVariableHelper.set(AWS_CSM_ENABLED_ENV_VAR, "true"); CsmConfiguration configuration = provider.getConfiguration(); assertEquals(DEFAULT_AWS_CSM_PORT, configuration.getPort()); assertEquals(DEFAULT_AWS_CSM_HOST, configuration.getHost()); assertEquals(DEFAULT_AWS_CSM_CLIENT_ID, configuration.getClientId()); }