Skip to content

Commit

Permalink
chore: implement toString
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Ivanov <[email protected]>
  • Loading branch information
0xivanov committed Nov 14, 2024
1 parent 3141deb commit 5f2e437
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.protobuf.ByteString;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand Down Expand Up @@ -196,8 +197,9 @@ public long getBlockNumber() {
return this.blockNumber;
}

public void setBlockNumber(long blockNumber) {
public MirrorNodeContractQuery setBlockNumber(long blockNumber) {
this.blockNumber = blockNumber;
return this;
}

/**
Expand Down Expand Up @@ -294,4 +296,19 @@ static String parseContractCallResult(String responseBody) {
static long parseHexEstimateToLong(String responseBody) {
return Integer.parseInt(parseContractCallResult(responseBody).substring(2), 16);
}

@Override
public String toString() {
return "MirrorNodeContractQuery{" +
"contractId=" + contractId +
", contractEvmAddress='" + contractEvmAddress + '\'' +
", sender=" + sender +
", senderEvmAddress='" + senderEvmAddress + '\'' +
", callData=" + Arrays.toString(callData) +
", value=" + value +
", gasLimit=" + gasLimit +
", gasPrice=" + gasPrice +
", blockNumber=" + blockNumber +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void testSetAndGetContractEvmAddress() {
String evmAddress = "0x1234567890abcdef1234567890abcdef12345678";
query.setContractEvmAddress(evmAddress);
assertEquals(evmAddress, query.getContractEvmAddress());
assertNull(query.getContractId(), "Setting EVM address should reset ContractId to null.");
assertNull(query.getContractId());
}

@Test
Expand All @@ -89,7 +89,7 @@ void testSetAndGetcallData() {
@Test
void testSetFunctionWithoutParameters() {
query.setFunction("myFunction");
assertNotNull(query.getCallData(), "Function parameters should not be null after setting a function.");
assertNotNull(query.getCallData());
}

@Test
Expand Down Expand Up @@ -139,10 +139,11 @@ void testCreateJsonPayloadAllFieldsSet() {
String blockNumber = "latest";
boolean estimate = true;

String jsonPayload = MirrorNodeContractQuery.createJsonPayload(data, senderAddress, contractAddress, gas, gasPrice, value, blockNumber, estimate);
String jsonPayload = MirrorNodeContractQuery.createJsonPayload(data, senderAddress, contractAddress, gas,
gasPrice, value, blockNumber, estimate);

JsonObject expectedJson = new JsonObject();
expectedJson.addProperty("data", "7465737444617461"); // "testData" in hex
expectedJson.addProperty("data", "testData");
expectedJson.addProperty("to", contractAddress);
expectedJson.addProperty("estimate", estimate);
expectedJson.addProperty("blockNumber", blockNumber);
Expand All @@ -165,10 +166,11 @@ void testCreateJsonPayloadOnlyRequiredFieldsSet() {
String blockNumber = "latest";
boolean estimate = true;

String jsonPayload = MirrorNodeContractQuery.createJsonPayload(data, senderAddress, contractAddress, gas, gasPrice, value, blockNumber, estimate);
String jsonPayload = MirrorNodeContractQuery.createJsonPayload(data, senderAddress, contractAddress, gas,
gasPrice, value, blockNumber, estimate);

JsonObject expectedJson = new JsonObject();
expectedJson.addProperty("data", "7465737444617461"); // "testData" in hex
expectedJson.addProperty("data", "testData");
expectedJson.addProperty("to", contractAddress);
expectedJson.addProperty("estimate", estimate);
expectedJson.addProperty("blockNumber", blockNumber);
Expand All @@ -187,10 +189,11 @@ void testCreateJsonPayloadSomeOptionalFieldsSet() {
String blockNumber = "latest";
boolean estimate = false;

String jsonPayload = MirrorNodeContractQuery.createJsonPayload(data, senderAddress, contractAddress, gas, gasPrice, value, blockNumber, estimate);
String jsonPayload = MirrorNodeContractQuery.createJsonPayload(data, senderAddress, contractAddress, gas,
gasPrice, value, blockNumber, estimate);

JsonObject expectedJson = new JsonObject();
expectedJson.addProperty("data", "7465737444617461"); // "testData" in hex
expectedJson.addProperty("data", "testData");
expectedJson.addProperty("to", contractAddress);
expectedJson.addProperty("estimate", estimate);
expectedJson.addProperty("blockNumber", blockNumber);
Expand All @@ -212,10 +215,11 @@ void testCreateJsonPayloadAllOptionalFieldsDefault() {
String blockNumber = "latest";
boolean estimate = false;

String jsonPayload = MirrorNodeContractQuery.createJsonPayload(data, senderAddress, contractAddress, gas, gasPrice, value, blockNumber, estimate);
String jsonPayload = MirrorNodeContractQuery.createJsonPayload(data, senderAddress, contractAddress, gas,
gasPrice, value, blockNumber, estimate);

JsonObject expectedJson = new JsonObject();
expectedJson.addProperty("data", "7465737444617461"); // "testData" in hex
expectedJson.addProperty("data", "testData");
expectedJson.addProperty("to", contractAddress);
expectedJson.addProperty("estimate", estimate);
expectedJson.addProperty("blockNumber", blockNumber);
Expand All @@ -236,4 +240,35 @@ void testParseContractCallResult() {
String parsedResult = MirrorNodeContractQuery.parseContractCallResult(responseBody);
assertEquals("0x1234abcdef", parsedResult);
}

@Test
void shouldSerialize() {
ContractId testContractId = new ContractId(0, 0, 1234);
String testEvmAddress = "0x1234567890abcdef1234567890abcdef12345678";
AccountId testSenderId = new AccountId(0, 0, 5678);
String testSenderEvmAddress = "0xabcdefabcdefabcdefabcdefabcdefabcdef";
ByteString testCallData = ByteString.copyFromUtf8("testData");
String testFunctionName = "myFunction";
ContractFunctionParameters testParams = new ContractFunctionParameters().addString("param1");

long testValue = 1000L;
long testGasLimit = 500000L;
long testGasPrice = 20L;
long testBlockNumber = 123456L;

var query = new MirrorNodeContractQuery()
.setContractId(testContractId)
.setContractEvmAddress(testEvmAddress)
.setSender(testSenderId)
.setSenderEvmAddress(testSenderEvmAddress)
.setFunction(testFunctionName, testParams)
.setFunctionParameters(testCallData)
.setValue(testValue)
.setGasLimit(testGasLimit)
.setGasPrice(testGasPrice)
.setBlockNumber(testBlockNumber);

SnapshotMatcher.expect(query.toString()
).toMatchSnapshot();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
com.hedera.hashgraph.sdk.MirrorNodeContractQueryTest.shouldSerialize=[
"MirrorNodeContractQuery{contractId=null, contractEvmAddress='0x1234567890abcdef1234567890abcdef12345678', sender=null, senderEvmAddress='0xabcdefabcdefabcdefabcdefabcdefabcdef', callData=[116, 101, 115, 116, 68, 97, 116, 97], value=1000, gasLimit=500000, gasPrice=20, blockNumber=123456}"
]

0 comments on commit 5f2e437

Please sign in to comment.