mirror of
https://github.com/corda/corda.git
synced 2024-12-19 21:17:58 +00:00
Test DSL: only "unverified" transactions get filled with dummy data to make them valid WireTransactions.
Recording is removed for a ledger with a single transaction.
This commit is contained in:
parent
65a59f680b
commit
210cb4658a
@ -1,6 +1,5 @@
|
||||
package net.corda.finance.contracts.asset;
|
||||
|
||||
import kotlin.Unit;
|
||||
import net.corda.core.contracts.PartyAndReference;
|
||||
import net.corda.core.identity.AnonymousParty;
|
||||
import net.corda.core.utilities.OpaqueBytes;
|
||||
@ -22,44 +21,41 @@ public class CashTestsJava {
|
||||
|
||||
@Test
|
||||
public void trivial() {
|
||||
ledger(lg -> {
|
||||
lg.transaction(tx -> {
|
||||
tx.input(inState);
|
||||
transaction(tx -> {
|
||||
tx.input(inState);
|
||||
|
||||
tx.tweak(tw -> {
|
||||
tw.output(new Cash.State(issuedBy(DOLLARS(2000), defaultIssuer), new AnonymousParty(getMINI_CORP_PUBKEY())));
|
||||
tw.command(getMEGA_CORP_PUBKEY(), new Cash.Commands.Move());
|
||||
return tw.failsWith("the amounts balance");
|
||||
});
|
||||
|
||||
tx.tweak(tw -> {
|
||||
tw.output(outState);
|
||||
tw.command(getMEGA_CORP_PUBKEY(), DummyCommandData.INSTANCE);
|
||||
// Invalid command
|
||||
return tw.failsWith("required net.corda.finance.contracts.asset.Cash.Commands.Move command");
|
||||
});
|
||||
tx.tweak(tw -> {
|
||||
tw.output(outState);
|
||||
tw.command(getMINI_CORP_PUBKEY(), new Cash.Commands.Move());
|
||||
return tw.failsWith("the owning keys are a subset of the signing keys");
|
||||
});
|
||||
tx.tweak(tw -> {
|
||||
tw.output(outState);
|
||||
// issuedBy() can't be directly imported because it conflicts with other identically named functions
|
||||
// with different overloads (for some reason).
|
||||
tw.output(outState.issuedBy(getMINI_CORP()));
|
||||
tw.command(getMEGA_CORP_PUBKEY(), new Cash.Commands.Move());
|
||||
return tw.failsWith("at least one cash input");
|
||||
});
|
||||
|
||||
// Simple reallocation works.
|
||||
return tx.tweak(tw -> {
|
||||
tw.output(outState);
|
||||
tw.command(getMEGA_CORP_PUBKEY(), new Cash.Commands.Move());
|
||||
return tw.verifies();
|
||||
});
|
||||
tx.tweak(tw -> {
|
||||
tw.output(new Cash.State(issuedBy(DOLLARS(2000), defaultIssuer), new AnonymousParty(getMINI_CORP_PUBKEY())));
|
||||
tw.command(getMEGA_CORP_PUBKEY(), new Cash.Commands.Move());
|
||||
return tw.failsWith("the amounts balance");
|
||||
});
|
||||
|
||||
tx.tweak(tw -> {
|
||||
tw.output(outState);
|
||||
tw.command(getMEGA_CORP_PUBKEY(), DummyCommandData.INSTANCE);
|
||||
// Invalid command
|
||||
return tw.failsWith("required net.corda.finance.contracts.asset.Cash.Commands.Move command");
|
||||
});
|
||||
tx.tweak(tw -> {
|
||||
tw.output(outState);
|
||||
tw.command(getMINI_CORP_PUBKEY(), new Cash.Commands.Move());
|
||||
return tw.failsWith("the owning keys are a subset of the signing keys");
|
||||
});
|
||||
tx.tweak(tw -> {
|
||||
tw.output(outState);
|
||||
// issuedBy() can't be directly imported because it conflicts with other identically named functions
|
||||
// with different overloads (for some reason).
|
||||
tw.output(outState.issuedBy(getMINI_CORP()));
|
||||
tw.command(getMEGA_CORP_PUBKEY(), new Cash.Commands.Move());
|
||||
return tw.failsWith("at least one cash input");
|
||||
});
|
||||
|
||||
// Simple reallocation works.
|
||||
return tx.tweak(tw -> {
|
||||
tw.output(outState);
|
||||
tw.command(getMEGA_CORP_PUBKEY(), new Cash.Commands.Move());
|
||||
return tw.verifies();
|
||||
});
|
||||
return Unit.INSTANCE;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -155,7 +155,9 @@ fun getFreeLocalPorts(hostName: String, numberToAlloc: Int): List<NetworkHostAnd
|
||||
transactionBuilder: TransactionBuilder = TransactionBuilder(notary = DUMMY_NOTARY),
|
||||
initialiseSerialization: Boolean = true,
|
||||
dsl: TransactionDSL<TransactionDSLInterpreter>.() -> EnforceVerifyOrFail
|
||||
) = ledger(initialiseSerialization = initialiseSerialization) { this.transaction(transactionLabel, transactionBuilder, dsl) }
|
||||
) = ledger(initialiseSerialization = initialiseSerialization) {
|
||||
dsl(TransactionDSL(TestTransactionDSLInterpreter(this.interpreter, transactionBuilder)))
|
||||
}
|
||||
|
||||
fun testNodeConfiguration(
|
||||
baseDirectory: Path,
|
||||
|
@ -244,11 +244,13 @@ data class TestLedgerDSLInterpreter private constructor(
|
||||
transactionLabel: String?,
|
||||
transactionBuilder: TransactionBuilder,
|
||||
dsl: TransactionDSL<TestTransactionDSLInterpreter>.() -> R,
|
||||
transactionMap: HashMap<SecureHash, WireTransactionWithLocation> = HashMap()
|
||||
transactionMap: HashMap<SecureHash, WireTransactionWithLocation> = HashMap(),
|
||||
/** If set to true, will add dummy components to [transactionBuilder] to make it valid. */
|
||||
fillTransaction: Boolean = false
|
||||
): WireTransaction {
|
||||
val transactionLocation = getCallerLocation()
|
||||
val transactionInterpreter = interpretTransactionDsl(transactionBuilder, dsl)
|
||||
makeTxValid(transactionBuilder)
|
||||
if (fillTransaction) fillTransaction(transactionBuilder)
|
||||
// Create the WireTransaction
|
||||
val wireTransaction = transactionInterpreter.toWireTransaction()
|
||||
// Record the output states
|
||||
@ -271,7 +273,7 @@ data class TestLedgerDSLInterpreter private constructor(
|
||||
* The base transaction may not be valid, but it still gets recorded to the ledger. This causes a test failure,
|
||||
* even though is not being used for anything afterwards.
|
||||
*/
|
||||
private fun makeTxValid(transactionBuilder: TransactionBuilder) {
|
||||
private fun fillTransaction(transactionBuilder: TransactionBuilder) {
|
||||
if (transactionBuilder.commands().isEmpty()) transactionBuilder.addCommand(dummyCommand())
|
||||
if (transactionBuilder.inputStates().isEmpty() && transactionBuilder.outputStates().isEmpty()) {
|
||||
transactionBuilder.addOutputState(DummyContract.SingleOwnerState(owner = ALICE))
|
||||
@ -288,7 +290,7 @@ data class TestLedgerDSLInterpreter private constructor(
|
||||
transactionLabel: String?,
|
||||
transactionBuilder: TransactionBuilder,
|
||||
dsl: TransactionDSL<TestTransactionDSLInterpreter>.() -> Unit
|
||||
) = recordTransactionWithTransactionMap(transactionLabel, transactionBuilder, dsl, nonVerifiedTransactionWithLocations)
|
||||
) = recordTransactionWithTransactionMap(transactionLabel, transactionBuilder, dsl, nonVerifiedTransactionWithLocations, fillTransaction = true)
|
||||
|
||||
override fun tweak(
|
||||
dsl: LedgerDSL<TestTransactionDSLInterpreter,
|
||||
|
Loading…
Reference in New Issue
Block a user