mirror of
https://github.com/corda/corda.git
synced 2025-06-13 04:38:19 +00:00
Add java samples
This commit is contained in:
@ -24,6 +24,11 @@ sourceSets {
|
||||
runtimeClasspath += main.output + test.output
|
||||
srcDir file('src/integration-test/kotlin')
|
||||
}
|
||||
java {
|
||||
compileClasspath += main.output + test.output
|
||||
runtimeClasspath += main.output + test.output
|
||||
srcDir file('src/integration-test/java')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,42 @@
|
||||
package net.corda.docs.java;
|
||||
|
||||
import kotlin.Unit;
|
||||
import net.corda.client.rpc.CordaRPCClient;
|
||||
import net.corda.core.messaging.CordaRPCOps;
|
||||
import net.corda.core.utilities.KotlinUtilsKt;
|
||||
import net.corda.docs.java.tutorial.flowstatemachines.ExampleSummingFlow;
|
||||
import net.corda.node.services.Permissions;
|
||||
import net.corda.testing.driver.*;
|
||||
import net.corda.testing.node.User;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static net.corda.testing.core.TestConstants.ALICE_NAME;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public final class TutorialFlowAsyncOperationTest {
|
||||
// DOCSTART summingWorks
|
||||
@Test
|
||||
public final void summingWorks() {
|
||||
Driver.driver(new DriverParameters(), (DriverDSL dsl) -> {
|
||||
User aliceUser = new User("aliceUser", "testPassword1",
|
||||
new HashSet<>(Collections.singletonList(Permissions.all()))
|
||||
);
|
||||
Future<NodeHandle> aliceFuture = dsl.startNode(new NodeParameters()
|
||||
.withProvidedName(ALICE_NAME)
|
||||
.withRpcUsers(Collections.singletonList(aliceUser))
|
||||
);
|
||||
NodeHandle alice = KotlinUtilsKt.getOrThrow(aliceFuture, null);
|
||||
CordaRPCClient aliceClient = new CordaRPCClient(alice.getRpcAddress());
|
||||
CordaRPCOps aliceProxy = aliceClient.start("aliceUser", "testPassword1").getProxy();
|
||||
Future<Integer> answerFuture = aliceProxy.startFlowDynamic(ExampleSummingFlow.class).getReturnValue();
|
||||
int answer = KotlinUtilsKt.getOrThrow(answerFuture, null);
|
||||
assertEquals(3, answer);
|
||||
return Unit.INSTANCE;
|
||||
});
|
||||
}
|
||||
// DOCEND summingWorks
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package net.corda.docs.java.tutorial.flowstatemachines;
|
||||
|
||||
import co.paralleluniverse.fibers.Suspendable;
|
||||
import net.corda.core.flows.FlowLogic;
|
||||
import net.corda.core.flows.StartableByRPC;
|
||||
import net.corda.core.internal.FlowAsyncOperationKt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
// DOCSTART ExampleSummingFlow
|
||||
@StartableByRPC
|
||||
public final class ExampleSummingFlow extends FlowLogic<Integer> {
|
||||
@Suspendable
|
||||
@NotNull
|
||||
@Override
|
||||
public Integer call() {
|
||||
return FlowAsyncOperationKt.executeAsync(this, new SummingOperation(1, 2), false);
|
||||
}
|
||||
}
|
||||
// DOCEND ExampleSummingFlow
|
@ -0,0 +1,32 @@
|
||||
package net.corda.docs.java.tutorial.flowstatemachines;
|
||||
|
||||
import net.corda.core.concurrent.CordaFuture;
|
||||
import net.corda.core.internal.FlowAsyncOperation;
|
||||
import net.corda.core.internal.concurrent.CordaFutureImplKt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
// DOCSTART SummingOperation
|
||||
public final class SummingOperation implements FlowAsyncOperation<Integer> {
|
||||
private final int a;
|
||||
private final int b;
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CordaFuture<Integer> execute() {
|
||||
return CordaFutureImplKt.doneFuture(this.a + this.b);
|
||||
}
|
||||
|
||||
public final int getA() {
|
||||
return this.a;
|
||||
}
|
||||
|
||||
public final int getB() {
|
||||
return this.b;
|
||||
}
|
||||
|
||||
public SummingOperation(int a, int b) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
}
|
||||
// DOCEND SummingOperation
|
@ -0,0 +1,31 @@
|
||||
package net.corda.docs.java.tutorial.flowstatemachines;
|
||||
|
||||
import net.corda.core.concurrent.CordaFuture;
|
||||
import net.corda.core.internal.FlowAsyncOperation;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
// DOCSTART SummingOperationThrowing
|
||||
public final class SummingOperationThrowing implements FlowAsyncOperation<Integer> {
|
||||
private final int a;
|
||||
private final int b;
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public CordaFuture<Integer> execute() {
|
||||
throw new IllegalStateException("You shouldn't be calling me");
|
||||
}
|
||||
|
||||
public final int getA() {
|
||||
return this.a;
|
||||
}
|
||||
|
||||
public final int getB() {
|
||||
return this.b;
|
||||
}
|
||||
|
||||
public SummingOperationThrowing(int a, int b) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
}
|
||||
// DOCEND SummingOperationThrowing
|
Reference in New Issue
Block a user