gnodet-bot commented on code in PR #26670:
URL: https://github.com/apache/camel/pull/26670#discussion_r4060684438


##########
components/camel-opa/src/test/java/org/apache/camel/component/opa/security/OpaSecurityPolicyWasmTest.java:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.camel.component.opa.security;
+
+import java.util.List;
+
+import org.apache.camel.CamelAuthorizationException;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.health.HealthCheck;
+import org.apache.camel.health.HealthCheckRegistry;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * {@link OpaSecurityPolicy} enforcing a route with an in-process WebAssembly 
bundle. The decision contract is the same
+ * as the REST engine - a match proceeds, a non-match throws {@code 
CamelAuthorizationException} - and no server
+ * readiness check is registered, because the policy is evaluated in-process 
(CAMEL-24830).
+ */
+public class OpaSecurityPolicyWasmTest extends CamelTestSupport {
+
+    private final OpaSecurityPolicy wasmPolicy = new OpaSecurityPolicy();
+    private final OpaSecurityPolicy restPolicy = new OpaSecurityPolicy();
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        wasmPolicy.setEvaluationMode("wasm");
+        wasmPolicy.setPolicyBundle("classpath:authz.wasm");
+        wasmPolicy.setPolicyPath("authz/allow");
+
+        // a rest-mode policy is the positive control for the readiness-check 
assertion: it registers a check, the
+        // wasm one must not
+        restPolicy.setServerUrl("http://opa-rest:8181";);
+        restPolicy.setPolicyPath("authz/allow");
+
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:wasm").policy(wasmPolicy).to("mock:allowed");
+                from("direct:rest").policy(restPolicy).to("mock:rest");

Review Comment:
   ⚠️ **Missing validation-error tests for the new `OpaSecurityPolicy` wasm 
fields.**
   
   `OpaWasmEvaluatorTest` already covers the endpoint's validation path 
(`policyBundle is required`, `poolSize must be at least 1`), but nothing covers 
those same paths through `OpaSecurityPolicy.buildEvaluator()`. The two paths 
are independent: `buildEvaluator()` forwards to `OpaWasmEvaluator.create()`, 
which does the validation, but if `buildEvaluator()` ever started intercepting 
and rewrapping those errors (already half-done: it has a `catch 
(RuntimeException e) { throw e }` guard for exactly that reason), the endpoint 
test would keep passing while the policy path silently breaks.
   
   Also missing: a test for an unknown `evaluationMode` value — that validation 
lives entirely in `buildEvaluator()`, not in `OpaWasmEvaluator.create()`, so 
the endpoint tests do not cover it at all.
   
   Add to this test class:
   
   ```java
   @Test
   void rejectsWasmModeWithoutPolicyBundle() {
       OpaSecurityPolicy policy = new OpaSecurityPolicy();
       policy.setEvaluationMode("wasm");
       policy.setPolicyPath("authz/allow");
       // evaluationMode=wasm but no policyBundle — must fail at route start
       assertThatThrownBy(() -> {
           RouteBuilder rb = new RouteBuilder() {
               @Override
               public void configure() {
                   from("direct:bad").policy(policy).to("mock:ignored");
               }
           };
           context.addRoutes(rb);
       }).hasMessageContaining("policyBundle is required");
   }
   
   @Test
   void rejectsUnknownEvaluationMode() {
       OpaSecurityPolicy policy = new OpaSecurityPolicy();
       policy.setEvaluationMode("grpc");
       policy.setPolicyPath("authz/allow");
       assertThatThrownBy(() -> {
           RouteBuilder rb = new RouteBuilder() {
               @Override
               public void configure() {
                   from("direct:bad2").policy(policy).to("mock:ignored");
               }
           };
           context.addRoutes(rb);
       }).isInstanceOf(IllegalArgumentException.class)
         .hasMessageContaining("Unknown evaluationMode");
   }
   ```



##########
components/camel-opa/src/test/java/org/apache/camel/component/opa/security/OpaSecurityPolicyWasmTest.java:
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.camel.component.opa.security;
+
+import java.util.List;
+
+import org.apache.camel.CamelAuthorizationException;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.health.HealthCheck;
+import org.apache.camel.health.HealthCheckRegistry;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * {@link OpaSecurityPolicy} enforcing a route with an in-process WebAssembly 
bundle. The decision contract is the same
+ * as the REST engine - a match proceeds, a non-match throws {@code 
CamelAuthorizationException} - and no server
+ * readiness check is registered, because the policy is evaluated in-process 
(CAMEL-24830).
+ */
+public class OpaSecurityPolicyWasmTest extends CamelTestSupport {
+
+    private final OpaSecurityPolicy wasmPolicy = new OpaSecurityPolicy();
+    private final OpaSecurityPolicy restPolicy = new OpaSecurityPolicy();
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        wasmPolicy.setEvaluationMode("wasm");
+        wasmPolicy.setPolicyBundle("classpath:authz.wasm");
+        wasmPolicy.setPolicyPath("authz/allow");
+
+        // a rest-mode policy is the positive control for the readiness-check 
assertion: it registers a check, the
+        // wasm one must not
+        restPolicy.setServerUrl("http://opa-rest:8181";);
+        restPolicy.setPolicyPath("authz/allow");
+
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:wasm").policy(wasmPolicy).to("mock:allowed");
+                from("direct:rest").policy(restPolicy).to("mock:rest");
+            }
+        };
+    }
+
+    @Test
+    void allowsWhenTheWasmPolicyMatches() throws Exception {
+        MockEndpoint allowed = getMockEndpoint("mock:allowed");
+        allowed.expectedMessageCount(1);
+
+        template.sendBodyAndHeader("direct:wasm", "payload", "user", "alice");
+
+        allowed.assertIsSatisfied();
+    }
+
+    @Test
+    void deniesWhenTheWasmPolicyDoesNotMatch() throws Exception {
+        MockEndpoint allowed = getMockEndpoint("mock:allowed");
+        allowed.expectedMessageCount(0);
+
+        assertThatThrownBy(() -> template.sendBodyAndHeader("direct:wasm", 
"payload", "user", "mallory"))
+                .isInstanceOf(CamelExecutionException.class)
+                .hasCauseInstanceOf(CamelAuthorizationException.class);
+
+        allowed.assertIsSatisfied();
+    }
+
+    @Test
+    void registersOnlyTheRestPolicysReadinessCheck() {
+        HealthCheckRegistry registry = HealthCheckRegistry.get(context);
+        assertThat(registry).isNotNull();
+        List<HealthCheck> checks = registry.stream()
+                .filter(hc -> hc.getId().startsWith("security-policy:opa-"))
+                .toList();
+
+        // exactly one, and it is the rest policy's - the wasm policy 
evaluates in-process with no server to probe
+        assertThat(checks).hasSize(1);
+        assertThat(checks.get(0).getId()).contains("opa-rest");

Review Comment:
   ⚠️ **Fragile assertion — tied to the `serverUrl` string, not the ID 
contract.**
   
   The assertion `contains("opa-rest")` passes only because `serverUrl` is 
`"http://opa-rest:8181"` and the health-check ID is built from that URL. Change 
the hostname in the fixture and the test still compiles but now proves nothing 
— a future refactor of the ID-building logic in `OpaSecurityPolicyHealthCheck` 
could silently invalidate this check without a test failure.
   
   Assert the full prefix that `OpaSecurityPolicyHealthCheck` actually 
constructs — this makes the test resilient and also documents the ID contract:
   
   ```suggestion
           
assertThat(checks.get(0).getId()).startsWith("security-policy:opa-http://opa-rest:8181/";);
   ```



-- 
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]

Reply via email to