Skip to content

Commit

Permalink
Fix IT
Browse files Browse the repository at this point in the history
  • Loading branch information
eolivelli committed Dec 13, 2023
1 parent 072a2ad commit a1f1f3c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public JstlEvaluator(String expression, Class<? extends T> type) {

@SneakyThrows
private void registerFunctions() {
this.expressionContext
.getFunctionMapper()
.mapFunction("fn", "length", JstlFunctions.class.getMethod("length", Object.class));
this.expressionContext
.getFunctionMapper()
.mapFunction("fn", "toJson", JstlFunctions.class.getMethod("toJson", Object.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ public static Map<String, Object> emptyMap() {
return Map.of();
}

public static long length(Object o) {
return o == null ? 0 : toString(o).length();
}

public static Map<String, Object> mapOf(Object... field) {
Map<String, Object> result = new HashMap<>();
for (int i = 0; i < field.length; i += 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ void testPrimitiveValue() {
assertEquals("test-message", value);
}

@Test
void testLength() {
MutableRecord primitiveStringContext =
Utils.createContextWithPrimitiveRecord(Schema.STRING, "test-message", "");

String value =
new JstlEvaluator<>("${fn:length(value)}", String.class)
.evaluate(primitiveStringContext);

assertEquals("12", value);
}

@Test
void testNowFunction() {
MutableRecord primitiveStringContext =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ public void testSource() {
.split(" "));
log.info("Output: {}", output);
String bigPayload = Strings.repeat("test", 10000);
String value = "the length is " + bigPayload.length();
Assertions.assertTrue(
output.contains(
"{\"record\":{\"key\":null,\"value\":\""
+ bigPayload
+ "\",\"headers\":{}}"));
"{\"record\":{\"key\":null,\"value\":\"" + value + "\",\"headers\":{}}"),
"Output doesn't contain the expected payload: " + output);

deleteAppAndAwaitCleanup(tenant, applicationId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ topics:
creation-mode: create-if-not-exists
schema:
type: string
resources:
size: 2
pipeline:
- name: "Source using Python"
resources:
size: 2
id: "test-python-source"
type: "python-source"
configuration:
className: example.TestSource
- name: "Compute length"
id: "test-python-source"
type: "compute"
output: ls-test-output
configuration:
className: example.TestSource
fields:
- name: "value"
expression: "fn:concat('the length is ', fn:len(value))"

0 comments on commit a1f1f3c

Please sign in to comment.