Skip to content

Add protolite tests for Android #673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions .github/workflows/unwanted_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Script ran as part of Github CEL-Java CI to verify that the runtime jar does not contain generated cel protos from @cel_spec.
# Script ran as part of Github CEL-Java CI to verify that the runtime jar does not contain unwanted dependencies.

runtime_deps="$(bazel query 'deps(//publish:cel_runtime)' --nohost_deps --noimplicit_deps --output graph | grep '@cel_spec')"
UNWANTED_DEPS=(
'@cel_spec' # Do not include generated CEL protos in the jar
'protobuf_java_util' # Does not support protolite
)

if [[ ! -z $runtime_deps ]]; then
echo -e "Runtime contains unwanted @cel_spec dependency!\n"
echo "cel_spec dependency graph:"
echo -e "$runtime_deps"
exit 1
fi
runtime_deps="$(bazel query 'deps(//publish:cel_runtime)' --nohost_deps --noimplicit_deps --output graph)"

for unwanted_dep in "${UNWANTED_DEPS[@]}"; do
if echo "$runtime_deps" | grep "$unwanted_dep" > /dev/null; then
echo -e "Runtime contains unwanted dependency: $unwanted_dep!\n"
echo "cel_spec dependency graph (including '$unwanted_dep'):"
echo "$(echo "$runtime_deps" | grep "$unwanted_dep")"
exit 1
fi
done

exit 0

2 changes: 1 addition & 1 deletion bundle/src/test/java/dev/cel/bundle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ java_library(
"//common:proto_ast",
"//common:source_location",
"//common/ast",
"//common/internal:proto_time_utils",
"//common/resources/testdata/proto3:standalone_global_enum_java_proto",
"//common/testing",
"//common/types",
Expand All @@ -55,7 +56,6 @@ java_library(
"@com_google_googleapis//google/type:type_java_proto",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:com_google_protobuf_protobuf_java_util",
"@maven//:com_google_testparameterinjector_test_parameter_injector",
"@maven//:com_google_truth_extensions_truth_proto_extension",
"@maven//:junit_junit",
Expand Down
9 changes: 5 additions & 4 deletions bundle/src/test/java/dev/cel/bundle/CelImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import com.google.protobuf.Struct;
import com.google.protobuf.TextFormat;
import com.google.protobuf.Timestamp;
import com.google.protobuf.util.Timestamps;
import com.google.rpc.context.AttributeContext;
import com.google.testing.junit.testparameterinjector.TestParameter;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
Expand All @@ -71,6 +70,7 @@
import dev.cel.common.CelVarDecl;
import dev.cel.common.ast.CelExpr;
import dev.cel.common.ast.CelExpr.CelList;
import dev.cel.common.internal.ProtoTimeUtils;
import dev.cel.common.testing.RepeatedTestProvider;
import dev.cel.common.types.CelKind;
import dev.cel.common.types.CelProtoMessageTypes;
Expand Down Expand Up @@ -409,6 +409,7 @@ public void program_setTypeFactoryOnAnyPackedMessage_messageConstructionSucceeds
}

@Test
@SuppressWarnings("unused") // testRunIndex name retained for test result readability
public void program_concurrentMessageConstruction_succeeds(
@TestParameter(valuesProvider = RepeatedTestProvider.class) int testRunIndex)
throws Exception {
Expand Down Expand Up @@ -814,7 +815,7 @@ public void program_duplicateTypeDescriptor() throws Exception {
.build();
CelRuntime.Program program =
cel.createProgram(cel.compile("protobuf.Timestamp{seconds: 12}").getAst());
assertThat(program.eval()).isEqualTo(Timestamps.fromSeconds(12));
assertThat(program.eval()).isEqualTo(ProtoTimeUtils.fromSecondsToTimestamp(12));
}

@Test
Expand All @@ -827,7 +828,7 @@ public void program_hermeticDescriptors_wellKnownProtobuf() throws Exception {
.build();
CelRuntime.Program program =
cel.createProgram(cel.compile("protobuf.Timestamp{seconds: 12}").getAst());
assertThat(program.eval()).isEqualTo(Timestamps.fromSeconds(12));
assertThat(program.eval()).isEqualTo(ProtoTimeUtils.fromSecondsToTimestamp(12));
}

@Test
Expand Down Expand Up @@ -956,7 +957,7 @@ public void program_typeProvider() throws Exception {
.build();
CelRuntime.Program program =
cel.createProgram(cel.compile("protobuf.Timestamp{seconds: 12}").getAst());
assertThat(program.eval()).isEqualTo(Timestamps.fromSeconds(12));
assertThat(program.eval()).isEqualTo(ProtoTimeUtils.fromSecondsToTimestamp(12));
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions codelab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ public void evaluate_jwtWithTimeVariable_producesJsonString() throws Exception {
@SuppressWarnings("unchecked")
Map<String, Object> evaluatedResult =
(Map<String, Object>)
exercise5.eval(ast, ImmutableMap.of("time", Timestamps.fromSeconds(1698361778)));
exercise5.eval(ast, ImmutableMap.of("time", ProtoTimeUtils.fromSecondsToTimestamp(1698361778)));
String jsonOutput = exercise5.toJson(evaluatedResult);

assertThat(jsonOutput)
Expand Down Expand Up @@ -776,7 +776,7 @@ public void evaluate_constructAttributeContext() {
+ "time: now"
+ "}";
// Values for `now` and `jwt` variables to be passed into the runtime
Timestamp now = Timestamps.now();
Timestamp now = ProtoTimeUtils.now();
ImmutableMap<String, Object> jwt =
ImmutableMap.of(
"sub", "serviceAccount:[email protected]",
Expand Down
4 changes: 2 additions & 2 deletions codelab/src/test/codelab/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ java_test(
"//codelab",
"//common:cel_ast",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java_util",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:com_google_testparameterinjector_test_parameter_injector",
"@maven//:junit_junit",
],
Expand All @@ -93,10 +93,10 @@ java_test(
"//:java_truth",
"//codelab",
"//common:cel_ast",
"//common/internal:proto_time_utils",
"@com_google_googleapis//google/rpc/context:attribute_context_java_proto",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:com_google_protobuf_protobuf_java_util",
"@maven//:com_google_testparameterinjector_test_parameter_injector",
"@maven//:junit_junit",
],
Expand Down
6 changes: 4 additions & 2 deletions codelab/src/test/codelab/Exercise5Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableMap;
import com.google.protobuf.util.Timestamps;
import com.google.protobuf.Timestamp;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import dev.cel.common.CelAbstractSyntaxTree;
import java.util.Map;
Expand Down Expand Up @@ -48,7 +48,9 @@ public void evaluate_jwtWithTimeVariable_producesJsonString() throws Exception {
@SuppressWarnings("unchecked")
Map<String, Object> evaluatedResult =
(Map<String, Object>)
exercise5.eval(ast, ImmutableMap.of("time", Timestamps.fromSeconds(1698361778)));
exercise5.eval(
ast,
ImmutableMap.of("time", Timestamp.newBuilder().setSeconds(1698361778).build()));
String jsonOutput = exercise5.toJson(evaluatedResult);

assertThat(jsonOutput)
Expand Down
4 changes: 2 additions & 2 deletions codelab/src/test/codelab/Exercise6Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import com.google.protobuf.Struct;
import com.google.protobuf.Timestamp;
import com.google.protobuf.Value;
import com.google.protobuf.util.Timestamps;
import com.google.rpc.context.AttributeContext;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.internal.ProtoTimeUtils;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -51,7 +51,7 @@ public void evaluate_constructAttributeContext() {
+ "time: now"
+ "}";
// Values for `now` and `jwt` variables to be passed into the runtime
Timestamp now = Timestamps.now();
Timestamp now = ProtoTimeUtils.now();
ImmutableMap<String, Object> jwt =
ImmutableMap.of(
"sub", "serviceAccount:[email protected]",
Expand Down
4 changes: 2 additions & 2 deletions codelab/src/test/codelab/solutions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ java_test(
"//codelab:solutions",
"//common:cel_ast",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java_util",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:com_google_testparameterinjector_test_parameter_injector",
"@maven//:junit_junit",
],
Expand All @@ -87,10 +87,10 @@ java_test(
"//:java_truth",
"//codelab:solutions",
"//common:cel_ast",
"//common/internal:proto_time_utils",
"@com_google_googleapis//google/rpc/context:attribute_context_java_proto",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:com_google_protobuf_protobuf_java_util",
"@maven//:com_google_testparameterinjector_test_parameter_injector",
"@maven//:junit_junit",
],
Expand Down
6 changes: 4 additions & 2 deletions codelab/src/test/codelab/solutions/Exercise5Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableMap;
import com.google.protobuf.util.Timestamps;
import com.google.protobuf.Timestamp;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import dev.cel.common.CelAbstractSyntaxTree;
import java.util.Map;
Expand Down Expand Up @@ -48,7 +48,9 @@ public void evaluate_jwtWithTimeVariable_producesJsonString() throws Exception {
@SuppressWarnings("unchecked")
Map<String, Object> evaluatedResult =
(Map<String, Object>)
exercise5.eval(ast, ImmutableMap.of("time", Timestamps.fromSeconds(1698361778)));
exercise5.eval(
ast,
ImmutableMap.of("time", Timestamp.newBuilder().setSeconds(1698361778).build()));
String jsonOutput = exercise5.toJson(evaluatedResult);

assertThat(jsonOutput)
Expand Down
4 changes: 2 additions & 2 deletions codelab/src/test/codelab/solutions/Exercise6Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import com.google.protobuf.Struct;
import com.google.protobuf.Timestamp;
import com.google.protobuf.Value;
import com.google.protobuf.util.Timestamps;
import com.google.rpc.context.AttributeContext;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.internal.ProtoTimeUtils;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -51,7 +51,7 @@ public void evaluate_constructAttributeContext() {
+ "time: now"
+ "}";
// Values for `now` and `jwt` variables to be passed into the runtime
Timestamp now = Timestamps.now();
Timestamp now = ProtoTimeUtils.now();
ImmutableMap<String, Object> jwt =
ImmutableMap.of(
"sub", "serviceAccount:[email protected]",
Expand Down
10 changes: 10 additions & 0 deletions common/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,13 @@ java_library(
name = "proto_java_qualified_names",
exports = ["//common/src/main/java/dev/cel/common/internal:proto_java_qualified_names"],
)

java_library(
name = "proto_time_utils",
exports = ["//common/src/main/java/dev/cel/common/internal:proto_time_utils"],
)

cel_android_library(
name = "proto_time_util_android",
exports = ["//common/src/main/java/dev/cel/common/internal:proto_time_util_android"],
)
2 changes: 1 addition & 1 deletion common/src/main/java/dev/cel/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ java_library(
tags = [
],
deps = [
"//common/internal:proto_time_utils",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:com_google_protobuf_protobuf_java_util",
"@maven_android//:com_google_protobuf_protobuf_javalite",
],
)
Expand Down
7 changes: 3 additions & 4 deletions common/src/main/java/dev/cel/common/CelProtoJsonAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import com.google.protobuf.Struct;
import com.google.protobuf.Timestamp;
import com.google.protobuf.Value;
import com.google.protobuf.util.Durations;
import com.google.protobuf.util.Timestamps;
import dev.cel.common.internal.ProtoTimeUtils;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
Expand Down Expand Up @@ -112,11 +111,11 @@ public static Value adaptValueToJsonValue(Object value) {
}
if (value instanceof Timestamp) {
// CEL follows the proto3 to JSON conversion which formats as an RFC 3339 encoded JSON string.
String ts = Timestamps.toString((Timestamp) value);
String ts = ProtoTimeUtils.toString((Timestamp) value);
return json.setStringValue(ts).build();
}
if (value instanceof Duration) {
String duration = Durations.toString((Duration) value);
String duration = ProtoTimeUtils.toString((Duration) value);
return json.setStringValue(duration).build();
}
if (value instanceof FieldMask) {
Expand Down
26 changes: 26 additions & 0 deletions common/src/main/java/dev/cel/common/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,29 @@ java_library(
"//common/annotations",
],
)

java_library(
name = "proto_time_utils",
srcs = ["ProtoTimeUtils.java"],
tags = [
],
deps = [
"//common/annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
],
)

cel_android_library(
name = "proto_time_util_android",
srcs = ["ProtoTimeUtils.java"],
tags = [
],
deps = [
"//common/annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
"@maven_android//:com_google_protobuf_protobuf_javalite",
],
)
Loading
Loading