From df81ee517ff4a8eecc3aa7dc823ab1599d73b1ad Mon Sep 17 00:00:00 2001 From: Lemeri123 <132246079+Lemeri123@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:29:29 +0300 Subject: [PATCH 1/2] Added tests for AccountClientImpl.getAccountBalance functionality Signed-off-by: Lemeri123 <132246079+Lemeri123@users.noreply.github.com> --- hiero-enterprise-base/pom.xml | 6 ++ .../implementation/AccountClientImpl.java | 4 +- .../base/test/AccountClientImplTest.java | 98 +++++++++++++++++++ pom.xml | 4 + 4 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/AccountClientImplTest.java diff --git a/hiero-enterprise-base/pom.xml b/hiero-enterprise-base/pom.xml index d818f228..2a4c848e 100644 --- a/hiero-enterprise-base/pom.xml +++ b/hiero-enterprise-base/pom.xml @@ -42,6 +42,12 @@ junit-jupiter test + + org.mockito + mockito-core + 5.11.0 + 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..c375e7ca 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,6 +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; import com.openelements.hiero.base.AccountClient; @@ -44,7 +42,7 @@ public void deleteAccount(@NonNull Account account, @NonNull Account toAccount) @NonNull @Override - public Hbar getAccountBalance(@NonNull AccountId account) throws HieroException { + public Hbar getAccountBalance(com.hedera.hashgraph.sdk.AccountId account) throws HieroException { final AccountBalanceRequest request = AccountBalanceRequest.of(account); final AccountBalanceResponse response = client.executeAccountBalanceQuery(request); return response.hbars(); 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..11fc2f2d --- /dev/null +++ b/hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/AccountClientImplTest.java @@ -0,0 +1,98 @@ +package com.openelements.hiero.base.test; +import com.openelements.hiero.base.implementation.AccountClientImpl; +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 { + // Fully qualify AccountId to ensure no ambiguity + com.hedera.hashgraph.sdk.AccountId accountId = com.hedera.hashgraph.sdk.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); + + // Explicit cast ensures we're calling the correct method + Hbar balance = accountClientImpl.getAccountBalance(accountId); + + assertEquals(expectedBalance, balance); + } + + @Test + public void testGetAccountBalance_ZeroBalance() throws HieroException { + com.hedera.hashgraph.sdk.AccountId accountId = com.hedera.hashgraph.sdk.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 { + // Use a valid but non-existing account ID to simulate invalid behavior + com.hedera.hashgraph.sdk.AccountId invalidAccountId = com.hedera.hashgraph.sdk.AccountId.fromString("0.0.9999999"); // Simulate invalid or non-existing account + + 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((com.hedera.hashgraph.sdk.AccountId) null); + }); + } + + + @Test + public void testGetAccountBalance_ProtocolLayerClientFails() throws HieroException { + com.hedera.hashgraph.sdk.AccountId accountId = com.hedera.hashgraph.sdk.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..b4de8060 100644 --- a/pom.xml +++ b/pom.xml @@ -159,6 +159,10 @@ junit-jupiter ${junit.version} + + org.mockito + mockito-core + org.slf4j slf4j-api From c7c2fd18fc0c6f75a77dcde0fd64de139b6390bb Mon Sep 17 00:00:00 2001 From: Lemeri123 <132246079+Lemeri123@users.noreply.github.com> Date: Thu, 12 Dec 2024 22:14:28 +0300 Subject: [PATCH 2/2] Made changes Signed-off-by: Lemeri123 <132246079+Lemeri123@users.noreply.github.com> --- hiero-enterprise-base/pom.xml | 1 - .../implementation/AccountClientImpl.java | 3 ++- .../base/test/AccountClientImplTest.java | 21 ++++++++----------- pom.xml | 3 ++- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/hiero-enterprise-base/pom.xml b/hiero-enterprise-base/pom.xml index 2a4c848e..a1feac37 100644 --- a/hiero-enterprise-base/pom.xml +++ b/hiero-enterprise-base/pom.xml @@ -45,7 +45,6 @@ org.mockito mockito-core - 5.11.0 test 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 c375e7ca..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,4 +1,5 @@ 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; import com.openelements.hiero.base.AccountClient; @@ -42,7 +43,7 @@ public void deleteAccount(@NonNull Account account, @NonNull Account toAccount) @NonNull @Override - public Hbar getAccountBalance(com.hedera.hashgraph.sdk.AccountId account) throws HieroException { + public Hbar getAccountBalance(@NonNull AccountId account) throws HieroException { final AccountBalanceRequest request = AccountBalanceRequest.of(account); final AccountBalanceResponse response = client.executeAccountBalanceQuery(request); return response.hbars(); 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 index 11fc2f2d..b9898428 100644 --- 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 @@ -1,5 +1,7 @@ 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; @@ -25,8 +27,7 @@ public void setUp() { @Test public void testGetAccountBalance_ValidPositiveBalance() throws HieroException { - // Fully qualify AccountId to ensure no ambiguity - com.hedera.hashgraph.sdk.AccountId accountId = com.hedera.hashgraph.sdk.AccountId.fromString("0.0.12345"); + AccountId accountId = AccountId.fromString("0.0.12345"); Hbar expectedBalance = new Hbar(10); // Mock the response @@ -37,7 +38,6 @@ public void testGetAccountBalance_ValidPositiveBalance() throws HieroException { ArgumentMatchers.any(AccountBalanceRequest.class) )).thenReturn(mockResponse); - // Explicit cast ensures we're calling the correct method Hbar balance = accountClientImpl.getAccountBalance(accountId); assertEquals(expectedBalance, balance); @@ -45,7 +45,7 @@ public void testGetAccountBalance_ValidPositiveBalance() throws HieroException { @Test public void testGetAccountBalance_ZeroBalance() throws HieroException { - com.hedera.hashgraph.sdk.AccountId accountId = com.hedera.hashgraph.sdk.AccountId.fromString("0.0.67890"); + AccountId accountId = AccountId.fromString("0.0.67890"); Hbar expectedBalance = new Hbar(0); AccountBalanceResponse mockResponse = mock(AccountBalanceResponse.class); @@ -62,30 +62,27 @@ public void testGetAccountBalance_ZeroBalance() throws HieroException { @Test public void testGetAccountBalance_InvalidAccount_ThrowsException() throws HieroException { - // Use a valid but non-existing account ID to simulate invalid behavior - com.hedera.hashgraph.sdk.AccountId invalidAccountId = com.hedera.hashgraph.sdk.AccountId.fromString("0.0.9999999"); // Simulate invalid or non-existing account - + 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((com.hedera.hashgraph.sdk.AccountId) null); + accountClientImpl.getAccountBalance((AccountId) null); }); } - @Test public void testGetAccountBalance_ProtocolLayerClientFails() throws HieroException { - com.hedera.hashgraph.sdk.AccountId accountId = com.hedera.hashgraph.sdk.AccountId.fromString("0.0.12345"); + AccountId accountId = AccountId.fromString("0.0.12345"); when(mockProtocolLayerClient.executeAccountBalanceQuery( ArgumentMatchers.any(AccountBalanceRequest.class) diff --git a/pom.xml b/pom.xml index b4de8060..273d22a5 100644 --- a/pom.xml +++ b/pom.xml @@ -159,9 +159,10 @@ junit-jupiter ${junit.version} - + org.mockito mockito-core + 5.11.0 org.slf4j