Skip to content

Commit 1fb6333

Browse files
committed
test(api): clean up test style and add missing coverage
- RateLimiterServletWhitelistTest: replace assertFalse(true) with fail() - ByteArrayTest: simplify testJsonHexToInt_ValidHex to use throws instead of try/catch; add NPE tests for jsonHexToLong/jsonHexToInt null input after null-check removal - UtilMockTest: extract duplicated ServletInputStream into mockPostJsonRequest helper
1 parent 861e914 commit 1fb6333

3 files changed

Lines changed: 29 additions & 74 deletions

File tree

framework/src/test/java/org/tron/common/utils/ByteArrayTest.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.junit.Assert.fail;
2424
import static org.tron.common.utils.ByteArray.fromHex;
2525
import static org.tron.common.utils.ByteArray.jsonHexToInt;
26+
import static org.tron.common.utils.ByteArray.jsonHexToLong;
2627

2728
import java.math.BigInteger;
2829
import lombok.extern.slf4j.Slf4j;
@@ -97,16 +98,21 @@ public void testFromObject_SerializableObject() {
9798
}
9899

99100
@Test
100-
public void testJsonHexToInt_ValidHex() {
101-
try {
102-
int result = jsonHexToInt("0x1A");
103-
assertEquals(26, result);
104-
} catch (Exception e) {
105-
fail("Exception should not have been thrown for valid hex string.");
106-
}
101+
public void testJsonHexToInt_ValidHex() throws Exception {
102+
assertEquals(26, jsonHexToInt("0x1A"));
107103
assertThrows(Exception.class, () -> ByteArray.jsonHexToInt("1A"));
108104
}
109105

106+
@Test(expected = NullPointerException.class)
107+
public void testJsonHexToIntNullThrowsNpe() throws Exception {
108+
jsonHexToInt(null);
109+
}
110+
111+
@Test(expected = NullPointerException.class)
112+
public void testJsonHexToLongNullThrowsNpe() throws Exception {
113+
jsonHexToLong(null);
114+
}
115+
110116
@Test
111117
public void testHexToBigIntegerRejectsOversizedInput() {
112118
// 0x + 98 hex = 100 chars, at the limit

framework/src/test/java/org/tron/core/services/http/RateLimiterServletWhitelistTest.java

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.tron.core.services.http;
22

33
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertFalse;
54
import static org.junit.Assert.assertNotNull;
65
import static org.junit.Assert.assertNull;
76
import static org.junit.Assert.assertTrue;
@@ -26,19 +25,14 @@ public class RateLimiterServletWhitelistTest {
2625

2726
@Test
2827
public void testWhitelistContents() {
29-
assertNotNull(allowedAdapters.get(GlobalPreemptibleAdapter.class.getSimpleName()));
30-
assertNotNull(allowedAdapters.get(QpsRateLimiterAdapter.class.getSimpleName()));
31-
assertNotNull(allowedAdapters.get(IPQPSRateLimiterAdapter.class.getSimpleName()));
32-
assertNotNull(allowedAdapters.get(DefaultBaseQqsAdapter.class.getSimpleName()));
33-
34-
assertTrue(GlobalPreemptibleAdapter.class
35-
.isAssignableFrom(allowedAdapters.get(GlobalPreemptibleAdapter.class.getSimpleName())));
36-
assertTrue(QpsRateLimiterAdapter.class
37-
.isAssignableFrom(allowedAdapters.get(QpsRateLimiterAdapter.class.getSimpleName())));
38-
assertTrue(IPQPSRateLimiterAdapter.class
39-
.isAssignableFrom(allowedAdapters.get(IPQPSRateLimiterAdapter.class.getSimpleName())));
40-
assertTrue(DefaultBaseQqsAdapter.class
41-
.isAssignableFrom(allowedAdapters.get(DefaultBaseQqsAdapter.class.getSimpleName())));
28+
assertEquals(GlobalPreemptibleAdapter.class,
29+
allowedAdapters.get(GlobalPreemptibleAdapter.class.getSimpleName()));
30+
assertEquals(QpsRateLimiterAdapter.class,
31+
allowedAdapters.get(QpsRateLimiterAdapter.class.getSimpleName()));
32+
assertEquals(IPQPSRateLimiterAdapter.class,
33+
allowedAdapters.get(IPQPSRateLimiterAdapter.class.getSimpleName()));
34+
assertEquals(DefaultBaseQqsAdapter.class,
35+
allowedAdapters.get(DefaultBaseQqsAdapter.class.getSimpleName()));
4236
}
4337

4438
@Test
@@ -47,21 +41,6 @@ public void testWhitelistRejectsUnknownAdapter() {
4741
assertNull(allowedAdapters.get("java.lang.Runtime"));
4842
}
4943

50-
@Test
51-
public void testWhitelistIsUnmodifiable() {
52-
try {
53-
allowedAdapters.put("EvilAdapter", DefaultBaseQqsAdapter.class);
54-
assertFalse("Whitelist should be unmodifiable", true);
55-
} catch (UnsupportedOperationException e) {
56-
// expected
57-
}
58-
}
59-
60-
@Test
61-
public void testWhitelistSize() {
62-
assertEquals(4, allowedAdapters.size());
63-
}
64-
6544
@Test
6645
public void testUnknownAdapterFallsBackToDefault() throws Exception {
6746
IRateLimiter limiter = RateLimiterServlet.buildAdapter(

framework/src/test/java/org/tron/core/services/http/UtilMockTest.java

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -279,48 +279,20 @@ public void testValidateParameter() {
279279

280280
@Test
281281
public void testGetAddressWithInvalidJsonBody() throws Exception {
282-
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
283-
Mockito.when(request.getMethod()).thenReturn("POST");
284-
Mockito.when(request.getContentType()).thenReturn("application/json");
285-
286-
// plain text, not JSON
287-
Mockito.when(request.getInputStream()).thenReturn(
288-
new javax.servlet.ServletInputStream() {
289-
private final java.io.InputStream in =
290-
new java.io.ByteArrayInputStream("not a json".getBytes());
291-
292-
@Override
293-
public int read() throws java.io.IOException {
294-
return in.read();
295-
}
296-
297-
@Override
298-
public boolean isFinished() {
299-
return false;
300-
}
301-
302-
@Override
303-
public boolean isReady() {
304-
return true;
305-
}
306-
307-
@Override
308-
public void setReadListener(javax.servlet.ReadListener l) {
309-
}
310-
});
311-
312-
byte[] address = Util.getAddress(request);
313-
Assert.assertNull(address);
282+
HttpServletRequest request = mockPostJsonRequest("not a json");
283+
Assert.assertNull(Util.getAddress(request));
314284
}
315285

316286
@Test
317287
public void testGetAddressWithJsonArrayBody() throws Exception {
288+
HttpServletRequest request = mockPostJsonRequest("[\"address1\",\"address2\"]");
289+
Assert.assertNull(Util.getAddress(request));
290+
}
291+
292+
private HttpServletRequest mockPostJsonRequest(String body) throws Exception {
318293
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
319294
Mockito.when(request.getMethod()).thenReturn("POST");
320295
Mockito.when(request.getContentType()).thenReturn("application/json");
321-
322-
// JSON array instead of JSON object
323-
String body = "[\"address1\",\"address2\"]";
324296
Mockito.when(request.getInputStream()).thenReturn(
325297
new javax.servlet.ServletInputStream() {
326298
private final java.io.InputStream in =
@@ -345,9 +317,7 @@ public boolean isReady() {
345317
public void setReadListener(javax.servlet.ReadListener l) {
346318
}
347319
});
348-
349-
byte[] address = Util.getAddress(request);
350-
Assert.assertNull(address);
320+
return request;
351321
}
352322

353323
@Test

0 commit comments

Comments
 (0)