AwsProfileRegionProvider extends AwsRegionProvider { @Override public String getRegion() throws SdkClientException { File configFile = locationProvider.getLocation(); if (configFile != null && configFile.exists()) { BasicProfile profile = loadProfile(configFile); if (profile != null && !StringUtils.isNullOrEmpty(profile.getRegion())) { return profile.getRegion(); } } return null; } AwsProfileRegionProvider();  AwsProfileRegionProvider(String profileName); @SdkTestInternalApi  AwsProfileRegionProvider(String profileName, AwsProfileFileLocationProvider locationProvider,
                             BasicProfileConfigLoader configLoader); @Override String getRegion();  }
@Test public void nullConfigFileLocation_ProvidesNullRegion() { when(locationProvider.getLocation()).thenReturn(null); assertNull(regionProvider.getRegion()); }
@Test public void nonExistentConfigFile_ProvidesNullRegion() { when(locationProvider.getLocation()).thenReturn(new File("/var/tmp/this/is/invalid.txt")); assertNull(regionProvider.getRegion()); }
@Test public void profilesAreEmpty_ProvidesNullRegion() { when(configLoader.loadProfiles(any(File.class))) .thenReturn(new AllProfiles(new HashMap<String, BasicProfile>())); assertNull(regionProvider.getRegion()); }
@Test public void profilesNonEmptyButGivenProfileNotPresent_ProvidesNullRegion() { final String otherProfileName = "other_profile"; final BasicProfile other_profile = new BasicProfile(otherProfileName, ImmutableMapParameter .of(ProfileKeyConstants.REGION, "us-east-8")); final AllProfiles profiles = new AllProfiles( ImmutableMapParameter.of(otherProfileName, other_profile)); stubLoadProfile(profiles); assertNull(regionProvider.getRegion()); }
@Test public void profilePresentButRegionIsNotSet_ProvidesNullRegion() { final BasicProfile profile = new BasicProfile(PROFILE, new HashMap<String, String>()); final AllProfiles profiles = new AllProfiles(ImmutableMapParameter.of(PROFILE, profile)); stubLoadProfile(profiles); assertNull(regionProvider.getRegion()); }
@Test public void profilePresentButRegionIsEmpty_ProvidesNullRegion() { final BasicProfile profile = new BasicProfile(PROFILE, ImmutableMapParameter .of(ProfileKeyConstants.REGION, "")); final AllProfiles profiles = new AllProfiles(ImmutableMapParameter.of(PROFILE, profile)); stubLoadProfile(profiles); assertNull(regionProvider.getRegion()); }
@Test public void profilePresentAndRegionIsSet_ProvidesCorrectRegion() { final String expectedRegion = "us-east-8"; final BasicProfile profile = new BasicProfile(PROFILE, ImmutableMapParameter .of(ProfileKeyConstants.REGION, expectedRegion)); final AllProfiles profiles = new AllProfiles(ImmutableMapParameter.of(PROFILE, profile)); stubLoadProfile(profiles); assertEquals(expectedRegion, regionProvider.getRegion()); }