CredentialUtils { public static AWSCredentialsProvider getCredentialsProvider( AmazonWebServiceRequest req, AWSCredentialsProvider base) { if (req != null && req.getRequestCredentialsProvider() != null) { return req.getRequestCredentialsProvider(); } return base; }  static AWSCredentialsProvider getCredentialsProvider(
            AmazonWebServiceRequest req,
            AWSCredentialsProvider base); static AWSCredentialsProvider getCredentialsProvider(
            RequestConfig requestConfig,
            AWSCredentialsProvider base);  }
@Test public void request_credentials_takes_precendence_over_client_credentials() { final String awsAccessKeyId = "foo"; final String awsSecretAccessKey = "bar"; final AWSCredentials reqCredentials = new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey); EmptyAmazonWebServiceRequest req = new EmptyAmazonWebServiceRequest(); req.setRequestCredentials(reqCredentials); AWSCredentialsProvider actual = CredentialUtils.getCredentialsProvider(req, null); Assert.assertThat(actual, Matchers.instanceOf(StaticCredentialsProvider .class)); assertEquals(awsAccessKeyId, actual.getCredentials().getAWSAccessKeyId()); assertEquals(awsSecretAccessKey, actual.getCredentials().getAWSSecretKey()); }
@Test public void base_credentials_returned_when_no_request_credentials_is_present() { final String awsAccessKeyId = "foo"; final String awsSecretAccessKey = "bar"; final StaticCredentialsProvider base = new StaticCredentialsProvider (new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey)); AWSCredentialsProvider actual = CredentialUtils .getCredentialsProvider((AmazonWebServiceRequest) null, base); Assert.assertThat(actual, Matchers.instanceOf(StaticCredentialsProvider .class)); assertEquals(awsAccessKeyId, actual.getCredentials().getAWSAccessKeyId()); assertEquals(awsSecretAccessKey, actual.getCredentials().getAWSSecretKey()); }
@Test public void requestCredentialsInRequestConfig_TakesPrecedenceOverClientCredentials() { AWSCredentialsProvider requestCredentials = mock(AWSCredentialsProvider.class); RequestConfig requestConfig = mock(RequestConfig.class); when(requestConfig.getCredentialsProvider()).thenReturn(requestCredentials); AWSCredentialsProvider actual = CredentialUtils .getCredentialsProvider(requestConfig, CLIENT_CREDENTIALS); assertEquals(requestCredentials, actual); }
@Test public void requestCredentialsNotSetInRequestConfig_ReturnsClientCredentials() { RequestConfig requestConfig = mock(RequestConfig.class); AWSCredentialsProvider actual = CredentialUtils .getCredentialsProvider(requestConfig, CLIENT_CREDENTIALS); assertEquals(CLIENT_CREDENTIALS, actual); }