|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this material except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package io.temporal.workflow.nexus; |
| 22 | + |
| 23 | +import io.nexusrpc.Operation; |
| 24 | +import io.nexusrpc.Service; |
| 25 | +import io.nexusrpc.handler.OperationHandler; |
| 26 | +import io.nexusrpc.handler.OperationImpl; |
| 27 | +import io.nexusrpc.handler.ServiceImpl; |
| 28 | +import io.temporal.client.WorkflowOptions; |
| 29 | +import io.temporal.nexus.WorkflowClientOperationHandlers; |
| 30 | +import io.temporal.nexus.WorkflowHandle; |
| 31 | +import io.temporal.testing.internal.SDKTestWorkflowRule; |
| 32 | +import io.temporal.workflow.NexusOperationOptions; |
| 33 | +import io.temporal.workflow.NexusServiceOptions; |
| 34 | +import io.temporal.workflow.Workflow; |
| 35 | +import io.temporal.workflow.shared.TestMultiArgWorkflowFunctions; |
| 36 | +import io.temporal.workflow.shared.TestWorkflows; |
| 37 | +import java.time.Duration; |
| 38 | +import org.junit.Assert; |
| 39 | +import org.junit.Rule; |
| 40 | +import org.junit.Test; |
| 41 | + |
| 42 | +public class WorkflowHandleStubTest { |
| 43 | + @Rule |
| 44 | + public SDKTestWorkflowRule testWorkflowRule = |
| 45 | + SDKTestWorkflowRule.newBuilder() |
| 46 | + .setWorkflowTypes( |
| 47 | + TestNexus.class, TestMultiArgWorkflowFunctions.TestMultiArgWorkflowImpl.class) |
| 48 | + .setNexusServiceImplementation(new TestNexusServiceFuncImpl()) |
| 49 | + .build(); |
| 50 | + |
| 51 | + @Test |
| 52 | + public void stubTests() { |
| 53 | + TestWorkflows.TestWorkflow1 workflowStub = |
| 54 | + testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflows.TestWorkflow1.class); |
| 55 | + String result = workflowStub.execute(testWorkflowRule.getTaskQueue()); |
| 56 | + Assert.assertEquals("success", result); |
| 57 | + } |
| 58 | + |
| 59 | + public static class TestNexus implements TestWorkflows.TestWorkflow1 { |
| 60 | + @Override |
| 61 | + public String execute(String input) { |
| 62 | + NexusOperationOptions options = |
| 63 | + NexusOperationOptions.newBuilder() |
| 64 | + .setScheduleToCloseTimeout(Duration.ofSeconds(10)) |
| 65 | + .build(); |
| 66 | + NexusServiceOptions serviceOptions = |
| 67 | + NexusServiceOptions.newBuilder().setOperationOptions(options).build(); |
| 68 | + |
| 69 | + TestNexusServiceProc serviceStub = |
| 70 | + Workflow.newNexusServiceStub(TestNexusServiceProc.class, serviceOptions); |
| 71 | + for (int i = 0; i < 7; i++) { |
| 72 | + serviceStub.operation(i); |
| 73 | + } |
| 74 | + return "success"; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Service |
| 79 | + public interface TestNexusServiceProc { |
| 80 | + @Operation |
| 81 | + Void operation(Integer input); |
| 82 | + } |
| 83 | + |
| 84 | + @ServiceImpl(service = TestNexusServiceProc.class) |
| 85 | + public class TestNexusServiceFuncImpl { |
| 86 | + @OperationImpl |
| 87 | + public OperationHandler<Integer, Void> operation() { |
| 88 | + return WorkflowClientOperationHandlers.fromWorkflowHandle( |
| 89 | + (context, details, client, input) -> { |
| 90 | + switch (input) { |
| 91 | + case 0: |
| 92 | + return WorkflowHandle.fromWorkflowStub( |
| 93 | + client.newUntypedWorkflowStub( |
| 94 | + "TestNoArgsWorkflowProc", |
| 95 | + WorkflowOptions.newBuilder().setWorkflowId(details.getRequestId()).build()), |
| 96 | + Void.class); |
| 97 | + case 1: |
| 98 | + return WorkflowHandle.fromWorkflowStub( |
| 99 | + client.newUntypedWorkflowStub( |
| 100 | + "Test1ArgWorkflowProc", |
| 101 | + WorkflowOptions.newBuilder().setWorkflowId(details.getRequestId()).build()), |
| 102 | + Void.class, |
| 103 | + "input"); |
| 104 | + case 2: |
| 105 | + return WorkflowHandle.fromWorkflowStub( |
| 106 | + client.newUntypedWorkflowStub( |
| 107 | + "Test2ArgWorkflowProc", |
| 108 | + WorkflowOptions.newBuilder().setWorkflowId(details.getRequestId()).build()), |
| 109 | + Void.class, |
| 110 | + "input", |
| 111 | + 2); |
| 112 | + case 3: |
| 113 | + return WorkflowHandle.fromWorkflowStub( |
| 114 | + client.newUntypedWorkflowStub( |
| 115 | + "Test3ArgWorkflowProc", |
| 116 | + WorkflowOptions.newBuilder().setWorkflowId(details.getRequestId()).build()), |
| 117 | + Void.class, |
| 118 | + "input", |
| 119 | + 2, |
| 120 | + 3); |
| 121 | + case 4: |
| 122 | + return WorkflowHandle.fromWorkflowStub( |
| 123 | + client.newUntypedWorkflowStub( |
| 124 | + "Test4ArgWorkflowProc", |
| 125 | + WorkflowOptions.newBuilder().setWorkflowId(details.getRequestId()).build()), |
| 126 | + Void.class, |
| 127 | + "input", |
| 128 | + 2, |
| 129 | + 3, |
| 130 | + 4); |
| 131 | + case 5: |
| 132 | + return WorkflowHandle.fromWorkflowStub( |
| 133 | + client.newUntypedWorkflowStub( |
| 134 | + "Test5ArgWorkflowProc", |
| 135 | + WorkflowOptions.newBuilder().setWorkflowId(details.getRequestId()).build()), |
| 136 | + Void.class, |
| 137 | + "input", |
| 138 | + 2, |
| 139 | + 3, |
| 140 | + 4, |
| 141 | + 5); |
| 142 | + case 6: |
| 143 | + return WorkflowHandle.fromWorkflowStub( |
| 144 | + client.newUntypedWorkflowStub( |
| 145 | + "Test6ArgWorkflowProc", |
| 146 | + WorkflowOptions.newBuilder().setWorkflowId(details.getRequestId()).build()), |
| 147 | + Void.class, |
| 148 | + "input", |
| 149 | + 2, |
| 150 | + 3, |
| 151 | + 4, |
| 152 | + 5, |
| 153 | + 6); |
| 154 | + default: |
| 155 | + return null; |
| 156 | + } |
| 157 | + }); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments