From 0d4e9c003e2f613263a6e5401e649a5ff0dd1d30 Mon Sep 17 00:00:00 2001 From: KiKoS0 <22998716+KiKoS0@users.noreply.github.com> Date: Thu, 12 Sep 2024 00:37:43 +0100 Subject: [PATCH] Add integration test for `step.invoke` (#78) --- .../testfunctions/MultiplyMatrixFunction.java | 42 ++++++++++++++ .../testfunctions/Scale2DObjectFunction.java | 45 +++++++++++++++ .../springbootdemo/DemoTestConfiguration.java | 2 + .../InngestFunctionTestHelpers.java | 2 +- .../InvokeFunctionIntegrationTest.java | 56 +++++++++++++++++++ 5 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/MultiplyMatrixFunction.java create mode 100644 inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/Scale2DObjectFunction.java create mode 100644 inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/InvokeFunctionIntegrationTest.java diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/MultiplyMatrixFunction.java b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/MultiplyMatrixFunction.java new file mode 100644 index 00000000..a0904c7c --- /dev/null +++ b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/MultiplyMatrixFunction.java @@ -0,0 +1,42 @@ +package com.inngest.springbootdemo.testfunctions; + +import com.inngest.*; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public class MultiplyMatrixFunction extends InngestFunction { + @NotNull + @Override + public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { + return builder + .id("MultiplyMatrixFunction") + .name("Multiply Matrix") + .triggerEvent("test/multiply.matrix"); + } + + + @Override + public List> execute(FunctionContext ctx, Step step) { + List> A = (List>) ctx.getEvent().getData().get("matrixA"); + List> B = (List>) ctx.getEvent().getData().get("matrixB"); + + return step.run("multiply-matrix", () -> + { + List> result = new ArrayList<>(); + for (List integers : A) { + List row = new ArrayList<>(); + for (int j = 0; j < B.get(0).size(); j++) { + double sum = 0; + for (int k = 0; k < integers.size(); k++) { + sum += integers.get(k) * B.get(k).get(j); + } + row.add(sum); + } + result.add(row); + } + return result; + }, List.class); + } +} diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/Scale2DObjectFunction.java b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/Scale2DObjectFunction.java new file mode 100644 index 00000000..c1480349 --- /dev/null +++ b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/Scale2DObjectFunction.java @@ -0,0 +1,45 @@ +package com.inngest.springbootdemo.testfunctions; + +import com.inngest.*; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; + +public class Scale2DObjectFunction extends InngestFunction { + @NotNull + @Override + public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { + return builder + .id("Scale2DObjectFunction") + .name("Scale 2D Object") + .triggerEvent("test/scale2d.object"); + } + + @Override + public List> execute(FunctionContext ctx, Step step) { + List> matrix = (List>) ctx.getEvent().getData().get("matrix"); + int scaleX = (int) ctx.getEvent().getData().get("scaleX"); + int scaleY = (int) ctx.getEvent().getData().get("scaleY"); + + List> scalingMatrix = new ArrayList<>(Arrays.asList( + new ArrayList<>(Arrays.asList(scaleX, 0)), + new ArrayList<>(Arrays.asList(0, scaleY)) + )); + + LinkedHashMap eventData = new LinkedHashMap() {{ + put("matrixA", matrix); + put("matrixB", scalingMatrix); + }}; + + return step.invoke( + "multiply-matrix", + "spring_test_demo", + "MultiplyMatrixFunction", + eventData, + null, + List.class); + } +} diff --git a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java index d4614f0f..cbada3ea 100644 --- a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java +++ b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java @@ -27,6 +27,8 @@ protected HashMap functions() { addInngestFunction(functions, new DebouncedFunction()); addInngestFunction(functions, new PriorityFunction()); addInngestFunction(functions, new IdempotentFunction()); + addInngestFunction(functions, new Scale2DObjectFunction()); + addInngestFunction(functions, new MultiplyMatrixFunction()); return functions; } diff --git a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/InngestFunctionTestHelpers.java b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/InngestFunctionTestHelpers.java index cce75134..eff448b3 100644 --- a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/InngestFunctionTestHelpers.java +++ b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/InngestFunctionTestHelpers.java @@ -12,7 +12,7 @@ static SendEventsResponse sendEvent(Inngest inngest, String eventName) { return sendEvent(inngest, eventName, new HashMap()); } - static SendEventsResponse sendEvent(Inngest inngest, String eventName, Map data) { + static SendEventsResponse sendEvent(Inngest inngest, String eventName, Map data) { InngestEvent event = new InngestEvent(eventName, data); SendEventsResponse response = inngest.send(event); diff --git a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/InvokeFunctionIntegrationTest.java b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/InvokeFunctionIntegrationTest.java new file mode 100644 index 00000000..028f47b9 --- /dev/null +++ b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/InvokeFunctionIntegrationTest.java @@ -0,0 +1,56 @@ +package com.inngest.springbootdemo; + +import com.inngest.Inngest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@IntegrationTest +@Execution(ExecutionMode.CONCURRENT) +class InvokeFunctionIntegrationTest { + @Autowired + private DevServerComponent devServer; + + static int sleepTime = 5000; + + @Autowired + private Inngest client; + + @Test + void testShouldInvokeScaleObjectFunctionAndReturnCorrectResult() throws Exception { + ArrayList> square = new ArrayList<>(Arrays.asList( + new ArrayList<>(Arrays.asList(1, 1)), + new ArrayList<>(Arrays.asList(1, 2)), + new ArrayList<>(Arrays.asList(2, 1)), + new ArrayList<>(Arrays.asList(2, 2)) + )); + + LinkedHashMap eventData = new LinkedHashMap() {{ + put("matrix", square); + put("scaleX", 2); + put("scaleY", 3); + }}; + + String eventId = InngestFunctionTestHelpers.sendEvent(client, "test/scale2d.object", eventData).getIds()[0]; + + Thread.sleep(sleepTime); + + RunEntry run = devServer.runsByEvent(eventId).first(); + + assertEquals(run.getStatus(), "Completed"); + + assertEquals(new ArrayList<>(Arrays.asList( + new ArrayList<>(Arrays.asList(2, 3)), + new ArrayList<>(Arrays.asList(2, 6)), + new ArrayList<>(Arrays.asList(4, 3)), + new ArrayList<>(Arrays.asList(4, 6)) + )), run.getOutput()); + } +}