|
| 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 | +} |
0 commit comments