-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add end-to-end test for custom function
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
jackson-jq/src/test/java/net/thisptr/jackson/jq/CustomFunctionTest.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,51 @@ | ||
package net.thisptr.jackson.jq; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.IntNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import net.thisptr.jackson.jq.exception.JsonQueryException; | ||
import net.thisptr.jackson.jq.path.Path; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
// end-to-end test of custom function | ||
public class CustomFunctionTest { | ||
|
||
@Test | ||
public void testCustomFunction() throws Exception { | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
Version version = Versions.JQ_1_6; | ||
|
||
Scope rootScope = Scope.newEmptyScope(); | ||
|
||
BuiltinFunctionLoader.getInstance().loadFunctions(version, rootScope); | ||
|
||
rootScope.addFunction("times100", 1, new Function() { | ||
@Override | ||
public void apply(Scope scope, List<Expression> args, JsonNode in, Path path, PathOutput output, Version version) throws JsonQueryException { | ||
args.get(0).apply(scope, in, (numberNode) -> { | ||
assert (numberNode.isIntegralNumber()); | ||
output.emit(new IntNode(numberNode.asInt() * 100), null); | ||
}); | ||
} | ||
}); | ||
|
||
String input = "{ \"a\": 5 }"; | ||
|
||
Scope childScope = Scope.newChildScope(rootScope); | ||
|
||
JsonQuery query = JsonQuery.compile("{ \"a\": times100(.a) }", version); | ||
|
||
final List<JsonNode> out = new ArrayList<>(); | ||
query.apply(childScope, mapper.readTree(input), out::add); | ||
assertThat(out).hasSize(1); | ||
assertThat(out.get(0)).isInstanceOf(ObjectNode.class); | ||
assertThat(out.get(0).toString()).isEqualTo("{\"a\":500}"); | ||
} | ||
} |