-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit tests and addressed comments in design doc
Signed-off-by: Jeremy Michael <[email protected]>
- Loading branch information
Jeremy Michael
committed
Dec 18, 2024
1 parent
b79f0d8
commit 29701d0
Showing
10 changed files
with
616 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,4 @@ sqs-pipeline: | |
region: <AWS_REGION> | ||
sts_role_arn: <IAM_ROLE_ARN> | ||
sink: | ||
- stdout: | ||
- stdout: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...test/java/org/opensearch/dataprepper/plugins/source/sqs/AwsAuthenticationAdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source.sqs; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.dataprepper.aws.api.AwsCredentialsOptions; | ||
import org.opensearch.dataprepper.aws.api.AwsCredentialsSupplier; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
import software.amazon.awssdk.regions.Region; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.Mockito.mock; | ||
|
||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AwsAuthenticationAdapterTest { | ||
@Mock | ||
private AwsCredentialsSupplier awsCredentialsSupplier; | ||
@Mock | ||
private SqsSourceConfig sqsSourceConfig; | ||
|
||
@Mock | ||
private AwsAuthenticationOptions awsAuthenticationOptions; | ||
private String stsRoleArn; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
when(sqsSourceConfig.getAwsAuthenticationOptions()).thenReturn(awsAuthenticationOptions); | ||
|
||
stsRoleArn = UUID.randomUUID().toString(); | ||
when(awsAuthenticationOptions.getAwsStsRoleArn()).thenReturn(stsRoleArn); | ||
} | ||
|
||
private AwsAuthenticationAdapter createObjectUnderTest() { | ||
return new AwsAuthenticationAdapter(awsCredentialsSupplier, sqsSourceConfig); | ||
} | ||
|
||
@Test | ||
void getCredentialsProvider_returns_AwsCredentialsProvider_from_AwsCredentialsSupplier() { | ||
final AwsCredentialsProvider expectedProvider = mock(AwsCredentialsProvider.class); | ||
when(awsCredentialsSupplier.getProvider(any(AwsCredentialsOptions.class))) | ||
.thenReturn(expectedProvider); | ||
|
||
assertThat(createObjectUnderTest().getCredentialsProvider(), equalTo(expectedProvider)); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"us-east-1", "eu-west-1"}) | ||
void getCredentialsProvider_creates_expected_AwsCredentialsOptions(final String regionString) { | ||
final String externalId = UUID.randomUUID().toString(); | ||
final Region region = Region.of(regionString); | ||
|
||
final Map<String, String> headerOverrides = Collections.singletonMap(UUID.randomUUID().toString(), UUID.randomUUID().toString()); | ||
when(awsAuthenticationOptions.getAwsStsExternalId()).thenReturn(externalId); | ||
when(awsAuthenticationOptions.getAwsRegion()).thenReturn(region); | ||
when(awsAuthenticationOptions.getAwsStsHeaderOverrides()).thenReturn(headerOverrides); | ||
|
||
createObjectUnderTest().getCredentialsProvider(); | ||
|
||
final ArgumentCaptor<AwsCredentialsOptions> credentialsOptionsArgumentCaptor = ArgumentCaptor.forClass(AwsCredentialsOptions.class); | ||
verify(awsCredentialsSupplier).getProvider(credentialsOptionsArgumentCaptor.capture()); | ||
|
||
final AwsCredentialsOptions actualOptions = credentialsOptionsArgumentCaptor.getValue(); | ||
|
||
assertThat(actualOptions, notNullValue()); | ||
assertThat(actualOptions.getStsRoleArn(), equalTo(stsRoleArn)); | ||
assertThat(actualOptions.getStsExternalId(), equalTo(externalId)); | ||
assertThat(actualOptions.getRegion(), equalTo(region)); | ||
assertThat(actualOptions.getStsHeaderOverrides(), equalTo(headerOverrides)); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...test/java/org/opensearch/dataprepper/plugins/source/sqs/AwsAuthenticationOptionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source.sqs; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
import software.amazon.awssdk.regions.Region; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.UUID; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
|
||
class AwsAuthenticationOptionsTest { | ||
|
||
private AwsAuthenticationOptions awsAuthenticationOptions; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
awsAuthenticationOptions = new AwsAuthenticationOptions(); | ||
} | ||
|
||
@Test | ||
void getAwsRegion_returns_Region_of() throws NoSuchFieldException, IllegalAccessException { | ||
final String regionString = UUID.randomUUID().toString(); | ||
final Region expectedRegionObject = mock(Region.class); | ||
reflectivelySetField(awsAuthenticationOptions, "awsRegion", regionString); | ||
final Region actualRegion; | ||
try(final MockedStatic<Region> regionMockedStatic = mockStatic(Region.class)) { | ||
regionMockedStatic.when(() -> Region.of(regionString)).thenReturn(expectedRegionObject); | ||
actualRegion = awsAuthenticationOptions.getAwsRegion(); | ||
} | ||
assertThat(actualRegion, equalTo(expectedRegionObject)); | ||
} | ||
|
||
@Test | ||
void getAwsRegion_returns_null_when_region_is_null() throws NoSuchFieldException, IllegalAccessException { | ||
reflectivelySetField(awsAuthenticationOptions, "awsRegion", null); | ||
assertThat(awsAuthenticationOptions.getAwsRegion(), nullValue()); | ||
} | ||
|
||
@Test | ||
void getStsExternalId_notNull() throws NoSuchFieldException, IllegalAccessException { | ||
final String externalId = UUID.randomUUID().toString(); | ||
reflectivelySetField(awsAuthenticationOptions, "awsStsExternalId", externalId); | ||
assertThat(awsAuthenticationOptions.getAwsStsExternalId(), equalTo(externalId)); | ||
} | ||
|
||
@Test | ||
void getStsExternalId_Null() throws NoSuchFieldException, IllegalAccessException { | ||
reflectivelySetField(awsAuthenticationOptions, "awsStsExternalId", null); | ||
assertThat(awsAuthenticationOptions.getAwsStsExternalId(), nullValue()); | ||
} | ||
|
||
private void reflectivelySetField(final AwsAuthenticationOptions awsAuthenticationOptions, final String fieldName, final Object value) throws NoSuchFieldException, IllegalAccessException { | ||
final Field field = AwsAuthenticationOptions.class.getDeclaredField(fieldName); | ||
try { | ||
field.setAccessible(true); | ||
field.set(awsAuthenticationOptions, value); | ||
} finally { | ||
field.setAccessible(false); | ||
} | ||
} | ||
} |
Oops, something went wrong.