public class MockServices implements ServiceHub
An implementation of interface 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 class 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.
interface ServiceHub
,
class MockNetwork
Modifier and Type | Class and Description |
---|---|
static class |
MockServices.Companion |
ServiceHub.DefaultImpls
Modifier and Type | Field and Description |
---|---|
static MockServices.Companion |
Companion |
Constructor and Description |
---|
MockServices(java.lang.Iterable<java.lang.String> cordappPackages,
NonExistentClass initialIdentity,
IdentityService identityService,
java.security.KeyPair moreKeys)
Create a mock
interface ServiceHub that looks for app code in the given package names, uses the provided identity service
(you can get one from MockServicesKt.makeTestIdentityService ) and represents the given identity. |
MockServices(java.lang.Iterable<java.lang.String> cordappPackages,
NonExistentClass initialIdentity,
IdentityService identityService,
NetworkParameters networkParameters,
java.security.KeyPair moreKeys) |
MockServices(java.lang.Iterable<java.lang.String> cordappPackages,
NonExistentClass initialIdentity,
IdentityService identityService,
NetworkParameters networkParameters,
java.security.KeyPair[] moreKeys,
KeyManagementService keyManagementService) |
MockServices(java.lang.Iterable<java.lang.String> cordappPackages,
CordaX500Name initialIdentityName,
IdentityService identityService,
java.security.KeyPair key,
java.security.KeyPair moreKeys)
Create a mock
interface ServiceHub that looks for app code in the given package names, uses the provided identity service
(you can get one from MockServicesKt.makeTestIdentityService ) and represents the given identity. |
MockServices(java.lang.Iterable<java.lang.String> cordappPackages,
CordaX500Name initialIdentityName,
IdentityService identityService)
Create a mock
interface ServiceHub that can't load CorDapp code, which uses the provided identity service
(you can get one from MockServicesKt.makeTestIdentityService ) and which represents the given identity. |
MockServices(java.lang.Iterable<java.lang.String> cordappPackages)
Create a mock
interface ServiceHub that can't load CorDapp code, and which uses a default service identity. |
MockServices(CordaX500Name initialIdentityName,
IdentityService identityService,
java.security.KeyPair key,
java.security.KeyPair moreKeys)
Create a mock
interface ServiceHub which uses the package of the caller to find CorDapp code. It uses the provided identity service
(you can get one from MockServicesKt.makeTestIdentityService ) and which represents the given identity. |
MockServices(CordaX500Name initialIdentityName,
IdentityService identityService)
Create a mock
interface ServiceHub which uses the package of the caller to find CorDapp code. It uses the provided identity service
(you can get one from MockServicesKt.makeTestIdentityService ) and which represents the given identity. It has no keys. |
MockServices(java.util.List<java.lang.String> cordappPackages,
CordaX500Name initialIdentityName,
IdentityService identityService,
NetworkParameters networkParameters) |
MockServices(java.util.List<java.lang.String> cordappPackages,
CordaX500Name initialIdentityName,
IdentityService identityService,
NetworkParameters networkParameters,
java.security.KeyPair key) |
MockServices(NonExistentClass firstIdentity,
NonExistentClass moreIdentities)
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(NonExistentClass firstIdentity,
NetworkParameters networkParameters,
NonExistentClass moreIdentities) |
MockServices(java.util.List<java.lang.String> cordappPackages,
NonExistentClass firstIdentity,
NonExistentClass moreIdentities) |
MockServices(java.util.List<java.lang.String> cordappPackages,
NonExistentClass firstIdentity,
NetworkParameters networkParameters,
NonExistentClass moreIdentities) |
MockServices()
Create a mock
interface ServiceHub which uses the package of the caller to find CorDapp code. It uses a default service
identity. |
Modifier and Type | Method and Description |
---|---|
void |
addMockCordapp(java.lang.String contractClassName)
Add the given package name to the list of packages which will be scanned for cordapp contract verification code
|
<T extends SerializeAsToken> |
cordaService(java.lang.Class<T> type)
Return the singleton instance of the given Corda service type. This is a class that is annotated with
interface CordaService and will have automatically been registered by the node. |
MockAttachmentStorage |
getAttachments()
Provides access to storage of arbitrary JAR files (which may contain only data, no code).
|
TestClock |
getClock()
A Clock representing the node's current time. This should be used in preference to directly accessing the
clock so the current time can be controlled during unit testing.
|
ContractUpgradeService |
getContractUpgradeService()
The
interface ContractUpgradeService is responsible for securely upgrading contract state objects according to
a specified and mutually agreed (amongst participants) contract version. |
java.lang.ClassLoader |
getCordappClassloader()
Returns the classloader containing all jar deployed in the 'cordapps' folder.
|
CordappProvider |
getCordappProvider()
Provides access to anything relating to cordapps including contract attachment resolution and app context
|
DiagnosticsService |
getDiagnosticsService()
The
interface DiagnosticsService provides diagnostic level information about the node, including the current version of the node, and the
CorDapps currently installed on the node. Note that this information should be used to provide diagnostics only, and no functional
decisions should be made based on this. |
IdentityService |
getIdentityService()
An identity service maintains a directory of parties by their associated distinguished name/public keys and thus
|
KeyManagementService |
getKeyManagementService()
The key management service is responsible for storing and using private keys to sign things. An
implementation of this may, for example, call out to a hardware security module that enforces various
auditing and frequency-of-use requirements.
|
NodeInfo |
getMyInfo()
The
class NodeInfo object corresponding to our own entry in the network map. |
NetworkMapCache |
getNetworkMapCache()
A network map contains lists of nodes on the network along with information about their identity keys, services
they provide and host names or IP addresses where they can be connected to. The cache wraps around a map fetched
from an authoritative service, and adds easy lookup of the data stored within it. Generally it would be initialised
with a specified network map service, which it fetches data from and then subscribes to updates of.
|
NetworkParameters |
getNetworkParameters()
Returns the network parameters the node is operating under.
|
NetworkParametersService |
getNetworkParametersService()
Provides access to historical network parameters that are used in transaction resolution.
|
ServicesForResolution |
getServicesForResolution() |
net.corda.core.node.services.TransactionVerifierService |
getTransactionVerifierService()
INTERNAL. DO NOT USE.
|
TransactionStorage |
getValidatedTransactions()
A map of hash-
|
VaultService |
getVaultService()
The vault service lets you observe, soft lock and add notes to states that involve you or are relevant to your
node in some way. Additionally you may query and track states that correspond to various criteria.
|
java.sql.Connection |
jdbcSession()
Exposes a JDBC connection (session) object using the currently configured database.
Applications can use this to execute arbitrary SQL queries (native, direct, prepared, callable)
against its Node database tables (including custom contract tables defined by extending
interface QueryableState ). |
NonExistentClass |
loadContractAttachment(StateRef stateRef)
Returns a dummy Attachment, in context of signature constrains non-downgrade rule this default to contract class version
1 . |
TransactionState<?> |
loadState(StateRef stateRef)
Given a
class StateRef loads the referenced transaction and looks up the specified output interface ContractState . |
java.util.Set<net.corda.core.contracts.StateAndRef> |
loadStates(java.util.Set<net.corda.core.contracts.StateRef> stateRefs)
Given a Set of
class StateRef 's loads the referenced transaction and looks up the specified output interface ContractState . |
void |
recordTransactions(StatesToRecord statesToRecord,
java.lang.Iterable<net.corda.core.transactions.SignedTransaction> txs)
Stores the given
class SignedTransaction s in the local transaction storage and then sends them to the vault for
further processing if statesToRecord is not StatesToRecord.NONE.
This is expected to be run within a database transaction. |
java.lang.Void |
registerUnloadHandler(Function0<Unit> runOnStop)
Allows the registration of a callback that may inform services when the app is shutting down.
|
void |
setNetworkParametersService(NetworkParametersService p)
Provides access to historical network parameters that are used in transaction resolution.
|
<T> T |
withEntityManager(Function1<? super error.NonExistentClass,? extends T> block) |
void |
withEntityManager(java.util.function.Consumer<NonExistentClass> block) |
addSignature, addSignature, cordaService, createSignature, createSignature, createSignature, createSignature, getAppContext, getClock, getContractUpgradeService, getDiagnosticsService, getKeyManagementService, getMyInfo, getNetworkMapCache, getValidatedTransactions, getVaultService, jdbcSession, recordTransactions, recordTransactions, recordTransactions, recordTransactions, recordTransactions, registerUnloadHandler, signInitialTransaction, signInitialTransaction, signInitialTransaction, toStateAndRef, withEntityManager, withEntityManager
getAttachments, getCordappProvider, getIdentityService, getNetworkParameters, getNetworkParametersService, loadContractAttachment, loadState, loadStates, specialise
public static MockServices.Companion Companion
public MockServices(java.lang.Iterable<java.lang.String> cordappPackages, NonExistentClass initialIdentity, IdentityService identityService, java.security.KeyPair moreKeys)
Create a mock interface ServiceHub
that looks for app code in the given package names, uses the provided identity service
(you can get one from MockServicesKt.makeTestIdentityService
) and represents the given identity.
public MockServices(java.lang.Iterable<java.lang.String> cordappPackages, NonExistentClass initialIdentity, IdentityService identityService, NetworkParameters networkParameters, java.security.KeyPair moreKeys)
public MockServices(java.lang.Iterable<java.lang.String> cordappPackages, NonExistentClass initialIdentity, IdentityService identityService, NetworkParameters networkParameters, java.security.KeyPair[] moreKeys, KeyManagementService keyManagementService)
public MockServices(java.lang.Iterable<java.lang.String> cordappPackages, CordaX500Name initialIdentityName, IdentityService identityService, java.security.KeyPair key, java.security.KeyPair moreKeys)
Create a mock interface ServiceHub
that looks for app code in the given package names, uses the provided identity service
(you can get one from MockServicesKt.makeTestIdentityService
) and represents the given identity.
public MockServices(java.lang.Iterable<java.lang.String> cordappPackages, CordaX500Name initialIdentityName, IdentityService identityService)
Create a mock interface ServiceHub
that can't load CorDapp code, which uses the provided identity service
(you can get one from MockServicesKt.makeTestIdentityService
) and which represents the given identity.
public MockServices(java.lang.Iterable<java.lang.String> cordappPackages)
Create a mock interface ServiceHub
that can't load CorDapp code, and which uses a default service identity.
interface ServiceHub
public MockServices(CordaX500Name initialIdentityName, IdentityService identityService, java.security.KeyPair key, java.security.KeyPair moreKeys)
Create a mock interface ServiceHub
which uses the package of the caller to find CorDapp code. It uses the provided identity service
(you can get one from MockServicesKt.makeTestIdentityService
) and which represents the given identity.
public MockServices(CordaX500Name initialIdentityName, IdentityService identityService)
Create a mock interface ServiceHub
which uses the package of the caller to find CorDapp code. It uses the provided identity service
(you can get one from MockServicesKt.makeTestIdentityService
) and which represents the given identity. It has no keys.
public MockServices(java.util.List<java.lang.String> cordappPackages, CordaX500Name initialIdentityName, IdentityService identityService, NetworkParameters networkParameters)
public MockServices(java.util.List<java.lang.String> cordappPackages, CordaX500Name initialIdentityName, IdentityService identityService, NetworkParameters networkParameters, java.security.KeyPair key)
public MockServices(NonExistentClass firstIdentity, NonExistentClass moreIdentities)
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.
public MockServices(NonExistentClass firstIdentity, NetworkParameters networkParameters, NonExistentClass moreIdentities)
public MockServices(java.util.List<java.lang.String> cordappPackages, NonExistentClass firstIdentity, NonExistentClass moreIdentities)
public MockServices(java.util.List<java.lang.String> cordappPackages, NonExistentClass firstIdentity, NetworkParameters networkParameters, NonExistentClass moreIdentities)
public MockServices()
Create a mock interface ServiceHub
which uses the package of the caller to find CorDapp code. It uses a default service
identity.
interface ServiceHub
public java.lang.ClassLoader getCordappClassloader()
Returns the classloader containing all jar deployed in the 'cordapps' folder.
public void recordTransactions(StatesToRecord statesToRecord, java.lang.Iterable<net.corda.core.transactions.SignedTransaction> txs)
Stores the given class SignedTransaction
s in the local transaction storage and then sends them to the vault for
further processing if statesToRecord is not StatesToRecord.NONE.
This is expected to be run within a database transaction.
txs
- The transactions to record.statesToRecord
- how the vault should treat the output states of the transaction.class SignedTransaction
public NetworkParameters getNetworkParameters()
Returns the network parameters the node is operating under.
public MockAttachmentStorage getAttachments()
Provides access to storage of arbitrary JAR files (which may contain only data, no code).
public VaultService getVaultService()
The vault service lets you observe, soft lock and add notes to states that involve you or are relevant to your node in some way. Additionally you may query and track states that correspond to various criteria.
public ContractUpgradeService getContractUpgradeService()
The interface ContractUpgradeService
is responsible for securely upgrading contract state objects according to
a specified and mutually agreed (amongst participants) contract version.
public NetworkMapCache getNetworkMapCache()
A network map contains lists of nodes on the network along with information about their identity keys, services they provide and host names or IP addresses where they can be connected to. The cache wraps around a map fetched from an authoritative service, and adds easy lookup of the data stored within it. Generally it would be initialised with a specified network map service, which it fetches data from and then subscribes to updates of.
public TestClock getClock()
A Clock representing the node's current time. This should be used in preference to directly accessing the clock so the current time can be controlled during unit testing.
public NodeInfo getMyInfo()
The class NodeInfo
object corresponding to our own entry in the network map.
class NodeInfo
public net.corda.core.node.services.TransactionVerifierService getTransactionVerifierService()
INTERNAL. DO NOT USE.
public CordappProvider getCordappProvider()
Provides access to anything relating to cordapps including contract attachment resolution and app context
public NetworkParametersService getNetworkParametersService()
Provides access to historical network parameters that are used in transaction resolution.
public void setNetworkParametersService(NetworkParametersService p)
Provides access to historical network parameters that are used in transaction resolution.
public DiagnosticsService getDiagnosticsService()
The interface DiagnosticsService
provides diagnostic level information about the node, including the current version of the node, and the
CorDapps currently installed on the node. Note that this information should be used to provide diagnostics only, and no functional
decisions should be made based on this.
interface DiagnosticsService
public ServicesForResolution getServicesForResolution()
public <T extends SerializeAsToken> T cordaService(java.lang.Class<T> type)
Return the singleton instance of the given Corda service type. This is a class that is annotated with
interface CordaService
and will have automatically been registered by the node.
interface CordaService
or if the instance is not found.interface CordaService
public java.sql.Connection jdbcSession()
Exposes a JDBC connection (session) object using the currently configured database.
Applications can use this to execute arbitrary SQL queries (native, direct, prepared, callable)
against its Node database tables (including custom contract tables defined by extending
interface QueryableState
).
When used within a flow, this session automatically forms part of the enclosing flow transaction boundary, and thus queryable data will include everything committed as of the last checkpoint.
We want to make sure users have a restricted access to administrative functions, this function will return a RestrictedConnection instance. The following methods are blocked:
abort(executor: Executor?)
clearWarnings()
close()
commit()
setSavepoint()
setSavepoint(name : String?)
releaseSavepoint(savepoint: Savepoint?)
rollback()
rollback(savepoint: Savepoint?)
setCatalog(catalog : String?)
setTransactionIsolation(level: Int)
setTypeMap(map: MutableMap
setHoldability(holdability: Int)
setSchema(schema: String?)
setNetworkTimeout(executor: Executor?, milliseconds: Int)
setAutoCommit(autoCommit: Boolean)
setReadOnly(readOnly: Boolean)
interface QueryableState
,
RestrictedConnectionpublic <T> T withEntityManager(Function1<? super error.NonExistentClass,? extends T> block)
public void withEntityManager(java.util.function.Consumer<NonExistentClass> block)
public java.lang.Void registerUnloadHandler(Function0<Unit> runOnStop)
Allows the registration of a callback that may inform services when the app is shutting down.
The intent is to allow the cleaning up of resources - e.g. releasing ports.
You should not rely on this to clean up executing flows - that's what quasar is for.
Please note that the shutdown handler is not guaranteed to be called. In production the node process may crash, be killed by the operating system and other forms of fatal termination may occur that result in this code never running. So you should use this functionality only for unit/integration testing or for code that can optimise this shutdown e.g. by cleaning up things that would otherwise trigger a slow recovery process next time the node starts.
public void addMockCordapp(java.lang.String contractClassName)
Add the given package name to the list of packages which will be scanned for cordapp contract verification code
public TransactionState<?> loadState(StateRef stateRef)
Given a class StateRef
loads the referenced transaction and looks up the specified output interface ContractState
.
WARNING Do not use this method unless you really only want a single state - any batch loading should
go through ServicesForResolution.loadStates
as repeatedly calling ServicesForResolution.loadState
can lead to repeat deserialsiation work and
severe performance degradation.
class StateRef
,
interface ContractState
,
ServicesForResolution.loadStates
,
ServicesForResolution.loadState
public java.util.Set<net.corda.core.contracts.StateAndRef> loadStates(java.util.Set<net.corda.core.contracts.StateRef> stateRefs)
Given a Set of class StateRef
's loads the referenced transaction and looks up the specified output interface ContractState
.
class StateRef
,
interface ContractState
public NonExistentClass loadContractAttachment(StateRef stateRef)
Returns a dummy Attachment, in context of signature constrains non-downgrade rule this default to contract class version 1
.
public TransactionStorage getValidatedTransactions()
A map of hash-
>tx where tx has been signature/contract validated and the states are known to be correct.The signatures aren't technically needed after that point, but we keep them around so that we can relaythe transaction data to other nodes that need it.
public IdentityService getIdentityService()
An identity service maintains a directory of parties by their associated distinguished name/public keys and thus
supports lookup of a party given its key, or name. The service also manages the certificates linking confidentialidentities back to the well known identity (i.e. the identity in the network map) of a party.
public KeyManagementService getKeyManagementService()
The key management service is responsible for storing and using private keys to sign things. An implementation of this may, for example, call out to a hardware security module that enforces various auditing and frequency-of-use requirements.
You don't normally need to use this directly. If you have a class TransactionBuilder
and wish to sign it to
get a class SignedTransaction
, look at signInitialTransaction.
class TransactionBuilder
,
class SignedTransaction