wenjin272 commented on code in PR #821:
URL: https://github.com/apache/flink-agents/pull/821#discussion_r3526058947
##########
runtime/pom.xml:
##########
@@ -193,9 +193,22 @@ under the License.
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
+ <dependency>
+ <groupId>dev.cel</groupId>
+ <artifactId>cel</artifactId>
+ <version>${cel.version}</version>
+ </dependency>
</dependencies>
<build>
+ <testResources>
+ <testResource>
+ <directory>src/test/resources</directory>
+ </testResource>
+ <testResource>
+
<directory>${project.basedir}/../e2e-test/cel-fixtures</directory>
Review Comment:
Why add an e2e directory to the runtime module?
##########
runtime/src/test/java/org/apache/flink/agents/runtime/condition/CelExpressionFacadeTest.java:
##########
@@ -0,0 +1,274 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.agents.runtime.condition;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+import dev.cel.runtime.CelEvaluationException;
+import dev.cel.runtime.CelRuntime;
+import org.apache.flink.agents.plan.condition.CelMacroPolicy;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link CelExpressionFacade}: parse validation and toProgram
compilation + caching. */
+class CelExpressionFacadeTest {
+
+ @BeforeEach
+ void clearCache() {
+ // Per-process cache keyed by source string — clear between tests so
cache assertions are
+ // deterministic.
+ CelExpressionFacade.clearProgramCacheForTests();
+ }
+
+ @ParameterizedTest(name = "rejects disallowed macro {0}")
+ @ValueSource(
+ strings = {
+ "[1, 2, 3].all(x, x > 0)",
+ "[1, 2, 3].exists(x, x > 0)",
+ "attributes.tags.exists(x, x == 'a')",
+ "has(attributes.x) && attributes.list.all(t, t > 0)"
+ })
+ void toProgram_rejectsDisallowedMacroCalls(String source) {
+ assertThatThrownBy(() -> CelExpressionFacade.toProgram(source))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("disallowed macro");
+ }
+
+ @Test
+ void disallowedMacroMessage_isByteIdenticalToPython() {
+ // Must match Python ``cel_facade._format_disallowed_message``
byte-for-byte. CI
+ // ``cel-conformance.yml`` validates this alignment via diff.
+ String expected =
+ "CEL expression uses disallowed macro 'all': \"test_source\". "
+ + "Only allows: [has].";
+ assertThat(CelMacroPolicy.formatDisallowedMessage("all",
"test_source"))
+ .isEqualTo(expected);
+ }
+
+ @Test
+ void toProgram_acceptsMacroNameInStringLiteral() {
+ // "exists" inside a string literal is not a macro call — should
compile fine.
+ CelRuntime.Program program = CelExpressionFacade.toProgram("type ==
'exists_check_event'");
+ assertThat(program).isNotNull();
+ }
+
+ @Test
+ void toProgram_acceptsMacroNameAsFieldSubstring() {
+ // "existing" contains "exist" but is not a macro call.
+ CelRuntime.Program program = CelExpressionFacade.toProgram("existing
== true");
+ assertThat(program).isNotNull();
+ }
+
+ @Test
+ void toProgram_doesNotMisclassifyFieldNamedAllAsDisallowedMacro() {
+ // `attributes.all` is a field access, not a macro call. AST-based
check (vs. old
+ // error-message string match) won't false-positive on the substring
"all".
+ CelRuntime.Program program =
CelExpressionFacade.toProgram("attributes.all == 'value'");
+ assertThat(program).isNotNull();
+ }
+
+ @Test
+ void toProgram_findsDisallowedMacroInDeeplyNestedExpression() {
+ // Macro buried inside arg of another macro arg — AST walker must find
it.
+ assertThatThrownBy(
+ () ->
+ CelExpressionFacade.toProgram(
+ "has(attributes.x) && (attributes.y >
0 || attributes.tags.exists(t, t == 'a'))"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("'exists'");
+ }
+
+ @Test
+ void parse_rejectsHasOnNonFieldArgument() {
+ // has(1 + 1) is neither a field selection nor an attribute name.
+ assertThatThrownBy(() -> CelExpressionFacade.toProgram("has(1 + 1)"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("invalid argument to has() macro");
+ }
+
+ // -- EventType map access --
+
+ @Test
+ void toProgram_unknownEventTypeConstantFailsAtPlanLoad() {
+ assertThatThrownBy(() -> CelExpressionFacade.toProgram("type ==
EventType.NotARealEvent"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Unknown EventType constant:
EventType.NotARealEvent");
+ }
+
+ // -- Dynamic identifier declaration --
+
+ @Test
+ void toProgram_userSuppliedIdSupportsNonStringComparison() throws
CelEvaluationException {
+ // id is declared DYN: users may supply numeric ids via attributes.
+ CelRuntime.Program program = CelExpressionFacade.toProgram("id > 0");
+ Map<String, Object> activation = new HashMap<>();
+ activation.put("id", 42L);
+ assertThat(program.eval(activation)).isEqualTo(true);
+ }
+
+ // -- toProgram(String) --
+
+ @Test
+ void toProgram_fromString_returnsRunnableProgram() throws
CelEvaluationException {
+ CelRuntime.Program program = CelExpressionFacade.toProgram("type ==
'a'");
+ Map<String, Object> activation = new HashMap<>();
+ activation.put("id", "42");
+ activation.put("type", "a");
+ activation.put("attributes", new HashMap<String, Object>());
+ Object result = program.eval(activation);
+ assertThat(result).isEqualTo(true);
+ }
+
+ @Test
+ void toProgram_fromString_throwsOnInvalidSource() {
+ assertThatThrownBy(() -> CelExpressionFacade.toProgram("type =="))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Invalid CEL condition expression");
+ }
+
+ @Test
+ void toProgram_fromString_throwsOnNullOrEmpty() {
Review Comment:
Why does the name mix camelCase and snake_case?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]