Search in sources :

Example 1 with ObjectT

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

the class PbObjectTest method protoObjectIsSet.

@Test
void protoObjectIsSet() {
    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;
    assertThat(objVal.isSet(stringOf("source_info"))).isSameAs(True);
    assertThat(objVal.isSet(stringOf("expr"))).isSameAs(False);
    assertThat(objVal.isSet(stringOf("bad_field"))).matches(Err::isError);
    assertThat(objVal.isSet(IntZero)).matches(Err::isError);
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) Err(org.projectnessie.cel.common.types.Err) 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 2 with ObjectT

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

the class JacksonTypeDescriptionTest method collectionsObjectTypeTest.

@Test
void collectionsObjectTypeTest() throws Exception {
    CollectionsObject collectionsObject = new CollectionsObject();
    // populate (primitive) map types
    collectionsObject.stringBooleanMap = singletonMap("a", true);
    collectionsObject.byteShortMap = singletonMap((byte) 1, (short) 2);
    collectionsObject.intLongMap = singletonMap(1, 2L);
    collectionsObject.ulongTimestampMap = singletonMap(ULong.valueOf(1), Timestamp.newBuilder().setSeconds(1).build());
    collectionsObject.ulongZonedDateTimeMap = singletonMap(ULong.valueOf(1), ZonedDateTime.of(LocalDateTime.ofEpochSecond(1, 0, ZoneOffset.UTC), ZoneId.of("UTC")));
    collectionsObject.stringProtoDurationMap = singletonMap("a", Duration.newBuilder().setSeconds(1).build());
    collectionsObject.stringJavaDurationMap = singletonMap("a", java.time.Duration.ofSeconds(1));
    collectionsObject.stringBytesMap = singletonMap("a", ByteString.copyFrom(new byte[] { (byte) 1 }));
    collectionsObject.floatDoubleMap = singletonMap(1f, 2d);
    // populate (primitive) list types
    collectionsObject.stringList = asList("a", "b", "c");
    collectionsObject.booleanList = asList(true, true, false, false);
    collectionsObject.byteList = asList((byte) 1, (byte) 2, (byte) 3);
    collectionsObject.shortList = asList((short) 4, (short) 5, (short) 6);
    collectionsObject.intList = asList(7, 8, 9);
    collectionsObject.longList = asList(10L, 11L, 12L);
    collectionsObject.ulongList = asList(ULong.valueOf(1), ULong.valueOf(2), ULong.valueOf(3));
    collectionsObject.timestampList = asList(Timestamp.newBuilder().setSeconds(1).build(), Timestamp.newBuilder().setSeconds(2).build(), Timestamp.newBuilder().setSeconds(3).build());
    collectionsObject.zonedDateTimeList = asList(ZonedDateTime.of(LocalDateTime.ofEpochSecond(1, 0, ZoneOffset.UTC), ZoneId.of("UTC")), ZonedDateTime.of(LocalDateTime.ofEpochSecond(2, 0, ZoneOffset.UTC), ZoneId.of("UTC")), ZonedDateTime.of(LocalDateTime.ofEpochSecond(3, 0, ZoneOffset.UTC), ZoneId.of("UTC")));
    collectionsObject.durationList = asList(Duration.newBuilder().setSeconds(1).build(), Duration.newBuilder().setSeconds(2).build(), Duration.newBuilder().setSeconds(3).build());
    collectionsObject.javaDurationList = asList(java.time.Duration.ofSeconds(1), java.time.Duration.ofSeconds(2), java.time.Duration.ofSeconds(3));
    collectionsObject.bytesList = asList(ByteString.copyFrom(new byte[] { (byte) 1 }), ByteString.copyFrom(new byte[] { (byte) 2 }), ByteString.copyFrom(new byte[] { (byte) 3 }));
    collectionsObject.floatList = asList(1f, 2f, 3f);
    collectionsObject.doubleList = asList(1d, 2d, 3d);
    // populate inner/nested type list/map
    InnerType inner1 = new InnerType();
    inner1.intProp = 1;
    inner1.wrappedIntProp = 2;
    collectionsObject.stringInnerMap = singletonMap("a", inner1);
    InnerType inner2 = new InnerType();
    inner2.intProp = 3;
    inner2.wrappedIntProp = 4;
    collectionsObject.innerTypes = asList(inner1, inner2);
    // populate enum-related fields
    collectionsObject.anEnum = AnEnum.ENUM_VALUE_2;
    collectionsObject.anEnumList = asList(AnEnum.ENUM_VALUE_2, AnEnum.ENUM_VALUE_3);
    collectionsObject.anEnumStringMap = singletonMap(AnEnum.ENUM_VALUE_2, "a");
    collectionsObject.stringAnEnumMap = singletonMap("a", AnEnum.ENUM_VALUE_2);
    // prepare registry
    JacksonRegistry reg = (JacksonRegistry) newRegistry();
    reg.register(CollectionsObject.class);
    Val collectionsVal = reg.nativeToValue(collectionsObject);
    assertThat(collectionsVal).isInstanceOf(ObjectT.class);
    ObjectT obj = (ObjectT) collectionsVal;
    for (String field : CollectionsObject.ALL_PROPERTIES) {
        assertThat(obj.isSet(stringOf(field))).isSameAs(True);
        assertThat(obj.get(stringOf(field))).isNotNull();
        Val fieldVal = obj.get(stringOf(field));
        Object fieldObj = CollectionsObject.class.getDeclaredField(field).get(collectionsObject);
        if (fieldObj instanceof Map) {
            assertThat(fieldVal).isInstanceOf(MapT.class);
        } else if (fieldObj instanceof List) {
            assertThat(fieldVal).isInstanceOf(ListT.class);
        }
        assertThat(fieldVal.equal(reg.nativeToValue(fieldObj))).isSameAs(True);
    }
    // check a few properties manually/explicitly
    MapT mapVal = (MapT) obj.get(stringOf("intLongMap"));
    assertThat(mapVal).extracting(MapT::size, m -> m.contains(intOf(42)), m -> m.contains(intOf(1)), m -> m.contains(intOf(2)), m -> m.contains(intOf(3)), m -> m.get(intOf(1))).containsExactly(intOf(1), False, True, False, False, intOf(2));
    ListT listVal = (ListT) obj.get(stringOf("ulongList"));
    assertThat(listVal).extracting(ListT::size, l -> l.contains(uintOf(42)), l -> l.contains(uintOf(1)), l -> l.contains(uintOf(2)), l -> l.contains(uintOf(3)), l -> l.get(intOf(0)), l -> l.get(intOf(1)), l -> l.get(intOf(2))).containsExactly(intOf(3), False, True, True, True, uintOf(1), uintOf(2), uintOf(3));
    mapVal = (MapT) obj.get(stringOf("stringInnerMap"));
    assertThat(mapVal).extracting(MapT::size, m -> m.contains(stringOf("42")), m -> m.contains(stringOf("a"))).containsExactly(intOf(1), False, True);
    ObjectT i = (ObjectT) mapVal.get(stringOf("a"));
    assertThat(i).extracting(o -> o.get(stringOf("intProp")), o -> o.get(stringOf("wrappedIntProp"))).containsExactly(intOf(1), intOf(2));
    listVal = (ListT) obj.get(stringOf("innerTypes"));
    assertThat(listVal).extracting(ListT::size).isEqualTo(intOf(2));
    i = (ObjectT) listVal.get(intOf(0));
    assertThat(i).extracting(o -> o.get(stringOf("intProp")), o -> o.get(stringOf("wrappedIntProp"))).containsExactly(intOf(1), intOf(2));
    i = (ObjectT) listVal.get(intOf(1));
    assertThat(i).extracting(o -> o.get(stringOf("intProp")), o -> o.get(stringOf("wrappedIntProp"))).containsExactly(intOf(3), intOf(4));
    // verify enums
    Val x = obj.get(stringOf("anEnum"));
    assertThat(x).isInstanceOf(IntT.class).isEqualTo(intOf(AnEnum.ENUM_VALUE_2.ordinal()));
    listVal = (ListT) obj.get(stringOf("anEnumList"));
    assertThat(listVal).extracting(l -> l.get(intOf(0)), l -> l.get(intOf(1))).containsExactly(intOf(AnEnum.ENUM_VALUE_2.ordinal()), intOf(AnEnum.ENUM_VALUE_3.ordinal()));
    mapVal = (MapT) obj.get(stringOf("anEnumStringMap"));
    assertThat(mapVal).extracting(l -> l.get(intOf(AnEnum.ENUM_VALUE_2.ordinal()))).isEqualTo(stringOf("a"));
    mapVal = (MapT) obj.get(stringOf("stringAnEnumMap"));
    assertThat(mapVal).extracting(l -> l.get(stringOf("a"))).isEqualTo(intOf(AnEnum.ENUM_VALUE_2.ordinal()));
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) ListT(org.projectnessie.cel.common.types.ListT) MapT(org.projectnessie.cel.common.types.MapT) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) AnEnum(org.projectnessie.cel.types.jackson.types.AnEnum) IntT(org.projectnessie.cel.common.types.IntT) ULong(org.projectnessie.cel.common.ULong) Timestamp(com.google.protobuf.Timestamp) ListType(com.google.api.expr.v1alpha1.Type.ListType) Err(org.projectnessie.cel.common.types.Err) True(org.projectnessie.cel.common.types.BoolT.True) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) JavaType(com.fasterxml.jackson.databind.JavaType) ZoneOffset(java.time.ZoneOffset) Val(org.projectnessie.cel.common.types.ref.Val) JacksonRegistry.newRegistry(org.projectnessie.cel.types.jackson.JacksonRegistry.newRegistry) MapType(com.google.api.expr.v1alpha1.Type.MapType) False(org.projectnessie.cel.common.types.BoolT.False) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) NullT(org.projectnessie.cel.common.types.NullT) IntT.intOf(org.projectnessie.cel.common.types.IntT.intOf) ZoneId(java.time.ZoneId) UintT.uintOf(org.projectnessie.cel.common.types.UintT.uintOf) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) CollectionsObject(org.projectnessie.cel.types.jackson.types.CollectionsObject) TypeT(org.projectnessie.cel.common.types.TypeT) InnerType(org.projectnessie.cel.types.jackson.types.InnerType) Duration(com.google.protobuf.Duration) List(java.util.List) ObjectT(org.projectnessie.cel.common.types.ObjectT) Checked(org.projectnessie.cel.common.types.pb.Checked) TypeKindCase(com.google.api.expr.v1alpha1.Type.TypeKindCase) IntT(org.projectnessie.cel.common.types.IntT) InnerType(org.projectnessie.cel.types.jackson.types.InnerType) ByteString(com.google.protobuf.ByteString) CollectionsObject(org.projectnessie.cel.types.jackson.types.CollectionsObject) ListT(org.projectnessie.cel.common.types.ListT) ObjectT(org.projectnessie.cel.common.types.ObjectT) CollectionsObject(org.projectnessie.cel.types.jackson.types.CollectionsObject) Arrays.asList(java.util.Arrays.asList) List(java.util.List) MapT(org.projectnessie.cel.common.types.MapT) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Test(org.junit.jupiter.api.Test)

Example 3 with ObjectT

use of org.projectnessie.cel.common.types.ObjectT 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 4 with ObjectT

use of org.projectnessie.cel.common.types.ObjectT 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 5 with ObjectT

use of org.projectnessie.cel.common.types.ObjectT 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)

Aggregations

Test (org.junit.jupiter.api.Test)9 ObjectT (org.projectnessie.cel.common.types.ObjectT)9 Val (org.projectnessie.cel.common.types.ref.Val)9 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)6 Err (org.projectnessie.cel.common.types.Err)4 ParsedExpr (com.google.api.expr.v1alpha1.ParsedExpr)3 CollectionsObject (org.projectnessie.cel.types.jackson.types.CollectionsObject)3 MetaTest (org.projectnessie.cel.types.jackson.types.MetaTest)3 ByteString (com.google.protobuf.ByteString)2 Instant (java.time.Instant)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 ListType (com.google.api.expr.v1alpha1.Type.ListType)1 MapType (com.google.api.expr.v1alpha1.Type.MapType)1 TypeKindCase (com.google.api.expr.v1alpha1.Type.TypeKindCase)1 Duration (com.google.protobuf.Duration)1 Timestamp (com.google.protobuf.Timestamp)1 LocalDateTime (java.time.LocalDateTime)1 ZoneId (java.time.ZoneId)1 ZoneOffset (java.time.ZoneOffset)1 ZonedDateTime (java.time.ZonedDateTime)1