Add tests for TransactionGraphSearch

This commit is contained in:
Ross Nicoll 2016-06-15 14:33:42 +01:00
parent 07153578f0
commit f831ffdf3d
7 changed files with 80 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import com.r3corda.contracts.cash.Cash
import com.r3corda.contracts.cash.CASH_PROGRAM_ID
import com.r3corda.core.contracts.Amount
import com.r3corda.core.contracts.Contract
import com.r3corda.core.contracts.DummyContract
import com.r3corda.core.crypto.NullPublicKey
import com.r3corda.core.crypto.Party
import com.r3corda.core.testing.DUMMY_NOTARY

View File

@ -1,6 +1,6 @@
package com.r3corda.contracts.cash
import com.r3corda.contracts.DummyContract
import com.r3corda.core.contracts.DummyContract
import com.r3corda.contracts.testing.`issued by`
import com.r3corda.contracts.testing.`owned by`
import com.r3corda.core.contracts.*

View File

@ -1,7 +1,5 @@
package com.r3corda.contracts
package com.r3corda.core.contracts
import com.r3corda.core.*
import com.r3corda.core.contracts.*
import com.r3corda.core.crypto.Party
import com.r3corda.core.crypto.SecureHash

View File

@ -13,7 +13,8 @@ import java.util.concurrent.Callable
*
* In future, this should support restricting the search by time, and other types of useful query.
*
* TODO: Write unit tests for this.
* @param transactions map of transaction id to [SignedTransaction]
* @param startPoints transactions to use as starting points for the search
*/
class TransactionGraphSearch(val transactions: TransactionStorage,
val startPoints: List<WireTransaction>) : Callable<List<WireTransaction>> {

View File

@ -77,7 +77,7 @@ class MockAttachmentStorage : AttachmentStorage {
}
}
class MockTransactionStorage : TransactionStorage {
open class MockTransactionStorage : TransactionStorage {
private val txns = HashMap<SecureHash, SignedTransaction>()
override fun addTransaction(transaction: SignedTransaction) {
txns[transaction.id] = transaction

View File

@ -0,0 +1,73 @@
package com.r3corda.core.contracts
import com.r3corda.core.node.services.testing.MockTransactionStorage
import com.r3corda.core.testing.DUMMY_NOTARY
import com.r3corda.core.testing.MEGA_CORP_KEY
import org.junit.Test
import java.security.KeyPair
import java.security.SecureRandom
import kotlin.test.assertEquals
class TransactionGraphSearchTests {
class GraphTransactionStorage(val originTx: SignedTransaction, val inputTx: SignedTransaction): MockTransactionStorage() {
init {
addTransaction(originTx)
addTransaction(inputTx)
}
}
fun random31BitValue(): Int = Math.abs(SecureRandom.getInstanceStrong().nextInt())
/**
* Build a pair of transactions. The first issues a dummy output state, and has a command applied, the second then
* references that state.
*
* @param command the command to add to the origin transaction.
* @param signer signer for the two transactions and their commands.
*/
fun buildTransactions(command: CommandData, signer: KeyPair): GraphTransactionStorage {
val originTx = TransactionBuilder().apply {
addOutputState(DummyContract.State(random31BitValue(), DUMMY_NOTARY))
addCommand(command, signer.public)
signWith(signer)
}.toSignedTransaction(false)
val inputTx = TransactionBuilder().apply {
addInputState(originTx.tx.outRef<DummyContract.State>(0).ref)
signWith(signer)
}.toSignedTransaction(false)
return GraphTransactionStorage(originTx, inputTx)
}
@Test
fun `return empty from empty`() {
val storage = buildTransactions(DummyContract.Commands.Create(), MEGA_CORP_KEY)
val search = TransactionGraphSearch(storage, emptyList())
search.query = TransactionGraphSearch.Query()
val expected = emptyList<WireTransaction>()
val actual = search.call()
assertEquals(expected, actual)
}
@Test
fun `return empty from no match`() {
val storage = buildTransactions(DummyContract.Commands.Create(), MEGA_CORP_KEY)
val search = TransactionGraphSearch(storage, listOf(storage.inputTx.tx))
search.query = TransactionGraphSearch.Query()
val expected = emptyList<WireTransaction>()
val actual = search.call()
assertEquals(expected, actual)
}
@Test
fun `return origin on match`() {
val storage = buildTransactions(DummyContract.Commands.Create(), MEGA_CORP_KEY)
val search = TransactionGraphSearch(storage, listOf(storage.inputTx.tx))
search.query = TransactionGraphSearch.Query(DummyContract.Commands.Create::class.java)
val expected = listOf(storage.originTx.tx)
val actual = search.call()
assertEquals(expected, actual)
}
}

View File

@ -1,6 +1,6 @@
package com.r3corda.node.internal.testing
import com.r3corda.contracts.DummyContract
import com.r3corda.core.contracts.DummyContract
import com.r3corda.core.contracts.StateRef
import com.r3corda.core.crypto.Party
import com.r3corda.core.seconds