Skip to content

Commit df81ee5

Browse files
committed
Added tests for AccountClientImpl.getAccountBalance functionality
Signed-off-by: Lemeri123 <[email protected]>
1 parent 8681d78 commit df81ee5

File tree

4 files changed

+109
-3
lines changed

4 files changed

+109
-3
lines changed

hiero-enterprise-base/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
<artifactId>junit-jupiter</artifactId>
4343
<scope>test</scope>
4444
</dependency>
45+
<dependency>
46+
<groupId>org.mockito</groupId>
47+
<artifactId>mockito-core</artifactId>
48+
<version> 5.11.0</version>
49+
<scope>test</scope>
50+
</dependency>
4551
<dependency>
4652
<groupId>io.grpc</groupId>
4753
<artifactId>grpc-okhttp</artifactId>

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/AccountClientImpl.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
package com.openelements.hiero.base.implementation;
2-
3-
import com.hedera.hashgraph.sdk.AccountId;
42
import com.hedera.hashgraph.sdk.Hbar;
53
import com.openelements.hiero.base.data.Account;
64
import com.openelements.hiero.base.AccountClient;
@@ -44,7 +42,7 @@ public void deleteAccount(@NonNull Account account, @NonNull Account toAccount)
4442

4543
@NonNull
4644
@Override
47-
public Hbar getAccountBalance(@NonNull AccountId account) throws HieroException {
45+
public Hbar getAccountBalance(com.hedera.hashgraph.sdk.AccountId account) throws HieroException {
4846
final AccountBalanceRequest request = AccountBalanceRequest.of(account);
4947
final AccountBalanceResponse response = client.executeAccountBalanceQuery(request);
5048
return response.hbars();
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.openelements.hiero.base.test;
2+
import com.openelements.hiero.base.implementation.AccountClientImpl;
3+
import com.hedera.hashgraph.sdk.Hbar;
4+
import com.openelements.hiero.base.HieroException;
5+
import com.openelements.hiero.base.protocol.AccountBalanceRequest;
6+
import com.openelements.hiero.base.protocol.AccountBalanceResponse;
7+
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.mockito.ArgumentMatchers;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
import static org.mockito.Mockito.*;
14+
15+
public class AccountClientImplTest {
16+
17+
private ProtocolLayerClient mockProtocolLayerClient;
18+
private AccountClientImpl accountClientImpl;
19+
20+
@BeforeEach
21+
public void setUp() {
22+
mockProtocolLayerClient = mock(ProtocolLayerClient.class);
23+
accountClientImpl = new AccountClientImpl(mockProtocolLayerClient);
24+
}
25+
26+
@Test
27+
public void testGetAccountBalance_ValidPositiveBalance() throws HieroException {
28+
// Fully qualify AccountId to ensure no ambiguity
29+
com.hedera.hashgraph.sdk.AccountId accountId = com.hedera.hashgraph.sdk.AccountId.fromString("0.0.12345");
30+
Hbar expectedBalance = new Hbar(10);
31+
32+
// Mock the response
33+
AccountBalanceResponse mockResponse = mock(AccountBalanceResponse.class);
34+
when(mockResponse.hbars()).thenReturn(expectedBalance);
35+
36+
when(mockProtocolLayerClient.executeAccountBalanceQuery(
37+
ArgumentMatchers.any(AccountBalanceRequest.class)
38+
)).thenReturn(mockResponse);
39+
40+
// Explicit cast ensures we're calling the correct method
41+
Hbar balance = accountClientImpl.getAccountBalance(accountId);
42+
43+
assertEquals(expectedBalance, balance);
44+
}
45+
46+
@Test
47+
public void testGetAccountBalance_ZeroBalance() throws HieroException {
48+
com.hedera.hashgraph.sdk.AccountId accountId = com.hedera.hashgraph.sdk.AccountId.fromString("0.0.67890");
49+
Hbar expectedBalance = new Hbar(0);
50+
51+
AccountBalanceResponse mockResponse = mock(AccountBalanceResponse.class);
52+
when(mockResponse.hbars()).thenReturn(expectedBalance);
53+
54+
when(mockProtocolLayerClient.executeAccountBalanceQuery(
55+
ArgumentMatchers.any(AccountBalanceRequest.class)
56+
)).thenReturn(mockResponse);
57+
58+
Hbar balance = accountClientImpl.getAccountBalance(accountId);
59+
60+
assertEquals(expectedBalance, balance);
61+
}
62+
63+
@Test
64+
public void testGetAccountBalance_InvalidAccount_ThrowsException() throws HieroException {
65+
// Use a valid but non-existing account ID to simulate invalid behavior
66+
com.hedera.hashgraph.sdk.AccountId invalidAccountId = com.hedera.hashgraph.sdk.AccountId.fromString("0.0.9999999"); // Simulate invalid or non-existing account
67+
68+
when(mockProtocolLayerClient.executeAccountBalanceQuery(
69+
ArgumentMatchers.any(AccountBalanceRequest.class)
70+
)).thenThrow(new HieroException("Invalid account"));
71+
72+
assertThrows(HieroException.class, () -> {
73+
accountClientImpl.getAccountBalance(invalidAccountId);
74+
});
75+
}
76+
77+
78+
@Test
79+
public void testGetAccountBalance_NullThrowsException() {
80+
assertThrows(NullPointerException.class, () -> {
81+
accountClientImpl.getAccountBalance((com.hedera.hashgraph.sdk.AccountId) null);
82+
});
83+
}
84+
85+
86+
@Test
87+
public void testGetAccountBalance_ProtocolLayerClientFails() throws HieroException {
88+
com.hedera.hashgraph.sdk.AccountId accountId = com.hedera.hashgraph.sdk.AccountId.fromString("0.0.12345");
89+
90+
when(mockProtocolLayerClient.executeAccountBalanceQuery(
91+
ArgumentMatchers.any(AccountBalanceRequest.class)
92+
)).thenThrow(new RuntimeException("Protocol Layer Failure"));
93+
94+
assertThrows(RuntimeException.class, () -> {
95+
accountClientImpl.getAccountBalance(accountId);
96+
});
97+
}
98+
}

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@
159159
<artifactId>junit-jupiter</artifactId>
160160
<version>${junit.version}</version>
161161
</dependency>
162+
<dependency>
163+
<groupId>org.mockito</groupId>
164+
<artifactId>mockito-core</artifactId>
165+
</dependency>
162166
<dependency>
163167
<groupId>org.slf4j</groupId>
164168
<artifactId>slf4j-api</artifactId>

0 commit comments

Comments
 (0)