Skip to content

[JUnit 5] Migrate some parameterized tests to JUnit 5 #2880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,112 +16,104 @@
package software.amazon.awssdk.auth.credentials.internal;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.function.Supplier;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.stream.Stream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import software.amazon.awssdk.core.SdkSystemSetting;
import software.amazon.awssdk.profiles.ProfileFile;
import software.amazon.awssdk.profiles.ProfileFileSystemSetting;
import software.amazon.awssdk.testutils.EnvironmentVariableHelper;

@RunWith(Parameterized.class)
public class Ec2MetadataConfigProviderEndpointModeTest {
private static final String TEST_PROFILES_PATH_PREFIX = "/software/amazon/awssdk/auth/credentials/internal/ec2metadataconfigprovider/";
private static final EnvironmentVariableHelper ENVIRONMENT_VARIABLE_HELPER = new EnvironmentVariableHelper();
private static final String CUSTOM_PROFILE = "myprofile";

@Parameterized.Parameter
public TestCase testCase;

@Rule
public ExpectedException thrown = ExpectedException.none();

@Parameterized.Parameters(name = "{0}")
public static Iterable<Object> testCases() {
return Arrays.asList(
new TestCase().expectedEndpointMode(null).expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV4),

new TestCase().envEndpointMode("ipv4").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV4),
new TestCase().envEndpointMode("IPv4").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV4),
new TestCase().envEndpointMode("ipv6").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
new TestCase().envEndpointMode("IPv6").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
new TestCase().envEndpointMode("Ipv99").expectedException(IllegalArgumentException.class),

new TestCase().systemPropertyEndpointMode("ipv4").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV4),
new TestCase().systemPropertyEndpointMode("IPv4").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV4),
new TestCase().systemPropertyEndpointMode("ipv6").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
new TestCase().systemPropertyEndpointMode("IPv6").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
new TestCase().systemPropertyEndpointMode("Ipv99").expectedException(IllegalArgumentException.class),

new TestCase().sharedConfigFile(TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv6")
.expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
new TestCase().sharedConfigFile(TEST_PROFILES_PATH_PREFIX + "endpoint_mode_invalidValue")
.expectedException(IllegalArgumentException.class),

// System property takes highest precedence
new TestCase().systemPropertyEndpointMode("ipv6").envEndpointMode("ipv4")
.expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
new TestCase().systemPropertyEndpointMode("ipv6").sharedConfigFile(TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv4")
.expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),

// env var has higher precedence than shared config
new TestCase().envEndpointMode("ipv6").sharedConfigFile(TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv4")
.expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),

// Test custom profile supplier and custom profile name
new TestCase().sharedConfigFile(TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv6_custom_profile")
.customProfileName(CUSTOM_PROFILE).expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
new TestCase().customProfileFile(Ec2MetadataConfigProviderEndpointModeTest::customProfileFile)
.customProfileName(CUSTOM_PROFILE).expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6)
public static Stream<Arguments> testData() {
return Stream.of(
arguments(null, null, null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV4, null),
arguments("ipv4", null, null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV4, null),
arguments("IPv4", null, null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV4, null),
arguments("ipv6", null, null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
arguments("IPv6", null, null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
arguments("Ipv99", null, null, null, null, null, IllegalArgumentException.class),

arguments(null, "ipv4", null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV4, null),
arguments(null, "IPv4", null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV4, null),
arguments(null, "ipv6", null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
arguments(null, "IPv6", null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
arguments(null, "Ipv99", null, null, null, null, IllegalArgumentException.class),

arguments(null, null, TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv6", null, null,
Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
arguments(null, null, TEST_PROFILES_PATH_PREFIX + "endpoint_mode_invalidValue", null, null, null,
IllegalArgumentException.class),

// System property takes highest precedence
arguments("ipv4", "ipv6", null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
arguments(null, "ipv6", TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv4", null, null,
Ec2MetadataConfigProvider.EndpointMode.IPV6, null),

// env var has higher precedence than shared config
arguments("ipv6", null, TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv4", null, null,
Ec2MetadataConfigProvider.EndpointMode.IPV6, null),

// Test custom profile supplier and custom profile name
arguments(null, null, TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv6_custom_profile", null, CUSTOM_PROFILE,
Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
arguments(null, null, null, customProfileFile(), CUSTOM_PROFILE, Ec2MetadataConfigProvider.EndpointMode.IPV6, null)
);
}

@Before
public void setup() {
@BeforeEach
@AfterEach
public void cleanup() {
ENVIRONMENT_VARIABLE_HELPER.reset();
System.clearProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE.property());
}

if (testCase.envEndpointMode != null) {
@ParameterizedTest
@MethodSource("testData")
void resolvesCorrectEndpointMode(String envEndpointMode,
String systemPropertyEndpointMode,
String sharedConfigFile,
ProfileFile customProfileFile,
String customProfileName,
Ec2MetadataConfigProvider.EndpointMode expectedEndpointMode,
Class<? extends Throwable> expectedException) {
if (envEndpointMode != null) {
ENVIRONMENT_VARIABLE_HELPER.set(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE.environmentVariable(),
testCase.envEndpointMode);
envEndpointMode);
}

if (testCase.systemPropertyEndpointMode != null) {
if (systemPropertyEndpointMode != null) {
System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE.property(),
testCase.systemPropertyEndpointMode);
systemPropertyEndpointMode);
}
if (testCase.sharedConfigFile != null) {
if (sharedConfigFile != null) {
ENVIRONMENT_VARIABLE_HELPER.set(ProfileFileSystemSetting.AWS_CONFIG_FILE.environmentVariable(),
getTestFilePath(testCase.sharedConfigFile));
getTestFilePath(sharedConfigFile));
}

if (testCase.expectedException != null) {
thrown.expect(testCase.expectedException);
}
}

@After
public void teardown() {
ENVIRONMENT_VARIABLE_HELPER.reset();
System.clearProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE.property());
}

@Test
public void resolvesCorrectEndpointMode() {
Ec2MetadataConfigProvider configProvider = Ec2MetadataConfigProvider.builder()
.profileFile(testCase.customProfileFile)
.profileName(testCase.customProfileName)
.build();

assertThat(configProvider.getEndpointMode()).isEqualTo(testCase.expectedEndpointMode);
.profileFile(customProfileFile == null ? null :
() -> customProfileFile)
.profileName(customProfileName)
.build();

if (expectedException != null) {
assertThatThrownBy(configProvider::getEndpointMode).isInstanceOf(expectedException);
} else {
assertThat(configProvider.getEndpointMode()).isEqualTo(expectedEndpointMode);
}
}

private static String getTestFilePath(String testFile) {
Expand All @@ -137,65 +129,4 @@ private static ProfileFile customProfileFile() {
.content(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)))
.build();
}

private static class TestCase {
private String envEndpointMode;
private String systemPropertyEndpointMode;

private String sharedConfigFile;

private Supplier<ProfileFile> customProfileFile;

private String customProfileName;

private Ec2MetadataConfigProvider.EndpointMode expectedEndpointMode;
private Class<? extends Throwable> expectedException;

public TestCase envEndpointMode(String envEndpointMode) {
this.envEndpointMode = envEndpointMode;
return this;
}
public TestCase systemPropertyEndpointMode(String systemPropertyEndpointMode) {
this.systemPropertyEndpointMode = systemPropertyEndpointMode;
return this;
}

public TestCase sharedConfigFile(String sharedConfigFile) {
this.sharedConfigFile = sharedConfigFile;
return this;
}

public TestCase customProfileFile(Supplier<ProfileFile> customProfileFile) {
this.customProfileFile = customProfileFile;
return this;
}

private TestCase customProfileName(String customProfileName) {
this.customProfileName = customProfileName;
return this;
}

public TestCase expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode expectedEndpointMode) {
this.expectedEndpointMode = expectedEndpointMode;
return this;
}

public TestCase expectedException(Class<? extends Throwable> expectedException) {
this.expectedException = expectedException;
return this;
}

@Override
public String toString() {
return "TestCase{" +
"envEndpointMode='" + envEndpointMode + '\'' +
", systemPropertyEndpointMode='" + systemPropertyEndpointMode + '\'' +
", sharedConfigFile='" + sharedConfigFile + '\'' +
", customProfileFile=" + customProfileFile +
", customProfileName='" + customProfileName + '\'' +
", expectedEndpointMode=" + expectedEndpointMode +
", expectedException=" + expectedException +
'}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@
package software.amazon.awssdk.core.internal.http.pipeline.stages;

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

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import software.amazon.awssdk.core.SdkRequest;
import software.amazon.awssdk.core.SdkRequestOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
Expand All @@ -40,28 +37,21 @@
import software.amazon.awssdk.http.SdkHttpFullRequest;
import utils.ValidSdkObjects;

@RunWith(Parameterized.class)
public class MergeCustomHeadersStageTest {
// List of headers that may appear only once in a request; i.e. is not a list of values.
// Taken from https://github.com/apache/httpcomponents-client/blob/81c1bc4dc3ca5a3134c5c60e8beff08be2fd8792/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/HttpTestUtils.java#L69-L85 with modifications:
// Taken from https://github.com/apache/httpcomponents-client/blob/81c1bc4dc3ca5a3134c5c60e8beff08be2fd8792/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/HttpTestUtils.java#L69-L85
// with modifications:
// removed: accept-ranges, if-match, if-none-match, vary since it looks like they're defined as lists
private static final Set<String> SINGLE_HEADERS = Stream.of("age", "authorization",
"content-length", "content-location", "content-md5", "content-range", "content-type",
"date", "etag", "expires", "from", "host", "if-modified-since", "if-range",
"if-unmodified-since", "last-modified", "location", "max-forwards",
"proxy-authorization", "range", "referer", "retry-after", "server", "user-agent")
.collect(Collectors.toSet());

@Parameterized.Parameters(name = "Header = {0}")
public static Collection<Object> data() {
return Arrays.asList(SINGLE_HEADERS.toArray(new Object[0]));
public static Stream<String> singleHeaders() {
return Stream.of("age", "authorization", "content-length", "content-location", "content-md5", "content-range",
"content-type", "date", "etag", "expires", "from", "host", "if-modified-since", "if-range",
"if-unmodified-since", "last-modified", "location", "max-forwards", "proxy-authorization", "range",
"referer", "retry-after", "server", "user-agent");
}

@Parameterized.Parameter
public String singleHeaderName;

@Test
public void singleHeader_inMarshalledRequest_overriddenOnClient() throws Exception {
@ParameterizedTest
@MethodSource("singleHeaders")
void singleHeader_inMarshalledRequest_overriddenOnClient(String singleHeaderName) throws Exception {
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder();

RequestExecutionContext ctx = requestContext(NoopTestRequest.builder().build());
Expand All @@ -82,8 +72,9 @@ public void singleHeader_inMarshalledRequest_overriddenOnClient() throws Excepti
assertThat(requestBuilder.headers().get(singleHeaderName)).containsExactly("client");
}

@Test
public void singleHeader_inMarshalledRequest_overriddenOnRequest() throws Exception {
@ParameterizedTest
@MethodSource("singleHeaders")
void singleHeader_inMarshalledRequest_overriddenOnRequest(String singleHeaderName) throws Exception {
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder();
requestBuilder.putHeader(singleHeaderName, "marshaller");

Expand All @@ -104,8 +95,9 @@ public void singleHeader_inMarshalledRequest_overriddenOnRequest() throws Except
assertThat(requestBuilder.headers().get(singleHeaderName)).containsExactly("request");
}

@Test
public void singleHeader_inClient_overriddenOnRequest() throws Exception {
@ParameterizedTest
@MethodSource("singleHeaders")
void singleHeader_inClient_overriddenOnRequest(String singleHeaderName) throws Exception {
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder();

RequestExecutionContext ctx = requestContext(NoopTestRequest.builder()
Expand All @@ -127,8 +119,9 @@ public void singleHeader_inClient_overriddenOnRequest() throws Exception {
assertThat(requestBuilder.headers().get(singleHeaderName)).containsExactly("request");
}

@Test
public void singleHeader_inMarshalledRequest_inClient_inRequest() throws Exception {
@ParameterizedTest
@MethodSource("singleHeaders")
void singleHeader_inMarshalledRequest_inClient_inRequest(String singleHeaderName) throws Exception {
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder();
requestBuilder.putHeader(singleHeaderName, "marshaller");

Expand All @@ -151,8 +144,9 @@ public void singleHeader_inMarshalledRequest_inClient_inRequest() throws Excepti
assertThat(requestBuilder.headers().get(singleHeaderName)).containsExactly("request");
}

@Test
public void singleHeader_inRequestAsList_keepsMultipleValues() throws Exception {
@ParameterizedTest
@MethodSource("singleHeaders")
void singleHeader_inRequestAsList_keepsMultipleValues(String singleHeaderName) throws Exception {
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder();
requestBuilder.putHeader(singleHeaderName, "marshaller");

Expand Down
Loading