Search in sources :

Example 1 with ScriptHost

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();
}
Also used : Script(org.projectnessie.cel.tools.Script) MetaTest(org.projectnessie.cel.types.jackson.types.MetaTest) ScriptHost(org.projectnessie.cel.tools.ScriptHost) Test(org.junit.jupiter.api.Test) MetaTest(org.projectnessie.cel.types.jackson.types.MetaTest)

Example 2 with ScriptHost

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());
}
Also used : Script(org.projectnessie.cel.tools.Script) ObjectListEnum(org.projectnessie.cel.types.jackson.types.ObjectListEnum) ScriptHost(org.projectnessie.cel.tools.ScriptHost) ClassWithEnum(org.projectnessie.cel.types.jackson.types.ClassWithEnum) Test(org.junit.jupiter.api.Test) MetaTest(org.projectnessie.cel.types.jackson.types.MetaTest)

Aggregations

Test (org.junit.jupiter.api.Test)2 Script (org.projectnessie.cel.tools.Script)2 ScriptHost (org.projectnessie.cel.tools.ScriptHost)2 MetaTest (org.projectnessie.cel.types.jackson.types.MetaTest)2 ClassWithEnum (org.projectnessie.cel.types.jackson.types.ClassWithEnum)1 ObjectListEnum (org.projectnessie.cel.types.jackson.types.ObjectListEnum)1