diff --git a/src/main/java/com/thoughtworks/gocd/secretmanager/aws/validators/CredentialValidator.java b/src/main/java/com/thoughtworks/gocd/secretmanager/aws/validators/CredentialValidator.java index 914df95..18862e5 100644 --- a/src/main/java/com/thoughtworks/gocd/secretmanager/aws/validators/CredentialValidator.java +++ b/src/main/java/com/thoughtworks/gocd/secretmanager/aws/validators/CredentialValidator.java @@ -31,7 +31,15 @@ import static org.apache.commons.lang3.StringUtils.isBlank; public class CredentialValidator implements Validator { - private final AWSCredentialsProviderChain credentialsProviderChain = new AWSCredentialsProviderChain(); + private final AWSCredentialsProviderChain credentialsProviderChain; + + public CredentialValidator() { + this(new AWSCredentialsProviderChain()); + } + + CredentialValidator(AWSCredentialsProviderChain credentialsProviderChain) { + this.credentialsProviderChain = credentialsProviderChain; + } @Override public ValidationResult validate(Map requestBody) { diff --git a/src/test/java/com/thoughtworks/gocd/secretmanager/aws/validators/CredentialValidatorTest.java b/src/test/java/com/thoughtworks/gocd/secretmanager/aws/validators/CredentialValidatorTest.java index edb74f9..fd65466 100644 --- a/src/test/java/com/thoughtworks/gocd/secretmanager/aws/validators/CredentialValidatorTest.java +++ b/src/test/java/com/thoughtworks/gocd/secretmanager/aws/validators/CredentialValidatorTest.java @@ -17,30 +17,38 @@ package com.thoughtworks.gocd.secretmanager.aws.validators; import cd.go.plugin.base.validation.ValidationResult; +import com.thoughtworks.gocd.secretmanager.aws.AWSCredentialsProviderChain; import com.thoughtworks.gocd.secretmanager.aws.annotations.JsonSource; +import com.thoughtworks.gocd.secretmanager.aws.exceptions.AWSCredentialsException; import com.thoughtworks.gocd.secretmanager.aws.extensions.EnvironmentVariable; import com.thoughtworks.gocd.secretmanager.aws.extensions.SystemProperty; import org.json.JSONException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.mockito.Mock; import java.util.HashMap; import java.util.Map; -import static com.amazonaws.SDKGlobalConfiguration.*; import static cd.go.plugin.base.GsonTransformer.toJson; +import static com.amazonaws.SDKGlobalConfiguration.*; import static com.thoughtworks.gocd.secretmanager.aws.models.SecretConfig.ACCESS_KEY; import static com.thoughtworks.gocd.secretmanager.aws.models.SecretConfig.SECRET_ACCESS_KEY; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; class CredentialValidatorTest { + @Mock + private AWSCredentialsProviderChain credentialsProviderChain; private CredentialValidator credentialValidator; @BeforeEach void setUp() { - credentialValidator = new CredentialValidator(); + initMocks(this); + credentialValidator = new CredentialValidator(credentialsProviderChain); } @Test @@ -71,6 +79,8 @@ void shouldBeValidIfCredentialsAreProvidedAsSystemProperties() { @ParameterizedTest @JsonSource(jsonFiles = "/missing-credentials-validation-error.json") void shouldBeInvalidWhenCredentialsAreNotProvidedAndFailsToDetectItUsingCredentialProviders(String expectedJson) throws JSONException { + when(credentialsProviderChain.autoDetectAWSCredentials()).thenThrow(new AWSCredentialsException("Boom!")); + ValidationResult result = credentialValidator.validate(secretConfig(null, null)); assertThat(result.isEmpty()).isFalse(); @@ -83,4 +93,4 @@ private Map secretConfig(String accessKey, String secretAccessKe secretConfigMap.put(SECRET_ACCESS_KEY, secretAccessKey); return secretConfigMap; } -} \ No newline at end of file +}