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:
Andrius Dagys 2017-08-25 11:03:44 +01:00
parent 65a59f680b
commit 210cb4658a
3 changed files with 42 additions and 42 deletions

View File

@ -1,6 +1,5 @@
package net.corda.finance.contracts.asset; package net.corda.finance.contracts.asset;
import kotlin.Unit;
import net.corda.core.contracts.PartyAndReference; import net.corda.core.contracts.PartyAndReference;
import net.corda.core.identity.AnonymousParty; import net.corda.core.identity.AnonymousParty;
import net.corda.core.utilities.OpaqueBytes; import net.corda.core.utilities.OpaqueBytes;
@ -22,44 +21,41 @@ public class CashTestsJava {
@Test @Test
public void trivial() { public void trivial() {
ledger(lg -> { transaction(tx -> {
lg.transaction(tx -> { tx.input(inState);
tx.input(inState);
tx.tweak(tw -> { tx.tweak(tw -> {
tw.output(new Cash.State(issuedBy(DOLLARS(2000), defaultIssuer), new AnonymousParty(getMINI_CORP_PUBKEY()))); tw.output(new Cash.State(issuedBy(DOLLARS(2000), defaultIssuer), new AnonymousParty(getMINI_CORP_PUBKEY())));
tw.command(getMEGA_CORP_PUBKEY(), new Cash.Commands.Move()); tw.command(getMEGA_CORP_PUBKEY(), new Cash.Commands.Move());
return tw.failsWith("the amounts balance"); return tw.failsWith("the amounts balance");
}); });
tx.tweak(tw -> { tx.tweak(tw -> {
tw.output(outState); tw.output(outState);
tw.command(getMEGA_CORP_PUBKEY(), DummyCommandData.INSTANCE); tw.command(getMEGA_CORP_PUBKEY(), DummyCommandData.INSTANCE);
// Invalid command // Invalid command
return tw.failsWith("required net.corda.finance.contracts.asset.Cash.Commands.Move command"); return tw.failsWith("required net.corda.finance.contracts.asset.Cash.Commands.Move command");
}); });
tx.tweak(tw -> { tx.tweak(tw -> {
tw.output(outState); tw.output(outState);
tw.command(getMINI_CORP_PUBKEY(), new Cash.Commands.Move()); tw.command(getMINI_CORP_PUBKEY(), new Cash.Commands.Move());
return tw.failsWith("the owning keys are a subset of the signing keys"); return tw.failsWith("the owning keys are a subset of the signing keys");
}); });
tx.tweak(tw -> { tx.tweak(tw -> {
tw.output(outState); tw.output(outState);
// issuedBy() can't be directly imported because it conflicts with other identically named functions // issuedBy() can't be directly imported because it conflicts with other identically named functions
// with different overloads (for some reason). // with different overloads (for some reason).
tw.output(outState.issuedBy(getMINI_CORP())); tw.output(outState.issuedBy(getMINI_CORP()));
tw.command(getMEGA_CORP_PUBKEY(), new Cash.Commands.Move()); tw.command(getMEGA_CORP_PUBKEY(), new Cash.Commands.Move());
return tw.failsWith("at least one cash input"); return tw.failsWith("at least one cash input");
}); });
// Simple reallocation works. // Simple reallocation works.
return tx.tweak(tw -> { return tx.tweak(tw -> {
tw.output(outState); tw.output(outState);
tw.command(getMEGA_CORP_PUBKEY(), new Cash.Commands.Move()); tw.command(getMEGA_CORP_PUBKEY(), new Cash.Commands.Move());
return tw.verifies(); return tw.verifies();
});
}); });
return Unit.INSTANCE;
}); });
} }
} }

View File

@ -155,7 +155,9 @@ fun getFreeLocalPorts(hostName: String, numberToAlloc: Int): List<NetworkHostAnd
transactionBuilder: TransactionBuilder = TransactionBuilder(notary = DUMMY_NOTARY), transactionBuilder: TransactionBuilder = TransactionBuilder(notary = DUMMY_NOTARY),
initialiseSerialization: Boolean = true, initialiseSerialization: Boolean = true,
dsl: TransactionDSL<TransactionDSLInterpreter>.() -> EnforceVerifyOrFail dsl: TransactionDSL<TransactionDSLInterpreter>.() -> EnforceVerifyOrFail
) = ledger(initialiseSerialization = initialiseSerialization) { this.transaction(transactionLabel, transactionBuilder, dsl) } ) = ledger(initialiseSerialization = initialiseSerialization) {
dsl(TransactionDSL(TestTransactionDSLInterpreter(this.interpreter, transactionBuilder)))
}
fun testNodeConfiguration( fun testNodeConfiguration(
baseDirectory: Path, baseDirectory: Path,

View File

@ -244,11 +244,13 @@ data class TestLedgerDSLInterpreter private constructor(
transactionLabel: String?, transactionLabel: String?,
transactionBuilder: TransactionBuilder, transactionBuilder: TransactionBuilder,
dsl: TransactionDSL<TestTransactionDSLInterpreter>.() -> R, 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 { ): WireTransaction {
val transactionLocation = getCallerLocation() val transactionLocation = getCallerLocation()
val transactionInterpreter = interpretTransactionDsl(transactionBuilder, dsl) val transactionInterpreter = interpretTransactionDsl(transactionBuilder, dsl)
makeTxValid(transactionBuilder) if (fillTransaction) fillTransaction(transactionBuilder)
// Create the WireTransaction // Create the WireTransaction
val wireTransaction = transactionInterpreter.toWireTransaction() val wireTransaction = transactionInterpreter.toWireTransaction()
// Record the output states // 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, * 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. * 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.commands().isEmpty()) transactionBuilder.addCommand(dummyCommand())
if (transactionBuilder.inputStates().isEmpty() && transactionBuilder.outputStates().isEmpty()) { if (transactionBuilder.inputStates().isEmpty() && transactionBuilder.outputStates().isEmpty()) {
transactionBuilder.addOutputState(DummyContract.SingleOwnerState(owner = ALICE)) transactionBuilder.addOutputState(DummyContract.SingleOwnerState(owner = ALICE))
@ -288,7 +290,7 @@ data class TestLedgerDSLInterpreter private constructor(
transactionLabel: String?, transactionLabel: String?,
transactionBuilder: TransactionBuilder, transactionBuilder: TransactionBuilder,
dsl: TransactionDSL<TestTransactionDSLInterpreter>.() -> Unit dsl: TransactionDSL<TestTransactionDSLInterpreter>.() -> Unit
) = recordTransactionWithTransactionMap(transactionLabel, transactionBuilder, dsl, nonVerifiedTransactionWithLocations) ) = recordTransactionWithTransactionMap(transactionLabel, transactionBuilder, dsl, nonVerifiedTransactionWithLocations, fillTransaction = true)
override fun tweak( override fun tweak(
dsl: LedgerDSL<TestTransactionDSLInterpreter, dsl: LedgerDSL<TestTransactionDSLInterpreter,