获取链上信息兼容

This commit is contained in:
lu7213 committed 2026-10-16 10:19:14 -04:00
1 parent ceb8807273
commit 2601bc898a
8 files changed
+412 -103

No files matched your search

@@ -0,0 +1,105 @@
package com.ruoyi.system.service;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.system.domain.vo.EnergyAndBandwidthVo;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class TronChainDataClientTest {
private HttpServer server;
private String baseUrl;
@Before
public void setUp() throws IOException {
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.start();
baseUrl = "http://127.0.0.1:" + server.getAddress().getPort();
}
@After
public void tearDown() {
if (server != null) {
server.stop(0);
}
}
@Test
public void shouldFallbackWhenEndpointReturns404OrMalformedBody() {
server.createContext("/missing/v1/accounts/TAddress/transactions/trc20",
new FixedResponseHandler(404, "not found"));
server.createContext("/malformed/v1/accounts/TAddress/transactions/trc20",
new FixedResponseHandler(200, "{\"success\":true}"));
server.createContext("/healthy/v1/accounts/TAddress/transactions/trc20",
new FixedResponseHandler(200, "{\"data\":[{\"transaction_id\":\"tx-1\"}]}"));
TronChainDataClient client = client(
Arrays.asList(baseUrl + "/missing", baseUrl + "/malformed", baseUrl + "/healthy"),
Collections.singletonList(baseUrl + "/missing"));
JSONArray transactions = client.queryConfirmedTrc20Transactions(
"mainnet", "TAddress", "TContract", "", 200);
assertEquals(1, transactions.size());
assertEquals("tx-1", transactions.getJSONObject(0).getString("transaction_id"));
}
@Test
public void shouldReturnSafeZeroSummaryWhenAllAccountEndpointsFail() {
server.createContext("/missing/api/accountv2", new FixedResponseHandler(404, "not found"));
TronChainDataClient client = client(Collections.singletonList(baseUrl + "/missing"),
Collections.singletonList(baseUrl + "/missing"));
EnergyAndBandwidthVo summary = client.queryDelegatedResourceSummary(
"mainnet", "TAddress", "");
assertEquals(0, summary.getDelegateEnergy().signum());
assertEquals(0, summary.getDelegateBandwidth().signum());
assertEquals(0, summary.getAcquiredEnergy().signum());
assertEquals(0, summary.getAcquiredBandwidth().signum());
assertEquals(Long.valueOf(0L), summary.getStakedTrxNum());
}
private TronChainDataClient client(List<String> transactions, List<String> accounts) {
Map<String, List<String>> transactionEndpoints = new LinkedHashMap<>();
Map<String, List<String>> accountEndpoints = new LinkedHashMap<>();
transactionEndpoints.put("mainnet", transactions);
accountEndpoints.put("mainnet", accounts);
return new TronChainDataClient(new RestTemplate(), transactionEndpoints, accountEndpoints, "");
}
private static class FixedResponseHandler implements HttpHandler {
private final int status;
private final byte[] body;
FixedResponseHandler(int status, String body) {
this.status = status;
this.body = body.getBytes(StandardCharsets.UTF_8);
}
@Override
public void handle(HttpExchange exchange) throws IOException {
exchange.getResponseHeaders().set("Content-Type", "application/json;charset=UTF-8");
exchange.sendResponseHeaders(status, body.length);
try (OutputStream output = exchange.getResponseBody()) {
output.write(body);
}
}
}
}