Skip to content

Commit 8ed92d1

Browse files
Merge pull request #123 from Lemeri123/main
Added tests for AccountClientImpl.getAccountBalance functionality
2 parents 3dd0f3f + c7c2fd1 commit 8ed92d1

File tree

4 files changed

+105
-1
lines changed

4 files changed

+105
-1
lines changed

hiero-enterprise-base/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
<artifactId>junit-jupiter</artifactId>
4343
<scope>test</scope>
4444
</dependency>
45+
<dependency>
46+
<groupId>org.mockito</groupId>
47+
<artifactId>mockito-core</artifactId>
48+
<scope>test</scope>
49+
</dependency>
4550
<dependency>
4651
<groupId>io.grpc</groupId>
4752
<artifactId>grpc-okhttp</artifactId>

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package com.openelements.hiero.base.implementation;
2-
32
import com.hedera.hashgraph.sdk.AccountId;
43
import com.hedera.hashgraph.sdk.Hbar;
54
import com.openelements.hiero.base.data.Account;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.openelements.hiero.base.test;
2+
3+
import com.openelements.hiero.base.implementation.AccountClientImpl;
4+
import com.hedera.hashgraph.sdk.AccountId;
5+
import com.hedera.hashgraph.sdk.Hbar;
6+
import com.openelements.hiero.base.HieroException;
7+
import com.openelements.hiero.base.protocol.AccountBalanceRequest;
8+
import com.openelements.hiero.base.protocol.AccountBalanceResponse;
9+
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
import org.mockito.ArgumentMatchers;
13+
14+
import static org.junit.jupiter.api.Assertions.*;
15+
import static org.mockito.Mockito.*;
16+
17+
public class AccountClientImplTest {
18+
19+
private ProtocolLayerClient mockProtocolLayerClient;
20+
private AccountClientImpl accountClientImpl;
21+
22+
@BeforeEach
23+
public void setUp() {
24+
mockProtocolLayerClient = mock(ProtocolLayerClient.class);
25+
accountClientImpl = new AccountClientImpl(mockProtocolLayerClient);
26+
}
27+
28+
@Test
29+
public void testGetAccountBalance_ValidPositiveBalance() throws HieroException {
30+
AccountId accountId = AccountId.fromString("0.0.12345");
31+
Hbar expectedBalance = new Hbar(10);
32+
33+
// Mock the response
34+
AccountBalanceResponse mockResponse = mock(AccountBalanceResponse.class);
35+
when(mockResponse.hbars()).thenReturn(expectedBalance);
36+
37+
when(mockProtocolLayerClient.executeAccountBalanceQuery(
38+
ArgumentMatchers.any(AccountBalanceRequest.class)
39+
)).thenReturn(mockResponse);
40+
41+
Hbar balance = accountClientImpl.getAccountBalance(accountId);
42+
43+
assertEquals(expectedBalance, balance);
44+
}
45+
46+
@Test
47+
public void testGetAccountBalance_ZeroBalance() throws HieroException {
48+
AccountId accountId = 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+
AccountId invalidAccountId = AccountId.fromString("0.0.9999999");
66+
67+
when(mockProtocolLayerClient.executeAccountBalanceQuery(
68+
ArgumentMatchers.any(AccountBalanceRequest.class)
69+
)).thenThrow(new HieroException("Invalid account"));
70+
71+
assertThrows(HieroException.class, () -> {
72+
accountClientImpl.getAccountBalance(invalidAccountId);
73+
});
74+
}
75+
76+
@Test
77+
public void testGetAccountBalance_NullThrowsException() {
78+
assertThrows(NullPointerException.class, () -> {
79+
accountClientImpl.getAccountBalance((AccountId) null);
80+
});
81+
}
82+
83+
@Test
84+
public void testGetAccountBalance_ProtocolLayerClientFails() throws HieroException {
85+
AccountId accountId = AccountId.fromString("0.0.12345");
86+
87+
when(mockProtocolLayerClient.executeAccountBalanceQuery(
88+
ArgumentMatchers.any(AccountBalanceRequest.class)
89+
)).thenThrow(new RuntimeException("Protocol Layer Failure"));
90+
91+
assertThrows(RuntimeException.class, () -> {
92+
accountClientImpl.getAccountBalance(accountId);
93+
});
94+
}
95+
}

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@
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+
<version> 5.11.0</version>
166+
</dependency>
162167
<dependency>
163168
<groupId>org.slf4j</groupId>
164169
<artifactId>slf4j-api</artifactId>

0 commit comments

Comments
 (0)