ProfileCsmConfigurationProvider implements CsmConfigurationProvider { @Override public CsmConfiguration getConfiguration() { String profileName = getProfileName(); BasicProfile profile = getProfile(profileName); if (profile == null) { throw new SdkClientException(String.format("Could not find the '%s' profile!", profileName)); } String enabled = profile.getPropertyValue(CSM_ENABLED_PROPERTY); if (enabled == null) { throw new SdkClientException(String.format("The '%s' profile does not define all the" + " required properties!", profileName)); } String host = profile.getPropertyValue(CSM_HOST_PROPERTY); host = host == null ? DEFAULT_AWS_CSM_HOST : host; String port = profile.getPropertyValue(CSM_PORT_PROPERTY); String clientId = profile.getPropertyValue(CSM_CLIENT_ID_PROPERTY); 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(String.format("Unable to load configuration from the '%s'" + " profile!", profileName), e); } } ProfileCsmConfigurationProvider();  ProfileCsmConfigurationProvider(String profileName);  ProfileCsmConfigurationProvider(String profileName,
            AwsProfileFileLocationProvider configFileLocationProvider); @Override CsmConfiguration getConfiguration(); static final String CSM_ENABLED_PROPERTY; static final String CSM_HOST_PROPERTY; static final String CSM_PORT_PROPERTY; static final String CSM_CLIENT_ID_PROPERTY; }
@Test public void testCorrectlyResolvesConfiguration() { ProfileCsmConfigurationProvider provider = new ProfileCsmConfigurationProvider(TEST_PROFILE_NAME, makeConfigLocationProvider(COMPLETE_CONFIG)); CsmConfiguration expected = CsmConfiguration.builder() .withEnabled(true) .withHost("bar") .withPort(1234) .withClientId("foo") .build(); assertEquals(expected, provider.getConfiguration()); }
@Test(expected = SdkClientException.class) public void testThrowsSdkClientExceptionWhenPropertiesNotSet() { new ProfileCsmConfigurationProvider(TEST_PROFILE_NAME, makeConfigLocationProvider(NO_PROPERTIES_CONFIG)) .getConfiguration(); }
@Test(expected = SdkClientException.class) public void testThrowsSdkClientExceptionWhenCsmPropertiesNotPresent() { new ProfileCsmConfigurationProvider(TEST_PROFILE_NAME, makeConfigLocationProvider(EMPTY_CONFIG_FILE)) .getConfiguration(); }
@Test(expected = SdkClientException.class) public void testThrowsSdkClientExceptionWhenPortCannotBeParsed() { new ProfileCsmConfigurationProvider(TEST_PROFILE_NAME, makeConfigLocationProvider(UNPARSEABLE_PORT_CONFIG)) .getConfiguration(); }
@Test(expected = SdkClientException.class) public void testThrowsSdkClientExceptionWhenConfigNotFound() { new ProfileCsmConfigurationProvider(TEST_PROFILE_NAME, makeConfigLocationProvider("DoesNotExist")) .getConfiguration(); }
@Test(expected = SdkClientException.class) public void testThrowsSdkClientExceptionWhenProfileNotFound() { ProfileCsmConfigurationProvider provider = new ProfileCsmConfigurationProvider("my_other_profile", makeConfigLocationProvider(COMPLETE_CONFIG)); provider.getConfiguration(); }
@Test public void testSourcesConfigurationFromCorrectProfile() { ProfileCsmConfigurationProvider provider = new ProfileCsmConfigurationProvider("aws_csm_2", makeConfigLocationProvider(MULTIPLE_PROFILES_WITH_CSM_PROPERTIES_CONFIG)); CsmConfiguration expected = CsmConfiguration.builder() .withEnabled(false) .withHost("bar2") .withPort(5678) .withClientId("bar") .build(); assertEquals(expected, provider.getConfiguration()); }
@Test public void testLocatesProfileNameUsingAwsProfileNameLoaderWhenNull_ProfileExists() { try { System.setProperty(AWS_PROFILE_SYSTEM_PROPERTY, TEST_PROFILE_NAME); ProfileCsmConfigurationProvider provider = new ProfileCsmConfigurationProvider(null, makeConfigLocationProvider(COMPLETE_CONFIG)); CsmConfiguration expected = CsmConfiguration.builder() .withEnabled(true) .withHost("bar") .withPort(1234) .withClientId("foo") .build(); assertEquals(expected, provider.getConfiguration()); } finally { System.clearProperty(AWS_PROFILE_SYSTEM_PROPERTY); } }
@Test(expected = SdkClientException.class) public void testLocatesProfileNameUsingAwsProfileNameLoaderWhenNull_ProfileNotExists() { try { System.setProperty(AWS_PROFILE_SYSTEM_PROPERTY, "my_other_profile"); ProfileCsmConfigurationProvider provider = new ProfileCsmConfigurationProvider(null, makeConfigLocationProvider(COMPLETE_CONFIG)); provider.getConfiguration(); } finally { System.clearProperty(AWS_PROFILE_SYSTEM_PROPERTY); } }
@Test public void optionalValuesNotSet_shouldUseDefaultValues() { ProfileCsmConfigurationProvider provider = new ProfileCsmConfigurationProvider(TEST_PROFILE_NAME, makeConfigLocationProvider(PROFILE_WITH_ONLY_ENABLED_PROPERTY)); assertEquals(CsmConfiguration.builder().withEnabled(true).build(), provider.getConfiguration()); }