|
19 | 19 |
|
20 | 20 | import java.net.URISyntaxException;
|
21 | 21 | import java.nio.file.Paths;
|
22 |
| -import org.junit.After; |
23 |
| -import org.junit.Before; |
| 22 | +import org.junit.Rule; |
24 | 23 | import org.junit.Test;
|
25 | 24 | import software.amazon.awssdk.core.AwsSystemSetting;
|
26 | 25 | import software.amazon.awssdk.core.regions.Region;
|
| 26 | +import software.amazon.awssdk.testutils.EnvironmentVariableHelper; |
27 | 27 |
|
28 | 28 | public class AwsProfileRegionProviderTest {
|
29 |
| - private String initialDefaultProfile; |
30 |
| - private String initialProfileLocation; |
31 | 29 |
|
32 |
| - @Before |
33 |
| - public void setup() { |
34 |
| - this.initialDefaultProfile = AwsSystemSetting.AWS_PROFILE.getStringValue().orElse(null); |
35 |
| - this.initialProfileLocation = AwsSystemSetting.AWS_CONFIG_FILE.getStringValue().orElse(null); |
36 |
| - } |
37 |
| - |
38 |
| - @After |
39 |
| - public void teardown() { |
40 |
| - if (initialDefaultProfile == null) { |
41 |
| - System.clearProperty(AwsSystemSetting.AWS_PROFILE.property()); |
42 |
| - } else { |
43 |
| - System.setProperty(AwsSystemSetting.AWS_PROFILE.property(), initialDefaultProfile); |
44 |
| - } |
45 |
| - |
46 |
| - if (initialProfileLocation == null) { |
47 |
| - System.clearProperty(AwsSystemSetting.AWS_CONFIG_FILE.property()); |
48 |
| - } else { |
49 |
| - System.setProperty(AwsSystemSetting.AWS_CONFIG_FILE.property(), initialProfileLocation); |
50 |
| - } |
51 |
| - } |
| 30 | + @Rule |
| 31 | + public EnvironmentVariableHelper settingsHelper = new EnvironmentVariableHelper(); |
52 | 32 |
|
53 | 33 | @Test
|
54 | 34 | public void nonExistentDefaultConfigFile_ReturnsNull() {
|
55 |
| - System.setProperty(AwsSystemSetting.AWS_CONFIG_FILE.property(), "/var/tmp/this/is/invalid.txt"); |
| 35 | + settingsHelper.set(AwsSystemSetting.AWS_CONFIG_FILE, "/var/tmp/this/is/invalid.txt"); |
| 36 | + settingsHelper.set(AwsSystemSetting.AWS_SHARED_CREDENTIALS_FILE, "/var/tmp/this/is/also.invalid.txt"); |
56 | 37 | assertThat(new AwsProfileRegionProvider().getRegion()).isNull();
|
57 | 38 | }
|
58 | 39 |
|
59 | 40 | @Test
|
60 | 41 | public void profilePresentAndRegionIsSet_ProvidesCorrectRegion() throws URISyntaxException {
|
61 | 42 | String testFile = "/resources/profileconfig/test-profiles.tst";
|
62 |
| - System.setProperty(AwsSystemSetting.AWS_PROFILE.property(), "test"); |
63 |
| - System.setProperty(AwsSystemSetting.AWS_CONFIG_FILE.property(), |
64 |
| - Paths.get(getClass().getResource(testFile).toURI()).toString()); |
| 43 | + |
| 44 | + settingsHelper.set(AwsSystemSetting.AWS_PROFILE, "test"); |
| 45 | + settingsHelper.set(AwsSystemSetting.AWS_CONFIG_FILE, Paths.get(getClass().getResource(testFile).toURI()).toString()); |
65 | 46 | assertThat(new AwsProfileRegionProvider().getRegion()).isEqualTo(Region.of("saa"));
|
66 | 47 | }
|
67 | 48 | }
|
0 commit comments