Skip to content

Commit 69b5d62

Browse files
committed
[ISSUE #9156] Refactor AclUtils#getAclRPCHook
1 parent de4e48d commit 69b5d62

File tree

2 files changed

+77
-49
lines changed

2 files changed

+77
-49
lines changed

acl/src/main/java/org/apache/rocketmq/acl/common/AclUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.Map;
2424
import java.util.SortedMap;
2525

26-
import com.alibaba.fastjson.JSONObject;
26+
import com.alibaba.fastjson2.JSONObject;
2727
import org.apache.commons.lang3.StringUtils;
2828
import org.apache.rocketmq.common.constant.LoggerName;
2929
import org.apache.rocketmq.logging.org.slf4j.Logger;

acl/src/test/java/org/apache/rocketmq/acl/common/AclUtilsTest.java

+76-48
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package org.apache.rocketmq.acl.common;
1818

19-
import com.alibaba.fastjson.JSONObject;
19+
import com.alibaba.fastjson2.JSONObject;
2020
import org.apache.commons.lang3.StringUtils;
2121
import org.apache.rocketmq.acl.plain.PlainAccessData;
2222
import org.apache.rocketmq.common.PlainAccessConfig;
@@ -32,8 +32,15 @@
3232
import java.util.Collections;
3333
import java.util.List;
3434
import java.util.Map;
35+
import java.util.Objects;
3536
import java.util.UUID;
3637

38+
import static org.junit.Assert.assertEquals;
39+
import static org.junit.Assert.assertFalse;
40+
import static org.junit.Assert.assertNotNull;
41+
import static org.junit.Assert.assertNull;
42+
import static org.junit.Assert.assertTrue;
43+
3744
public class AclUtilsTest {
3845

3946
@Test
@@ -47,7 +54,7 @@ public void testGetAddresses() {
4754
addressList.add("1.1.1.2");
4855
addressList.add("1.1.1.3");
4956
addressList.add("1.1.1.4");
50-
Assert.assertEquals(newAddressList, addressList);
57+
assertEquals(newAddressList, addressList);
5158

5259
// IPv6 test
5360
String ipv6Address = "1:ac41:9987::bb22:666:{1,2,3,4}";
@@ -60,7 +67,7 @@ public void testGetAddresses() {
6067
ipv6AddressList.add("1:ac41:9987::bb22:666:2");
6168
ipv6AddressList.add("1:ac41:9987::bb22:666:3");
6269
ipv6AddressList.add("1:ac41:9987::bb22:666:4");
63-
Assert.assertEquals(newIPv6AddressList, ipv6AddressList);
70+
assertEquals(newIPv6AddressList, ipv6AddressList);
6471
}
6572

6673
@Test
@@ -70,9 +77,9 @@ public void testIsScope_StringArray() {
7077
for (int i = 0; i < 6; i++) {
7178
boolean isScope = AclUtils.isScope(address, 4);
7279
if (i == 3) {
73-
Assert.assertTrue(isScope);
80+
assertTrue(isScope);
7481
} else {
75-
Assert.assertFalse(isScope);
82+
assertFalse(isScope);
7683
}
7784
address = address + ".12";
7885
}
@@ -82,46 +89,46 @@ public void testIsScope_StringArray() {
8289
public void testIsScope_Array() {
8390
String[] address = StringUtils.split("12.12.12.12", ".");
8491
boolean isScope = AclUtils.isScope(address, 4);
85-
Assert.assertTrue(isScope);
92+
assertTrue(isScope);
8693
isScope = AclUtils.isScope(address, 3);
87-
Assert.assertTrue(isScope);
94+
assertTrue(isScope);
8895

8996
address = StringUtils.split("12.12.1222.1222", ".");
9097
isScope = AclUtils.isScope(address, 4);
91-
Assert.assertFalse(isScope);
98+
assertFalse(isScope);
9299
isScope = AclUtils.isScope(address, 3);
93-
Assert.assertFalse(isScope);
100+
assertFalse(isScope);
94101

95102
// IPv6 test
96103
address = StringUtils.split("1050:0000:0000:0000:0005:0600:300c:326b", ":");
97104
isScope = AclUtils.isIPv6Scope(address, 8);
98-
Assert.assertTrue(isScope);
105+
assertTrue(isScope);
99106
isScope = AclUtils.isIPv6Scope(address, 4);
100-
Assert.assertTrue(isScope);
107+
assertTrue(isScope);
101108

102109
address = StringUtils.split("1050:9876:0000:0000:0005:akkg:300c:326b", ":");
103110
isScope = AclUtils.isIPv6Scope(address, 8);
104-
Assert.assertFalse(isScope);
111+
assertFalse(isScope);
105112
isScope = AclUtils.isIPv6Scope(address, 4);
106-
Assert.assertTrue(isScope);
113+
assertTrue(isScope);
107114

108115
address = StringUtils.split(AclUtils.expandIP("1050::0005:akkg:300c:326b", 8), ":");
109116
isScope = AclUtils.isIPv6Scope(address, 8);
110-
Assert.assertFalse(isScope);
117+
assertFalse(isScope);
111118
isScope = AclUtils.isIPv6Scope(address, 4);
112-
Assert.assertTrue(isScope);
119+
assertTrue(isScope);
113120
}
114121

115122
@Test
116123
public void testIsScope_String() {
117124
for (int i = 0; i < 256; i++) {
118125
boolean isScope = AclUtils.isScope(i + "");
119-
Assert.assertTrue(isScope);
126+
assertTrue(isScope);
120127
}
121128
boolean isScope = AclUtils.isScope("-1");
122-
Assert.assertFalse(isScope);
129+
assertFalse(isScope);
123130
isScope = AclUtils.isScope("256");
124-
Assert.assertFalse(isScope);
131+
assertFalse(isScope);
125132
}
126133

127134
@Test
@@ -131,9 +138,9 @@ public void testIsScope_Integral() {
131138
Assert.assertTrue(isScope);
132139
}
133140
boolean isScope = AclUtils.isScope(-1);
134-
Assert.assertFalse(isScope);
141+
assertFalse(isScope);
135142
isScope = AclUtils.isScope(256);
136-
Assert.assertFalse(isScope);
143+
assertFalse(isScope);
137144

138145
// IPv6 test
139146
int min = Integer.parseInt("0", 16);
@@ -143,71 +150,71 @@ public void testIsScope_Integral() {
143150
Assert.assertTrue(isScope);
144151
}
145152
isScope = AclUtils.isIPv6Scope(-1);
146-
Assert.assertFalse(isScope);
153+
assertFalse(isScope);
147154
isScope = AclUtils.isIPv6Scope(max + 1);
148-
Assert.assertFalse(isScope);
155+
assertFalse(isScope);
149156
}
150157

151158
@Test
152159
public void testIsAsterisk() {
153160
boolean isAsterisk = AclUtils.isAsterisk("*");
154-
Assert.assertTrue(isAsterisk);
161+
assertTrue(isAsterisk);
155162

156163
isAsterisk = AclUtils.isAsterisk(",");
157-
Assert.assertFalse(isAsterisk);
164+
assertFalse(isAsterisk);
158165
}
159166

160167
@Test
161168
public void testIsComma() {
162169
boolean isColon = AclUtils.isComma(",");
163-
Assert.assertTrue(isColon);
170+
assertTrue(isColon);
164171

165172
isColon = AclUtils.isComma("-");
166-
Assert.assertFalse(isColon);
173+
assertFalse(isColon);
167174
}
168175

169176
@Test
170177
public void testIsMinus() {
171178
boolean isMinus = AclUtils.isMinus("-");
172-
Assert.assertTrue(isMinus);
179+
assertTrue(isMinus);
173180

174181
isMinus = AclUtils.isMinus("*");
175-
Assert.assertFalse(isMinus);
182+
assertFalse(isMinus);
176183
}
177184

178185
@Test
179186
public void testV6ipProcess() {
180187
String remoteAddr = "5::7:6:1-200:*";
181-
Assert.assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0007:0006");
188+
assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0007:0006");
182189

183190
remoteAddr = "5::7:6:1-200";
184-
Assert.assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0000:0007:0006");
191+
assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0000:0007:0006");
185192
remoteAddr = "5::7:6:*";
186-
Assert.assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0000:0007:0006");
193+
assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0000:0000:0000:0000:0007:0006");
187194

188195
remoteAddr = "5:7:6:*";
189-
Assert.assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0007:0006");
196+
assertEquals(AclUtils.v6ipProcess(remoteAddr), "0005:0007:0006");
190197
}
191198

192199
@Test
193200
public void testExpandIP() {
194-
Assert.assertEquals(AclUtils.expandIP("::", 8), "0000:0000:0000:0000:0000:0000:0000:0000");
195-
Assert.assertEquals(AclUtils.expandIP("::1", 8), "0000:0000:0000:0000:0000:0000:0000:0001");
196-
Assert.assertEquals(AclUtils.expandIP("3::", 8), "0003:0000:0000:0000:0000:0000:0000:0000");
197-
Assert.assertEquals(AclUtils.expandIP("2::2", 8), "0002:0000:0000:0000:0000:0000:0000:0002");
198-
Assert.assertEquals(AclUtils.expandIP("4::aac4:92", 8), "0004:0000:0000:0000:0000:0000:AAC4:0092");
199-
Assert.assertEquals(AclUtils.expandIP("ab23:56:901a::cc6:765:bb:9011", 8), "AB23:0056:901A:0000:0CC6:0765:00BB:9011");
200-
Assert.assertEquals(AclUtils.expandIP("ab23:56:901a:1:cc6:765:bb:9011", 8), "AB23:0056:901A:0001:0CC6:0765:00BB:9011");
201-
Assert.assertEquals(AclUtils.expandIP("5::7:6", 6), "0005:0000:0000:0000:0007:0006");
201+
assertEquals(AclUtils.expandIP("::", 8), "0000:0000:0000:0000:0000:0000:0000:0000");
202+
assertEquals(AclUtils.expandIP("::1", 8), "0000:0000:0000:0000:0000:0000:0000:0001");
203+
assertEquals(AclUtils.expandIP("3::", 8), "0003:0000:0000:0000:0000:0000:0000:0000");
204+
assertEquals(AclUtils.expandIP("2::2", 8), "0002:0000:0000:0000:0000:0000:0000:0002");
205+
assertEquals(AclUtils.expandIP("4::aac4:92", 8), "0004:0000:0000:0000:0000:0000:AAC4:0092");
206+
assertEquals(AclUtils.expandIP("ab23:56:901a::cc6:765:bb:9011", 8), "AB23:0056:901A:0000:0CC6:0765:00BB:9011");
207+
assertEquals(AclUtils.expandIP("ab23:56:901a:1:cc6:765:bb:9011", 8), "AB23:0056:901A:0001:0CC6:0765:00BB:9011");
208+
assertEquals(AclUtils.expandIP("5::7:6", 6), "0005:0000:0000:0000:0007:0006");
202209
}
203210

204211
@SuppressWarnings("unchecked")
205212
@Test
206213
public void testGetYamlDataObject() throws IOException {
207214
try (InputStream is = AclUtilsTest.class.getClassLoader().getResourceAsStream("conf/plain_acl_correct.yml")) {
208215
Map<String, Object> map = AclUtils.getYamlDataObject(is, Map.class);
209-
Assert.assertNotNull(map);
210-
Assert.assertFalse(map.isEmpty());
216+
assertNotNull(map);
217+
assertFalse(map.isEmpty());
211218
}
212219
}
213220

@@ -225,7 +232,7 @@ private static String randomTmpFile() {
225232
public void writeDataObject2YamlFileTest() throws IOException {
226233
String targetFileName = randomTmpFile();
227234
File transport = new File(targetFileName);
228-
Assert.assertTrue(transport.createNewFile());
235+
assertTrue(transport.createNewFile());
229236
transport.deleteOnExit();
230237

231238
PlainAccessData aclYamlMap = new PlainAccessData();
@@ -248,14 +255,14 @@ public void writeDataObject2YamlFileTest() throws IOException {
248255
};
249256
accounts.add(accountsMap);
250257
aclYamlMap.setAccounts(accounts);
251-
Assert.assertTrue(AclUtils.writeDataObject(targetFileName, aclYamlMap));
258+
assertTrue(AclUtils.writeDataObject(targetFileName, aclYamlMap));
252259
}
253260

254261
@Test
255262
public void updateExistedYamlFileTest() throws IOException {
256263
String targetFileName = randomTmpFile();
257264
File transport = new File(targetFileName);
258-
Assert.assertTrue(transport.createNewFile());
265+
assertTrue(transport.createNewFile());
259266
transport.deleteOnExit();
260267

261268
PlainAccessData aclYamlMap = new PlainAccessData();
@@ -279,21 +286,42 @@ public void updateExistedYamlFileTest() throws IOException {
279286

280287
PlainAccessData readableMap = AclUtils.getYamlDataObject(targetFileName, PlainAccessData.class);
281288
List<String> updatedGlobalWhiteRemoteAddrs = readableMap.getGlobalWhiteRemoteAddresses();
282-
Assert.assertEquals("192.168.1.2", updatedGlobalWhiteRemoteAddrs.get(0));
289+
assertEquals("192.168.1.2", updatedGlobalWhiteRemoteAddrs.get(0));
283290
}
284291

285292
@Test
286293
public void getYamlDataIgnoreFileNotFoundExceptionTest() {
287294

288295
JSONObject yamlDataObject = AclUtils.getYamlDataObject("plain_acl.yml", JSONObject.class);
289-
Assert.assertNull(yamlDataObject);
296+
assertNull(yamlDataObject);
290297
}
291298

292299
@Test
293300
public void getAclRPCHookTest() throws IOException {
294301
try (InputStream is = AclUtilsTest.class.getClassLoader().getResourceAsStream("conf/plain_acl_incomplete.yml")) {
295302
RPCHook incompleteContRPCHook = AclUtils.getAclRPCHook(is);
296-
Assert.assertNull(incompleteContRPCHook);
303+
assertNull(incompleteContRPCHook);
297304
}
298305
}
306+
307+
@Test
308+
public void testGetAclRPCHookByFileName() {
309+
RPCHook actual = AclUtils.getAclRPCHook(Objects.requireNonNull(AclUtilsTest.class.getResource("/acl_hook/plain_acl.yml")).getPath());
310+
assertNotNull(actual);
311+
assertTrue(actual instanceof AclClientRPCHook);
312+
assertAclClientRPCHook((AclClientRPCHook) actual);
313+
}
314+
315+
@Test
316+
public void testGetAclRPCHookByInputStream() {
317+
RPCHook actual = AclUtils.getAclRPCHook(Objects.requireNonNull(AclUtilsTest.class.getResourceAsStream("/acl_hook/plain_acl.yml")));
318+
assertNotNull(actual);
319+
assertTrue(actual instanceof AclClientRPCHook);
320+
assertAclClientRPCHook((AclClientRPCHook) actual);
321+
}
322+
323+
private void assertAclClientRPCHook(final AclClientRPCHook actual) {
324+
assertEquals("rocketmq2", actual.getSessionCredentials().getAccessKey());
325+
assertEquals("12345678", actual.getSessionCredentials().getSecretKey());
326+
}
299327
}

0 commit comments

Comments
 (0)