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

test: Implemented test to validate the functionality of the transferNft() and transferNfts() #150

Merged
merged 1 commit into from
Feb 5, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
import com.openelements.hiero.base.protocol.TokenCreateRequest;
import com.openelements.hiero.base.protocol.TokenCreateResult;
import com.openelements.hiero.base.protocol.TokenTransferRequest;
import com.openelements.hiero.base.protocol.TokenTransferResult;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -27,6 +30,7 @@ public class NftClientImplTest {
NftClientImpl nftClientImpl;

ArgumentCaptor<TokenCreateRequest> tokenRequestCaptor = ArgumentCaptor.forClass(TokenCreateRequest.class);
ArgumentCaptor<TokenTransferRequest> tokenTransferCaptor = ArgumentCaptor.forClass(TokenTransferRequest.class);

@BeforeEach
public void setup() {
Expand Down Expand Up @@ -202,4 +206,111 @@ void testCreateNftForNullParam() {
() -> nftClientImpl.createNftType(null, null, null, null, (PrivateKey) null)
);
}

@Test
void testTransferNft() throws HieroException {
// mock
final TokenTransferResult tokenTransferResult = Mockito.mock(TokenTransferResult.class);

// given
final TokenId tokenId = TokenId.fromString("1.2.3");
final long serialNumber = 1L;
final AccountId fromAccount = AccountId.fromString("1.2.3");
final AccountId toAccount = AccountId.fromString("4.5.6");
final PrivateKey fromAccountKey = PrivateKey.generateECDSA();

// when
when(protocolLayerClient.executeTransferTransaction(any(TokenTransferRequest.class)))
.thenReturn(tokenTransferResult);
nftClientImpl.transferNft(tokenId, serialNumber, fromAccount, fromAccountKey, toAccount);

// then
verify(protocolLayerClient, times(1))
.executeTransferTransaction(tokenTransferCaptor.capture());

final TokenTransferRequest request = tokenTransferCaptor.getValue();
Assertions.assertEquals(tokenId, request.tokenId());
Assertions.assertEquals(List.of(serialNumber), request.serials());
Assertions.assertEquals(fromAccount, request.sender());
Assertions.assertEquals(toAccount, request.receiver());
Assertions.assertEquals(fromAccountKey, request.senderKey());
}

@Test
void testTransferNfts() throws HieroException {
// mock
final TokenTransferResult tokenTransferResult = Mockito.mock(TokenTransferResult.class);

// given
final TokenId tokenId = TokenId.fromString("1.2.3");
final List<Long> serialNumbers = List.of(1L, 2L);
final AccountId fromAccount = AccountId.fromString("1.2.3");
final AccountId toAccount = AccountId.fromString("4.5.6");
final PrivateKey fromAccountKey = PrivateKey.generateECDSA();

// when
when(protocolLayerClient.executeTransferTransaction(any(TokenTransferRequest.class)))
.thenReturn(tokenTransferResult);
nftClientImpl.transferNfts(tokenId, serialNumbers, fromAccount, fromAccountKey, toAccount);

// then
verify(protocolLayerClient, times(1))
.executeTransferTransaction(tokenTransferCaptor.capture());

final TokenTransferRequest request = tokenTransferCaptor.getValue();
Assertions.assertEquals(tokenId, request.tokenId());
Assertions.assertEquals(serialNumbers, request.serials());
Assertions.assertEquals(fromAccount, request.sender());
Assertions.assertEquals(toAccount, request.receiver());
Assertions.assertEquals(fromAccountKey, request.senderKey());
}

@Test
void testTransferNftThrowsExceptionForInvalidTokenId() throws HieroException {
//given
final TokenId tokenId = TokenId.fromString("1.2.3");
final AccountId fromAccount = AccountId.fromString("1.2.3");
final AccountId toAccount = AccountId.fromString("4.5.6");
final PrivateKey fromAccountKey = PrivateKey.generateECDSA();
final long serial = 1L;

//when
when(protocolLayerClient.executeTransferTransaction(any(TokenTransferRequest.class)))
.thenThrow(new HieroException("Failed to execute transaction of type TokenTransferTransaction"));

//then
Assertions.assertThrows(HieroException.class, () -> nftClientImpl.transferNft(tokenId, serial,
fromAccount, fromAccountKey, toAccount));
}

@Test
void testTransferNftThrowsExceptionForInvalidSerial() {
final String e1Message = "serial must be positive";
final String e2Message = "either amount or serial must be provided";

//given
final TokenId tokenId = TokenId.fromString("1.2.3");
final AccountId fromAccount = AccountId.fromString("1.2.3");
final AccountId toAccount = AccountId.fromString("4.5.6");
final PrivateKey fromAccountKey = PrivateKey.generateECDSA();
final long serial = -1L;

IllegalArgumentException e1 = Assertions.assertThrows(IllegalArgumentException.class,
() -> nftClientImpl.transferNft(tokenId, serial, fromAccount, fromAccountKey, toAccount));
Assertions.assertEquals(e1Message, e1.getMessage());

IllegalArgumentException e2 = Assertions.assertThrows(IllegalArgumentException.class,
() -> nftClientImpl.transferNfts(tokenId, List.of(), fromAccount, fromAccountKey, toAccount));
Assertions.assertEquals(e2Message, e2.getMessage());
}

@Test
void testTransferNftNullParams() {
Assertions.assertThrows(NullPointerException.class,
() -> nftClientImpl.transferNft(null, 1L, null,
null, null));
Assertions.assertThrows(NullPointerException.class,
() -> nftClientImpl.transferNfts(null, null, null,
null, null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.TokenId;
import com.openelements.hiero.base.HieroException;
import com.openelements.hiero.base.data.Account;
import com.openelements.hiero.base.AccountClient;
import com.openelements.hiero.base.NftClient;
Expand Down Expand Up @@ -118,6 +119,20 @@ void transferNft() throws Exception {
});
}

@Test
void transferNftThrowsExceptionForInvalidTokenId() throws Exception {
//given
final TokenId tokenId = TokenId.fromString("1.2.3");
final Account treasuryAccount = accountClient.createAccount(1);
final Account userAccount = accountClient.createAccount(1);
final long serial = 1L;
//then
Assertions.assertThrows(HieroException.class, () -> nftClient.transferNft(tokenId, serial,
treasuryAccount.accountId(), treasuryAccount.privateKey(), userAccount.accountId()));
Assertions.assertThrows(HieroException.class, () -> nftClient.transferNfts(tokenId, List.of(serial),
treasuryAccount.accountId(), treasuryAccount.privateKey(), userAccount.accountId()));
}

@Test
void mintNftByNewUserAndTransferByAnotherUser() throws Exception {
//given
Expand Down