Skip to content

Commit

Permalink
Implemented tests for createAccount method
Browse files Browse the repository at this point in the history
Signed-off-by: Lemeri123 <[email protected]>
  • Loading branch information
Lemeri123 committed Dec 19, 2024
1 parent bce1fb8 commit 75f6c2a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,23 @@ public AccountClientImpl(@NonNull final ProtocolLayerClient client) {
@NonNull
@Override
public Account createAccount(@NonNull Hbar initialBalance) throws HieroException {
final AccountCreateRequest request = AccountCreateRequest.of(initialBalance);
final AccountCreateResult result = client.executeAccountCreateTransaction(request);
return result.newAccount();
if (initialBalance == null) {
throw new NullPointerException("initialBalance must not be null");
}

if (initialBalance.toTinybars() < 0) {
throw new HieroException("Invalid initial balance: must be non-negative");
}

try {
final AccountCreateRequest request = AccountCreateRequest.of(initialBalance);
final AccountCreateResult result = client.executeAccountCreateTransaction(request);
return result.newAccount();
} catch (IllegalArgumentException e) {
throw new HieroException("Invalid initial balance: " + e.getMessage(), e);
}
}


@Override
public void deleteAccount(@NonNull Account account) throws HieroException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

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.openelements.hiero.base.HieroException;
import com.openelements.hiero.base.protocol.AccountBalanceRequest;
import com.openelements.hiero.base.protocol.AccountBalanceResponse;
import com.openelements.hiero.base.protocol.AccountCreateRequest;
import com.openelements.hiero.base.protocol.AccountCreateResult;
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.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

public class AccountClientImplTest {
Expand Down Expand Up @@ -92,4 +96,53 @@ public void testGetAccountBalance_ProtocolLayerClientFails() throws HieroExcepti
accountClientImpl.getAccountBalance(accountId);
});
}
}

//tests for createAccount method
@Test
void testCreateAccount_successful() throws HieroException {
Hbar initialBalance = Hbar.from(100);

AccountCreateResult mockResult = mock(AccountCreateResult.class);
Account mockAccount = mock(Account.class);

when(mockAccount.accountId()).thenReturn(AccountId.fromString("0.0.12345"));
when(mockResult.newAccount()).thenReturn(mockAccount);
when(mockProtocolLayerClient.executeAccountCreateTransaction(any(AccountCreateRequest.class)))
.thenReturn(mockResult);

Account result = accountClientImpl.createAccount(initialBalance);

assertNotNull(result);
assertEquals(AccountId.fromString("0.0.12345"), result.accountId());
verify(mockProtocolLayerClient, times(1))
.executeAccountCreateTransaction(any(AccountCreateRequest.class));
}

@Test
void testCreateAccount_invalidInitialBalance_null() {
Hbar initialBalance = null;

assertThrows(NullPointerException.class, () -> accountClientImpl.createAccount(initialBalance));
}

@Test
void testCreateAccount_invalidInitialBalance_negative() {
Hbar initialBalance = Hbar.from(-100);
HieroException exception = assertThrows(HieroException.class,
() -> accountClientImpl.createAccount(initialBalance));

assertTrue(exception.getMessage().contains("Invalid initial balance"));
}


@Test
void testCreateAccount_hieroExceptionThrown() throws HieroException {
Hbar initialBalance = Hbar.from(100);

when(mockProtocolLayerClient.executeAccountCreateTransaction(any(AccountCreateRequest.class)))
.thenThrow(new HieroException("Transaction failed"));

Exception exception = assertThrows(HieroException.class, () -> accountClientImpl.createAccount(initialBalance));
assertEquals("Transaction failed", exception.getMessage());
}
}

0 comments on commit 75f6c2a

Please sign in to comment.