CORDA-3569 - Add RestrictedConnection and more blocked methods to RestrictedEntityManager (#6129)

* adding blocked functions ro RestrictedEntityManager and creating RestrictedConnection class

* adding flow tests and fixing issues regarding the review

* adding quasar util to gradle

* updating flow tests

* adding space before } at .isThrownBy()

* adding spaces
This commit is contained in:
nikinagy
2020-04-21 14:39:41 +01:00
committed by GitHub
parent 9ca251c65f
commit 2bcaa2ac80
8 changed files with 428 additions and 2 deletions

View File

@ -0,0 +1,78 @@
package net.corda.nodeapitests.internal.persistence
import co.paralleluniverse.fibers.Suspendable
import net.corda.core.flows.FlowLogic
import net.corda.core.flows.InitiatingFlow
import net.corda.core.identity.CordaX500Name
import net.corda.core.utilities.getOrThrow
import net.corda.nodeapi.internal.persistence.RestrictedConnection
import net.corda.testing.node.MockNetwork
import net.corda.testing.node.MockNetworkParameters
import net.corda.testing.node.StartedMockNode
import org.assertj.core.api.Assertions
import org.junit.After
import org.junit.Before
import org.junit.Test
import kotlin.test.assertTrue
class RestrictedConnectionFlowTest {
private lateinit var aliceNode: StartedMockNode
private lateinit var mockNetwork: MockNetwork
@InitiatingFlow
class TestIfItIsRestrictedConnection : FlowLogic<Boolean>() {
@Suspendable
override fun call() : Boolean {
val connection = serviceHub.jdbcSession()
return connection is RestrictedConnection
}
}
@InitiatingFlow
class TestAutoCommitMethodIsBlocked : FlowLogic<Unit>() {
@Suspendable
override fun call() {
val connection = serviceHub.jdbcSession()
connection.autoCommit = true
}
}
@InitiatingFlow
class TestCloseMethodIsBlocked : FlowLogic<Unit>() {
@Suspendable
override fun call() {
val connection = serviceHub.jdbcSession()
connection.close()
}
}
@Before
fun init() {
mockNetwork = MockNetwork(MockNetworkParameters())
aliceNode = mockNetwork.createPartyNode(CordaX500Name("Alice", "London", "GB"))
}
@After
fun done() {
mockNetwork.stopNodes()
}
@Test(timeout=300_000)
fun testIfItIsRestrictedConnection() {
assertTrue { aliceNode.startFlow(TestIfItIsRestrictedConnection()).get() }
mockNetwork.runNetwork()
}
@Test(timeout=300_000)
fun testMethodsAreBlocked() {
Assertions.assertThatExceptionOfType(UnsupportedOperationException::class.java)
.isThrownBy { aliceNode.startFlow(TestAutoCommitMethodIsBlocked()).getOrThrow() }
.withMessageContaining("This method cannot be called via ServiceHub.jdbcSession.")
Assertions.assertThatExceptionOfType(UnsupportedOperationException::class.java)
.isThrownBy { aliceNode.startFlow(TestCloseMethodIsBlocked()).getOrThrow() }
.withMessageContaining("This method cannot be called via ServiceHub.jdbcSession.")
mockNetwork.runNetwork()
}
}

View File

@ -0,0 +1,84 @@
package net.corda.nodeapitests.internal.persistence
import co.paralleluniverse.fibers.Suspendable
import net.corda.core.flows.FlowLogic
import net.corda.core.flows.InitiatingFlow
import net.corda.core.identity.CordaX500Name
import net.corda.core.utilities.getOrThrow
import net.corda.nodeapi.internal.persistence.RestrictedEntityManager
import net.corda.testing.node.MockNetwork
import net.corda.testing.node.MockNetworkParameters
import net.corda.testing.node.StartedMockNode
import org.assertj.core.api.Assertions
import org.junit.After
import org.junit.Before
import org.junit.Test
import kotlin.test.assertTrue
class RestrictedEntityManagerFlowTest {
private lateinit var aliceNode: StartedMockNode
private lateinit var mockNetwork: MockNetwork
@InitiatingFlow
class TestIfItIsRestrictedEntityManager : FlowLogic<Boolean>() {
@Suspendable
override fun call() : Boolean {
var result = false
serviceHub.withEntityManager() {
result = this is RestrictedEntityManager
}
return result
}
}
@InitiatingFlow
class TestCloseMethodIsBlocked : FlowLogic<Unit>() {
@Suspendable
override fun call() {
serviceHub.withEntityManager() {
this.close()
}
}
}
@InitiatingFlow
class TestJoinTransactionMethodIsBlocked : FlowLogic<Unit>() {
@Suspendable
override fun call() {
serviceHub.withEntityManager() {
this.joinTransaction()
}
}
}
@Before
fun init() {
mockNetwork = MockNetwork(MockNetworkParameters())
aliceNode = mockNetwork.createPartyNode(CordaX500Name("Alice", "London", "GB"))
}
@After
fun done() {
mockNetwork.stopNodes()
}
@Test(timeout=300_000)
fun testIfItIsRestrictedConnection() {
assertTrue { aliceNode.startFlow(TestIfItIsRestrictedEntityManager()).get() }
mockNetwork.runNetwork()
}
@Test(timeout=300_000)
fun testMethodsAreBlocked() {
Assertions.assertThatExceptionOfType(UnsupportedOperationException::class.java)
.isThrownBy { aliceNode.startFlow(TestCloseMethodIsBlocked()).getOrThrow() }
.withMessageContaining("This method cannot be called via ServiceHub.withEntityManager.")
Assertions.assertThatExceptionOfType(UnsupportedOperationException::class.java)
.isThrownBy { aliceNode.startFlow(TestJoinTransactionMethodIsBlocked()).getOrThrow() }
.withMessageContaining("This method cannot be called via ServiceHub.withEntityManager.")
mockNetwork.runNetwork()
}
}