Search in sources :

Example 46 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class JacksonRegistryTest method nessieCommitMetaPart.

@Test
void nessieCommitMetaPart() {
    TypeRegistry reg = JacksonRegistry.newRegistry();
    Instant now = Instant.now();
    MetaTest cm = MetaTest.builder().commitTime(now).committer("committer@projectnessie.org").hash("beeffeed123412341234123412341234").message("Feed of beef").build();
    Val cmVal = reg.nativeToValue(cm);
    assertThat(cmVal).isInstanceOf(ObjectT.class);
    assertThat(cmVal.type().typeEnum()).isSameAs(TypeEnum.Object);
    assertThat(cmVal.type().typeName()).isEqualTo(cm.getClass().getName());
    assertThat(cmVal.type().typeEnum()).isSameAs(TypeEnum.Object);
    ObjectT cmObj = (ObjectT) cmVal;
    assertThat(cmObj.isSet(stringOf("foo"))).isInstanceOf(Err.class).asString().isEqualTo("no such field 'foo'");
    assertThat(cmObj.isSet(stringOf("commitTime"))).isEqualTo(True);
    assertThat(cmObj.isSet(stringOf("authorTime"))).isEqualTo(False);
    assertThat(cmObj.isSet(stringOf("committer"))).isEqualTo(True);
    assertThat(cmObj.isSet(stringOf("author"))).isEqualTo(False);
    assertThat(cmObj.isSet(stringOf("hash"))).isEqualTo(True);
    assertThat(cmObj.isSet(stringOf("message"))).isEqualTo(True);
    assertThat(cmObj.isSet(stringOf("signedOffBy"))).isEqualTo(False);
    // just empty
    assertThat(cmObj.isSet(stringOf("properties"))).isEqualTo(True);
    assertThat(cmObj.get(stringOf("foo"))).isInstanceOf(Err.class).asString().isEqualTo("no such field 'foo'");
    assertThat(cmObj.get(stringOf("commitTime"))).isEqualTo(timestampOf(now));
    assertThat(cmObj.get(stringOf("authorTime"))).isEqualTo(NullValue);
    assertThat(cmObj.get(stringOf("committer"))).isEqualTo(stringOf("committer@projectnessie.org"));
    assertThat(cmObj.get(stringOf("author"))).isEqualTo(NullValue);
    assertThat(cmObj.get(stringOf("hash"))).isEqualTo(stringOf("beeffeed123412341234123412341234"));
    assertThat(cmObj.get(stringOf("message"))).isEqualTo(stringOf("Feed of beef"));
    assertThat(cmObj.get(stringOf("signedOffBy"))).isEqualTo(NullValue);
    assertThat(cmObj.get(stringOf("properties"))).isEqualTo(newMaybeWrappedMap(reg, emptyMap()));
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Instant(java.time.Instant) MetaTest(org.projectnessie.cel.types.jackson.types.MetaTest) ObjectT(org.projectnessie.cel.common.types.ObjectT) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) MetaTest(org.projectnessie.cel.types.jackson.types.MetaTest) Test(org.junit.jupiter.api.Test)

Example 47 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class JacksonRegistryTest method nessieCommitMetaFull.

@Test
void nessieCommitMetaFull() {
    TypeRegistry reg = JacksonRegistry.newRegistry();
    Instant now = Instant.now();
    Instant nowMinus5 = now.minus(5, ChronoUnit.MINUTES);
    MetaTest cm = MetaTest.builder().commitTime(now).authorTime(nowMinus5).committer("committer@projectnessie.org").author("author@projectnessie.org").hash("beeffeed123412341234123412341234").message("Feed of beef").signedOffBy("signed-off@projectnessie.org").putProperties("prop-1", "value-1").putProperties("prop-2", "value-2").build();
    Val cmVal = reg.nativeToValue(cm);
    assertThat(cmVal).isInstanceOf(ObjectT.class);
    assertThat(cmVal.type().typeEnum()).isSameAs(TypeEnum.Object);
    assertThat(cmVal.type().typeName()).isEqualTo(cm.getClass().getName());
    assertThat(cmVal.type().typeEnum()).isSameAs(TypeEnum.Object);
    ObjectT cmObj = (ObjectT) cmVal;
    assertThat(cmObj.isSet(stringOf("foo"))).isInstanceOf(Err.class).asString().isEqualTo("no such field 'foo'");
    assertThat(cmObj.isSet(stringOf("commitTime"))).isEqualTo(True);
    assertThat(cmObj.isSet(stringOf("authorTime"))).isEqualTo(True);
    assertThat(cmObj.isSet(stringOf("committer"))).isEqualTo(True);
    assertThat(cmObj.isSet(stringOf("author"))).isEqualTo(True);
    assertThat(cmObj.isSet(stringOf("hash"))).isEqualTo(True);
    assertThat(cmObj.isSet(stringOf("message"))).isEqualTo(True);
    assertThat(cmObj.isSet(stringOf("signedOffBy"))).isEqualTo(True);
    assertThat(cmObj.isSet(stringOf("properties"))).isEqualTo(True);
    Map<String, String> expectMap = new HashMap<>();
    expectMap.put("prop-1", "value-1");
    expectMap.put("prop-2", "value-2");
    assertThat(cmObj.get(stringOf("foo"))).isInstanceOf(Err.class).asString().isEqualTo("no such field 'foo'");
    assertThat(cmObj.get(stringOf("commitTime"))).isEqualTo(timestampOf(now));
    assertThat(cmObj.get(stringOf("authorTime"))).isEqualTo(timestampOf(nowMinus5));
    assertThat(cmObj.get(stringOf("committer"))).isEqualTo(stringOf("committer@projectnessie.org"));
    assertThat(cmObj.get(stringOf("author"))).isEqualTo(stringOf("author@projectnessie.org"));
    assertThat(cmObj.get(stringOf("hash"))).isEqualTo(stringOf("beeffeed123412341234123412341234"));
    assertThat(cmObj.get(stringOf("message"))).isEqualTo(stringOf("Feed of beef"));
    assertThat(cmObj.get(stringOf("signedOffBy"))).isEqualTo(stringOf("signed-off@projectnessie.org"));
    assertThat(cmObj.get(stringOf("properties"))).isEqualTo(newMaybeWrappedMap(reg, expectMap));
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) HashMap(java.util.HashMap) Instant(java.time.Instant) MetaTest(org.projectnessie.cel.types.jackson.types.MetaTest) ObjectT(org.projectnessie.cel.common.types.ObjectT) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) MetaTest(org.projectnessie.cel.types.jackson.types.MetaTest) Test(org.junit.jupiter.api.Test)

Example 48 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class JacksonRegistryTest method nessieBranch.

@Test
void nessieBranch() {
    TypeRegistry reg = JacksonRegistry.newRegistry();
    RefVariantB refVariantB = RefVariantB.of("main", "cafebabe123412341234123412341234");
    Val branchVal = reg.nativeToValue(refVariantB);
    assertThat(branchVal).isInstanceOf(ObjectT.class);
    assertThat(branchVal.type().typeEnum()).isSameAs(TypeEnum.Object);
    assertThat(branchVal.type().typeName()).isEqualTo(refVariantB.getClass().getName());
    ObjectT branchObj = (ObjectT) branchVal;
    assertThat(branchObj.isSet(stringOf("foo"))).isInstanceOf(Err.class).asString().isEqualTo("no such field 'foo'");
    assertThat(branchObj.isSet(stringOf("name"))).isEqualTo(True);
    assertThat(branchObj.isSet(stringOf("hash"))).isEqualTo(True);
    assertThat(branchObj.get(stringOf("foo"))).isInstanceOf(Err.class).asString().isEqualTo("no such field 'foo'");
    assertThat(branchObj.get(stringOf("name"))).isEqualTo(stringOf("main"));
    assertThat(branchObj.get(stringOf("hash"))).isEqualTo(stringOf("cafebabe123412341234123412341234"));
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) ObjectT(org.projectnessie.cel.common.types.ObjectT) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) RefVariantB(org.projectnessie.cel.types.jackson.types.RefVariantB) MetaTest(org.projectnessie.cel.types.jackson.types.MetaTest) Test(org.junit.jupiter.api.Test)

Example 49 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class PbObjectTest method protoObjectConvertToType.

@Test
void protoObjectConvertToType() {
    TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
    ParsedExpr msg = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
    Val obj = reg.nativeToValue(msg);
    assertThat(obj).isInstanceOf(ObjectT.class);
    ObjectT objVal = (ObjectT) obj;
    Type tv = objVal.type();
    assertThat(objVal.convertToType(TypeType).equal(tv)).isSameAs(True);
    assertThat(objVal.convertToType(objVal.type())).isSameAs(objVal);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) TypeType(org.projectnessie.cel.common.types.TypeT.TypeType) Type(org.projectnessie.cel.common.types.ref.Type) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) ObjectT(org.projectnessie.cel.common.types.ObjectT) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test)

Example 50 with TypeRegistry

use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.

the class PbObjectTest method protoObjectConvertToNative.

@Test
void protoObjectConvertToNative() throws Exception {
    TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
    ParsedExpr msg = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
    Val objVal = reg.nativeToValue(msg);
    // Proto Message
    ParsedExpr val = objVal.convertToNative(ParsedExpr.class);
    assertThat(val).isEqualTo(msg);
    // Dynamic protobuf
    Val dynPB = reg.newValue(msg.getDescriptorForType().getFullName(), Collections.singletonMap("source_info", reg.nativeToValue(msg.getSourceInfo())));
    Val dynVal = reg.nativeToValue(dynPB);
    val = dynVal.convertToNative(msg.getClass());
    assertThat(val).isEqualTo(msg);
    // google.protobuf.Any
    Any anyVal = objVal.convertToNative(Any.class);
    Message unpackedAny = anyVal.unpack(ParsedExpr.class);
    assertThat(unpackedAny).isEqualTo(objVal.value());
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Message(com.google.protobuf.Message) ParsedExpr(com.google.api.expr.v1alpha1.ParsedExpr) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Any(com.google.protobuf.Any) Test(org.junit.jupiter.api.Test)

Aggregations

TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)51 Test (org.junit.jupiter.api.Test)40 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)26 AttributeFactory.newAttributeFactory (org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory)25 Activation.newActivation (org.projectnessie.cel.interpreter.Activation.newActivation)21 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)21 Val (org.projectnessie.cel.common.types.ref.Val)20 NamespacedAttribute (org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute)20 Activation.emptyActivation (org.projectnessie.cel.interpreter.Activation.emptyActivation)19 Qualifier (org.projectnessie.cel.interpreter.AttributeFactory.Qualifier)16 Container (org.projectnessie.cel.common.containers.Container)14 Container.newContainer (org.projectnessie.cel.common.containers.Container.newContainer)12 Attribute (org.projectnessie.cel.interpreter.AttributeFactory.Attribute)12 MethodSource (org.junit.jupiter.params.provider.MethodSource)11 ProtoTypeRegistry (org.projectnessie.cel.common.types.pb.ProtoTypeRegistry)10 ParseResult (org.projectnessie.cel.parser.Parser.ParseResult)10 Source (org.projectnessie.cel.common.Source)9 Interpreter.newStandardInterpreter (org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter)9 Source.newTextSource (org.projectnessie.cel.common.Source.newTextSource)8