|
| 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.nexus.WorkflowClientOperationHandlers; |
| 29 | +import io.temporal.testing.internal.SDKTestWorkflowRule; |
| 30 | +import io.temporal.workflow.NexusServiceOptions; |
| 31 | +import io.temporal.workflow.NexusServiceStub; |
| 32 | +import io.temporal.workflow.Workflow; |
| 33 | +import io.temporal.workflow.shared.TestWorkflows; |
| 34 | +import org.junit.Assert; |
| 35 | +import org.junit.ClassRule; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +// Test an operation that takes and returns a void type |
| 39 | +public class VoidOperationTest { |
| 40 | + @ClassRule |
| 41 | + public static SDKTestWorkflowRule testWorkflowRule = |
| 42 | + SDKTestWorkflowRule.newBuilder() |
| 43 | + .setWorkflowTypes(TestNexus.class) |
| 44 | + .setNexusServiceImplementation(new TestNexusServiceImpl()) |
| 45 | + .build(); |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testVoidOperation() { |
| 49 | + TestWorkflows.TestWorkflow1 workflowStub = |
| 50 | + testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflows.TestWorkflow1.class); |
| 51 | + String result = workflowStub.execute(testWorkflowRule.getTaskQueue()); |
| 52 | + Assert.assertEquals("success", result); |
| 53 | + } |
| 54 | + |
| 55 | + public static class TestNexus implements TestWorkflows.TestWorkflow1 { |
| 56 | + @Override |
| 57 | + public String execute(String input) { |
| 58 | + NexusServiceOptions serviceOptions = |
| 59 | + NexusServiceOptions.newBuilder() |
| 60 | + .setEndpoint(testWorkflowRule.getNexusEndpoint().getSpec().getName()) |
| 61 | + .build(); |
| 62 | + // Try to call with the typed stub |
| 63 | + TestNexusService serviceStub = |
| 64 | + Workflow.newNexusServiceStub(TestNexusService.class, serviceOptions); |
| 65 | + serviceStub.noop(); |
| 66 | + // Try to call with an untyped stub |
| 67 | + NexusServiceStub untypedServiceStub = |
| 68 | + Workflow.newUntypedNexusServiceStub("TestNexusService", serviceOptions); |
| 69 | + untypedServiceStub.execute("noop", Void.class, null); |
| 70 | + untypedServiceStub.execute("noop", Void.class, Void.class, null); |
| 71 | + return "success"; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Service |
| 76 | + public interface TestNexusService { |
| 77 | + @Operation |
| 78 | + void noop(); |
| 79 | + } |
| 80 | + |
| 81 | + @ServiceImpl(service = TestNexusService.class) |
| 82 | + public static class TestNexusServiceImpl { |
| 83 | + @OperationImpl |
| 84 | + public OperationHandler<Void, Void> noop() { |
| 85 | + return WorkflowClientOperationHandlers.sync((context, details, client, input) -> null); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments