Skip to content

Commit f83b457

Browse files
Merge pull request #144 from manishdait/issue-90
test: Implemented test to validate the functionality of the createNftType
2 parents 609ac88 + 70df9c2 commit f83b457

File tree

3 files changed

+264
-44
lines changed

3 files changed

+264
-44
lines changed

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

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.junit.jupiter.api.Assertions;
1818
import org.junit.jupiter.api.BeforeEach;
1919
import org.junit.jupiter.api.Test;
20-
import org.mockito.Mock;
2120
import org.mockito.Mockito;
2221

2322
import java.time.Instant;
@@ -35,15 +34,11 @@
3534
public class FileClientImplTest {
3635
ProtocolLayerClient protocolLayerClient;
3736
FileClientImpl fileClientImpl;
38-
39-
@Mock
40-
private FileClientImpl fileClient;
4137

4238
@BeforeEach
4339
void setup() {
4440
protocolLayerClient = Mockito.mock(ProtocolLayerClient.class);
4541
fileClientImpl = new FileClientImpl(protocolLayerClient);
46-
fileClient = new FileClientImpl(protocolLayerClient);
4742
}
4843

4944
@Test
@@ -277,47 +272,47 @@ void testGetFileSizeThrowsExceptionForNullId() {
277272
}
278273

279274

280-
//tests for deletefile method
281-
@Test
282-
public void testIsDeleted_FileIsDeleted() throws HieroException {
283-
// Given
284-
FileId fileId = FileId.fromString("0.0.123");
285-
FileInfoResponse response = mock(FileInfoResponse.class);
286-
when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))).thenReturn(response);
287-
when(response.deleted()).thenReturn(true);
288-
289-
// When
290-
boolean result = fileClient.isDeleted(fileId);
291-
292-
// Then
293-
assertTrue(result);
294-
}
295-
296-
@Test
297-
public void testIsDeleted_FileIsNotDeleted() throws HieroException {
298-
// Given
299-
FileId fileId = FileId.fromString("0.0.123");
300-
FileInfoResponse response = mock(FileInfoResponse.class);
301-
when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))).thenReturn(response);
302-
when(response.deleted()).thenReturn(false);
303-
304-
// When
305-
boolean result = fileClient.isDeleted(fileId);
306-
307-
// Then
308-
assertFalse(result);
309-
}
275+
//tests for deletefile method
276+
@Test
277+
public void testIsDeleted_FileIsDeleted() throws HieroException {
278+
// Given
279+
FileId fileId = FileId.fromString("0.0.123");
280+
FileInfoResponse response = mock(FileInfoResponse.class);
281+
when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))).thenReturn(response);
282+
when(response.deleted()).thenReturn(true);
283+
284+
// When
285+
boolean result = fileClientImpl.isDeleted(fileId);
286+
287+
// Then
288+
assertTrue(result);
289+
}
310290

311-
@Test
312-
public void testIsDeleted_NullFileId() {
313-
// When
314-
NullPointerException exception = assertThrows(NullPointerException.class, () -> {
315-
fileClient.isDeleted(null);
316-
});
291+
@Test
292+
public void testIsDeleted_FileIsNotDeleted() throws HieroException {
293+
// Given
294+
FileId fileId = FileId.fromString("0.0.123");
295+
FileInfoResponse response = mock(FileInfoResponse.class);
296+
when(protocolLayerClient.executeFileInfoQuery(any(FileInfoRequest.class))).thenReturn(response);
297+
when(response.deleted()).thenReturn(false);
298+
299+
// When
300+
boolean result = fileClientImpl.isDeleted(fileId);
301+
302+
// Then
303+
assertFalse(result);
304+
}
317305

318-
// Then
319-
assertEquals("fileId must not be null", exception.getMessage());
320-
}
306+
@Test
307+
public void testIsDeleted_NullFileId() {
308+
// When
309+
NullPointerException exception = assertThrows(NullPointerException.class, () -> {
310+
fileClientImpl.isDeleted(null);
311+
});
312+
313+
// Then
314+
assertEquals("fileId must not be null", exception.getMessage());
315+
}
321316

322317

323318
@Test
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
package com.openelements.hiero.base.test;
2+
3+
import com.hedera.hashgraph.sdk.AccountId;
4+
import com.hedera.hashgraph.sdk.PrivateKey;
5+
import com.hedera.hashgraph.sdk.TokenId;
6+
import com.openelements.hiero.base.HieroException;
7+
import com.openelements.hiero.base.data.Account;
8+
import com.openelements.hiero.base.implementation.NftClientImpl;
9+
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
10+
import com.openelements.hiero.base.protocol.TokenCreateRequest;
11+
import com.openelements.hiero.base.protocol.TokenCreateResult;
12+
import org.junit.jupiter.api.Assertions;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
15+
import org.mockito.ArgumentCaptor;
16+
import org.mockito.Mockito;
17+
18+
import static org.mockito.ArgumentMatchers.any;
19+
import static org.mockito.Mockito.when;
20+
import static org.mockito.Mockito.verify;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.times;
23+
24+
public class NftClientImplTest {
25+
ProtocolLayerClient protocolLayerClient;
26+
Account operationalAccount;
27+
NftClientImpl nftClientImpl;
28+
29+
ArgumentCaptor<TokenCreateRequest> tokenRequestCaptor = ArgumentCaptor.forClass(TokenCreateRequest.class);
30+
31+
@BeforeEach
32+
public void setup() {
33+
protocolLayerClient = Mockito.mock(ProtocolLayerClient.class);
34+
operationalAccount = Mockito.mock(Account.class);
35+
nftClientImpl = new NftClientImpl(protocolLayerClient, operationalAccount);
36+
}
37+
38+
@Test
39+
void testCreateNftWithNameAndSymbol() throws HieroException {
40+
// mock
41+
final PrivateKey privateKey = PrivateKey.generateECDSA();
42+
final AccountId accountId = AccountId.fromString("1.2.3");
43+
final TokenId tokenId = TokenId.fromString("1.2.3");
44+
final TokenCreateResult tokenCreateResult = mock(TokenCreateResult.class);
45+
46+
// given
47+
final String name = "TOKEN";
48+
final String symbol = "NFT";
49+
50+
// when
51+
when(operationalAccount.privateKey()).thenReturn(privateKey);
52+
when(operationalAccount.accountId()).thenReturn(accountId);
53+
when(protocolLayerClient.executeTokenCreateTransaction(any(TokenCreateRequest.class)))
54+
.thenReturn(tokenCreateResult);
55+
when(tokenCreateResult.tokenId()).thenReturn(tokenId);
56+
57+
final TokenId result = nftClientImpl.createNftType(name, symbol);
58+
59+
// then
60+
// 1st for treasuryKey and 2nd for supplier Key
61+
verify(operationalAccount, times(2)).privateKey();
62+
verify(operationalAccount, times(1)).accountId();
63+
verify(protocolLayerClient, times(1))
64+
.executeTokenCreateTransaction(tokenRequestCaptor.capture());
65+
66+
TokenCreateRequest tokenCreateRequest = tokenRequestCaptor.getValue();
67+
68+
Assertions.assertEquals(privateKey, tokenCreateRequest.treasuryKey());
69+
Assertions.assertEquals(privateKey, tokenCreateRequest.supplyKey());
70+
Assertions.assertEquals(accountId, tokenCreateRequest.treasuryAccountId());
71+
Assertions.assertEquals(name, tokenCreateRequest.name());
72+
Assertions.assertEquals(symbol, tokenCreateRequest.symbol());
73+
Assertions.assertEquals(tokenId, result);
74+
}
75+
76+
@Test
77+
void testCreateNftWithNameSymbolAndSupplier() throws HieroException {
78+
// mock
79+
final PrivateKey privateKey = PrivateKey.generateECDSA();
80+
final AccountId accountId = AccountId.fromString("1.2.3");
81+
final TokenId tokenId = TokenId.fromString("1.2.3");
82+
final TokenCreateResult tokenCreateResult = Mockito.mock(TokenCreateResult.class);
83+
84+
// given
85+
final String name = "TOKEN";
86+
final String symbol = "NFT";
87+
final PrivateKey supplierKey = PrivateKey.generateECDSA();
88+
89+
// when
90+
when(operationalAccount.privateKey()).thenReturn(privateKey);
91+
when(operationalAccount.accountId()).thenReturn(accountId);
92+
when(protocolLayerClient.executeTokenCreateTransaction(any(TokenCreateRequest.class)))
93+
.thenReturn(tokenCreateResult);
94+
when(tokenCreateResult.tokenId()).thenReturn(tokenId);
95+
96+
final TokenId result = nftClientImpl.createNftType(name, symbol, supplierKey);
97+
98+
//then
99+
verify(operationalAccount, times(1)).privateKey();
100+
verify(operationalAccount, times(1)).accountId();
101+
verify(protocolLayerClient, times(1))
102+
.executeTokenCreateTransaction(tokenRequestCaptor.capture());
103+
104+
TokenCreateRequest tokenCreateRequest = tokenRequestCaptor.getValue();
105+
106+
Assertions.assertEquals(privateKey, tokenCreateRequest.treasuryKey());
107+
Assertions.assertEquals(supplierKey, tokenCreateRequest.supplyKey());
108+
Assertions.assertEquals(accountId, tokenCreateRequest.treasuryAccountId());
109+
Assertions.assertEquals(name, tokenCreateRequest.name());
110+
Assertions.assertEquals(symbol, tokenCreateRequest.symbol());
111+
112+
Assertions.assertEquals(tokenId, result);
113+
}
114+
115+
@Test
116+
void testCreateNftWithNameSymbolTreasuryAccountIdAndKey() throws HieroException {
117+
// mock
118+
final PrivateKey privateKey = PrivateKey.generateECDSA();
119+
final TokenId tokenId = TokenId.fromString("1.2.3");
120+
final TokenCreateResult tokenCreateResult = Mockito.mock(TokenCreateResult.class);
121+
122+
// given
123+
final String name = "TOKEN";
124+
final String symbol = "NFT";
125+
final PrivateKey treasuryKey = PrivateKey.generateECDSA();
126+
final AccountId accountId = AccountId.fromString("1.2.3");
127+
128+
// when
129+
when(operationalAccount.privateKey()).thenReturn(privateKey);
130+
when(protocolLayerClient.executeTokenCreateTransaction(any(TokenCreateRequest.class)))
131+
.thenReturn(tokenCreateResult);
132+
when(tokenCreateResult.tokenId()).thenReturn(tokenId);
133+
134+
final TokenId result = nftClientImpl.createNftType(name, symbol, accountId, treasuryKey);
135+
136+
// then
137+
verify(operationalAccount, times(1)).privateKey();
138+
verify(protocolLayerClient, times(1))
139+
.executeTokenCreateTransaction(tokenRequestCaptor.capture());
140+
141+
TokenCreateRequest tokenCreateRequest = tokenRequestCaptor.getValue();
142+
143+
Assertions.assertEquals(treasuryKey, tokenCreateRequest.treasuryKey());
144+
Assertions.assertEquals(privateKey, tokenCreateRequest.supplyKey());
145+
Assertions.assertEquals(accountId, tokenCreateRequest.treasuryAccountId());
146+
Assertions.assertEquals(name, tokenCreateRequest.name());
147+
Assertions.assertEquals(symbol, tokenCreateRequest.symbol());
148+
149+
Assertions.assertEquals(tokenId, result);
150+
}
151+
152+
153+
@Test
154+
void testCreateNftWithAllParam() throws HieroException {
155+
// mock
156+
final TokenId tokenId = TokenId.fromString("1.2.3");
157+
final TokenCreateResult tokenCreateResult = Mockito.mock(TokenCreateResult.class);
158+
159+
// given
160+
final String name = "TOKEN";
161+
final String symbol = "NFT";
162+
final PrivateKey supplierKey = PrivateKey.generateECDSA();
163+
final PrivateKey treasuryKey = PrivateKey.generateECDSA();
164+
final AccountId accountId = AccountId.fromString("1.2.3");
165+
166+
// when
167+
when(protocolLayerClient.executeTokenCreateTransaction(any(TokenCreateRequest.class)))
168+
.thenReturn(tokenCreateResult);
169+
when(tokenCreateResult.tokenId()).thenReturn(tokenId);
170+
171+
final TokenId result = nftClientImpl.createNftType(name, symbol, accountId, treasuryKey, supplierKey);
172+
173+
// then
174+
verify(protocolLayerClient, times(1))
175+
.executeTokenCreateTransaction(tokenRequestCaptor.capture());
176+
177+
TokenCreateRequest tokenCreateRequest = tokenRequestCaptor.getValue();
178+
179+
Assertions.assertEquals(treasuryKey, tokenCreateRequest.treasuryKey());
180+
Assertions.assertEquals(supplierKey, tokenCreateRequest.supplyKey());
181+
Assertions.assertEquals(accountId, tokenCreateRequest.treasuryAccountId());
182+
Assertions.assertEquals(name, tokenCreateRequest.name());
183+
Assertions.assertEquals(symbol, tokenCreateRequest.symbol());
184+
185+
Assertions.assertEquals(tokenId, result);
186+
}
187+
188+
@Test
189+
void testCreateNftForNullParam() {
190+
Assertions.assertThrows(
191+
NullPointerException.class, () -> nftClientImpl.createNftType((String)null, null)
192+
);
193+
Assertions.assertThrows(
194+
NullPointerException.class, () -> nftClientImpl.createNftType(null, null, (PrivateKey) null)
195+
);
196+
Assertions.assertThrows(
197+
NullPointerException.class,
198+
() -> nftClientImpl.createNftType(null, null, (AccountId) null, (PrivateKey) null)
199+
);
200+
Assertions.assertThrows(
201+
NullPointerException.class,
202+
() -> nftClientImpl.createNftType(null, null, null, null, (PrivateKey) null)
203+
);
204+
}
205+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.openelements.hiero.spring.test;
22

3+
import com.hedera.hashgraph.sdk.AccountId;
4+
import com.hedera.hashgraph.sdk.PrivateKey;
35
import com.hedera.hashgraph.sdk.TokenId;
46
import com.openelements.hiero.base.data.Account;
57
import com.openelements.hiero.base.AccountClient;
@@ -33,6 +35,24 @@ void createNftType() throws Exception {
3335
Assertions.assertNotNull(tokenId);
3436
}
3537

38+
@Test
39+
void testCreateNftForNullParam() {
40+
Assertions.assertThrows(
41+
NullPointerException.class, () -> nftClient.createNftType((String)null, null)
42+
);
43+
Assertions.assertThrows(
44+
NullPointerException.class, () -> nftClient.createNftType(null, null, (PrivateKey) null)
45+
);
46+
Assertions.assertThrows(
47+
NullPointerException.class,
48+
() -> nftClient.createNftType(null, null, (AccountId) null, (PrivateKey) null)
49+
);
50+
Assertions.assertThrows(
51+
NullPointerException.class,
52+
() -> nftClient.createNftType(null, null, null, null, (PrivateKey) null)
53+
);
54+
}
55+
3656
@Test
3757
void mintNft() throws Exception {
3858
//given

0 commit comments

Comments
 (0)