Skip to content

Commit 08f4068

Browse files
authored
HADOOP-19416. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-kms. (#7637)
1 parent 5770647 commit 08f4068

File tree

7 files changed

+378
-380
lines changed

7 files changed

+378
-380
lines changed

hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMS.java

+243-246
Large diffs are not rendered by default.

hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSACLs.java

+35-36
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@
2020
import static org.apache.hadoop.crypto.key.kms.server.KMSConfiguration.*;
2121
import static org.apache.hadoop.crypto.key.kms.server.KeyAuthorizationKeyProvider.KEY_ACL;
2222
import static org.apache.hadoop.crypto.key.kms.server.KeyAuthorizationKeyProvider.KeyOpType;
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertFalse;
25+
import static org.junit.jupiter.api.Assertions.assertNotNull;
26+
import static org.junit.jupiter.api.Assertions.assertTrue;
2327

2428
import org.apache.hadoop.conf.Configuration;
2529
import org.apache.hadoop.security.UserGroupInformation;
2630
import org.apache.hadoop.security.authorize.AccessControlList;
27-
import org.junit.Assert;
28-
import org.junit.Rule;
29-
import org.junit.rules.Timeout;
30-
import org.junit.Test;
31+
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.api.Timeout;
3133

3234
import java.util.Collection;
3335
import java.util.HashMap;
3436
import java.util.HashSet;
3537

38+
@Timeout(180)
3639
public class TestKMSACLs {
37-
@Rule
38-
public final Timeout globalTimeout = new Timeout(180000);
39-
4040
@Test
4141
public void testDefaults() {
4242
final KMSACLs acls = new KMSACLs(new Configuration(false));
4343
for (KMSACLs.Type type : KMSACLs.Type.values()) {
44-
Assert.assertTrue(acls.hasAccess(type,
44+
assertTrue(acls.hasAccess(type,
4545
UserGroupInformation.createRemoteUser("foo")));
4646
}
4747
}
@@ -54,9 +54,9 @@ public void testCustom() {
5454
}
5555
final KMSACLs acls = new KMSACLs(conf);
5656
for (KMSACLs.Type type : KMSACLs.Type.values()) {
57-
Assert.assertTrue(acls.hasAccess(type,
57+
assertTrue(acls.hasAccess(type,
5858
UserGroupInformation.createRemoteUser(type.toString())));
59-
Assert.assertFalse(acls.hasAccess(type,
59+
assertFalse(acls.hasAccess(type,
6060
UserGroupInformation.createRemoteUser("foo")));
6161
}
6262
}
@@ -72,16 +72,16 @@ public void testKeyAclConfigurationLoad() {
7272
conf.set(DEFAULT_KEY_ACL_PREFIX + "ALL", "invalid");
7373
conf.set(WHITELIST_KEY_ACL_PREFIX + "ALL", "invalid");
7474
final KMSACLs acls = new KMSACLs(conf);
75-
Assert.assertTrue("expected key ACL size is 2 but got "
76-
+ acls.keyAcls.size(), acls.keyAcls.size() == 2);
77-
Assert.assertTrue("expected whitelist ACL size is 1 but got "
78-
+ acls.whitelistKeyAcls.size(), acls.whitelistKeyAcls.size() == 1);
79-
Assert.assertFalse("ALL should not be allowed for whitelist ACLs.",
80-
acls.whitelistKeyAcls.containsKey(KeyOpType.ALL));
81-
Assert.assertTrue("expected default ACL size is 1 but got "
82-
+ acls.defaultKeyAcls.size(), acls.defaultKeyAcls.size() == 1);
83-
Assert.assertTrue("ALL should not be allowed for default ACLs.",
84-
acls.defaultKeyAcls.size() == 1);
75+
assertTrue(acls.keyAcls.size() == 2, "expected key ACL size is 2 but got "
76+
+ acls.keyAcls.size());
77+
assertTrue(acls.whitelistKeyAcls.size() == 1, "expected whitelist ACL size is 1 but got "
78+
+ acls.whitelistKeyAcls.size());
79+
assertFalse(acls.whitelistKeyAcls.containsKey(KeyOpType.ALL),
80+
"ALL should not be allowed for whitelist ACLs.");
81+
assertTrue(acls.defaultKeyAcls.size() == 1, "expected default ACL size is 1 but got "
82+
+ acls.defaultKeyAcls.size());
83+
assertTrue(acls.defaultKeyAcls.size() == 1,
84+
"ALL should not be allowed for default ACLs.");
8585
}
8686

8787
@Test
@@ -98,15 +98,15 @@ public void testKeyAclDuplicateEntries() {
9898
conf.set(WHITELIST_KEY_ACL_PREFIX + "DECRYPT_EEK", "whitelist1");
9999
conf.set(WHITELIST_KEY_ACL_PREFIX + "DECRYPT_EEK", "*");
100100
final KMSACLs acls = new KMSACLs(conf);
101-
Assert.assertTrue("expected key ACL size is 2 but got "
102-
+ acls.keyAcls.size(), acls.keyAcls.size() == 2);
101+
assertTrue(acls.keyAcls.size() == 2, "expected key ACL size is 2 but got "
102+
+ acls.keyAcls.size());
103103
assertKeyAcl("test_key_1", acls, KeyOpType.DECRYPT_EEK, "decrypt2");
104104
assertKeyAcl("test_key_2", acls, KeyOpType.ALL, "all1", "all3");
105105
assertDefaultKeyAcl(acls, KeyOpType.MANAGEMENT);
106106
assertDefaultKeyAcl(acls, KeyOpType.DECRYPT_EEK);
107107
AccessControlList acl = acls.whitelistKeyAcls.get(KeyOpType.DECRYPT_EEK);
108-
Assert.assertNotNull(acl);
109-
Assert.assertTrue(acl.isAllAllowed());
108+
assertNotNull(acl);
109+
assertTrue(acl.isAllAllowed());
110110
}
111111

112112
@Test
@@ -163,8 +163,8 @@ public void testKeyAclReload() {
163163
conf.set(DEFAULT_KEY_ACL_PREFIX + "DECRYPT_EEK", "*");
164164
acls.setKeyACLs(conf);
165165
AccessControlList acl = acls.defaultKeyAcls.get(KeyOpType.DECRYPT_EEK);
166-
Assert.assertTrue(acl.isAllAllowed());
167-
Assert.assertTrue(acl.getUsers().isEmpty());
166+
assertTrue(acl.isAllAllowed());
167+
assertTrue(acl.getUsers().isEmpty());
168168
// everything else should still be the same.
169169
assertDefaultKeyAcl(acls, KeyOpType.READ, "read2");
170170
assertDefaultKeyAcl(acls, KeyOpType.MANAGEMENT, "mgmt1", "mgmt2");
@@ -181,10 +181,10 @@ public void testKeyAclReload() {
181181
conf.set(DEFAULT_KEY_ACL_PREFIX + "DECRYPT_EEK", "new");
182182
acls.setKeyACLs(conf);
183183
assertDefaultKeyAcl(acls, KeyOpType.DECRYPT_EEK, "new");
184-
Assert.assertTrue(acls.keyAcls.isEmpty());
185-
Assert.assertTrue(acls.whitelistKeyAcls.isEmpty());
186-
Assert.assertEquals("Got unexpected sized acls:"
187-
+ acls.defaultKeyAcls, 1, acls.defaultKeyAcls.size());
184+
assertTrue(acls.keyAcls.isEmpty());
185+
assertTrue(acls.whitelistKeyAcls.isEmpty());
186+
assertEquals(1, acls.defaultKeyAcls.size(), "Got unexpected sized acls:"
187+
+ acls.defaultKeyAcls);
188188
}
189189

190190
private void assertDefaultKeyAcl(final KMSACLs acls, final KeyOpType op,
@@ -201,23 +201,22 @@ private void assertWhitelistKeyAcl(final KMSACLs acls, final KeyOpType op,
201201

202202
private void assertKeyAcl(final String keyName, final KMSACLs acls,
203203
final KeyOpType op, final String... names) {
204-
Assert.assertTrue(acls.keyAcls.containsKey(keyName));
204+
assertTrue(acls.keyAcls.containsKey(keyName));
205205
final HashMap<KeyOpType, AccessControlList> keyacl =
206206
acls.keyAcls.get(keyName);
207-
Assert.assertNotNull(keyacl.get(op));
207+
assertNotNull(keyacl.get(op));
208208
assertAcl(keyacl.get(op), op, names);
209209
}
210210

211211
private void assertAcl(final AccessControlList acl,
212212
final KeyOpType op, final String... names) {
213-
Assert.assertNotNull(acl);
214-
Assert.assertFalse(acl.isAllAllowed());
213+
assertNotNull(acl);
214+
assertFalse(acl.isAllAllowed());
215215
final Collection<String> actual = acl.getUsers();
216216
final HashSet<String> expected = new HashSet<>();
217217
for (String name : names) {
218218
expected.add(name);
219219
}
220-
Assert.assertEquals("defaultKeyAcls don't match for op:" + op,
221-
expected, actual);
220+
assertEquals(expected, actual, "defaultKeyAcls don't match for op:" + op);
222221
}
223222
}

hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSAudit.java

+19-20
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.io.OutputStream;
2525
import java.io.PrintStream;
2626
import java.util.List;
27-
import java.util.concurrent.TimeUnit;
2827

2928
import org.apache.commons.lang3.reflect.FieldUtils;
3029
import org.apache.hadoop.conf.Configuration;
@@ -35,13 +34,16 @@
3534
import org.apache.hadoop.util.ThreadUtil;
3635
import org.apache.log4j.LogManager;
3736
import org.apache.log4j.PropertyConfigurator;
38-
import org.junit.After;
39-
import org.junit.Assert;
40-
import org.junit.Before;
41-
import org.junit.Rule;
42-
import org.junit.Test;
43-
import org.junit.rules.Timeout;
37+
import org.junit.jupiter.api.AfterEach;
38+
import org.junit.jupiter.api.BeforeEach;
39+
import org.junit.jupiter.api.Test;;
40+
import org.junit.jupiter.api.Timeout;
4441

42+
import static org.junit.jupiter.api.Assertions.assertEquals;
43+
import static org.junit.jupiter.api.Assertions.assertTrue;
44+
import static org.junit.jupiter.api.Assertions.fail;
45+
46+
@Timeout(180)
4547
public class TestKMSAudit {
4648

4749
private PrintStream originalOut;
@@ -63,10 +65,7 @@ public void setOutputStream(OutputStream out) {
6365
}
6466
}
6567

66-
@Rule
67-
public final Timeout testTimeout = new Timeout(180000L, TimeUnit.MILLISECONDS);
68-
69-
@Before
68+
@BeforeEach
7069
public void setUp() throws IOException {
7170
originalOut = System.err;
7271
memOut = new ByteArrayOutputStream();
@@ -81,7 +80,7 @@ public void setUp() throws IOException {
8180
this.kmsAudit = new KMSAudit(conf);
8281
}
8382

84-
@After
83+
@AfterEach
8584
public void cleanUp() {
8685
System.setErr(originalOut);
8786
LogManager.resetConfiguration();
@@ -138,7 +137,7 @@ public void testAggregation() throws Exception {
138137
+ "OK\\[op=REENCRYPT_EEK_BATCH, key=k1, user=luser@REALM\\] testmsg"
139138
+ "OK\\[op=REENCRYPT_EEK_BATCH, key=k1, user=luser@REALM\\] "
140139
+ "testmsg");
141-
Assert.assertTrue(doesMatch);
140+
assertTrue(doesMatch);
142141
}
143142

144143
@Test
@@ -179,7 +178,7 @@ public void testAggregationUnauth() throws Exception {
179178
+ " interval=[^m]{1,4}ms\\] testmsg"
180179
+ "OK\\[op=GENERATE_EEK, key=k3, user=luser@REALM, accessCount=1,"
181180
+ " interval=[^m]{1,4}ms\\] testmsg");
182-
Assert.assertTrue(doesMatch);
181+
assertTrue(doesMatch);
183182
}
184183

185184
@Test
@@ -192,7 +191,7 @@ public void testAuditLogFormat() throws Exception {
192191
kmsAudit.unauthenticated("remotehost", "method", "url", "testmsg");
193192
String out = getAndResetLogOutput();
194193
System.out.println(out);
195-
Assert.assertTrue(out.matches(
194+
assertTrue(out.matches(
196195
"OK\\[op=GENERATE_EEK, key=k4, user=luser@REALM, accessCount=1, "
197196
+ "interval=[^m]{1,4}ms\\] testmsg"
198197
+ "OK\\[op=GENERATE_EEK, user=luser@REALM\\] testmsg"
@@ -211,8 +210,8 @@ public void testInitAuditLoggers() throws Exception {
211210
List<KMSAuditLogger> loggers = (List<KMSAuditLogger>) FieldUtils.
212211
getField(KMSAudit.class, "auditLoggers", true).get(kmsAudit);
213212

214-
Assert.assertEquals(1, loggers.size());
215-
Assert.assertEquals(SimpleKMSAuditLogger.class, loggers.get(0).getClass());
213+
assertEquals(1, loggers.size());
214+
assertEquals(SimpleKMSAuditLogger.class, loggers.get(0).getClass());
216215

217216
// Explicitly configure the simple logger. Duplicates are ignored.
218217
final Configuration conf = new Configuration();
@@ -222,15 +221,15 @@ public void testInitAuditLoggers() throws Exception {
222221
final KMSAudit audit = new KMSAudit(conf);
223222
loggers = (List<KMSAuditLogger>) FieldUtils.
224223
getField(KMSAudit.class, "auditLoggers", true).get(kmsAudit);
225-
Assert.assertEquals(1, loggers.size());
226-
Assert.assertEquals(SimpleKMSAuditLogger.class, loggers.get(0).getClass());
224+
assertEquals(1, loggers.size());
225+
assertEquals(SimpleKMSAuditLogger.class, loggers.get(0).getClass());
227226

228227
// If any loggers unable to load, init should fail.
229228
conf.set(KMSConfiguration.KMS_AUDIT_LOGGER_KEY,
230229
SimpleKMSAuditLogger.class.getName() + ",unknown");
231230
try {
232231
new KMSAudit(conf);
233-
Assert.fail("loggers configured but invalid, init should fail.");
232+
fail("loggers configured but invalid, init should fail.");
234233
} catch (Exception ex) {
235234
GenericTestUtils
236235
.assertExceptionContains(KMSConfiguration.KMS_AUDIT_LOGGER_KEY, ex);

hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSAuthenticationFilter.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@
2323
.DelegationTokenAuthenticationHandler;
2424
import org.apache.hadoop.security.token.delegation.web
2525
.PseudoDelegationTokenAuthenticationHandler;
26-
import org.junit.Test;
26+
import org.junit.jupiter.api.Test;
2727
import java.util.Properties;
2828

29-
import static org.junit.Assert.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
3030

3131
/**
3232
* Test KMS Authentication Filter.
3333
*/
3434
public class TestKMSAuthenticationFilter {
3535

36-
@Test public void testConfiguration() throws Exception {
36+
@Test
37+
public void testConfiguration() throws Exception {
3738
Configuration conf = new Configuration();
3839
conf.set("hadoop.kms.authentication.type", "simple");
3940

hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSMDCFilter.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
*/
1818
package org.apache.hadoop.crypto.key.kms.server;
1919

20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertNull;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNull;
22+
import static org.mockito.Mockito.mock;
2223
import static org.mockito.Mockito.when;
2324

2425
import java.io.IOException;
@@ -30,9 +31,8 @@
3031
import javax.servlet.http.HttpServletRequest;
3132
import javax.servlet.http.HttpServletResponse;
3233

33-
import org.junit.Before;
34-
import org.junit.Test;
35-
import org.mockito.Mockito;
34+
import org.junit.jupiter.api.BeforeEach;
35+
import org.junit.jupiter.api.Test;
3636

3737
/**
3838
* Test for {@link KMSMDCFilter}.
@@ -48,11 +48,11 @@ public class TestKMSMDCFilter {
4848
private HttpServletRequest httpRequest;
4949
private HttpServletResponse httpResponse;
5050

51-
@Before
51+
@BeforeEach
5252
public void setUp() throws IOException {
5353
filter = new KMSMDCFilter();
54-
httpRequest = Mockito.mock(HttpServletRequest.class);
55-
httpResponse = Mockito.mock(HttpServletResponse.class);
54+
httpRequest = mock(HttpServletRequest.class);
55+
httpResponse = mock(HttpServletResponse.class);
5656
KMSMDCFilter.setContext(null, null, null, null);
5757
}
5858

@@ -66,10 +66,10 @@ public void testFilter() throws IOException, ServletException {
6666
@Override
6767
public void doFilter(ServletRequest request, ServletResponse response)
6868
throws IOException, ServletException {
69-
assertEquals("filter.remoteClientAddress", REMOTE_ADDRESS,
70-
KMSMDCFilter.getRemoteClientAddress());
71-
assertEquals("filter.method", METHOD, KMSMDCFilter.getMethod());
72-
assertEquals("filter.url", URL, KMSMDCFilter.getURL());
69+
assertEquals(REMOTE_ADDRESS,
70+
KMSMDCFilter.getRemoteClientAddress(), "filter.remoteClientAddress");
71+
assertEquals(METHOD, KMSMDCFilter.getMethod(), "filter.method");
72+
assertEquals(URL, KMSMDCFilter.getURL(), "filter.url");
7373
}
7474
};
7575

@@ -79,10 +79,10 @@ public void doFilter(ServletRequest request, ServletResponse response)
7979
}
8080

8181
private void checkMDCValuesAreEmpty() {
82-
assertNull("getRemoteClientAddress", KMSMDCFilter.getRemoteClientAddress());
83-
assertNull("getMethod", KMSMDCFilter.getMethod());
84-
assertNull("getURL", KMSMDCFilter.getURL());
85-
assertNull("getUgi", KMSMDCFilter.getUgi());
82+
assertNull(KMSMDCFilter.getRemoteClientAddress(), "getRemoteClientAddress");
83+
assertNull(KMSMDCFilter.getMethod(), "getMethod");
84+
assertNull(KMSMDCFilter.getURL(), "getURL");
85+
assertNull(KMSMDCFilter.getUgi(), "getUgi");
8686
}
8787

8888
}

hadoop-common-project/hadoop-kms/src/test/java/org/apache/hadoop/crypto/key/kms/server/TestKMSWithZK.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package org.apache.hadoop.crypto.key.kms.server;
1919

20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
2022
import org.apache.curator.test.TestingServer;
2123
import org.apache.hadoop.conf.Configuration;
2224
import org.apache.hadoop.crypto.key.kms.KMSRESTConstants;
@@ -25,8 +27,7 @@
2527
import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
2628
import org.apache.hadoop.security.authentication.util.ZKSignerSecretProvider;
2729
import org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL;
28-
import org.junit.Assert;
29-
import org.junit.Test;
30+
import org.junit.jupiter.api.Test;
3031

3132
import java.io.File;
3233
import java.net.HttpURLConnection;
@@ -97,7 +98,7 @@ public void testMultipleKMSInstancesWithZKSigner() throws Exception {
9798
@Override
9899
public Object run() throws Exception {
99100
HttpURLConnection conn = aUrl.openConnection(url1, token);
100-
Assert.assertEquals(HttpURLConnection.HTTP_OK,
101+
assertEquals(HttpURLConnection.HTTP_OK,
101102
conn.getResponseCode());
102103
return null;
103104
}
@@ -107,7 +108,7 @@ public Object run() throws Exception {
107108
@Override
108109
public Object run() throws Exception {
109110
HttpURLConnection conn = aUrl.openConnection(url2, token);
110-
Assert.assertEquals(HttpURLConnection.HTTP_OK,
111+
assertEquals(HttpURLConnection.HTTP_OK,
111112
conn.getResponseCode());
112113
return null;
113114
}
@@ -119,7 +120,7 @@ public Object run() throws Exception {
119120
final DelegationTokenAuthenticatedURL.Token emptyToken =
120121
new DelegationTokenAuthenticatedURL.Token();
121122
HttpURLConnection conn = aUrl.openConnection(url2, emptyToken);
122-
Assert.assertEquals(HttpURLConnection.HTTP_FORBIDDEN,
123+
assertEquals(HttpURLConnection.HTTP_FORBIDDEN,
123124
conn.getResponseCode());
124125
return null;
125126
}

0 commit comments

Comments
 (0)