99import com .openelements .hiero .base .protocol .ProtocolLayerClient ;
1010import com .openelements .hiero .base .protocol .data .TokenCreateRequest ;
1111import com .openelements .hiero .base .protocol .data .TokenCreateResult ;
12+ import com .openelements .hiero .base .protocol .TokenCreateRequest ;
13+ import com .openelements .hiero .base .protocol .TokenCreateResult ;
14+ import com .openelements .hiero .base .protocol .TokenTransferRequest ;
15+ import com .openelements .hiero .base .protocol .TokenTransferResult ;
16+ import java .util .List ;
1217import org .junit .jupiter .api .Assertions ;
1318import org .junit .jupiter .api .BeforeEach ;
1419import org .junit .jupiter .api .Test ;
@@ -27,6 +32,7 @@ public class NftClientImplTest {
2732 NftClientImpl nftClientImpl ;
2833
2934 ArgumentCaptor <TokenCreateRequest > tokenRequestCaptor = ArgumentCaptor .forClass (TokenCreateRequest .class );
35+ ArgumentCaptor <TokenTransferRequest > tokenTransferCaptor = ArgumentCaptor .forClass (TokenTransferRequest .class );
3036
3137 @ BeforeEach
3238 public void setup () {
@@ -202,4 +208,111 @@ void testCreateNftForNullParam() {
202208 () -> nftClientImpl .createNftType (null , null , null , null , (PrivateKey ) null )
203209 );
204210 }
211+
212+ @ Test
213+ void testTransferNft () throws HieroException {
214+ // mock
215+ final TokenTransferResult tokenTransferResult = Mockito .mock (TokenTransferResult .class );
216+
217+ // given
218+ final TokenId tokenId = TokenId .fromString ("1.2.3" );
219+ final long serialNumber = 1L ;
220+ final AccountId fromAccount = AccountId .fromString ("1.2.3" );
221+ final AccountId toAccount = AccountId .fromString ("4.5.6" );
222+ final PrivateKey fromAccountKey = PrivateKey .generateECDSA ();
223+
224+ // when
225+ when (protocolLayerClient .executeTransferTransaction (any (TokenTransferRequest .class )))
226+ .thenReturn (tokenTransferResult );
227+ nftClientImpl .transferNft (tokenId , serialNumber , fromAccount , fromAccountKey , toAccount );
228+
229+ // then
230+ verify (protocolLayerClient , times (1 ))
231+ .executeTransferTransaction (tokenTransferCaptor .capture ());
232+
233+ final TokenTransferRequest request = tokenTransferCaptor .getValue ();
234+ Assertions .assertEquals (tokenId , request .tokenId ());
235+ Assertions .assertEquals (List .of (serialNumber ), request .serials ());
236+ Assertions .assertEquals (fromAccount , request .sender ());
237+ Assertions .assertEquals (toAccount , request .receiver ());
238+ Assertions .assertEquals (fromAccountKey , request .senderKey ());
239+ }
240+
241+ @ Test
242+ void testTransferNfts () throws HieroException {
243+ // mock
244+ final TokenTransferResult tokenTransferResult = Mockito .mock (TokenTransferResult .class );
245+
246+ // given
247+ final TokenId tokenId = TokenId .fromString ("1.2.3" );
248+ final List <Long > serialNumbers = List .of (1L , 2L );
249+ final AccountId fromAccount = AccountId .fromString ("1.2.3" );
250+ final AccountId toAccount = AccountId .fromString ("4.5.6" );
251+ final PrivateKey fromAccountKey = PrivateKey .generateECDSA ();
252+
253+ // when
254+ when (protocolLayerClient .executeTransferTransaction (any (TokenTransferRequest .class )))
255+ .thenReturn (tokenTransferResult );
256+ nftClientImpl .transferNfts (tokenId , serialNumbers , fromAccount , fromAccountKey , toAccount );
257+
258+ // then
259+ verify (protocolLayerClient , times (1 ))
260+ .executeTransferTransaction (tokenTransferCaptor .capture ());
261+
262+ final TokenTransferRequest request = tokenTransferCaptor .getValue ();
263+ Assertions .assertEquals (tokenId , request .tokenId ());
264+ Assertions .assertEquals (serialNumbers , request .serials ());
265+ Assertions .assertEquals (fromAccount , request .sender ());
266+ Assertions .assertEquals (toAccount , request .receiver ());
267+ Assertions .assertEquals (fromAccountKey , request .senderKey ());
268+ }
269+
270+ @ Test
271+ void testTransferNftThrowsExceptionForInvalidTokenId () throws HieroException {
272+ //given
273+ final TokenId tokenId = TokenId .fromString ("1.2.3" );
274+ final AccountId fromAccount = AccountId .fromString ("1.2.3" );
275+ final AccountId toAccount = AccountId .fromString ("4.5.6" );
276+ final PrivateKey fromAccountKey = PrivateKey .generateECDSA ();
277+ final long serial = 1L ;
278+
279+ //when
280+ when (protocolLayerClient .executeTransferTransaction (any (TokenTransferRequest .class )))
281+ .thenThrow (new HieroException ("Failed to execute transaction of type TokenTransferTransaction" ));
282+
283+ //then
284+ Assertions .assertThrows (HieroException .class , () -> nftClientImpl .transferNft (tokenId , serial ,
285+ fromAccount , fromAccountKey , toAccount ));
286+ }
287+
288+ @ Test
289+ void testTransferNftThrowsExceptionForInvalidSerial () {
290+ final String e1Message = "serial must be positive" ;
291+ final String e2Message = "either amount or serial must be provided" ;
292+
293+ //given
294+ final TokenId tokenId = TokenId .fromString ("1.2.3" );
295+ final AccountId fromAccount = AccountId .fromString ("1.2.3" );
296+ final AccountId toAccount = AccountId .fromString ("4.5.6" );
297+ final PrivateKey fromAccountKey = PrivateKey .generateECDSA ();
298+ final long serial = -1L ;
299+
300+ IllegalArgumentException e1 = Assertions .assertThrows (IllegalArgumentException .class ,
301+ () -> nftClientImpl .transferNft (tokenId , serial , fromAccount , fromAccountKey , toAccount ));
302+ Assertions .assertEquals (e1Message , e1 .getMessage ());
303+
304+ IllegalArgumentException e2 = Assertions .assertThrows (IllegalArgumentException .class ,
305+ () -> nftClientImpl .transferNfts (tokenId , List .of (), fromAccount , fromAccountKey , toAccount ));
306+ Assertions .assertEquals (e2Message , e2 .getMessage ());
307+ }
308+
309+ @ Test
310+ void testTransferNftNullParams () {
311+ Assertions .assertThrows (NullPointerException .class ,
312+ () -> nftClientImpl .transferNft (null , 1L , null ,
313+ null , null ));
314+ Assertions .assertThrows (NullPointerException .class ,
315+ () -> nftClientImpl .transferNfts (null , null , null ,
316+ null , null ));
317+ }
205318}
0 commit comments