corda / net.corda.testing.node / MockServices

MockServices

open class MockServices

An implementation of ServiceHub that is designed for in-memory unit tests of contract validation logic. It has enough functionality to do tests of code that queries the vault, inserts to the vault, and constructs/checks transactions. However it isn't enough to test flows and other aspects of an app that require a network. For that you should investigate MockNetwork.

There are a variety of constructors that can be used to supply enough data to simulate a node. Each mock service hub must have at least an identity of its own. The other components have defaults that work in most situations.

Constructors

<init>

MockServices(cordappPackages: Iterable<String>, initialIdentity: <ERROR CLASS>, identityService: <ERROR CLASS> = makeTestIdentityService(), vararg moreKeys: KeyPair)
MockServices(cordappPackages: Iterable<String>, initialIdentityName: <ERROR CLASS>, identityService: <ERROR CLASS> = makeTestIdentityService(), key: KeyPair, vararg moreKeys: KeyPair)

Create a mock ServiceHub that looks for app code in the given package names, uses the provided identity service (you can get one from makeTestIdentityService) and represents the given identity.

MockServices(cordappPackages: Iterable<String>, initialIdentity: <ERROR CLASS>, identityService: <ERROR CLASS>, networkParameters: <ERROR CLASS>, vararg moreKeys: KeyPair)
MockServices(cordappPackages: Iterable<String>, initialIdentity: <ERROR CLASS>, identityService: <ERROR CLASS>, networkParameters: <ERROR CLASS>, vararg moreKeys: KeyPair, keyManagementService: <ERROR CLASS>)
MockServices(cordappPackages: List<String>, initialIdentityName: <ERROR CLASS>, identityService: <ERROR CLASS>, networkParameters: <ERROR CLASS>)
MockServices(cordappPackages: List<String>, initialIdentityName: <ERROR CLASS>, identityService: <ERROR CLASS>, networkParameters: <ERROR CLASS>, key: KeyPair)
MockServices(firstIdentity: <ERROR CLASS>, networkParameters: <ERROR CLASS>, vararg moreIdentities: <ERROR CLASS>)
MockServices(cordappPackages: List<String>, firstIdentity: <ERROR CLASS>, vararg moreIdentities: <ERROR CLASS>)
MockServices(cordappPackages: List<String>, firstIdentity: <ERROR CLASS>, networkParameters: <ERROR CLASS>, vararg moreIdentities: <ERROR CLASS>)MockServices(cordappPackages: Iterable<String>, initialIdentityName: <ERROR CLASS>, identityService: <ERROR CLASS> = makeTestIdentityService())

Create a mock ServiceHub that can't load CorDapp code, which uses the provided identity service (you can get one from makeTestIdentityService) and which represents the given identity.

MockServices(cordappPackages: Iterable<String>)

Create a mock ServiceHub that can't load CorDapp code, and which uses a default service identity.

MockServices(initialIdentityName: <ERROR CLASS>, identityService: <ERROR CLASS> = makeTestIdentityService(), key: KeyPair, vararg moreKeys: KeyPair)

Create a mock ServiceHub which uses the package of the caller to find CorDapp code. It uses the provided identity service (you can get one from makeTestIdentityService) and which represents the given identity.

MockServices(initialIdentityName: <ERROR CLASS>, identityService: <ERROR CLASS> = makeTestIdentityService())

Create a mock ServiceHub which uses the package of the caller to find CorDapp code. It uses the provided identity service (you can get one from makeTestIdentityService) and which represents the given identity. It has no keys.

MockServices(firstIdentity: <ERROR CLASS>, vararg moreIdentities: <ERROR CLASS>)

A helper constructor that requires at least one test identity to be registered, and which takes the package of the caller as the package in which to find app code. This is the most convenient constructor and the one that is normally worth using. The first identity is the identity of this service hub, the rest are identities that it is aware of.

MockServices()

Create a mock ServiceHub which uses the package of the caller to find CorDapp code. It uses a default service identity.

Properties

attachments

val attachments: MockAttachmentStorage

clock

open val clock: TestClock

contractUpgradeService

open val contractUpgradeService: <ERROR CLASS>

cordappClassloader

val cordappClassloader: ClassLoader

Returns the classloader containing all jar deployed in the 'cordapps' folder.

cordappProvider

open val cordappProvider: <ERROR CLASS>

diagnosticsService

open val diagnosticsService: <ERROR CLASS>

identityService

open val identityService: <ERROR CLASS>

keyManagementService

open val keyManagementService: <ERROR CLASS>

myInfo

open val myInfo: <ERROR CLASS>

networkMapCache

open val networkMapCache: <ERROR CLASS>

networkParameters

open val networkParameters: <ERROR CLASS>

networkParametersService

open var networkParametersService: <ERROR CLASS>

servicesForResolution

val servicesForResolution: <ERROR CLASS>

transactionVerifierService

open val transactionVerifierService: <ERROR CLASS>

validatedTransactions

open val validatedTransactions: <ERROR CLASS>

vaultService

open val vaultService: <ERROR CLASS>

Functions

addMockCordapp

fun addMockCordapp(contractClassName: <ERROR CLASS>): Unit

Add the given package name to the list of packages which will be scanned for cordapp contract verification code

cordaService

open fun <T> cordaService(type: Class<T>): T

jdbcSession

open fun jdbcSession(): Connection

loadContractAttachment

open fun loadContractAttachment(stateRef: <ERROR CLASS>): <ERROR CLASS>

Returns a dummy Attachment, in context of signature constrains non-downgrade rule this default to contract class version 1.

loadState

open fun loadState(stateRef: <ERROR CLASS>): <ERROR CLASS>

loadStates

open fun loadStates(stateRefs: Set<<ERROR CLASS>>): <ERROR CLASS>

recordTransactions

open fun recordTransactions(statesToRecord: <ERROR CLASS>, txs: Iterable<<ERROR CLASS>>): Unit

registerUnloadHandler

open fun registerUnloadHandler(runOnStop: () -> Unit): Nothing

withEntityManager

open fun <T> withEntityManager(block: <ERROR CLASS>.() -> T): T
open fun withEntityManager(block: Consumer<<ERROR CLASS>>): Unit

Companion Object Functions

makeTestDataSourceProperties

fun makeTestDataSourceProperties(nodeName: String = SecureHash.randomSHA256().toString()): Properties
fun makeTestDataSourceProperties(nodeName: String = SecureHash.randomSHA256().toString(), inMemory: Boolean = false): Properties

Make properties appropriate for creating a DataSource for unit tests. Defaults configuration of on-disk H2 instance. If 'databaseProvider' system property is set then creates a config from the relevant config file is present in resources folder (used to parametrize test to run against a remote database).

makeTestDatabaseAndMockServices

fun makeTestDatabaseAndMockServices(cordappPackages: List<String>, identityService: <ERROR CLASS>, initialIdentity: <ERROR CLASS>, networkParameters: <ERROR CLASS> = testNetworkParameters(modifiedTime = Instant.MIN), vararg moreKeys: KeyPair, softLockingSqlInClauseSize: Int = NodeVaultService.DEFAULT_SOFT_LOCKING_SQL_IN_CLAUSE_SIZE): <ERROR CLASS><<ERROR CLASS>, MockServices>

Makes database and mock services appropriate for unit tests.

makeTestDatabaseAndPersistentServices

fun makeTestDatabaseAndPersistentServices(cordappPackages: List<String>, initialIdentity: <ERROR CLASS>, networkParameters: <ERROR CLASS> = testNetworkParameters(modifiedTime = Instant.MIN), moreKeys: Set<KeyPair>, moreIdentities: Set<<ERROR CLASS>>, cacheFactory: TestingNamedCacheFactory = TestingNamedCacheFactory()): <ERROR CLASS><<ERROR CLASS>, MockServices>

Makes database and persistent services appropriate for unit tests which require persistence across the vault, identity service and key managment service.