rippled
Loading...
Searching...
No Matches
NFTokenMintTests.cpp
1// Auto-generated unit tests for transaction NFTokenMint
2
3
4#include <gtest/gtest.h>
5
6#include <protocol_autogen/TestHelpers.h>
7
8#include <xrpl/protocol/SecretKey.h>
9#include <xrpl/protocol/Seed.h>
10#include <xrpl/protocol/STTx.h>
11#include <xrpl/protocol_autogen/transactions/NFTokenMint.h>
12#include <xrpl/protocol_autogen/transactions/AccountSet.h>
13
14#include <string>
15
16namespace xrpl::transactions {
17
18// 1 & 4) Set fields via builder setters, build, then read them back via
19// wrapper getters. After build(), validate() should succeed.
20TEST(TransactionsNFTokenMintTests, BuilderSettersRoundTrip)
21{
22 // Generate a deterministic keypair for signing
23 auto const [publicKey, secretKey] =
25
26 // Common transaction fields
27 auto const accountValue = calcAccountID(publicKey);
28 std::uint32_t const sequenceValue = 1;
29 auto const feeValue = canonical_AMOUNT();
30
31 // Transaction-specific field values
32 auto const nFTokenTaxonValue = canonical_UINT32();
33 auto const transferFeeValue = canonical_UINT16();
34 auto const issuerValue = canonical_ACCOUNT();
35 auto const uRIValue = canonical_VL();
36 auto const amountValue = canonical_AMOUNT();
37 auto const destinationValue = canonical_ACCOUNT();
38 auto const expirationValue = canonical_UINT32();
39
40 NFTokenMintBuilder builder{
41 accountValue,
42 nFTokenTaxonValue,
43 sequenceValue,
44 feeValue
45 };
46
47 // Set optional fields
48 builder.setTransferFee(transferFeeValue);
49 builder.setIssuer(issuerValue);
50 builder.setURI(uRIValue);
51 builder.setAmount(amountValue);
52 builder.setDestination(destinationValue);
53 builder.setExpiration(expirationValue);
54
55 auto tx = builder.build(publicKey, secretKey);
56
57 std::string reason;
58 EXPECT_TRUE(tx.validate(reason)) << reason;
59
60 // Verify signing was applied
61 EXPECT_FALSE(tx.getSigningPubKey().empty());
62 EXPECT_TRUE(tx.hasTxnSignature());
63
64 // Verify common fields
65 EXPECT_EQ(tx.getAccount(), accountValue);
66 EXPECT_EQ(tx.getSequence(), sequenceValue);
67 EXPECT_EQ(tx.getFee(), feeValue);
68
69 // Verify required fields
70 {
71 auto const& expected = nFTokenTaxonValue;
72 auto const actual = tx.getNFTokenTaxon();
73 expectEqualField(expected, actual, "sfNFTokenTaxon");
74 }
75
76 // Verify optional fields
77 {
78 auto const& expected = transferFeeValue;
79 auto const actualOpt = tx.getTransferFee();
80 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfTransferFee should be present";
81 expectEqualField(expected, *actualOpt, "sfTransferFee");
82 EXPECT_TRUE(tx.hasTransferFee());
83 }
84
85 {
86 auto const& expected = issuerValue;
87 auto const actualOpt = tx.getIssuer();
88 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfIssuer should be present";
89 expectEqualField(expected, *actualOpt, "sfIssuer");
90 EXPECT_TRUE(tx.hasIssuer());
91 }
92
93 {
94 auto const& expected = uRIValue;
95 auto const actualOpt = tx.getURI();
96 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfURI should be present";
97 expectEqualField(expected, *actualOpt, "sfURI");
98 EXPECT_TRUE(tx.hasURI());
99 }
100
101 {
102 auto const& expected = amountValue;
103 auto const actualOpt = tx.getAmount();
104 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfAmount should be present";
105 expectEqualField(expected, *actualOpt, "sfAmount");
106 EXPECT_TRUE(tx.hasAmount());
107 }
108
109 {
110 auto const& expected = destinationValue;
111 auto const actualOpt = tx.getDestination();
112 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfDestination should be present";
113 expectEqualField(expected, *actualOpt, "sfDestination");
114 EXPECT_TRUE(tx.hasDestination());
115 }
116
117 {
118 auto const& expected = expirationValue;
119 auto const actualOpt = tx.getExpiration();
120 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfExpiration should be present";
121 expectEqualField(expected, *actualOpt, "sfExpiration");
122 EXPECT_TRUE(tx.hasExpiration());
123 }
124
125}
126
127// 2 & 4) Start from an STTx, construct a builder from it, build a new wrapper,
128// and verify all fields match.
129TEST(TransactionsNFTokenMintTests, BuilderFromStTxRoundTrip)
130{
131 // Generate a deterministic keypair for signing
132 auto const [publicKey, secretKey] =
133 generateKeyPair(KeyType::secp256k1, generateSeed("testNFTokenMintFromTx"));
134
135 // Common transaction fields
136 auto const accountValue = calcAccountID(publicKey);
137 std::uint32_t const sequenceValue = 2;
138 auto const feeValue = canonical_AMOUNT();
139
140 // Transaction-specific field values
141 auto const nFTokenTaxonValue = canonical_UINT32();
142 auto const transferFeeValue = canonical_UINT16();
143 auto const issuerValue = canonical_ACCOUNT();
144 auto const uRIValue = canonical_VL();
145 auto const amountValue = canonical_AMOUNT();
146 auto const destinationValue = canonical_ACCOUNT();
147 auto const expirationValue = canonical_UINT32();
148
149 // Build an initial transaction
150 NFTokenMintBuilder initialBuilder{
151 accountValue,
152 nFTokenTaxonValue,
153 sequenceValue,
154 feeValue
155 };
156
157 initialBuilder.setTransferFee(transferFeeValue);
158 initialBuilder.setIssuer(issuerValue);
159 initialBuilder.setURI(uRIValue);
160 initialBuilder.setAmount(amountValue);
161 initialBuilder.setDestination(destinationValue);
162 initialBuilder.setExpiration(expirationValue);
163
164 auto initialTx = initialBuilder.build(publicKey, secretKey);
165
166 // Create builder from existing STTx
167 NFTokenMintBuilder builderFromTx{initialTx.getSTTx()};
168
169 auto rebuiltTx = builderFromTx.build(publicKey, secretKey);
170
171 std::string reason;
172 EXPECT_TRUE(rebuiltTx.validate(reason)) << reason;
173
174 // Verify common fields
175 EXPECT_EQ(rebuiltTx.getAccount(), accountValue);
176 EXPECT_EQ(rebuiltTx.getSequence(), sequenceValue);
177 EXPECT_EQ(rebuiltTx.getFee(), feeValue);
178
179 // Verify required fields
180 {
181 auto const& expected = nFTokenTaxonValue;
182 auto const actual = rebuiltTx.getNFTokenTaxon();
183 expectEqualField(expected, actual, "sfNFTokenTaxon");
184 }
185
186 // Verify optional fields
187 {
188 auto const& expected = transferFeeValue;
189 auto const actualOpt = rebuiltTx.getTransferFee();
190 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfTransferFee should be present";
191 expectEqualField(expected, *actualOpt, "sfTransferFee");
192 }
193
194 {
195 auto const& expected = issuerValue;
196 auto const actualOpt = rebuiltTx.getIssuer();
197 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfIssuer should be present";
198 expectEqualField(expected, *actualOpt, "sfIssuer");
199 }
200
201 {
202 auto const& expected = uRIValue;
203 auto const actualOpt = rebuiltTx.getURI();
204 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfURI should be present";
205 expectEqualField(expected, *actualOpt, "sfURI");
206 }
207
208 {
209 auto const& expected = amountValue;
210 auto const actualOpt = rebuiltTx.getAmount();
211 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfAmount should be present";
212 expectEqualField(expected, *actualOpt, "sfAmount");
213 }
214
215 {
216 auto const& expected = destinationValue;
217 auto const actualOpt = rebuiltTx.getDestination();
218 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfDestination should be present";
219 expectEqualField(expected, *actualOpt, "sfDestination");
220 }
221
222 {
223 auto const& expected = expirationValue;
224 auto const actualOpt = rebuiltTx.getExpiration();
225 ASSERT_TRUE(actualOpt.has_value()) << "Optional field sfExpiration should be present";
226 expectEqualField(expected, *actualOpt, "sfExpiration");
227 }
228
229}
230
231// 3) Verify wrapper throws when constructed from wrong transaction type.
232TEST(TransactionsNFTokenMintTests, WrapperThrowsOnWrongTxType)
233{
234 // Build a valid transaction of a different type
235 auto const [pk, sk] =
237 auto const account = calcAccountID(pk);
238
239 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
240 auto wrongTx = wrongBuilder.build(pk, sk);
241
242 EXPECT_THROW(NFTokenMint{wrongTx.getSTTx()}, std::runtime_error);
243}
244
245// 4) Verify builder throws when constructed from wrong transaction type.
246TEST(TransactionsNFTokenMintTests, BuilderThrowsOnWrongTxType)
247{
248 // Build a valid transaction of a different type
249 auto const [pk, sk] =
250 generateKeyPair(KeyType::secp256k1, generateSeed("testWrongTypeBuilder"));
251 auto const account = calcAccountID(pk);
252
253 AccountSetBuilder wrongBuilder{account, 1, canonical_AMOUNT()};
254 auto wrongTx = wrongBuilder.build(pk, sk);
255
256 EXPECT_THROW(NFTokenMintBuilder{wrongTx.getSTTx()}, std::runtime_error);
257}
258
259// 5) Build with only required fields and verify optional fields return nullopt.
260TEST(TransactionsNFTokenMintTests, OptionalFieldsReturnNullopt)
261{
262 // Generate a deterministic keypair for signing
263 auto const [publicKey, secretKey] =
264 generateKeyPair(KeyType::secp256k1, generateSeed("testNFTokenMintNullopt"));
265
266 // Common transaction fields
267 auto const accountValue = calcAccountID(publicKey);
268 std::uint32_t const sequenceValue = 3;
269 auto const feeValue = canonical_AMOUNT();
270
271 // Transaction-specific required field values
272 auto const nFTokenTaxonValue = canonical_UINT32();
273
274 NFTokenMintBuilder builder{
275 accountValue,
276 nFTokenTaxonValue,
277 sequenceValue,
278 feeValue
279 };
280
281 // Do NOT set optional fields
282
283 auto tx = builder.build(publicKey, secretKey);
284
285 // Verify optional fields are not present
286 EXPECT_FALSE(tx.hasTransferFee());
287 EXPECT_FALSE(tx.getTransferFee().has_value());
288 EXPECT_FALSE(tx.hasIssuer());
289 EXPECT_FALSE(tx.getIssuer().has_value());
290 EXPECT_FALSE(tx.hasURI());
291 EXPECT_FALSE(tx.getURI().has_value());
292 EXPECT_FALSE(tx.hasAmount());
293 EXPECT_FALSE(tx.getAmount().has_value());
294 EXPECT_FALSE(tx.hasDestination());
295 EXPECT_FALSE(tx.getDestination().has_value());
296 EXPECT_FALSE(tx.hasExpiration());
297 EXPECT_FALSE(tx.getExpiration().has_value());
298}
299
300}
NFTokenMint build(PublicKey const &publicKey, SecretKey const &secretKey)
Build and return the NFTokenMint wrapper.
std::shared_ptr< STTx const > getSTTx() const
Get the underlying STTx object.
TEST(TransactionsAccountDeleteTests, BuilderSettersRoundTrip)
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
Definition Seed.cpp:57
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
AccountID calcAccountID(PublicKey const &pk)
void expectEqualField(T const &expected, T const &actual, char const *fieldName)