|
| 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 | +} |
0 commit comments