-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration test for
step.invoke
(#78)
- Loading branch information
Showing
5 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
...t-demo/src/main/java/com/inngest/springbootdemo/testfunctions/MultiplyMatrixFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<Double>> execute(FunctionContext ctx, Step step) { | ||
List<List<Integer>> A = (List<List<Integer>>) ctx.getEvent().getData().get("matrixA"); | ||
List<List<Integer>> B = (List<List<Integer>>) ctx.getEvent().getData().get("matrixB"); | ||
|
||
return step.run("multiply-matrix", () -> | ||
{ | ||
List<List<Double>> result = new ArrayList<>(); | ||
for (List<Integer> integers : A) { | ||
List<Double> 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); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/Scale2DObjectFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<Double>> execute(FunctionContext ctx, Step step) { | ||
List<List<Integer>> matrix = (List<List<Integer>>) ctx.getEvent().getData().get("matrix"); | ||
int scaleX = (int) ctx.getEvent().getData().get("scaleX"); | ||
int scaleY = (int) ctx.getEvent().getData().get("scaleY"); | ||
|
||
List<List<Integer>> scalingMatrix = new ArrayList<>(Arrays.asList( | ||
new ArrayList<>(Arrays.asList(scaleX, 0)), | ||
new ArrayList<>(Arrays.asList(0, scaleY)) | ||
)); | ||
|
||
LinkedHashMap<String, Object> eventData = new LinkedHashMap<String, Object>() {{ | ||
put("matrixA", matrix); | ||
put("matrixB", scalingMatrix); | ||
}}; | ||
|
||
return step.invoke( | ||
"multiply-matrix", | ||
"spring_test_demo", | ||
"MultiplyMatrixFunction", | ||
eventData, | ||
null, | ||
List.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...ing-boot-demo/src/test/java/com/inngest/springbootdemo/InvokeFunctionIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ArrayList<Integer>> 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<String, Object> eventData = new LinkedHashMap<String, Object>() {{ | ||
put("matrix", square); | ||
put("scaleX", 2); | ||
put("scaleY", 3); | ||
}}; | ||
|
||
String eventId = InngestFunctionTestHelpers.sendEvent(client, "test/scale2d.object", eventData).getIds()[0]; | ||
|
||
Thread.sleep(sleepTime); | ||
|
||
RunEntry<Object> 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()); | ||
} | ||
} |