Skip to content

Commit 75f6c2a

Browse files
committed
Implemented tests for createAccount method
Signed-off-by: Lemeri123 <[email protected]>
1 parent bce1fb8 commit 75f6c2a

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,23 @@ public AccountClientImpl(@NonNull final ProtocolLayerClient client) {
2424
@NonNull
2525
@Override
2626
public Account createAccount(@NonNull Hbar initialBalance) throws HieroException {
27-
final AccountCreateRequest request = AccountCreateRequest.of(initialBalance);
28-
final AccountCreateResult result = client.executeAccountCreateTransaction(request);
29-
return result.newAccount();
27+
if (initialBalance == null) {
28+
throw new NullPointerException("initialBalance must not be null");
29+
}
30+
31+
if (initialBalance.toTinybars() < 0) {
32+
throw new HieroException("Invalid initial balance: must be non-negative");
33+
}
34+
35+
try {
36+
final AccountCreateRequest request = AccountCreateRequest.of(initialBalance);
37+
final AccountCreateResult result = client.executeAccountCreateTransaction(request);
38+
return result.newAccount();
39+
} catch (IllegalArgumentException e) {
40+
throw new HieroException("Invalid initial balance: " + e.getMessage(), e);
41+
}
3042
}
43+
3144

3245
@Override
3346
public void deleteAccount(@NonNull Account account) throws HieroException {

hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/AccountClientImplTest.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
import com.openelements.hiero.base.implementation.AccountClientImpl;
44
import com.hedera.hashgraph.sdk.AccountId;
5+
import com.openelements.hiero.base.data.Account;
56
import com.hedera.hashgraph.sdk.Hbar;
67
import com.openelements.hiero.base.HieroException;
78
import com.openelements.hiero.base.protocol.AccountBalanceRequest;
89
import com.openelements.hiero.base.protocol.AccountBalanceResponse;
10+
import com.openelements.hiero.base.protocol.AccountCreateRequest;
11+
import com.openelements.hiero.base.protocol.AccountCreateResult;
912
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
1013
import org.junit.jupiter.api.BeforeEach;
1114
import org.junit.jupiter.api.Test;
1215
import org.mockito.ArgumentMatchers;
1316

1417
import static org.junit.jupiter.api.Assertions.*;
18+
import static org.mockito.ArgumentMatchers.any;
1519
import static org.mockito.Mockito.*;
1620

1721
public class AccountClientImplTest {
@@ -92,4 +96,53 @@ public void testGetAccountBalance_ProtocolLayerClientFails() throws HieroExcepti
9296
accountClientImpl.getAccountBalance(accountId);
9397
});
9498
}
95-
}
99+
100+
//tests for createAccount method
101+
@Test
102+
void testCreateAccount_successful() throws HieroException {
103+
Hbar initialBalance = Hbar.from(100);
104+
105+
AccountCreateResult mockResult = mock(AccountCreateResult.class);
106+
Account mockAccount = mock(Account.class);
107+
108+
when(mockAccount.accountId()).thenReturn(AccountId.fromString("0.0.12345"));
109+
when(mockResult.newAccount()).thenReturn(mockAccount);
110+
when(mockProtocolLayerClient.executeAccountCreateTransaction(any(AccountCreateRequest.class)))
111+
.thenReturn(mockResult);
112+
113+
Account result = accountClientImpl.createAccount(initialBalance);
114+
115+
assertNotNull(result);
116+
assertEquals(AccountId.fromString("0.0.12345"), result.accountId());
117+
verify(mockProtocolLayerClient, times(1))
118+
.executeAccountCreateTransaction(any(AccountCreateRequest.class));
119+
}
120+
121+
@Test
122+
void testCreateAccount_invalidInitialBalance_null() {
123+
Hbar initialBalance = null;
124+
125+
assertThrows(NullPointerException.class, () -> accountClientImpl.createAccount(initialBalance));
126+
}
127+
128+
@Test
129+
void testCreateAccount_invalidInitialBalance_negative() {
130+
Hbar initialBalance = Hbar.from(-100);
131+
HieroException exception = assertThrows(HieroException.class,
132+
() -> accountClientImpl.createAccount(initialBalance));
133+
134+
assertTrue(exception.getMessage().contains("Invalid initial balance"));
135+
}
136+
137+
138+
@Test
139+
void testCreateAccount_hieroExceptionThrown() throws HieroException {
140+
Hbar initialBalance = Hbar.from(100);
141+
142+
when(mockProtocolLayerClient.executeAccountCreateTransaction(any(AccountCreateRequest.class)))
143+
.thenThrow(new HieroException("Transaction failed"));
144+
145+
Exception exception = assertThrows(HieroException.class, () -> accountClientImpl.createAccount(initialBalance));
146+
assertEquals("Transaction failed", exception.getMessage());
147+
}
148+
}

0 commit comments

Comments
 (0)