diff --git a/core/src/main/java/io/temporal/samples/nexus/README.MD b/core/src/main/java/io/temporal/samples/nexus/README.MD index 3798e203..497ddc51 100644 --- a/core/src/main/java/io/temporal/samples/nexus/README.MD +++ b/core/src/main/java/io/temporal/samples/nexus/README.MD @@ -99,5 +99,5 @@ In separate terminal windows: which should result in: ``` [main] INFO i.t.s.nexus.caller.CallerStarter - Workflow result: Nexus Echo πŸ‘‹ -[main] INFO i.t.s.nexus.caller.CallerStarter - Workflow result: Hola Nexus πŸ‘‹ +[main] INFO i.t.s.nexus.caller.CallerStarter - Workflow result: Β‘Hola! Nexus πŸ‘‹ ``` diff --git a/core/src/main/java/io/temporal/samples/nexus/caller/CallerStarter.java b/core/src/main/java/io/temporal/samples/nexus/caller/CallerStarter.java index 3d209831..fa68b230 100644 --- a/core/src/main/java/io/temporal/samples/nexus/caller/CallerStarter.java +++ b/core/src/main/java/io/temporal/samples/nexus/caller/CallerStarter.java @@ -2,6 +2,7 @@ import io.temporal.client.WorkflowClient; import io.temporal.client.WorkflowOptions; +import io.temporal.client.WorkflowStub; import io.temporal.samples.nexus.options.ClientOptions; import io.temporal.samples.nexus.service.NexusService; import org.slf4j.Logger; @@ -17,9 +18,17 @@ public static void main(String[] args) { WorkflowOptions.newBuilder().setTaskQueue(CallerWorker.DEFAULT_TASK_QUEUE_NAME).build(); EchoCallerWorkflow echoWorkflow = client.newWorkflowStub(EchoCallerWorkflow.class, workflowOptions); - logger.info("Workflow result: " + echoWorkflow.echo("Nexus Echo πŸ‘‹")); + logger.info("Workflow result: {}", echoWorkflow.echo("Nexus Echo πŸ‘‹")); + logger.info( + "Started workflow workflowId: {} runId; {}", + WorkflowStub.fromTyped(echoWorkflow).getExecution().getWorkflowId(), + WorkflowStub.fromTyped(echoWorkflow).getExecution().getRunId()); HelloCallerWorkflow helloWorkflow = client.newWorkflowStub(HelloCallerWorkflow.class, workflowOptions); - logger.info("Workflow result: " + helloWorkflow.hello("Nexus", NexusService.Language.ES)); + logger.info("Workflow result: {}", helloWorkflow.hello("Nexus", NexusService.Language.ES)); + logger.info( + "Started workflow workflowId: {} runId; {}", + WorkflowStub.fromTyped(helloWorkflow).getExecution().getWorkflowId(), + WorkflowStub.fromTyped(helloWorkflow).getExecution().getRunId()); } } diff --git a/core/src/main/java/io/temporal/samples/nexus/caller/HelloCallerWorkflowImpl.java b/core/src/main/java/io/temporal/samples/nexus/caller/HelloCallerWorkflowImpl.java index e95dce17..90486dbf 100644 --- a/core/src/main/java/io/temporal/samples/nexus/caller/HelloCallerWorkflowImpl.java +++ b/core/src/main/java/io/temporal/samples/nexus/caller/HelloCallerWorkflowImpl.java @@ -2,10 +2,21 @@ import io.temporal.samples.nexus.service.NexusService; import io.temporal.workflow.NexusOperationHandle; +import io.temporal.workflow.NexusOperationOptions; +import io.temporal.workflow.NexusServiceOptions; import io.temporal.workflow.Workflow; +import java.time.Duration; public class HelloCallerWorkflowImpl implements HelloCallerWorkflow { - NexusService nexusService = Workflow.newNexusServiceStub(NexusService.class); + NexusService nexusService = + Workflow.newNexusServiceStub( + NexusService.class, + NexusServiceOptions.newBuilder() + .setOperationOptions( + NexusOperationOptions.newBuilder() + .setScheduleToCloseTimeout(Duration.ofSeconds(10)) + .build()) + .build()); @Override public String hello(String message, NexusService.Language language) { @@ -13,8 +24,7 @@ public String hello(String message, NexusService.Language language) { Workflow.startNexusOperation( nexusService::hello, new NexusService.HelloInput(message, language)); // Optionally wait for the operation to be started. NexusOperationExecution will contain the - // operation ID in - // case this operation is asynchronous. + // operation ID in case this operation is asynchronous. handle.getExecution().get(); return handle.getResult().get().getMessage(); } diff --git a/core/src/main/java/io/temporal/samples/nexus/handler/HelloHandlerWorkflowImpl.java b/core/src/main/java/io/temporal/samples/nexus/handler/HelloHandlerWorkflowImpl.java index 84787b65..c897a518 100644 --- a/core/src/main/java/io/temporal/samples/nexus/handler/HelloHandlerWorkflowImpl.java +++ b/core/src/main/java/io/temporal/samples/nexus/handler/HelloHandlerWorkflowImpl.java @@ -14,7 +14,7 @@ public NexusService.HelloOutput hello(NexusService.HelloInput input) { case DE: return new NexusService.HelloOutput("Hallo " + input.getName() + " πŸ‘‹"); case ES: - return new NexusService.HelloOutput("Hola " + input.getName() + " πŸ‘‹"); + return new NexusService.HelloOutput("Β‘Hola! " + input.getName() + " πŸ‘‹"); case TR: return new NexusService.HelloOutput("Merhaba " + input.getName() + " πŸ‘‹"); } diff --git a/core/src/main/java/io/temporal/samples/nexus/handler/NexusServiceImpl.java b/core/src/main/java/io/temporal/samples/nexus/handler/NexusServiceImpl.java index 023cb869..aa3cc81e 100644 --- a/core/src/main/java/io/temporal/samples/nexus/handler/NexusServiceImpl.java +++ b/core/src/main/java/io/temporal/samples/nexus/handler/NexusServiceImpl.java @@ -7,6 +7,9 @@ import io.temporal.nexus.WorkflowClientOperationHandlers; import io.temporal.samples.nexus.service.NexusService; +// To create a service implementation, annotate the class with @ServiceImpl and provide the +// interface that the service implements. The service implementation class should have methods that +// return OperationHandler that correspond to the operations defined in the service interface. @ServiceImpl(service = NexusService.class) public class NexusServiceImpl { @OperationImpl @@ -25,7 +28,6 @@ public OperationHandler echo() public OperationHandler hello() { // Use the WorkflowClientOperationHandlers.fromWorkflowMethod constructor, which is the easiest // way to expose a workflow as an operation. - // See alternatives at TODO. return WorkflowClientOperationHandlers.fromWorkflowMethod( (ctx, details, client, input) -> client.newWorkflowStub( diff --git a/core/src/test/java/io/temporal/samples/nexus/caller/CallerWorkflowTest.java b/core/src/test/java/io/temporal/samples/nexus/caller/CallerWorkflowTest.java new file mode 100644 index 00000000..4bbea043 --- /dev/null +++ b/core/src/test/java/io/temporal/samples/nexus/caller/CallerWorkflowTest.java @@ -0,0 +1,80 @@ +package io.temporal.samples.nexus.caller; + +import io.temporal.client.WorkflowOptions; +import io.temporal.samples.nexus.handler.HelloHandlerWorkflow; +import io.temporal.samples.nexus.handler.NexusServiceImpl; +import io.temporal.samples.nexus.service.NexusService; +import io.temporal.testing.TestWorkflowRule; +import io.temporal.worker.WorkflowImplementationOptions; +import io.temporal.workflow.NexusServiceOptions; +import org.junit.Rule; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class CallerWorkflowTest { + + @Rule + public TestWorkflowRule testWorkflowRule = + TestWorkflowRule.newBuilder() + // If a Nexus service is registered as part of the test, the TestWorkflowRule will ,by + // default, automatically create a Nexus service endpoint and workflows registered as part + // of the TestWorkflowRule will automatically inherit the endpoint if none is set. + .setNexusServiceImplementation(new NexusServiceImpl()) + .setWorkflowTypes(HelloCallerWorkflowImpl.class) + .setDoNotStart(true) + .build(); + + @Test + public void testHelloWorkflow() { + testWorkflowRule + .getWorker() + .registerWorkflowImplementationFactory( + HelloHandlerWorkflow.class, + () -> { + HelloHandlerWorkflow wf = mock(HelloHandlerWorkflow.class); + when(wf.hello(any())).thenReturn(new NexusService.HelloOutput("Hello World πŸ‘‹")); + return wf; + }); + testWorkflowRule.getTestEnvironment().start(); + + HelloCallerWorkflow workflow = + testWorkflowRule + .getWorkflowClient() + .newWorkflowStub( + HelloCallerWorkflow.class, + WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.getTaskQueue()).build()); + String greeting = workflow.hello("World", NexusService.Language.EN); + assertEquals("Hello World πŸ‘‹", greeting); + + testWorkflowRule.getTestEnvironment().shutdown(); + } + + @Test + public void testEchoWorkflow() { + testWorkflowRule + .getWorker() + .registerWorkflowImplementationTypes( + WorkflowImplementationOptions.newBuilder() + .setDefaultNexusServiceOptions( + NexusServiceOptions.newBuilder() + .setEndpoint(testWorkflowRule.getNexusEndpoint().getSpec().getName()) + .build()) + .build(), + EchoCallerWorkflowImpl.class); + testWorkflowRule.getTestEnvironment().start(); + + EchoCallerWorkflow workflow = + testWorkflowRule + .getWorkflowClient() + .newWorkflowStub( + EchoCallerWorkflow.class, + WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.getTaskQueue()).build()); + String greeting = workflow.echo("Hello"); + assertEquals("Hello", greeting); + testWorkflowRule.getTestEnvironment().shutdown(); + } +}