Skip to content

Commit e2915d0

Browse files
[JUnit 5] Migrate some parameterized tests to JUnit 5 (#2880)
* [JUnit 5] Migrate some parameterized tests to JUnit 5 JUnit 4 -> 5 migrations are often done in a 3-step process: 1. Update dependencies to JUnit 5, including the vintage module, causing all tests to leverage the new JUnit 5 Platform 2. Incrementally migrate JUnit 4 tests to JUnit 5 tests 3. Once no JUnit 4 usage remains, remove the dependency on the vintage module This PR continues step 2 by migrating some parameterized tests to JUnit 5.
1 parent 68f0123 commit e2915d0

File tree

4 files changed

+191
-305
lines changed

4 files changed

+191
-305
lines changed

core/auth/src/test/java/software/amazon/awssdk/auth/credentials/internal/Ec2MetadataConfigProviderEndpointModeTest.java

Lines changed: 71 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -16,112 +16,104 @@
1616
package software.amazon.awssdk.auth.credentials.internal;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
20+
import static org.junit.jupiter.params.provider.Arguments.arguments;
21+
1922
import java.io.ByteArrayInputStream;
2023
import java.nio.charset.StandardCharsets;
21-
import java.util.Arrays;
22-
import java.util.function.Supplier;
23-
import org.junit.After;
24-
import org.junit.Before;
25-
import org.junit.Rule;
26-
import org.junit.Test;
27-
import org.junit.rules.ExpectedException;
28-
import org.junit.runner.RunWith;
29-
import org.junit.runners.Parameterized;
24+
import java.util.stream.Stream;
25+
import org.junit.jupiter.api.AfterEach;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.params.ParameterizedTest;
28+
import org.junit.jupiter.params.provider.Arguments;
29+
import org.junit.jupiter.params.provider.MethodSource;
3030
import software.amazon.awssdk.core.SdkSystemSetting;
3131
import software.amazon.awssdk.profiles.ProfileFile;
3232
import software.amazon.awssdk.profiles.ProfileFileSystemSetting;
3333
import software.amazon.awssdk.testutils.EnvironmentVariableHelper;
3434

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

41-
@Parameterized.Parameter
42-
public TestCase testCase;
43-
44-
@Rule
45-
public ExpectedException thrown = ExpectedException.none();
46-
47-
@Parameterized.Parameters(name = "{0}")
48-
public static Iterable<Object> testCases() {
49-
return Arrays.asList(
50-
new TestCase().expectedEndpointMode(null).expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV4),
51-
52-
new TestCase().envEndpointMode("ipv4").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV4),
53-
new TestCase().envEndpointMode("IPv4").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV4),
54-
new TestCase().envEndpointMode("ipv6").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
55-
new TestCase().envEndpointMode("IPv6").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
56-
new TestCase().envEndpointMode("Ipv99").expectedException(IllegalArgumentException.class),
57-
58-
new TestCase().systemPropertyEndpointMode("ipv4").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV4),
59-
new TestCase().systemPropertyEndpointMode("IPv4").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV4),
60-
new TestCase().systemPropertyEndpointMode("ipv6").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
61-
new TestCase().systemPropertyEndpointMode("IPv6").expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
62-
new TestCase().systemPropertyEndpointMode("Ipv99").expectedException(IllegalArgumentException.class),
63-
64-
new TestCase().sharedConfigFile(TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv6")
65-
.expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
66-
new TestCase().sharedConfigFile(TEST_PROFILES_PATH_PREFIX + "endpoint_mode_invalidValue")
67-
.expectedException(IllegalArgumentException.class),
68-
69-
// System property takes highest precedence
70-
new TestCase().systemPropertyEndpointMode("ipv6").envEndpointMode("ipv4")
71-
.expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
72-
new TestCase().systemPropertyEndpointMode("ipv6").sharedConfigFile(TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv4")
73-
.expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
74-
75-
// env var has higher precedence than shared config
76-
new TestCase().envEndpointMode("ipv6").sharedConfigFile(TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv4")
77-
.expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
78-
79-
// Test custom profile supplier and custom profile name
80-
new TestCase().sharedConfigFile(TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv6_custom_profile")
81-
.customProfileName(CUSTOM_PROFILE).expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6),
82-
new TestCase().customProfileFile(Ec2MetadataConfigProviderEndpointModeTest::customProfileFile)
83-
.customProfileName(CUSTOM_PROFILE).expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode.IPV6)
40+
public static Stream<Arguments> testData() {
41+
return Stream.of(
42+
arguments(null, null, null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV4, null),
43+
arguments("ipv4", null, null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV4, null),
44+
arguments("IPv4", null, null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV4, null),
45+
arguments("ipv6", null, null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
46+
arguments("IPv6", null, null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
47+
arguments("Ipv99", null, null, null, null, null, IllegalArgumentException.class),
48+
49+
arguments(null, "ipv4", null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV4, null),
50+
arguments(null, "IPv4", null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV4, null),
51+
arguments(null, "ipv6", null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
52+
arguments(null, "IPv6", null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
53+
arguments(null, "Ipv99", null, null, null, null, IllegalArgumentException.class),
54+
55+
arguments(null, null, TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv6", null, null,
56+
Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
57+
arguments(null, null, TEST_PROFILES_PATH_PREFIX + "endpoint_mode_invalidValue", null, null, null,
58+
IllegalArgumentException.class),
59+
60+
// System property takes highest precedence
61+
arguments("ipv4", "ipv6", null, null, null, Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
62+
arguments(null, "ipv6", TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv4", null, null,
63+
Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
64+
65+
// env var has higher precedence than shared config
66+
arguments("ipv6", null, TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv4", null, null,
67+
Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
68+
69+
// Test custom profile supplier and custom profile name
70+
arguments(null, null, TEST_PROFILES_PATH_PREFIX + "endpoint_mode_ipv6_custom_profile", null, CUSTOM_PROFILE,
71+
Ec2MetadataConfigProvider.EndpointMode.IPV6, null),
72+
arguments(null, null, null, customProfileFile(), CUSTOM_PROFILE, Ec2MetadataConfigProvider.EndpointMode.IPV6, null)
8473
);
8574
}
8675

87-
@Before
88-
public void setup() {
76+
@BeforeEach
77+
@AfterEach
78+
public void cleanup() {
8979
ENVIRONMENT_VARIABLE_HELPER.reset();
9080
System.clearProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE.property());
81+
}
9182

92-
if (testCase.envEndpointMode != null) {
83+
@ParameterizedTest
84+
@MethodSource("testData")
85+
void resolvesCorrectEndpointMode(String envEndpointMode,
86+
String systemPropertyEndpointMode,
87+
String sharedConfigFile,
88+
ProfileFile customProfileFile,
89+
String customProfileName,
90+
Ec2MetadataConfigProvider.EndpointMode expectedEndpointMode,
91+
Class<? extends Throwable> expectedException) {
92+
if (envEndpointMode != null) {
9393
ENVIRONMENT_VARIABLE_HELPER.set(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE.environmentVariable(),
94-
testCase.envEndpointMode);
94+
envEndpointMode);
9595
}
9696

97-
if (testCase.systemPropertyEndpointMode != null) {
97+
if (systemPropertyEndpointMode != null) {
9898
System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE.property(),
99-
testCase.systemPropertyEndpointMode);
99+
systemPropertyEndpointMode);
100100
}
101-
if (testCase.sharedConfigFile != null) {
101+
if (sharedConfigFile != null) {
102102
ENVIRONMENT_VARIABLE_HELPER.set(ProfileFileSystemSetting.AWS_CONFIG_FILE.environmentVariable(),
103-
getTestFilePath(testCase.sharedConfigFile));
103+
getTestFilePath(sharedConfigFile));
104104
}
105105

106-
if (testCase.expectedException != null) {
107-
thrown.expect(testCase.expectedException);
108-
}
109-
}
110-
111-
@After
112-
public void teardown() {
113-
ENVIRONMENT_VARIABLE_HELPER.reset();
114-
System.clearProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE.property());
115-
}
116-
117-
@Test
118-
public void resolvesCorrectEndpointMode() {
119106
Ec2MetadataConfigProvider configProvider = Ec2MetadataConfigProvider.builder()
120-
.profileFile(testCase.customProfileFile)
121-
.profileName(testCase.customProfileName)
122-
.build();
123-
124-
assertThat(configProvider.getEndpointMode()).isEqualTo(testCase.expectedEndpointMode);
107+
.profileFile(customProfileFile == null ? null :
108+
() -> customProfileFile)
109+
.profileName(customProfileName)
110+
.build();
111+
112+
if (expectedException != null) {
113+
assertThatThrownBy(configProvider::getEndpointMode).isInstanceOf(expectedException);
114+
} else {
115+
assertThat(configProvider.getEndpointMode()).isEqualTo(expectedEndpointMode);
116+
}
125117
}
126118

127119
private static String getTestFilePath(String testFile) {
@@ -137,65 +129,4 @@ private static ProfileFile customProfileFile() {
137129
.content(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)))
138130
.build();
139131
}
140-
141-
private static class TestCase {
142-
private String envEndpointMode;
143-
private String systemPropertyEndpointMode;
144-
145-
private String sharedConfigFile;
146-
147-
private Supplier<ProfileFile> customProfileFile;
148-
149-
private String customProfileName;
150-
151-
private Ec2MetadataConfigProvider.EndpointMode expectedEndpointMode;
152-
private Class<? extends Throwable> expectedException;
153-
154-
public TestCase envEndpointMode(String envEndpointMode) {
155-
this.envEndpointMode = envEndpointMode;
156-
return this;
157-
}
158-
public TestCase systemPropertyEndpointMode(String systemPropertyEndpointMode) {
159-
this.systemPropertyEndpointMode = systemPropertyEndpointMode;
160-
return this;
161-
}
162-
163-
public TestCase sharedConfigFile(String sharedConfigFile) {
164-
this.sharedConfigFile = sharedConfigFile;
165-
return this;
166-
}
167-
168-
public TestCase customProfileFile(Supplier<ProfileFile> customProfileFile) {
169-
this.customProfileFile = customProfileFile;
170-
return this;
171-
}
172-
173-
private TestCase customProfileName(String customProfileName) {
174-
this.customProfileName = customProfileName;
175-
return this;
176-
}
177-
178-
public TestCase expectedEndpointMode(Ec2MetadataConfigProvider.EndpointMode expectedEndpointMode) {
179-
this.expectedEndpointMode = expectedEndpointMode;
180-
return this;
181-
}
182-
183-
public TestCase expectedException(Class<? extends Throwable> expectedException) {
184-
this.expectedException = expectedException;
185-
return this;
186-
}
187-
188-
@Override
189-
public String toString() {
190-
return "TestCase{" +
191-
"envEndpointMode='" + envEndpointMode + '\'' +
192-
", systemPropertyEndpointMode='" + systemPropertyEndpointMode + '\'' +
193-
", sharedConfigFile='" + sharedConfigFile + '\'' +
194-
", customProfileFile=" + customProfileFile +
195-
", customProfileName='" + customProfileName + '\'' +
196-
", expectedEndpointMode=" + expectedEndpointMode +
197-
", expectedException=" + expectedException +
198-
'}';
199-
}
200-
}
201132
}

core/sdk-core/src/test/java/software/amazon/awssdk/core/internal/http/pipeline/stages/MergeCustomHeadersStageTest.java

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@
1616
package software.amazon.awssdk.core.internal.http.pipeline.stages;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
19+
1920
import java.util.Arrays;
20-
import java.util.Collection;
2121
import java.util.Collections;
2222
import java.util.HashMap;
2323
import java.util.List;
2424
import java.util.Map;
25-
import java.util.Set;
26-
import java.util.stream.Collectors;
2725
import java.util.stream.Stream;
28-
import org.junit.Test;
29-
import org.junit.runner.RunWith;
30-
import org.junit.runners.Parameterized;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.MethodSource;
3128
import software.amazon.awssdk.core.SdkRequest;
3229
import software.amazon.awssdk.core.SdkRequestOverrideConfiguration;
3330
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
@@ -40,28 +37,21 @@
4037
import software.amazon.awssdk.http.SdkHttpFullRequest;
4138
import utils.ValidSdkObjects;
4239

43-
@RunWith(Parameterized.class)
4440
public class MergeCustomHeadersStageTest {
4541
// List of headers that may appear only once in a request; i.e. is not a list of values.
46-
// 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:
42+
// 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
43+
// with modifications:
4744
// removed: accept-ranges, if-match, if-none-match, vary since it looks like they're defined as lists
48-
private static final Set<String> SINGLE_HEADERS = Stream.of("age", "authorization",
49-
"content-length", "content-location", "content-md5", "content-range", "content-type",
50-
"date", "etag", "expires", "from", "host", "if-modified-since", "if-range",
51-
"if-unmodified-since", "last-modified", "location", "max-forwards",
52-
"proxy-authorization", "range", "referer", "retry-after", "server", "user-agent")
53-
.collect(Collectors.toSet());
54-
55-
@Parameterized.Parameters(name = "Header = {0}")
56-
public static Collection<Object> data() {
57-
return Arrays.asList(SINGLE_HEADERS.toArray(new Object[0]));
45+
public static Stream<String> singleHeaders() {
46+
return Stream.of("age", "authorization", "content-length", "content-location", "content-md5", "content-range",
47+
"content-type", "date", "etag", "expires", "from", "host", "if-modified-since", "if-range",
48+
"if-unmodified-since", "last-modified", "location", "max-forwards", "proxy-authorization", "range",
49+
"referer", "retry-after", "server", "user-agent");
5850
}
5951

60-
@Parameterized.Parameter
61-
public String singleHeaderName;
62-
63-
@Test
64-
public void singleHeader_inMarshalledRequest_overriddenOnClient() throws Exception {
52+
@ParameterizedTest
53+
@MethodSource("singleHeaders")
54+
void singleHeader_inMarshalledRequest_overriddenOnClient(String singleHeaderName) throws Exception {
6555
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder();
6656

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

85-
@Test
86-
public void singleHeader_inMarshalledRequest_overriddenOnRequest() throws Exception {
75+
@ParameterizedTest
76+
@MethodSource("singleHeaders")
77+
void singleHeader_inMarshalledRequest_overriddenOnRequest(String singleHeaderName) throws Exception {
8778
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder();
8879
requestBuilder.putHeader(singleHeaderName, "marshaller");
8980

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

107-
@Test
108-
public void singleHeader_inClient_overriddenOnRequest() throws Exception {
98+
@ParameterizedTest
99+
@MethodSource("singleHeaders")
100+
void singleHeader_inClient_overriddenOnRequest(String singleHeaderName) throws Exception {
109101
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder();
110102

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

130-
@Test
131-
public void singleHeader_inMarshalledRequest_inClient_inRequest() throws Exception {
122+
@ParameterizedTest
123+
@MethodSource("singleHeaders")
124+
void singleHeader_inMarshalledRequest_inClient_inRequest(String singleHeaderName) throws Exception {
132125
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder();
133126
requestBuilder.putHeader(singleHeaderName, "marshaller");
134127

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

154-
@Test
155-
public void singleHeader_inRequestAsList_keepsMultipleValues() throws Exception {
147+
@ParameterizedTest
148+
@MethodSource("singleHeaders")
149+
void singleHeader_inRequestAsList_keepsMultipleValues(String singleHeaderName) throws Exception {
156150
SdkHttpFullRequest.Builder requestBuilder = SdkHttpFullRequest.builder();
157151
requestBuilder.putHeader(singleHeaderName, "marshaller");
158152

0 commit comments

Comments
 (0)