Skip to content

Commit

Permalink
Begin adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns committed Oct 15, 2024
1 parent 13b72ea commit 08b311b
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/io/temporal/samples/nexus/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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 👋
```
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@

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) {
NexusOperationHandle<NexusService.HelloOutput> handle =
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() + " 👋");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +28,6 @@ public OperationHandler<NexusService.EchoInput, NexusService.EchoOutput> echo()
public OperationHandler<NexusService.HelloInput, NexusService.HelloOutput> 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 08b311b

Please sign in to comment.