mirror of
https://github.com/corda/corda.git
synced 2025-06-22 17:09:00 +00:00
CORDA-1942: StartedMockNode.registerResponderFlow simplified (#4483)
The ResponderFlowFactory parameter is not used and so removed. Also, instead of returning a Future it returns an Observable of responder flows, to support multiple invocations. And finally renamed to registerInitiatedFlow to stick with the existing naming strategy.
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package net.corda.testing.node;
|
||||
|
||||
import co.paralleluniverse.fibers.Suspendable;
|
||||
import net.corda.core.Utils;
|
||||
import net.corda.core.concurrent.CordaFuture;
|
||||
import net.corda.core.flows.*;
|
||||
import net.corda.core.identity.Party;
|
||||
@ -39,10 +40,10 @@ public class TestResponseFlowInIsolationInJava {
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
// This method returns the Responder flow object used by node B.
|
||||
Future<Responder> initiatedResponderFlowFuture = b.registerResponderFlow(
|
||||
Future<Responder> initiatedResponderFlowFuture = Utils.toFuture(b.registerInitiatedFlow(
|
||||
// We tell node B to respond to BadInitiator with Responder.
|
||||
// We want to observe the Responder flow object to check for errors.
|
||||
BadInitiator.class, Responder::new, Responder.class);
|
||||
BadInitiator.class, Responder.class));
|
||||
|
||||
// We run the BadInitiator flow on node A.
|
||||
BadInitiator flow = new BadInitiator(b.getInfo().getLegalIdentities().get(0));
|
||||
|
@ -3,16 +3,15 @@ package net.corda.testing.node.internal
|
||||
import co.paralleluniverse.fibers.Suspendable
|
||||
import net.corda.core.flows.*
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.toFuture
|
||||
import net.corda.core.utilities.getOrThrow
|
||||
import net.corda.core.utilities.unwrap
|
||||
import net.corda.testing.internal.chooseIdentity
|
||||
import net.corda.testing.node.MockNetwork
|
||||
import net.corda.testing.node.MockNetworkParameters
|
||||
import net.corda.testing.node.registerResponderFlow
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThatExceptionOfType
|
||||
import org.junit.After
|
||||
import org.junit.Test
|
||||
import java.util.concurrent.ExecutionException
|
||||
import kotlin.test.assertFailsWith
|
||||
|
||||
/**
|
||||
* Test based on the example given as an answer to this SO question:
|
||||
@ -32,7 +31,7 @@ class TestResponseFlowInIsolation {
|
||||
|
||||
// This is the real implementation of Initiator.
|
||||
@InitiatingFlow
|
||||
open class Initiator(val counterparty: Party) : FlowLogic<Unit>() {
|
||||
open class Initiator(private val counterparty: Party) : FlowLogic<Unit>() {
|
||||
@Suspendable
|
||||
override fun call() {
|
||||
val session = initiateFlow(counterparty)
|
||||
@ -42,7 +41,7 @@ class TestResponseFlowInIsolation {
|
||||
|
||||
// This is the response flow that we want to isolate for testing.
|
||||
@InitiatedBy(Initiator::class)
|
||||
class Responder(val counterpartySession: FlowSession) : FlowLogic<Unit>() {
|
||||
class Responder(private val counterpartySession: FlowSession) : FlowLogic<Unit>() {
|
||||
@Suspendable
|
||||
override fun call() {
|
||||
val string = counterpartySession.receive<String>().unwrap { contents -> contents }
|
||||
@ -54,7 +53,7 @@ class TestResponseFlowInIsolation {
|
||||
|
||||
// This is a fake implementation of Initiator to check how Responder responds to non-golden-path scenarios.
|
||||
@InitiatingFlow
|
||||
class BadInitiator(val counterparty: Party): FlowLogic<Unit>() {
|
||||
class BadInitiator(private val counterparty: Party): FlowLogic<Unit>() {
|
||||
@Suspendable
|
||||
override fun call() {
|
||||
val session = initiateFlow(counterparty)
|
||||
@ -63,12 +62,10 @@ class TestResponseFlowInIsolation {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test`() {
|
||||
fun test() {
|
||||
// This method returns the Responder flow object used by node B.
|
||||
// We tell node B to respond to BadInitiator with Responder.
|
||||
val initiatedResponderFlowFuture = b.registerResponderFlow(
|
||||
initiatingFlowClass = BadInitiator::class.java,
|
||||
flowFactory = ::Responder)
|
||||
val initiatedResponderFlowFuture = b.registerInitiatedFlow(BadInitiator::class.java, Responder::class.java).toFuture()
|
||||
|
||||
// We run the BadInitiator flow on node A.
|
||||
val flow = BadInitiator(b.info.chooseIdentity())
|
||||
@ -77,14 +74,11 @@ class TestResponseFlowInIsolation {
|
||||
future.get()
|
||||
|
||||
// We check that the invocation of the Responder flow object has caused an ExecutionException.
|
||||
val initiatedResponderFlow = initiatedResponderFlowFuture.get()
|
||||
val initiatedResponderFlow = initiatedResponderFlowFuture.getOrThrow()
|
||||
val initiatedResponderFlowResultFuture = initiatedResponderFlow.stateMachine.resultFuture
|
||||
|
||||
val exceptionFromFlow = assertFailsWith<ExecutionException> {
|
||||
initiatedResponderFlowResultFuture.get()
|
||||
}.cause
|
||||
assertThat(exceptionFromFlow)
|
||||
.isInstanceOf(FlowException::class.java)
|
||||
.hasMessage("String did not contain the expected message.")
|
||||
assertThatExceptionOfType(FlowException::class.java)
|
||||
.isThrownBy { initiatedResponderFlowResultFuture.getOrThrow() }
|
||||
.withMessage("String did not contain the expected message.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user