Skip to content

Commit

Permalink
Adds missing unit tests as noted in PR feedback. Updates the testing …
Browse files Browse the repository at this point in the history
…approach to deserialize using Jackson to better simulate the final usage.

Signed-off-by: David Venable <dlv@amazon.com>
  • Loading branch information
dlvenable committed Jun 2, 2023
1 parent dcbc251 commit f34627a
Showing 1 changed file with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,72 @@

package org.opensearch.dataprepper.plugins.sink.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.opensearch.dataprepper.test.helper.ReflectivelySetField;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
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.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;

class AwsAuthenticationOptionsTest {

private AwsAuthenticationOptions awsAuthenticationOptions;
private ObjectMapper objectMapper;

@BeforeEach
void setUp() {
awsAuthenticationOptions = new AwsAuthenticationOptions();
objectMapper = new ObjectMapper();
}

@ParameterizedTest
@ValueSource(strings = {"us-east-1", "us-west-2", "eu-central-1"})
void getAwsRegion_returns_Region_of(final String regionString) {
final Region expectedRegionObject = Region.of(regionString);
final Map<String, Object> jsonMap = Map.of("region", regionString);
final AwsAuthenticationOptions objectUnderTest = objectMapper.convertValue(jsonMap, AwsAuthenticationOptions.class);
assertThat(objectUnderTest.getAwsRegion(), equalTo(expectedRegionObject));
}

@Test
void getAwsRegion_returns_null_when_region_is_null() {
final Map<String, Object> jsonMap = Collections.emptyMap();
final AwsAuthenticationOptions objectUnderTest = objectMapper.convertValue(jsonMap, AwsAuthenticationOptions.class);
assertThat(objectUnderTest.getAwsRegion(), nullValue());
}

@Test
void getAwsStsRoleArn_returns_value_from_deserialized_JSON() {
final String stsRoleArn = UUID.randomUUID().toString();
final Map<String, Object> jsonMap = Map.of("sts_role_arn", stsRoleArn);
final AwsAuthenticationOptions objectUnderTest = objectMapper.convertValue(jsonMap, AwsAuthenticationOptions.class);
assertThat(objectUnderTest.getAwsStsRoleArn(), equalTo(stsRoleArn));
}

@Test
void getAwsStsRoleArn_returns_null_if_not_in_JSON() {
final Map<String, Object> jsonMap = Collections.emptyMap();
final AwsAuthenticationOptions objectUnderTest = objectMapper.convertValue(jsonMap, AwsAuthenticationOptions.class);
assertThat(objectUnderTest.getAwsStsRoleArn(), nullValue());
}

@Test
void getAwsRegion_returns_Region_of() throws NoSuchFieldException, IllegalAccessException {
final String regionString = UUID.randomUUID().toString();
final Region expectedRegionObject = mock(Region.class);
ReflectivelySetField.setField(AwsAuthenticationOptions.class, 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));
void getAwsStsHeaderOverrides_returns_value_from_deserialized_JSON() {
final Map<String, String> stsHeaderOverrides = Map.of(UUID.randomUUID().toString(), UUID.randomUUID().toString());
final Map<String, Object> jsonMap = Map.of("sts_header_overrides", stsHeaderOverrides);
final AwsAuthenticationOptions objectUnderTest = objectMapper.convertValue(jsonMap, AwsAuthenticationOptions.class);
assertThat(objectUnderTest.getAwsStsHeaderOverrides(), equalTo(stsHeaderOverrides));
}

@Test
void getAwsRegion_returns_null_when_region_is_null() throws NoSuchFieldException, IllegalAccessException {
ReflectivelySetField.setField(AwsAuthenticationOptions.class, awsAuthenticationOptions, "awsRegion", null);
assertThat(awsAuthenticationOptions.getAwsRegion(), nullValue());
void getAwsStsHeaderOverrides_returns_null_if_not_in_JSON() {
final Map<String, Object> jsonMap = Collections.emptyMap();
final AwsAuthenticationOptions objectUnderTest = objectMapper.convertValue(jsonMap, AwsAuthenticationOptions.class);
assertThat(objectUnderTest.getAwsStsHeaderOverrides(), nullValue());
}
}

0 comments on commit f34627a

Please sign in to comment.