|
2 | 2 |
|
3 | 3 | import com.openelements.hiero.base.implementation.AccountClientImpl; |
4 | 4 | import com.hedera.hashgraph.sdk.AccountId; |
| 5 | +import com.openelements.hiero.base.data.Account; |
5 | 6 | import com.hedera.hashgraph.sdk.Hbar; |
6 | 7 | import com.openelements.hiero.base.HieroException; |
7 | 8 | import com.openelements.hiero.base.protocol.AccountBalanceRequest; |
8 | 9 | import com.openelements.hiero.base.protocol.AccountBalanceResponse; |
| 10 | +import com.openelements.hiero.base.protocol.AccountCreateRequest; |
| 11 | +import com.openelements.hiero.base.protocol.AccountCreateResult; |
9 | 12 | import com.openelements.hiero.base.protocol.ProtocolLayerClient; |
10 | 13 | import org.junit.jupiter.api.BeforeEach; |
11 | 14 | import org.junit.jupiter.api.Test; |
12 | 15 | import org.mockito.ArgumentMatchers; |
13 | 16 |
|
14 | 17 | import static org.junit.jupiter.api.Assertions.*; |
| 18 | +import static org.mockito.ArgumentMatchers.any; |
15 | 19 | import static org.mockito.Mockito.*; |
16 | 20 |
|
17 | 21 | public class AccountClientImplTest { |
@@ -92,4 +96,53 @@ public void testGetAccountBalance_ProtocolLayerClientFails() throws HieroExcepti |
92 | 96 | accountClientImpl.getAccountBalance(accountId); |
93 | 97 | }); |
94 | 98 | } |
| 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 | + } |
95 | 148 | } |
0 commit comments