Skip to content

Commit 1bdc631

Browse files
halibobo1205claude
andcommitted
fix: resolve all 14 PR review comments — strengthen test assertions and add JSONArray auto-parse
- Add string auto-parse fallback in JSONArray.getJSONObject(int) and getJSONArray(int) for Fastjson compatibility (same fix as JSONObject) - Use non-default ENERGY enum in 4 resource tests and assert parsed value (FreezeBalanceV2, UnFreezeBalanceV2, DelegateResource, UnDelegateResource) - Add token ID assertions in Exchange/Market servlet tests (ExchangeCreate, ExchangeWithdraw, ExchangeInject, ExchangeTransaction, MarketSellAsset) - Add description/url byte field assertions in UpdateAssetServletTest - Add verify+argThat with owner/name/energyLimit in DeployContractServletTest - Fix TestPojo to support Jackson deserialization (add no-arg constructor and @Setter) so testGetObjectPojo actually validates POJO path - Add fuzz tests for JSONArray index-based stringified JSON auto-parse Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 19cfd6a commit 1bdc631

13 files changed

Lines changed: 104 additions & 30 deletions

common/src/main/java/org/tron/json/JSONArray.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ public JSONObject getJSONObject(int index) {
7171
if (child.isNull()) {
7272
return null;
7373
}
74-
if (!child.isObject()) {
75-
throw new JSONException("Element at index " + index + " is not an object");
74+
if (child.isObject()) {
75+
return new JSONObject((ObjectNode) child);
7676
}
77-
return new JSONObject((ObjectNode) child);
77+
// Fastjson auto-parses stringified JSON objects
78+
if (child.isTextual()) {
79+
return JSON.parseObject(child.asText());
80+
}
81+
throw new JSONException("Element at index " + index + " is not an object");
7882
}
7983

8084
public JSONArray getJSONArray(int index) {
@@ -83,10 +87,14 @@ public JSONArray getJSONArray(int index) {
8387
if (child.isNull()) {
8488
return null;
8589
}
86-
if (!child.isArray()) {
87-
throw new JSONException("Element at index " + index + " is not an array");
90+
if (child.isArray()) {
91+
return new JSONArray((ArrayNode) child);
92+
}
93+
// Fastjson auto-parses stringified JSON arrays
94+
if (child.isTextual()) {
95+
return JSON.parseArray(child.asText());
8896
}
89-
return new JSONArray((ArrayNode) child);
97+
throw new JSONException("Element at index " + index + " is not an array");
9098
}
9199

92100
public String getString(int index) {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.tron.core.capsule.TransactionCapsule;
1515
import org.tron.protos.Protocol;
1616
import org.tron.protos.contract.BalanceContract;
17+
import org.tron.protos.contract.Common.ResourceCode;
1718

1819
public class DelegateResourceServletTest extends BaseHttpTest {
1920

@@ -37,7 +38,7 @@ public void testDelegateResource() throws Exception {
3738
+ "\"owner_address\": \"" + ownerAddr + "\","
3839
+ "\"receiver_address\": \"" + receiverAddr + "\","
3940
+ "\"balance\": 1000000,"
40-
+ "\"resource\": \"BANDWIDTH\""
41+
+ "\"resource\": \"ENERGY\""
4142
+ "}";
4243
MockHttpServletRequest request = postRequest(jsonParam);
4344

@@ -49,7 +50,9 @@ && addressEquals(((BalanceContract.DelegateResourceContract) c)
4950
.getOwnerAddress(), ownerAddr)
5051
&& addressEquals(((BalanceContract.DelegateResourceContract) c)
5152
.getReceiverAddress(), receiverAddr)
52-
&& ((BalanceContract.DelegateResourceContract) c).getBalance() == 1000000),
53+
&& ((BalanceContract.DelegateResourceContract) c).getBalance() == 1000000
54+
&& ((BalanceContract.DelegateResourceContract) c)
55+
.getResource() == ResourceCode.ENERGY),
5356
eq(Protocol.Transaction.Contract.ContractType.DelegateResourceContract));
5457
assertTransactionResponse(response);
5558
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package org.tron.core.services.http;
22

33
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertFalse;
5-
import static org.junit.Assert.assertTrue;
64
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.ArgumentMatchers.argThat;
76
import static org.mockito.ArgumentMatchers.eq;
7+
import static org.mockito.Mockito.verify;
88
import static org.mockito.Mockito.when;
99

1010
import org.junit.Test;
1111
import org.springframework.mock.web.MockHttpServletRequest;
1212
import org.springframework.mock.web.MockHttpServletResponse;
1313
import org.tron.core.capsule.TransactionCapsule;
1414
import org.tron.protos.Protocol;
15+
import org.tron.protos.contract.SmartContractOuterClass.CreateSmartContract;
1516

1617
public class DeployContractServletTest extends BaseHttpTest {
1718

@@ -44,6 +45,14 @@ public void testDeployContract() throws Exception {
4445
MockHttpServletResponse response = newResponse();
4546
servlet.doPost(request, response);
4647
assertEquals(200, response.getStatus());
48+
verify(wallet).createTransactionCapsule(
49+
argThat(c -> c instanceof CreateSmartContract
50+
&& addressEquals(((CreateSmartContract) c).getOwnerAddress(),
51+
"4199357684bc659f5166046b56c95a0e99f1265cd1")
52+
&& ((CreateSmartContract) c).getNewContract().getName().equals("TestContract")
53+
&& ((CreateSmartContract) c).getNewContract()
54+
.getOriginEnergyLimit() == 10000000),
55+
eq(Protocol.Transaction.Contract.ContractType.CreateSmartContract));
4756
assertTransactionResponse(response);
4857
}
4958
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public void testExchangeCreate() throws Exception {
4747
&& addressEquals(((ExchangeCreateContract) c)
4848
.getOwnerAddress(), ownerAddr)
4949
&& ((ExchangeCreateContract) c).getFirstTokenBalance() == 100
50-
&& ((ExchangeCreateContract) c).getSecondTokenBalance() == 200),
50+
&& ((ExchangeCreateContract) c).getSecondTokenBalance() == 200
51+
&& !((ExchangeCreateContract) c).getFirstTokenId().isEmpty()
52+
&& !((ExchangeCreateContract) c).getSecondTokenId().isEmpty()),
5153
eq(ContractType.ExchangeCreateContract));
5254
assertTransactionResponse(response);
5355
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public void testExchangeInject() throws Exception {
4747
&& addressEquals(((ExchangeContract.ExchangeInjectContract) c)
4848
.getOwnerAddress(), ownerAddr)
4949
&& ((ExchangeContract.ExchangeInjectContract) c).getExchangeId() == 1
50-
&& ((ExchangeContract.ExchangeInjectContract) c).getQuant() == 100),
50+
&& ((ExchangeContract.ExchangeInjectContract) c).getQuant() == 100
51+
&& !((ExchangeContract.ExchangeInjectContract) c).getTokenId().isEmpty()),
5152
eq(Protocol.Transaction.Contract.ContractType.ExchangeInjectContract));
5253
assertTransactionResponse(response);
5354
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ && addressEquals(((ExchangeContract.ExchangeTransactionContract) c)
4949
.getOwnerAddress(), ownerAddr)
5050
&& ((ExchangeContract.ExchangeTransactionContract) c).getExchangeId() == 1
5151
&& ((ExchangeContract.ExchangeTransactionContract) c).getQuant() == 100
52-
&& ((ExchangeContract.ExchangeTransactionContract) c).getExpected() == 10),
52+
&& ((ExchangeContract.ExchangeTransactionContract) c).getExpected() == 10
53+
&& !((ExchangeContract.ExchangeTransactionContract) c).getTokenId().isEmpty()),
5354
eq(Protocol.Transaction.Contract.ContractType.ExchangeTransactionContract));
5455
assertTransactionResponse(response);
5556
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public void testExchangeWithdraw() throws Exception {
4747
&& addressEquals(((ExchangeContract.ExchangeWithdrawContract) c)
4848
.getOwnerAddress(), ownerAddr)
4949
&& ((ExchangeContract.ExchangeWithdrawContract) c).getExchangeId() == 1
50-
&& ((ExchangeContract.ExchangeWithdrawContract) c).getQuant() == 50),
50+
&& ((ExchangeContract.ExchangeWithdrawContract) c).getQuant() == 50
51+
&& !((ExchangeContract.ExchangeWithdrawContract) c).getTokenId().isEmpty()),
5152
eq(Protocol.Transaction.Contract.ContractType.ExchangeWithdrawContract));
5253
assertTransactionResponse(response);
5354
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.tron.core.capsule.TransactionCapsule;
1515
import org.tron.protos.Protocol;
1616
import org.tron.protos.contract.BalanceContract;
17+
import org.tron.protos.contract.Common.ResourceCode;
1718

1819
public class FreezeBalanceV2ServletTest extends BaseHttpTest {
1920

@@ -35,7 +36,7 @@ public void testFreezeBalanceV2() throws Exception {
3536
String jsonParam = "{"
3637
+ "\"owner_address\": \"" + ownerAddr + "\","
3738
+ "\"frozen_balance\": 1000000,"
38-
+ "\"resource\": \"BANDWIDTH\""
39+
+ "\"resource\": \"ENERGY\""
3940
+ "}";
4041
MockHttpServletRequest request = postRequest(jsonParam);
4142

@@ -45,7 +46,9 @@ public void testFreezeBalanceV2() throws Exception {
4546
argThat(c -> c instanceof BalanceContract.FreezeBalanceV2Contract
4647
&& addressEquals(((BalanceContract.FreezeBalanceV2Contract) c)
4748
.getOwnerAddress(), ownerAddr)
48-
&& ((BalanceContract.FreezeBalanceV2Contract) c).getFrozenBalance() == 1000000),
49+
&& ((BalanceContract.FreezeBalanceV2Contract) c).getFrozenBalance() == 1000000
50+
&& ((BalanceContract.FreezeBalanceV2Contract) c)
51+
.getResource() == ResourceCode.ENERGY),
4952
eq(Protocol.Transaction.Contract.ContractType.FreezeBalanceV2Contract));
5053
assertTransactionResponse(response);
5154
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public void testMarketSellAsset() throws Exception {
4848
&& addressEquals(((MarketContract.MarketSellAssetContract) c)
4949
.getOwnerAddress(), ownerAddr)
5050
&& ((MarketContract.MarketSellAssetContract) c).getSellTokenQuantity() == 100
51-
&& ((MarketContract.MarketSellAssetContract) c).getBuyTokenQuantity() == 200),
51+
&& ((MarketContract.MarketSellAssetContract) c).getBuyTokenQuantity() == 200
52+
&& !((MarketContract.MarketSellAssetContract) c).getSellTokenId().isEmpty()
53+
&& !((MarketContract.MarketSellAssetContract) c).getBuyTokenId().isEmpty()),
5254
eq(Protocol.Transaction.Contract.ContractType.MarketSellAssetContract));
5355
assertTransactionResponse(response);
5456
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.tron.core.capsule.TransactionCapsule;
1515
import org.tron.protos.Protocol;
1616
import org.tron.protos.contract.BalanceContract;
17+
import org.tron.protos.contract.Common.ResourceCode;
1718

1819
public class UnDelegateResourceServletTest extends BaseHttpTest {
1920

@@ -37,7 +38,7 @@ public void testUnDelegateResource() throws Exception {
3738
+ "\"owner_address\": \"" + ownerAddr + "\","
3839
+ "\"receiver_address\": \"" + receiverAddr + "\","
3940
+ "\"balance\": 1000000,"
40-
+ "\"resource\": \"BANDWIDTH\""
41+
+ "\"resource\": \"ENERGY\""
4142
+ "}";
4243
MockHttpServletRequest request = postRequest(jsonParam);
4344

@@ -49,7 +50,9 @@ && addressEquals(((BalanceContract.UnDelegateResourceContract) c)
4950
.getOwnerAddress(), ownerAddr)
5051
&& addressEquals(((BalanceContract.UnDelegateResourceContract) c)
5152
.getReceiverAddress(), receiverAddr)
52-
&& ((BalanceContract.UnDelegateResourceContract) c).getBalance() == 1000000),
53+
&& ((BalanceContract.UnDelegateResourceContract) c).getBalance() == 1000000
54+
&& ((BalanceContract.UnDelegateResourceContract) c)
55+
.getResource() == ResourceCode.ENERGY),
5356
eq(Protocol.Transaction.Contract.ContractType.UnDelegateResourceContract));
5457
assertTransactionResponse(response);
5558
}

0 commit comments

Comments
 (0)