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 static org.junit.Assert.assertThat; 19 import static software.amazon.awssdk.utils.StringUtils.isBlank; 20 21 import java.io.IOException; 22 import java.io.InputStream; 23 import org.hamcrest.Description; 24 import org.hamcrest.Matcher; 25 import org.hamcrest.TypeSafeMatcher; 26 import reactor.blockhound.BlockHound; 27 import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain; 28 import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; 29 import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; 30 import software.amazon.awssdk.core.exception.SdkServiceException; 31 import software.amazon.awssdk.utils.IoUtils; 32 33 public abstract class AwsTestBase { 34 35 static { BlockHound.install()36 BlockHound.install(); 37 } 38 39 /** Default Properties Credentials file path. */ 40 private static final String TEST_CREDENTIALS_PROFILE_NAME = "aws-test-account"; 41 42 public static final AwsCredentialsProviderChain CREDENTIALS_PROVIDER_CHAIN = 43 AwsCredentialsProviderChain.of(ProfileCredentialsProvider.builder() 44 .profileName(TEST_CREDENTIALS_PROFILE_NAME) 45 .build(), 46 DefaultCredentialsProvider.create()); 47 48 /** 49 * @deprecated Extend from {@link AwsIntegrationTestBase} to access credentials 50 */ 51 @Deprecated setUpCredentials()52 public static void setUpCredentials() { 53 // Ignored 54 } 55 56 /** 57 * Reads a system resource fully into a String 58 * 59 * @param location 60 * Relative or absolute location of system resource. 61 * @return String contents of resource file 62 * @throws RuntimeException 63 * if any error occurs 64 */ getResourceAsString(Class<?> clazz, String location)65 protected static String getResourceAsString(Class<?> clazz, String location) { 66 try (InputStream resourceStream = clazz.getResourceAsStream(location)) { 67 return IoUtils.toUtf8String(resourceStream); 68 } catch (IOException e) { 69 throw new RuntimeException(e); 70 } 71 } 72 73 /** 74 * @deprecated Use {@link #isValidSdkServiceException} in a hamcrest matcher 75 */ 76 @Deprecated assertValidException(SdkServiceException e)77 protected void assertValidException(SdkServiceException e) { 78 assertThat(e, isValidSdkServiceException()); 79 } 80 isValidSdkServiceException()81 public static Matcher<SdkServiceException> isValidSdkServiceException() { 82 return new TypeSafeMatcher<SdkServiceException>() { 83 private StringBuilder sb = new StringBuilder(); 84 @Override 85 protected boolean matchesSafely(SdkServiceException item) { 86 isNotBlank(item.requestId(), "requestId"); 87 isNotBlank(item.getMessage(), "message"); 88 return sb.length() == 0; 89 } 90 91 @Override 92 public void describeTo(Description description) { 93 description.appendText(sb.toString()); 94 } 95 96 private void isNotBlank(String value, String fieldName) { 97 if (isBlank(value)) { 98 if (sb.length() > 0) { 99 sb.append(", "); 100 } 101 sb.append(fieldName).append(" should not be null or blank"); 102 } 103 } 104 }; 105 } 106 } 107