Skip to main content

alef_e2e/codegen/
java.rs

1//! Java e2e test generator using JUnit 5.
2//!
3//! Generates `e2e/java/pom.xml` and `src/test/java/dev/kreuzberg/e2e/{Category}Test.java`
4//! files from JSON fixtures, driven entirely by `E2eConfig` and `CallConfig`.
5
6use crate::config::E2eConfig;
7use crate::escape::{escape_java, sanitize_filename};
8use crate::field_access::FieldResolver;
9use crate::fixture::{Assertion, CallbackAction, Fixture, FixtureGroup, HttpFixture};
10use alef_core::backend::GeneratedFile;
11use alef_core::config::ResolvedCrateConfig;
12use alef_core::hash::{self, CommentStyle};
13use alef_core::template_versions as tv;
14use anyhow::Result;
15use heck::{ToLowerCamelCase, ToUpperCamelCase};
16use std::path::PathBuf;
17
18use super::E2eCodegen;
19use super::client;
20
21/// Java e2e code generator.
22pub struct JavaCodegen;
23
24impl E2eCodegen for JavaCodegen {
25    fn generate(
26        &self,
27        groups: &[FixtureGroup],
28        e2e_config: &E2eConfig,
29        config: &ResolvedCrateConfig,
30        _type_defs: &[alef_core::ir::TypeDef],
31    ) -> Result<Vec<GeneratedFile>> {
32        let lang = self.language_name();
33        let output_base = PathBuf::from(e2e_config.effective_output()).join(lang);
34
35        let mut files = Vec::new();
36
37        // Resolve call config with overrides.
38        let call = &e2e_config.call;
39        let overrides = call.overrides.get(lang);
40        let _module_path = overrides
41            .and_then(|o| o.module.as_ref())
42            .cloned()
43            .unwrap_or_else(|| call.module.clone());
44        let function_name = overrides
45            .and_then(|o| o.function.as_ref())
46            .cloned()
47            .unwrap_or_else(|| call.function.clone());
48        let class_name = overrides
49            .and_then(|o| o.class.as_ref())
50            .cloned()
51            .unwrap_or_else(|| config.name.to_upper_camel_case());
52        let result_is_simple = overrides.is_some_and(|o| o.result_is_simple);
53        let result_var = &call.result_var;
54
55        // Resolve package config.
56        let java_pkg = e2e_config.resolve_package("java");
57        let pkg_name = java_pkg
58            .as_ref()
59            .and_then(|p| p.name.as_ref())
60            .cloned()
61            .unwrap_or_else(|| config.name.clone());
62
63        // Resolve Java package info for the dependency.
64        let java_group_id = config.java_group_id();
65        let binding_pkg = config.java_package();
66        let pkg_version = config.resolved_version().unwrap_or_else(|| "0.1.0".to_string());
67
68        // Generate pom.xml.
69        files.push(GeneratedFile {
70            path: output_base.join("pom.xml"),
71            content: render_pom_xml(
72                &pkg_name,
73                &java_group_id,
74                &pkg_version,
75                e2e_config.dep_mode,
76                &e2e_config.test_documents_relative_from(0),
77            ),
78            generated_header: false,
79        });
80
81        // Detect whether any fixture needs the mock-server (HTTP fixtures or
82        // fixtures with a `mock_response`). When present, emit a
83        // JUnit Platform LauncherSessionListener that spawns the mock-server
84        // before any test runs and a META-INF/services SPI manifest registering
85        // it. Without this, every fixture-bound test failed with
86        // `LiterLlmRsException: error sending request for url` because
87        // `System.getenv("MOCK_SERVER_URL")` was null.
88        let needs_mock_server = groups
89            .iter()
90            .flat_map(|g| g.fixtures.iter())
91            .any(|f| f.needs_mock_server());
92
93        // Generate test files per category. Path mirrors the configured Java
94        // package — `dev.myorg` becomes `dev/myorg`, etc. — so the package
95        // declaration in each test file matches its filesystem location.
96        let mut test_base = output_base.join("src").join("test").join("java");
97        for segment in java_group_id.split('.') {
98            test_base = test_base.join(segment);
99        }
100        let test_base = test_base.join("e2e");
101
102        if needs_mock_server {
103            files.push(GeneratedFile {
104                path: test_base.join("MockServerListener.java"),
105                content: render_mock_server_listener(&java_group_id),
106                generated_header: true,
107            });
108            files.push(GeneratedFile {
109                path: output_base
110                    .join("src")
111                    .join("test")
112                    .join("resources")
113                    .join("META-INF")
114                    .join("services")
115                    .join("org.junit.platform.launcher.LauncherSessionListener"),
116                content: format!("{java_group_id}.e2e.MockServerListener\n"),
117                generated_header: false,
118            });
119        }
120
121        // Resolve options_type from override.
122        let options_type = overrides.and_then(|o| o.options_type.clone());
123
124        // Resolve enum_fields and nested_types from Java override config.
125        static EMPTY_ENUM_FIELDS: std::sync::LazyLock<std::collections::HashMap<String, String>> =
126            std::sync::LazyLock::new(std::collections::HashMap::new);
127        let _enum_fields = overrides.map(|o| &o.enum_fields).unwrap_or(&EMPTY_ENUM_FIELDS);
128
129        // Build effective nested_types by merging defaults with configured overrides.
130        let mut effective_nested_types = default_java_nested_types();
131        if let Some(overrides_map) = overrides.map(|o| &o.nested_types) {
132            effective_nested_types.extend(overrides_map.clone());
133        }
134
135        // Resolve nested_types_optional from override (defaults to true for backward compatibility).
136        let nested_types_optional = overrides.map(|o| o.nested_types_optional).unwrap_or(true);
137
138        let field_resolver = FieldResolver::new(
139            &e2e_config.fields,
140            &e2e_config.fields_optional,
141            &e2e_config.result_fields,
142            &e2e_config.fields_array,
143            &std::collections::HashSet::new(),
144        );
145
146        for group in groups {
147            let active: Vec<&Fixture> = group
148                .fixtures
149                .iter()
150                .filter(|f| super::should_include_fixture(f, lang, e2e_config))
151                .collect();
152
153            if active.is_empty() {
154                continue;
155            }
156
157            let class_file_name = format!("{}Test.java", sanitize_filename(&group.category).to_upper_camel_case());
158            let content = render_test_file(
159                &group.category,
160                &active,
161                &class_name,
162                &function_name,
163                &java_group_id,
164                &binding_pkg,
165                result_var,
166                &e2e_config.call.args,
167                options_type.as_deref(),
168                &field_resolver,
169                result_is_simple,
170                &e2e_config.fields_enum,
171                e2e_config,
172                &effective_nested_types,
173                nested_types_optional,
174            );
175            files.push(GeneratedFile {
176                path: test_base.join(class_file_name),
177                content,
178                generated_header: true,
179            });
180        }
181
182        Ok(files)
183    }
184
185    fn language_name(&self) -> &'static str {
186        "java"
187    }
188}
189
190// ---------------------------------------------------------------------------
191// Rendering
192// ---------------------------------------------------------------------------
193
194fn render_pom_xml(
195    pkg_name: &str,
196    java_group_id: &str,
197    pkg_version: &str,
198    dep_mode: crate::config::DependencyMode,
199    test_documents_path: &str,
200) -> String {
201    // pkg_name may be in "groupId:artifactId" Maven format; split accordingly.
202    let (dep_group_id, dep_artifact_id) = if let Some((g, a)) = pkg_name.split_once(':') {
203        (g, a)
204    } else {
205        (java_group_id, pkg_name)
206    };
207    let artifact_id = format!("{dep_artifact_id}-e2e-java");
208    let dep_block = match dep_mode {
209        crate::config::DependencyMode::Registry => {
210            format!(
211                r#"        <dependency>
212            <groupId>{dep_group_id}</groupId>
213            <artifactId>{dep_artifact_id}</artifactId>
214            <version>{pkg_version}</version>
215        </dependency>"#
216            )
217        }
218        crate::config::DependencyMode::Local => {
219            format!(
220                r#"        <dependency>
221            <groupId>{dep_group_id}</groupId>
222            <artifactId>{dep_artifact_id}</artifactId>
223            <version>{pkg_version}</version>
224            <scope>system</scope>
225            <systemPath>${{project.basedir}}/../../packages/java/target/{dep_artifact_id}-{pkg_version}.jar</systemPath>
226        </dependency>"#
227            )
228        }
229    };
230    crate::template_env::render(
231        "java/pom.xml.jinja",
232        minijinja::context! {
233            artifact_id => artifact_id,
234            java_group_id => java_group_id,
235            dep_block => dep_block,
236            junit_version => tv::maven::JUNIT,
237            jackson_version => tv::maven::JACKSON_E2E,
238            build_helper_version => tv::maven::BUILD_HELPER_MAVEN_PLUGIN,
239            maven_surefire_version => tv::maven::MAVEN_SUREFIRE_PLUGIN_E2E,
240            test_documents_path => test_documents_path,
241        },
242    )
243}
244
245/// Render the JUnit Platform LauncherSessionListener that spawns the
246/// mock-server binary once per launcher session and tears it down on close.
247///
248/// Mirrors the Ruby `spec_helper.rb` and Python `conftest.py` patterns. The
249/// URL is exposed as a JVM system property `mockServerUrl`; generated test
250/// bodies prefer it over the `MOCK_SERVER_URL` env var so external overrides
251/// (e.g. CI exporting MOCK_SERVER_URL) still work without rerouting through
252/// JNI's lack of `setenv`.
253fn render_mock_server_listener(java_group_id: &str) -> String {
254    let header = hash::header(CommentStyle::DoubleSlash);
255    let mut out = header;
256    out.push_str(&format!("package {java_group_id}.e2e;\n\n"));
257    out.push_str("import java.io.BufferedReader;\n");
258    out.push_str("import java.io.File;\n");
259    out.push_str("import java.io.IOException;\n");
260    out.push_str("import java.io.InputStreamReader;\n");
261    out.push_str("import java.nio.charset.StandardCharsets;\n");
262    out.push_str("import java.nio.file.Path;\n");
263    out.push_str("import java.nio.file.Paths;\n");
264    out.push_str("import java.util.regex.Matcher;\n");
265    out.push_str("import java.util.regex.Pattern;\n");
266    out.push_str("import org.junit.platform.launcher.LauncherSession;\n");
267    out.push_str("import org.junit.platform.launcher.LauncherSessionListener;\n");
268    out.push('\n');
269    out.push_str("/**\n");
270    out.push_str(" * Spawns the mock-server binary once per JUnit launcher session and\n");
271    out.push_str(" * exposes its URL as the `mockServerUrl` system property. Generated\n");
272    out.push_str(" * test bodies read the property (with `MOCK_SERVER_URL` env-var\n");
273    out.push_str(" * fallback) so tests can run via plain `mvn test` without any external\n");
274    out.push_str(" * mock-server orchestration. Mirrors the Ruby spec_helper / Python\n");
275    out.push_str(" * conftest spawn pattern. Honors a pre-set MOCK_SERVER_URL by\n");
276    out.push_str(" * skipping the spawn entirely.\n");
277    out.push_str(" */\n");
278    out.push_str("public class MockServerListener implements LauncherSessionListener {\n");
279    out.push_str("    private Process mockServer;\n");
280    out.push('\n');
281    out.push_str("    @Override\n");
282    out.push_str("    public void launcherSessionOpened(LauncherSession session) {\n");
283    out.push_str("        String preset = System.getenv(\"MOCK_SERVER_URL\");\n");
284    out.push_str("        if (preset != null && !preset.isEmpty()) {\n");
285    out.push_str("            System.setProperty(\"mockServerUrl\", preset);\n");
286    out.push_str("            return;\n");
287    out.push_str("        }\n");
288    out.push_str("        Path repoRoot = locateRepoRoot();\n");
289    out.push_str("        if (repoRoot == null) {\n");
290    out.push_str("            throw new IllegalStateException(\"MockServerListener: could not locate repo root (looked for fixtures/ in ancestors of \" + System.getProperty(\"user.dir\") + \")\");\n");
291    out.push_str("        }\n");
292    out.push_str("        String binName = System.getProperty(\"os.name\", \"\").toLowerCase().contains(\"win\") ? \"mock-server.exe\" : \"mock-server\";\n");
293    out.push_str("        File bin = repoRoot.resolve(\"e2e\").resolve(\"rust\").resolve(\"target\").resolve(\"release\").resolve(binName).toFile();\n");
294    out.push_str("        File fixturesDir = repoRoot.resolve(\"fixtures\").toFile();\n");
295    out.push_str("        if (!bin.exists()) {\n");
296    out.push_str("            throw new IllegalStateException(\"MockServerListener: mock-server binary not found at \" + bin + \" — run: cargo build --manifest-path e2e/rust/Cargo.toml --bin mock-server --release\");\n");
297    out.push_str("        }\n");
298    out.push_str(
299        "        ProcessBuilder pb = new ProcessBuilder(bin.getAbsolutePath(), fixturesDir.getAbsolutePath())\n",
300    );
301    out.push_str("            .redirectErrorStream(false);\n");
302    out.push_str("        try {\n");
303    out.push_str("            mockServer = pb.start();\n");
304    out.push_str("        } catch (IOException e) {\n");
305    out.push_str(
306        "            throw new IllegalStateException(\"MockServerListener: failed to start mock-server\", e);\n",
307    );
308    out.push_str("        }\n");
309    out.push_str("        // Read until we see MOCK_SERVER_URL= and optionally MOCK_SERVERS=.\n");
310    out.push_str("        // Cap the loop so a misbehaving mock-server cannot block indefinitely.\n");
311    out.push_str("        BufferedReader stdout = new BufferedReader(new InputStreamReader(mockServer.getInputStream(), StandardCharsets.UTF_8));\n");
312    out.push_str("        String url = null;\n");
313    out.push_str("        try {\n");
314    out.push_str("            for (int i = 0; i < 16; i++) {\n");
315    out.push_str("                String line = stdout.readLine();\n");
316    out.push_str("                if (line == null) break;\n");
317    out.push_str("                if (line.startsWith(\"MOCK_SERVER_URL=\")) {\n");
318    out.push_str("                    url = line.substring(\"MOCK_SERVER_URL=\".length()).trim();\n");
319    out.push_str("                } else if (line.startsWith(\"MOCK_SERVERS=\")) {\n");
320    out.push_str("                    String jsonVal = line.substring(\"MOCK_SERVERS=\".length()).trim();\n");
321    out.push_str("                    System.setProperty(\"mockServers\", jsonVal);\n");
322    out.push_str("                    // Parse JSON map of fixture_id -> url and expose as system properties.\n");
323    out.push_str("                    Pattern p = Pattern.compile(\"\\\"([^\\\"]+)\\\":\\\"([^\\\"]+)\\\"\");\n");
324    out.push_str("                    Matcher matcher = p.matcher(jsonVal);\n");
325    out.push_str("                    while (matcher.find()) {\n");
326    out.push_str("                        String fid = matcher.group(1);\n");
327    out.push_str("                        String furl = matcher.group(2);\n");
328    out.push_str("                        System.setProperty(\"mockServer.\" + fid, furl);\n");
329    out.push_str("                    }\n");
330    out.push_str("                    break;\n");
331    out.push_str("                } else if (url != null) {\n");
332    out.push_str("                    break;\n");
333    out.push_str("                }\n");
334    out.push_str("            }\n");
335    out.push_str("        } catch (IOException e) {\n");
336    out.push_str("            mockServer.destroyForcibly();\n");
337    out.push_str(
338        "            throw new IllegalStateException(\"MockServerListener: failed to read mock-server stdout\", e);\n",
339    );
340    out.push_str("        }\n");
341    out.push_str("        if (url == null || url.isEmpty()) {\n");
342    out.push_str("            mockServer.destroyForcibly();\n");
343    out.push_str("            throw new IllegalStateException(\"MockServerListener: mock-server did not emit MOCK_SERVER_URL\");\n");
344    out.push_str("        }\n");
345    out.push_str("        // TCP-readiness probe: ensure axum::serve is accepting before tests start.\n");
346    out.push_str("        // The mock-server binds the TcpListener synchronously then prints the URL\n");
347    out.push_str("        // before tokio::spawn(axum::serve(...)) is polled, so under Surefire\n");
348    out.push_str("        // parallel mode tests can race startup. Poll-connect (max 5s, 50ms backoff)\n");
349    out.push_str("        // until success.\n");
350    out.push_str("        java.net.URI healthUri = java.net.URI.create(url);\n");
351    out.push_str("        String host = healthUri.getHost();\n");
352    out.push_str("        int port = healthUri.getPort();\n");
353    out.push_str("        long deadline = System.nanoTime() + 5_000_000_000L;\n");
354    out.push_str("        while (System.nanoTime() < deadline) {\n");
355    out.push_str("            try (java.net.Socket s = new java.net.Socket()) {\n");
356    out.push_str("                s.connect(new java.net.InetSocketAddress(host, port), 100);\n");
357    out.push_str("                break;\n");
358    out.push_str("            } catch (java.io.IOException ignored) {\n");
359    out.push_str("                try { Thread.sleep(50); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); break; }\n");
360    out.push_str("            }\n");
361    out.push_str("        }\n");
362    out.push_str("        System.setProperty(\"mockServerUrl\", url);\n");
363    out.push_str("        // Drain remaining stdout/stderr in daemon threads so a full pipe\n");
364    out.push_str("        // does not block the child.\n");
365    out.push_str("        Process server = mockServer;\n");
366    out.push_str("        Thread drainOut = new Thread(() -> drain(stdout));\n");
367    out.push_str("        drainOut.setDaemon(true);\n");
368    out.push_str("        drainOut.start();\n");
369    out.push_str("        Thread drainErr = new Thread(() -> drain(new BufferedReader(new InputStreamReader(server.getErrorStream(), StandardCharsets.UTF_8))));\n");
370    out.push_str("        drainErr.setDaemon(true);\n");
371    out.push_str("        drainErr.start();\n");
372    out.push_str("    }\n");
373    out.push('\n');
374    out.push_str("    @Override\n");
375    out.push_str("    public void launcherSessionClosed(LauncherSession session) {\n");
376    out.push_str("        if (mockServer == null) return;\n");
377    out.push_str("        try { mockServer.getOutputStream().close(); } catch (IOException ignored) {}\n");
378    out.push_str("        try {\n");
379    out.push_str("            if (!mockServer.waitFor(2, java.util.concurrent.TimeUnit.SECONDS)) {\n");
380    out.push_str("                mockServer.destroyForcibly();\n");
381    out.push_str("            }\n");
382    out.push_str("        } catch (InterruptedException ignored) {\n");
383    out.push_str("            Thread.currentThread().interrupt();\n");
384    out.push_str("            mockServer.destroyForcibly();\n");
385    out.push_str("        }\n");
386    out.push_str("    }\n");
387    out.push('\n');
388    out.push_str("    private static Path locateRepoRoot() {\n");
389    out.push_str("        Path dir = Paths.get(\"\").toAbsolutePath();\n");
390    out.push_str("        while (dir != null) {\n");
391    out.push_str("            if (dir.resolve(\"fixtures\").toFile().isDirectory()\n");
392    out.push_str("                && dir.resolve(\"e2e\").toFile().isDirectory()) {\n");
393    out.push_str("                return dir;\n");
394    out.push_str("            }\n");
395    out.push_str("            dir = dir.getParent();\n");
396    out.push_str("        }\n");
397    out.push_str("        return null;\n");
398    out.push_str("    }\n");
399    out.push('\n');
400    out.push_str("    private static void drain(BufferedReader reader) {\n");
401    out.push_str("        try {\n");
402    out.push_str("            char[] buf = new char[1024];\n");
403    out.push_str("            while (reader.read(buf) >= 0) { /* drain */ }\n");
404    out.push_str("        } catch (IOException ignored) {}\n");
405    out.push_str("    }\n");
406    out.push_str("}\n");
407    out
408}
409
410#[allow(clippy::too_many_arguments)]
411fn render_test_file(
412    category: &str,
413    fixtures: &[&Fixture],
414    class_name: &str,
415    function_name: &str,
416    java_group_id: &str,
417    binding_pkg: &str,
418    result_var: &str,
419    args: &[crate::config::ArgMapping],
420    options_type: Option<&str>,
421    field_resolver: &FieldResolver,
422    result_is_simple: bool,
423    enum_fields: &std::collections::HashSet<String>,
424    e2e_config: &E2eConfig,
425    nested_types: &std::collections::HashMap<String, String>,
426    nested_types_optional: bool,
427) -> String {
428    let header = hash::header(CommentStyle::DoubleSlash);
429    let test_class_name = format!("{}Test", sanitize_filename(category).to_upper_camel_case());
430
431    // If the class_name is fully qualified (contains '.'), import it and use
432    // only the simple name for method calls.  Otherwise use it as-is.
433    let (import_path, simple_class) = if class_name.contains('.') {
434        let simple = class_name.rsplit('.').next().unwrap_or(class_name);
435        (class_name, simple)
436    } else {
437        ("", class_name)
438    };
439
440    // Check if any fixture (with its resolved call) will emit MAPPER usage.
441    let lang_for_om = "java";
442    let needs_object_mapper_for_handle = fixtures.iter().any(|f| {
443        args.iter().filter(|a| a.arg_type == "handle").any(|a| {
444            let v = f.input.get(&a.field).unwrap_or(&serde_json::Value::Null);
445            !(v.is_null() || v.is_object() && v.as_object().is_some_and(|o| o.is_empty()))
446        })
447    });
448    // HTTP fixtures always need ObjectMapper for JSON body comparison.
449    let has_http_fixtures = fixtures.iter().any(|f| f.http.is_some());
450    let needs_object_mapper = needs_object_mapper_for_handle || has_http_fixtures;
451
452    // Collect all options_type values used (class-level + per-fixture call overrides).
453    let mut all_options_types: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
454    if let Some(t) = options_type {
455        all_options_types.insert(t.to_string());
456    }
457    for f in fixtures.iter() {
458        let call_cfg = e2e_config.resolve_call_for_fixture(f.call.as_deref(), &f.input);
459        if let Some(ov) = call_cfg.overrides.get(lang_for_om) {
460            if let Some(t) = &ov.options_type {
461                all_options_types.insert(t.clone());
462            }
463        }
464        // Auto-fallback: when the Java override does not declare an options_type
465        // but another non-prefixed binding (csharp/c/go/php/python) does, mirror
466        // that name into the import set so the auto-emitted `Type.fromJson(json)`
467        // expression compiles. The Java POJO class name matches the Rust source
468        // type name for these backends.
469        let java_has_type = call_cfg
470            .overrides
471            .get(lang_for_om)
472            .and_then(|o| o.options_type.as_deref())
473            .is_some();
474        if !java_has_type {
475            for cand in ["csharp", "c", "go", "php", "python"] {
476                if let Some(o) = call_cfg.overrides.get(cand) {
477                    if let Some(t) = &o.options_type {
478                        all_options_types.insert(t.clone());
479                        break;
480                    }
481                }
482            }
483        }
484        // Detect batch item types used in this fixture
485        for arg in &call_cfg.args {
486            if let Some(elem_type) = &arg.element_type {
487                if elem_type == "BatchBytesItem" || elem_type == "BatchFileItem" {
488                    all_options_types.insert(elem_type.clone());
489                }
490            }
491        }
492    }
493
494    // Collect nested config types actually referenced in fixture builder expressions.
495    // Note: enum types don't need explicit imports since they're in the same package.
496    let mut nested_types_used: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
497    for f in fixtures.iter() {
498        let call_cfg = e2e_config.resolve_call_for_fixture(f.call.as_deref(), &f.input);
499        for arg in &call_cfg.args {
500            if arg.arg_type == "json_object" {
501                let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
502                if let Some(val) = f.input.get(field) {
503                    if !val.is_null() && !val.is_array() {
504                        if let Some(obj) = val.as_object() {
505                            collect_nested_type_names(obj, nested_types, &mut nested_types_used);
506                        }
507                    }
508                }
509            }
510        }
511    }
512
513    // Effective binding package for FQN imports of binding types
514    // (ChatCompletionRequest, etc.). Prefer the explicit `[crates.java] package`
515    // wired in via `binding_pkg`; fall back to the package derived from a
516    // fully-qualified `class_name` when present.
517    let binding_pkg_for_imports: String = if !binding_pkg.is_empty() {
518        binding_pkg.to_string()
519    } else if !import_path.is_empty() {
520        import_path
521            .rsplit_once('.')
522            .map(|(p, _)| p.to_string())
523            .unwrap_or_default()
524    } else {
525        String::new()
526    };
527
528    // Build imports list
529    let mut imports: Vec<String> = Vec::new();
530    imports.push("import org.junit.jupiter.api.Test;".to_string());
531    imports.push("import static org.junit.jupiter.api.Assertions.*;".to_string());
532
533    // Import the test entry-point class itself when it is fully-qualified or
534    // when we know the binding package — emit the FQN so javac resolves it.
535    if !import_path.is_empty() {
536        imports.push(format!("import {import_path};"));
537    } else if !binding_pkg_for_imports.is_empty() && !class_name.is_empty() {
538        imports.push(format!("import {binding_pkg_for_imports}.{class_name};"));
539    }
540
541    if needs_object_mapper {
542        imports.push("import com.fasterxml.jackson.databind.ObjectMapper;".to_string());
543        imports.push("import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;".to_string());
544    }
545
546    // Import all options types used across fixtures (for builder expressions and MAPPER).
547    if !all_options_types.is_empty() {
548        for opts_type in &all_options_types {
549            let qualified = if binding_pkg_for_imports.is_empty() {
550                opts_type.clone()
551            } else {
552                format!("{binding_pkg_for_imports}.{opts_type}")
553            };
554            imports.push(format!("import {qualified};"));
555        }
556    }
557
558    // Import nested options types
559    if !nested_types_used.is_empty() && !binding_pkg_for_imports.is_empty() {
560        for type_name in &nested_types_used {
561            imports.push(format!("import {binding_pkg_for_imports}.{type_name};"));
562        }
563    }
564
565    // Import CrawlConfig when handle args need JSON deserialization.
566    if needs_object_mapper_for_handle && !binding_pkg_for_imports.is_empty() {
567        imports.push(format!("import {binding_pkg_for_imports}.CrawlConfig;"));
568    }
569
570    // Import visitor types when any fixture uses visitor callbacks.
571    let has_visitor_fixtures = fixtures.iter().any(|f| f.visitor.is_some());
572    if has_visitor_fixtures && !binding_pkg_for_imports.is_empty() {
573        imports.push(format!("import {binding_pkg_for_imports}.Visitor;"));
574        imports.push(format!("import {binding_pkg_for_imports}.NodeContext;"));
575        imports.push(format!("import {binding_pkg_for_imports}.VisitResult;"));
576    }
577
578    // Import Optional when using builder expressions with optional fields
579    if !all_options_types.is_empty() {
580        imports.push("import java.util.Optional;".to_string());
581    }
582
583    // Render all test methods
584    let mut fixtures_body = String::new();
585    for (i, fixture) in fixtures.iter().enumerate() {
586        render_test_method(
587            &mut fixtures_body,
588            fixture,
589            simple_class,
590            function_name,
591            result_var,
592            args,
593            options_type,
594            field_resolver,
595            result_is_simple,
596            enum_fields,
597            e2e_config,
598            nested_types,
599            nested_types_optional,
600        );
601        if i + 1 < fixtures.len() {
602            fixtures_body.push('\n');
603        }
604    }
605
606    // Render template
607    crate::template_env::render(
608        "java/test_file.jinja",
609        minijinja::context! {
610            header => header,
611            java_group_id => java_group_id,
612            test_class_name => test_class_name,
613            category => category,
614            imports => imports,
615            needs_object_mapper => needs_object_mapper,
616            fixtures_body => fixtures_body,
617        },
618    )
619}
620
621// ---------------------------------------------------------------------------
622// HTTP test rendering — shared-driver integration
623// ---------------------------------------------------------------------------
624
625/// Thin renderer that emits JUnit 5 test methods targeting a mock server via
626/// `java.net.http.HttpClient`. Satisfies [`client::TestClientRenderer`] so the
627/// shared [`client::http_call::render_http_test`] driver drives the call sequence.
628struct JavaTestClientRenderer;
629
630impl client::TestClientRenderer for JavaTestClientRenderer {
631    fn language_name(&self) -> &'static str {
632        "java"
633    }
634
635    /// Convert a fixture id to the UpperCamelCase suffix appended to `test`.
636    ///
637    /// The emitted method name is `test{fn_name}`, matching the pre-existing shape.
638    fn sanitize_test_name(&self, id: &str) -> String {
639        id.to_upper_camel_case()
640    }
641
642    /// Emit `@Test void test{fn_name}() throws Exception {`.
643    ///
644    /// When `skip_reason` is `Some`, the body is a single
645    /// `Assumptions.assumeTrue(false, ...)` call and `render_test_close` closes
646    /// the brace symmetrically.
647    fn render_test_open(&self, out: &mut String, fn_name: &str, description: &str, skip_reason: Option<&str>) {
648        let escaped_reason = skip_reason.map(escape_java);
649        let rendered = crate::template_env::render(
650            "java/http_test_open.jinja",
651            minijinja::context! {
652                fn_name => fn_name,
653                description => description,
654                skip_reason => escaped_reason,
655            },
656        );
657        out.push_str(&rendered);
658    }
659
660    /// Emit the closing `}` for a test method.
661    fn render_test_close(&self, out: &mut String) {
662        let rendered = crate::template_env::render("java/http_test_close.jinja", minijinja::context! {});
663        out.push_str(&rendered);
664    }
665
666    /// Emit a `java.net.http.HttpClient` request to `baseUrl + path`.
667    ///
668    /// Binds the response to `response` (the `ctx.response_var`). Java's
669    /// `HttpClient` disallows a fixed set of restricted headers; those are
670    /// silently dropped so the test compiles.
671    fn render_call(&self, out: &mut String, ctx: &client::CallCtx<'_>) {
672        // Java's HttpClient throws IllegalArgumentException for these headers.
673        const JAVA_RESTRICTED_HEADERS: &[&str] = &["connection", "content-length", "expect", "host", "upgrade"];
674
675        let method = ctx.method.to_uppercase();
676
677        // Build the path, appending query params when present.
678        let path = if ctx.query_params.is_empty() {
679            ctx.path.to_string()
680        } else {
681            let pairs: Vec<String> = ctx
682                .query_params
683                .iter()
684                .map(|(k, v)| {
685                    let val_str = match v {
686                        serde_json::Value::String(s) => s.clone(),
687                        other => other.to_string(),
688                    };
689                    format!("{}={}", k, escape_java(&val_str))
690                })
691                .collect();
692            format!("{}?{}", ctx.path, pairs.join("&"))
693        };
694
695        let body_publisher = if let Some(body) = ctx.body {
696            let json = serde_json::to_string(body).unwrap_or_default();
697            let escaped = escape_java(&json);
698            format!("java.net.http.HttpRequest.BodyPublishers.ofString(\"{escaped}\")")
699        } else {
700            "java.net.http.HttpRequest.BodyPublishers.noBody()".to_string()
701        };
702
703        // Content-Type header — only when a body is present.
704        let content_type = if ctx.body.is_some() {
705            let ct = ctx.content_type.unwrap_or("application/json");
706            // Only emit when not already in ctx.headers (avoid duplicate Content-Type).
707            if !ctx.headers.keys().any(|k| k.to_lowercase() == "content-type") {
708                Some(ct.to_string())
709            } else {
710                None
711            }
712        } else {
713            None
714        };
715
716        // Build header lines — skip Java-restricted ones.
717        let mut headers_lines: Vec<String> = Vec::new();
718        for (name, value) in ctx.headers {
719            if JAVA_RESTRICTED_HEADERS.contains(&name.to_lowercase().as_str()) {
720                continue;
721            }
722            let escaped_name = escape_java(name);
723            let escaped_value = escape_java(value);
724            headers_lines.push(format!(
725                "builder = builder.header(\"{escaped_name}\", \"{escaped_value}\");"
726            ));
727        }
728
729        // Cookies as a single `Cookie` header.
730        let cookies_line = if !ctx.cookies.is_empty() {
731            let cookie_str: Vec<String> = ctx.cookies.iter().map(|(k, v)| format!("{k}={v}")).collect();
732            let cookie_header = escape_java(&cookie_str.join("; "));
733            Some(format!("builder = builder.header(\"Cookie\", \"{cookie_header}\");"))
734        } else {
735            None
736        };
737
738        let rendered = crate::template_env::render(
739            "java/http_request.jinja",
740            minijinja::context! {
741                method => method,
742                path => path,
743                body_publisher => body_publisher,
744                content_type => content_type,
745                headers_lines => headers_lines,
746                cookies_line => cookies_line,
747                response_var => ctx.response_var,
748            },
749        );
750        out.push_str(&rendered);
751    }
752
753    /// Emit `assertEquals(status, response.statusCode(), ...)`.
754    fn render_assert_status(&self, out: &mut String, response_var: &str, status: u16) {
755        let rendered = crate::template_env::render(
756            "java/http_assertions.jinja",
757            minijinja::context! {
758                response_var => response_var,
759                status_code => status,
760                headers => Vec::<std::collections::HashMap<&str, String>>::new(),
761                body_assertion => String::new(),
762                partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
763                validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
764            },
765        );
766        out.push_str(&rendered);
767    }
768
769    /// Emit a header assertion using `response.headers().firstValue(...)`.
770    ///
771    /// Handles special tokens: `<<present>>`, `<<absent>>`, `<<uuid>>`.
772    fn render_assert_header(&self, out: &mut String, response_var: &str, name: &str, expected: &str) {
773        let escaped_name = escape_java(name);
774        let assertion_code = match expected {
775            "<<present>>" => {
776                format!(
777                    "assertTrue({response_var}.headers().firstValue(\"{escaped_name}\").isPresent(), \"header {escaped_name} should be present\");"
778                )
779            }
780            "<<absent>>" => {
781                format!(
782                    "assertTrue({response_var}.headers().firstValue(\"{escaped_name}\").isEmpty(), \"header {escaped_name} should be absent\");"
783                )
784            }
785            "<<uuid>>" => {
786                format!(
787                    "assertTrue({response_var}.headers().firstValue(\"{escaped_name}\").orElse(\"\").matches(\"[0-9a-fA-F]{{8}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{12}}\"), \"header {escaped_name} should be a UUID\");"
788                )
789            }
790            literal => {
791                let escaped_value = escape_java(literal);
792                format!(
793                    "assertTrue({response_var}.headers().firstValue(\"{escaped_name}\").orElse(\"\").contains(\"{escaped_value}\"), \"header {escaped_name} mismatch\");"
794                )
795            }
796        };
797
798        let mut headers = vec![std::collections::HashMap::new()];
799        headers[0].insert("assertion_code", assertion_code);
800
801        let rendered = crate::template_env::render(
802            "java/http_assertions.jinja",
803            minijinja::context! {
804                response_var => response_var,
805                status_code => 0u16,
806                headers => headers,
807                body_assertion => String::new(),
808                partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
809                validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
810            },
811        );
812        out.push_str(&rendered);
813    }
814
815    /// Emit a JSON body equality assertion using Jackson's `MAPPER.readTree`.
816    fn render_assert_json_body(&self, out: &mut String, response_var: &str, expected: &serde_json::Value) {
817        let body_assertion = match expected {
818            serde_json::Value::Object(_) | serde_json::Value::Array(_) => {
819                let json_str = serde_json::to_string(expected).unwrap_or_default();
820                let escaped = escape_java(&json_str);
821                format!(
822                    "var bodyJson = MAPPER.readTree({response_var}.body());\n        var expectedJson = MAPPER.readTree(\"{escaped}\");\n        assertEquals(expectedJson, bodyJson, \"body mismatch\");"
823                )
824            }
825            serde_json::Value::String(s) => {
826                let escaped = escape_java(s);
827                format!("assertEquals(\"{escaped}\", {response_var}.body().trim(), \"body mismatch\");")
828            }
829            other => {
830                let escaped = escape_java(&other.to_string());
831                format!("assertEquals(\"{escaped}\", {response_var}.body().trim(), \"body mismatch\");")
832            }
833        };
834
835        let rendered = crate::template_env::render(
836            "java/http_assertions.jinja",
837            minijinja::context! {
838                response_var => response_var,
839                status_code => 0u16,
840                headers => Vec::<std::collections::HashMap<&str, String>>::new(),
841                body_assertion => body_assertion,
842                partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
843                validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
844            },
845        );
846        out.push_str(&rendered);
847    }
848
849    /// Emit partial JSON body assertions: parse once, then assert each expected field.
850    fn render_assert_partial_body(&self, out: &mut String, response_var: &str, expected: &serde_json::Value) {
851        if let Some(obj) = expected.as_object() {
852            let mut partial_body: Vec<std::collections::HashMap<&str, String>> = Vec::new();
853            for (key, val) in obj {
854                let escaped_key = escape_java(key);
855                let json_str = serde_json::to_string(val).unwrap_or_default();
856                let escaped_val = escape_java(&json_str);
857                let assertion_code = format!(
858                    "assertEquals(MAPPER.readTree(\"{escaped_val}\"), partialJson.get(\"{escaped_key}\"), \"body field '{escaped_key}' mismatch\");"
859                );
860                let mut entry = std::collections::HashMap::new();
861                entry.insert("assertion_code", assertion_code);
862                partial_body.push(entry);
863            }
864
865            let rendered = crate::template_env::render(
866                "java/http_assertions.jinja",
867                minijinja::context! {
868                    response_var => response_var,
869                    status_code => 0u16,
870                    headers => Vec::<std::collections::HashMap<&str, String>>::new(),
871                    body_assertion => String::new(),
872                    partial_body => partial_body,
873                    validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
874                },
875            );
876            out.push_str(&rendered);
877        }
878    }
879
880    /// Emit validation-error assertions: parse the body and check each expected message.
881    fn render_assert_validation_errors(
882        &self,
883        out: &mut String,
884        response_var: &str,
885        errors: &[crate::fixture::ValidationErrorExpectation],
886    ) {
887        let mut validation_errors: Vec<std::collections::HashMap<&str, String>> = Vec::new();
888        for err in errors {
889            let escaped_msg = escape_java(&err.msg);
890            let assertion_code = format!(
891                "assertTrue(veBody.contains(\"{escaped_msg}\"), \"expected validation error message: {escaped_msg}\");"
892            );
893            let mut entry = std::collections::HashMap::new();
894            entry.insert("assertion_code", assertion_code);
895            validation_errors.push(entry);
896        }
897
898        let rendered = crate::template_env::render(
899            "java/http_assertions.jinja",
900            minijinja::context! {
901                response_var => response_var,
902                status_code => 0u16,
903                headers => Vec::<std::collections::HashMap<&str, String>>::new(),
904                body_assertion => String::new(),
905                partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
906                validation_errors => validation_errors,
907            },
908        );
909        out.push_str(&rendered);
910    }
911}
912
913/// Render an HTTP server test method using `java.net.http.HttpClient` against
914/// `MOCK_SERVER_URL`. Delegates to the shared
915/// [`client::http_call::render_http_test`] driver via [`JavaTestClientRenderer`].
916///
917/// The one Java-specific pre-condition — HTTP 101 (WebSocket upgrade) causing an
918/// `EOFException` in `HttpClient` — is handled here before delegating.
919fn render_http_test_method(out: &mut String, fixture: &Fixture, http: &HttpFixture) {
920    // HTTP 101 (WebSocket upgrade) causes Java's HttpClient to throw EOFException.
921    // Emit an assumeTrue(false, ...) stub so the test is skipped rather than failing.
922    if http.expected_response.status_code == 101 {
923        let method_name = fixture.id.to_upper_camel_case();
924        let description = &fixture.description;
925        out.push_str(&crate::template_env::render(
926            "java/http_test_skip_101.jinja",
927            minijinja::context! {
928                method_name => method_name,
929                description => description,
930            },
931        ));
932        return;
933    }
934
935    client::http_call::render_http_test(out, &JavaTestClientRenderer, fixture);
936}
937
938#[allow(clippy::too_many_arguments)]
939fn render_test_method(
940    out: &mut String,
941    fixture: &Fixture,
942    class_name: &str,
943    _function_name: &str,
944    _result_var: &str,
945    _args: &[crate::config::ArgMapping],
946    options_type: Option<&str>,
947    field_resolver: &FieldResolver,
948    result_is_simple: bool,
949    enum_fields: &std::collections::HashSet<String>,
950    e2e_config: &E2eConfig,
951    nested_types: &std::collections::HashMap<String, String>,
952    nested_types_optional: bool,
953) {
954    // Delegate HTTP fixtures to the HTTP-specific renderer.
955    if let Some(http) = &fixture.http {
956        render_http_test_method(out, fixture, http);
957        return;
958    }
959
960    // Resolve per-fixture call config (supports named calls via fixture.call field).
961    // Use resolve_call_for_fixture to support auto-routing via select_when.
962    let call_config = e2e_config.resolve_call_for_fixture(fixture.call.as_deref(), &fixture.input);
963    let lang = "java";
964    let call_overrides = call_config.overrides.get(lang);
965    let effective_function_name = call_overrides
966        .and_then(|o| o.function.as_ref())
967        .cloned()
968        .unwrap_or_else(|| call_config.function.to_lower_camel_case());
969    let effective_result_var = &call_config.result_var;
970    let effective_args = &call_config.args;
971    let function_name = effective_function_name.as_str();
972    let result_var = effective_result_var.as_str();
973    let args: &[crate::config::ArgMapping] = effective_args.as_slice();
974
975    let method_name = fixture.id.to_upper_camel_case();
976    let description = &fixture.description;
977    let expects_error = fixture.assertions.iter().any(|a| a.assertion_type == "error");
978
979    // Resolve per-fixture options_type: prefer the java call override, fall back to
980    // class-level, then to any other language's options_type for the same call (the
981    // generated Java POJO class name matches the Rust type name across bindings, so
982    // mirroring the C/csharp/go option lets us auto-emit `Type.fromJson(json)` without
983    // requiring an explicit Java override per call).
984    let effective_options_type: Option<String> = call_overrides
985        .and_then(|o| o.options_type.clone())
986        .or_else(|| options_type.map(|s| s.to_string()))
987        .or_else(|| {
988            // Borrow from any other backend's options_type. Prefer non-language-prefixed
989            // names (csharp/c/go/php/python) over wasm or ruby which use prefixed types
990            // like `WasmCreateBatchRequest` or `LiterLlm::CreateBatchRequest`.
991            for cand in ["csharp", "c", "go", "php", "python"] {
992                if let Some(o) = call_config.overrides.get(cand) {
993                    if let Some(t) = &o.options_type {
994                        return Some(t.clone());
995                    }
996                }
997            }
998            None
999        });
1000    let effective_options_type = effective_options_type.as_deref();
1001    // When options_type is resolvable but no explicit options_via is given for Java,
1002    // default to "from_json" so the typed-request arg is emitted as
1003    // `Type.fromJson(json)` rather than the raw JSON string. The Java backend exposes
1004    // a static `fromJson(String)` factory on every record type (Stage A).
1005    let auto_from_json = effective_options_type.is_some()
1006        && call_overrides.and_then(|o| o.options_via.as_deref()).is_none()
1007        && e2e_config
1008            .call
1009            .overrides
1010            .get(lang)
1011            .and_then(|o| o.options_via.as_deref())
1012            .is_none();
1013
1014    // Resolve client_factory: prefer call-level java override, fall back to file-level java override.
1015    let client_factory: Option<String> = call_overrides.and_then(|o| o.client_factory.clone()).or_else(|| {
1016        e2e_config
1017            .call
1018            .overrides
1019            .get(lang)
1020            .and_then(|o| o.client_factory.clone())
1021    });
1022
1023    // Resolve options_via: "kwargs" (default), "from_json", "json", "dict".
1024    // Auto-default to "from_json" when an options_type is resolvable and no explicit
1025    // options_via is configured — this lets typed-request args emit `Type.fromJson(json)`
1026    // even when alef.toml only declares the type in another binding's override block.
1027    let options_via: String = call_overrides
1028        .and_then(|o| o.options_via.clone())
1029        .or_else(|| e2e_config.call.overrides.get(lang).and_then(|o| o.options_via.clone()))
1030        .unwrap_or_else(|| {
1031            if auto_from_json {
1032                "from_json".to_string()
1033            } else {
1034                "kwargs".to_string()
1035            }
1036        });
1037
1038    // Resolve per-fixture result_is_simple and result_is_bytes from the call override.
1039    let effective_result_is_simple =
1040        call_overrides.is_some_and(|o| o.result_is_simple) || call_config.result_is_simple || result_is_simple;
1041    let effective_result_is_bytes = call_overrides.is_some_and(|o| o.result_is_bytes);
1042
1043    // Check if this test needs ObjectMapper deserialization for json_object args.
1044    let needs_deser = effective_options_type.is_some()
1045        && args.iter().any(|arg| {
1046            if arg.arg_type != "json_object" {
1047                return false;
1048            }
1049            let val = super::resolve_field(&fixture.input, &arg.field);
1050            !val.is_null() && !val.is_array()
1051        });
1052
1053    // Emit builder expressions for json_object args.
1054    let mut builder_expressions = String::new();
1055    if let (true, Some(opts_type)) = (needs_deser, effective_options_type) {
1056        for arg in args {
1057            if arg.arg_type == "json_object" {
1058                let val = super::resolve_field(&fixture.input, &arg.field);
1059                if !val.is_null() && !val.is_array() {
1060                    if options_via == "from_json" {
1061                        // Build the typed POJO via static fromJson(String) method.
1062                        let json_str = serde_json::to_string(val).unwrap_or_default();
1063                        let escaped = escape_java(&json_str);
1064                        let var_name = &arg.name;
1065                        builder_expressions.push_str(&format!(
1066                            "        var {var_name} = {opts_type}.fromJson(\"{escaped}\");\n",
1067                        ));
1068                    } else if let Some(obj) = val.as_object() {
1069                        // Generate builder expression: TypeName.builder().withFieldName(value)...build()
1070                        let empty_path_fields: Vec<String> = Vec::new();
1071                        let path_fields = call_overrides.map(|o| &o.path_fields).unwrap_or(&empty_path_fields);
1072                        let builder_expr = java_builder_expression(
1073                            obj,
1074                            opts_type,
1075                            enum_fields,
1076                            nested_types,
1077                            nested_types_optional,
1078                            path_fields,
1079                        );
1080                        let var_name = &arg.name;
1081                        builder_expressions.push_str(&format!("        var {} = {};\n", var_name, builder_expr));
1082                    }
1083                }
1084            }
1085        }
1086    }
1087
1088    let (mut setup_lines, args_str) =
1089        build_args_and_setup(&fixture.input, args, class_name, effective_options_type, fixture);
1090
1091    // Per-language `extra_args` from call overrides — verbatim trailing
1092    // expressions appended after the configured args (e.g. `null` for an
1093    // optional trailing parameter the fixture cannot supply). Mirrors the
1094    // TypeScript and C# implementations.
1095    let extra_args_slice: &[String] = call_overrides.map_or(&[], |o| o.extra_args.as_slice());
1096
1097    // Build visitor if present and add to setup
1098    let mut visitor_var = String::new();
1099    let mut has_visitor_fixture = false;
1100    if let Some(visitor_spec) = &fixture.visitor {
1101        visitor_var = build_java_visitor(&mut setup_lines, visitor_spec, class_name);
1102        has_visitor_fixture = true;
1103    }
1104
1105    // When visitor is present, attach it to the options parameter
1106    let mut final_args = if has_visitor_fixture {
1107        if args_str.is_empty() {
1108            format!("new ConversionOptions().withVisitor({})", visitor_var)
1109        } else if args_str.contains("new ConversionOptions")
1110            || args_str.contains("ConversionOptionsBuilder")
1111            || args_str.contains(".builder()")
1112        {
1113            // Options are being built (either new ConversionOptions(), builder pattern, or .builder().build())
1114            // append .withVisitor() call before .build() if present
1115            if args_str.contains(".build()") {
1116                let idx = args_str.rfind(".build()").unwrap();
1117                format!("{}.withVisitor({}){}", &args_str[..idx], visitor_var, &args_str[idx..])
1118            } else {
1119                format!("{}.withVisitor({})", args_str, visitor_var)
1120            }
1121        } else if args_str.ends_with(", null") {
1122            let base = &args_str[..args_str.len() - 6];
1123            format!("{}, new ConversionOptions().withVisitor({})", base, visitor_var)
1124        } else {
1125            format!("{}, new ConversionOptions().withVisitor({})", args_str, visitor_var)
1126        }
1127    } else {
1128        args_str
1129    };
1130
1131    if !extra_args_slice.is_empty() {
1132        let extra_str = extra_args_slice.join(", ");
1133        final_args = if final_args.is_empty() {
1134            extra_str
1135        } else {
1136            format!("{final_args}, {extra_str}")
1137        };
1138    }
1139
1140    // Render assertions_body
1141    let mut assertions_body = String::new();
1142
1143    // Emit a `source` variable for run_query assertions that need the raw bytes.
1144    let needs_source_var = fixture
1145        .assertions
1146        .iter()
1147        .any(|a| a.assertion_type == "method_result" && a.method.as_deref() == Some("run_query"));
1148    if needs_source_var {
1149        if let Some(source_arg) = args.iter().find(|a| a.field == "source_code") {
1150            let field = source_arg.field.strip_prefix("input.").unwrap_or(&source_arg.field);
1151            if let Some(val) = fixture.input.get(field) {
1152                let java_val = json_to_java(val);
1153                assertions_body.push_str(&format!("        var source = {}.getBytes();\n", java_val));
1154            }
1155        }
1156    }
1157
1158    // Merge per-call java enum_fields with the file-level java enum_fields so that
1159    // call-specific enum-typed result fields (e.g. `choices[0].finish_reason` for
1160    // chat) trigger Optional<Enum> coercion even when the global override block
1161    // does not list them. Per-call entries take precedence.
1162    // Combine global enum_fields (HashSet) with per-call overrides (HashMap).
1163    let mut effective_enum_fields: std::collections::HashSet<String> = enum_fields.clone();
1164    if let Some(co) = call_overrides {
1165        for k in co.enum_fields.keys() {
1166            effective_enum_fields.insert(k.clone());
1167        }
1168    }
1169
1170    for assertion in &fixture.assertions {
1171        render_assertion(
1172            &mut assertions_body,
1173            assertion,
1174            result_var,
1175            class_name,
1176            field_resolver,
1177            effective_result_is_simple,
1178            effective_result_is_bytes,
1179            &effective_enum_fields,
1180        );
1181    }
1182
1183    let throws_clause = " throws Exception";
1184
1185    // When client_factory is set, instantiate a client and dispatch the call as
1186    // a method on the client; otherwise call the static helper on `class_name`.
1187    let (client_setup_lines, call_target) = if let Some(factory) = client_factory.as_deref() {
1188        let factory_name = factory.to_lower_camel_case();
1189        let fixture_id = &fixture.id;
1190        let mut setup: Vec<String> = Vec::new();
1191        let has_mock = fixture.mock_response.is_some() || fixture.http.is_some();
1192        let api_key_var = fixture.env.as_ref().and_then(|e| e.api_key_var.as_deref());
1193        if let Some(var) = api_key_var.filter(|_| has_mock) {
1194            setup.push(format!("String apiKey = System.getenv(\"{var}\");"));
1195            setup.push(format!(
1196                "String baseUrl = (apiKey != null && !apiKey.isEmpty()) ? null : System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\";"
1197            ));
1198            setup.push(format!(
1199                "System.out.println(\"{fixture_id}: \" + (baseUrl == null ? \"using real API ({var} is set)\" : \"using mock server ({var} not set)\"));"
1200            ));
1201            setup.push(format!(
1202                "var client = {class_name}.{factory_name}(baseUrl == null ? apiKey : \"test-key\", baseUrl, null, null, null);"
1203            ));
1204        } else if has_mock {
1205            if fixture.has_host_root_route() {
1206                setup.push(format!(
1207                    "String mockUrl = System.getProperty(\"mockServer.{fixture_id}\", System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\");"
1208                ));
1209            } else {
1210                setup.push(format!(
1211                    "String mockUrl = System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\";"
1212                ));
1213            }
1214            setup.push(format!(
1215                "var client = {class_name}.{factory_name}(\"test-key\", mockUrl, null, null, null);"
1216            ));
1217        } else if let Some(api_key_var) = api_key_var {
1218            setup.push(format!("String apiKey = System.getenv(\"{api_key_var}\");"));
1219            setup.push(format!(
1220                "org.junit.jupiter.api.Assumptions.assumeTrue(apiKey != null && !apiKey.isEmpty(), \"{api_key_var} not set\");"
1221            ));
1222            setup.push(format!("var client = {class_name}.{factory_name}(apiKey);"));
1223        } else {
1224            setup.push(format!("var client = {class_name}.{factory_name}(\"test-key\");"));
1225        }
1226        (setup, "client".to_string())
1227    } else {
1228        (Vec::new(), class_name.to_string())
1229    };
1230
1231    // Prepend client setup before any other setup_lines.
1232    let combined_setup: Vec<String> = client_setup_lines.into_iter().chain(setup_lines).collect();
1233
1234    let call_expr = format!("{call_target}.{function_name}({final_args})");
1235
1236    let rendered = crate::template_env::render(
1237        "java/test_method.jinja",
1238        minijinja::context! {
1239            method_name => method_name,
1240            description => description,
1241            builder_expressions => builder_expressions,
1242            setup_lines => combined_setup,
1243            throws_clause => throws_clause,
1244            expects_error => expects_error,
1245            call_expr => call_expr,
1246            result_var => result_var,
1247            assertions_body => assertions_body,
1248        },
1249    );
1250    out.push_str(&rendered);
1251}
1252
1253/// Build setup lines (e.g. handle creation) and the argument list for the function call.
1254///
1255/// Returns `(setup_lines, args_string)`.
1256fn build_args_and_setup(
1257    input: &serde_json::Value,
1258    args: &[crate::config::ArgMapping],
1259    class_name: &str,
1260    options_type: Option<&str>,
1261    fixture: &crate::fixture::Fixture,
1262) -> (Vec<String>, String) {
1263    let fixture_id = &fixture.id;
1264    if args.is_empty() {
1265        return (Vec::new(), String::new());
1266    }
1267
1268    let mut setup_lines: Vec<String> = Vec::new();
1269    let mut parts: Vec<String> = Vec::new();
1270
1271    for arg in args {
1272        if arg.arg_type == "mock_url" {
1273            if fixture.has_host_root_route() {
1274                setup_lines.push(format!(
1275                    "String {} = System.getProperty(\"mockServer.{fixture_id}\", System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\");",
1276                    arg.name,
1277                ));
1278            } else {
1279                setup_lines.push(format!(
1280                    "String {} = System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\";",
1281                    arg.name,
1282                ));
1283            }
1284            parts.push(arg.name.clone());
1285            continue;
1286        }
1287
1288        if arg.arg_type == "handle" {
1289            // Generate a createEngine (or equivalent) call and pass the variable.
1290            let constructor_name = format!("create{}", arg.name.to_upper_camel_case());
1291            let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1292            let config_value = input.get(field).unwrap_or(&serde_json::Value::Null);
1293            if config_value.is_null()
1294                || config_value.is_object() && config_value.as_object().is_some_and(|o| o.is_empty())
1295            {
1296                setup_lines.push(format!("var {} = {class_name}.{constructor_name}(null);", arg.name,));
1297            } else {
1298                let json_str = serde_json::to_string(config_value).unwrap_or_default();
1299                let name = &arg.name;
1300                setup_lines.push(format!(
1301                    "var {name}Config = MAPPER.readValue(\"{}\", CrawlConfig.class);",
1302                    escape_java(&json_str),
1303                ));
1304                setup_lines.push(format!(
1305                    "var {} = {class_name}.{constructor_name}({name}Config);",
1306                    arg.name,
1307                    name = name,
1308                ));
1309            }
1310            parts.push(arg.name.clone());
1311            continue;
1312        }
1313
1314        let resolved = super::resolve_field(input, &arg.field);
1315        let val = if resolved.is_null() { None } else { Some(resolved) };
1316        match val {
1317            None | Some(serde_json::Value::Null) if arg.optional => {
1318                // Optional arg with no fixture value: emit positional null/default so the call
1319                // has the right arity. For json_object optional args, build an empty default object
1320                // so we get the right type rather than a raw null.
1321                if arg.arg_type == "json_object" {
1322                    if let Some(opts_type) = options_type {
1323                        parts.push(format!("{opts_type}.builder().build()"));
1324                    } else {
1325                        parts.push("null".to_string());
1326                    }
1327                } else {
1328                    parts.push("null".to_string());
1329                }
1330            }
1331            None | Some(serde_json::Value::Null) => {
1332                // Required arg with no fixture value: pass a language-appropriate default.
1333                let default_val = match arg.arg_type.as_str() {
1334                    "string" | "file_path" => "\"\"".to_string(),
1335                    "int" | "integer" => "0".to_string(),
1336                    "float" | "number" => "0.0d".to_string(),
1337                    "bool" | "boolean" => "false".to_string(),
1338                    _ => "null".to_string(),
1339                };
1340                parts.push(default_val);
1341            }
1342            Some(v) => {
1343                if arg.arg_type == "json_object" {
1344                    // Array json_object args: emit inline Java list expression.
1345                    // Check for batch item arrays first (element_type = BatchBytesItem/BatchFileItem).
1346                    if v.is_array() {
1347                        if let Some(elem_type) = &arg.element_type {
1348                            if elem_type == "BatchBytesItem" || elem_type == "BatchFileItem" {
1349                                parts.push(emit_java_batch_item_array(v, elem_type));
1350                                continue;
1351                            }
1352                        }
1353                        // Otherwise use element_type to emit the correct numeric literal suffix (f vs d).
1354                        let elem_type = arg.element_type.as_deref();
1355                        parts.push(json_to_java_typed(v, elem_type));
1356                        continue;
1357                    }
1358                    // Object json_object args with options_type: use pre-deserialized variable.
1359                    if options_type.is_some() {
1360                        parts.push(arg.name.clone());
1361                        continue;
1362                    }
1363                    parts.push(json_to_java(v));
1364                    continue;
1365                }
1366                // bytes args must be passed as byte[], not String.
1367                if arg.arg_type == "bytes" {
1368                    let val = json_to_java(v);
1369                    parts.push(format!("{val}.getBytes()"));
1370                    continue;
1371                }
1372                // file_path args must be wrapped in java.nio.file.Path.of().
1373                if arg.arg_type == "file_path" {
1374                    let val = json_to_java(v);
1375                    parts.push(format!("java.nio.file.Path.of({val})"));
1376                    continue;
1377                }
1378                parts.push(json_to_java(v));
1379            }
1380        }
1381    }
1382
1383    (setup_lines, parts.join(", "))
1384}
1385
1386#[allow(clippy::too_many_arguments)]
1387fn render_assertion(
1388    out: &mut String,
1389    assertion: &Assertion,
1390    result_var: &str,
1391    class_name: &str,
1392    field_resolver: &FieldResolver,
1393    result_is_simple: bool,
1394    result_is_bytes: bool,
1395    enum_fields: &std::collections::HashSet<String>,
1396) {
1397    // Byte-buffer returns: emit length-based assertions instead of struct-field
1398    // accessors. The result is `byte[]`, which has no `isEmpty()`/struct-field methods.
1399    // Field paths on byte-buffer results (e.g. `audio`, `content`) are pseudo-fields
1400    // referencing the buffer itself — treat them the same as no-field assertions.
1401    if result_is_bytes {
1402        match assertion.assertion_type.as_str() {
1403            "not_empty" => {
1404                out.push_str(&format!(
1405                    "        assertTrue({result_var}.length > 0, \"expected non-empty value\");\n"
1406                ));
1407                return;
1408            }
1409            "is_empty" => {
1410                out.push_str(&format!(
1411                    "        assertEquals(0, {result_var}.length, \"expected empty value\");\n"
1412                ));
1413                return;
1414            }
1415            "count_equals" | "length_equals" => {
1416                if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1417                    out.push_str(&format!("        assertEquals({n}, {result_var}.length);\n"));
1418                }
1419                return;
1420            }
1421            "count_min" | "length_min" => {
1422                if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1423                    out.push_str(&format!(
1424                        "        assertTrue({result_var}.length >= {n}, \"expected length >= {n}\");\n"
1425                    ));
1426                }
1427                return;
1428            }
1429            _ => {
1430                out.push_str(&format!(
1431                    "        // skipped: assertion type '{}' not supported on byte[] result\n",
1432                    assertion.assertion_type
1433                ));
1434                return;
1435            }
1436        }
1437    }
1438
1439    // Handle synthetic/virtual fields that are computed rather than direct record accessors.
1440    if let Some(f) = &assertion.field {
1441        match f.as_str() {
1442            // ---- ExtractionResult chunk-level computed predicates ----
1443            "chunks_have_content" => {
1444                let pred = format!(
1445                    "{result_var}.chunks().orElse(java.util.List.of()).stream().allMatch(c -> c.content() != null && !c.content().isBlank())"
1446                );
1447                out.push_str(&crate::template_env::render(
1448                    "java/synthetic_assertion.jinja",
1449                    minijinja::context! {
1450                        assertion_kind => "chunks_content",
1451                        assertion_type => assertion.assertion_type.as_str(),
1452                        pred => pred,
1453                        field_name => f,
1454                    },
1455                ));
1456                return;
1457            }
1458            "chunks_have_heading_context" => {
1459                let pred = format!(
1460                    "{result_var}.chunks().orElse(java.util.List.of()).stream().allMatch(c -> c.metadata().headingContext().isPresent())"
1461                );
1462                out.push_str(&crate::template_env::render(
1463                    "java/synthetic_assertion.jinja",
1464                    minijinja::context! {
1465                        assertion_kind => "chunks_heading_context",
1466                        assertion_type => assertion.assertion_type.as_str(),
1467                        pred => pred,
1468                        field_name => f,
1469                    },
1470                ));
1471                return;
1472            }
1473            "chunks_have_embeddings" => {
1474                let pred = format!(
1475                    "{result_var}.chunks().orElse(java.util.List.of()).stream().allMatch(c -> c.embedding() != null && !c.embedding().isEmpty())"
1476                );
1477                out.push_str(&crate::template_env::render(
1478                    "java/synthetic_assertion.jinja",
1479                    minijinja::context! {
1480                        assertion_kind => "chunks_embeddings",
1481                        assertion_type => assertion.assertion_type.as_str(),
1482                        pred => pred,
1483                        field_name => f,
1484                    },
1485                ));
1486                return;
1487            }
1488            "first_chunk_starts_with_heading" => {
1489                let pred = format!(
1490                    "{result_var}.chunks().orElse(java.util.List.of()).stream().findFirst().map(c -> c.metadata().headingContext().isPresent()).orElse(false)"
1491                );
1492                out.push_str(&crate::template_env::render(
1493                    "java/synthetic_assertion.jinja",
1494                    minijinja::context! {
1495                        assertion_kind => "first_chunk_heading",
1496                        assertion_type => assertion.assertion_type.as_str(),
1497                        pred => pred,
1498                        field_name => f,
1499                    },
1500                ));
1501                return;
1502            }
1503            // ---- EmbedResponse virtual fields ----
1504            // When result_is_simple=true the result IS List<List<Float>> (the raw embeddings list).
1505            // When result_is_simple=false the result has an .embeddings() accessor.
1506            "embedding_dimensions" => {
1507                // Dimension = size of the first embedding vector in the list.
1508                let embed_list = if result_is_simple {
1509                    result_var.to_string()
1510                } else {
1511                    format!("{result_var}.embeddings()")
1512                };
1513                let expr = format!("({embed_list}.isEmpty() ? 0 : {embed_list}.get(0).size())");
1514                let java_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
1515                out.push_str(&crate::template_env::render(
1516                    "java/synthetic_assertion.jinja",
1517                    minijinja::context! {
1518                        assertion_kind => "embedding_dimensions",
1519                        assertion_type => assertion.assertion_type.as_str(),
1520                        expr => expr,
1521                        java_val => java_val,
1522                        field_name => f,
1523                    },
1524                ));
1525                return;
1526            }
1527            "embeddings_valid" | "embeddings_finite" | "embeddings_non_zero" | "embeddings_normalized" => {
1528                // These are validation predicates that require iterating the embedding matrix.
1529                let embed_list = if result_is_simple {
1530                    result_var.to_string()
1531                } else {
1532                    format!("{result_var}.embeddings()")
1533                };
1534                let pred = match f.as_str() {
1535                    "embeddings_valid" => {
1536                        format!("{embed_list}.stream().allMatch(e -> e != null && !e.isEmpty())")
1537                    }
1538                    "embeddings_finite" => {
1539                        format!("{embed_list}.stream().flatMap(java.util.Collection::stream).allMatch(Float::isFinite)")
1540                    }
1541                    "embeddings_non_zero" => {
1542                        format!("{embed_list}.stream().allMatch(e -> e.stream().anyMatch(v -> v != 0.0f))")
1543                    }
1544                    "embeddings_normalized" => format!(
1545                        "{embed_list}.stream().allMatch(e -> {{ double n = e.stream().mapToDouble(v -> v * v).sum(); return Math.abs(n - 1.0) < 1e-3; }})"
1546                    ),
1547                    _ => unreachable!(),
1548                };
1549                let assertion_kind = format!("embeddings_{}", f.strip_prefix("embeddings_").unwrap_or(f));
1550                out.push_str(&crate::template_env::render(
1551                    "java/synthetic_assertion.jinja",
1552                    minijinja::context! {
1553                        assertion_kind => assertion_kind,
1554                        assertion_type => assertion.assertion_type.as_str(),
1555                        pred => pred,
1556                        field_name => f,
1557                    },
1558                ));
1559                return;
1560            }
1561            // ---- Fields not present on the Java ExtractionResult ----
1562            "keywords" | "keywords_count" => {
1563                out.push_str(&crate::template_env::render(
1564                    "java/synthetic_assertion.jinja",
1565                    minijinja::context! {
1566                        assertion_kind => "keywords",
1567                        field_name => f,
1568                    },
1569                ));
1570                return;
1571            }
1572            // ---- metadata not_empty / is_empty: Metadata is a required record, not Optional ----
1573            // Metadata has no .isEmpty() method; check that at least one optional field is present.
1574            "metadata" => {
1575                match assertion.assertion_type.as_str() {
1576                    "not_empty" | "is_empty" => {
1577                        out.push_str(&crate::template_env::render(
1578                            "java/synthetic_assertion.jinja",
1579                            minijinja::context! {
1580                                assertion_kind => "metadata",
1581                                assertion_type => assertion.assertion_type.as_str(),
1582                                result_var => result_var,
1583                            },
1584                        ));
1585                        return;
1586                    }
1587                    _ => {} // fall through to normal handling
1588                }
1589            }
1590            _ => {}
1591        }
1592    }
1593
1594    // Skip assertions on fields that don't exist on the result type.
1595    if let Some(f) = &assertion.field {
1596        if !f.is_empty() && !field_resolver.is_valid_for_result(f) {
1597            out.push_str(&crate::template_env::render(
1598                "java/synthetic_assertion.jinja",
1599                minijinja::context! {
1600                    assertion_kind => "skipped",
1601                    field_name => f,
1602                },
1603            ));
1604            return;
1605        }
1606    }
1607
1608    // Determine if this field is an enum type (no `.contains()` on enums in Java).
1609    // Check both the raw fixture field path and the resolved (aliased) path so that
1610    // `fields_enum` entries can use either form (e.g., `"assets[].category"` or the
1611    // resolved `"assets[].asset_category"`).
1612    let field_is_enum = assertion
1613        .field
1614        .as_deref()
1615        .is_some_and(|f| enum_fields.contains(f) || enum_fields.contains(field_resolver.resolve(f)));
1616
1617    // Determine if this field is an array (List<T>) — needed to choose .toString() for
1618    // contains assertions, since List.contains(Object) uses equals() which won't match
1619    // strings against complex record types like StructureItem.
1620    let field_is_array = assertion
1621        .field
1622        .as_deref()
1623        .is_some_and(|f| field_resolver.is_array(field_resolver.resolve(f)));
1624
1625    let field_expr = if result_is_simple {
1626        result_var.to_string()
1627    } else {
1628        match &assertion.field {
1629            Some(f) if !f.is_empty() => {
1630                let accessor = field_resolver.accessor(f, "java", result_var);
1631                let resolved = field_resolver.resolve(f);
1632                // Unwrap Optional fields with a type-appropriate fallback.
1633                // Map.get() returns nullable, not Optional, so skip .orElse() for map access.
1634                // NOTE: is_optional() means the field is in optional_fields, but that doesn't
1635                // guarantee it returns Optional<T> in Java — nested fields like metadata.twitterCard
1636                // return @Nullable String, not Optional<String>. We detect this by checking
1637                // if the field path contains a dot (nested access).
1638                if field_resolver.is_optional(resolved) && !field_resolver.has_map_access(f) {
1639                    // All nullable fields in the Java binding return @Nullable types, not Optional<T>.
1640                    // Wrap them in Optional.ofNullable() so e2e tests can use .orElse() fallbacks.
1641                    let optional_expr = format!("java.util.Optional.ofNullable({accessor})");
1642                    // Enum-typed optional fields need .map(v -> v.getValue()) to coerce to String
1643                    // before the orElse("") fallback can type-check (Optional<Enum>.orElse("") would
1644                    // be a type mismatch — Optional<String>.orElse("") is the only safe form).
1645                    if field_is_enum {
1646                        match assertion.assertion_type.as_str() {
1647                            "not_empty" | "is_empty" => optional_expr,
1648                            _ => format!("{optional_expr}.map(v -> v.getValue()).orElse(\"\")"),
1649                        }
1650                    } else {
1651                        match assertion.assertion_type.as_str() {
1652                            // For not_empty / is_empty on Optional fields, return the raw Optional
1653                            // so the assertion arms can call isPresent()/isEmpty().
1654                            "not_empty" | "is_empty" => optional_expr,
1655                            // For size/count assertions on Optional<List<T>> fields, use List.of() fallback.
1656                            "count_min" | "count_equals" => {
1657                                format!("{optional_expr}.orElse(java.util.List.of())")
1658                            }
1659                            // For numeric comparisons on Optional<Long/Integer> fields, use 0L.
1660                            "greater_than" | "less_than" | "greater_than_or_equal" | "less_than_or_equal" => {
1661                                if field_resolver.is_array(resolved) {
1662                                    format!("{optional_expr}.orElse(java.util.List.of())")
1663                                } else {
1664                                    format!("{optional_expr}.orElse(0L)")
1665                                }
1666                            }
1667                            // For equals on Optional fields, determine fallback based on whether value is numeric.
1668                            // If the fixture value is a number, use 0L; otherwise use "".
1669                            "equals" => {
1670                                if let Some(expected) = &assertion.value {
1671                                    if expected.is_number() {
1672                                        format!("{optional_expr}.orElse(0L)")
1673                                    } else {
1674                                        format!("{optional_expr}.orElse(\"\")")
1675                                    }
1676                                } else {
1677                                    format!("{optional_expr}.orElse(\"\")")
1678                                }
1679                            }
1680                            _ if field_resolver.is_array(resolved) => {
1681                                format!("{optional_expr}.orElse(java.util.List.of())")
1682                            }
1683                            _ => format!("{optional_expr}.orElse(\"\")"),
1684                        }
1685                    }
1686                } else {
1687                    accessor
1688                }
1689            }
1690            _ => result_var.to_string(),
1691        }
1692    };
1693
1694    // For enum fields, string-based assertions need .getValue() to convert the enum to
1695    // its serde-serialized lowercase string value (e.g., AssetCategory.Image -> "image").
1696    // All alef-generated Java enums expose a getValue() method annotated with @JsonValue.
1697    // Optional enum fields are already coerced to String via `.map(v -> v.getValue()).orElse("")`
1698    // upstream in field_expr; in that case the value is already a String and we must not
1699    // call .getValue() again. Detect by looking for `.map(v -> v.getValue())` in the expr.
1700    let string_expr = if field_is_enum && !field_expr.contains(".map(v -> v.getValue())") {
1701        format!("{field_expr}.getValue()")
1702    } else {
1703        field_expr.clone()
1704    };
1705
1706    // Pre-compute context for template
1707    let assertion_type = assertion.assertion_type.as_str();
1708    let java_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
1709    let is_string_val = assertion.value.as_ref().is_some_and(|v| v.is_string());
1710    let is_numeric_val = assertion.value.as_ref().is_some_and(|v| v.is_number());
1711
1712    let values_java: Vec<String> = assertion
1713        .values
1714        .as_ref()
1715        .map(|values| values.iter().map(json_to_java).collect())
1716        .unwrap_or_default();
1717
1718    let contains_any_expr = if !values_java.is_empty() {
1719        values_java
1720            .iter()
1721            .map(|v| format!("{string_expr}.contains({v})"))
1722            .collect::<Vec<_>>()
1723            .join(" || ")
1724    } else {
1725        String::new()
1726    };
1727
1728    let length_expr = if result_is_bytes {
1729        format!("{field_expr}.length")
1730    } else {
1731        format!("{field_expr}.length()")
1732    };
1733
1734    let n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);
1735
1736    let call_expr = if let Some(method_name) = &assertion.method {
1737        build_java_method_call(result_var, method_name, assertion.args.as_ref(), class_name)
1738    } else {
1739        String::new()
1740    };
1741
1742    let check = assertion.check.as_deref().unwrap_or("is_true");
1743
1744    let java_check_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
1745
1746    let check_n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);
1747
1748    let is_bool_val = assertion.value.as_ref().is_some_and(|v| v.is_boolean());
1749    let bool_is_true = assertion.value.as_ref().is_some_and(|v| v.as_bool() == Some(true));
1750
1751    let method_returns_collection = assertion
1752        .method
1753        .as_ref()
1754        .is_some_and(|m| matches!(m.as_str(), "find_nodes_by_type" | "findNodesByType"));
1755
1756    let rendered = crate::template_env::render(
1757        "java/assertion.jinja",
1758        minijinja::context! {
1759            assertion_type,
1760            java_val,
1761            string_expr,
1762            field_expr,
1763            field_is_enum,
1764            field_is_array,
1765            is_string_val,
1766            is_numeric_val,
1767            values_java => values_java,
1768            contains_any_expr,
1769            length_expr,
1770            n,
1771            call_expr,
1772            check,
1773            java_check_val,
1774            check_n,
1775            is_bool_val,
1776            bool_is_true,
1777            method_returns_collection,
1778        },
1779    );
1780    out.push_str(&rendered);
1781}
1782
1783/// Build a Java call expression for a `method_result` assertion on a tree-sitter Tree.
1784///
1785/// Maps method names to the appropriate Java static/instance method calls.
1786fn build_java_method_call(
1787    result_var: &str,
1788    method_name: &str,
1789    args: Option<&serde_json::Value>,
1790    class_name: &str,
1791) -> String {
1792    match method_name {
1793        "root_child_count" => format!("{result_var}.rootNode().childCount()"),
1794        "root_node_type" => format!("{result_var}.rootNode().kind()"),
1795        "named_children_count" => format!("{result_var}.rootNode().namedChildCount()"),
1796        "has_error_nodes" => format!("{class_name}.treeHasErrorNodes({result_var})"),
1797        "error_count" | "tree_error_count" => format!("{class_name}.treeErrorCount({result_var})"),
1798        "tree_to_sexp" => format!("{class_name}.treeToSexp({result_var})"),
1799        "contains_node_type" => {
1800            let node_type = args
1801                .and_then(|a| a.get("node_type"))
1802                .and_then(|v| v.as_str())
1803                .unwrap_or("");
1804            format!("{class_name}.treeContainsNodeType({result_var}, \"{node_type}\")")
1805        }
1806        "find_nodes_by_type" => {
1807            let node_type = args
1808                .and_then(|a| a.get("node_type"))
1809                .and_then(|v| v.as_str())
1810                .unwrap_or("");
1811            format!("{class_name}.findNodesByType({result_var}, \"{node_type}\")")
1812        }
1813        "run_query" => {
1814            let query_source = args
1815                .and_then(|a| a.get("query_source"))
1816                .and_then(|v| v.as_str())
1817                .unwrap_or("");
1818            let language = args
1819                .and_then(|a| a.get("language"))
1820                .and_then(|v| v.as_str())
1821                .unwrap_or("");
1822            let escaped_query = escape_java(query_source);
1823            format!("{class_name}.runQuery({result_var}, \"{language}\", \"{escaped_query}\", source)")
1824        }
1825        _ => {
1826            format!("{result_var}.{}()", method_name.to_lower_camel_case())
1827        }
1828    }
1829}
1830
1831/// Convert a `serde_json::Value` to a Java literal string.
1832fn json_to_java(value: &serde_json::Value) -> String {
1833    json_to_java_typed(value, None)
1834}
1835
1836/// Convert a JSON value to a Java literal, optionally overriding number type for array elements.
1837/// `element_type` controls how numeric array elements are emitted: "f32" → `1.0f`, otherwise `1.0d`.
1838/// Emit Java batch item constructors for BatchBytesItem or BatchFileItem arrays.
1839fn emit_java_batch_item_array(arr: &serde_json::Value, elem_type: &str) -> String {
1840    if let Some(items) = arr.as_array() {
1841        let item_strs: Vec<String> = items
1842            .iter()
1843            .filter_map(|item| {
1844                if let Some(obj) = item.as_object() {
1845                    match elem_type {
1846                        "BatchBytesItem" => {
1847                            let content = obj.get("content").and_then(|v| v.as_array());
1848                            let mime_type = obj.get("mime_type").and_then(|v| v.as_str()).unwrap_or("text/plain");
1849                            let content_code = if let Some(arr) = content {
1850                                let bytes: Vec<String> = arr
1851                                    .iter()
1852                                    .filter_map(|v| v.as_u64().map(|n| format!("(byte) {}", n)))
1853                                    .collect();
1854                                format!("new byte[] {{{}}}", bytes.join(", "))
1855                            } else {
1856                                "new byte[] {}".to_string()
1857                            };
1858                            Some(format!("new {}({}, \"{}\", null)", elem_type, content_code, mime_type))
1859                        }
1860                        "BatchFileItem" => {
1861                            let path = obj.get("path").and_then(|v| v.as_str()).unwrap_or("");
1862                            Some(format!(
1863                                "new {}(java.nio.file.Paths.get(\"{}\"), null)",
1864                                elem_type, path
1865                            ))
1866                        }
1867                        _ => None,
1868                    }
1869                } else {
1870                    None
1871                }
1872            })
1873            .collect();
1874        format!("java.util.Arrays.asList({})", item_strs.join(", "))
1875    } else {
1876        "java.util.List.of()".to_string()
1877    }
1878}
1879
1880fn json_to_java_typed(value: &serde_json::Value, element_type: Option<&str>) -> String {
1881    match value {
1882        serde_json::Value::String(s) => format!("\"{}\"", escape_java(s)),
1883        serde_json::Value::Bool(b) => b.to_string(),
1884        serde_json::Value::Number(n) => {
1885            if n.is_f64() {
1886                match element_type {
1887                    Some("f32" | "float" | "Float") => format!("{}f", n),
1888                    _ => format!("{}d", n),
1889                }
1890            } else {
1891                n.to_string()
1892            }
1893        }
1894        serde_json::Value::Null => "null".to_string(),
1895        serde_json::Value::Array(arr) => {
1896            let items: Vec<String> = arr.iter().map(|v| json_to_java_typed(v, element_type)).collect();
1897            format!("java.util.List.of({})", items.join(", "))
1898        }
1899        serde_json::Value::Object(_) => {
1900            let json_str = serde_json::to_string(value).unwrap_or_default();
1901            format!("\"{}\"", escape_java(&json_str))
1902        }
1903    }
1904}
1905
1906/// Generate a Java builder expression for a JSON object.
1907/// E.g., `obj = {"language": "abl", "chunk_max_size": 50}`
1908/// becomes: `TypeName.builder().withLanguage("abl").withChunkMaxSize(50L).build()`
1909///
1910/// For enums: emit `EnumType.VariantName` (detected via camelCase lookup in enum_fields)
1911/// For strings and bools: use the value directly
1912/// For plain numbers: emit the literal with type suffix (long uses L, double uses d)
1913/// For nested objects: recurse with Options suffix
1914/// When `nested_types_optional` is false, nested builders are passed directly without
1915/// Optional.of() wrapping, allowing non-optional nested config types.
1916fn java_builder_expression(
1917    obj: &serde_json::Map<String, serde_json::Value>,
1918    type_name: &str,
1919    enum_fields: &std::collections::HashSet<String>,
1920    nested_types: &std::collections::HashMap<String, String>,
1921    nested_types_optional: bool,
1922    path_fields: &[String],
1923) -> String {
1924    let mut expr = format!("{}.builder()", type_name);
1925    for (key, val) in obj {
1926        // Convert snake_case key to camelCase for method name
1927        let camel_key = key.to_lower_camel_case();
1928        let method_name = format!("with{}", camel_key.to_upper_camel_case());
1929
1930        let java_val = match val {
1931            serde_json::Value::String(s) => {
1932                // Check if this field is an enum type by checking enum_fields.
1933                // Infer enum type name from camelCase field name by converting to UpperCamelCase.
1934                if enum_fields.contains(&camel_key) {
1935                    // Enum field: infer type name from field name (e.g., "codeBlockStyle" -> "CodeBlockStyle")
1936                    let enum_type_name = camel_key.to_upper_camel_case();
1937                    let variant_name = s.to_upper_camel_case();
1938                    format!("{}.{}", enum_type_name, variant_name)
1939                } else if camel_key == "preset" && type_name == "PreprocessingOptions" {
1940                    // Special case: preset field in PreprocessingOptions maps to PreprocessingPreset
1941                    let variant_name = s.to_upper_camel_case();
1942                    format!("PreprocessingPreset.{}", variant_name)
1943                } else if path_fields.contains(key) {
1944                    // Path field: wrap in Optional.of(java.nio.file.Path.of(...))
1945                    format!("Optional.of(java.nio.file.Path.of(\"{}\"))", escape_java(s))
1946                } else {
1947                    // String field: emit as a quoted literal
1948                    format!("\"{}\"", escape_java(s))
1949                }
1950            }
1951            serde_json::Value::Bool(b) => b.to_string(),
1952            serde_json::Value::Null => "null".to_string(),
1953            serde_json::Value::Number(n) => {
1954                // Number field: emit literal with type suffix.
1955                // Java records/classes use either `long` (primitive, not nullable) or
1956                // `Optional<Long>` (nullable). The codegen wraps in `Optional.of(...)`
1957                // by default since most options builder fields are Optional, but several
1958                // record types (e.g. SecurityLimits) use primitive `long` throughout.
1959                // Skip the wrap for: (a) known-primitive top-level fields and (b) any
1960                // method on a record type whose builder methods take primitives only.
1961                let camel_key = key.to_lower_camel_case();
1962                let is_plain_field = matches!(camel_key.as_str(), "listIndentWidth" | "wrapWidth");
1963                // Builders for typed-record nested config classes use primitives
1964                // throughout — they're not the optional-options pattern.
1965                let is_primitive_builder = matches!(type_name, "SecurityLimits" | "SecurityLimitsBuilder");
1966
1967                if is_plain_field || is_primitive_builder {
1968                    // Plain numeric field: no Optional wrapper
1969                    if n.is_f64() {
1970                        format!("{}d", n)
1971                    } else {
1972                        format!("{}L", n)
1973                    }
1974                } else {
1975                    // Optional numeric field: wrap in Optional.of()
1976                    if n.is_f64() {
1977                        format!("Optional.of({}d)", n)
1978                    } else {
1979                        format!("Optional.of({}L)", n)
1980                    }
1981                }
1982            }
1983            serde_json::Value::Array(arr) => {
1984                let items: Vec<String> = arr.iter().map(|v| json_to_java_typed(v, None)).collect();
1985                format!("java.util.List.of({})", items.join(", "))
1986            }
1987            serde_json::Value::Object(nested) => {
1988                // Recurse with the type from nested_types mapping, or default to snake_case → PascalCase + "Options".
1989                let nested_type = nested_types
1990                    .get(key.as_str())
1991                    .cloned()
1992                    .unwrap_or_else(|| format!("{}Options", key.to_upper_camel_case()));
1993                let inner = java_builder_expression(
1994                    nested,
1995                    &nested_type,
1996                    enum_fields,
1997                    nested_types,
1998                    nested_types_optional,
1999                    &[],
2000                );
2001                // Top-level config builders (e.g. ExtractionConfigBuilder) declare nested
2002                // record fields as `Optional<T>` (since they are nullable). Primitive-fields
2003                // builders (SecurityLimitsBuilder etc.) take the bare type directly.
2004                let is_primitive_builder = matches!(type_name, "SecurityLimits" | "SecurityLimitsBuilder");
2005                if is_primitive_builder || !nested_types_optional {
2006                    inner
2007                } else {
2008                    format!("Optional.of({inner})")
2009                }
2010            }
2011        };
2012        expr.push_str(&format!(".{}({})", method_name, java_val));
2013    }
2014    expr.push_str(".build()");
2015    expr
2016}
2017
2018/// Build default nested type mappings for Java extraction config types.
2019///
2020/// Maps known Kreuzberg/Kreuzcrawl config field names (in snake_case) to their
2021/// Java record type names (in PascalCase). These defaults allow e2e codegen to
2022/// automatically deserialize nested config objects without requiring explicit
2023/// configuration in alef.toml. User-provided overrides take precedence.
2024fn default_java_nested_types() -> std::collections::HashMap<String, String> {
2025    [
2026        ("chunking", "ChunkingConfig"),
2027        ("ocr", "OcrConfig"),
2028        ("images", "ImageExtractionConfig"),
2029        ("html_output", "HtmlOutputConfig"),
2030        ("language_detection", "LanguageDetectionConfig"),
2031        ("postprocessor", "PostProcessorConfig"),
2032        ("acceleration", "AccelerationConfig"),
2033        ("email", "EmailConfig"),
2034        ("pages", "PageConfig"),
2035        ("pdf_options", "PdfConfig"),
2036        ("layout", "LayoutDetectionConfig"),
2037        ("tree_sitter", "TreeSitterConfig"),
2038        ("structured_extraction", "StructuredExtractionConfig"),
2039        ("content_filter", "ContentFilterConfig"),
2040        ("token_reduction", "TokenReductionOptions"),
2041        ("security_limits", "SecurityLimits"),
2042    ]
2043    .iter()
2044    .map(|(k, v)| (k.to_string(), v.to_string()))
2045    .collect()
2046}
2047
2048// ---------------------------------------------------------------------------
2049// Import collection helpers
2050// ---------------------------------------------------------------------------
2051
2052/// Recursively collect enum types and nested option types used in a builder expression.
2053/// Enums are keyed in the enum_fields map by camelCase names (e.g., "codeBlockStyle" → "CodeBlockStyle").
2054#[allow(dead_code)]
2055fn collect_enum_and_nested_types(
2056    obj: &serde_json::Map<String, serde_json::Value>,
2057    enum_fields: &std::collections::HashMap<String, String>,
2058    types_out: &mut std::collections::BTreeSet<String>,
2059) {
2060    for (key, val) in obj {
2061        // enum_fields is keyed by camelCase, not snake_case.
2062        let camel_key = key.to_lower_camel_case();
2063        if let Some(enum_type) = enum_fields.get(&camel_key) {
2064            // Add the enum type from the mapping (e.g., "CodeBlockStyle").
2065            types_out.insert(enum_type.clone());
2066        } else if camel_key == "preset" {
2067            // Special case: preset field uses PreprocessingPreset enum.
2068            types_out.insert("PreprocessingPreset".to_string());
2069        }
2070        // Recurse into nested objects to find their nested enum types.
2071        if let Some(nested) = val.as_object() {
2072            collect_enum_and_nested_types(nested, enum_fields, types_out);
2073        }
2074    }
2075}
2076
2077fn collect_nested_type_names(
2078    obj: &serde_json::Map<String, serde_json::Value>,
2079    nested_types: &std::collections::HashMap<String, String>,
2080    types_out: &mut std::collections::BTreeSet<String>,
2081) {
2082    for (key, val) in obj {
2083        if let Some(type_name) = nested_types.get(key.as_str()) {
2084            types_out.insert(type_name.clone());
2085        }
2086        if let Some(nested) = val.as_object() {
2087            collect_nested_type_names(nested, nested_types, types_out);
2088        }
2089    }
2090}
2091
2092// ---------------------------------------------------------------------------
2093// Visitor generation
2094// ---------------------------------------------------------------------------
2095
2096/// Build a Java visitor class and add setup lines. Returns the visitor variable name.
2097fn build_java_visitor(
2098    setup_lines: &mut Vec<String>,
2099    visitor_spec: &crate::fixture::VisitorSpec,
2100    class_name: &str,
2101) -> String {
2102    setup_lines.push("class _TestVisitor implements Visitor {".to_string());
2103    for (method_name, action) in &visitor_spec.callbacks {
2104        emit_java_visitor_method(setup_lines, method_name, action, class_name);
2105    }
2106    setup_lines.push("}".to_string());
2107    setup_lines.push("var visitor = new _TestVisitor();".to_string());
2108    "visitor".to_string()
2109}
2110
2111/// Emit a Java visitor method for a callback action.
2112fn emit_java_visitor_method(
2113    setup_lines: &mut Vec<String>,
2114    method_name: &str,
2115    action: &CallbackAction,
2116    _class_name: &str,
2117) {
2118    let camel_method = method_to_camel(method_name);
2119    let params = match method_name {
2120        "visit_link" => "NodeContext ctx, String href, String text, String title",
2121        "visit_image" => "NodeContext ctx, String src, String alt, String title",
2122        "visit_heading" => "NodeContext ctx, int level, String text, String id",
2123        "visit_code_block" => "NodeContext ctx, String lang, String code",
2124        "visit_code_inline"
2125        | "visit_strong"
2126        | "visit_emphasis"
2127        | "visit_strikethrough"
2128        | "visit_underline"
2129        | "visit_subscript"
2130        | "visit_superscript"
2131        | "visit_mark"
2132        | "visit_button"
2133        | "visit_summary"
2134        | "visit_figcaption"
2135        | "visit_definition_term"
2136        | "visit_definition_description" => "NodeContext ctx, String text",
2137        "visit_text" => "NodeContext ctx, String text",
2138        "visit_list_item" => "NodeContext ctx, boolean ordered, String marker, String text",
2139        "visit_blockquote" => "NodeContext ctx, String content, long depth",
2140        "visit_table_row" => "NodeContext ctx, java.util.List<String> cells, boolean isHeader",
2141        "visit_custom_element" => "NodeContext ctx, String tagName, String html",
2142        "visit_form" => "NodeContext ctx, String actionUrl, String method",
2143        "visit_input" => "NodeContext ctx, String inputType, String name, String value",
2144        "visit_audio" | "visit_video" | "visit_iframe" => "NodeContext ctx, String src",
2145        "visit_details" => "NodeContext ctx, boolean isOpen",
2146        "visit_element_end" | "visit_table_end" | "visit_definition_list_end" | "visit_figure_end" => {
2147            "NodeContext ctx, String output"
2148        }
2149        "visit_list_start" => "NodeContext ctx, boolean ordered",
2150        "visit_list_end" => "NodeContext ctx, boolean ordered, String output",
2151        _ => "NodeContext ctx",
2152    };
2153
2154    // Determine action type and values for template
2155    let (action_type, action_value, format_args) = match action {
2156        CallbackAction::Skip => ("skip", String::new(), Vec::new()),
2157        CallbackAction::Continue => ("continue", String::new(), Vec::new()),
2158        CallbackAction::PreserveHtml => ("preserve_html", String::new(), Vec::new()),
2159        CallbackAction::Custom { output } => ("custom_literal", escape_java(output), Vec::new()),
2160        CallbackAction::CustomTemplate { template } => {
2161            // Extract {placeholder} names from the template (in order of appearance).
2162            let mut format_str = String::with_capacity(template.len());
2163            let mut format_args: Vec<String> = Vec::new();
2164            let mut chars = template.chars().peekable();
2165            while let Some(ch) = chars.next() {
2166                if ch == '{' {
2167                    // Collect identifier chars until '}'.
2168                    let mut name = String::new();
2169                    let mut closed = false;
2170                    for inner in chars.by_ref() {
2171                        if inner == '}' {
2172                            closed = true;
2173                            break;
2174                        }
2175                        name.push(inner);
2176                    }
2177                    if closed && !name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_') {
2178                        let camel_name = name.as_str().to_lower_camel_case();
2179                        format_args.push(camel_name);
2180                        format_str.push_str("%s");
2181                    } else {
2182                        // Not a simple placeholder — emit literally.
2183                        format_str.push('{');
2184                        format_str.push_str(&name);
2185                        if closed {
2186                            format_str.push('}');
2187                        }
2188                    }
2189                } else {
2190                    format_str.push(ch);
2191                }
2192            }
2193            let escaped = escape_java(&format_str);
2194            if format_args.is_empty() {
2195                ("custom_literal", escaped, Vec::new())
2196            } else {
2197                ("custom_formatted", escaped, format_args)
2198            }
2199        }
2200    };
2201
2202    let params = params.to_string();
2203
2204    let rendered = crate::template_env::render(
2205        "java/visitor_method.jinja",
2206        minijinja::context! {
2207            camel_method,
2208            params,
2209            action_type,
2210            action_value,
2211            format_args => format_args,
2212        },
2213    );
2214    setup_lines.push(rendered);
2215}
2216
2217/// Convert snake_case method names to Java camelCase.
2218fn method_to_camel(snake: &str) -> String {
2219    snake.to_lower_camel_case()
2220}
2221
2222#[cfg(test)]
2223mod tests {
2224    use crate::config::{CallConfig, E2eConfig, SelectWhen};
2225    use crate::fixture::Fixture;
2226    use std::collections::HashMap;
2227
2228    fn make_fixture_with_input(id: &str, input: serde_json::Value) -> Fixture {
2229        Fixture {
2230            id: id.to_string(),
2231            category: None,
2232            description: "test fixture".to_string(),
2233            tags: vec![],
2234            skip: None,
2235            env: None,
2236            call: None,
2237            input,
2238            mock_response: None,
2239            source: String::new(),
2240            http: None,
2241            assertions: vec![],
2242            visitor: None,
2243        }
2244    }
2245
2246    /// Test that resolve_call_for_fixture correctly routes to batchScrape
2247    /// when input has batch_urls and select_when condition matches.
2248    #[test]
2249    fn test_java_select_when_routes_to_batch_scrape() {
2250        let mut calls = HashMap::new();
2251        calls.insert(
2252            "batch_scrape".to_string(),
2253            CallConfig {
2254                function: "batchScrape".to_string(),
2255                module: "com.example.kreuzcrawl".to_string(),
2256                select_when: Some(SelectWhen::InputHas("batch_urls".to_string())),
2257                ..CallConfig::default()
2258            },
2259        );
2260
2261        let e2e_config = E2eConfig {
2262            call: CallConfig {
2263                function: "scrape".to_string(),
2264                module: "com.example.kreuzcrawl".to_string(),
2265                ..CallConfig::default()
2266            },
2267            calls,
2268            ..E2eConfig::default()
2269        };
2270
2271        // Fixture with batch_urls but no explicit call field should route to batch_scrape
2272        let fixture = make_fixture_with_input("batch_empty_urls", serde_json::json!({ "batch_urls": [] }));
2273
2274        let resolved_call = e2e_config.resolve_call_for_fixture(fixture.call.as_deref(), &fixture.input);
2275        assert_eq!(resolved_call.function, "batchScrape");
2276
2277        // Fixture without batch_urls should fall back to default scrape
2278        let fixture_no_batch =
2279            make_fixture_with_input("simple_scrape", serde_json::json!({ "url": "https://example.com" }));
2280        let resolved_default =
2281            e2e_config.resolve_call_for_fixture(fixture_no_batch.call.as_deref(), &fixture_no_batch.input);
2282        assert_eq!(resolved_default.function, "scrape");
2283    }
2284}