1use 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
21fn is_numeric_type_hint(ty: &str) -> bool {
23 matches!(ty, "f32" | "f64" | "float" | "double" | "Float" | "Double")
24}
25
26fn is_java_builtin_type(ty: &str) -> bool {
28 matches!(
29 ty,
30 "String" | "Boolean" | "Integer" | "Long" | "Double" | "Float" | "Byte" | "Short" | "Character" | "Void"
31 )
32}
33
34pub struct JavaCodegen;
36
37impl E2eCodegen for JavaCodegen {
38 fn generate(
39 &self,
40 groups: &[FixtureGroup],
41 e2e_config: &E2eConfig,
42 config: &ResolvedCrateConfig,
43 type_defs: &[alef_core::ir::TypeDef],
44 enums: &[alef_core::ir::EnumDef],
45 ) -> Result<Vec<GeneratedFile>> {
46 let lang = self.language_name();
47 let output_base = PathBuf::from(e2e_config.effective_output()).join(lang);
48
49 let mut files = Vec::new();
50
51 let call = &e2e_config.call;
53 let overrides = call.overrides.get(lang);
54 let _module_path = overrides
55 .and_then(|o| o.module.as_ref())
56 .cloned()
57 .unwrap_or_else(|| call.module.clone());
58 let function_name = overrides
59 .and_then(|o| o.function.as_ref())
60 .cloned()
61 .unwrap_or_else(|| call.function.clone());
62 let class_name = overrides
63 .and_then(|o| o.class.as_ref())
64 .cloned()
65 .unwrap_or_else(|| config.name.to_upper_camel_case());
66 let result_is_simple = overrides.is_some_and(|o| o.result_is_simple);
67 let result_var = &call.result_var;
68
69 let java_pkg = e2e_config.resolve_package("java");
71 let pkg_name = java_pkg
72 .as_ref()
73 .and_then(|p| p.name.as_ref())
74 .cloned()
75 .unwrap_or_else(|| config.name.clone());
76
77 let java_group_id = config.java_group_id();
79 let binding_pkg = config.java_package();
80 let pkg_version = config.resolved_version().unwrap_or_else(|| "0.1.0".to_string());
81
82 files.push(GeneratedFile {
84 path: output_base.join("pom.xml"),
85 content: render_pom_xml(
86 &pkg_name,
87 &java_group_id,
88 &pkg_version,
89 e2e_config.dep_mode,
90 &e2e_config.test_documents_relative_from(0),
91 ),
92 generated_header: false,
93 });
94
95 let needs_mock_server = groups
103 .iter()
104 .flat_map(|g| g.fixtures.iter())
105 .any(|f| f.needs_mock_server());
106
107 let mut test_base = output_base.join("src").join("test").join("java");
111 for segment in java_group_id.split('.') {
112 test_base = test_base.join(segment);
113 }
114 let test_base = test_base.join("e2e");
115
116 if needs_mock_server {
117 files.push(GeneratedFile {
118 path: test_base.join("MockServerListener.java"),
119 content: render_mock_server_listener(&java_group_id),
120 generated_header: true,
121 });
122 files.push(GeneratedFile {
123 path: output_base
124 .join("src")
125 .join("test")
126 .join("resources")
127 .join("META-INF")
128 .join("services")
129 .join("org.junit.platform.launcher.LauncherSessionListener"),
130 content: format!("{java_group_id}.e2e.MockServerListener\n"),
131 generated_header: false,
132 });
133 }
134
135 let sealed_display_types: std::collections::BTreeSet<String> = std::iter::once(&e2e_config.call)
140 .chain(e2e_config.calls.values())
141 .filter_map(|c| c.overrides.get(lang))
142 .flat_map(|o| o.assert_enum_fields.values().cloned())
143 .collect();
144
145 for type_name in &sealed_display_types {
146 if let Some(enum_def) = enums.iter().find(|e| &e.name == type_name) {
147 files.push(GeneratedFile {
148 path: test_base.join(format!("{type_name}Display.java")),
149 content: render_sealed_display(type_name, enum_def, type_defs, &java_group_id),
150 generated_header: true,
151 });
152 }
153 }
154
155 let options_type = overrides.and_then(|o| o.options_type.clone());
157
158 static EMPTY_ENUM_FIELDS: std::sync::LazyLock<std::collections::HashMap<String, String>> =
160 std::sync::LazyLock::new(std::collections::HashMap::new);
161 let _enum_fields = overrides.map(|o| &o.enum_fields).unwrap_or(&EMPTY_ENUM_FIELDS);
162
163 let mut effective_nested_types: std::collections::HashMap<String, String> = std::collections::HashMap::new();
165 if let Some(overrides_map) = overrides.map(|o| &o.nested_types) {
166 effective_nested_types.extend(overrides_map.clone());
167 }
168
169 let nested_types_optional = overrides.map(|o| o.nested_types_optional).unwrap_or(true);
171
172 for group in groups {
173 let active: Vec<&Fixture> = group
174 .fixtures
175 .iter()
176 .filter(|f| super::should_include_fixture(f, lang, e2e_config))
177 .collect();
178
179 if active.is_empty() {
180 continue;
181 }
182
183 let class_file_name = format!("{}Test.java", sanitize_filename(&group.category).to_upper_camel_case());
184 let content = render_test_file(
185 &group.category,
186 &active,
187 &class_name,
188 &function_name,
189 &java_group_id,
190 &binding_pkg,
191 result_var,
192 &e2e_config.call.args,
193 options_type.as_deref(),
194 result_is_simple,
195 e2e_config,
196 &effective_nested_types,
197 nested_types_optional,
198 &config.adapters,
199 );
200 files.push(GeneratedFile {
201 path: test_base.join(class_file_name),
202 content,
203 generated_header: true,
204 });
205 }
206
207 Ok(files)
208 }
209
210 fn language_name(&self) -> &'static str {
211 "java"
212 }
213}
214
215fn render_pom_xml(
220 pkg_name: &str,
221 java_group_id: &str,
222 pkg_version: &str,
223 dep_mode: crate::config::DependencyMode,
224 test_documents_path: &str,
225) -> String {
226 let (dep_group_id, dep_artifact_id) = if let Some((g, a)) = pkg_name.split_once(':') {
228 (g, a)
229 } else {
230 (java_group_id, pkg_name)
231 };
232 let artifact_id = format!("{dep_artifact_id}-e2e-java");
233 let dep_block = match dep_mode {
234 crate::config::DependencyMode::Registry => {
235 format!(
236 r#" <dependency>
237 <groupId>{dep_group_id}</groupId>
238 <artifactId>{dep_artifact_id}</artifactId>
239 <version>{pkg_version}</version>
240 </dependency>"#
241 )
242 }
243 crate::config::DependencyMode::Local => {
244 format!(
245 r#" <dependency>
246 <groupId>{dep_group_id}</groupId>
247 <artifactId>{dep_artifact_id}</artifactId>
248 <version>{pkg_version}</version>
249 <scope>system</scope>
250 <systemPath>${{project.basedir}}/../../packages/java/target/{dep_artifact_id}-{pkg_version}.jar</systemPath>
251 </dependency>"#
252 )
253 }
254 };
255 crate::template_env::render(
256 "java/pom.xml.jinja",
257 minijinja::context! {
258 artifact_id => artifact_id,
259 java_group_id => java_group_id,
260 dep_block => dep_block,
261 junit_version => tv::maven::JUNIT,
262 jackson_version => tv::maven::JACKSON_E2E,
263 build_helper_version => tv::maven::BUILD_HELPER_MAVEN_PLUGIN,
264 maven_surefire_version => tv::maven::MAVEN_SUREFIRE_PLUGIN_E2E,
265 test_documents_path => test_documents_path,
266 },
267 )
268}
269
270fn render_mock_server_listener(java_group_id: &str) -> String {
279 let header = hash::header(CommentStyle::DoubleSlash);
280 let mut out = header;
281 out.push_str(&format!("package {java_group_id}.e2e;\n\n"));
282 out.push_str("import java.io.BufferedReader;\n");
283 out.push_str("import java.io.File;\n");
284 out.push_str("import java.io.IOException;\n");
285 out.push_str("import java.io.InputStreamReader;\n");
286 out.push_str("import java.nio.charset.StandardCharsets;\n");
287 out.push_str("import java.nio.file.Path;\n");
288 out.push_str("import java.nio.file.Paths;\n");
289 out.push_str("import java.util.regex.Matcher;\n");
290 out.push_str("import java.util.regex.Pattern;\n");
291 out.push_str("import org.junit.platform.launcher.LauncherSession;\n");
292 out.push_str("import org.junit.platform.launcher.LauncherSessionListener;\n");
293 out.push('\n');
294 out.push_str("/**\n");
295 out.push_str(" * Spawns the mock-server binary once per JUnit launcher session and\n");
296 out.push_str(" * exposes its URL as the `mockServerUrl` system property. Generated\n");
297 out.push_str(" * test bodies read the property (with `MOCK_SERVER_URL` env-var\n");
298 out.push_str(" * fallback) so tests can run via plain `mvn test` without any external\n");
299 out.push_str(" * mock-server orchestration. Mirrors the Ruby spec_helper / Python\n");
300 out.push_str(" * conftest spawn pattern. Honors a pre-set MOCK_SERVER_URL by\n");
301 out.push_str(" * skipping the spawn entirely.\n");
302 out.push_str(" */\n");
303 out.push_str("public class MockServerListener implements LauncherSessionListener {\n");
304 out.push_str(" private Process mockServer;\n");
305 out.push('\n');
306 out.push_str(" @Override\n");
307 out.push_str(" public void launcherSessionOpened(LauncherSession session) {\n");
308 out.push_str(" String preset = System.getenv(\"MOCK_SERVER_URL\");\n");
309 out.push_str(" if (preset != null && !preset.isEmpty()) {\n");
310 out.push_str(" System.setProperty(\"mockServerUrl\", preset);\n");
311 out.push_str(" return;\n");
312 out.push_str(" }\n");
313 out.push_str(" Path repoRoot = locateRepoRoot();\n");
314 out.push_str(" if (repoRoot == null) {\n");
315 out.push_str(" throw new IllegalStateException(\"MockServerListener: could not locate repo root (looked for fixtures/ in ancestors of \" + System.getProperty(\"user.dir\") + \")\");\n");
316 out.push_str(" }\n");
317 out.push_str(" String binName = System.getProperty(\"os.name\", \"\").toLowerCase().contains(\"win\") ? \"mock-server.exe\" : \"mock-server\";\n");
318 out.push_str(" File bin = repoRoot.resolve(\"e2e\").resolve(\"rust\").resolve(\"target\").resolve(\"release\").resolve(binName).toFile();\n");
319 out.push_str(" File fixturesDir = repoRoot.resolve(\"fixtures\").toFile();\n");
320 out.push_str(" if (!bin.exists()) {\n");
321 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");
322 out.push_str(" }\n");
323 out.push_str(
324 " ProcessBuilder pb = new ProcessBuilder(bin.getAbsolutePath(), fixturesDir.getAbsolutePath())\n",
325 );
326 out.push_str(" .redirectErrorStream(false);\n");
327 out.push_str(" try {\n");
328 out.push_str(" mockServer = pb.start();\n");
329 out.push_str(" } catch (IOException e) {\n");
330 out.push_str(
331 " throw new IllegalStateException(\"MockServerListener: failed to start mock-server\", e);\n",
332 );
333 out.push_str(" }\n");
334 out.push_str(" // Read until we see MOCK_SERVER_URL= and optionally MOCK_SERVERS=.\n");
335 out.push_str(" // Cap the loop so a misbehaving mock-server cannot block indefinitely.\n");
336 out.push_str(" BufferedReader stdout = new BufferedReader(new InputStreamReader(mockServer.getInputStream(), StandardCharsets.UTF_8));\n");
337 out.push_str(" String url = null;\n");
338 out.push_str(" try {\n");
339 out.push_str(" for (int i = 0; i < 16; i++) {\n");
340 out.push_str(" String line = stdout.readLine();\n");
341 out.push_str(" if (line == null) break;\n");
342 out.push_str(" if (line.startsWith(\"MOCK_SERVER_URL=\")) {\n");
343 out.push_str(" url = line.substring(\"MOCK_SERVER_URL=\".length()).trim();\n");
344 out.push_str(" } else if (line.startsWith(\"MOCK_SERVERS=\")) {\n");
345 out.push_str(" String jsonVal = line.substring(\"MOCK_SERVERS=\".length()).trim();\n");
346 out.push_str(" System.setProperty(\"mockServers\", jsonVal);\n");
347 out.push_str(" // Parse JSON map of fixture_id -> url and expose as system properties.\n");
348 out.push_str(" Pattern p = Pattern.compile(\"\\\"([^\\\"]+)\\\":\\\"([^\\\"]+)\\\"\");\n");
349 out.push_str(" Matcher matcher = p.matcher(jsonVal);\n");
350 out.push_str(" while (matcher.find()) {\n");
351 out.push_str(" String fid = matcher.group(1);\n");
352 out.push_str(" String furl = matcher.group(2);\n");
353 out.push_str(" System.setProperty(\"mockServer.\" + fid, furl);\n");
354 out.push_str(" }\n");
355 out.push_str(" break;\n");
356 out.push_str(" } else if (url != null) {\n");
357 out.push_str(" break;\n");
358 out.push_str(" }\n");
359 out.push_str(" }\n");
360 out.push_str(" } catch (IOException e) {\n");
361 out.push_str(" mockServer.destroyForcibly();\n");
362 out.push_str(
363 " throw new IllegalStateException(\"MockServerListener: failed to read mock-server stdout\", e);\n",
364 );
365 out.push_str(" }\n");
366 out.push_str(" if (url == null || url.isEmpty()) {\n");
367 out.push_str(" mockServer.destroyForcibly();\n");
368 out.push_str(" throw new IllegalStateException(\"MockServerListener: mock-server did not emit MOCK_SERVER_URL\");\n");
369 out.push_str(" }\n");
370 out.push_str(" // TCP-readiness probe: ensure axum::serve is accepting before tests start.\n");
371 out.push_str(" // The mock-server binds the TcpListener synchronously then prints the URL\n");
372 out.push_str(" // before tokio::spawn(axum::serve(...)) is polled, so under Surefire\n");
373 out.push_str(" // parallel mode tests can race startup. Poll-connect (max 5s, 50ms backoff)\n");
374 out.push_str(" // until success.\n");
375 out.push_str(" java.net.URI healthUri = java.net.URI.create(url);\n");
376 out.push_str(" String host = healthUri.getHost();\n");
377 out.push_str(" int port = healthUri.getPort();\n");
378 out.push_str(" long deadline = System.nanoTime() + 5_000_000_000L;\n");
379 out.push_str(" while (System.nanoTime() < deadline) {\n");
380 out.push_str(" try (java.net.Socket s = new java.net.Socket()) {\n");
381 out.push_str(" s.connect(new java.net.InetSocketAddress(host, port), 100);\n");
382 out.push_str(" break;\n");
383 out.push_str(" } catch (java.io.IOException ignored) {\n");
384 out.push_str(" try { Thread.sleep(50); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); break; }\n");
385 out.push_str(" }\n");
386 out.push_str(" }\n");
387 out.push_str(" System.setProperty(\"mockServerUrl\", url);\n");
388 out.push_str(" // Drain remaining stdout/stderr in daemon threads so a full pipe\n");
389 out.push_str(" // does not block the child.\n");
390 out.push_str(" Process server = mockServer;\n");
391 out.push_str(" Thread drainOut = new Thread(() -> drain(stdout));\n");
392 out.push_str(" drainOut.setDaemon(true);\n");
393 out.push_str(" drainOut.start();\n");
394 out.push_str(" Thread drainErr = new Thread(() -> drain(new BufferedReader(new InputStreamReader(server.getErrorStream(), StandardCharsets.UTF_8))));\n");
395 out.push_str(" drainErr.setDaemon(true);\n");
396 out.push_str(" drainErr.start();\n");
397 out.push_str(" }\n");
398 out.push('\n');
399 out.push_str(" @Override\n");
400 out.push_str(" public void launcherSessionClosed(LauncherSession session) {\n");
401 out.push_str(" if (mockServer == null) return;\n");
402 out.push_str(" try { mockServer.getOutputStream().close(); } catch (IOException ignored) {}\n");
403 out.push_str(" try {\n");
404 out.push_str(" if (!mockServer.waitFor(2, java.util.concurrent.TimeUnit.SECONDS)) {\n");
405 out.push_str(" mockServer.destroyForcibly();\n");
406 out.push_str(" }\n");
407 out.push_str(" } catch (InterruptedException ignored) {\n");
408 out.push_str(" Thread.currentThread().interrupt();\n");
409 out.push_str(" mockServer.destroyForcibly();\n");
410 out.push_str(" }\n");
411 out.push_str(" }\n");
412 out.push('\n');
413 out.push_str(" private static Path locateRepoRoot() {\n");
414 out.push_str(" Path dir = Paths.get(\"\").toAbsolutePath();\n");
415 out.push_str(" while (dir != null) {\n");
416 out.push_str(" if (dir.resolve(\"fixtures\").toFile().isDirectory()\n");
417 out.push_str(" && dir.resolve(\"e2e\").toFile().isDirectory()) {\n");
418 out.push_str(" return dir;\n");
419 out.push_str(" }\n");
420 out.push_str(" dir = dir.getParent();\n");
421 out.push_str(" }\n");
422 out.push_str(" return null;\n");
423 out.push_str(" }\n");
424 out.push('\n');
425 out.push_str(" private static void drain(BufferedReader reader) {\n");
426 out.push_str(" try {\n");
427 out.push_str(" char[] buf = new char[1024];\n");
428 out.push_str(" while (reader.read(buf) >= 0) { /* drain */ }\n");
429 out.push_str(" } catch (IOException ignored) {}\n");
430 out.push_str(" }\n");
431 out.push_str("}\n");
432 out
433}
434
435fn render_sealed_display(
448 type_name: &str,
449 enum_def: &alef_core::ir::EnumDef,
450 type_defs: &[alef_core::ir::TypeDef],
451 java_group_id: &str,
452) -> String {
453 let helper_class = format!("{type_name}Display");
454 let header = hash::header(CommentStyle::DoubleSlash);
455 let mut out = header;
456 out.push_str(&format!("package {java_group_id}.e2e;\n\n"));
457 out.push_str(&format!("import {java_group_id}.{type_name};\n"));
458 out.push('\n');
459 out.push_str(&format!(
460 "/**\n * Helper class for extracting display strings from {type_name} sealed interface.\n */\n"
461 ));
462 out.push_str(&format!("class {helper_class} {{\n"));
463 out.push_str(&format!(" static String toDisplayString({type_name} value) {{\n"));
464 out.push_str(" if (value == null) return \"\";\n");
465 out.push_str(" return switch (value) {\n");
466
467 for variant in &enum_def.variants {
468 let variant_name = &variant.name;
469 let has_format_field = variant.is_tuple && variant.fields.len() == 1 && {
474 let field_type_name = match &variant.fields[0].ty {
475 alef_core::ir::TypeRef::Named(n) => Some(n.as_str()),
476 _ => None,
477 };
478 field_type_name.is_some_and(|tn| {
479 type_defs
480 .iter()
481 .find(|td| td.name == tn)
482 .is_some_and(|td| td.fields.iter().any(|f| f.name == "format"))
483 })
484 };
485
486 let display = if has_format_field {
487 "i.value().format()".to_string()
488 } else {
489 let serde_name = variant
491 .serde_rename
492 .as_deref()
493 .unwrap_or(variant_name.as_str())
494 .to_lowercase();
495 format!("\"{serde_name}\"")
496 };
497
498 let binding = if has_format_field {
499 format!("{type_name}.{variant_name} i")
500 } else {
501 format!("{type_name}.{variant_name} _")
502 };
503
504 out.push_str(&format!(" case {binding} -> {display};\n"));
505 }
506
507 out.push_str(" default -> \"unknown\";\n");
508 out.push_str(" };\n");
509 out.push_str(" }\n");
510 out.push_str("}\n");
511 out
512}
513
514#[allow(clippy::too_many_arguments)]
515fn render_test_file(
516 category: &str,
517 fixtures: &[&Fixture],
518 class_name: &str,
519 function_name: &str,
520 java_group_id: &str,
521 binding_pkg: &str,
522 result_var: &str,
523 args: &[crate::config::ArgMapping],
524 options_type: Option<&str>,
525 result_is_simple: bool,
526 e2e_config: &E2eConfig,
527 nested_types: &std::collections::HashMap<String, String>,
528 nested_types_optional: bool,
529 adapters: &[alef_core::config::extras::AdapterConfig],
530) -> String {
531 let header = hash::header(CommentStyle::DoubleSlash);
532 let test_class_name = format!("{}Test", sanitize_filename(category).to_upper_camel_case());
533
534 let (import_path, simple_class) = if class_name.contains('.') {
537 let simple = class_name.rsplit('.').next().unwrap_or(class_name);
538 (class_name, simple)
539 } else {
540 ("", class_name)
541 };
542
543 let lang_for_om = "java";
545 let needs_object_mapper_for_handle = fixtures.iter().any(|f| {
546 args.iter().filter(|a| a.arg_type == "handle").any(|a| {
547 let v = f.input.get(&a.field).unwrap_or(&serde_json::Value::Null);
548 !(v.is_null() || v.is_object() && v.as_object().is_some_and(|o| o.is_empty()))
549 })
550 });
551 let has_http_fixtures = fixtures.iter().any(|f| f.http.is_some());
553 let needs_object_mapper = needs_object_mapper_for_handle || has_http_fixtures;
554
555 let mut all_options_types: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
557 if let Some(t) = options_type {
558 all_options_types.insert(t.to_string());
559 }
560 for f in fixtures.iter() {
561 let call_cfg =
562 e2e_config.resolve_call_for_fixture(f.call.as_deref(), &f.id, &f.resolved_category(), &f.tags, &f.input);
563 if let Some(ov) = call_cfg.overrides.get(lang_for_om) {
564 if let Some(t) = &ov.options_type {
565 all_options_types.insert(t.clone());
566 }
567 }
568 let java_has_type = call_cfg
574 .overrides
575 .get(lang_for_om)
576 .and_then(|o| o.options_type.as_deref())
577 .is_some();
578 if !java_has_type {
579 for cand in ["csharp", "c", "go", "php", "python"] {
580 if let Some(o) = call_cfg.overrides.get(cand) {
581 if let Some(t) = &o.options_type {
582 all_options_types.insert(t.clone());
583 break;
584 }
585 }
586 }
587 }
588 for arg in &call_cfg.args {
591 if let Some(elem_type) = &arg.element_type {
592 if elem_type == "BatchBytesItem" || elem_type == "BatchFileItem" {
593 all_options_types.insert(elem_type.clone());
594 } else if arg.arg_type == "json_object"
595 && !is_numeric_type_hint(elem_type)
596 && !is_java_builtin_type(elem_type)
597 {
598 all_options_types.insert(elem_type.clone());
601 }
602 }
603 }
604 }
605
606 let mut nested_types_used: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
609 for f in fixtures.iter() {
610 let call_cfg =
611 e2e_config.resolve_call_for_fixture(f.call.as_deref(), &f.id, &f.resolved_category(), &f.tags, &f.input);
612 for arg in &call_cfg.args {
613 if arg.arg_type == "json_object" {
614 let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
615 if let Some(val) = f.input.get(field) {
616 if !val.is_null() && !val.is_array() {
617 if let Some(obj) = val.as_object() {
618 collect_nested_type_names(obj, nested_types, &mut nested_types_used);
619 }
620 }
621 }
622 }
623 }
624 }
625
626 let binding_pkg_for_imports: String = if !binding_pkg.is_empty() {
631 binding_pkg.to_string()
632 } else if !import_path.is_empty() {
633 import_path
634 .rsplit_once('.')
635 .map(|(p, _)| p.to_string())
636 .unwrap_or_default()
637 } else {
638 String::new()
639 };
640
641 let mut imports: Vec<String> = Vec::new();
643 imports.push("import org.junit.jupiter.api.Test;".to_string());
644 imports.push("import static org.junit.jupiter.api.Assertions.*;".to_string());
645
646 if !import_path.is_empty() {
649 imports.push(format!("import {import_path};"));
650 } else if !binding_pkg_for_imports.is_empty() && !class_name.is_empty() {
651 imports.push(format!("import {binding_pkg_for_imports}.{class_name};"));
652 }
653
654 if needs_object_mapper {
655 imports.push("import com.fasterxml.jackson.databind.ObjectMapper;".to_string());
656 imports.push("import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;".to_string());
657 }
658
659 if !all_options_types.is_empty() {
661 for opts_type in &all_options_types {
662 let qualified = if binding_pkg_for_imports.is_empty() {
663 opts_type.clone()
664 } else {
665 format!("{binding_pkg_for_imports}.{opts_type}")
666 };
667 imports.push(format!("import {qualified};"));
668 }
669 }
670
671 if !nested_types_used.is_empty() && !binding_pkg_for_imports.is_empty() {
673 for type_name in &nested_types_used {
674 imports.push(format!("import {binding_pkg_for_imports}.{type_name};"));
675 }
676 }
677
678 if needs_object_mapper_for_handle && !binding_pkg_for_imports.is_empty() {
680 imports.push(format!("import {binding_pkg_for_imports}.CrawlConfig;"));
681 }
682
683 let has_visitor_fixtures = fixtures.iter().any(|f| f.visitor.is_some());
685 if has_visitor_fixtures && !binding_pkg_for_imports.is_empty() {
686 imports.push(format!("import {binding_pkg_for_imports}.Visitor;"));
687 imports.push(format!("import {binding_pkg_for_imports}.NodeContext;"));
688 imports.push(format!("import {binding_pkg_for_imports}.VisitResult;"));
689 }
690
691 if !all_options_types.is_empty() {
695 imports.push("import java.util.Optional;".to_string());
696 if !binding_pkg_for_imports.is_empty() {
697 imports.push(format!("import {binding_pkg_for_imports}.JsonUtil;"));
698 }
699 }
700
701 let has_streaming_fixture = fixtures.iter().any(|f| {
712 let call_cfg =
713 e2e_config.resolve_call_for_fixture(f.call.as_deref(), &f.id, &f.resolved_category(), &f.tags, &f.input);
714 crate::codegen::streaming_assertions::resolve_is_streaming(f, call_cfg.streaming)
715 });
716 if has_streaming_fixture && !binding_pkg_for_imports.is_empty() {
717 let mut streaming_imports: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
722 for adapter in adapters {
723 if !matches!(adapter.pattern, alef_core::config::extras::AdapterPattern::Streaming) {
724 continue;
725 }
726 if let Some(item) = adapter.item_type.as_deref() {
727 let simple = item.rsplit("::").next().unwrap_or(item);
728 if !simple.is_empty() {
729 streaming_imports.insert(simple.to_string());
730 }
731 }
732 if let Some(req) = adapter.request_type.as_deref() {
733 let simple = req.rsplit("::").next().unwrap_or(req);
734 if !simple.is_empty() {
735 streaming_imports.insert(simple.to_string());
736 }
737 }
738 }
739 for ty in streaming_imports {
740 imports.push(format!("import {binding_pkg_for_imports}.{ty};"));
741 }
742 }
743
744 let mut fixtures_body = String::new();
746 for (i, fixture) in fixtures.iter().enumerate() {
747 render_test_method(
748 &mut fixtures_body,
749 fixture,
750 simple_class,
751 function_name,
752 result_var,
753 args,
754 options_type,
755 result_is_simple,
756 e2e_config,
757 nested_types,
758 nested_types_optional,
759 adapters,
760 );
761 if i + 1 < fixtures.len() {
762 fixtures_body.push('\n');
763 }
764 }
765
766 crate::template_env::render(
768 "java/test_file.jinja",
769 minijinja::context! {
770 header => header,
771 java_group_id => java_group_id,
772 test_class_name => test_class_name,
773 category => category,
774 imports => imports,
775 needs_object_mapper => needs_object_mapper,
776 fixtures_body => fixtures_body,
777 },
778 )
779}
780
781struct JavaTestClientRenderer;
789
790impl client::TestClientRenderer for JavaTestClientRenderer {
791 fn language_name(&self) -> &'static str {
792 "java"
793 }
794
795 fn sanitize_test_name(&self, id: &str) -> String {
799 id.to_upper_camel_case()
800 }
801
802 fn render_test_open(&self, out: &mut String, fn_name: &str, description: &str, skip_reason: Option<&str>) {
808 let escaped_reason = skip_reason.map(escape_java);
809 let rendered = crate::template_env::render(
810 "java/http_test_open.jinja",
811 minijinja::context! {
812 fn_name => fn_name,
813 description => description,
814 skip_reason => escaped_reason,
815 },
816 );
817 out.push_str(&rendered);
818 }
819
820 fn render_test_close(&self, out: &mut String) {
822 let rendered = crate::template_env::render("java/http_test_close.jinja", minijinja::context! {});
823 out.push_str(&rendered);
824 }
825
826 fn render_call(&self, out: &mut String, ctx: &client::CallCtx<'_>) {
832 const JAVA_RESTRICTED_HEADERS: &[&str] = &["connection", "content-length", "expect", "host", "upgrade"];
834
835 let method = ctx.method.to_uppercase();
836
837 let path = if ctx.query_params.is_empty() {
839 ctx.path.to_string()
840 } else {
841 let pairs: Vec<String> = ctx
842 .query_params
843 .iter()
844 .map(|(k, v)| {
845 let val_str = match v {
846 serde_json::Value::String(s) => s.clone(),
847 other => other.to_string(),
848 };
849 format!("{}={}", k, escape_java(&val_str))
850 })
851 .collect();
852 format!("{}?{}", ctx.path, pairs.join("&"))
853 };
854
855 let body_publisher = if let Some(body) = ctx.body {
856 let json = serde_json::to_string(body).unwrap_or_default();
857 let escaped = escape_java(&json);
858 format!("java.net.http.HttpRequest.BodyPublishers.ofString(\"{escaped}\")")
859 } else {
860 "java.net.http.HttpRequest.BodyPublishers.noBody()".to_string()
861 };
862
863 let content_type = if ctx.body.is_some() {
865 let ct = ctx.content_type.unwrap_or("application/json");
866 if !ctx.headers.keys().any(|k| k.to_lowercase() == "content-type") {
868 Some(ct.to_string())
869 } else {
870 None
871 }
872 } else {
873 None
874 };
875
876 let mut headers_lines: Vec<String> = Vec::new();
878 for (name, value) in ctx.headers {
879 if JAVA_RESTRICTED_HEADERS.contains(&name.to_lowercase().as_str()) {
880 continue;
881 }
882 let escaped_name = escape_java(name);
883 let escaped_value = escape_java(value);
884 headers_lines.push(format!(
885 "builder = builder.header(\"{escaped_name}\", \"{escaped_value}\");"
886 ));
887 }
888
889 let cookies_line = if !ctx.cookies.is_empty() {
891 let cookie_str: Vec<String> = ctx.cookies.iter().map(|(k, v)| format!("{k}={v}")).collect();
892 let cookie_header = escape_java(&cookie_str.join("; "));
893 Some(format!("builder = builder.header(\"Cookie\", \"{cookie_header}\");"))
894 } else {
895 None
896 };
897
898 let rendered = crate::template_env::render(
899 "java/http_request.jinja",
900 minijinja::context! {
901 method => method,
902 path => path,
903 body_publisher => body_publisher,
904 content_type => content_type,
905 headers_lines => headers_lines,
906 cookies_line => cookies_line,
907 response_var => ctx.response_var,
908 },
909 );
910 out.push_str(&rendered);
911 }
912
913 fn render_assert_status(&self, out: &mut String, response_var: &str, status: u16) {
915 let rendered = crate::template_env::render(
916 "java/http_assertions.jinja",
917 minijinja::context! {
918 response_var => response_var,
919 status_code => status,
920 headers => Vec::<std::collections::HashMap<&str, String>>::new(),
921 body_assertion => String::new(),
922 partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
923 validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
924 },
925 );
926 out.push_str(&rendered);
927 }
928
929 fn render_assert_header(&self, out: &mut String, response_var: &str, name: &str, expected: &str) {
933 let escaped_name = escape_java(name);
934 let assertion_code = match expected {
935 "<<present>>" => {
936 format!(
937 "assertTrue({response_var}.headers().firstValue(\"{escaped_name}\").isPresent(), \"header {escaped_name} should be present\");"
938 )
939 }
940 "<<absent>>" => {
941 format!(
942 "assertTrue({response_var}.headers().firstValue(\"{escaped_name}\").isEmpty(), \"header {escaped_name} should be absent\");"
943 )
944 }
945 "<<uuid>>" => {
946 format!(
947 "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\");"
948 )
949 }
950 literal => {
951 let escaped_value = escape_java(literal);
952 format!(
953 "assertTrue({response_var}.headers().firstValue(\"{escaped_name}\").orElse(\"\").contains(\"{escaped_value}\"), \"header {escaped_name} mismatch\");"
954 )
955 }
956 };
957
958 let mut headers = vec![std::collections::HashMap::new()];
959 headers[0].insert("assertion_code", assertion_code);
960
961 let rendered = crate::template_env::render(
962 "java/http_assertions.jinja",
963 minijinja::context! {
964 response_var => response_var,
965 status_code => 0u16,
966 headers => headers,
967 body_assertion => String::new(),
968 partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
969 validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
970 },
971 );
972 out.push_str(&rendered);
973 }
974
975 fn render_assert_json_body(&self, out: &mut String, response_var: &str, expected: &serde_json::Value) {
977 let body_assertion = match expected {
978 serde_json::Value::Object(_) | serde_json::Value::Array(_) => {
979 let json_str = serde_json::to_string(expected).unwrap_or_default();
980 let escaped = escape_java(&json_str);
981 format!(
982 "var bodyJson = MAPPER.readTree({response_var}.body());\n var expectedJson = MAPPER.readTree(\"{escaped}\");\n assertEquals(expectedJson, bodyJson, \"body mismatch\");"
983 )
984 }
985 serde_json::Value::String(s) => {
986 let escaped = escape_java(s);
987 format!("assertEquals(\"{escaped}\", {response_var}.body().trim(), \"body mismatch\");")
988 }
989 other => {
990 let escaped = escape_java(&other.to_string());
991 format!("assertEquals(\"{escaped}\", {response_var}.body().trim(), \"body mismatch\");")
992 }
993 };
994
995 let rendered = crate::template_env::render(
996 "java/http_assertions.jinja",
997 minijinja::context! {
998 response_var => response_var,
999 status_code => 0u16,
1000 headers => Vec::<std::collections::HashMap<&str, String>>::new(),
1001 body_assertion => body_assertion,
1002 partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
1003 validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
1004 },
1005 );
1006 out.push_str(&rendered);
1007 }
1008
1009 fn render_assert_partial_body(&self, out: &mut String, response_var: &str, expected: &serde_json::Value) {
1011 if let Some(obj) = expected.as_object() {
1012 let mut partial_body: Vec<std::collections::HashMap<&str, String>> = Vec::new();
1013 for (key, val) in obj {
1014 let escaped_key = escape_java(key);
1015 let json_str = serde_json::to_string(val).unwrap_or_default();
1016 let escaped_val = escape_java(&json_str);
1017 let assertion_code = format!(
1018 "assertEquals(MAPPER.readTree(\"{escaped_val}\"), partialJson.get(\"{escaped_key}\"), \"body field '{escaped_key}' mismatch\");"
1019 );
1020 let mut entry = std::collections::HashMap::new();
1021 entry.insert("assertion_code", assertion_code);
1022 partial_body.push(entry);
1023 }
1024
1025 let rendered = crate::template_env::render(
1026 "java/http_assertions.jinja",
1027 minijinja::context! {
1028 response_var => response_var,
1029 status_code => 0u16,
1030 headers => Vec::<std::collections::HashMap<&str, String>>::new(),
1031 body_assertion => String::new(),
1032 partial_body => partial_body,
1033 validation_errors => Vec::<std::collections::HashMap<&str, String>>::new(),
1034 },
1035 );
1036 out.push_str(&rendered);
1037 }
1038 }
1039
1040 fn render_assert_validation_errors(
1042 &self,
1043 out: &mut String,
1044 response_var: &str,
1045 errors: &[crate::fixture::ValidationErrorExpectation],
1046 ) {
1047 let mut validation_errors: Vec<std::collections::HashMap<&str, String>> = Vec::new();
1048 for err in errors {
1049 let escaped_msg = escape_java(&err.msg);
1050 let assertion_code = format!(
1051 "assertTrue(veBody.contains(\"{escaped_msg}\"), \"expected validation error message: {escaped_msg}\");"
1052 );
1053 let mut entry = std::collections::HashMap::new();
1054 entry.insert("assertion_code", assertion_code);
1055 validation_errors.push(entry);
1056 }
1057
1058 let rendered = crate::template_env::render(
1059 "java/http_assertions.jinja",
1060 minijinja::context! {
1061 response_var => response_var,
1062 status_code => 0u16,
1063 headers => Vec::<std::collections::HashMap<&str, String>>::new(),
1064 body_assertion => String::new(),
1065 partial_body => Vec::<std::collections::HashMap<&str, String>>::new(),
1066 validation_errors => validation_errors,
1067 },
1068 );
1069 out.push_str(&rendered);
1070 }
1071}
1072
1073fn render_http_test_method(out: &mut String, fixture: &Fixture, http: &HttpFixture) {
1080 if http.expected_response.status_code == 101 {
1083 let method_name = fixture.id.to_upper_camel_case();
1084 let description = &fixture.description;
1085 out.push_str(&crate::template_env::render(
1086 "java/http_test_skip_101.jinja",
1087 minijinja::context! {
1088 method_name => method_name,
1089 description => description,
1090 },
1091 ));
1092 return;
1093 }
1094
1095 client::http_call::render_http_test(out, &JavaTestClientRenderer, fixture);
1096}
1097
1098#[allow(clippy::too_many_arguments)]
1099fn render_test_method(
1100 out: &mut String,
1101 fixture: &Fixture,
1102 class_name: &str,
1103 _function_name: &str,
1104 _result_var: &str,
1105 _args: &[crate::config::ArgMapping],
1106 options_type: Option<&str>,
1107 result_is_simple: bool,
1108 e2e_config: &E2eConfig,
1109 nested_types: &std::collections::HashMap<String, String>,
1110 nested_types_optional: bool,
1111 adapters: &[alef_core::config::extras::AdapterConfig],
1112) {
1113 if let Some(http) = &fixture.http {
1115 render_http_test_method(out, fixture, http);
1116 return;
1117 }
1118
1119 let call_config = e2e_config.resolve_call_for_fixture(
1122 fixture.call.as_deref(),
1123 &fixture.id,
1124 &fixture.resolved_category(),
1125 &fixture.tags,
1126 &fixture.input,
1127 );
1128 let call_field_resolver = FieldResolver::new(
1131 e2e_config.effective_fields(call_config),
1132 e2e_config.effective_fields_optional(call_config),
1133 e2e_config.effective_result_fields(call_config),
1134 e2e_config.effective_fields_array(call_config),
1135 &std::collections::HashSet::new(),
1136 );
1137 let field_resolver = &call_field_resolver;
1138 let effective_enum_fields = e2e_config.effective_fields_enum(call_config);
1139 let enum_fields = effective_enum_fields;
1140 let lang = "java";
1141 let call_overrides = call_config.overrides.get(lang);
1142 let effective_function_name = call_overrides
1143 .and_then(|o| o.function.as_ref())
1144 .cloned()
1145 .unwrap_or_else(|| call_config.function.to_lower_camel_case());
1146 let effective_result_var = &call_config.result_var;
1147 let effective_args = &call_config.args;
1148 let function_name = effective_function_name.as_str();
1149 let result_var = effective_result_var.as_str();
1150 let args: &[crate::config::ArgMapping] = effective_args.as_slice();
1151
1152 let method_name = fixture.id.to_upper_camel_case();
1153 let description = &fixture.description;
1154 let expects_error = fixture.assertions.iter().any(|a| a.assertion_type == "error");
1155
1156 let effective_options_type: Option<String> = call_overrides
1162 .and_then(|o| o.options_type.clone())
1163 .or_else(|| options_type.map(|s| s.to_string()))
1164 .or_else(|| {
1165 for cand in ["csharp", "c", "go", "php", "python"] {
1169 if let Some(o) = call_config.overrides.get(cand) {
1170 if let Some(t) = &o.options_type {
1171 return Some(t.clone());
1172 }
1173 }
1174 }
1175 None
1176 });
1177 let effective_options_type = effective_options_type.as_deref();
1178 let auto_from_json = effective_options_type.is_some()
1183 && call_overrides.and_then(|o| o.options_via.as_deref()).is_none()
1184 && e2e_config
1185 .call
1186 .overrides
1187 .get(lang)
1188 .and_then(|o| o.options_via.as_deref())
1189 .is_none();
1190
1191 let client_factory: Option<String> = call_overrides.and_then(|o| o.client_factory.clone()).or_else(|| {
1193 e2e_config
1194 .call
1195 .overrides
1196 .get(lang)
1197 .and_then(|o| o.client_factory.clone())
1198 });
1199
1200 let options_via: String = call_overrides
1205 .and_then(|o| o.options_via.clone())
1206 .or_else(|| e2e_config.call.overrides.get(lang).and_then(|o| o.options_via.clone()))
1207 .unwrap_or_else(|| {
1208 if auto_from_json {
1209 "from_json".to_string()
1210 } else {
1211 "kwargs".to_string()
1212 }
1213 });
1214
1215 let effective_result_is_simple =
1217 call_overrides.is_some_and(|o| o.result_is_simple) || call_config.result_is_simple || result_is_simple;
1218 let effective_result_is_bytes = call_overrides.is_some_and(|o| o.result_is_bytes);
1219 let effective_result_is_option = call_overrides.is_some_and(|o| o.result_is_option) || call_config.result_is_option;
1225
1226 let needs_deser = effective_options_type.is_some()
1228 && args.iter().any(|arg| {
1229 if arg.arg_type != "json_object" {
1230 return false;
1231 }
1232 let val = super::resolve_field(&fixture.input, &arg.field);
1233 !val.is_null() && !val.is_array()
1234 });
1235
1236 let mut builder_expressions = String::new();
1238 if let (true, Some(opts_type)) = (needs_deser, effective_options_type) {
1239 for arg in args {
1240 if arg.arg_type == "json_object" {
1241 let val = super::resolve_field(&fixture.input, &arg.field);
1242 if !val.is_null() && !val.is_array() {
1243 if options_via == "from_json" {
1244 let normalized = super::transform_json_keys_for_language(val, "snake_case");
1249 let json_str = serde_json::to_string(&normalized).unwrap_or_default();
1250 let escaped = escape_java(&json_str);
1251 let var_name = &arg.name;
1252 builder_expressions.push_str(&format!(
1253 " var {var_name} = JsonUtil.fromJson(\"{escaped}\", {opts_type}.class);\n",
1254 ));
1255 } else if let Some(obj) = val.as_object() {
1256 let empty_path_fields: Vec<String> = Vec::new();
1258 let path_fields = call_overrides.map(|o| &o.path_fields).unwrap_or(&empty_path_fields);
1259 let builder_expr = java_builder_expression(
1260 obj,
1261 opts_type,
1262 enum_fields,
1263 nested_types,
1264 nested_types_optional,
1265 path_fields,
1266 );
1267 let var_name = &arg.name;
1268 builder_expressions.push_str(&format!(" var {} = {};\n", var_name, builder_expr));
1269 }
1270 }
1271 }
1272 }
1273 }
1274
1275 let adapter = adapters.iter().find(|a| a.name == call_config.function.as_str());
1276 let adapter_request_type: Option<String> = adapter
1277 .and_then(|a| a.request_type.as_deref())
1278 .map(|rt| rt.rsplit("::").next().unwrap_or(rt).to_string());
1279
1280 let is_streaming_adapter =
1282 adapter.is_some_and(|a| matches!(a.pattern, alef_core::config::extras::AdapterPattern::Streaming));
1283
1284 let filtered_args: Vec<_> = if adapter.is_some_and(|a| a.owner_type.is_some()) && !is_streaming_adapter {
1289 args.iter().filter(|arg| arg.arg_type != "handle").cloned().collect()
1290 } else {
1291 args.to_vec()
1292 };
1293
1294 let (mut setup_lines, args_str) = build_args_and_setup(
1295 &fixture.input,
1296 &filtered_args,
1297 class_name,
1298 effective_options_type,
1299 fixture,
1300 adapter_request_type.as_deref(),
1301 );
1302
1303 let extra_args_slice: &[String] = call_overrides.map_or(&[], |o| o.extra_args.as_slice());
1308
1309 let mut visitor_var = String::new();
1311 let mut has_visitor_fixture = false;
1312 if let Some(visitor_spec) = &fixture.visitor {
1313 visitor_var = build_java_visitor(&mut setup_lines, visitor_spec, class_name);
1314 has_visitor_fixture = true;
1315 }
1316
1317 let mut final_args = if has_visitor_fixture {
1319 if args_str.is_empty() {
1320 format!("new ConversionOptions().withVisitor({})", visitor_var)
1321 } else if args_str.contains("new ConversionOptions")
1322 || args_str.contains("ConversionOptionsBuilder")
1323 || args_str.contains(".builder()")
1324 {
1325 if args_str.contains(".build()") {
1328 let idx = args_str.rfind(".build()").unwrap();
1329 format!("{}.withVisitor({}){}", &args_str[..idx], visitor_var, &args_str[idx..])
1330 } else {
1331 format!("{}.withVisitor({})", args_str, visitor_var)
1332 }
1333 } else if args_str.ends_with(", null") {
1334 let base = &args_str[..args_str.len() - 6];
1335 format!("{}, new ConversionOptions().withVisitor({})", base, visitor_var)
1336 } else {
1337 format!("{}, new ConversionOptions().withVisitor({})", args_str, visitor_var)
1338 }
1339 } else {
1340 args_str
1341 };
1342
1343 if !extra_args_slice.is_empty() {
1344 let extra_str = extra_args_slice.join(", ");
1345 final_args = if final_args.is_empty() {
1346 extra_str
1347 } else {
1348 format!("{final_args}, {extra_str}")
1349 };
1350 }
1351
1352 let mut assertions_body = String::new();
1354
1355 let needs_source_var = fixture
1357 .assertions
1358 .iter()
1359 .any(|a| a.assertion_type == "method_result" && a.method.as_deref() == Some("run_query"));
1360 if needs_source_var {
1361 if let Some(source_arg) = args.iter().find(|a| a.field == "source_code") {
1362 let field = source_arg.field.strip_prefix("input.").unwrap_or(&source_arg.field);
1363 if let Some(val) = fixture.input.get(field) {
1364 let java_val = json_to_java(val);
1365 assertions_body.push_str(&format!(" var source = {}.getBytes();\n", java_val));
1366 }
1367 }
1368 }
1369
1370 let assert_enum_types: std::collections::HashMap<String, String> = if let Some(co) = call_overrides {
1377 co.assert_enum_fields.clone()
1378 } else {
1379 std::collections::HashMap::new()
1380 };
1381
1382 let mut effective_enum_fields: std::collections::HashSet<String> = enum_fields.clone();
1384 if let Some(co) = call_overrides {
1385 for k in co.enum_fields.keys() {
1386 effective_enum_fields.insert(k.clone());
1387 }
1388 }
1389
1390 let is_streaming = crate::codegen::streaming_assertions::resolve_is_streaming(fixture, call_config.streaming);
1395
1396 for assertion in &fixture.assertions {
1397 render_assertion(
1398 &mut assertions_body,
1399 assertion,
1400 result_var,
1401 class_name,
1402 field_resolver,
1403 effective_result_is_simple,
1404 effective_result_is_bytes,
1405 effective_result_is_option,
1406 is_streaming,
1407 &effective_enum_fields,
1408 &assert_enum_types,
1409 );
1410 }
1411
1412 let throws_clause = " throws Exception";
1413
1414 let (client_setup_lines, call_target) = if let Some(factory) = client_factory.as_deref() {
1417 let factory_name = factory.to_lower_camel_case();
1418 let fixture_id = &fixture.id;
1419 let mut setup: Vec<String> = Vec::new();
1420 let has_mock = fixture.mock_response.is_some() || fixture.http.is_some();
1421 let api_key_var = fixture.env.as_ref().and_then(|e| e.api_key_var.as_deref());
1422 if let Some(var) = api_key_var.filter(|_| has_mock) {
1423 setup.push(format!("String apiKey = System.getenv(\"{var}\");"));
1424 setup.push(format!(
1425 "String baseUrl = (apiKey != null && !apiKey.isEmpty()) ? null : System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\";"
1426 ));
1427 setup.push(format!(
1428 "System.out.println(\"{fixture_id}: \" + (baseUrl == null ? \"using real API ({var} is set)\" : \"using mock server ({var} not set)\"));"
1429 ));
1430 setup.push(format!(
1431 "var client = {class_name}.{factory_name}(baseUrl == null ? apiKey : \"test-key\", baseUrl, null, null, null);"
1432 ));
1433 } else if has_mock {
1434 if fixture.has_host_root_route() {
1435 setup.push(format!(
1436 "String mockUrl = System.getProperty(\"mockServer.{fixture_id}\", System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\");"
1437 ));
1438 } else {
1439 setup.push(format!(
1440 "String mockUrl = System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\";"
1441 ));
1442 }
1443 setup.push(format!(
1444 "var client = {class_name}.{factory_name}(\"test-key\", mockUrl, null, null, null);"
1445 ));
1446 } else if let Some(api_key_var) = api_key_var {
1447 setup.push(format!("String apiKey = System.getenv(\"{api_key_var}\");"));
1448 setup.push(format!(
1449 "org.junit.jupiter.api.Assumptions.assumeTrue(apiKey != null && !apiKey.isEmpty(), \"{api_key_var} not set\");"
1450 ));
1451 setup.push(format!("var client = {class_name}.{factory_name}(apiKey);"));
1452 } else {
1453 setup.push(format!("var client = {class_name}.{factory_name}(\"test-key\");"));
1454 }
1455 (setup, "client".to_string())
1456 } else {
1457 (Vec::new(), class_name.to_string())
1458 };
1459
1460 let combined_setup: Vec<String> = client_setup_lines.into_iter().chain(setup_lines).collect();
1462
1463 let call_expr = format!("{call_target}.{function_name}({final_args})");
1464
1465 let collect_snippet = if is_streaming && !expects_error {
1467 let item_type_for_streaming = adapter
1469 .and_then(|a| a.item_type.as_deref())
1470 .map(|it| it.rsplit("::").next().unwrap_or(it));
1471 crate::codegen::streaming_assertions::StreamingFieldResolver::collect_snippet_typed(
1472 "java",
1473 result_var,
1474 "chunks",
1475 item_type_for_streaming,
1476 )
1477 .unwrap_or_default()
1478 } else {
1479 String::new()
1480 };
1481
1482 let rendered = crate::template_env::render(
1483 "java/test_method.jinja",
1484 minijinja::context! {
1485 method_name => method_name,
1486 description => description,
1487 builder_expressions => builder_expressions,
1488 setup_lines => combined_setup,
1489 throws_clause => throws_clause,
1490 expects_error => expects_error,
1491 call_expr => call_expr,
1492 result_var => result_var,
1493 returns_void => call_config.returns_void,
1494 collect_snippet => collect_snippet,
1495 assertions_body => assertions_body,
1496 },
1497 );
1498 out.push_str(&rendered);
1499}
1500
1501fn build_args_and_setup(
1505 input: &serde_json::Value,
1506 args: &[crate::config::ArgMapping],
1507 class_name: &str,
1508 options_type: Option<&str>,
1509 fixture: &crate::fixture::Fixture,
1510 adapter_request_type: Option<&str>,
1511) -> (Vec<String>, String) {
1512 let fixture_id = &fixture.id;
1513 if args.is_empty() {
1514 return (Vec::new(), String::new());
1515 }
1516
1517 let mut setup_lines: Vec<String> = Vec::new();
1518 let mut parts: Vec<String> = Vec::new();
1519
1520 for arg in args {
1521 if arg.arg_type == "mock_url" {
1522 if fixture.has_host_root_route() {
1523 setup_lines.push(format!(
1524 "String {} = System.getProperty(\"mockServer.{fixture_id}\", System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\");",
1525 arg.name,
1526 ));
1527 } else {
1528 setup_lines.push(format!(
1529 "String {} = System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\";",
1530 arg.name,
1531 ));
1532 }
1533 if let Some(req_type) = adapter_request_type {
1534 let req_var = format!("{}Req", arg.name);
1535 setup_lines.push(format!("var {req_var} = new {req_type}({});", arg.name));
1536 parts.push(req_var);
1537 } else {
1538 parts.push(arg.name.clone());
1539 }
1540 continue;
1541 }
1542
1543 if arg.arg_type == "mock_url_list" {
1544 let env_key = format!("MOCK_SERVER_{}", fixture_id.to_uppercase());
1550 let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1551 let val = input.get(field).unwrap_or(&serde_json::Value::Null);
1552 let paths: Vec<String> = if let Some(arr) = val.as_array() {
1553 arr.iter()
1554 .filter_map(|v| v.as_str().map(|s| format!("\"{}\"", escape_java(s))))
1555 .collect()
1556 } else {
1557 Vec::new()
1558 };
1559 let paths_literal = paths.join(", ");
1560 let name = &arg.name;
1561 setup_lines.push(format!(
1562 "String {name}Base = System.getenv().getOrDefault(\"{env_key}\", System.getenv(\"MOCK_SERVER_URL\") + \"/fixtures/{fixture_id}\");"
1563 ));
1564 setup_lines.push(format!(
1565 "java.util.List<String> {name} = java.util.Arrays.stream(new String[]{{{paths_literal}}}).map(p -> p.startsWith(\"http\") ? p : {name}Base + p).collect(java.util.stream.Collectors.toList());"
1566 ));
1567 parts.push(name.clone());
1568 continue;
1569 }
1570
1571 if arg.arg_type == "handle" {
1572 let constructor_name = format!("create{}", arg.name.to_upper_camel_case());
1574 let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1575 let config_value = input.get(field).unwrap_or(&serde_json::Value::Null);
1576 if config_value.is_null()
1577 || config_value.is_object() && config_value.as_object().is_some_and(|o| o.is_empty())
1578 {
1579 setup_lines.push(format!("var {} = {class_name}.{constructor_name}(null);", arg.name,));
1580 } else {
1581 let json_str = serde_json::to_string(config_value).unwrap_or_default();
1582 let name = &arg.name;
1583 setup_lines.push(format!(
1584 "var {name}Config = MAPPER.readValue(\"{}\", CrawlConfig.class);",
1585 escape_java(&json_str),
1586 ));
1587 setup_lines.push(format!(
1588 "var {} = {class_name}.{constructor_name}({name}Config);",
1589 arg.name,
1590 name = name,
1591 ));
1592 }
1593 parts.push(arg.name.clone());
1594 continue;
1595 }
1596
1597 let resolved = super::resolve_field(input, &arg.field);
1598 let val = if resolved.is_null() { None } else { Some(resolved) };
1599 match val {
1600 None | Some(serde_json::Value::Null) if arg.optional => {
1601 if arg.arg_type == "json_object" {
1605 if let Some(opts_type) = options_type {
1606 parts.push(format!("{opts_type}.builder().build()"));
1607 } else {
1608 parts.push("null".to_string());
1609 }
1610 } else {
1611 parts.push("null".to_string());
1612 }
1613 }
1614 None | Some(serde_json::Value::Null) => {
1615 let default_val = match arg.arg_type.as_str() {
1617 "string" | "file_path" => "\"\"".to_string(),
1618 "int" | "integer" => "0".to_string(),
1619 "float" | "number" => "0.0d".to_string(),
1620 "bool" | "boolean" => "false".to_string(),
1621 _ => "null".to_string(),
1622 };
1623 parts.push(default_val);
1624 }
1625 Some(v) => {
1626 if arg.arg_type == "json_object" {
1627 if v.is_array() {
1630 if let Some(elem_type) = &arg.element_type {
1631 if elem_type == "BatchBytesItem" || elem_type == "BatchFileItem" {
1632 parts.push(emit_java_batch_item_array(v, elem_type));
1633 continue;
1634 }
1635 if !is_numeric_type_hint(elem_type) {
1637 parts.push(emit_java_object_array(v, elem_type));
1638 continue;
1639 }
1640 }
1641 let elem_type = arg.element_type.as_deref();
1643 parts.push(json_to_java_typed(v, elem_type));
1644 continue;
1645 }
1646 if options_type.is_some() {
1648 parts.push(arg.name.clone());
1649 continue;
1650 }
1651 parts.push(json_to_java(v));
1652 continue;
1653 }
1654 if arg.arg_type == "bytes" {
1658 let val = json_to_java(v);
1659 parts.push(format!(
1660 "java.nio.file.Files.readAllBytes(java.nio.file.Path.of({val}))"
1661 ));
1662 continue;
1663 }
1664 if arg.arg_type == "file_path" {
1666 let val = json_to_java(v);
1667 parts.push(format!("java.nio.file.Path.of({val})"));
1668 continue;
1669 }
1670 parts.push(json_to_java(v));
1671 }
1672 }
1673 }
1674
1675 (setup_lines, parts.join(", "))
1676}
1677
1678#[allow(clippy::too_many_arguments)]
1679fn render_assertion(
1680 out: &mut String,
1681 assertion: &Assertion,
1682 result_var: &str,
1683 class_name: &str,
1684 field_resolver: &FieldResolver,
1685 result_is_simple: bool,
1686 result_is_bytes: bool,
1687 result_is_option: bool,
1688 is_streaming: bool,
1689 enum_fields: &std::collections::HashSet<String>,
1690 assert_enum_types: &std::collections::HashMap<String, String>,
1691) {
1692 let bare_field = assertion.field.as_deref().is_none_or(str::is_empty);
1697 if result_is_option && bare_field {
1698 match assertion.assertion_type.as_str() {
1699 "is_empty" => {
1700 out.push_str(&format!(
1701 " assertNull({result_var}, \"expected empty value\");\n"
1702 ));
1703 return;
1704 }
1705 "not_empty" => {
1706 out.push_str(&format!(
1707 " assertNotNull({result_var}, \"expected non-empty value\");\n"
1708 ));
1709 return;
1710 }
1711 _ => {}
1712 }
1713 }
1714
1715 if result_is_bytes {
1720 match assertion.assertion_type.as_str() {
1721 "not_empty" => {
1722 out.push_str(&format!(
1723 " assertTrue({result_var}.length > 0, \"expected non-empty value\");\n"
1724 ));
1725 return;
1726 }
1727 "is_empty" => {
1728 out.push_str(&format!(
1729 " assertEquals(0, {result_var}.length, \"expected empty value\");\n"
1730 ));
1731 return;
1732 }
1733 "count_equals" | "length_equals" => {
1734 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1735 out.push_str(&format!(" assertEquals({n}, {result_var}.length);\n"));
1736 }
1737 return;
1738 }
1739 "count_min" | "length_min" => {
1740 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1741 out.push_str(&format!(
1742 " assertTrue({result_var}.length >= {n}, \"expected length >= {n}\");\n"
1743 ));
1744 }
1745 return;
1746 }
1747 "not_error" => {
1748 out.push_str(&format!(
1751 " assertNotNull({result_var}, \"expected non-null byte[] response\");\n"
1752 ));
1753 return;
1754 }
1755 _ => {
1756 out.push_str(&format!(
1757 " // skipped: assertion type '{}' not supported on byte[] result\n",
1758 assertion.assertion_type
1759 ));
1760 return;
1761 }
1762 }
1763 }
1764
1765 if let Some(f) = &assertion.field {
1767 match f.as_str() {
1768 "chunks_have_content" => {
1770 let pred = format!(
1771 "java.util.Optional.ofNullable({result_var}.chunks()).orElse(java.util.List.of()).stream().allMatch(c -> c.content() != null && !c.content().isBlank())"
1772 );
1773 out.push_str(&crate::template_env::render(
1774 "java/synthetic_assertion.jinja",
1775 minijinja::context! {
1776 assertion_kind => "chunks_content",
1777 assertion_type => assertion.assertion_type.as_str(),
1778 pred => pred,
1779 field_name => f,
1780 },
1781 ));
1782 return;
1783 }
1784 "chunks_have_heading_context" => {
1785 let pred = format!(
1786 "java.util.Optional.ofNullable({result_var}.chunks()).orElse(java.util.List.of()).stream().allMatch(c -> c.metadata().headingContext() != null)"
1787 );
1788 out.push_str(&crate::template_env::render(
1789 "java/synthetic_assertion.jinja",
1790 minijinja::context! {
1791 assertion_kind => "chunks_heading_context",
1792 assertion_type => assertion.assertion_type.as_str(),
1793 pred => pred,
1794 field_name => f,
1795 },
1796 ));
1797 return;
1798 }
1799 "chunks_have_embeddings" => {
1800 let pred = format!(
1801 "java.util.Optional.ofNullable({result_var}.chunks()).orElse(java.util.List.of()).stream().allMatch(c -> c.embedding() != null && !c.embedding().isEmpty())"
1802 );
1803 out.push_str(&crate::template_env::render(
1804 "java/synthetic_assertion.jinja",
1805 minijinja::context! {
1806 assertion_kind => "chunks_embeddings",
1807 assertion_type => assertion.assertion_type.as_str(),
1808 pred => pred,
1809 field_name => f,
1810 },
1811 ));
1812 return;
1813 }
1814 "first_chunk_starts_with_heading" => {
1815 let pred = format!(
1816 "java.util.Optional.ofNullable({result_var}.chunks()).orElse(java.util.List.of()).stream().findFirst().map(c -> c.metadata().headingContext() != null).orElse(false)"
1817 );
1818 out.push_str(&crate::template_env::render(
1819 "java/synthetic_assertion.jinja",
1820 minijinja::context! {
1821 assertion_kind => "first_chunk_heading",
1822 assertion_type => assertion.assertion_type.as_str(),
1823 pred => pred,
1824 field_name => f,
1825 },
1826 ));
1827 return;
1828 }
1829 "embedding_dimensions" => {
1833 let embed_list = if result_is_simple {
1835 result_var.to_string()
1836 } else {
1837 format!("{result_var}.embeddings()")
1838 };
1839 let expr = format!("({embed_list}.isEmpty() ? 0 : {embed_list}.get(0).size())");
1840 let java_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
1841 out.push_str(&crate::template_env::render(
1842 "java/synthetic_assertion.jinja",
1843 minijinja::context! {
1844 assertion_kind => "embedding_dimensions",
1845 assertion_type => assertion.assertion_type.as_str(),
1846 expr => expr,
1847 java_val => java_val,
1848 field_name => f,
1849 },
1850 ));
1851 return;
1852 }
1853 "embeddings_valid" | "embeddings_finite" | "embeddings_non_zero" | "embeddings_normalized" => {
1854 let embed_list = if result_is_simple {
1856 result_var.to_string()
1857 } else {
1858 format!("{result_var}.embeddings()")
1859 };
1860 let pred = match f.as_str() {
1861 "embeddings_valid" => {
1862 format!("{embed_list}.stream().allMatch(e -> e != null && !e.isEmpty())")
1863 }
1864 "embeddings_finite" => {
1865 format!("{embed_list}.stream().flatMap(java.util.Collection::stream).allMatch(Float::isFinite)")
1866 }
1867 "embeddings_non_zero" => {
1868 format!("{embed_list}.stream().allMatch(e -> e.stream().anyMatch(v -> v != 0.0f))")
1869 }
1870 "embeddings_normalized" => format!(
1871 "{embed_list}.stream().allMatch(e -> {{ double n = e.stream().mapToDouble(v -> v * v).sum(); return Math.abs(n - 1.0) < 1e-3; }})"
1872 ),
1873 _ => unreachable!(),
1874 };
1875 let assertion_kind = format!("embeddings_{}", f.strip_prefix("embeddings_").unwrap_or(f));
1876 out.push_str(&crate::template_env::render(
1877 "java/synthetic_assertion.jinja",
1878 minijinja::context! {
1879 assertion_kind => assertion_kind,
1880 assertion_type => assertion.assertion_type.as_str(),
1881 pred => pred,
1882 field_name => f,
1883 },
1884 ));
1885 return;
1886 }
1887 "keywords" | "keywords_count" => {
1889 out.push_str(&crate::template_env::render(
1890 "java/synthetic_assertion.jinja",
1891 minijinja::context! {
1892 assertion_kind => "keywords",
1893 field_name => f,
1894 },
1895 ));
1896 return;
1897 }
1898 "metadata" => {
1901 match assertion.assertion_type.as_str() {
1902 "not_empty" | "is_empty" => {
1903 out.push_str(&crate::template_env::render(
1904 "java/synthetic_assertion.jinja",
1905 minijinja::context! {
1906 assertion_kind => "metadata",
1907 assertion_type => assertion.assertion_type.as_str(),
1908 result_var => result_var,
1909 },
1910 ));
1911 return;
1912 }
1913 _ => {} }
1915 }
1916 _ => {}
1917 }
1918 }
1919
1920 if let Some(f) = &assertion.field {
1926 if is_streaming && !f.is_empty() && crate::codegen::streaming_assertions::is_streaming_virtual_field(f) {
1927 if let Some(expr) =
1928 crate::codegen::streaming_assertions::StreamingFieldResolver::accessor(f, "java", "chunks")
1929 {
1930 let line = match assertion.assertion_type.as_str() {
1931 "count_min" => {
1932 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1933 format!(" assertTrue({expr}.size() >= {n}, \"expected >= {n} chunks\");\n")
1934 } else {
1935 String::new()
1936 }
1937 }
1938 "count_equals" => {
1939 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1940 format!(" assertEquals({n}, {expr}.size());\n")
1941 } else {
1942 String::new()
1943 }
1944 }
1945 "equals" => {
1946 if let Some(serde_json::Value::String(s)) = &assertion.value {
1947 let escaped = crate::escape::escape_java(s);
1948 format!(" assertEquals(\"{escaped}\", {expr});\n")
1949 } else if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1950 format!(" assertEquals({n}, {expr});\n")
1951 } else {
1952 String::new()
1953 }
1954 }
1955 "not_empty" => format!(" assertFalse({expr}.isEmpty(), \"expected non-empty\");\n"),
1956 "is_empty" => format!(" assertTrue({expr}.isEmpty(), \"expected empty\");\n"),
1957 "is_true" => format!(" assertTrue({expr}, \"expected true\");\n"),
1958 "is_false" => format!(" assertFalse({expr}, \"expected false\");\n"),
1959 "greater_than" => {
1960 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1961 format!(" assertTrue({expr} > {n}, \"expected > {n}\");\n")
1962 } else {
1963 String::new()
1964 }
1965 }
1966 "greater_than_or_equal" => {
1967 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1968 format!(" assertTrue({expr} >= {n}, \"expected >= {n}\");\n")
1969 } else {
1970 String::new()
1971 }
1972 }
1973 "contains" => {
1974 if let Some(serde_json::Value::String(s)) = &assertion.value {
1975 let escaped = crate::escape::escape_java(s);
1976 format!(
1977 " assertTrue({expr}.contains(\"{escaped}\"), \"expected to contain: {escaped}\");\n"
1978 )
1979 } else {
1980 String::new()
1981 }
1982 }
1983 _ => format!(
1984 " // streaming field '{f}': assertion type '{}' not rendered\n",
1985 assertion.assertion_type
1986 ),
1987 };
1988 if !line.is_empty() {
1989 out.push_str(&line);
1990 }
1991 }
1992 return;
1993 }
1994 }
1995
1996 if let Some(f) = &assertion.field {
1998 if !f.is_empty() && !field_resolver.is_valid_for_result(f) {
1999 out.push_str(&crate::template_env::render(
2000 "java/synthetic_assertion.jinja",
2001 minijinja::context! {
2002 assertion_kind => "skipped",
2003 field_name => f,
2004 },
2005 ));
2006 return;
2007 }
2008 }
2009
2010 let sealed_display_type: Option<String> = assertion.field.as_deref().and_then(|f| {
2015 let resolved = field_resolver.resolve(f);
2016 assert_enum_types
2017 .get(f)
2018 .or_else(|| assert_enum_types.get(resolved))
2019 .cloned()
2020 });
2021 let is_sealed_display_field = sealed_display_type.is_some();
2022
2023 let field_is_enum = assertion.field.as_deref().is_some_and(|f| {
2030 let resolved = field_resolver.resolve(f);
2031 let in_enum_fields = enum_fields.get(f).is_some() || enum_fields.get(resolved).is_some();
2032 in_enum_fields && !is_sealed_display_field
2033 });
2034
2035 let field_is_array = assertion
2039 .field
2040 .as_deref()
2041 .is_some_and(|f| field_resolver.is_array(field_resolver.resolve(f)));
2042
2043 let field_expr = if result_is_simple {
2044 result_var.to_string()
2045 } else {
2046 match &assertion.field {
2047 Some(f) if !f.is_empty() => {
2048 let accessor = field_resolver.accessor(f, "java", result_var);
2049 let resolved = field_resolver.resolve(f);
2050 if field_resolver.is_optional(resolved) && !field_resolver.has_map_access(f) {
2057 let optional_expr = format!("java.util.Optional.ofNullable({accessor})");
2060 if field_is_enum {
2064 match assertion.assertion_type.as_str() {
2065 "not_empty" | "is_empty" => optional_expr,
2066 _ => {
2067 format!("{optional_expr}.map(v -> v.getValue()).orElse(\"\")")
2071 }
2072 }
2073 } else {
2074 match assertion.assertion_type.as_str() {
2075 "not_empty" | "is_empty" => optional_expr,
2078 "count_min" | "count_equals" => {
2080 format!("{optional_expr}.orElse(java.util.List.of())")
2081 }
2082 "greater_than" | "less_than" | "greater_than_or_equal" | "less_than_or_equal" => {
2089 if field_resolver.is_array(resolved) {
2090 format!("{optional_expr}.orElse(java.util.List.of())")
2091 } else {
2092 format!("{optional_expr}.map(Number::longValue).orElse(0L)")
2093 }
2094 }
2095 "equals" => {
2101 if is_sealed_display_field {
2102 optional_expr
2104 } else if let Some(expected) = &assertion.value {
2105 if expected.is_number() {
2106 format!("{optional_expr}.map(Number::longValue).orElse(0L)")
2107 } else {
2108 format!("{optional_expr}.orElse(\"\")")
2109 }
2110 } else {
2111 format!("{optional_expr}.orElse(\"\")")
2112 }
2113 }
2114 _ if field_resolver.is_array(resolved) => {
2115 format!("{optional_expr}.orElse(java.util.List.of())")
2116 }
2117 _ => format!("{optional_expr}.orElse(\"\")"),
2118 }
2119 }
2120 } else {
2121 accessor
2122 }
2123 }
2124 _ => result_var.to_string(),
2125 }
2126 };
2127
2128 let string_expr = if field_is_enum && !field_expr.contains(".map(v -> v.getValue())") {
2136 format!("{field_expr}.getValue()")
2137 } else if let Some(ref stype) = sealed_display_type {
2138 let inner_expr = if field_expr.contains("Optional.ofNullable") {
2142 format!("{field_expr}.orElse(null)")
2143 } else {
2144 field_expr.clone()
2145 };
2146 format!("{stype}Display.toDisplayString({inner_expr})")
2147 } else {
2148 field_expr.clone()
2149 };
2150
2151 let assertion_type = assertion.assertion_type.as_str();
2153 let java_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
2154 let is_string_val = assertion.value.as_ref().is_some_and(|v| v.is_string());
2155 let is_numeric_val = assertion.value.as_ref().is_some_and(|v| v.is_number());
2156
2157 let values_java: Vec<String> = assertion
2161 .values
2162 .as_ref()
2163 .map(|values| values.iter().map(json_to_java).collect::<Vec<_>>())
2164 .or_else(|| assertion.value.as_ref().map(|v| vec![json_to_java(v)]))
2165 .unwrap_or_default();
2166
2167 let contains_any_expr = if !values_java.is_empty() {
2168 values_java
2169 .iter()
2170 .map(|v| format!("{string_expr}.contains({v})"))
2171 .collect::<Vec<_>>()
2172 .join(" || ")
2173 } else {
2174 String::new()
2175 };
2176
2177 let length_expr = if result_is_bytes {
2178 format!("{field_expr}.length")
2179 } else {
2180 format!("{field_expr}.length()")
2181 };
2182
2183 let n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);
2184
2185 let call_expr = if let Some(method_name) = &assertion.method {
2186 build_java_method_call(result_var, method_name, assertion.args.as_ref(), class_name)
2187 } else {
2188 String::new()
2189 };
2190
2191 let check = assertion.check.as_deref().unwrap_or("is_true");
2192
2193 let java_check_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
2194
2195 let check_n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);
2196
2197 let is_bool_val = assertion.value.as_ref().is_some_and(|v| v.is_boolean());
2198 let bool_is_true = assertion.value.as_ref().is_some_and(|v| v.as_bool() == Some(true));
2199
2200 let method_returns_collection = assertion
2201 .method
2202 .as_ref()
2203 .is_some_and(|m| matches!(m.as_str(), "find_nodes_by_type" | "findNodesByType"));
2204
2205 let rendered = crate::template_env::render(
2206 "java/assertion.jinja",
2207 minijinja::context! {
2208 assertion_type,
2209 java_val,
2210 string_expr,
2211 field_expr,
2212 field_is_enum,
2213 field_is_array,
2214 is_string_val,
2215 is_numeric_val,
2216 values_java => values_java,
2217 contains_any_expr,
2218 length_expr,
2219 n,
2220 call_expr,
2221 check,
2222 java_check_val,
2223 check_n,
2224 is_bool_val,
2225 bool_is_true,
2226 method_returns_collection,
2227 },
2228 );
2229 out.push_str(&rendered);
2230}
2231
2232fn build_java_method_call(
2236 result_var: &str,
2237 method_name: &str,
2238 args: Option<&serde_json::Value>,
2239 class_name: &str,
2240) -> String {
2241 match method_name {
2242 "root_child_count" => format!("{result_var}.rootNode().childCount()"),
2243 "root_node_type" => format!("{result_var}.rootNode().kind()"),
2244 "named_children_count" => format!("{result_var}.rootNode().namedChildCount()"),
2245 "has_error_nodes" => format!("{class_name}.treeHasErrorNodes({result_var})"),
2246 "error_count" | "tree_error_count" => format!("{class_name}.treeErrorCount({result_var})"),
2247 "tree_to_sexp" => format!("{class_name}.treeToSexp({result_var})"),
2248 "contains_node_type" => {
2249 let node_type = args
2250 .and_then(|a| a.get("node_type"))
2251 .and_then(|v| v.as_str())
2252 .unwrap_or("");
2253 format!("{class_name}.treeContainsNodeType({result_var}, \"{node_type}\")")
2254 }
2255 "find_nodes_by_type" => {
2256 let node_type = args
2257 .and_then(|a| a.get("node_type"))
2258 .and_then(|v| v.as_str())
2259 .unwrap_or("");
2260 format!("{class_name}.findNodesByType({result_var}, \"{node_type}\")")
2261 }
2262 "run_query" => {
2263 let query_source = args
2264 .and_then(|a| a.get("query_source"))
2265 .and_then(|v| v.as_str())
2266 .unwrap_or("");
2267 let language = args
2268 .and_then(|a| a.get("language"))
2269 .and_then(|v| v.as_str())
2270 .unwrap_or("");
2271 let escaped_query = escape_java(query_source);
2272 format!("{class_name}.runQuery({result_var}, \"{language}\", \"{escaped_query}\", source)")
2273 }
2274 _ => {
2275 format!("{result_var}.{}()", method_name.to_lower_camel_case())
2276 }
2277 }
2278}
2279
2280fn emit_java_object_array(arr: &serde_json::Value, elem_type: &str) -> String {
2283 if let Some(items) = arr.as_array() {
2284 if items.is_empty() {
2285 return "java.util.List.of()".to_string();
2286 }
2287 let item_strs: Vec<String> = items
2288 .iter()
2289 .map(|item| {
2290 let json_str = serde_json::to_string(item).unwrap_or_default();
2291 let escaped = escape_java(&json_str);
2292 format!("JsonUtil.fromJson(\"{escaped}\", {elem_type}.class)")
2293 })
2294 .collect();
2295 format!("java.util.Arrays.asList({})", item_strs.join(", "))
2296 } else {
2297 "java.util.List.of()".to_string()
2298 }
2299}
2300
2301fn json_to_java(value: &serde_json::Value) -> String {
2303 json_to_java_typed(value, None)
2304}
2305
2306fn emit_java_batch_item_array(arr: &serde_json::Value, elem_type: &str) -> String {
2310 if let Some(items) = arr.as_array() {
2311 let item_strs: Vec<String> = items
2312 .iter()
2313 .filter_map(|item| {
2314 if let Some(obj) = item.as_object() {
2315 match elem_type {
2316 "BatchBytesItem" => {
2317 let content = obj.get("content").and_then(|v| v.as_array());
2318 let mime_type = obj.get("mime_type").and_then(|v| v.as_str()).unwrap_or("text/plain");
2319 let content_code = if let Some(arr) = content {
2320 let bytes: Vec<String> = arr
2321 .iter()
2322 .filter_map(|v| v.as_u64().map(|n| format!("(byte) {}", n)))
2323 .collect();
2324 format!("new byte[] {{{}}}", bytes.join(", "))
2325 } else {
2326 "new byte[] {}".to_string()
2327 };
2328 Some(format!("new {}({}, \"{}\", null)", elem_type, content_code, mime_type))
2329 }
2330 "BatchFileItem" => {
2331 let path = obj.get("path").and_then(|v| v.as_str()).unwrap_or("");
2332 Some(format!(
2333 "new {}(java.nio.file.Paths.get(\"{}\"), null)",
2334 elem_type, path
2335 ))
2336 }
2337 _ => None,
2338 }
2339 } else {
2340 None
2341 }
2342 })
2343 .collect();
2344 format!("java.util.Arrays.asList({})", item_strs.join(", "))
2345 } else {
2346 "java.util.List.of()".to_string()
2347 }
2348}
2349
2350fn json_to_java_typed(value: &serde_json::Value, element_type: Option<&str>) -> String {
2351 match value {
2352 serde_json::Value::String(s) => format!("\"{}\"", escape_java(s)),
2353 serde_json::Value::Bool(b) => b.to_string(),
2354 serde_json::Value::Number(n) => {
2355 if n.is_f64() {
2356 match element_type {
2357 Some("f32" | "float" | "Float") => format!("{}f", n),
2358 _ => format!("{}d", n),
2359 }
2360 } else {
2361 n.to_string()
2362 }
2363 }
2364 serde_json::Value::Null => "null".to_string(),
2365 serde_json::Value::Array(arr) => {
2366 let items: Vec<String> = arr.iter().map(|v| json_to_java_typed(v, element_type)).collect();
2367 format!("java.util.List.of({})", items.join(", "))
2368 }
2369 serde_json::Value::Object(_) => {
2370 let json_str = serde_json::to_string(value).unwrap_or_default();
2371 format!("\"{}\"", escape_java(&json_str))
2372 }
2373 }
2374}
2375
2376fn java_builder_expression(
2387 obj: &serde_json::Map<String, serde_json::Value>,
2388 type_name: &str,
2389 enum_fields: &std::collections::HashSet<String>,
2390 nested_types: &std::collections::HashMap<String, String>,
2391 nested_types_optional: bool,
2392 path_fields: &[String],
2393) -> String {
2394 let mut expr = format!("{}.builder()", type_name);
2395 for (key, val) in obj {
2396 let camel_key = key.to_lower_camel_case();
2398 let method_name = format!("with{}", camel_key.to_upper_camel_case());
2399
2400 let java_val = match val {
2401 serde_json::Value::String(s) => {
2402 if enum_fields.contains(&camel_key) {
2405 let enum_type_name = camel_key.to_upper_camel_case();
2407 let variant_name = s.to_upper_camel_case();
2408 format!("{}.{}", enum_type_name, variant_name)
2409 } else if camel_key == "preset" && type_name == "PreprocessingOptions" {
2410 let variant_name = s.to_upper_camel_case();
2412 format!("PreprocessingPreset.{}", variant_name)
2413 } else if path_fields.contains(key) {
2414 format!("Optional.of(java.nio.file.Path.of(\"{}\"))", escape_java(s))
2416 } else {
2417 format!("\"{}\"", escape_java(s))
2419 }
2420 }
2421 serde_json::Value::Bool(b) => b.to_string(),
2422 serde_json::Value::Null => "null".to_string(),
2423 serde_json::Value::Number(n) => {
2424 let camel_key = key.to_lower_camel_case();
2432 let is_plain_field = matches!(camel_key.as_str(), "listIndentWidth" | "wrapWidth");
2433 let is_primitive_builder = matches!(type_name, "SecurityLimits" | "SecurityLimitsBuilder");
2436
2437 if is_plain_field || is_primitive_builder {
2438 if n.is_f64() {
2440 format!("{}d", n)
2441 } else {
2442 format!("{}L", n)
2443 }
2444 } else {
2445 if n.is_f64() {
2447 format!("Optional.of({}d)", n)
2448 } else {
2449 format!("Optional.of({}L)", n)
2450 }
2451 }
2452 }
2453 serde_json::Value::Array(arr) => {
2454 let items: Vec<String> = arr.iter().map(|v| json_to_java_typed(v, None)).collect();
2455 format!("java.util.List.of({})", items.join(", "))
2456 }
2457 serde_json::Value::Object(nested) => {
2458 let nested_type = nested_types
2460 .get(key.as_str())
2461 .cloned()
2462 .unwrap_or_else(|| format!("{}Options", key.to_upper_camel_case()));
2463 let inner = java_builder_expression(
2464 nested,
2465 &nested_type,
2466 enum_fields,
2467 nested_types,
2468 nested_types_optional,
2469 &[],
2470 );
2471 let is_primitive_builder = matches!(type_name, "SecurityLimits" | "SecurityLimitsBuilder");
2475 if is_primitive_builder || !nested_types_optional {
2476 inner
2477 } else {
2478 format!("Optional.of({inner})")
2479 }
2480 }
2481 };
2482 expr.push_str(&format!(".{}({})", method_name, java_val));
2483 }
2484 expr.push_str(".build()");
2485 expr
2486}
2487
2488#[allow(dead_code)]
2495fn collect_enum_and_nested_types(
2496 obj: &serde_json::Map<String, serde_json::Value>,
2497 enum_fields: &std::collections::HashMap<String, String>,
2498 types_out: &mut std::collections::BTreeSet<String>,
2499) {
2500 for (key, val) in obj {
2501 let camel_key = key.to_lower_camel_case();
2503 if let Some(enum_type) = enum_fields.get(&camel_key) {
2504 types_out.insert(enum_type.clone());
2506 } else if camel_key == "preset" {
2507 types_out.insert("PreprocessingPreset".to_string());
2509 }
2510 if let Some(nested) = val.as_object() {
2512 collect_enum_and_nested_types(nested, enum_fields, types_out);
2513 }
2514 }
2515}
2516
2517fn collect_nested_type_names(
2518 obj: &serde_json::Map<String, serde_json::Value>,
2519 nested_types: &std::collections::HashMap<String, String>,
2520 types_out: &mut std::collections::BTreeSet<String>,
2521) {
2522 for (key, val) in obj {
2523 if let Some(type_name) = nested_types.get(key.as_str()) {
2524 types_out.insert(type_name.clone());
2525 }
2526 if let Some(nested) = val.as_object() {
2527 collect_nested_type_names(nested, nested_types, types_out);
2528 }
2529 }
2530}
2531
2532fn build_java_visitor(
2538 setup_lines: &mut Vec<String>,
2539 visitor_spec: &crate::fixture::VisitorSpec,
2540 class_name: &str,
2541) -> String {
2542 setup_lines.push("class _TestVisitor implements Visitor {".to_string());
2543 for (method_name, action) in &visitor_spec.callbacks {
2544 emit_java_visitor_method(setup_lines, method_name, action, class_name);
2545 }
2546 setup_lines.push("}".to_string());
2547 setup_lines.push("var visitor = new _TestVisitor();".to_string());
2548 "visitor".to_string()
2549}
2550
2551fn emit_java_visitor_method(
2553 setup_lines: &mut Vec<String>,
2554 method_name: &str,
2555 action: &CallbackAction,
2556 _class_name: &str,
2557) {
2558 let camel_method = method_to_camel(method_name);
2559 let params = match method_name {
2560 "visit_link" => "NodeContext ctx, String href, String text, String title",
2561 "visit_image" => "NodeContext ctx, String src, String alt, String title",
2562 "visit_heading" => "NodeContext ctx, int level, String text, String id",
2563 "visit_code_block" => "NodeContext ctx, String lang, String code",
2564 "visit_code_inline"
2565 | "visit_strong"
2566 | "visit_emphasis"
2567 | "visit_strikethrough"
2568 | "visit_underline"
2569 | "visit_subscript"
2570 | "visit_superscript"
2571 | "visit_mark"
2572 | "visit_button"
2573 | "visit_summary"
2574 | "visit_figcaption"
2575 | "visit_definition_term"
2576 | "visit_definition_description" => "NodeContext ctx, String text",
2577 "visit_text" => "NodeContext ctx, String text",
2578 "visit_list_item" => "NodeContext ctx, boolean ordered, String marker, String text",
2579 "visit_blockquote" => "NodeContext ctx, String content, long depth",
2580 "visit_table_row" => "NodeContext ctx, java.util.List<String> cells, boolean isHeader",
2581 "visit_custom_element" => "NodeContext ctx, String tagName, String html",
2582 "visit_form" => "NodeContext ctx, String actionUrl, String method",
2583 "visit_input" => "NodeContext ctx, String inputType, String name, String value",
2584 "visit_audio" | "visit_video" | "visit_iframe" => "NodeContext ctx, String src",
2585 "visit_details" => "NodeContext ctx, boolean isOpen",
2586 "visit_element_end" | "visit_table_end" | "visit_definition_list_end" | "visit_figure_end" => {
2587 "NodeContext ctx, String output"
2588 }
2589 "visit_list_start" => "NodeContext ctx, boolean ordered",
2590 "visit_list_end" => "NodeContext ctx, boolean ordered, String output",
2591 _ => "NodeContext ctx",
2592 };
2593
2594 let (action_type, action_value, format_args) = match action {
2596 CallbackAction::Skip => ("skip", String::new(), Vec::new()),
2597 CallbackAction::Continue => ("continue", String::new(), Vec::new()),
2598 CallbackAction::PreserveHtml => ("preserve_html", String::new(), Vec::new()),
2599 CallbackAction::Custom { output } => ("custom_literal", escape_java(output), Vec::new()),
2600 CallbackAction::CustomTemplate { template, .. } => {
2601 let mut format_str = String::with_capacity(template.len());
2603 let mut format_args: Vec<String> = Vec::new();
2604 let mut chars = template.chars().peekable();
2605 while let Some(ch) = chars.next() {
2606 if ch == '{' {
2607 let mut name = String::new();
2609 let mut closed = false;
2610 for inner in chars.by_ref() {
2611 if inner == '}' {
2612 closed = true;
2613 break;
2614 }
2615 name.push(inner);
2616 }
2617 if closed && !name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_') {
2618 let camel_name = name.as_str().to_lower_camel_case();
2619 format_args.push(camel_name);
2620 format_str.push_str("%s");
2621 } else {
2622 format_str.push('{');
2624 format_str.push_str(&name);
2625 if closed {
2626 format_str.push('}');
2627 }
2628 }
2629 } else {
2630 format_str.push(ch);
2631 }
2632 }
2633 let escaped = escape_java(&format_str);
2634 if format_args.is_empty() {
2635 ("custom_literal", escaped, Vec::new())
2636 } else {
2637 ("custom_formatted", escaped, format_args)
2638 }
2639 }
2640 };
2641
2642 let params = params.to_string();
2643
2644 let rendered = crate::template_env::render(
2645 "java/visitor_method.jinja",
2646 minijinja::context! {
2647 camel_method,
2648 params,
2649 action_type,
2650 action_value,
2651 format_args => format_args,
2652 },
2653 );
2654 setup_lines.push(rendered);
2655}
2656
2657fn method_to_camel(snake: &str) -> String {
2659 snake.to_lower_camel_case()
2660}
2661
2662#[cfg(test)]
2663mod tests {
2664 use crate::config::{CallConfig, E2eConfig, SelectWhen};
2665 use crate::fixture::Fixture;
2666 use std::collections::HashMap;
2667
2668 fn make_fixture_with_input(id: &str, input: serde_json::Value) -> Fixture {
2669 Fixture {
2670 id: id.to_string(),
2671 category: None,
2672 description: "test fixture".to_string(),
2673 tags: vec![],
2674 skip: None,
2675 env: None,
2676 call: None,
2677 input,
2678 mock_response: None,
2679 source: String::new(),
2680 http: None,
2681 assertions: vec![],
2682 visitor: None,
2683 }
2684 }
2685
2686 #[test]
2689 fn test_java_select_when_routes_to_batch_scrape() {
2690 let mut calls = HashMap::new();
2691 calls.insert(
2692 "batch_scrape".to_string(),
2693 CallConfig {
2694 function: "batchScrape".to_string(),
2695 module: "com.example.kreuzcrawl".to_string(),
2696 select_when: Some(SelectWhen {
2697 input_has: Some("batch_urls".to_string()),
2698 ..Default::default()
2699 }),
2700 ..CallConfig::default()
2701 },
2702 );
2703
2704 let e2e_config = E2eConfig {
2705 call: CallConfig {
2706 function: "scrape".to_string(),
2707 module: "com.example.kreuzcrawl".to_string(),
2708 ..CallConfig::default()
2709 },
2710 calls,
2711 ..E2eConfig::default()
2712 };
2713
2714 let fixture = make_fixture_with_input("batch_empty_urls", serde_json::json!({ "batch_urls": [] }));
2716
2717 let resolved_call = e2e_config.resolve_call_for_fixture(
2718 fixture.call.as_deref(),
2719 &fixture.id,
2720 &fixture.resolved_category(),
2721 &fixture.tags,
2722 &fixture.input,
2723 );
2724 assert_eq!(resolved_call.function, "batchScrape");
2725
2726 let fixture_no_batch =
2728 make_fixture_with_input("simple_scrape", serde_json::json!({ "url": "https://example.com" }));
2729 let resolved_default = e2e_config.resolve_call_for_fixture(
2730 fixture_no_batch.call.as_deref(),
2731 &fixture_no_batch.id,
2732 &fixture_no_batch.resolved_category(),
2733 &fixture_no_batch.tags,
2734 &fixture_no_batch.input,
2735 );
2736 assert_eq!(resolved_default.function, "scrape");
2737 }
2738}