Skip to content

Commit 00392dc

Browse files
CopilotkyonRay
andauthored
Add unit tests for SDK v3 client and transaction packages (#2)
* Initial plan * Add unit tests for client and transaction packages - part 1 Co-authored-by: kyonRay <[email protected]> * Add more unit tests for client protocol response and transaction dto classes Co-authored-by: kyonRay <[email protected]> * Add unit tests for LogFilterRequest, model BO classes and CommonConstant Co-authored-by: kyonRay <[email protected]> * Fix ResultCodeEnumTest to avoid mutating enum state in tests Co-authored-by: kyonRay <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: kyonRay <[email protected]>
1 parent bb52c2d commit 00392dc

25 files changed

+1980
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package org.fisco.bcos.sdk.v3.client.exceptions;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class ClientExceptionTest {
7+
8+
@Test
9+
public void testConstructorWithErrorCodeAndMessage() {
10+
int errorCode = 100;
11+
String errorMessage = "Test Error";
12+
String message = "Full message";
13+
14+
ClientException exception = new ClientException(errorCode, errorMessage, message);
15+
16+
Assert.assertEquals(errorCode, exception.getErrorCode());
17+
Assert.assertEquals(errorMessage, exception.getErrorMessage());
18+
Assert.assertEquals(message, exception.getMessage());
19+
}
20+
21+
@Test
22+
public void testConstructorWithErrorCodeMessageAndCause() {
23+
int errorCode = 200;
24+
String errorMessage = "Test Error";
25+
String message = "Full message";
26+
Throwable cause = new RuntimeException("Cause");
27+
28+
ClientException exception = new ClientException(errorCode, errorMessage, message, cause);
29+
30+
Assert.assertEquals(errorCode, exception.getErrorCode());
31+
Assert.assertEquals(errorMessage, exception.getErrorMessage());
32+
Assert.assertEquals(message, exception.getMessage());
33+
Assert.assertEquals(cause, exception.getCause());
34+
}
35+
36+
@Test
37+
public void testConstructorWithMessage() {
38+
String message = "Simple message";
39+
40+
ClientException exception = new ClientException(message);
41+
42+
Assert.assertEquals(message, exception.getMessage());
43+
}
44+
45+
@Test
46+
public void testConstructorWithMessageAndCause() {
47+
String message = "Message with cause";
48+
Throwable cause = new RuntimeException("Root cause");
49+
50+
ClientException exception = new ClientException(message, cause);
51+
52+
Assert.assertEquals(message, exception.getMessage());
53+
Assert.assertEquals(cause, exception.getCause());
54+
}
55+
56+
@Test
57+
public void testSetErrorCode() {
58+
ClientException exception = new ClientException("Test");
59+
exception.setErrorCode(300);
60+
61+
Assert.assertEquals(300, exception.getErrorCode());
62+
}
63+
64+
@Test
65+
public void testSetErrorMessage() {
66+
ClientException exception = new ClientException("Test");
67+
exception.setErrorMessage("New error message");
68+
69+
Assert.assertEquals("New error message", exception.getErrorMessage());
70+
}
71+
72+
@Test
73+
public void testToString() {
74+
int errorCode = 400;
75+
String errorMessage = "Error Message";
76+
String message = "Full Message";
77+
78+
ClientException exception = new ClientException(errorCode, errorMessage, message);
79+
80+
String result = exception.toString();
81+
Assert.assertNotNull(result);
82+
Assert.assertTrue(result.contains(String.valueOf(errorCode)));
83+
Assert.assertTrue(result.contains(errorMessage));
84+
Assert.assertTrue(result.contains(message));
85+
}
86+
87+
@Test
88+
public void testEquals() {
89+
ClientException exception1 = new ClientException(100, "Error", "Message");
90+
ClientException exception2 = new ClientException(100, "Error", "Message");
91+
ClientException exception3 = new ClientException(200, "Error", "Message");
92+
93+
Assert.assertEquals(exception1, exception2);
94+
Assert.assertNotEquals(exception1, exception3);
95+
Assert.assertEquals(exception1, exception1);
96+
Assert.assertNotEquals(exception1, null);
97+
Assert.assertNotEquals(exception1, "String");
98+
}
99+
100+
@Test
101+
public void testHashCode() {
102+
ClientException exception1 = new ClientException(100, "Error", "Message");
103+
ClientException exception2 = new ClientException(100, "Error", "Message");
104+
105+
Assert.assertEquals(exception1.hashCode(), exception2.hashCode());
106+
}
107+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.fisco.bcos.sdk.v3.client.protocol.model;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class GroupStatusTest {
7+
8+
@Test
9+
public void testGettersAndSetters() {
10+
GroupStatus groupStatus = new GroupStatus();
11+
12+
groupStatus.setCode("200");
13+
groupStatus.setMessage("Success");
14+
groupStatus.setStatus("running");
15+
16+
Assert.assertEquals("200", groupStatus.getCode());
17+
Assert.assertEquals("Success", groupStatus.getMessage());
18+
Assert.assertEquals("running", groupStatus.getStatus());
19+
}
20+
21+
@Test
22+
public void testEquals() {
23+
GroupStatus status1 = new GroupStatus();
24+
status1.setCode("200");
25+
status1.setMessage("Success");
26+
status1.setStatus("running");
27+
28+
GroupStatus status2 = new GroupStatus();
29+
status2.setCode("200");
30+
status2.setMessage("Success");
31+
status2.setStatus("running");
32+
33+
GroupStatus status3 = new GroupStatus();
34+
status3.setCode("404");
35+
status3.setMessage("Not Found");
36+
status3.setStatus("stopped");
37+
38+
Assert.assertEquals(status1, status2);
39+
Assert.assertNotEquals(status1, status3);
40+
Assert.assertEquals(status1, status1);
41+
Assert.assertNotEquals(status1, null);
42+
Assert.assertNotEquals(status1, "String");
43+
}
44+
45+
@Test
46+
public void testHashCode() {
47+
GroupStatus status1 = new GroupStatus();
48+
status1.setCode("200");
49+
status1.setMessage("Success");
50+
status1.setStatus("running");
51+
52+
GroupStatus status2 = new GroupStatus();
53+
status2.setCode("200");
54+
status2.setMessage("Success");
55+
status2.setStatus("running");
56+
57+
Assert.assertEquals(status1.hashCode(), status2.hashCode());
58+
}
59+
60+
@Test
61+
public void testToString() {
62+
GroupStatus groupStatus = new GroupStatus();
63+
groupStatus.setCode("200");
64+
groupStatus.setMessage("Success");
65+
groupStatus.setStatus("running");
66+
67+
String result = groupStatus.toString();
68+
Assert.assertNotNull(result);
69+
Assert.assertTrue(result.contains("200"));
70+
Assert.assertTrue(result.contains("Success"));
71+
Assert.assertTrue(result.contains("running"));
72+
}
73+
74+
@Test
75+
public void testWithNullValues() {
76+
GroupStatus groupStatus = new GroupStatus();
77+
78+
Assert.assertNull(groupStatus.getCode());
79+
Assert.assertNull(groupStatus.getMessage());
80+
Assert.assertNull(groupStatus.getStatus());
81+
}
82+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.fisco.bcos.sdk.v3.client.protocol.model;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class TransactionAttributeTest {
7+
8+
@Test
9+
public void testEVM_ABI_CODEC() {
10+
Assert.assertEquals(0x1, TransactionAttribute.EVM_ABI_CODEC);
11+
}
12+
13+
@Test
14+
public void testLIQUID_SCALE_CODEC() {
15+
Assert.assertEquals(0x2, TransactionAttribute.LIQUID_SCALE_CODEC);
16+
}
17+
18+
@Test
19+
public void testDAG() {
20+
Assert.assertEquals(0x4, TransactionAttribute.DAG);
21+
}
22+
23+
@Test
24+
public void testLIQUID_CREATE() {
25+
Assert.assertEquals(0x8, TransactionAttribute.LIQUID_CREATE);
26+
}
27+
28+
@Test
29+
public void testAttributeValues() {
30+
// Verify that the constants are unique
31+
Assert.assertNotEquals(TransactionAttribute.EVM_ABI_CODEC, TransactionAttribute.LIQUID_SCALE_CODEC);
32+
Assert.assertNotEquals(TransactionAttribute.EVM_ABI_CODEC, TransactionAttribute.DAG);
33+
Assert.assertNotEquals(TransactionAttribute.EVM_ABI_CODEC, TransactionAttribute.LIQUID_CREATE);
34+
Assert.assertNotEquals(TransactionAttribute.LIQUID_SCALE_CODEC, TransactionAttribute.DAG);
35+
Assert.assertNotEquals(TransactionAttribute.LIQUID_SCALE_CODEC, TransactionAttribute.LIQUID_CREATE);
36+
Assert.assertNotEquals(TransactionAttribute.DAG, TransactionAttribute.LIQUID_CREATE);
37+
}
38+
39+
@Test
40+
public void testAttributesBitFlags() {
41+
// Test that attributes can be used as bit flags
42+
int combined = TransactionAttribute.EVM_ABI_CODEC | TransactionAttribute.DAG;
43+
Assert.assertEquals(0x5, combined);
44+
45+
Assert.assertTrue((combined & TransactionAttribute.EVM_ABI_CODEC) != 0);
46+
Assert.assertTrue((combined & TransactionAttribute.DAG) != 0);
47+
Assert.assertFalse((combined & TransactionAttribute.LIQUID_SCALE_CODEC) != 0);
48+
}
49+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.fisco.bcos.sdk.v3.client.protocol.request;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class DefaultBlockParameterNameTest {
7+
8+
@Test
9+
public void testEarliestValue() {
10+
Assert.assertEquals("earliest", DefaultBlockParameterName.EARLIEST.getValue());
11+
}
12+
13+
@Test
14+
public void testLatestValue() {
15+
Assert.assertEquals("latest", DefaultBlockParameterName.LATEST.getValue());
16+
}
17+
18+
@Test
19+
public void testIsLatest() {
20+
Assert.assertTrue(DefaultBlockParameterName.LATEST.isLatest());
21+
Assert.assertFalse(DefaultBlockParameterName.EARLIEST.isLatest());
22+
}
23+
24+
@Test
25+
public void testIsEarliest() {
26+
Assert.assertTrue(DefaultBlockParameterName.EARLIEST.isEarliest());
27+
Assert.assertFalse(DefaultBlockParameterName.LATEST.isEarliest());
28+
}
29+
30+
@Test
31+
public void testFromStringWithEarliest() {
32+
DefaultBlockParameterName result = DefaultBlockParameterName.fromString("earliest");
33+
Assert.assertEquals(DefaultBlockParameterName.EARLIEST, result);
34+
}
35+
36+
@Test
37+
public void testFromStringWithLatest() {
38+
DefaultBlockParameterName result = DefaultBlockParameterName.fromString("latest");
39+
Assert.assertEquals(DefaultBlockParameterName.LATEST, result);
40+
}
41+
42+
@Test
43+
public void testFromStringCaseInsensitive() {
44+
DefaultBlockParameterName result1 = DefaultBlockParameterName.fromString("EARLIEST");
45+
Assert.assertEquals(DefaultBlockParameterName.EARLIEST, result1);
46+
47+
DefaultBlockParameterName result2 = DefaultBlockParameterName.fromString("Latest");
48+
Assert.assertEquals(DefaultBlockParameterName.LATEST, result2);
49+
}
50+
51+
@Test(expected = IllegalArgumentException.class)
52+
public void testFromStringWithInvalidValue() {
53+
DefaultBlockParameterName.fromString("invalid");
54+
}
55+
56+
@Test(expected = NullPointerException.class)
57+
public void testFromStringWithNull() {
58+
DefaultBlockParameterName.fromString(null);
59+
}
60+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.fisco.bcos.sdk.v3.client.protocol.request;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import java.math.BigInteger;
6+
7+
public class DefaultBlockParameterNumberTest {
8+
9+
@Test
10+
public void testConstructorWithBigInteger() {
11+
BigInteger blockNumber = BigInteger.valueOf(100);
12+
DefaultBlockParameterNumber parameter = new DefaultBlockParameterNumber(blockNumber);
13+
14+
Assert.assertEquals(blockNumber, parameter.getBlockNumber());
15+
}
16+
17+
@Test
18+
public void testConstructorWithLong() {
19+
long blockNumber = 200L;
20+
DefaultBlockParameterNumber parameter = new DefaultBlockParameterNumber(blockNumber);
21+
22+
Assert.assertEquals(BigInteger.valueOf(blockNumber), parameter.getBlockNumber());
23+
}
24+
25+
@Test
26+
public void testGetValueReturnsHexString() {
27+
DefaultBlockParameterNumber parameter = new DefaultBlockParameterNumber(BigInteger.valueOf(100));
28+
29+
String value = parameter.getValue();
30+
Assert.assertNotNull(value);
31+
Assert.assertTrue(value.startsWith("0x"));
32+
}
33+
34+
@Test
35+
public void testIsLatest() {
36+
DefaultBlockParameterNumber parameter = new DefaultBlockParameterNumber(BigInteger.valueOf(100));
37+
38+
Assert.assertFalse(parameter.isLatest());
39+
}
40+
41+
@Test
42+
public void testIsEarliest() {
43+
DefaultBlockParameterNumber parameter = new DefaultBlockParameterNumber(BigInteger.valueOf(100));
44+
45+
Assert.assertFalse(parameter.isEarliest());
46+
}
47+
48+
@Test
49+
public void testWithZeroBlockNumber() {
50+
DefaultBlockParameterNumber parameter = new DefaultBlockParameterNumber(BigInteger.ZERO);
51+
52+
Assert.assertEquals(BigInteger.ZERO, parameter.getBlockNumber());
53+
Assert.assertEquals("0x0", parameter.getValue());
54+
}
55+
56+
@Test
57+
public void testWithLargeBlockNumber() {
58+
BigInteger largeNumber = new BigInteger("999999999999999999");
59+
DefaultBlockParameterNumber parameter = new DefaultBlockParameterNumber(largeNumber);
60+
61+
Assert.assertEquals(largeNumber, parameter.getBlockNumber());
62+
}
63+
}

0 commit comments

Comments
 (0)