|
| 1 | +/* |
| 2 | + * Copyright 2026 AxonFlow |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.getaxonflow.sdk.types.workflow; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.DisplayName; |
| 22 | + |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.*; |
| 27 | + |
| 28 | +/** |
| 29 | + * Tests for workflow policy types (Issues #1019, #1020, #1021). |
| 30 | + */ |
| 31 | +@DisplayName("Workflow Policy Types") |
| 32 | +class WorkflowPolicyTypesTest { |
| 33 | + |
| 34 | + private ObjectMapper objectMapper; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setUp() { |
| 38 | + objectMapper = new ObjectMapper(); |
| 39 | + } |
| 40 | + |
| 41 | + // PolicyMatch tests |
| 42 | + |
| 43 | + @Test |
| 44 | + @DisplayName("PolicyMatch - should build with all fields") |
| 45 | + void policyMatchShouldBuildWithAllFields() { |
| 46 | + PolicyMatch match = PolicyMatch.builder() |
| 47 | + .policyId("policy-123") |
| 48 | + .policyName("block-gpt4") |
| 49 | + .action("block") |
| 50 | + .reason("GPT-4 not allowed in production") |
| 51 | + .build(); |
| 52 | + |
| 53 | + assertThat(match.getPolicyId()).isEqualTo("policy-123"); |
| 54 | + assertThat(match.getPolicyName()).isEqualTo("block-gpt4"); |
| 55 | + assertThat(match.getAction()).isEqualTo("block"); |
| 56 | + assertThat(match.getReason()).isEqualTo("GPT-4 not allowed in production"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @DisplayName("PolicyMatch - isBlocking returns true for block action") |
| 61 | + void policyMatchIsBlockingShouldReturnTrueForBlockAction() { |
| 62 | + PolicyMatch match = PolicyMatch.builder() |
| 63 | + .policyId("policy-123") |
| 64 | + .action("block") |
| 65 | + .build(); |
| 66 | + |
| 67 | + assertThat(match.isBlocking()).isTrue(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + @DisplayName("PolicyMatch - isBlocking returns false for allow action") |
| 72 | + void policyMatchIsBlockingShouldReturnFalseForAllowAction() { |
| 73 | + PolicyMatch match = PolicyMatch.builder() |
| 74 | + .policyId("policy-123") |
| 75 | + .action("allow") |
| 76 | + .build(); |
| 77 | + |
| 78 | + assertThat(match.isBlocking()).isFalse(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @DisplayName("PolicyMatch - requiresApproval returns true for require_approval action") |
| 83 | + void policyMatchRequiresApprovalShouldReturnTrue() { |
| 84 | + PolicyMatch match = PolicyMatch.builder() |
| 85 | + .policyId("policy-123") |
| 86 | + .action("require_approval") |
| 87 | + .build(); |
| 88 | + |
| 89 | + assertThat(match.requiresApproval()).isTrue(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + @DisplayName("PolicyMatch - should deserialize from JSON") |
| 94 | + void policyMatchShouldDeserialize() throws Exception { |
| 95 | + String json = "{" |
| 96 | + + "\"policy_id\": \"policy-456\"," |
| 97 | + + "\"policy_name\": \"pii-detection\"," |
| 98 | + + "\"action\": \"redact\"," |
| 99 | + + "\"reason\": \"PII detected in input\"" |
| 100 | + + "}"; |
| 101 | + |
| 102 | + PolicyMatch match = objectMapper.readValue(json, PolicyMatch.class); |
| 103 | + |
| 104 | + assertThat(match.getPolicyId()).isEqualTo("policy-456"); |
| 105 | + assertThat(match.getPolicyName()).isEqualTo("pii-detection"); |
| 106 | + assertThat(match.getAction()).isEqualTo("redact"); |
| 107 | + assertThat(match.getReason()).isEqualTo("PII detected in input"); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + @DisplayName("PolicyMatch - should serialize to JSON") |
| 112 | + void policyMatchShouldSerialize() throws Exception { |
| 113 | + PolicyMatch match = PolicyMatch.builder() |
| 114 | + .policyId("policy-789") |
| 115 | + .policyName("cost-limit") |
| 116 | + .action("allow") |
| 117 | + .reason("Within budget") |
| 118 | + .build(); |
| 119 | + |
| 120 | + String json = objectMapper.writeValueAsString(match); |
| 121 | + |
| 122 | + assertThat(json).contains("\"policy_id\":\"policy-789\""); |
| 123 | + assertThat(json).contains("\"policy_name\":\"cost-limit\""); |
| 124 | + assertThat(json).contains("\"action\":\"allow\""); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + @DisplayName("PolicyMatch - equals and hashCode") |
| 129 | + void policyMatchEqualsAndHashCode() { |
| 130 | + PolicyMatch match1 = PolicyMatch.builder() |
| 131 | + .policyId("policy-123") |
| 132 | + .policyName("test") |
| 133 | + .action("allow") |
| 134 | + .build(); |
| 135 | + |
| 136 | + PolicyMatch match2 = PolicyMatch.builder() |
| 137 | + .policyId("policy-123") |
| 138 | + .policyName("test") |
| 139 | + .action("allow") |
| 140 | + .build(); |
| 141 | + |
| 142 | + PolicyMatch match3 = PolicyMatch.builder() |
| 143 | + .policyId("policy-456") |
| 144 | + .policyName("other") |
| 145 | + .action("block") |
| 146 | + .build(); |
| 147 | + |
| 148 | + assertThat(match1).isEqualTo(match2); |
| 149 | + assertThat(match1.hashCode()).isEqualTo(match2.hashCode()); |
| 150 | + assertThat(match1).isNotEqualTo(match3); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + @DisplayName("PolicyMatch - toString contains all fields") |
| 155 | + void policyMatchToStringShouldContainAllFields() { |
| 156 | + PolicyMatch match = PolicyMatch.builder() |
| 157 | + .policyId("policy-123") |
| 158 | + .policyName("test-policy") |
| 159 | + .action("block") |
| 160 | + .reason("test reason") |
| 161 | + .build(); |
| 162 | + |
| 163 | + String str = match.toString(); |
| 164 | + |
| 165 | + assertThat(str).contains("policy-123"); |
| 166 | + assertThat(str).contains("test-policy"); |
| 167 | + assertThat(str).contains("block"); |
| 168 | + assertThat(str).contains("test reason"); |
| 169 | + } |
| 170 | + |
| 171 | + // PolicyEvaluationResult tests |
| 172 | + |
| 173 | + @Test |
| 174 | + @DisplayName("PolicyEvaluationResult - should build with all fields") |
| 175 | + void policyEvaluationResultShouldBuildWithAllFields() { |
| 176 | + List<String> policies = Arrays.asList("cost-limit", "model-restriction"); |
| 177 | + PolicyEvaluationResult result = PolicyEvaluationResult.builder() |
| 178 | + .allowed(true) |
| 179 | + .appliedPolicies(policies) |
| 180 | + .riskScore(0.2) |
| 181 | + .build(); |
| 182 | + |
| 183 | + assertThat(result.isAllowed()).isTrue(); |
| 184 | + assertThat(result.getAppliedPolicies()).containsExactly("cost-limit", "model-restriction"); |
| 185 | + assertThat(result.getRiskScore()).isEqualTo(0.2); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + @DisplayName("PolicyEvaluationResult - should deserialize from JSON") |
| 190 | + void policyEvaluationResultShouldDeserialize() throws Exception { |
| 191 | + String json = "{" |
| 192 | + + "\"allowed\": false," |
| 193 | + + "\"applied_policies\": [\"high-risk-block\"]," |
| 194 | + + "\"risk_score\": 0.85" |
| 195 | + + "}"; |
| 196 | + |
| 197 | + PolicyEvaluationResult result = objectMapper.readValue(json, PolicyEvaluationResult.class); |
| 198 | + |
| 199 | + assertThat(result.isAllowed()).isFalse(); |
| 200 | + assertThat(result.getAppliedPolicies()).containsExactly("high-risk-block"); |
| 201 | + assertThat(result.getRiskScore()).isEqualTo(0.85); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + @DisplayName("PolicyEvaluationResult - should serialize to JSON") |
| 206 | + void policyEvaluationResultShouldSerialize() throws Exception { |
| 207 | + PolicyEvaluationResult result = PolicyEvaluationResult.builder() |
| 208 | + .allowed(true) |
| 209 | + .appliedPolicies(Arrays.asList("policy-1", "policy-2")) |
| 210 | + .riskScore(0.1) |
| 211 | + .build(); |
| 212 | + |
| 213 | + String json = objectMapper.writeValueAsString(result); |
| 214 | + |
| 215 | + assertThat(json).contains("\"allowed\":true"); |
| 216 | + assertThat(json).contains("\"applied_policies\""); |
| 217 | + assertThat(json).contains("\"risk_score\":0.1"); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + @DisplayName("PolicyEvaluationResult - equals and hashCode") |
| 222 | + void policyEvaluationResultEqualsAndHashCode() { |
| 223 | + List<String> policies = Arrays.asList("policy-1"); |
| 224 | + PolicyEvaluationResult result1 = PolicyEvaluationResult.builder() |
| 225 | + .allowed(true) |
| 226 | + .appliedPolicies(policies) |
| 227 | + .riskScore(0.5) |
| 228 | + .build(); |
| 229 | + |
| 230 | + PolicyEvaluationResult result2 = PolicyEvaluationResult.builder() |
| 231 | + .allowed(true) |
| 232 | + .appliedPolicies(policies) |
| 233 | + .riskScore(0.5) |
| 234 | + .build(); |
| 235 | + |
| 236 | + assertThat(result1).isEqualTo(result2); |
| 237 | + assertThat(result1.hashCode()).isEqualTo(result2.hashCode()); |
| 238 | + } |
| 239 | + |
| 240 | + @Test |
| 241 | + @DisplayName("PolicyEvaluationResult - toString contains all fields") |
| 242 | + void policyEvaluationResultToStringShouldContainAllFields() { |
| 243 | + PolicyEvaluationResult result = PolicyEvaluationResult.builder() |
| 244 | + .allowed(false) |
| 245 | + .appliedPolicies(Arrays.asList("test-policy")) |
| 246 | + .riskScore(0.75) |
| 247 | + .build(); |
| 248 | + |
| 249 | + String str = result.toString(); |
| 250 | + |
| 251 | + assertThat(str).contains("allowed=false"); |
| 252 | + assertThat(str).contains("test-policy"); |
| 253 | + assertThat(str).contains("0.75"); |
| 254 | + } |
| 255 | + |
| 256 | + // PlanExecutionResponse tests |
| 257 | + |
| 258 | + @Test |
| 259 | + @DisplayName("PlanExecutionResponse - should deserialize from JSON") |
| 260 | + void planExecutionResponseShouldDeserialize() throws Exception { |
| 261 | + String json = "{" |
| 262 | + + "\"success\": true," |
| 263 | + + "\"result\": \"Plan executed successfully\"," |
| 264 | + + "\"policy_info\": {" |
| 265 | + + " \"allowed\": true," |
| 266 | + + " \"applied_policies\": [\"cost-limit\"]," |
| 267 | + + " \"risk_score\": 0.2" |
| 268 | + + "}" |
| 269 | + + "}"; |
| 270 | + |
| 271 | + PlanExecutionResponse response = objectMapper.readValue(json, PlanExecutionResponse.class); |
| 272 | + |
| 273 | + assertThat(response.isSuccess()).isTrue(); |
| 274 | + assertThat(response.getResult()).isEqualTo("Plan executed successfully"); |
| 275 | + assertThat(response.getPolicyInfo()).isNotNull(); |
| 276 | + assertThat(response.getPolicyInfo().isAllowed()).isTrue(); |
| 277 | + assertThat(response.getPolicyInfo().getAppliedPolicies()).containsExactly("cost-limit"); |
| 278 | + } |
| 279 | + |
| 280 | + @Test |
| 281 | + @DisplayName("PlanExecutionResponse - should handle blocked response") |
| 282 | + void planExecutionResponseShouldHandleBlockedResponse() throws Exception { |
| 283 | + String json = "{" |
| 284 | + + "\"success\": false," |
| 285 | + + "\"error\": \"Plan execution blocked by policy\"," |
| 286 | + + "\"policy_info\": {" |
| 287 | + + " \"allowed\": false," |
| 288 | + + " \"applied_policies\": [\"high-risk-block\"]," |
| 289 | + + " \"risk_score\": 0.9" |
| 290 | + + "}" |
| 291 | + + "}"; |
| 292 | + |
| 293 | + PlanExecutionResponse response = objectMapper.readValue(json, PlanExecutionResponse.class); |
| 294 | + |
| 295 | + assertThat(response.isSuccess()).isFalse(); |
| 296 | + assertThat(response.getError()).isEqualTo("Plan execution blocked by policy"); |
| 297 | + assertThat(response.getPolicyInfo().isAllowed()).isFalse(); |
| 298 | + } |
| 299 | + |
| 300 | + @Test |
| 301 | + @DisplayName("PlanExecutionResponse - should build with all fields") |
| 302 | + void planExecutionResponseShouldBuildWithAllFields() { |
| 303 | + PolicyEvaluationResult policyInfo = PolicyEvaluationResult.builder() |
| 304 | + .allowed(true) |
| 305 | + .riskScore(0.1) |
| 306 | + .build(); |
| 307 | + |
| 308 | + PlanExecutionResponse response = PlanExecutionResponse.builder() |
| 309 | + .success(true) |
| 310 | + .result("completed") |
| 311 | + .policyInfo(policyInfo) |
| 312 | + .build(); |
| 313 | + |
| 314 | + assertThat(response.isSuccess()).isTrue(); |
| 315 | + assertThat(response.getResult()).isEqualTo("completed"); |
| 316 | + assertThat(response.getPolicyInfo()).isEqualTo(policyInfo); |
| 317 | + } |
| 318 | + |
| 319 | + @Test |
| 320 | + @DisplayName("PlanExecutionResponse - equals and hashCode") |
| 321 | + void planExecutionResponseEqualsAndHashCode() { |
| 322 | + PolicyEvaluationResult policyInfo = PolicyEvaluationResult.builder() |
| 323 | + .allowed(true) |
| 324 | + .riskScore(0.5) |
| 325 | + .build(); |
| 326 | + |
| 327 | + PlanExecutionResponse response1 = PlanExecutionResponse.builder() |
| 328 | + .success(true) |
| 329 | + .result("done") |
| 330 | + .policyInfo(policyInfo) |
| 331 | + .build(); |
| 332 | + |
| 333 | + PlanExecutionResponse response2 = PlanExecutionResponse.builder() |
| 334 | + .success(true) |
| 335 | + .result("done") |
| 336 | + .policyInfo(policyInfo) |
| 337 | + .build(); |
| 338 | + |
| 339 | + assertThat(response1).isEqualTo(response2); |
| 340 | + assertThat(response1.hashCode()).isEqualTo(response2.hashCode()); |
| 341 | + } |
| 342 | + |
| 343 | + @Test |
| 344 | + @DisplayName("PlanExecutionResponse - toString contains all fields") |
| 345 | + void planExecutionResponseToStringShouldContainAllFields() { |
| 346 | + PlanExecutionResponse response = PlanExecutionResponse.builder() |
| 347 | + .success(true) |
| 348 | + .result("test-result") |
| 349 | + .build(); |
| 350 | + |
| 351 | + String str = response.toString(); |
| 352 | + |
| 353 | + assertThat(str).contains("success=true"); |
| 354 | + assertThat(str).contains("test-result"); |
| 355 | + } |
| 356 | +} |
0 commit comments