Skip to content

Commit 5375f5e

Browse files
committed
added test case to validate association and dissociation of token
Signed-off-by: Manish Dait <[email protected]>
1 parent a077c80 commit 5375f5e

File tree

5 files changed

+431
-16
lines changed

5 files changed

+431
-16
lines changed
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
package com.openelements.hiero.base.test;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.Mockito.times;
5+
import static org.mockito.Mockito.verify;
6+
import static org.mockito.Mockito.when;
7+
8+
import com.hedera.hashgraph.sdk.AccountId;
9+
import com.hedera.hashgraph.sdk.PrivateKey;
10+
import com.hedera.hashgraph.sdk.PublicKey;
11+
import com.hedera.hashgraph.sdk.TokenId;
12+
import com.openelements.hiero.base.HieroException;
13+
import com.openelements.hiero.base.data.Account;
14+
import com.openelements.hiero.base.implementation.FungibleTokenClientImpl;
15+
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
16+
import com.openelements.hiero.base.protocol.data.TokenAssociateRequest;
17+
import com.openelements.hiero.base.protocol.data.TokenAssociateResult;
18+
import com.openelements.hiero.base.protocol.data.TokenDissociateRequest;
19+
import com.openelements.hiero.base.protocol.data.TokenDissociateResult;
20+
21+
import java.util.List;
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
import org.mockito.ArgumentCaptor;
26+
import org.mockito.Mockito;
27+
28+
public class FungibleClientImplTest {
29+
ProtocolLayerClient protocolLayerClient;
30+
Account operationalAccount;
31+
FungibleTokenClientImpl fungibleClientImpl;
32+
33+
ArgumentCaptor<TokenAssociateRequest> tokenAssociateCaptor = ArgumentCaptor.forClass(TokenAssociateRequest.class);
34+
ArgumentCaptor<TokenDissociateRequest> tokenDissociateCaptor = ArgumentCaptor.forClass(TokenDissociateRequest.class);
35+
36+
@BeforeEach
37+
public void setup() {
38+
protocolLayerClient = Mockito.mock(ProtocolLayerClient.class);
39+
operationalAccount = Mockito.mock(Account.class);
40+
fungibleClientImpl = new FungibleTokenClientImpl(protocolLayerClient, operationalAccount);
41+
}
42+
43+
44+
@Test
45+
void testAssociateToken() throws HieroException {
46+
final TokenAssociateResult tokenAssociateResult = Mockito.mock(TokenAssociateResult.class);
47+
48+
final TokenId tokenId = TokenId.fromString("1.2.3");
49+
final AccountId accountId = AccountId.fromString("1.2.3");
50+
final PrivateKey accountKey = PrivateKey.generateECDSA();
51+
52+
when(protocolLayerClient.executeTokenAssociateTransaction(any(TokenAssociateRequest.class)))
53+
.thenReturn(tokenAssociateResult);
54+
55+
fungibleClientImpl.associateToken(tokenId, accountId, accountKey);
56+
57+
verify(protocolLayerClient, times(1))
58+
.executeTokenAssociateTransaction(tokenAssociateCaptor.capture());
59+
60+
final TokenAssociateRequest request = tokenAssociateCaptor.getValue();
61+
Assertions.assertEquals(List.of(tokenId), request.tokenIds());
62+
Assertions.assertEquals(accountId, request.accountId());
63+
Assertions.assertEquals(accountKey, request.accountPrivateKey());
64+
}
65+
66+
@Test
67+
void testAssociateTokenWithAccount() throws HieroException {
68+
final TokenAssociateResult tokenAssociateResult = Mockito.mock(TokenAssociateResult.class);
69+
final AccountId accountId = AccountId.fromString("1.2.3");
70+
final PrivateKey privateKey = PrivateKey.generateECDSA();
71+
final PublicKey publicKey = privateKey.getPublicKey();
72+
73+
final TokenId tokenId = TokenId.fromString("1.2.3");
74+
final Account account = new Account(accountId, publicKey, privateKey);
75+
76+
when(protocolLayerClient.executeTokenAssociateTransaction(any(TokenAssociateRequest.class)))
77+
.thenReturn(tokenAssociateResult);
78+
79+
fungibleClientImpl.associateToken(tokenId, account);
80+
81+
verify(protocolLayerClient, times(1))
82+
.executeTokenAssociateTransaction(tokenAssociateCaptor.capture());
83+
84+
final TokenAssociateRequest request = tokenAssociateCaptor.getValue();
85+
Assertions.assertEquals(List.of(tokenId), request.tokenIds());
86+
Assertions.assertEquals(accountId, request.accountId());
87+
Assertions.assertEquals(privateKey, request.accountPrivateKey());
88+
}
89+
90+
@Test
91+
void testAssociateTokenThrowsExceptionForInvalidId() throws HieroException {
92+
final TokenId tokenId = TokenId.fromString("1.2.3");
93+
final AccountId accountId = AccountId.fromString("1.2.3");
94+
final PrivateKey accountKey = PrivateKey.generateECDSA();
95+
final Account account = new Account(accountId, accountKey.getPublicKey(), accountKey);
96+
97+
when(protocolLayerClient.executeTokenAssociateTransaction(any(TokenAssociateRequest.class)))
98+
.thenThrow(new HieroException("Failed to execute transaction of type TokenAssociateTransaction"));
99+
100+
Assertions.assertThrows(HieroException.class, () -> fungibleClientImpl.associateToken(tokenId, accountId, accountKey));
101+
Assertions.assertThrows(HieroException.class, () -> fungibleClientImpl.associateToken(tokenId, account));
102+
}
103+
104+
@Test
105+
void testAssociateTokenNullParam() {
106+
Assertions.assertThrows(NullPointerException.class,
107+
() -> fungibleClientImpl.associateToken((TokenId) null, (AccountId) null, (PrivateKey) null));
108+
Assertions.assertThrows(NullPointerException.class,
109+
() -> fungibleClientImpl.associateToken((TokenId) null, null));
110+
}
111+
112+
@Test
113+
void testAssociateTokenWithMultipleToken() throws HieroException {
114+
final TokenAssociateResult tokenAssociateResult = Mockito.mock(TokenAssociateResult.class);
115+
116+
final TokenId tokenId1 = TokenId.fromString("1.2.3");
117+
final TokenId tokenId2 = TokenId.fromString("1.2.4");
118+
119+
final AccountId accountId = AccountId.fromString("1.2.3");
120+
final PrivateKey accountKey = PrivateKey.generateECDSA();
121+
122+
when(protocolLayerClient.executeTokenAssociateTransaction(any(TokenAssociateRequest.class)))
123+
.thenReturn(tokenAssociateResult);
124+
125+
fungibleClientImpl.associateToken(List.of(tokenId1, tokenId2), accountId, accountKey);
126+
127+
verify(protocolLayerClient, times(1))
128+
.executeTokenAssociateTransaction(tokenAssociateCaptor.capture());
129+
130+
final TokenAssociateRequest request = tokenAssociateCaptor.getValue();
131+
Assertions.assertEquals(List.of(tokenId1, tokenId2), request.tokenIds());
132+
Assertions.assertEquals(accountId, request.accountId());
133+
Assertions.assertEquals(accountKey, request.accountPrivateKey());
134+
}
135+
136+
@Test
137+
void testAssociateTokenWithMultipleTokenThrowExceptionIfListEmpty() {
138+
final AccountId accountId = AccountId.fromString("1.2.3");
139+
final PrivateKey accountKey = PrivateKey.generateECDSA();
140+
141+
IllegalArgumentException e = Assertions.assertThrows(IllegalArgumentException.class,
142+
() -> fungibleClientImpl.associateToken(List.of(), accountId, accountKey));
143+
Assertions.assertEquals("tokenIds must not be empty", e.getMessage());
144+
}
145+
146+
@Test
147+
void testDissociateToken() throws HieroException {
148+
final TokenDissociateResult tokenDissociateResult = Mockito.mock(TokenDissociateResult.class);
149+
150+
final TokenId tokenId = TokenId.fromString("1.2.3");
151+
final AccountId accountId = AccountId.fromString("1.2.3");
152+
final PrivateKey accountKey = PrivateKey.generateECDSA();
153+
154+
when(protocolLayerClient.executeTokenDissociateTransaction(any(TokenDissociateRequest.class)))
155+
.thenReturn(tokenDissociateResult);
156+
157+
fungibleClientImpl.dissociateToken(tokenId, accountId, accountKey);
158+
159+
verify(protocolLayerClient, times(1))
160+
.executeTokenDissociateTransaction(tokenDissociateCaptor.capture());
161+
162+
final TokenDissociateRequest request = tokenDissociateCaptor.getValue();
163+
Assertions.assertEquals(List.of(tokenId), request.tokenIds());
164+
Assertions.assertEquals(accountId, request.accountId());
165+
Assertions.assertEquals(accountKey, request.accountKey());
166+
}
167+
168+
@Test
169+
void testDissociateTokenThrowsExceptionForInvalidId() throws HieroException {
170+
final TokenId tokenId = TokenId.fromString("1.2.3");
171+
final AccountId accountId = AccountId.fromString("1.2.3");
172+
final PrivateKey accountKey = PrivateKey.generateECDSA();
173+
final Account account = new Account(accountId, accountKey.getPublicKey(), accountKey);
174+
175+
when(protocolLayerClient.executeTokenDissociateTransaction(any(TokenDissociateRequest.class)))
176+
.thenThrow(new HieroException("Failed to execute transaction of type TokenDissociateTransaction"));
177+
178+
Assertions.assertThrows(HieroException.class, () -> fungibleClientImpl.dissociateToken(tokenId, accountId, accountKey));
179+
Assertions.assertThrows(HieroException.class, () -> fungibleClientImpl.dissociateToken(tokenId, account));
180+
}
181+
182+
@Test
183+
void testDissociateTokenNullParam() {
184+
Assertions.assertThrows(NullPointerException.class,
185+
() -> fungibleClientImpl.dissociateToken((TokenId) null, null, null));
186+
Assertions.assertThrows(NullPointerException.class,
187+
() -> fungibleClientImpl.dissociateToken((TokenId) null, null));
188+
}
189+
190+
@Test
191+
void testDissociateTokenWithMultipleToken() throws HieroException {
192+
final TokenDissociateResult tokenDissociateResult = Mockito.mock(TokenDissociateResult.class);
193+
194+
final TokenId tokenId1 = TokenId.fromString("1.2.3");
195+
final TokenId tokenId2 = TokenId.fromString("1.2.4");
196+
197+
final AccountId accountId = AccountId.fromString("1.2.3");
198+
final PrivateKey accountKey = PrivateKey.generateECDSA();
199+
200+
when(protocolLayerClient.executeTokenDissociateTransaction(any(TokenDissociateRequest.class)))
201+
.thenReturn(tokenDissociateResult);
202+
203+
fungibleClientImpl.dissociateToken(List.of(tokenId1, tokenId2), accountId, accountKey);
204+
205+
verify(protocolLayerClient, times(1))
206+
.executeTokenDissociateTransaction(tokenDissociateCaptor.capture());
207+
208+
final TokenDissociateRequest request = tokenDissociateCaptor.getValue();
209+
Assertions.assertEquals(List.of(tokenId1, tokenId2), request.tokenIds());
210+
Assertions.assertEquals(accountId, request.accountId());
211+
Assertions.assertEquals(accountKey, request.accountKey());
212+
}
213+
214+
@Test
215+
void testDissociateTokenWithMultipleTokenThrowExceptionIfListEmpty() {
216+
final AccountId accountId = AccountId.fromString("1.2.3");
217+
final PrivateKey accountKey = PrivateKey.generateECDSA();
218+
219+
IllegalArgumentException e = Assertions.assertThrows(IllegalArgumentException.class,
220+
() -> fungibleClientImpl.dissociateToken(List.of(), accountId, accountKey));
221+
Assertions.assertEquals("tokenIds must not be empty", e.getMessage());
222+
}
223+
}

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

Lines changed: 118 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import com.openelements.hiero.base.protocol.ProtocolLayerClient;
1717
import com.openelements.hiero.base.protocol.data.TokenAssociateRequest;
1818
import com.openelements.hiero.base.protocol.data.TokenAssociateResult;
19+
import com.openelements.hiero.base.protocol.data.TokenDissociateRequest;
20+
import com.openelements.hiero.base.protocol.data.TokenDissociateResult;
1921
import com.openelements.hiero.base.protocol.data.TokenBurnRequest;
2022
import com.openelements.hiero.base.protocol.data.TokenBurnResult;
2123
import com.openelements.hiero.base.protocol.data.TokenCreateRequest;
@@ -39,6 +41,7 @@ public class NftClientImplTest {
3941
ArgumentCaptor<TokenTransferRequest> tokenTransferCaptor = ArgumentCaptor.forClass(TokenTransferRequest.class);
4042
ArgumentCaptor<TokenBurnRequest> tokenBurnCaptor = ArgumentCaptor.forClass(TokenBurnRequest.class);
4143
ArgumentCaptor<TokenAssociateRequest> tokenAssociateCaptor = ArgumentCaptor.forClass(TokenAssociateRequest.class);
44+
ArgumentCaptor<TokenDissociateRequest> tokenDissociateCaptor = ArgumentCaptor.forClass(TokenDissociateRequest.class);
4245

4346
@BeforeEach
4447
public void setup() {
@@ -462,7 +465,7 @@ void testAssociateNft() throws HieroException {
462465
.executeTokenAssociateTransaction(tokenAssociateCaptor.capture());
463466

464467
final TokenAssociateRequest request = tokenAssociateCaptor.getValue();
465-
Assertions.assertEquals(tokenId, request.tokenId());
468+
Assertions.assertEquals(List.of(tokenId), request.tokenIds());
466469
Assertions.assertEquals(accountId, request.accountId());
467470
Assertions.assertEquals(accountKey, request.accountPrivateKey());
468471
}
@@ -486,7 +489,7 @@ void testAssociateNftWithAccount() throws HieroException {
486489
.executeTokenAssociateTransaction(tokenAssociateCaptor.capture());
487490

488491
final TokenAssociateRequest request = tokenAssociateCaptor.getValue();
489-
Assertions.assertEquals(tokenId, request.tokenId());
492+
Assertions.assertEquals(List.of(tokenId), request.tokenIds());
490493
Assertions.assertEquals(accountId, request.accountId());
491494
Assertions.assertEquals(privateKey, request.accountPrivateKey());
492495
}
@@ -510,6 +513,118 @@ void testAssociateNftNullParam() {
510513
Assertions.assertThrows(NullPointerException.class,
511514
() -> nftClientImpl.associateNft((TokenId) null, (AccountId) null, (PrivateKey) null));
512515
Assertions.assertThrows(NullPointerException.class,
513-
() -> nftClientImpl.associateNft(null, null));
516+
() -> nftClientImpl.associateNft((TokenId) null, null));
517+
}
518+
519+
@Test
520+
void testAssociateNftWithMultipleToken() throws HieroException {
521+
final TokenAssociateResult tokenAssociateResult = Mockito.mock(TokenAssociateResult.class);
522+
523+
final TokenId tokenId1 = TokenId.fromString("1.2.3");
524+
final TokenId tokenId2 = TokenId.fromString("1.2.4");
525+
526+
final AccountId accountId = AccountId.fromString("1.2.3");
527+
final PrivateKey accountKey = PrivateKey.generateECDSA();
528+
529+
when(protocolLayerClient.executeTokenAssociateTransaction(any(TokenAssociateRequest.class)))
530+
.thenReturn(tokenAssociateResult);
531+
532+
nftClientImpl.associateNft(List.of(tokenId1, tokenId2), accountId, accountKey);
533+
534+
verify(protocolLayerClient, times(1))
535+
.executeTokenAssociateTransaction(tokenAssociateCaptor.capture());
536+
537+
final TokenAssociateRequest request = tokenAssociateCaptor.getValue();
538+
Assertions.assertEquals(List.of(tokenId1, tokenId2), request.tokenIds());
539+
Assertions.assertEquals(accountId, request.accountId());
540+
Assertions.assertEquals(accountKey, request.accountPrivateKey());
541+
}
542+
543+
@Test
544+
void testAssociateNftWithMultipleTokenThrowExceptionIfListEmpty() {
545+
final AccountId accountId = AccountId.fromString("1.2.3");
546+
final PrivateKey accountKey = PrivateKey.generateECDSA();
547+
548+
IllegalArgumentException e = Assertions.assertThrows(IllegalArgumentException.class,
549+
() -> nftClientImpl.associateNft(List.of(), accountId, accountKey));
550+
Assertions.assertEquals("tokenIds must not be empty", e.getMessage());
551+
}
552+
553+
@Test
554+
void testDissociateNft() throws HieroException {
555+
final TokenDissociateResult tokenDissociateResult = Mockito.mock(TokenDissociateResult.class);
556+
557+
final TokenId tokenId = TokenId.fromString("1.2.3");
558+
final AccountId accountId = AccountId.fromString("1.2.3");
559+
final PrivateKey accountKey = PrivateKey.generateECDSA();
560+
561+
when(protocolLayerClient.executeTokenDissociateTransaction(any(TokenDissociateRequest.class)))
562+
.thenReturn(tokenDissociateResult);
563+
564+
nftClientImpl.dissociateNft(tokenId, accountId, accountKey);
565+
566+
verify(protocolLayerClient, times(1))
567+
.executeTokenDissociateTransaction(tokenDissociateCaptor.capture());
568+
569+
final TokenDissociateRequest request = tokenDissociateCaptor.getValue();
570+
Assertions.assertEquals(List.of(tokenId), request.tokenIds());
571+
Assertions.assertEquals(accountId, request.accountId());
572+
Assertions.assertEquals(accountKey, request.accountKey());
573+
}
574+
575+
@Test
576+
void testDissociateNftThrowsExceptionForInvalidId() throws HieroException {
577+
final TokenId tokenId = TokenId.fromString("1.2.3");
578+
final AccountId accountId = AccountId.fromString("1.2.3");
579+
final PrivateKey accountKey = PrivateKey.generateECDSA();
580+
final Account account = new Account(accountId, accountKey.getPublicKey(), accountKey);
581+
582+
when(protocolLayerClient.executeTokenDissociateTransaction(any(TokenDissociateRequest.class)))
583+
.thenThrow(new HieroException("Failed to execute transaction of type TokenDissociateTransaction"));
584+
585+
Assertions.assertThrows(HieroException.class, () -> nftClientImpl.dissociateNft(tokenId, accountId, accountKey));
586+
Assertions.assertThrows(HieroException.class, () -> nftClientImpl.dissociateNft(tokenId, account));
587+
}
588+
589+
@Test
590+
void testDissociateNftNullParam() {
591+
Assertions.assertThrows(NullPointerException.class,
592+
() -> nftClientImpl.dissociateNft((TokenId) null, null, null));
593+
Assertions.assertThrows(NullPointerException.class,
594+
() -> nftClientImpl.dissociateNft((TokenId) null, null));
595+
}
596+
597+
@Test
598+
void testDissociateNftWithMultipleToken() throws HieroException {
599+
final TokenDissociateResult tokenDissociateResult = Mockito.mock(TokenDissociateResult.class);
600+
601+
final TokenId tokenId1 = TokenId.fromString("1.2.3");
602+
final TokenId tokenId2 = TokenId.fromString("1.2.4");
603+
604+
final AccountId accountId = AccountId.fromString("1.2.3");
605+
final PrivateKey accountKey = PrivateKey.generateECDSA();
606+
607+
when(protocolLayerClient.executeTokenDissociateTransaction(any(TokenDissociateRequest.class)))
608+
.thenReturn(tokenDissociateResult);
609+
610+
nftClientImpl.dissociateNft(List.of(tokenId1, tokenId2), accountId, accountKey);
611+
612+
verify(protocolLayerClient, times(1))
613+
.executeTokenDissociateTransaction(tokenDissociateCaptor.capture());
614+
615+
final TokenDissociateRequest request = tokenDissociateCaptor.getValue();
616+
Assertions.assertEquals(List.of(tokenId1, tokenId2), request.tokenIds());
617+
Assertions.assertEquals(accountId, request.accountId());
618+
Assertions.assertEquals(accountKey, request.accountKey());
619+
}
620+
621+
@Test
622+
void testDissociateNftWithMultipleTokenThrowExceptionIfListEmpty() {
623+
final AccountId accountId = AccountId.fromString("1.2.3");
624+
final PrivateKey accountKey = PrivateKey.generateECDSA();
625+
626+
IllegalArgumentException e = Assertions.assertThrows(IllegalArgumentException.class,
627+
() -> nftClientImpl.dissociateNft(List.of(), accountId, accountKey));
628+
Assertions.assertEquals("tokenIds must not be empty", e.getMessage());
514629
}
515630
}

0 commit comments

Comments
 (0)