diff --git a/hiero-enterprise-base/pom.xml b/hiero-enterprise-base/pom.xml index d818f228..a1feac37 100644 --- a/hiero-enterprise-base/pom.xml +++ b/hiero-enterprise-base/pom.xml @@ -42,6 +42,11 @@ junit-jupiter test + + org.mockito + mockito-core + test + io.grpc grpc-okhttp diff --git a/hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/AccountClientImpl.java b/hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/AccountClientImpl.java index bb6e436f..7ddb646b 100644 --- a/hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/AccountClientImpl.java +++ b/hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/AccountClientImpl.java @@ -1,5 +1,4 @@ package com.openelements.hiero.base.implementation; - import com.hedera.hashgraph.sdk.AccountId; import com.hedera.hashgraph.sdk.Hbar; import com.openelements.hiero.base.data.Account; diff --git a/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/AccountClientImplTest.java b/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/AccountClientImplTest.java new file mode 100644 index 00000000..b9898428 --- /dev/null +++ b/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/AccountClientImplTest.java @@ -0,0 +1,95 @@ +package com.openelements.hiero.base.test; + +import com.openelements.hiero.base.implementation.AccountClientImpl; +import com.hedera.hashgraph.sdk.AccountId; +import com.hedera.hashgraph.sdk.Hbar; +import com.openelements.hiero.base.HieroException; +import com.openelements.hiero.base.protocol.AccountBalanceRequest; +import com.openelements.hiero.base.protocol.AccountBalanceResponse; +import com.openelements.hiero.base.protocol.ProtocolLayerClient; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentMatchers; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +public class AccountClientImplTest { + + private ProtocolLayerClient mockProtocolLayerClient; + private AccountClientImpl accountClientImpl; + + @BeforeEach + public void setUp() { + mockProtocolLayerClient = mock(ProtocolLayerClient.class); + accountClientImpl = new AccountClientImpl(mockProtocolLayerClient); + } + + @Test + public void testGetAccountBalance_ValidPositiveBalance() throws HieroException { + AccountId accountId = AccountId.fromString("0.0.12345"); + Hbar expectedBalance = new Hbar(10); + + // Mock the response + AccountBalanceResponse mockResponse = mock(AccountBalanceResponse.class); + when(mockResponse.hbars()).thenReturn(expectedBalance); + + when(mockProtocolLayerClient.executeAccountBalanceQuery( + ArgumentMatchers.any(AccountBalanceRequest.class) + )).thenReturn(mockResponse); + + Hbar balance = accountClientImpl.getAccountBalance(accountId); + + assertEquals(expectedBalance, balance); + } + + @Test + public void testGetAccountBalance_ZeroBalance() throws HieroException { + AccountId accountId = AccountId.fromString("0.0.67890"); + Hbar expectedBalance = new Hbar(0); + + AccountBalanceResponse mockResponse = mock(AccountBalanceResponse.class); + when(mockResponse.hbars()).thenReturn(expectedBalance); + + when(mockProtocolLayerClient.executeAccountBalanceQuery( + ArgumentMatchers.any(AccountBalanceRequest.class) + )).thenReturn(mockResponse); + + Hbar balance = accountClientImpl.getAccountBalance(accountId); + + assertEquals(expectedBalance, balance); + } + + @Test + public void testGetAccountBalance_InvalidAccount_ThrowsException() throws HieroException { + AccountId invalidAccountId = AccountId.fromString("0.0.9999999"); + + when(mockProtocolLayerClient.executeAccountBalanceQuery( + ArgumentMatchers.any(AccountBalanceRequest.class) + )).thenThrow(new HieroException("Invalid account")); + + assertThrows(HieroException.class, () -> { + accountClientImpl.getAccountBalance(invalidAccountId); + }); + } + + @Test + public void testGetAccountBalance_NullThrowsException() { + assertThrows(NullPointerException.class, () -> { + accountClientImpl.getAccountBalance((AccountId) null); + }); + } + + @Test + public void testGetAccountBalance_ProtocolLayerClientFails() throws HieroException { + AccountId accountId = AccountId.fromString("0.0.12345"); + + when(mockProtocolLayerClient.executeAccountBalanceQuery( + ArgumentMatchers.any(AccountBalanceRequest.class) + )).thenThrow(new RuntimeException("Protocol Layer Failure")); + + assertThrows(RuntimeException.class, () -> { + accountClientImpl.getAccountBalance(accountId); + }); + } +} diff --git a/pom.xml b/pom.xml index 0acd1c4b..273d22a5 100644 --- a/pom.xml +++ b/pom.xml @@ -159,6 +159,11 @@ junit-jupiter ${junit.version} + + org.mockito + mockito-core + 5.11.0 + org.slf4j slf4j-api