|
| 1 | +package com.functions; |
| 2 | + |
| 3 | +import com.microsoft.azure.functions.*; |
| 4 | +import com.microsoft.azure.functions.annotation.*; |
| 5 | +import com.microsoft.durabletask.*; |
| 6 | +import com.microsoft.durabletask.azurefunctions.DurableClientContext; |
| 7 | +import com.microsoft.durabletask.azurefunctions.DurableClientInput; |
| 8 | +import com.microsoft.durabletask.azurefunctions.DurableEntityTrigger; |
| 9 | +import com.microsoft.durabletask.azurefunctions.DurableOrchestrationTrigger; |
| 10 | + |
| 11 | +import java.util.Optional; |
| 12 | + |
| 13 | +/** |
| 14 | + * Azure Functions for entity e2e tests. |
| 15 | + * <p> |
| 16 | + * Tests: |
| 17 | + * <ul> |
| 18 | + * <li>{@code callEntity} — orchestrator calls entity and returns result</li> |
| 19 | + * <li>{@code signalEntity} — orchestrator signals entity (fire-and-forget), then calls to verify</li> |
| 20 | + * <li>{@code signalAndCallEntity} — orchestrator signals entity to add, then calls to get updated value</li> |
| 21 | + * </ul> |
| 22 | + */ |
| 23 | +public class EntityFunctions { |
| 24 | + |
| 25 | + // ─── Entity trigger ─── |
| 26 | + |
| 27 | + @FunctionName("Counter") |
| 28 | + public String counterEntity( |
| 29 | + @DurableEntityTrigger(name = "req") String req) { |
| 30 | + return EntityRunner.loadAndRun(req, CounterEntity::new); |
| 31 | + } |
| 32 | + |
| 33 | + // ─── Orchestrations ─── |
| 34 | + |
| 35 | + /** |
| 36 | + * Orchestration that calls the counter entity "add" operation and returns the result. |
| 37 | + * Input: JSON with entityKey and addValue fields. |
| 38 | + */ |
| 39 | + @FunctionName("CallEntityOrchestration") |
| 40 | + public int callEntityOrchestration( |
| 41 | + @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
| 42 | + EntityPayload input = ctx.getInput(EntityPayload.class); |
| 43 | + EntityInstanceId entityId = new EntityInstanceId("counter", input.entityKey); |
| 44 | + return ctx.callEntity(entityId, "add", input.addValue, Integer.class).await(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Orchestration that signals the counter entity to "add" then calls "get" to verify. |
| 49 | + * This tests both signalEntity (fire-and-forget) and callEntity (request-response). |
| 50 | + */ |
| 51 | + @FunctionName("SignalThenCallEntityOrchestration") |
| 52 | + public int signalThenCallEntityOrchestration( |
| 53 | + @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
| 54 | + EntityPayload input = ctx.getInput(EntityPayload.class); |
| 55 | + EntityInstanceId entityId = new EntityInstanceId("counter", input.entityKey); |
| 56 | + |
| 57 | + // Signal — fire-and-forget |
| 58 | + ctx.signalEntity(entityId, "add", input.addValue); |
| 59 | + |
| 60 | + // Call — request-response (should see the updated state) |
| 61 | + return ctx.callEntity(entityId, "get", null, Integer.class).await(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Orchestration that calls "get" on a fresh counter entity (tests zero-initialized state). |
| 66 | + */ |
| 67 | + @FunctionName("CallEntityGetOrchestration") |
| 68 | + public int callEntityGetOrchestration( |
| 69 | + @DurableOrchestrationTrigger(name = "ctx") TaskOrchestrationContext ctx) { |
| 70 | + String entityKey = ctx.getInput(String.class); |
| 71 | + EntityInstanceId entityId = new EntityInstanceId("counter", entityKey); |
| 72 | + return ctx.callEntity(entityId, "get", null, Integer.class).await(); |
| 73 | + } |
| 74 | + |
| 75 | + // ─── HTTP triggers ─── |
| 76 | + |
| 77 | + /** |
| 78 | + * POST /api/StartCallEntityOrchestration?key={key}&value={value} |
| 79 | + */ |
| 80 | + @FunctionName("StartCallEntityOrchestration") |
| 81 | + public HttpResponseMessage startCallEntityOrchestration( |
| 82 | + @HttpTrigger(name = "req", methods = {HttpMethod.POST}, |
| 83 | + authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
| 84 | + @DurableClientInput(name = "durableContext") DurableClientContext durableContext, |
| 85 | + final ExecutionContext context) { |
| 86 | + String key = request.getQueryParameters().getOrDefault("key", "e2e-call-" + System.currentTimeMillis()); |
| 87 | + int value = Integer.parseInt(request.getQueryParameters().getOrDefault("value", "5")); |
| 88 | + |
| 89 | + DurableTaskClient client = durableContext.getClient(); |
| 90 | + EntityPayload payload = new EntityPayload(key, value); |
| 91 | + String instanceId = client.scheduleNewOrchestrationInstance("CallEntityOrchestration", payload); |
| 92 | + context.getLogger().info("Started CallEntityOrchestration: " + instanceId); |
| 93 | + return durableContext.createCheckStatusResponse(request, instanceId); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * POST /api/StartSignalThenCallEntityOrchestration?key={key}&value={value} |
| 98 | + */ |
| 99 | + @FunctionName("StartSignalThenCallEntityOrchestration") |
| 100 | + public HttpResponseMessage startSignalThenCallEntityOrchestration( |
| 101 | + @HttpTrigger(name = "req", methods = {HttpMethod.POST}, |
| 102 | + authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
| 103 | + @DurableClientInput(name = "durableContext") DurableClientContext durableContext, |
| 104 | + final ExecutionContext context) { |
| 105 | + String key = request.getQueryParameters().getOrDefault("key", "e2e-signal-" + System.currentTimeMillis()); |
| 106 | + int value = Integer.parseInt(request.getQueryParameters().getOrDefault("value", "10")); |
| 107 | + |
| 108 | + DurableTaskClient client = durableContext.getClient(); |
| 109 | + EntityPayload payload = new EntityPayload(key, value); |
| 110 | + String instanceId = client.scheduleNewOrchestrationInstance("SignalThenCallEntityOrchestration", payload); |
| 111 | + context.getLogger().info("Started SignalThenCallEntityOrchestration: " + instanceId); |
| 112 | + return durableContext.createCheckStatusResponse(request, instanceId); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * POST /api/StartCallEntityGetOrchestration?key={key} |
| 117 | + */ |
| 118 | + @FunctionName("StartCallEntityGetOrchestration") |
| 119 | + public HttpResponseMessage startCallEntityGetOrchestration( |
| 120 | + @HttpTrigger(name = "req", methods = {HttpMethod.POST}, |
| 121 | + authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
| 122 | + @DurableClientInput(name = "durableContext") DurableClientContext durableContext, |
| 123 | + final ExecutionContext context) { |
| 124 | + String key = request.getQueryParameters().getOrDefault("key", "e2e-get-" + System.currentTimeMillis()); |
| 125 | + |
| 126 | + DurableTaskClient client = durableContext.getClient(); |
| 127 | + String instanceId = client.scheduleNewOrchestrationInstance("CallEntityGetOrchestration", key); |
| 128 | + context.getLogger().info("Started CallEntityGetOrchestration: " + instanceId); |
| 129 | + return durableContext.createCheckStatusResponse(request, instanceId); |
| 130 | + } |
| 131 | + |
| 132 | + // ─── Helpers ─── |
| 133 | + |
| 134 | + /** |
| 135 | + * Payload for entity orchestrations. |
| 136 | + */ |
| 137 | + public static class EntityPayload { |
| 138 | + public String entityKey; |
| 139 | + public int addValue; |
| 140 | + |
| 141 | + public EntityPayload() { |
| 142 | + } |
| 143 | + |
| 144 | + public EntityPayload(String entityKey, int addValue) { |
| 145 | + this.entityKey = entityKey; |
| 146 | + this.addValue = addValue; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments