1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.testutils.service; 17 18 import java.io.IOException; 19 import java.io.InputStream; 20 import reactor.blockhound.BlockHound; 21 import software.amazon.awssdk.auth.credentials.AwsCredentials; 22 import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain; 23 import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; 24 import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; 25 import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; 26 import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; 27 import software.amazon.awssdk.identity.spi.IdentityProvider; 28 import software.amazon.awssdk.utils.IoUtils; 29 30 public abstract class AwsIntegrationTestBase { 31 32 static { BlockHound.install()33 BlockHound.install(); 34 } 35 36 /** Default Properties Credentials file path. */ 37 private static final String TEST_CREDENTIALS_PROFILE_NAME = "aws-test-account"; 38 39 public static final AwsCredentialsProviderChain CREDENTIALS_PROVIDER_CHAIN = 40 AwsCredentialsProviderChain.of(ProfileCredentialsProvider.builder() 41 .profileName(TEST_CREDENTIALS_PROFILE_NAME) 42 .build(), 43 DefaultCredentialsProvider.create()); 44 45 46 /** 47 * Shared AWS credentials, loaded from a properties file. 48 */ 49 private static final AwsCredentials CREDENTIALS = CREDENTIALS_PROVIDER_CHAIN.resolveCredentials(); 50 51 /** 52 * @return AWSCredentials to use during tests. Setup by base fixture 53 * @deprecated by {@link #getCredentialsProvider()} 54 */ 55 @Deprecated getCredentials()56 protected static AwsCredentials getCredentials() { 57 return CREDENTIALS; 58 } 59 60 /** 61 * @return {@link IdentityProvider<AwsCredentialsIdentity>} to use during tests. Setup by base fixture 62 */ getCredentialsProvider()63 protected static IdentityProvider<AwsCredentialsIdentity> getCredentialsProvider() { 64 return StaticCredentialsProvider.create(CREDENTIALS); 65 } 66 67 /** 68 * Reads a system resource fully into a String 69 * 70 * @param location 71 * Relative or absolute location of system resource. 72 * @return String contents of resource file 73 * @throws RuntimeException 74 * if any error occurs 75 */ getResourceAsString(Class<?> clazz, String location)76 protected String getResourceAsString(Class<?> clazz, String location) { 77 try (InputStream resourceStream = clazz.getResourceAsStream(location)) { 78 String resourceAsString = IoUtils.toUtf8String(resourceStream); 79 resourceStream.close(); 80 return resourceAsString; 81 } catch (IOException e) { 82 throw new RuntimeException(e); 83 } 84 } 85 86 } 87