From c36bea3af521be69f7702d7b7abbbfac0258de1f Mon Sep 17 00:00:00 2001 From: Andrzej Cichocki Date: Thu, 7 Dec 2017 11:55:18 +0000 Subject: [PATCH] CORDA-654 MockServices no longer has hard-coded identities (#2192) * Resurrect a test * Fix a broken test * Pass in the initial identity * Make IdentityService easier to mock --- .../core/contracts/DummyContractV2Tests.kt | 13 +++++---- .../core/flows/CollectSignaturesFlowTests.kt | 2 +- .../TransactionSerializationTests.kt | 7 +++-- .../LedgerTransactionQueryTests.kt | 14 ++++++---- .../finance/contracts/CommercialPaperTests.kt | 19 ++++++------- .../finance/contracts/asset/CashTests.kt | 15 +++++++---- .../contracts/asset/ObligationTests.kt | 13 ++++++--- .../ContractAttachmentSerializerTest.kt | 6 ++--- .../net/corda/node/internal/AbstractNode.kt | 5 ++-- .../services/api/IdentityServiceInternal.kt | 11 ++++++++ .../identity/InMemoryIdentityService.kt | 4 +-- .../identity/PersistentIdentityService.kt | 4 +-- .../keys/E2ETestKeyManagementService.kt | 4 +-- .../net/corda/node/services/keys/KMSUtils.kt | 6 ++--- .../keys/PersistentKeyManagementService.kt | 4 +-- .../services/vault/VaultQueryJavaTests.java | 24 ++++++++--------- .../node/messaging/TwoPartyTradeFlowTests.kt | 27 ++++++++++++++----- .../persistence/HibernateConfigurationTest.kt | 15 ++++++----- .../services/vault/NodeVaultServiceTest.kt | 17 +++++++----- .../node/services/vault/VaultQueryTests.kt | 8 +++--- .../node/services/vault/VaultWithCashTest.kt | 6 ++--- .../corda/irs/api/NodeInterestRatesTest.kt | 7 ++--- .../kotlin/net/corda/irs/contract/IRSTests.kt | 16 +++++++---- .../traderdemo/TransactionGraphSearchTests.kt | 5 ++-- .../kotlin/net/corda/testing/NodeTestUtils.kt | 5 ++-- .../kotlin/net/corda/testing/node/MockNode.kt | 3 ++- .../net/corda/testing/node/MockServices.kt | 24 ++++++++--------- .../net/corda/loadtest/tests/NotaryTest.kt | 4 ++- 28 files changed, 168 insertions(+), 120 deletions(-) create mode 100644 node/src/main/kotlin/net/corda/node/services/api/IdentityServiceInternal.kt diff --git a/core/src/test/kotlin/net/corda/core/contracts/DummyContractV2Tests.kt b/core/src/test/kotlin/net/corda/core/contracts/DummyContractV2Tests.kt index 361fb6e876..29ad386359 100644 --- a/core/src/test/kotlin/net/corda/core/contracts/DummyContractV2Tests.kt +++ b/core/src/test/kotlin/net/corda/core/contracts/DummyContractV2Tests.kt @@ -1,13 +1,14 @@ package net.corda.core.contracts +import com.nhaarman.mockito_kotlin.doReturn +import com.nhaarman.mockito_kotlin.whenever +import net.corda.core.cordapp.CordappProvider import net.corda.core.crypto.SecureHash import net.corda.core.internal.UpgradeCommand -import net.corda.testing.ALICE -import net.corda.testing.DUMMY_NOTARY +import net.corda.core.node.ServicesForResolution +import net.corda.testing.* import net.corda.testing.contracts.DummyContract import net.corda.testing.contracts.DummyContractV2 -import net.corda.testing.node.MockServices -import net.corda.testing.SerializationEnvironmentRule import org.junit.Rule import org.junit.Test import kotlin.test.assertEquals @@ -23,7 +24,9 @@ class DummyContractV2Tests { @Test fun `upgrade from v1`() { - val services = MockServices() + val services = rigorousMock().also { + doReturn(rigorousMock()).whenever(it).cordappProvider + } val contractUpgrade = DummyContractV2() val v1State = TransactionState(DummyContract.SingleOwnerState(0, ALICE), DummyContract.PROGRAM_ID, DUMMY_NOTARY, constraint = AlwaysAcceptAttachmentConstraint) val v1Ref = StateRef(SecureHash.randomSHA256(), 0) diff --git a/core/src/test/kotlin/net/corda/core/flows/CollectSignaturesFlowTests.kt b/core/src/test/kotlin/net/corda/core/flows/CollectSignaturesFlowTests.kt index 0c2a00331b..d38e0fa841 100644 --- a/core/src/test/kotlin/net/corda/core/flows/CollectSignaturesFlowTests.kt +++ b/core/src/test/kotlin/net/corda/core/flows/CollectSignaturesFlowTests.kt @@ -129,7 +129,7 @@ class CollectSignaturesFlowTests { @Test fun `fails when not signed by initiator`() { val onePartyDummyContract = DummyContract.generateInitial(1337, notary, alice.ref(1)) - val miniCorpServices = MockServices(listOf("net.corda.testing.contracts"), MINI_CORP.name, MINI_CORP_KEY) + val miniCorpServices = MockServices(listOf("net.corda.testing.contracts"), rigorousMock(), MINI_CORP.name, MINI_CORP_KEY) val ptx = miniCorpServices.signInitialTransaction(onePartyDummyContract) val flow = aliceNode.services.startFlow(CollectSignaturesFlow(ptx, emptySet())) mockNet.runNetwork() diff --git a/core/src/test/kotlin/net/corda/core/serialization/TransactionSerializationTests.kt b/core/src/test/kotlin/net/corda/core/serialization/TransactionSerializationTests.kt index efa87a441f..631af14bfd 100644 --- a/core/src/test/kotlin/net/corda/core/serialization/TransactionSerializationTests.kt +++ b/core/src/test/kotlin/net/corda/core/serialization/TransactionSerializationTests.kt @@ -49,9 +49,8 @@ class TransactionSerializationTests { val inputState = StateAndRef(TransactionState(TestCash.State(depositRef, 100.POUNDS, MEGA_CORP), TEST_CASH_PROGRAM_ID, DUMMY_NOTARY), fakeStateRef) val outputState = TransactionState(TestCash.State(depositRef, 600.POUNDS, MEGA_CORP), TEST_CASH_PROGRAM_ID, DUMMY_NOTARY) val changeState = TransactionState(TestCash.State(depositRef, 400.POUNDS, MEGA_CORP), TEST_CASH_PROGRAM_ID, DUMMY_NOTARY) - - val megaCorpServices = MockServices(listOf("net.corda.core.serialization"), MEGA_CORP.name, MEGA_CORP_KEY) - val notaryServices = MockServices(listOf("net.corda.core.serialization"), DUMMY_NOTARY.name, DUMMY_NOTARY_KEY) + val megaCorpServices = MockServices(listOf("net.corda.core.serialization"), rigorousMock(), MEGA_CORP.name, MEGA_CORP_KEY) + val notaryServices = MockServices(listOf("net.corda.core.serialization"), rigorousMock(), DUMMY_NOTARY.name, DUMMY_NOTARY_KEY) lateinit var tx: TransactionBuilder @Before @@ -95,7 +94,7 @@ class TransactionSerializationTests { Command(TestCash.Commands.Move(), DUMMY_KEY_2.public)) val ptx2 = notaryServices.signInitialTransaction(tx2) - val dummyServices = MockServices(DUMMY_KEY_2) + val dummyServices = MockServices(rigorousMock(), MEGA_CORP.name, DUMMY_KEY_2) val stx2 = dummyServices.addSignature(ptx2) stx.copy(sigs = stx2.sigs).verifyRequiredSignatures() diff --git a/core/src/test/kotlin/net/corda/core/transactions/LedgerTransactionQueryTests.kt b/core/src/test/kotlin/net/corda/core/transactions/LedgerTransactionQueryTests.kt index eba531f13a..d9a8e9bc10 100644 --- a/core/src/test/kotlin/net/corda/core/transactions/LedgerTransactionQueryTests.kt +++ b/core/src/test/kotlin/net/corda/core/transactions/LedgerTransactionQueryTests.kt @@ -1,14 +1,15 @@ package net.corda.core.transactions +import com.nhaarman.mockito_kotlin.doReturn +import com.nhaarman.mockito_kotlin.whenever import net.corda.core.contracts.* +import net.corda.core.crypto.generateKeyPair import net.corda.core.identity.AbstractParty import net.corda.core.identity.Party -import net.corda.testing.DUMMY_NOTARY -import net.corda.testing.SerializationEnvironmentRule +import net.corda.node.services.api.IdentityServiceInternal +import net.corda.testing.* import net.corda.testing.contracts.DummyContract -import net.corda.testing.dummyCommand import net.corda.testing.node.MockServices -import net.corda.testing.singleIdentity import org.junit.Before import org.junit.Rule import org.junit.Test @@ -21,7 +22,10 @@ class LedgerTransactionQueryTests { @Rule @JvmField val testSerialization = SerializationEnvironmentRule() - private val services: MockServices = MockServices() + private val keyPair = generateKeyPair() + private val services = MockServices(rigorousMock().also { + doReturn(null).whenever(it).partyFromKey(keyPair.public) + }, MEGA_CORP.name, keyPair) private val identity: Party = services.myInfo.singleIdentity() @Before diff --git a/finance/src/test/kotlin/net/corda/finance/contracts/CommercialPaperTests.kt b/finance/src/test/kotlin/net/corda/finance/contracts/CommercialPaperTests.kt index 25d65f3dab..fc02955c6a 100644 --- a/finance/src/test/kotlin/net/corda/finance/contracts/CommercialPaperTests.kt +++ b/finance/src/test/kotlin/net/corda/finance/contracts/CommercialPaperTests.kt @@ -17,7 +17,6 @@ import net.corda.testing.contracts.VaultFiller import net.corda.testing.node.MockServices import net.corda.testing.node.MockServices.Companion.makeTestDatabaseAndMockServices import net.corda.testing.node.makeTestIdentityService -import org.junit.Ignore import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -224,19 +223,16 @@ class CommercialPaperTestsGeneric { private lateinit var aliceServices: MockServices private lateinit var aliceVaultService: VaultService private lateinit var alicesVault: Vault - - private val notaryServices = MockServices(DUMMY_NOTARY_KEY) - private val issuerServices = MockServices(DUMMY_CASH_ISSUER_KEY) - + private val notaryServices = MockServices(rigorousMock(), MEGA_CORP.name, DUMMY_NOTARY_KEY) + private val issuerServices = MockServices(listOf("net.corda.finance.contracts"), rigorousMock(), MEGA_CORP.name, DUMMY_CASH_ISSUER_KEY) private lateinit var moveTX: SignedTransaction - - // @Test - @Ignore - fun `issue move and then redeem`() = withTestSerialization { + @Test + fun `issue move and then redeem`() { val aliceDatabaseAndServices = makeTestDatabaseAndMockServices( listOf(ALICE_KEY), makeTestIdentityService(listOf(MEGA_CORP_IDENTITY, MINI_CORP_IDENTITY, DUMMY_CASH_ISSUER_IDENTITY, DUMMY_NOTARY_IDENTITY)), - initialIdentityName = MEGA_CORP.name) + listOf("net.corda.finance.contracts"), + MEGA_CORP.name) val databaseAlice = aliceDatabaseAndServices.first aliceServices = aliceDatabaseAndServices.second aliceVaultService = aliceServices.vaultService @@ -248,7 +244,8 @@ class CommercialPaperTestsGeneric { val bigCorpDatabaseAndServices = makeTestDatabaseAndMockServices( listOf(BIG_CORP_KEY), makeTestIdentityService(listOf(MEGA_CORP_IDENTITY, MINI_CORP_IDENTITY, DUMMY_CASH_ISSUER_IDENTITY, DUMMY_NOTARY_IDENTITY)), - initialIdentityName = MEGA_CORP.name) + listOf("net.corda.finance.contracts"), + MEGA_CORP.name) val databaseBigCorp = bigCorpDatabaseAndServices.first bigCorpServices = bigCorpDatabaseAndServices.second bigCorpVaultService = bigCorpServices.vaultService diff --git a/finance/src/test/kotlin/net/corda/finance/contracts/asset/CashTests.kt b/finance/src/test/kotlin/net/corda/finance/contracts/asset/CashTests.kt index bfec19b57d..2cf9daa949 100644 --- a/finance/src/test/kotlin/net/corda/finance/contracts/asset/CashTests.kt +++ b/finance/src/test/kotlin/net/corda/finance/contracts/asset/CashTests.kt @@ -1,5 +1,6 @@ package net.corda.finance.contracts.asset +import com.nhaarman.mockito_kotlin.* import net.corda.core.contracts.* import net.corda.core.crypto.SecureHash import net.corda.core.crypto.generateKeyPair @@ -18,6 +19,7 @@ import net.corda.finance.utils.sumCash import net.corda.finance.utils.sumCashBy import net.corda.finance.utils.sumCashOrNull import net.corda.finance.utils.sumCashOrZero +import net.corda.node.services.api.IdentityServiceInternal import net.corda.node.services.vault.NodeVaultService import net.corda.nodeapi.internal.persistence.CordaPersistence import net.corda.testing.* @@ -67,9 +69,11 @@ class CashTests { @Before fun setUp() { LogHelper.setLevel(NodeVaultService::class) - megaCorpServices = MockServices(listOf("net.corda.finance.contracts.asset"), MEGA_CORP.name, MEGA_CORP_KEY) - miniCorpServices = MockServices(listOf("net.corda.finance.contracts.asset"), MINI_CORP.name, MINI_CORP_KEY) - val notaryServices = MockServices(listOf("net.corda.finance.contracts.asset"), DUMMY_NOTARY.name, DUMMY_NOTARY_KEY) + megaCorpServices = MockServices(listOf("net.corda.finance.contracts.asset"), rigorousMock(), MEGA_CORP.name, MEGA_CORP_KEY) + miniCorpServices = MockServices(listOf("net.corda.finance.contracts.asset"), rigorousMock().also { + doNothing().whenever(it).justVerifyAndRegisterIdentity(argThat { name == MINI_CORP.name }) + }, MINI_CORP.name, MINI_CORP_KEY) + val notaryServices = MockServices(listOf("net.corda.finance.contracts.asset"), rigorousMock(), DUMMY_NOTARY.name, DUMMY_NOTARY_KEY) val databaseAndServices = makeTestDatabaseAndMockServices( listOf(generateKeyPair()), makeTestIdentityService(listOf(MEGA_CORP_IDENTITY, MINI_CORP_IDENTITY, DUMMY_CASH_ISSUER_IDENTITY, DUMMY_NOTARY_IDENTITY)), @@ -774,8 +778,9 @@ class CashTests { // Double spend. @Test fun chainCashDoubleSpendFailsWith() { - val mockService = MockServices(listOf("net.corda.finance.contracts.asset"), MEGA_CORP.name, MEGA_CORP_KEY) - + val mockService = MockServices(listOf("net.corda.finance.contracts.asset"), rigorousMock().also { + doReturn(MEGA_CORP).whenever(it).partyFromKey(MEGA_CORP_PUBKEY) + }, MEGA_CORP.name, MEGA_CORP_KEY) ledger(mockService) { unverifiedTransaction { attachment(Cash.PROGRAM_ID) diff --git a/finance/src/test/kotlin/net/corda/finance/contracts/asset/ObligationTests.kt b/finance/src/test/kotlin/net/corda/finance/contracts/asset/ObligationTests.kt index 22e7db887f..207955b0dd 100644 --- a/finance/src/test/kotlin/net/corda/finance/contracts/asset/ObligationTests.kt +++ b/finance/src/test/kotlin/net/corda/finance/contracts/asset/ObligationTests.kt @@ -1,5 +1,7 @@ package net.corda.finance.contracts.asset +import com.nhaarman.mockito_kotlin.doReturn +import com.nhaarman.mockito_kotlin.whenever import net.corda.core.contracts.* import net.corda.core.crypto.NullKeys.NULL_PARTY import net.corda.core.crypto.SecureHash @@ -15,6 +17,7 @@ import net.corda.finance.* import net.corda.finance.contracts.Commodity import net.corda.finance.contracts.NetType import net.corda.finance.contracts.asset.Obligation.Lifecycle +import net.corda.node.services.api.IdentityServiceInternal import net.corda.testing.* import net.corda.testing.contracts.DummyContract import net.corda.testing.contracts.DummyState @@ -51,9 +54,13 @@ class ObligationTests { beneficiary = CHARLIE ) private val outState = inState.copy(beneficiary = AnonymousParty(BOB_PUBKEY)) - private val miniCorpServices = MockServices(listOf("net.corda.finance.contracts.asset"), MINI_CORP.name, MINI_CORP_KEY) - private val notaryServices = MockServices(DUMMY_NOTARY_KEY) - private val mockService = MockServices(listOf("net.corda.finance.contracts.asset")) + private val miniCorpServices = MockServices(listOf("net.corda.finance.contracts.asset"), rigorousMock(), MINI_CORP.name, MINI_CORP_KEY) + private val notaryServices = MockServices(rigorousMock(), MEGA_CORP.name, DUMMY_NOTARY_KEY) + private val mockService = MockServices(listOf("net.corda.finance.contracts.asset"), rigorousMock().also { + doReturn(null).whenever(it).partyFromKey(ALICE_PUBKEY) + doReturn(null).whenever(it).partyFromKey(BOB_PUBKEY) + doReturn(MEGA_CORP).whenever(it).partyFromKey(MEGA_CORP_PUBKEY) + }, MEGA_CORP.name) private fun cashObligationTestRoots( group: LedgerDSL diff --git a/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/ContractAttachmentSerializerTest.kt b/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/ContractAttachmentSerializerTest.kt index 0f9d2dd116..c542330ebd 100644 --- a/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/ContractAttachmentSerializerTest.kt +++ b/node-api/src/test/kotlin/net/corda/nodeapi/internal/serialization/ContractAttachmentSerializerTest.kt @@ -2,7 +2,7 @@ package net.corda.nodeapi.internal.serialization import net.corda.core.contracts.ContractAttachment import net.corda.core.serialization.* -import net.corda.testing.SerializationEnvironmentRule +import net.corda.testing.* import net.corda.testing.contracts.DummyContract import net.corda.testing.node.MockServices import org.assertj.core.api.Assertions.assertThat @@ -22,9 +22,7 @@ class ContractAttachmentSerializerTest { private lateinit var factory: SerializationFactory private lateinit var context: SerializationContext private lateinit var contextWithToken: SerializationContext - - private val mockServices = MockServices() - + private val mockServices = MockServices(rigorousMock(), MEGA_CORP.name) @Before fun setup() { factory = testSerialization.env.serializationFactory diff --git a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt index 9aca690993..a20123f6b5 100644 --- a/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt +++ b/node/src/main/kotlin/net/corda/node/internal/AbstractNode.kt @@ -46,6 +46,7 @@ import net.corda.node.services.config.NotaryConfig import net.corda.node.services.config.configureWithDevSSLCertificate import net.corda.node.services.events.NodeSchedulerService import net.corda.node.services.events.ScheduledActivityObserver +import net.corda.node.services.api.IdentityServiceInternal import net.corda.node.services.identity.PersistentIdentityService import net.corda.node.services.keys.PersistentKeyManagementService import net.corda.node.services.messaging.MessagingService @@ -497,7 +498,7 @@ abstract class AbstractNode(val configuration: NodeConfiguration, * Builds node internal, advertised, and plugin services. * Returns a list of tokenizable services to be added to the serialisation context. */ - private fun makeServices(keyPairs: Set, schemaService: SchemaService, transactionStorage: WritableTransactionStorage, database: CordaPersistence, info: NodeInfo, identityService: IdentityService): MutableList { + private fun makeServices(keyPairs: Set, schemaService: SchemaService, transactionStorage: WritableTransactionStorage, database: CordaPersistence, info: NodeInfo, identityService: IdentityServiceInternal): MutableList { checkpointStorage = DBCheckpointStorage() val metrics = MetricRegistry() attachments = NodeAttachmentService(metrics) @@ -591,7 +592,7 @@ abstract class AbstractNode(val configuration: NodeConfiguration, } } - protected open fun makeKeyManagementService(identityService: IdentityService, keyPairs: Set): KeyManagementService { + protected open fun makeKeyManagementService(identityService: IdentityServiceInternal, keyPairs: Set): KeyManagementService { return PersistentKeyManagementService(identityService, keyPairs) } diff --git a/node/src/main/kotlin/net/corda/node/services/api/IdentityServiceInternal.kt b/node/src/main/kotlin/net/corda/node/services/api/IdentityServiceInternal.kt new file mode 100644 index 0000000000..5694a68940 --- /dev/null +++ b/node/src/main/kotlin/net/corda/node/services/api/IdentityServiceInternal.kt @@ -0,0 +1,11 @@ +package net.corda.node.services.api + +import net.corda.core.identity.PartyAndCertificate +import net.corda.core.node.services.IdentityService + +interface IdentityServiceInternal : IdentityService { + /** This method exists so it can be mocked with doNothing, rather than having to make up a possibly invalid return value. */ + fun justVerifyAndRegisterIdentity(identity: PartyAndCertificate) { + verifyAndRegisterIdentity(identity) + } +} diff --git a/node/src/main/kotlin/net/corda/node/services/identity/InMemoryIdentityService.kt b/node/src/main/kotlin/net/corda/node/services/identity/InMemoryIdentityService.kt index 97e1c03adc..81cb4f75fb 100644 --- a/node/src/main/kotlin/net/corda/node/services/identity/InMemoryIdentityService.kt +++ b/node/src/main/kotlin/net/corda/node/services/identity/InMemoryIdentityService.kt @@ -5,11 +5,11 @@ import net.corda.core.crypto.toStringShort import net.corda.core.identity.* import net.corda.core.internal.cert import net.corda.core.internal.toX509CertHolder -import net.corda.core.node.services.IdentityService import net.corda.core.node.services.UnknownAnonymousPartyException import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.utilities.contextLogger import net.corda.core.utilities.trace +import net.corda.node.services.api.IdentityServiceInternal import net.corda.nodeapi.internal.crypto.X509CertificateFactory import org.bouncycastle.cert.X509CertificateHolder import java.security.InvalidAlgorithmParameterException @@ -25,7 +25,7 @@ import javax.annotation.concurrent.ThreadSafe */ @ThreadSafe class InMemoryIdentityService(identities: Iterable, - trustRoot: X509CertificateHolder) : SingletonSerializeAsToken(), IdentityService { + trustRoot: X509CertificateHolder) : SingletonSerializeAsToken(), IdentityServiceInternal { companion object { private val log = contextLogger() } diff --git a/node/src/main/kotlin/net/corda/node/services/identity/PersistentIdentityService.kt b/node/src/main/kotlin/net/corda/node/services/identity/PersistentIdentityService.kt index da2415aad0..66a5ea4024 100644 --- a/node/src/main/kotlin/net/corda/node/services/identity/PersistentIdentityService.kt +++ b/node/src/main/kotlin/net/corda/node/services/identity/PersistentIdentityService.kt @@ -6,12 +6,12 @@ import net.corda.core.crypto.toStringShort import net.corda.core.identity.* import net.corda.core.internal.cert import net.corda.core.internal.toX509CertHolder -import net.corda.core.node.services.IdentityService import net.corda.core.node.services.UnknownAnonymousPartyException import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.utilities.MAX_HASH_HEX_SIZE import net.corda.core.utilities.contextLogger import net.corda.core.utilities.debug +import net.corda.node.services.api.IdentityServiceInternal import net.corda.node.utilities.AppendOnlyPersistentMap import net.corda.nodeapi.internal.persistence.NODE_DATABASE_PREFIX import net.corda.nodeapi.internal.crypto.X509CertificateFactory @@ -27,7 +27,7 @@ import javax.persistence.Lob @ThreadSafe class PersistentIdentityService(override val trustRoot: X509Certificate, - vararg caCertificates: X509Certificate) : SingletonSerializeAsToken(), IdentityService { + vararg caCertificates: X509Certificate) : SingletonSerializeAsToken(), IdentityServiceInternal { constructor(trustRoot: X509CertificateHolder) : this(trustRoot.cert) companion object { diff --git a/node/src/main/kotlin/net/corda/node/services/keys/E2ETestKeyManagementService.kt b/node/src/main/kotlin/net/corda/node/services/keys/E2ETestKeyManagementService.kt index 0c0eb61778..34b1c94e46 100644 --- a/node/src/main/kotlin/net/corda/node/services/keys/E2ETestKeyManagementService.kt +++ b/node/src/main/kotlin/net/corda/node/services/keys/E2ETestKeyManagementService.kt @@ -3,9 +3,9 @@ package net.corda.node.services.keys import net.corda.core.crypto.* import net.corda.core.identity.PartyAndCertificate import net.corda.core.internal.ThreadBox -import net.corda.core.node.services.IdentityService import net.corda.core.node.services.KeyManagementService import net.corda.core.serialization.SingletonSerializeAsToken +import net.corda.node.services.api.IdentityServiceInternal import org.bouncycastle.operator.ContentSigner import java.security.KeyPair import java.security.PrivateKey @@ -25,7 +25,7 @@ import javax.annotation.concurrent.ThreadSafe * etc. */ @ThreadSafe -class E2ETestKeyManagementService(val identityService: IdentityService, +class E2ETestKeyManagementService(val identityService: IdentityServiceInternal, initialKeys: Set) : SingletonSerializeAsToken(), KeyManagementService { private class InnerState { val keys = HashMap() diff --git a/node/src/main/kotlin/net/corda/node/services/keys/KMSUtils.kt b/node/src/main/kotlin/net/corda/node/services/keys/KMSUtils.kt index 6f520f37f1..694b2128c1 100644 --- a/node/src/main/kotlin/net/corda/node/services/keys/KMSUtils.kt +++ b/node/src/main/kotlin/net/corda/node/services/keys/KMSUtils.kt @@ -4,8 +4,8 @@ import net.corda.core.crypto.Crypto import net.corda.core.identity.PartyAndCertificate import net.corda.core.internal.cert import net.corda.core.internal.toX509CertHolder -import net.corda.core.node.services.IdentityService import net.corda.core.utilities.days +import net.corda.node.services.api.IdentityServiceInternal import net.corda.nodeapi.internal.crypto.CertificateType import net.corda.nodeapi.internal.crypto.ContentSignerBuilder import net.corda.nodeapi.internal.crypto.X509CertificateFactory @@ -28,7 +28,7 @@ import java.time.Duration * @param revocationEnabled whether to check revocation status of certificates in the certificate path. * @return X.509 certificate and path to the trust root. */ -fun freshCertificate(identityService: IdentityService, +fun freshCertificate(identityService: IdentityServiceInternal, subjectPublicKey: PublicKey, issuer: PartyAndCertificate, issuerSigner: ContentSigner, @@ -39,7 +39,7 @@ fun freshCertificate(identityService: IdentityService, issuerSigner, issuer.name, subjectPublicKey, window) val ourCertPath = X509CertificateFactory().delegate.generateCertPath(listOf(ourCertificate.cert) + issuer.certPath.certificates) val anonymisedIdentity = PartyAndCertificate(ourCertPath) - identityService.verifyAndRegisterIdentity(anonymisedIdentity) + identityService.justVerifyAndRegisterIdentity(anonymisedIdentity) return anonymisedIdentity } diff --git a/node/src/main/kotlin/net/corda/node/services/keys/PersistentKeyManagementService.kt b/node/src/main/kotlin/net/corda/node/services/keys/PersistentKeyManagementService.kt index 96f41e9670..c72a20fcd8 100644 --- a/node/src/main/kotlin/net/corda/node/services/keys/PersistentKeyManagementService.kt +++ b/node/src/main/kotlin/net/corda/node/services/keys/PersistentKeyManagementService.kt @@ -2,10 +2,10 @@ package net.corda.node.services.keys import net.corda.core.crypto.* import net.corda.core.identity.PartyAndCertificate -import net.corda.core.node.services.IdentityService import net.corda.core.node.services.KeyManagementService import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.utilities.MAX_HASH_HEX_SIZE +import net.corda.node.services.api.IdentityServiceInternal import net.corda.node.utilities.AppendOnlyPersistentMap import net.corda.nodeapi.internal.persistence.NODE_DATABASE_PREFIX import org.bouncycastle.operator.ContentSigner @@ -24,7 +24,7 @@ import javax.persistence.Lob * * This class needs database transactions to be in-flight during method calls and init. */ -class PersistentKeyManagementService(val identityService: IdentityService, +class PersistentKeyManagementService(val identityService: IdentityServiceInternal, initialKeys: Set) : SingletonSerializeAsToken(), KeyManagementService { @Entity diff --git a/node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java b/node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java index 838370c38c..48aa58a07f 100644 --- a/node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java +++ b/node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java @@ -4,9 +4,9 @@ import com.google.common.collect.ImmutableSet; import kotlin.Pair; import kotlin.Triple; import net.corda.core.contracts.*; +import net.corda.core.crypto.CryptoUtils; import net.corda.core.identity.AbstractParty; import net.corda.core.messaging.DataFeed; -import net.corda.core.node.services.IdentityService; import net.corda.core.node.services.Vault; import net.corda.core.node.services.VaultQueryException; import net.corda.core.node.services.VaultService; @@ -14,11 +14,11 @@ import net.corda.core.node.services.vault.*; import net.corda.core.node.services.vault.QueryCriteria.LinearStateQueryCriteria; import net.corda.core.node.services.vault.QueryCriteria.VaultCustomQueryCriteria; import net.corda.core.node.services.vault.QueryCriteria.VaultQueryCriteria; -import net.corda.core.utilities.EncodingUtils; import net.corda.core.utilities.OpaqueBytes; import net.corda.finance.contracts.DealState; import net.corda.finance.contracts.asset.Cash; import net.corda.finance.schemas.CashSchemaV1; +import net.corda.node.services.api.IdentityServiceInternal; import net.corda.nodeapi.internal.persistence.CordaPersistence; import net.corda.nodeapi.internal.persistence.DatabaseTransaction; import net.corda.testing.SerializationEnvironmentRule; @@ -61,13 +61,13 @@ public class VaultQueryJavaTests { @Before public void setUp() throws CertificateException, InvalidAlgorithmParameterException { List cordappPackages = Arrays.asList("net.corda.testing.contracts", "net.corda.finance.contracts.asset", CashSchemaV1.class.getPackage().getName()); - IdentityService identitySvc = makeTestIdentityService(Arrays.asList(getMEGA_CORP_IDENTITY(), getDUMMY_CASH_ISSUER_IDENTITY(), getDUMMY_NOTARY_IDENTITY())); + IdentityServiceInternal identitySvc = makeTestIdentityService(Arrays.asList(getMEGA_CORP_IDENTITY(), getDUMMY_CASH_ISSUER_IDENTITY(), getDUMMY_NOTARY_IDENTITY())); Pair databaseAndServices = makeTestDatabaseAndMockServices( Arrays.asList(getMEGA_CORP_KEY(), getDUMMY_NOTARY_KEY()), identitySvc, cordappPackages, getMEGA_CORP().getName()); - issuerServices = new MockServices(cordappPackages, getDUMMY_CASH_ISSUER_NAME(), getDUMMY_CASH_ISSUER_KEY(), getBOC_KEY()); + issuerServices = new MockServices(cordappPackages, rigorousMock(IdentityServiceInternal.class), getDUMMY_CASH_ISSUER_NAME(), getDUMMY_CASH_ISSUER_KEY(), getBOC_KEY()); database = databaseAndServices.getFirst(); MockServices services = databaseAndServices.getSecond(); vaultFiller = new VaultFiller(services, getDUMMY_NOTARY(), getDUMMY_NOTARY_KEY()); @@ -449,30 +449,28 @@ public class VaultQueryJavaTests { // DOCSTART VaultJavaQueryExample23 Field pennies = CashSchemaV1.PersistentCashState.class.getDeclaredField("pennies"); Field currency = CashSchemaV1.PersistentCashState.class.getDeclaredField("currency"); - Field issuerParty = CashSchemaV1.PersistentCashState.class.getDeclaredField("issuerParty"); - - QueryCriteria sumCriteria = new VaultCustomQueryCriteria(Builder.sum(pennies, Arrays.asList(issuerParty, currency), Sort.Direction.DESC)); - + Field issuerPartyHash = CashSchemaV1.PersistentCashState.class.getDeclaredField("issuerPartyHash"); + QueryCriteria sumCriteria = new VaultCustomQueryCriteria(Builder.sum(pennies, Arrays.asList(issuerPartyHash, currency), Sort.Direction.DESC)); Vault.Page results = vaultService.queryBy(Cash.State.class, sumCriteria); // DOCEND VaultJavaQueryExample23 assertThat(results.getOtherResults()).hasSize(12); assertThat(results.getOtherResults().get(0)).isEqualTo(400L); - assertThat(results.getOtherResults().get(1)).isEqualTo(EncodingUtils.toBase58String(getBOC_PUBKEY())); + assertThat(results.getOtherResults().get(1)).isEqualTo(CryptoUtils.toStringShort(getBOC_PUBKEY())); assertThat(results.getOtherResults().get(2)).isEqualTo("GBP"); assertThat(results.getOtherResults().get(3)).isEqualTo(300L); - assertThat(results.getOtherResults().get(4)).isEqualTo(EncodingUtils.toBase58String(getDUMMY_CASH_ISSUER().getParty().getOwningKey())); + assertThat(results.getOtherResults().get(4)).isEqualTo(CryptoUtils.toStringShort(getDUMMY_CASH_ISSUER().getParty().getOwningKey())); assertThat(results.getOtherResults().get(5)).isEqualTo("GBP"); assertThat(results.getOtherResults().get(6)).isEqualTo(200L); - assertThat(results.getOtherResults().get(7)).isEqualTo(EncodingUtils.toBase58String(getBOC_PUBKEY())); + assertThat(results.getOtherResults().get(7)).isEqualTo(CryptoUtils.toStringShort(getBOC_PUBKEY())); assertThat(results.getOtherResults().get(8)).isEqualTo("USD"); assertThat(results.getOtherResults().get(9)).isEqualTo(100L); - assertThat(results.getOtherResults().get(10)).isEqualTo(EncodingUtils.toBase58String(getDUMMY_CASH_ISSUER().getParty().getOwningKey())); + assertThat(results.getOtherResults().get(10)).isEqualTo(CryptoUtils.toStringShort(getDUMMY_CASH_ISSUER().getParty().getOwningKey())); assertThat(results.getOtherResults().get(11)).isEqualTo("USD"); } catch (NoSuchFieldException e) { - e.printStackTrace(); + throw new RuntimeException(e); } return tx; }); diff --git a/node/src/test/kotlin/net/corda/node/messaging/TwoPartyTradeFlowTests.kt b/node/src/test/kotlin/net/corda/node/messaging/TwoPartyTradeFlowTests.kt index 145a7b933b..0daa972cc2 100644 --- a/node/src/test/kotlin/net/corda/node/messaging/TwoPartyTradeFlowTests.kt +++ b/node/src/test/kotlin/net/corda/node/messaging/TwoPartyTradeFlowTests.kt @@ -1,6 +1,8 @@ package net.corda.node.messaging import co.paralleluniverse.fibers.Suspendable +import com.nhaarman.mockito_kotlin.doReturn +import com.nhaarman.mockito_kotlin.whenever import net.corda.core.concurrent.CordaFuture import net.corda.core.contracts.* import net.corda.core.crypto.* @@ -34,6 +36,7 @@ import net.corda.finance.flows.TwoPartyTradeFlow.Buyer import net.corda.finance.flows.TwoPartyTradeFlow.Seller import net.corda.node.internal.StartedNode import net.corda.node.services.api.WritableTransactionStorage +import net.corda.node.services.api.IdentityServiceInternal import net.corda.node.services.persistence.DBTransactionStorage import net.corda.node.services.persistence.checkpoints import net.corda.nodeapi.internal.persistence.CordaPersistence @@ -90,13 +93,15 @@ class TwoPartyTradeFlowTests(private val anonymous: Boolean) { // we run in the unit test thread exclusively to speed things up, ensure deterministic results and // allow interruption half way through. mockNet = MockNetwork(threadPerNode = true, cordappPackages = cordappPackages) - ledger(MockServices(cordappPackages)) { + val ledgerIdentityService = rigorousMock() + ledger(MockServices(cordappPackages, ledgerIdentityService, MEGA_CORP.name)) { val notaryNode = mockNet.defaultNotaryNode val aliceNode = mockNet.createPartyNode(ALICE_NAME) val bobNode = mockNet.createPartyNode(BOB_NAME) val bankNode = mockNet.createPartyNode(BOC_NAME) val alice = aliceNode.info.singleIdentity() val bank = bankNode.info.singleIdentity() + doReturn(null).whenever(ledgerIdentityService).partyFromKey(bank.owningKey) val bob = bobNode.info.singleIdentity() val notary = mockNet.defaultNotaryIdentity val cashIssuer = bank.ref(1) @@ -140,13 +145,15 @@ class TwoPartyTradeFlowTests(private val anonymous: Boolean) { @Test(expected = InsufficientBalanceException::class) fun `trade cash for commercial paper fails using soft locking`() { mockNet = MockNetwork(threadPerNode = true, cordappPackages = cordappPackages) - ledger(MockServices(cordappPackages)) { + val ledgerIdentityService = rigorousMock() + ledger(MockServices(cordappPackages, ledgerIdentityService, MEGA_CORP.name)) { val notaryNode = mockNet.defaultNotaryNode val aliceNode = mockNet.createPartyNode(ALICE_NAME) val bobNode = mockNet.createPartyNode(BOB_NAME) val bankNode = mockNet.createPartyNode(BOC_NAME) val alice = aliceNode.info.singleIdentity() val bank = bankNode.info.singleIdentity() + doReturn(null).whenever(ledgerIdentityService).partyFromKey(bank.owningKey) val bob = bobNode.info.singleIdentity() val issuer = bank.ref(1) val notary = mockNet.defaultNotaryIdentity @@ -196,7 +203,8 @@ class TwoPartyTradeFlowTests(private val anonymous: Boolean) { @Test fun `shutdown and restore`() { mockNet = MockNetwork(cordappPackages = cordappPackages) - ledger(MockServices(cordappPackages)) { + val ledgerIdentityService = rigorousMock() + ledger(MockServices(cordappPackages, ledgerIdentityService, MEGA_CORP.name)) { val notaryNode = mockNet.defaultNotaryNode val aliceNode = mockNet.createPartyNode(ALICE_NAME) var bobNode = mockNet.createPartyNode(BOB_NAME) @@ -210,6 +218,7 @@ class TwoPartyTradeFlowTests(private val anonymous: Boolean) { val notary = mockNet.defaultNotaryIdentity val alice = aliceNode.info.singleIdentity() val bank = bankNode.info.singleIdentity() + doReturn(null).whenever(ledgerIdentityService).partyFromKey(bank.owningKey) val bob = bobNode.info.singleIdentity() val issuer = bank.ref(1, 2, 3) @@ -491,16 +500,18 @@ class TwoPartyTradeFlowTests(private val anonymous: Boolean) { @Test fun `dependency with error on buyer side`() { mockNet = MockNetwork(cordappPackages = cordappPackages) - ledger(MockServices(cordappPackages)) { - runWithError(true, false, "at least one cash input") + val ledgerIdentityService = rigorousMock() + ledger(MockServices(cordappPackages, ledgerIdentityService, MEGA_CORP.name)) { + runWithError(ledgerIdentityService, true, false, "at least one cash input") } } @Test fun `dependency with error on seller side`() { mockNet = MockNetwork(cordappPackages = cordappPackages) - ledger(MockServices(cordappPackages)) { - runWithError(false, true, "Issuances have a time-window") + val ledgerIdentityService = rigorousMock() + ledger(MockServices(cordappPackages, ledgerIdentityService, MEGA_CORP.name)) { + runWithError(ledgerIdentityService, false, true, "Issuances have a time-window") } } @@ -562,6 +573,7 @@ class TwoPartyTradeFlowTests(private val anonymous: Boolean) { data class TestTx(val notaryIdentity: Party, val price: Amount, val anonymous: Boolean) private fun LedgerDSL.runWithError( + ledgerIdentityService: IdentityServiceInternal, bobError: Boolean, aliceError: Boolean, expectedMessageSubstring: String @@ -575,6 +587,7 @@ class TwoPartyTradeFlowTests(private val anonymous: Boolean) { val alice = aliceNode.info.singleIdentity() val bob = bobNode.info.singleIdentity() val bank = bankNode.info.singleIdentity() + doReturn(null).whenever(ledgerIdentityService).partyFromKey(bank.owningKey) val issuer = bank.ref(1, 2, 3) val bobsBadCash = bobNode.database.transaction { diff --git a/node/src/test/kotlin/net/corda/node/services/persistence/HibernateConfigurationTest.kt b/node/src/test/kotlin/net/corda/node/services/persistence/HibernateConfigurationTest.kt index 3d627eaf24..7c36962968 100644 --- a/node/src/test/kotlin/net/corda/node/services/persistence/HibernateConfigurationTest.kt +++ b/node/src/test/kotlin/net/corda/node/services/persistence/HibernateConfigurationTest.kt @@ -1,8 +1,6 @@ package net.corda.node.services.persistence -import com.nhaarman.mockito_kotlin.any -import com.nhaarman.mockito_kotlin.doReturn -import com.nhaarman.mockito_kotlin.whenever +import com.nhaarman.mockito_kotlin.* import net.corda.core.contracts.Amount import net.corda.core.contracts.StateAndRef import net.corda.core.contracts.StateRef @@ -35,6 +33,7 @@ import net.corda.node.services.schema.HibernateObserver import net.corda.node.services.schema.NodeSchemaService import net.corda.node.services.vault.VaultSchemaV1 import net.corda.node.internal.configureDatabase +import net.corda.node.services.api.IdentityServiceInternal import net.corda.nodeapi.internal.persistence.CordaPersistence import net.corda.nodeapi.internal.persistence.DatabaseConfig import net.corda.nodeapi.internal.persistence.HibernateConfiguration @@ -85,9 +84,9 @@ class HibernateConfigurationTest { @Before fun setUp() { val cordappPackages = listOf("net.corda.testing.contracts", "net.corda.finance.contracts.asset") - bankServices = MockServices(cordappPackages, BOC.name, BOC_KEY) - issuerServices = MockServices(cordappPackages, DUMMY_CASH_ISSUER_NAME, DUMMY_CASH_ISSUER_KEY) - notaryServices = MockServices(cordappPackages, DUMMY_NOTARY.name, DUMMY_NOTARY_KEY) + bankServices = MockServices(cordappPackages, rigorousMock(), BOC.name, BOC_KEY) + issuerServices = MockServices(cordappPackages, rigorousMock(), DUMMY_CASH_ISSUER_NAME, DUMMY_CASH_ISSUER_KEY) + notaryServices = MockServices(cordappPackages, rigorousMock(), DUMMY_NOTARY.name, DUMMY_NOTARY_KEY) notary = notaryServices.myInfo.singleIdentity() val dataSourceProps = makeTestDataSourceProperties() val identityService = rigorousMock().also { mock -> @@ -102,7 +101,9 @@ class HibernateConfigurationTest { database.transaction { hibernateConfig = database.hibernateConfig // `consumeCash` expects we can self-notarise transactions - services = object : MockServices(cordappPackages, BOB_NAME, generateKeyPair(), DUMMY_NOTARY_KEY) { + services = object : MockServices(cordappPackages, rigorousMock().also { + doNothing().whenever(it).justVerifyAndRegisterIdentity(argThat { name == BOB_NAME }) + }, BOB_NAME, generateKeyPair(), DUMMY_NOTARY_KEY) { override val vaultService = makeVaultService(database.hibernateConfig, schemaService) override fun recordTransactions(statesToRecord: StatesToRecord, txs: Iterable) { for (stx in txs) { diff --git a/node/src/test/kotlin/net/corda/node/services/vault/NodeVaultServiceTest.kt b/node/src/test/kotlin/net/corda/node/services/vault/NodeVaultServiceTest.kt index 94fae003e2..7bab864399 100644 --- a/node/src/test/kotlin/net/corda/node/services/vault/NodeVaultServiceTest.kt +++ b/node/src/test/kotlin/net/corda/node/services/vault/NodeVaultServiceTest.kt @@ -1,6 +1,9 @@ package net.corda.node.services.vault import co.paralleluniverse.fibers.Suspendable +import com.nhaarman.mockito_kotlin.argThat +import com.nhaarman.mockito_kotlin.doNothing +import com.nhaarman.mockito_kotlin.whenever import net.corda.core.contracts.Amount import net.corda.core.contracts.Issued import net.corda.core.contracts.StateAndRef @@ -34,6 +37,7 @@ import net.corda.finance.contracts.asset.DUMMY_CASH_ISSUER_NAME import net.corda.finance.contracts.getCashBalance import net.corda.finance.schemas.CashSchemaV1 import net.corda.finance.utils.sumCash +import net.corda.node.services.api.IdentityServiceInternal import net.corda.nodeapi.internal.persistence.CordaPersistence import net.corda.testing.* import net.corda.testing.contracts.VaultFiller @@ -83,9 +87,8 @@ class NodeVaultServiceTest { vaultFiller = VaultFiller(services, DUMMY_NOTARY, DUMMY_NOTARY_KEY) // This is safe because MockServices only ever have a single identity identity = services.myInfo.singleIdentityAndCert() - issuerServices = MockServices(cordappPackages, DUMMY_CASH_ISSUER_NAME, DUMMY_CASH_ISSUER_KEY) - bocServices = MockServices(cordappPackages, BOC_NAME, BOC_KEY) - + issuerServices = MockServices(cordappPackages, rigorousMock(), DUMMY_CASH_ISSUER_NAME, DUMMY_CASH_ISSUER_KEY) + bocServices = MockServices(cordappPackages, rigorousMock(), BOC_NAME, BOC_KEY) services.identityService.verifyAndRegisterIdentity(DUMMY_CASH_ISSUER_IDENTITY) services.identityService.verifyAndRegisterIdentity(BOC_IDENTITY) } @@ -125,7 +128,7 @@ class NodeVaultServiceTest { assertThat(w1).hasSize(3) val originalVault = vaultService - val services2 = object : MockServices() { + val services2 = object : MockServices(rigorousMock(), MEGA_CORP.name) { override val vaultService: NodeVaultService get() = originalVault override fun recordTransactions(statesToRecord: StatesToRecord, txs: Iterable) { for (stx in txs) { @@ -468,7 +471,7 @@ class NodeVaultServiceTest { @Test fun addNoteToTransaction() { - val megaCorpServices = MockServices(cordappPackages, MEGA_CORP.name, MEGA_CORP_KEY) + val megaCorpServices = MockServices(cordappPackages, rigorousMock(), MEGA_CORP.name, MEGA_CORP_KEY) database.transaction { val freshKey = identity.owningKey @@ -575,7 +578,9 @@ class NodeVaultServiceTest { val identity = services.myInfo.singleIdentityAndCert() assertEquals(services.identityService.partyFromKey(identity.owningKey), identity.party) val anonymousIdentity = services.keyManagementService.freshKeyAndCert(identity, false) - val thirdPartyServices = MockServices() + val thirdPartyServices = MockServices(rigorousMock().also { + doNothing().whenever(it).justVerifyAndRegisterIdentity(argThat { name == MEGA_CORP.name }) + }, MEGA_CORP.name) val thirdPartyIdentity = thirdPartyServices.keyManagementService.freshKeyAndCert(thirdPartyServices.myInfo.singleIdentityAndCert(), false) val amount = Amount(1000, Issued(BOC.ref(1), GBP)) diff --git a/node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt b/node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt index ac7edb2c53..7f1563880b 100644 --- a/node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt @@ -89,7 +89,7 @@ class VaultQueryTests { services = databaseAndServices.second vaultFiller = VaultFiller(services, DUMMY_NOTARY, DUMMY_NOTARY_KEY) vaultFillerCashNotary = VaultFiller(services, DUMMY_NOTARY, DUMMY_NOTARY_KEY, CASH_NOTARY) - notaryServices = MockServices(cordappPackages, DUMMY_NOTARY.name, DUMMY_NOTARY_KEY, DUMMY_CASH_ISSUER_KEY, BOC_KEY, MEGA_CORP_KEY) + notaryServices = MockServices(cordappPackages, rigorousMock(), DUMMY_NOTARY.name, DUMMY_NOTARY_KEY, DUMMY_CASH_ISSUER_KEY, BOC_KEY, MEGA_CORP_KEY) identitySvc = services.identityService // Register all of the identities we're going to use (notaryServices.myInfo.legalIdentitiesAndCerts + BOC_IDENTITY + CASH_NOTARY_IDENTITY + MINI_CORP_IDENTITY + MEGA_CORP_IDENTITY).forEach { identity -> @@ -1324,15 +1324,15 @@ class VaultQueryTests { fun `unconsumed fungible assets for selected issuer parties`() { // GBP issuer val gbpCashIssuerName = CordaX500Name(organisation = "British Pounds Cash Issuer", locality = "London", country = "GB") - val gbpCashIssuerServices = MockServices(cordappPackages, gbpCashIssuerName, generateKeyPair()) + val gbpCashIssuerServices = MockServices(cordappPackages, rigorousMock(), gbpCashIssuerName, generateKeyPair()) val gbpCashIssuer = gbpCashIssuerServices.myInfo.singleIdentityAndCert() // USD issuer val usdCashIssuerName = CordaX500Name(organisation = "US Dollars Cash Issuer", locality = "New York", country = "US") - val usdCashIssuerServices = MockServices(cordappPackages, usdCashIssuerName, generateKeyPair()) + val usdCashIssuerServices = MockServices(cordappPackages, rigorousMock(), usdCashIssuerName, generateKeyPair()) val usdCashIssuer = usdCashIssuerServices.myInfo.singleIdentityAndCert() // CHF issuer val chfCashIssuerName = CordaX500Name(organisation = "Swiss Francs Cash Issuer", locality = "Zurich", country = "CH") - val chfCashIssuerServices = MockServices(cordappPackages, chfCashIssuerName, generateKeyPair()) + val chfCashIssuerServices = MockServices(cordappPackages, rigorousMock(), chfCashIssuerName, generateKeyPair()) val chfCashIssuer = chfCashIssuerServices.myInfo.singleIdentityAndCert() listOf(gbpCashIssuer, usdCashIssuer, chfCashIssuer).forEach { identity -> services.identityService.verifyAndRegisterIdentity(identity) diff --git a/node/src/test/kotlin/net/corda/node/services/vault/VaultWithCashTest.kt b/node/src/test/kotlin/net/corda/node/services/vault/VaultWithCashTest.kt index 733082b183..451a82d9de 100644 --- a/node/src/test/kotlin/net/corda/node/services/vault/VaultWithCashTest.kt +++ b/node/src/test/kotlin/net/corda/node/services/vault/VaultWithCashTest.kt @@ -69,8 +69,8 @@ class VaultWithCashTest { database = databaseAndServices.first services = databaseAndServices.second vaultFiller = VaultFiller(services, DUMMY_NOTARY, DUMMY_NOTARY_KEY) - issuerServices = MockServices(cordappPackages, DUMMY_CASH_ISSUER_NAME, DUMMY_CASH_ISSUER_KEY, MEGA_CORP_KEY) - notaryServices = MockServices(cordappPackages, DUMMY_NOTARY.name, DUMMY_NOTARY_KEY) + issuerServices = MockServices(cordappPackages, rigorousMock(), DUMMY_CASH_ISSUER_NAME, DUMMY_CASH_ISSUER_KEY, MEGA_CORP_KEY) + notaryServices = MockServices(cordappPackages, rigorousMock(), DUMMY_NOTARY.name, DUMMY_NOTARY_KEY) notary = notaryServices.myInfo.legalIdentitiesAndCerts.single().party } @@ -101,7 +101,7 @@ class VaultWithCashTest { @Test fun `issue and spend total correctly and irrelevant ignored`() { - val megaCorpServices = MockServices(cordappPackages, MEGA_CORP.name, MEGA_CORP_KEY) + val megaCorpServices = MockServices(cordappPackages, rigorousMock(), MEGA_CORP.name, MEGA_CORP_KEY) val freshKey = services.keyManagementService.freshKey() val usefulTX = diff --git a/samples/irs-demo/cordapp/src/test/kotlin/net/corda/irs/api/NodeInterestRatesTest.kt b/samples/irs-demo/cordapp/src/test/kotlin/net/corda/irs/api/NodeInterestRatesTest.kt index 97729cadf0..ad45bbfbc6 100644 --- a/samples/irs-demo/cordapp/src/test/kotlin/net/corda/irs/api/NodeInterestRatesTest.kt +++ b/samples/irs-demo/cordapp/src/test/kotlin/net/corda/irs/api/NodeInterestRatesTest.kt @@ -19,11 +19,8 @@ import net.corda.node.internal.configureDatabase import net.corda.nodeapi.internal.persistence.CordaPersistence import net.corda.nodeapi.internal.persistence.DatabaseConfig import net.corda.testing.* -import net.corda.testing.node.MockNetwork -import net.corda.testing.node.MockNodeParameters -import net.corda.testing.node.MockServices +import net.corda.testing.node.* import net.corda.testing.node.MockServices.Companion.makeTestDataSourceProperties -import net.corda.testing.node.createMockCordaService import org.junit.After import org.junit.Assert.* import org.junit.Before @@ -50,7 +47,7 @@ class NodeInterestRatesTest { private val DUMMY_CASH_ISSUER_KEY = generateKeyPair() private val DUMMY_CASH_ISSUER = Party(CordaX500Name(organisation = "Cash issuer", locality = "London", country = "GB"), DUMMY_CASH_ISSUER_KEY.public) - private val services = MockServices(listOf("net.corda.finance.contracts.asset"), DUMMY_CASH_ISSUER.name, DUMMY_CASH_ISSUER_KEY, MEGA_CORP_KEY) + private val services = MockServices(listOf("net.corda.finance.contracts.asset"), rigorousMock(), DUMMY_CASH_ISSUER.name, DUMMY_CASH_ISSUER_KEY, MEGA_CORP_KEY) // This is safe because MockServices only ever have a single identity private val identity = services.myInfo.singleIdentity() diff --git a/samples/irs-demo/cordapp/src/test/kotlin/net/corda/irs/contract/IRSTests.kt b/samples/irs-demo/cordapp/src/test/kotlin/net/corda/irs/contract/IRSTests.kt index c200bd7c88..4f35dcc18e 100644 --- a/samples/irs-demo/cordapp/src/test/kotlin/net/corda/irs/contract/IRSTests.kt +++ b/samples/irs-demo/cordapp/src/test/kotlin/net/corda/irs/contract/IRSTests.kt @@ -1,5 +1,7 @@ package net.corda.irs.contract +import com.nhaarman.mockito_kotlin.doReturn +import com.nhaarman.mockito_kotlin.whenever import net.corda.core.contracts.Amount import net.corda.core.contracts.UniqueIdentifier import net.corda.core.transactions.SignedTransaction @@ -18,6 +20,7 @@ import net.corda.finance.contracts.FixOf import net.corda.finance.contracts.Frequency import net.corda.finance.contracts.PaymentRule import net.corda.finance.contracts.Tenor +import net.corda.node.services.api.IdentityServiceInternal import net.corda.testing.* import net.corda.testing.node.MockServices import org.junit.Rule @@ -212,10 +215,9 @@ class IRSTests { @Rule @JvmField val testSerialization = SerializationEnvironmentRule() - private val megaCorpServices = MockServices(listOf("net.corda.irs.contract"), MEGA_CORP.name, MEGA_CORP_KEY) - private val miniCorpServices = MockServices(listOf("net.corda.irs.contract"), MINI_CORP.name, MINI_CORP_KEY) - private val notaryServices = MockServices(listOf("net.corda.irs.contract"), DUMMY_NOTARY.name, DUMMY_NOTARY_KEY) - + private val megaCorpServices = MockServices(listOf("net.corda.irs.contract"), rigorousMock(), MEGA_CORP.name, MEGA_CORP_KEY) + private val miniCorpServices = MockServices(listOf("net.corda.irs.contract"), rigorousMock(), MINI_CORP.name, MINI_CORP_KEY) + private val notaryServices = MockServices(listOf("net.corda.irs.contract"), rigorousMock(), DUMMY_NOTARY.name, DUMMY_NOTARY_KEY) @Test fun ok() { trade().verifies() @@ -311,7 +313,11 @@ class IRSTests { */ @Test fun generateIRSandFixSome() { - val services = MockServices(listOf("net.corda.irs.contract")) + val services = MockServices(listOf("net.corda.irs.contract"), rigorousMock().also { + listOf(MEGA_CORP, MINI_CORP).forEach { party -> + doReturn(party).whenever(it).partyFromKey(party.owningKey) + } + }, MEGA_CORP.name) var previousTXN = generateIRSTxn(1) previousTXN.toLedgerTransaction(services).verify() services.recordTransactions(previousTXN) diff --git a/samples/trader-demo/src/test/kotlin/net/corda/traderdemo/TransactionGraphSearchTests.kt b/samples/trader-demo/src/test/kotlin/net/corda/traderdemo/TransactionGraphSearchTests.kt index 0a893c54a6..dea21d50d3 100644 --- a/samples/trader-demo/src/test/kotlin/net/corda/traderdemo/TransactionGraphSearchTests.kt +++ b/samples/trader-demo/src/test/kotlin/net/corda/traderdemo/TransactionGraphSearchTests.kt @@ -36,9 +36,8 @@ class TransactionGraphSearchTests { * @param signer signer for the two transactions and their commands. */ fun buildTransactions(command: CommandData): GraphTransactionStorage { - val megaCorpServices = MockServices(listOf("net.corda.testing.contracts"), MEGA_CORP.name, MEGA_CORP_KEY) - val notaryServices = MockServices(listOf("net.corda.testing.contracts"), DUMMY_NOTARY.name, DUMMY_NOTARY_KEY) - + val megaCorpServices = MockServices(listOf("net.corda.testing.contracts"), rigorousMock(), MEGA_CORP.name, MEGA_CORP_KEY) + val notaryServices = MockServices(listOf("net.corda.testing.contracts"), rigorousMock(), DUMMY_NOTARY.name, DUMMY_NOTARY_KEY) val originBuilder = TransactionBuilder(DUMMY_NOTARY) .addOutputState(DummyState(random31BitValue()), DummyContract.PROGRAM_ID) .addCommand(command, MEGA_CORP_PUBKEY) diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/NodeTestUtils.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/NodeTestUtils.kt index 41b4e360ff..85fa0eef1f 100644 --- a/testing/node-driver/src/main/kotlin/net/corda/testing/NodeTestUtils.kt +++ b/testing/node-driver/src/main/kotlin/net/corda/testing/NodeTestUtils.kt @@ -23,6 +23,7 @@ import net.corda.node.services.config.VerifierType import net.corda.nodeapi.internal.config.User import net.corda.testing.node.MockServices import net.corda.testing.node.MockServices.Companion.makeTestDataSourceProperties +import net.corda.testing.node.makeTestIdentityService import java.nio.file.Path /** @@ -31,7 +32,7 @@ import java.nio.file.Path */ @JvmOverloads fun ledger( - services: ServiceHub = MockServices(), + services: ServiceHub = MockServices(makeTestIdentityService(listOf(MEGA_CORP_IDENTITY, MINI_CORP_IDENTITY, DUMMY_CASH_ISSUER_IDENTITY, DUMMY_NOTARY_IDENTITY)), MEGA_CORP.name), dsl: LedgerDSL.() -> Unit ): LedgerDSL { return LedgerDSL(TestLedgerDSLInterpreter(services)).also { dsl(it) } @@ -47,7 +48,7 @@ fun transaction( transactionBuilder: TransactionBuilder = TransactionBuilder(notary = DUMMY_NOTARY), cordappPackages: List = emptyList(), dsl: TransactionDSL.() -> EnforceVerifyOrFail -) = ledger(services = MockServices(cordappPackages)) { +) = ledger(services = MockServices(cordappPackages, makeTestIdentityService(listOf(MEGA_CORP_IDENTITY, MINI_CORP_IDENTITY, DUMMY_CASH_ISSUER_IDENTITY, DUMMY_NOTARY_IDENTITY)), MEGA_CORP.name)) { dsl(TransactionDSL(TestTransactionDSLInterpreter(this.interpreter, transactionBuilder))) } diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNode.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNode.kt index 2b9a966e3a..aa3776c02c 100644 --- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNode.kt +++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockNode.kt @@ -29,6 +29,7 @@ import net.corda.node.services.api.SchemaService import net.corda.node.services.config.BFTSMaRtConfiguration import net.corda.node.services.config.NodeConfiguration import net.corda.node.services.config.NotaryConfig +import net.corda.node.services.api.IdentityServiceInternal import net.corda.node.services.keys.E2ETestKeyManagementService import net.corda.node.services.messaging.MessagingService import net.corda.node.services.transactions.BFTNonValidatingNotaryService @@ -274,7 +275,7 @@ class MockNetwork(defaultParameters: MockNetworkParameters = MockNetworkParamete network = messagingServiceSpy } - override fun makeKeyManagementService(identityService: IdentityService, keyPairs: Set): KeyManagementService { + override fun makeKeyManagementService(identityService: IdentityServiceInternal, keyPairs: Set): KeyManagementService { return E2ETestKeyManagementService(identityService, keyPairs) } diff --git a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt index 0d119ddf12..f0870f6a32 100644 --- a/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt +++ b/testing/node-driver/src/main/kotlin/net/corda/testing/node/MockServices.kt @@ -27,6 +27,7 @@ import net.corda.node.services.schema.NodeSchemaService import net.corda.node.services.transactions.InMemoryTransactionVerifierService import net.corda.node.services.vault.NodeVaultService import net.corda.node.internal.configureDatabase +import net.corda.node.services.api.IdentityServiceInternal import net.corda.nodeapi.internal.persistence.CordaPersistence import net.corda.nodeapi.internal.persistence.DatabaseConfig import net.corda.nodeapi.internal.persistence.HibernateConfiguration @@ -46,10 +47,11 @@ fun makeTestIdentityService(identities: Iterable = emptySet * A singleton utility that only provides a mock identity, key and storage service. However, this is sufficient for * building chains of transactions and verifying them. It isn't sufficient for testing flows however. */ -open class MockServices( +open class MockServices private constructor( cordappLoader: CordappLoader, override val validatedTransactions: WritableTransactionStorage, - private val initialIdentityName: CordaX500Name = MEGA_CORP.name, + override val identityService: IdentityServiceInternal, + private val initialIdentityName: CordaX500Name, vararg val keys: KeyPair ) : ServiceHub, StateLoader by validatedTransactions { companion object { @@ -75,13 +77,13 @@ open class MockServices( /** * Makes database and mock services appropriate for unit tests. * @param keys a list of [KeyPair] instances to be used by [MockServices]. - * @param identityService an instance of [IdentityService], see [makeTestIdentityService]. + * @param identityService an instance of [IdentityServiceInternal], see [makeTestIdentityService]. * @param initialIdentityName the name of the first (typically sole) identity the services will represent. * @return a pair where the first element is the instance of [CordaPersistence] and the second is [MockServices]. */ @JvmStatic fun makeTestDatabaseAndMockServices(keys: List, - identityService: IdentityService, + identityService: IdentityServiceInternal, cordappPackages: List = emptyList(), initialIdentityName: CordaX500Name): Pair { val cordappLoader = CordappLoader.createWithTestPackages(cordappPackages) @@ -89,8 +91,7 @@ open class MockServices( val schemaService = NodeSchemaService(cordappLoader.cordappSchemas) val database = configureDatabase(dataSourceProps, DatabaseConfig(), identityService, schemaService) val mockService = database.transaction { - object : MockServices(cordappLoader, initialIdentityName = initialIdentityName, keys = *(keys.toTypedArray())) { - override val identityService get() = identityService + object : MockServices(cordappLoader, identityService, initialIdentityName, *(keys.toTypedArray())) { override val vaultService: VaultServiceInternal = makeVaultService(database.hibernateConfig, schemaService) override fun recordTransactions(statesToRecord: StatesToRecord, txs: Iterable) { @@ -106,10 +107,10 @@ open class MockServices( } } - constructor(cordappLoader: CordappLoader, initialIdentityName: CordaX500Name = MEGA_CORP.name, vararg keys: KeyPair) : this(cordappLoader, MockTransactionStorage(), initialIdentityName = initialIdentityName, keys = *keys) - constructor(cordappPackages: List, initialIdentityName: CordaX500Name = MEGA_CORP.name, vararg keys: KeyPair) : this(CordappLoader.createWithTestPackages(cordappPackages), initialIdentityName = initialIdentityName, keys = *keys) - constructor(vararg keys: KeyPair) : this(emptyList(), MEGA_CORP.name, *keys) - constructor() : this(generateKeyPair()) + private constructor(cordappLoader: CordappLoader, identityService: IdentityServiceInternal, initialIdentityName: CordaX500Name, vararg keys: KeyPair) : this(cordappLoader, MockTransactionStorage(), identityService, initialIdentityName, *keys) + constructor(cordappPackages: List, identityService: IdentityServiceInternal, initialIdentityName: CordaX500Name, vararg keys: KeyPair) : this(CordappLoader.createWithTestPackages(cordappPackages), identityService, initialIdentityName, *keys) + constructor(identityService: IdentityServiceInternal, initialIdentityName: CordaX500Name, vararg keys: KeyPair) : this(emptyList(), identityService, initialIdentityName, *keys) + constructor(identityService: IdentityServiceInternal, initialIdentityName: CordaX500Name) : this(identityService, initialIdentityName, generateKeyPair()) val key: KeyPair get() = keys.first() @@ -120,7 +121,6 @@ open class MockServices( } final override val attachments = MockAttachmentStorage() - override val identityService: IdentityService = makeTestIdentityService(listOf(MEGA_CORP_IDENTITY, MINI_CORP_IDENTITY, DUMMY_CASH_ISSUER_IDENTITY, DUMMY_NOTARY_IDENTITY)) override val keyManagementService: KeyManagementService by lazy { MockKeyManagementService(identityService, *keys) } override val vaultService: VaultService get() = throw UnsupportedOperationException() @@ -152,7 +152,7 @@ open class MockServices( override fun jdbcSession(): Connection = throw UnsupportedOperationException() } -class MockKeyManagementService(val identityService: IdentityService, +class MockKeyManagementService(val identityService: IdentityServiceInternal, vararg initialKeys: KeyPair) : SingletonSerializeAsToken(), KeyManagementService { private val keyStore: MutableMap = initialKeys.associateByTo(HashMap(), { it.public }, { it.private }) diff --git a/tools/loadtest/src/main/kotlin/net/corda/loadtest/tests/NotaryTest.kt b/tools/loadtest/src/main/kotlin/net/corda/loadtest/tests/NotaryTest.kt index b96f6f4f40..5c8900ebb3 100644 --- a/tools/loadtest/src/main/kotlin/net/corda/loadtest/tests/NotaryTest.kt +++ b/tools/loadtest/src/main/kotlin/net/corda/loadtest/tests/NotaryTest.kt @@ -10,8 +10,10 @@ import net.corda.finance.contracts.asset.DUMMY_CASH_ISSUER import net.corda.finance.contracts.asset.DUMMY_CASH_ISSUER_KEY import net.corda.loadtest.LoadTest import net.corda.loadtest.NodeConnection +import net.corda.testing.* import net.corda.testing.contracts.DummyContract import net.corda.testing.node.MockServices +import net.corda.testing.node.makeTestIdentityService import org.slf4j.LoggerFactory private val log = LoggerFactory.getLogger("NotaryTest") @@ -21,7 +23,7 @@ data class NotariseCommand(val issueTx: SignedTransaction, val moveTx: SignedTra val dummyNotarisationTest = LoadTest( "Notarising dummy transactions", generate = { _, _ -> - val issuerServices = MockServices(DUMMY_CASH_ISSUER_KEY) + val issuerServices = MockServices(makeTestIdentityService(listOf(MEGA_CORP_IDENTITY, MINI_CORP_IDENTITY, DUMMY_CASH_ISSUER_IDENTITY, DUMMY_NOTARY_IDENTITY)), MEGA_CORP.name, DUMMY_CASH_ISSUER_KEY) val generateTx = Generator.pickOne(simpleNodes).flatMap { node -> Generator.int().map { val issueBuilder = DummyContract.generateInitial(it, notary.info.legalIdentities[1], DUMMY_CASH_ISSUER) // TODO notary choice