|
| 1 | +package io.temporal.samples.hello; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.fail; |
| 5 | + |
| 6 | +import io.temporal.client.WorkflowFailedException; |
| 7 | +import io.temporal.client.WorkflowOptions; |
| 8 | +import io.temporal.client.WorkflowStub; |
| 9 | +import io.temporal.testing.TestWorkflowEnvironment; |
| 10 | +import io.temporal.testing.TestWorkflowExtension; |
| 11 | +import io.temporal.worker.Worker; |
| 12 | +import io.temporal.worker.WorkflowImplementationOptions; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 15 | + |
| 16 | +public class HelloSignalWithStartAndWorkflowInitTest { |
| 17 | + @RegisterExtension |
| 18 | + public static final TestWorkflowExtension testWorkflowExtension = |
| 19 | + TestWorkflowExtension.newBuilder() |
| 20 | + .registerWorkflowImplementationTypes( |
| 21 | + HelloSignalWithStartAndWorkflowInit.WithInitMyWorkflowImpl.class) |
| 22 | + .registerWorkflowImplementationTypes( |
| 23 | + WorkflowImplementationOptions.newBuilder() |
| 24 | + .setFailWorkflowExceptionTypes(NullPointerException.class) |
| 25 | + .build(), |
| 26 | + HelloSignalWithStartAndWorkflowInit.WithoutInitMyWorkflowImpl.class) |
| 27 | + .setActivityImplementations( |
| 28 | + new HelloSignalWithStartAndWorkflowInit.MyGreetingActivitiesImpl()) |
| 29 | + .build(); |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testWithInit(TestWorkflowEnvironment testEnv, Worker worker) { |
| 33 | + HelloSignalWithStartAndWorkflowInit.MyWorkflowWithInit withInitStub = |
| 34 | + testEnv |
| 35 | + .getWorkflowClient() |
| 36 | + .newWorkflowStub( |
| 37 | + HelloSignalWithStartAndWorkflowInit.MyWorkflowWithInit.class, |
| 38 | + WorkflowOptions.newBuilder() |
| 39 | + .setWorkflowId("with-init") |
| 40 | + .setTaskQueue(worker.getTaskQueue()) |
| 41 | + .build()); |
| 42 | + WorkflowStub.fromTyped(withInitStub) |
| 43 | + .signalWithStart( |
| 44 | + "addGreeting", |
| 45 | + new Object[] {new HelloSignalWithStartAndWorkflowInit.Person("Michael", "Jordan", 55)}, |
| 46 | + new Object[] {new HelloSignalWithStartAndWorkflowInit.Person("John", "Stockton", 57)}); |
| 47 | + String result = WorkflowStub.fromTyped(withInitStub).getResult(String.class); |
| 48 | + assertEquals("Hello Michael Jordan,Hello John Stockton", result); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testWithoutInit(TestWorkflowEnvironment testEnv, Worker worker) { |
| 53 | + HelloSignalWithStartAndWorkflowInit.MyWorkflowNoInit noInitStub = |
| 54 | + testEnv |
| 55 | + .getWorkflowClient() |
| 56 | + .newWorkflowStub( |
| 57 | + HelloSignalWithStartAndWorkflowInit.MyWorkflowNoInit.class, |
| 58 | + WorkflowOptions.newBuilder() |
| 59 | + .setWorkflowId("without-init") |
| 60 | + .setTaskQueue(worker.getTaskQueue()) |
| 61 | + .build()); |
| 62 | + WorkflowStub.fromTyped(noInitStub) |
| 63 | + .signalWithStart( |
| 64 | + "addGreeting", |
| 65 | + new Object[] {new HelloSignalWithStartAndWorkflowInit.Person("Michael", "Jordan", 55)}, |
| 66 | + new Object[] {new HelloSignalWithStartAndWorkflowInit.Person("John", "Stockton", 57)}); |
| 67 | + try { |
| 68 | + WorkflowStub.fromTyped(noInitStub).getResult(String.class); |
| 69 | + fail("Workflow execution should have failed"); |
| 70 | + } catch (Exception e) { |
| 71 | + if (!(e instanceof WorkflowFailedException)) { |
| 72 | + fail("Workflow execution should have failed"); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments