use of software.amazon.awssdk.testutils.EnvironmentVariableHelper in project aws-sdk-java-v2 by aws.
the class DualstackEndpointTest method resolvesCorrectEndpoint.
@Test
public void resolvesCorrectEndpoint() {
String systemPropertyBeforeTest = System.getProperty(SdkSystemSetting.AWS_USE_DUALSTACK_ENDPOINT.property());
EnvironmentVariableHelper helper = new EnvironmentVariableHelper();
try {
ProtocolRestJsonClientBuilder builder = ProtocolRestJsonClient.builder().region(Region.US_WEST_2).credentialsProvider(AnonymousCredentialsProvider.create());
if (testCase.clientSetting != null) {
builder.dualstackEnabled(testCase.clientSetting);
}
if (testCase.systemPropSetting != null) {
System.setProperty(SdkSystemSetting.AWS_USE_DUALSTACK_ENDPOINT.property(), testCase.systemPropSetting);
}
if (testCase.envVarSetting != null) {
helper.set(SdkSystemSetting.AWS_USE_DUALSTACK_ENDPOINT.environmentVariable(), testCase.envVarSetting);
}
ProfileFile.Builder profileFile = ProfileFile.builder().type(ProfileFile.Type.CONFIGURATION);
if (testCase.profileSetting != null) {
profileFile.content(new StringInputStream("[default]\n" + ProfileProperty.USE_DUALSTACK_ENDPOINT + " = " + testCase.profileSetting));
} else {
profileFile.content(new StringInputStream(""));
}
EndpointCapturingInterceptor interceptor = new EndpointCapturingInterceptor();
builder.overrideConfiguration(c -> c.defaultProfileFile(profileFile.build()).defaultProfileName("default").addExecutionInterceptor(interceptor));
if (testCase instanceof SuccessCase) {
ProtocolRestJsonClient client = builder.build();
try {
client.allTypes();
} catch (EndpointCapturingInterceptor.CaptureCompletedException e) {
// Expected
}
boolean expectedDualstackEnabled = ((SuccessCase) testCase).expectedValue;
String expectedEndpoint = expectedDualstackEnabled ? "https://customresponsemetadata.us-west-2.api.aws/2016-03-11/allTypes" : "https://customresponsemetadata.us-west-2.amazonaws.com/2016-03-11/allTypes";
assertThat(interceptor.endpoints()).singleElement().isEqualTo(expectedEndpoint);
} else {
FailureCase failure = Validate.isInstanceOf(FailureCase.class, testCase, "Unexpected test case type.");
assertThatThrownBy(builder::build).hasMessageContaining(failure.exceptionMessage);
}
} finally {
if (systemPropertyBeforeTest != null) {
System.setProperty(SdkSystemSetting.AWS_USE_DUALSTACK_ENDPOINT.property(), systemPropertyBeforeTest);
} else {
System.clearProperty(SdkSystemSetting.AWS_USE_DUALSTACK_ENDPOINT.property());
}
helper.reset();
}
}
use of software.amazon.awssdk.testutils.EnvironmentVariableHelper in project aws-sdk-java-v2 by aws.
the class FipsEndpointTest method resolvesCorrectEndpoint.
@Test
public void resolvesCorrectEndpoint() {
String systemPropertyBeforeTest = System.getProperty(SdkSystemSetting.AWS_USE_FIPS_ENDPOINT.property());
EnvironmentVariableHelper helper = new EnvironmentVariableHelper();
try {
ProtocolRestJsonClientBuilder builder = ProtocolRestJsonClient.builder().region(Region.US_WEST_2).credentialsProvider(AnonymousCredentialsProvider.create());
if (testCase.clientSetting != null) {
builder.fipsEnabled(testCase.clientSetting);
}
if (testCase.systemPropSetting != null) {
System.setProperty(SdkSystemSetting.AWS_USE_FIPS_ENDPOINT.property(), testCase.systemPropSetting);
}
if (testCase.envVarSetting != null) {
helper.set(SdkSystemSetting.AWS_USE_FIPS_ENDPOINT.environmentVariable(), testCase.envVarSetting);
}
ProfileFile.Builder profileFile = ProfileFile.builder().type(ProfileFile.Type.CONFIGURATION);
if (testCase.profileSetting != null) {
profileFile.content(new StringInputStream("[default]\n" + ProfileProperty.USE_FIPS_ENDPOINT + " = " + testCase.profileSetting));
} else {
profileFile.content(new StringInputStream(""));
}
EndpointCapturingInterceptor interceptor = new EndpointCapturingInterceptor();
builder.overrideConfiguration(c -> c.defaultProfileFile(profileFile.build()).defaultProfileName("default").addExecutionInterceptor(interceptor));
if (testCase instanceof SuccessCase) {
ProtocolRestJsonClient client = builder.build();
try {
client.allTypes();
} catch (EndpointCapturingInterceptor.CaptureCompletedException e) {
// Expected
}
boolean expectedFipsEnabled = ((SuccessCase) testCase).expectedValue;
String expectedEndpoint = expectedFipsEnabled ? "https://customresponsemetadata-fips.us-west-2.amazonaws.com/2016-03-11/allTypes" : "https://customresponsemetadata.us-west-2.amazonaws.com/2016-03-11/allTypes";
assertThat(interceptor.endpoints()).singleElement().isEqualTo(expectedEndpoint);
} else {
FailureCase failure = Validate.isInstanceOf(FailureCase.class, testCase, "Unexpected test case type.");
assertThatThrownBy(builder::build).hasMessageContaining(failure.exceptionMessage);
}
} finally {
if (systemPropertyBeforeTest != null) {
System.setProperty(SdkSystemSetting.AWS_USE_FIPS_ENDPOINT.property(), systemPropertyBeforeTest);
} else {
System.clearProperty(SdkSystemSetting.AWS_USE_FIPS_ENDPOINT.property());
}
helper.reset();
}
}
use of software.amazon.awssdk.testutils.EnvironmentVariableHelper in project aws-sdk-java-v2 by aws.
the class S3EndpointResolutionTest method regionalSettingDisabled_usesGlobalEndpoint.
@Test
public void regionalSettingDisabled_usesGlobalEndpoint() throws UnsupportedEncodingException {
EnvironmentVariableHelper environmentVariableHelper = new EnvironmentVariableHelper();
environmentVariableHelper.set(SdkSystemSetting.AWS_S3_US_EAST_1_REGIONAL_ENDPOINT.environmentVariable(), "nonregional");
mockHttpClient.stubNextResponse(mockListObjectsResponse());
S3Client s3Client = S3Client.builder().credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))).httpClient(mockHttpClient).region(Region.US_EAST_1).serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(true).build()).build();
try {
s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());
assertThat(mockHttpClient.getLastRequest().getUri().getHost()).isEqualTo("s3.amazonaws.com");
} finally {
environmentVariableHelper.reset();
}
}
use of software.amazon.awssdk.testutils.EnvironmentVariableHelper in project aws-sdk-java-v2 by aws.
the class S3EndpointResolutionTest method regionalSettingEnabled_usesRegionalIadEndpoint.
@Test
public void regionalSettingEnabled_usesRegionalIadEndpoint() throws UnsupportedEncodingException {
EnvironmentVariableHelper environmentVariableHelper = new EnvironmentVariableHelper();
environmentVariableHelper.set(SdkSystemSetting.AWS_S3_US_EAST_1_REGIONAL_ENDPOINT.environmentVariable(), "regional");
mockHttpClient.stubNextResponse(mockListObjectsResponse());
S3Client s3Client = S3Client.builder().credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"))).httpClient(mockHttpClient).region(Region.US_EAST_1).serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(true).build()).build();
try {
s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());
assertThat(mockHttpClient.getLastRequest().getUri().getHost()).isEqualTo("s3.us-east-1.amazonaws.com");
} finally {
environmentVariableHelper.reset();
}
}
Aggregations