AwsRegionProviderChain extends AwsRegionProvider { @Override public String getRegion() throws SdkClientException { for (AwsRegionProvider provider : providers) { try { final String region = provider.getRegion(); if (region != null) { return region; } } catch (Exception e) { LOG.debug("Unable to load region from " + provider.toString() + ": " + e.getMessage()); } } throw new SdkClientException("Unable to load region information from any provider in the chain"); } AwsRegionProviderChain(AwsRegionProvider... providers); @Override String getRegion();  }
@Test public void firstProviderInChainGivesRegionInformation_DoesNotConsultOtherProviders() { AwsRegionProvider providerOne = mock(AwsRegionProvider.class); AwsRegionProvider providerTwo = mock(AwsRegionProvider.class); AwsRegionProvider providerThree = mock(AwsRegionProvider.class); AwsRegionProviderChain chain = new AwsRegionProviderChain(providerOne, providerTwo, providerThree); final String expectedRegion = "some-region-string"; when(providerOne.getRegion()).thenReturn(expectedRegion); assertEquals(expectedRegion, chain.getRegion()); verify(providerTwo, never()).getRegion(); verify(providerThree, never()).getRegion(); }
@Test public void lastProviderInChainGivesRegionInformation() { final String expectedRegion = "some-region-string"; AwsRegionProviderChain chain = new AwsRegionProviderChain(new NeverAwsRegionProvider(), new NeverAwsRegionProvider(), new StaticAwsRegionProvider( expectedRegion)); assertEquals(expectedRegion, chain.getRegion()); }
@Test public void providerThrowsException_ContinuesToNextInChain() { final String expectedRegion = "some-region-string"; AwsRegionProviderChain chain = new AwsRegionProviderChain(new NeverAwsRegionProvider(), new FaultyAwsRegionProvider(), new StaticAwsRegionProvider( expectedRegion)); assertEquals(expectedRegion, chain.getRegion()); }
@Test(expected = Error.class) public void providerThrowsError_DoesNotContinueChain() { final String expectedRegion = "some-region-string"; AwsRegionProviderChain chain = new AwsRegionProviderChain(new NeverAwsRegionProvider(), new FatalAwsRegionProvider(), new StaticAwsRegionProvider( expectedRegion)); assertEquals(expectedRegion, chain.getRegion()); }
@Test(expected = AmazonClientException.class) public void noProviderGivesRegion_ThrowsAmazonClientException() { AwsRegionProviderChain chain = new AwsRegionProviderChain(new NeverAwsRegionProvider(), new NeverAwsRegionProvider(), new NeverAwsRegionProvider()); chain.getRegion(); }