Skip to content
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

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions hiero-enterprise-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-okhttp</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.openelements.hiero.base.implementation;

import com.hedera.hashgraph.sdk.AccountId;

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

import com.hedera.hashgraph.sdk.Hbar;
import com.openelements.hiero.base.data.Account;
Expand Down
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.*;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the asterisk wildcard

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.*;

Choose a reason for hiding this comment

The 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);
});
}
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Lemeri123 marked this conversation as resolved.
Show resolved Hide resolved
<version> 5.11.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Loading