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..822678da --- /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( + "failing-function", + "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 c782dc31..476c7285 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 @@ -26,6 +26,8 @@ protected HashMap functions() { addInngestFunction(functions, new ThrottledFunction()); addInngestFunction(functions, new DebouncedFunction()); addInngestFunction(functions, new PriorityFunction()); + 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 8f0f3b6d..7676f0e3 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 @@ -8,7 +8,11 @@ public class InngestFunctionTestHelpers { static SendEventsResponse sendEvent(Inngest inngest, String eventName) { - InngestEvent event = new InngestEvent(eventName, new HashMap()); + return sendEvent(inngest, eventName, new HashMap<>()); + } + + static SendEventsResponse sendEvent(Inngest inngest, String eventName, HashMap data) { + InngestEvent event = new InngestEvent(eventName, data); SendEventsResponse response = inngest.send(event); assert Objects.requireNonNull(response).getIds().length > 0; 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()); + } +}