Skip to content

Commit a437d3c

Browse files
authored
Merge pull request #1863 from aws/millem/fix-release-3
Revert "[JUnit 5] Migrate some parameterized tests to JUnit 5 (#2880)"
2 parents 7850df9 + 3ae4210 commit a437d3c

File tree

4 files changed

+305
-191
lines changed

4 files changed

+305
-191
lines changed

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

+140-71
Original file line numberDiff line numberDiff line change
@@ -16,106 +16,114 @@
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-
2219
import java.io.ByteArrayInputStream;
2320
import java.nio.charset.StandardCharsets;
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;
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;
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)
3536
public class Ec2MetadataConfigProviderEndpointModeTest {
3637
private static final String TEST_PROFILES_PATH_PREFIX = "/software/amazon/awssdk/auth/credentials/internal/ec2metadataconfigprovider/";
3738
private static final EnvironmentVariableHelper ENVIRONMENT_VARIABLE_HELPER = new EnvironmentVariableHelper();
3839
private static final String CUSTOM_PROFILE = "myprofile";
3940

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)
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)
7384
);
7485
}
7586

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

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) {
92+
if (testCase.envEndpointMode != null) {
9393
ENVIRONMENT_VARIABLE_HELPER.set(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE.environmentVariable(),
94-
envEndpointMode);
94+
testCase.envEndpointMode);
9595
}
9696

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

106-
Ec2MetadataConfigProvider configProvider = Ec2MetadataConfigProvider.builder()
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);
106+
if (testCase.expectedException != null) {
107+
thrown.expect(testCase.expectedException);
116108
}
117109
}
118110

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() {
119+
Ec2MetadataConfigProvider configProvider = Ec2MetadataConfigProvider.builder()
120+
.profileFile(testCase.customProfileFile)
121+
.profileName(testCase.customProfileName)
122+
.build();
123+
124+
assertThat(configProvider.getEndpointMode()).isEqualTo(testCase.expectedEndpointMode);
125+
}
126+
119127
private static String getTestFilePath(String testFile) {
120128
return Ec2MetadataConfigProviderEndpointModeTest.class.getResource(testFile).getFile();
121129
}
@@ -129,4 +137,65 @@ private static ProfileFile customProfileFile() {
129137
.content(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)))
130138
.build();
131139
}
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+
}
132201
}

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

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

1818
import static org.assertj.core.api.Assertions.assertThat;
19-
2019
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;
2527
import java.util.stream.Stream;
26-
import org.junit.jupiter.params.ParameterizedTest;
27-
import org.junit.jupiter.params.provider.MethodSource;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.Parameterized;
2831
import software.amazon.awssdk.core.SdkRequest;
2932
import software.amazon.awssdk.core.SdkRequestOverrideConfiguration;
3033
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
@@ -37,21 +40,28 @@
3740
import software.amazon.awssdk.http.SdkHttpFullRequest;
3841
import utils.ValidSdkObjects;
3942

43+
@RunWith(Parameterized.class)
4044
public class MergeCustomHeadersStageTest {
4145
// List of headers that may appear only once in a request; i.e. is not a list of values.
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:
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:
4447
// removed: accept-ranges, if-match, if-none-match, vary since it looks like they're defined as lists
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");
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]));
5058
}
5159

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

5767
RequestExecutionContext ctx = requestContext(NoopTestRequest.builder().build());
@@ -72,9 +82,8 @@ void singleHeader_inMarshalledRequest_overriddenOnClient(String singleHeaderName
7282
assertThat(requestBuilder.headers().get(singleHeaderName)).containsExactly("client");
7383
}
7484

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

@@ -95,9 +104,8 @@ void singleHeader_inMarshalledRequest_overriddenOnRequest(String singleHeaderNam
95104
assertThat(requestBuilder.headers().get(singleHeaderName)).containsExactly("request");
96105
}
97106

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

103111
RequestExecutionContext ctx = requestContext(NoopTestRequest.builder()
@@ -119,9 +127,8 @@ void singleHeader_inClient_overriddenOnRequest(String singleHeaderName) throws E
119127
assertThat(requestBuilder.headers().get(singleHeaderName)).containsExactly("request");
120128
}
121129

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

@@ -144,9 +151,8 @@ void singleHeader_inMarshalledRequest_inClient_inRequest(String singleHeaderName
144151
assertThat(requestBuilder.headers().get(singleHeaderName)).containsExactly("request");
145152
}
146153

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

0 commit comments

Comments
 (0)