use of org.projectnessie.cel.tools.ScriptHost in project cel-java by projectnessie.
the class JacksonScriptHostTest method simple.
@Test
void simple() throws Exception {
ScriptHost scriptHost = ScriptHost.newBuilder().registry(JacksonRegistry.newRegistry()).build();
Script script = scriptHost.buildScript("param.author == 'foo@bar.baz'").withDeclarations(Decls.newVar("param", Decls.newObjectType(MetaTest.class.getName()))).withTypes(MetaTest.class).build();
MetaTest cmMatch = MetaTest.builder().author("foo@bar.baz").build();
MetaTest cmNoMatch = MetaTest.builder().author("foo@foo.foo").build();
assertThat(script.execute(Boolean.class, singletonMap("param", cmMatch))).isTrue();
assertThat(script.execute(Boolean.class, singletonMap("param", cmNoMatch))).isFalse();
}
use of org.projectnessie.cel.tools.ScriptHost in project cel-java by projectnessie.
the class JacksonScriptHostTest method complexInput.
@Test
void complexInput() throws Exception {
ScriptHost scriptHost = ScriptHost.newBuilder().registry(JacksonRegistry.newRegistry()).build();
Script script = scriptHost.buildScript("param.entries[0].type == org.projectnessie.cel.types.jackson.types.ClassWithEnum.ClassEnum.VAL_2").withDeclarations(Decls.newVar("param", Decls.newObjectType(ObjectListEnum.class.getName()))).withTypes(ObjectListEnum.class).build();
ObjectListEnum val = ObjectListEnum.builder().addEntries(ObjectListEnum.Entry.builder().type(ClassEnum.VAL_2).holder(new ClassWithEnum("foo")).build()).build();
assertThat(script.execute(Boolean.class, singletonMap("param", val))).isTrue();
// same as above, but use the 'container'
script = scriptHost.buildScript("param.entries[0].type == ClassWithEnum.ClassEnum.VAL_2").withDeclarations(Decls.newVar("param", Decls.newObjectType(ObjectListEnum.class.getName()))).withContainer("org.projectnessie.cel.types.jackson.types").withTypes(ObjectListEnum.class).build();
assertThat(script.execute(Boolean.class, singletonMap("param", val))).isTrue();
// return the enum
script = scriptHost.buildScript("param.entries[0].type").withDeclarations(Decls.newVar("param", Decls.newObjectType(ObjectListEnum.class.getName()))).withContainer("org.projectnessie.cel.types.jackson.types").withTypes(ObjectListEnum.class).build();
assertThat(script.execute(Integer.class, singletonMap("param", val))).isEqualTo(ClassEnum.VAL_2.ordinal());
}
Aggregations