Skip to content

Commit 4b81c80

Browse files
Merge pull request #151 from manishdait/issue-94
test: Implemented test to validate functionality for burnNft() & burnNfts()
2 parents 1d3d731 + d586ad1 commit 4b81c80

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed

hiero-enterprise-base/src/test/java/com/openelements/hiero/base/test/NftClientImplTest.java

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
import com.openelements.hiero.base.data.Account;
1414
import com.openelements.hiero.base.implementation.NftClientImpl;
1515
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
16+
import com.openelements.hiero.base.protocol.data.TokenBurnRequest;
17+
import com.openelements.hiero.base.protocol.data.TokenBurnResult;
1618
import com.openelements.hiero.base.protocol.data.TokenCreateRequest;
1719
import com.openelements.hiero.base.protocol.data.TokenCreateResult;
1820
import com.openelements.hiero.base.protocol.data.TokenTransferRequest;
1921
import com.openelements.hiero.base.protocol.data.TokenTransferResult;
2022
import java.util.List;
23+
import java.util.Set;
2124
import org.junit.jupiter.api.Assertions;
2225
import org.junit.jupiter.api.BeforeEach;
2326
import org.junit.jupiter.api.Test;
@@ -31,6 +34,7 @@ public class NftClientImplTest {
3134

3235
ArgumentCaptor<TokenCreateRequest> tokenRequestCaptor = ArgumentCaptor.forClass(TokenCreateRequest.class);
3336
ArgumentCaptor<TokenTransferRequest> tokenTransferCaptor = ArgumentCaptor.forClass(TokenTransferRequest.class);
37+
ArgumentCaptor<TokenBurnRequest> tokenBurnCaptor = ArgumentCaptor.forClass(TokenBurnRequest.class);
3438

3539
@BeforeEach
3640
public void setup() {
@@ -313,4 +317,127 @@ void testTransferNftNullParams() {
313317
() -> nftClientImpl.transferNfts(null, null, null,
314318
null, null));
315319
}
320+
321+
@Test
322+
void testBurnNft() throws HieroException {
323+
final PrivateKey privateKey = PrivateKey.generateECDSA();
324+
final TokenBurnResult tokenBurnRequest = Mockito.mock(TokenBurnResult.class);
325+
326+
final TokenId tokenId = TokenId.fromString("1.2.3");
327+
final long serials = 1L;
328+
329+
when(operationalAccount.privateKey()).thenReturn(privateKey);
330+
when(protocolLayerClient.executeBurnTokenTransaction(any(TokenBurnRequest.class)))
331+
.thenReturn(tokenBurnRequest);
332+
333+
nftClientImpl.burnNft(tokenId, serials);
334+
335+
verify(operationalAccount, times(1)).privateKey();
336+
verify(protocolLayerClient, times(1))
337+
.executeBurnTokenTransaction(tokenBurnCaptor.capture());
338+
339+
final TokenBurnRequest request = tokenBurnCaptor.getValue();
340+
Assertions.assertEquals(tokenId, request.tokenId());
341+
Assertions.assertEquals(Set.of(serials), request.serials());
342+
Assertions.assertEquals(privateKey, request.supplyKey());
343+
}
344+
345+
@Test
346+
void testBurnNftWithSupplyKey() throws HieroException {
347+
final TokenBurnResult tokenBurnRequest = Mockito.mock(TokenBurnResult.class);
348+
349+
final TokenId tokenId = TokenId.fromString("1.2.3");
350+
final long serials = 1L;
351+
final PrivateKey privateKey = PrivateKey.generateECDSA();
352+
353+
when(protocolLayerClient.executeBurnTokenTransaction(any(TokenBurnRequest.class)))
354+
.thenReturn(tokenBurnRequest);
355+
356+
nftClientImpl.burnNft(tokenId, serials, privateKey);
357+
358+
verify(protocolLayerClient, times(1))
359+
.executeBurnTokenTransaction(tokenBurnCaptor.capture());
360+
361+
final TokenBurnRequest request = tokenBurnCaptor.getValue();
362+
Assertions.assertEquals(tokenId, request.tokenId());
363+
Assertions.assertEquals(Set.of(serials), request.serials());
364+
Assertions.assertEquals(privateKey, request.supplyKey());
365+
}
366+
367+
@Test
368+
void testBurnNfts() throws HieroException {
369+
final PrivateKey privateKey = PrivateKey.generateECDSA();
370+
final TokenBurnResult tokenBurnRequest = Mockito.mock(TokenBurnResult.class);
371+
372+
final TokenId tokenId = TokenId.fromString("1.2.3");
373+
final Set<Long> serials = Set.of(1L);
374+
375+
when(operationalAccount.privateKey()).thenReturn(privateKey);
376+
when(protocolLayerClient.executeBurnTokenTransaction(any(TokenBurnRequest.class)))
377+
.thenReturn(tokenBurnRequest);
378+
379+
nftClientImpl.burnNfts(tokenId, serials);
380+
381+
verify(operationalAccount, times(1)).privateKey();
382+
verify(protocolLayerClient, times(1))
383+
.executeBurnTokenTransaction(tokenBurnCaptor.capture());
384+
385+
final TokenBurnRequest request = tokenBurnCaptor.getValue();
386+
Assertions.assertEquals(tokenId, request.tokenId());
387+
Assertions.assertEquals(serials, request.serials());
388+
Assertions.assertEquals(privateKey, request.supplyKey());
389+
}
390+
391+
@Test
392+
void testBurnNftsWithSupplyKey() throws HieroException {
393+
final TokenBurnResult tokenBurnRequest = Mockito.mock(TokenBurnResult.class);
394+
395+
final TokenId tokenId = TokenId.fromString("1.2.3");
396+
final Set<Long> serials = Set.of(1L);
397+
final PrivateKey privateKey = PrivateKey.generateECDSA();
398+
399+
when(protocolLayerClient.executeBurnTokenTransaction(any(TokenBurnRequest.class)))
400+
.thenReturn(tokenBurnRequest);
401+
402+
nftClientImpl.burnNfts(tokenId, serials, privateKey);
403+
404+
verify(protocolLayerClient, times(1))
405+
.executeBurnTokenTransaction(tokenBurnCaptor.capture());
406+
407+
final TokenBurnRequest request = tokenBurnCaptor.getValue();
408+
Assertions.assertEquals(tokenId, request.tokenId());
409+
Assertions.assertEquals(serials, request.serials());
410+
Assertions.assertEquals(privateKey, request.supplyKey());
411+
}
412+
413+
@Test
414+
void testBurnNftThrowsExceptionForInvalidTokenId() throws HieroException {
415+
final PrivateKey privateKey = PrivateKey.generateECDSA();
416+
final TokenId tokenId = TokenId.fromString("1.2.3");
417+
final long serials = 1L;
418+
419+
when(operationalAccount.privateKey()).thenReturn(privateKey);
420+
when(protocolLayerClient.executeBurnTokenTransaction(any(TokenBurnRequest.class)))
421+
.thenThrow(new HieroException("Failed to execute transaction of type TokenBurnTransaction"));
422+
423+
Assertions.assertThrows(HieroException.class , () -> nftClientImpl.burnNft(tokenId, serials));
424+
Assertions.assertThrows(HieroException.class , () -> nftClientImpl.burnNft(tokenId, serials, privateKey));
425+
Assertions.assertThrows(HieroException.class , () -> nftClientImpl.burnNfts(tokenId, Set.of(serials)));
426+
Assertions.assertThrows(HieroException.class , () -> nftClientImpl.burnNfts(tokenId, Set.of(serials), privateKey));
427+
}
428+
429+
@Test
430+
void testBurnNftNullParam() {
431+
Assertions.assertThrows(NullPointerException.class,
432+
() -> nftClientImpl.burnNft(null, 0));
433+
434+
Assertions.assertThrows(NullPointerException.class,
435+
() -> nftClientImpl.burnNft(null, 0, null));
436+
437+
Assertions.assertThrows(NullPointerException.class,
438+
() -> nftClientImpl.burnNfts(null, null));
439+
440+
Assertions.assertThrows(NullPointerException.class,
441+
() -> nftClientImpl.burnNfts(null, null, null));
442+
}
316443
}

hiero-enterprise-spring/src/test/java/com/openelements/hiero/spring/test/NftClientTests.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.openelements.hiero.base.NftClient;
1010
import java.nio.charset.StandardCharsets;
1111
import java.util.List;
12+
import java.util.Set;
1213
import org.junit.jupiter.api.Assertions;
1314
import org.junit.jupiter.api.Test;
1415
import org.springframework.beans.factory.annotation.Autowired;
@@ -153,4 +154,74 @@ void mintNftByNewUserAndTransferByAnotherUser() throws Exception {
153154
nftClient.transferNft(tokenId, serial, treasuryAccount, userAccount.accountId());
154155
});
155156
}
157+
158+
@Test
159+
void burnNft() throws HieroException {
160+
final String name = "Test NFT";
161+
final String symbol = "TST";
162+
final byte[] metadata1 = "https://example.com/metadata".getBytes(StandardCharsets.UTF_8);
163+
final byte[] metadata2 = "https://example.com/metadata".getBytes(StandardCharsets.UTF_8);
164+
final Account supplierAccount = accountClient.createAccount();
165+
166+
final TokenId tokenId = nftClient.createNftType(name, symbol);
167+
final List<Long> serial = nftClient.mintNfts(tokenId, metadata1, metadata2);
168+
169+
Assertions.assertDoesNotThrow(() -> nftClient.burnNft(tokenId, serial.get(0)));
170+
Assertions.assertDoesNotThrow(() -> nftClient.burnNft(tokenId, serial.get(1),supplierAccount.privateKey()));
171+
}
172+
173+
@Test
174+
void burnNfts() throws HieroException {
175+
final String name = "Test NFT";
176+
final String symbol = "TST";
177+
final byte[] metadata1 = "https://example.com/metadata".getBytes(StandardCharsets.UTF_8);
178+
final byte[] metadata2 = "https://example.com/metadata".getBytes(StandardCharsets.UTF_8);
179+
final Account supplierAccount = accountClient.createAccount();
180+
181+
final TokenId tokenId = nftClient.createNftType(name, symbol);
182+
final List<Long> serials = nftClient.mintNfts(tokenId, metadata1, metadata2);
183+
184+
Assertions.assertDoesNotThrow(() -> nftClient.burnNfts(tokenId, Set.of(serials.get(0))));
185+
Assertions.assertDoesNotThrow(() -> nftClient.burnNfts(tokenId, Set.of(serials.get(1)),supplierAccount.privateKey()));
186+
}
187+
188+
@Test
189+
void burnNftThrowExceptionForUnMintToken() throws HieroException {
190+
final String name = "Test NFT";
191+
final String symbol = "TST";
192+
final TokenId tokenId = nftClient.createNftType(name, symbol);
193+
final Account supplierAccount = accountClient.createAccount();
194+
final long serial = 1L;
195+
196+
Assertions.assertThrows(HieroException.class, () -> nftClient.burnNft(tokenId, serial));
197+
Assertions.assertThrows(HieroException.class,
198+
() -> nftClient.burnNft(tokenId, serial, supplierAccount.privateKey()));
199+
Assertions.assertThrows(HieroException.class, () -> nftClient.burnNfts(tokenId, Set.of(serial)));
200+
Assertions.assertThrows(HieroException.class,
201+
() -> nftClient.burnNfts(tokenId, Set.of(serial), supplierAccount.privateKey()));
202+
}
203+
204+
@Test
205+
void burnNftThrowExceptionForInvalidId() {
206+
final TokenId tokenId = TokenId.fromString("1.2.3");
207+
final PrivateKey privateKey = PrivateKey.generateECDSA();
208+
final long serial = 1L;
209+
210+
Assertions.assertThrows(HieroException.class, () -> nftClient.burnNft(tokenId, serial));
211+
Assertions.assertThrows(HieroException.class, () -> nftClient.burnNft(tokenId, serial, privateKey));
212+
213+
Assertions.assertThrows(HieroException.class, () -> nftClient.burnNfts(tokenId, Set.of(serial)));
214+
Assertions.assertThrows(HieroException.class, () -> nftClient.burnNfts(tokenId, Set.of(serial), privateKey));
215+
}
216+
217+
@Test
218+
void burnNftNullParam() {
219+
Assertions.assertThrows(NullPointerException.class, () -> nftClient.burnNft(null, 0));
220+
Assertions.assertThrows(NullPointerException.class,
221+
() -> nftClient.burnNft(null, 0, null));
222+
223+
Assertions.assertThrows(NullPointerException.class, () -> nftClient.burnNfts(null, null));
224+
Assertions.assertThrows(NullPointerException.class,
225+
() -> nftClient.burnNfts(null, null, null));
226+
}
156227
}

0 commit comments

Comments
 (0)