Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.openelements.hiero.base.test;

import com.openelements.hiero.base.implementation.AccountClientImpl;
import com.hedera.hashgraph.sdk.AccountId;
import com.openelements.hiero.base.data.Account;
import com.hedera.hashgraph.sdk.Hbar;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.PublicKey;

import com.openelements.hiero.base.implementation.AccountClientImpl;
import com.openelements.hiero.base.data.Account;
import com.openelements.hiero.base.HieroException;
import com.openelements.hiero.base.protocol.data.AccountBalanceRequest;
import com.openelements.hiero.base.protocol.data.AccountBalanceResponse;
Expand All @@ -14,9 +17,17 @@
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;

public class AccountClientImplTest {

Expand All @@ -34,7 +45,6 @@ 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);

Expand Down Expand Up @@ -97,7 +107,6 @@ public void testGetAccountBalance_ProtocolLayerClientFails() throws HieroExcepti
});
}

//tests for createAccount method
@Test
void testCreateAccount_successful() throws HieroException {
Hbar initialBalance = Hbar.from(100);
Expand Down Expand Up @@ -145,4 +154,67 @@ void testCreateAccount_hieroExceptionThrown() throws HieroException {
Exception exception = assertThrows(HieroException.class, () -> accountClientImpl.createAccount(initialBalance));
assertEquals("Transaction failed", exception.getMessage());
}


@Test
void DeleteAccount_nullAccount_throwsNullPointerException() {
assertThrows(NullPointerException.class, () -> accountClientImpl.deleteAccount(null));
}


@Test
void DeleteAccount_successful() throws HieroException {
Account mockAccount = mock(Account.class);

accountClientImpl.deleteAccount(mockAccount);

verify(mockProtocolLayerClient, times(1))
.executeAccountDeleteTransaction(any());
}


@Test
void DeleteAccount_withTransfer_nullFromAccount_throwsException() {
Account toAccount = mock(Account.class);

assertThrows(NullPointerException.class, () -> {
accountClientImpl.deleteAccount(null, toAccount);
});
}

@Test
void deleteAccount_withNullToAccount_shouldNotThrow() {
AccountId accountId = AccountId.fromString("0.0.123");
PrivateKey privateKey = PrivateKey.generate();
Account fromAccount = new Account(accountId, privateKey.getPublicKey(), privateKey);

assertDoesNotThrow(() -> accountClientImpl.deleteAccount(fromAccount, null),
"deleteAccount should not throw when toAccount is null (should fallback to operator)");
}

@Test
void deleteAccount_withTransfer_throwsHieroException() throws HieroException {
PrivateKey fromPrivateKey = PrivateKey.generate();
PublicKey fromPublicKey = fromPrivateKey.getPublicKey();
AccountId fromAccountId = AccountId.fromString("0.0.1234");

PrivateKey toPrivateKey = PrivateKey.generate();
PublicKey toPublicKey = toPrivateKey.getPublicKey();
AccountId toAccountId = AccountId.fromString("0.0.5678");

Account fromAccount = new Account(fromAccountId, fromPublicKey, fromPrivateKey);
Account toAccount = new Account(toAccountId, toPublicKey, toPrivateKey);

doThrow(new HieroException("Transfer deletion failed"))
.when(mockProtocolLayerClient)
.executeAccountDeleteTransaction(any());

HieroException exception = assertThrows(HieroException.class, () ->
accountClientImpl.deleteAccount(fromAccount, toAccount)
);

assertEquals("Transfer deletion failed", exception.getMessage());

verify(mockProtocolLayerClient, times(1)).executeAccountDeleteTransaction(any());
}
}