/*
 * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package software.amazon.awssdk.core.regions.providers;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URISyntaxException;
import java.nio.file.Paths;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import software.amazon.awssdk.core.AwsSystemSetting;
import software.amazon.awssdk.core.regions.Region;

public class AwsProfileRegionProviderTest {
    private String initialDefaultProfile;
    private String initialProfileLocation;

    @Before
    public void setup() {
        this.initialDefaultProfile = AwsSystemSetting.AWS_PROFILE.getStringValue().orElse(null);
        this.initialProfileLocation = AwsSystemSetting.AWS_CONFIG_FILE.getStringValue().orElse(null);
    }

    @After
    public void teardown() {
        if (initialDefaultProfile == null) {
            System.clearProperty(AwsSystemSetting.AWS_PROFILE.property());
        } else {
            System.setProperty(AwsSystemSetting.AWS_PROFILE.property(), initialDefaultProfile);
        }

        if (initialProfileLocation == null) {
            System.clearProperty(AwsSystemSetting.AWS_CONFIG_FILE.property());
        } else {
            System.setProperty(AwsSystemSetting.AWS_CONFIG_FILE.property(), initialProfileLocation);
        }
    }

    @Test
    public void nonExistentDefaultConfigFile_ReturnsNull() {
        System.setProperty(AwsSystemSetting.AWS_CONFIG_FILE.property(), "/var/tmp/this/is/invalid.txt");
        assertThat(new AwsProfileRegionProvider().getRegion()).isNull();
    }

    @Test
    public void profilePresentAndRegionIsSet_ProvidesCorrectRegion() throws URISyntaxException {
        String testFile = "/resources/profileconfig/test-profiles.tst";
        System.setProperty(AwsSystemSetting.AWS_PROFILE.property(), "test");
        System.setProperty(AwsSystemSetting.AWS_CONFIG_FILE.property(),
                           Paths.get(getClass().getResource(testFile).toURI()).toString());
        assertThat(new AwsProfileRegionProvider().getRegion()).isEqualTo(Region.of("saa"));
    }
}

@@ -779,37 +779,11 @@
 nit.
-After;%0Aimport org.junit.Befor
+Rul
 e;%0Ai
@@ -907,16 +907,83 @@
 .Region;
+%0Aimport software.amazon.awssdk.testutils.EnvironmentVariableHelper;
 %0A%0Apublic
@@ -1024,348 +1024,116 @@
 t %7B%0A
+%0A
     
-private String initialDefaultProfile;%0A    private String initialProfileLocation;%0A%0A    @Before%0A    public void setup() %7B%0A        this.initialDefaultProfile = AwsSystemSetting.AWS_PROFILE.getStringValue().orElse(null);%0A        this.initialProfileLocation = AwsSystemSetting.AWS_CONFIG_FILE.getStringValue().orElse(null);%0A    %7D
+@Rule%0A    public EnvironmentVariableHelper settingsHelper = new EnvironmentVariableHelper();
 %0A%0A    @
-After
+Test
 %0A   
@@ -1149,307 +1149,49 @@
 oid 
-teardown() %7B%0A        if (initialDefaultProfile == null) %7B%0A            System.clearProperty(AwsSystemSetting.AWS_PROFILE.property());%0A        %7D else %7B%0A            System.setProperty(AwsSystemSetting.AWS_PROFILE.property(), initialDefaultProfile);%0A        %7D%0A%0A        if (initialProfileLocation == n
+nonExistentDefaultConfigFile_ReturnsN
 ull
+(
 ) %7B%0A
@@ -1202,355 +1202,165 @@
     
-  
-  System.clearProperty(AwsSystemSetting.AWS_CONFIG_FILE.property());%0A        %7D else %7B%0A            System.setProperty(AwsSystemSetting.AWS_CONFIG_FILE.property(), initialProfileLocation);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void nonExistentDefaultConfigFile_ReturnsNull() %7B%0A        System.setProperty(AwsSystemSetting.AWS_CONFIG_FILE.property()
+settingsHelper.set(AwsSystemSetting.AWS_CONFIG_FILE, %22/var/tmp/this/is/invalid.txt%22);%0A        settingsHelper.set(AwsSystemSetting.AWS_SHARED_CREDENTIALS_FILE
 , %22/
@@ -1367,32 +1367,37 @@
 var/tmp/this/is/
+also.
 invalid.txt%22);%0A 
@@ -1654,24 +1654,25 @@
 s.tst%22;%0A
+%0A
         
 System.s
@@ -1663,34 +1663,34 @@
         
-System.setProperty
+settingsHelper.set
 (AwsSyst
@@ -1710,27 +1710,16 @@
 _PROFILE
-.property()
 , %22test%22
@@ -1733,26 +1733,26 @@
     
-System.setProperty
+settingsHelper.set
 (Aws
@@ -1784,47 +1784,9 @@
 FILE
-.property(),%0A                          
+,
  Pat

Fix profile test so that it doesn't pick up the credentials file if the config file is missing.