SystemPropertyCsmConfigurationProvider implements CsmConfigurationProvider { @Override public CsmConfiguration getConfiguration() throws SdkClientException { String enabled = System.getProperty(AWS_CSM_ENABLED_SYSTEM_PROPERTY); if (enabled == null) { throw new SdkClientException("Unable to load Client Side Monitoring configurations from" + " system properties variables!"); } String host = System.getProperty(AWS_CSM_HOST_SYSTEM_PROPERTY, DEFAULT_AWS_CSM_HOST); String port = System.getProperty(AWS_CSM_PORT_SYSTEM_PROPERTY); String clientId = System.getProperty(AWS_CSM_CLIENT_ID_SYSTEM_PROPERTY, ""); 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" + " system properties variables!", e); } }  @Override CsmConfiguration getConfiguration();  }
@Test public void testCorrectlyResolvesConfiguration() { System.setProperty(AWS_CSM_ENABLED_SYSTEM_PROPERTY, "true"); System.setProperty(AWS_CSM_HOST_SYSTEM_PROPERTY, "bar"); System.setProperty(AWS_CSM_PORT_SYSTEM_PROPERTY, "1234"); System.setProperty(AWS_CSM_CLIENT_ID_SYSTEM_PROPERTY, "foo"); CsmConfiguration cfg = provider.getConfiguration(); CsmConfiguration expected = CsmConfiguration.builder() .withEnabled(true) .withHost("bar") .withPort(1234) .withClientId("foo") .build(); assertEquals(expected, cfg); }
@Test(expected = SdkClientException.class) public void testThrowsSdkClientExceptionWhenVariablesNotPresent() { provider.getConfiguration(); }
@Test(expected = SdkClientException.class) public void testThrowsSdkClientExceptionWhenPortCannotBeParsed() { System.setProperty(AWS_CSM_ENABLED_SYSTEM_PROPERTY, "true"); System.setProperty(AWS_CSM_PORT_SYSTEM_PROPERTY, "onetwothreefour"); System.setProperty(AWS_CSM_CLIENT_ID_SYSTEM_PROPERTY, "foo"); provider.getConfiguration(); }
@Test public void optionalValuesNotSet_shouldUseDefaultValues() { System.setProperty(AWS_CSM_ENABLED_SYSTEM_PROPERTY, "true"); CsmConfiguration configuration = provider.getConfiguration(); assertEquals(DEFAULT_AWS_CSM_HOST, configuration.getHost()); assertEquals(DEFAULT_AWS_CSM_PORT, configuration.getPort()); assertEquals(DEFAULT_AWS_CSM_CLIENT_ID, configuration.getClientId()); }