-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added tests for AccountClientImpl.getAccountBalance functionality #123
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the asterisk wildcard There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But if I remove it then I get errors since they set up the testing framework and mocking utilities for the test method |
||
import static org.mockito.Mockito.*; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the asterisk wildcard |
||
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); | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this change