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 filtered_args: Vec<_> = if adapter.is_some_and(|a| a.owner_type.is_some()) {
1284 args.iter().filter(|arg| arg.arg_type != "handle").cloned().collect()
1285 } else {
1286 args.to_vec()
1287 };
1288
1289 let (mut setup_lines, args_str) = build_args_and_setup(
1290 &fixture.input,
1291 &filtered_args,
1292 class_name,
1293 effective_options_type,
1294 fixture,
1295 adapter_request_type.as_deref(),
1296 );
1297
1298 let extra_args_slice: &[String] = call_overrides.map_or(&[], |o| o.extra_args.as_slice());
1303
1304 let mut visitor_var = String::new();
1306 let mut has_visitor_fixture = false;
1307 if let Some(visitor_spec) = &fixture.visitor {
1308 visitor_var = build_java_visitor(&mut setup_lines, visitor_spec, class_name);
1309 has_visitor_fixture = true;
1310 }
1311
1312 let mut final_args = if has_visitor_fixture {
1314 if args_str.is_empty() {
1315 format!("new ConversionOptions().withVisitor({})", visitor_var)
1316 } else if args_str.contains("new ConversionOptions")
1317 || args_str.contains("ConversionOptionsBuilder")
1318 || args_str.contains(".builder()")
1319 {
1320 if args_str.contains(".build()") {
1323 let idx = args_str.rfind(".build()").unwrap();
1324 format!("{}.withVisitor({}){}", &args_str[..idx], visitor_var, &args_str[idx..])
1325 } else {
1326 format!("{}.withVisitor({})", args_str, visitor_var)
1327 }
1328 } else if args_str.ends_with(", null") {
1329 let base = &args_str[..args_str.len() - 6];
1330 format!("{}, new ConversionOptions().withVisitor({})", base, visitor_var)
1331 } else {
1332 format!("{}, new ConversionOptions().withVisitor({})", args_str, visitor_var)
1333 }
1334 } else {
1335 args_str
1336 };
1337
1338 if !extra_args_slice.is_empty() {
1339 let extra_str = extra_args_slice.join(", ");
1340 final_args = if final_args.is_empty() {
1341 extra_str
1342 } else {
1343 format!("{final_args}, {extra_str}")
1344 };
1345 }
1346
1347 let mut assertions_body = String::new();
1349
1350 let needs_source_var = fixture
1352 .assertions
1353 .iter()
1354 .any(|a| a.assertion_type == "method_result" && a.method.as_deref() == Some("run_query"));
1355 if needs_source_var {
1356 if let Some(source_arg) = args.iter().find(|a| a.field == "source_code") {
1357 let field = source_arg.field.strip_prefix("input.").unwrap_or(&source_arg.field);
1358 if let Some(val) = fixture.input.get(field) {
1359 let java_val = json_to_java(val);
1360 assertions_body.push_str(&format!(" var source = {}.getBytes();\n", java_val));
1361 }
1362 }
1363 }
1364
1365 let assert_enum_types: std::collections::HashMap<String, String> = if let Some(co) = call_overrides {
1372 co.assert_enum_fields.clone()
1373 } else {
1374 std::collections::HashMap::new()
1375 };
1376
1377 let mut effective_enum_fields: std::collections::HashSet<String> = enum_fields.clone();
1379 if let Some(co) = call_overrides {
1380 for k in co.enum_fields.keys() {
1381 effective_enum_fields.insert(k.clone());
1382 }
1383 }
1384
1385 let is_streaming = crate::codegen::streaming_assertions::resolve_is_streaming(fixture, call_config.streaming);
1390
1391 for assertion in &fixture.assertions {
1392 render_assertion(
1393 &mut assertions_body,
1394 assertion,
1395 result_var,
1396 class_name,
1397 field_resolver,
1398 effective_result_is_simple,
1399 effective_result_is_bytes,
1400 effective_result_is_option,
1401 is_streaming,
1402 &effective_enum_fields,
1403 &assert_enum_types,
1404 );
1405 }
1406
1407 let throws_clause = " throws Exception";
1408
1409 let (client_setup_lines, call_target) = if let Some(factory) = client_factory.as_deref() {
1412 let factory_name = factory.to_lower_camel_case();
1413 let fixture_id = &fixture.id;
1414 let mut setup: Vec<String> = Vec::new();
1415 let has_mock = fixture.mock_response.is_some() || fixture.http.is_some();
1416 let api_key_var = fixture.env.as_ref().and_then(|e| e.api_key_var.as_deref());
1417 if let Some(var) = api_key_var.filter(|_| has_mock) {
1418 setup.push(format!("String apiKey = System.getenv(\"{var}\");"));
1419 setup.push(format!(
1420 "String baseUrl = (apiKey != null && !apiKey.isEmpty()) ? null : System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\";"
1421 ));
1422 setup.push(format!(
1423 "System.out.println(\"{fixture_id}: \" + (baseUrl == null ? \"using real API ({var} is set)\" : \"using mock server ({var} not set)\"));"
1424 ));
1425 setup.push(format!(
1426 "var client = {class_name}.{factory_name}(baseUrl == null ? apiKey : \"test-key\", baseUrl, null, null, null);"
1427 ));
1428 } else if has_mock {
1429 if fixture.has_host_root_route() {
1430 setup.push(format!(
1431 "String mockUrl = System.getProperty(\"mockServer.{fixture_id}\", System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\");"
1432 ));
1433 } else {
1434 setup.push(format!(
1435 "String mockUrl = System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\";"
1436 ));
1437 }
1438 setup.push(format!(
1439 "var client = {class_name}.{factory_name}(\"test-key\", mockUrl, null, null, null);"
1440 ));
1441 } else if let Some(api_key_var) = api_key_var {
1442 setup.push(format!("String apiKey = System.getenv(\"{api_key_var}\");"));
1443 setup.push(format!(
1444 "org.junit.jupiter.api.Assumptions.assumeTrue(apiKey != null && !apiKey.isEmpty(), \"{api_key_var} not set\");"
1445 ));
1446 setup.push(format!("var client = {class_name}.{factory_name}(apiKey);"));
1447 } else {
1448 setup.push(format!("var client = {class_name}.{factory_name}(\"test-key\");"));
1449 }
1450 (setup, "client".to_string())
1451 } else {
1452 (Vec::new(), class_name.to_string())
1453 };
1454
1455 let combined_setup: Vec<String> = client_setup_lines.into_iter().chain(setup_lines).collect();
1457
1458 let call_expr = format!("{call_target}.{function_name}({final_args})");
1459
1460 let collect_snippet = if is_streaming && !expects_error {
1462 let item_type_for_streaming = adapter
1464 .and_then(|a| a.item_type.as_deref())
1465 .map(|it| it.rsplit("::").next().unwrap_or(it));
1466 crate::codegen::streaming_assertions::StreamingFieldResolver::collect_snippet_typed(
1467 "java",
1468 result_var,
1469 "chunks",
1470 item_type_for_streaming,
1471 )
1472 .unwrap_or_default()
1473 } else {
1474 String::new()
1475 };
1476
1477 let rendered = crate::template_env::render(
1478 "java/test_method.jinja",
1479 minijinja::context! {
1480 method_name => method_name,
1481 description => description,
1482 builder_expressions => builder_expressions,
1483 setup_lines => combined_setup,
1484 throws_clause => throws_clause,
1485 expects_error => expects_error,
1486 call_expr => call_expr,
1487 result_var => result_var,
1488 returns_void => call_config.returns_void,
1489 collect_snippet => collect_snippet,
1490 assertions_body => assertions_body,
1491 },
1492 );
1493 out.push_str(&rendered);
1494}
1495
1496fn build_args_and_setup(
1500 input: &serde_json::Value,
1501 args: &[crate::config::ArgMapping],
1502 class_name: &str,
1503 options_type: Option<&str>,
1504 fixture: &crate::fixture::Fixture,
1505 adapter_request_type: Option<&str>,
1506) -> (Vec<String>, String) {
1507 let fixture_id = &fixture.id;
1508 if args.is_empty() {
1509 return (Vec::new(), String::new());
1510 }
1511
1512 let mut setup_lines: Vec<String> = Vec::new();
1513 let mut parts: Vec<String> = Vec::new();
1514
1515 for arg in args {
1516 if arg.arg_type == "mock_url" {
1517 if fixture.has_host_root_route() {
1518 setup_lines.push(format!(
1519 "String {} = System.getProperty(\"mockServer.{fixture_id}\", System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\");",
1520 arg.name,
1521 ));
1522 } else {
1523 setup_lines.push(format!(
1524 "String {} = System.getProperty(\"mockServerUrl\", System.getenv(\"MOCK_SERVER_URL\")) + \"/fixtures/{fixture_id}\";",
1525 arg.name,
1526 ));
1527 }
1528 if let Some(req_type) = adapter_request_type {
1529 let req_var = format!("{}Req", arg.name);
1530 setup_lines.push(format!("var {req_var} = new {req_type}({});", arg.name));
1531 parts.push(req_var);
1532 } else {
1533 parts.push(arg.name.clone());
1534 }
1535 continue;
1536 }
1537
1538 if arg.arg_type == "mock_url_list" {
1539 let env_key = format!("MOCK_SERVER_{}", fixture_id.to_uppercase());
1545 let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1546 let val = input.get(field).unwrap_or(&serde_json::Value::Null);
1547 let paths: Vec<String> = if let Some(arr) = val.as_array() {
1548 arr.iter()
1549 .filter_map(|v| v.as_str().map(|s| format!("\"{}\"", escape_java(s))))
1550 .collect()
1551 } else {
1552 Vec::new()
1553 };
1554 let paths_literal = paths.join(", ");
1555 let name = &arg.name;
1556 setup_lines.push(format!(
1557 "String {name}Base = System.getenv().getOrDefault(\"{env_key}\", System.getenv(\"MOCK_SERVER_URL\") + \"/fixtures/{fixture_id}\");"
1558 ));
1559 setup_lines.push(format!(
1560 "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());"
1561 ));
1562 parts.push(name.clone());
1563 continue;
1564 }
1565
1566 if arg.arg_type == "handle" {
1567 let constructor_name = format!("create{}", arg.name.to_upper_camel_case());
1569 let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
1570 let config_value = input.get(field).unwrap_or(&serde_json::Value::Null);
1571 if config_value.is_null()
1572 || config_value.is_object() && config_value.as_object().is_some_and(|o| o.is_empty())
1573 {
1574 setup_lines.push(format!("var {} = {class_name}.{constructor_name}(null);", arg.name,));
1575 } else {
1576 let json_str = serde_json::to_string(config_value).unwrap_or_default();
1577 let name = &arg.name;
1578 setup_lines.push(format!(
1579 "var {name}Config = MAPPER.readValue(\"{}\", CrawlConfig.class);",
1580 escape_java(&json_str),
1581 ));
1582 setup_lines.push(format!(
1583 "var {} = {class_name}.{constructor_name}({name}Config);",
1584 arg.name,
1585 name = name,
1586 ));
1587 }
1588 parts.push(arg.name.clone());
1589 continue;
1590 }
1591
1592 let resolved = super::resolve_field(input, &arg.field);
1593 let val = if resolved.is_null() { None } else { Some(resolved) };
1594 match val {
1595 None | Some(serde_json::Value::Null) if arg.optional => {
1596 if arg.arg_type == "json_object" {
1600 if let Some(opts_type) = options_type {
1601 parts.push(format!("{opts_type}.builder().build()"));
1602 } else {
1603 parts.push("null".to_string());
1604 }
1605 } else {
1606 parts.push("null".to_string());
1607 }
1608 }
1609 None | Some(serde_json::Value::Null) => {
1610 let default_val = match arg.arg_type.as_str() {
1612 "string" | "file_path" => "\"\"".to_string(),
1613 "int" | "integer" => "0".to_string(),
1614 "float" | "number" => "0.0d".to_string(),
1615 "bool" | "boolean" => "false".to_string(),
1616 _ => "null".to_string(),
1617 };
1618 parts.push(default_val);
1619 }
1620 Some(v) => {
1621 if arg.arg_type == "json_object" {
1622 if v.is_array() {
1625 if let Some(elem_type) = &arg.element_type {
1626 if elem_type == "BatchBytesItem" || elem_type == "BatchFileItem" {
1627 parts.push(emit_java_batch_item_array(v, elem_type));
1628 continue;
1629 }
1630 if !is_numeric_type_hint(elem_type) {
1632 parts.push(emit_java_object_array(v, elem_type));
1633 continue;
1634 }
1635 }
1636 let elem_type = arg.element_type.as_deref();
1638 parts.push(json_to_java_typed(v, elem_type));
1639 continue;
1640 }
1641 if options_type.is_some() {
1643 parts.push(arg.name.clone());
1644 continue;
1645 }
1646 parts.push(json_to_java(v));
1647 continue;
1648 }
1649 if arg.arg_type == "bytes" {
1653 let val = json_to_java(v);
1654 parts.push(format!(
1655 "java.nio.file.Files.readAllBytes(java.nio.file.Path.of({val}))"
1656 ));
1657 continue;
1658 }
1659 if arg.arg_type == "file_path" {
1661 let val = json_to_java(v);
1662 parts.push(format!("java.nio.file.Path.of({val})"));
1663 continue;
1664 }
1665 parts.push(json_to_java(v));
1666 }
1667 }
1668 }
1669
1670 (setup_lines, parts.join(", "))
1671}
1672
1673#[allow(clippy::too_many_arguments)]
1674fn render_assertion(
1675 out: &mut String,
1676 assertion: &Assertion,
1677 result_var: &str,
1678 class_name: &str,
1679 field_resolver: &FieldResolver,
1680 result_is_simple: bool,
1681 result_is_bytes: bool,
1682 result_is_option: bool,
1683 is_streaming: bool,
1684 enum_fields: &std::collections::HashSet<String>,
1685 assert_enum_types: &std::collections::HashMap<String, String>,
1686) {
1687 let bare_field = assertion.field.as_deref().is_none_or(str::is_empty);
1692 if result_is_option && bare_field {
1693 match assertion.assertion_type.as_str() {
1694 "is_empty" => {
1695 out.push_str(&format!(
1696 " assertNull({result_var}, \"expected empty value\");\n"
1697 ));
1698 return;
1699 }
1700 "not_empty" => {
1701 out.push_str(&format!(
1702 " assertNotNull({result_var}, \"expected non-empty value\");\n"
1703 ));
1704 return;
1705 }
1706 _ => {}
1707 }
1708 }
1709
1710 if result_is_bytes {
1715 match assertion.assertion_type.as_str() {
1716 "not_empty" => {
1717 out.push_str(&format!(
1718 " assertTrue({result_var}.length > 0, \"expected non-empty value\");\n"
1719 ));
1720 return;
1721 }
1722 "is_empty" => {
1723 out.push_str(&format!(
1724 " assertEquals(0, {result_var}.length, \"expected empty value\");\n"
1725 ));
1726 return;
1727 }
1728 "count_equals" | "length_equals" => {
1729 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1730 out.push_str(&format!(" assertEquals({n}, {result_var}.length);\n"));
1731 }
1732 return;
1733 }
1734 "count_min" | "length_min" => {
1735 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1736 out.push_str(&format!(
1737 " assertTrue({result_var}.length >= {n}, \"expected length >= {n}\");\n"
1738 ));
1739 }
1740 return;
1741 }
1742 "not_error" => {
1743 out.push_str(&format!(
1746 " assertNotNull({result_var}, \"expected non-null byte[] response\");\n"
1747 ));
1748 return;
1749 }
1750 _ => {
1751 out.push_str(&format!(
1752 " // skipped: assertion type '{}' not supported on byte[] result\n",
1753 assertion.assertion_type
1754 ));
1755 return;
1756 }
1757 }
1758 }
1759
1760 if let Some(f) = &assertion.field {
1762 match f.as_str() {
1763 "chunks_have_content" => {
1765 let pred = format!(
1766 "java.util.Optional.ofNullable({result_var}.chunks()).orElse(java.util.List.of()).stream().allMatch(c -> c.content() != null && !c.content().isBlank())"
1767 );
1768 out.push_str(&crate::template_env::render(
1769 "java/synthetic_assertion.jinja",
1770 minijinja::context! {
1771 assertion_kind => "chunks_content",
1772 assertion_type => assertion.assertion_type.as_str(),
1773 pred => pred,
1774 field_name => f,
1775 },
1776 ));
1777 return;
1778 }
1779 "chunks_have_heading_context" => {
1780 let pred = format!(
1781 "java.util.Optional.ofNullable({result_var}.chunks()).orElse(java.util.List.of()).stream().allMatch(c -> c.metadata().headingContext() != null)"
1782 );
1783 out.push_str(&crate::template_env::render(
1784 "java/synthetic_assertion.jinja",
1785 minijinja::context! {
1786 assertion_kind => "chunks_heading_context",
1787 assertion_type => assertion.assertion_type.as_str(),
1788 pred => pred,
1789 field_name => f,
1790 },
1791 ));
1792 return;
1793 }
1794 "chunks_have_embeddings" => {
1795 let pred = format!(
1796 "java.util.Optional.ofNullable({result_var}.chunks()).orElse(java.util.List.of()).stream().allMatch(c -> c.embedding() != null && !c.embedding().isEmpty())"
1797 );
1798 out.push_str(&crate::template_env::render(
1799 "java/synthetic_assertion.jinja",
1800 minijinja::context! {
1801 assertion_kind => "chunks_embeddings",
1802 assertion_type => assertion.assertion_type.as_str(),
1803 pred => pred,
1804 field_name => f,
1805 },
1806 ));
1807 return;
1808 }
1809 "first_chunk_starts_with_heading" => {
1810 let pred = format!(
1811 "java.util.Optional.ofNullable({result_var}.chunks()).orElse(java.util.List.of()).stream().findFirst().map(c -> c.metadata().headingContext() != null).orElse(false)"
1812 );
1813 out.push_str(&crate::template_env::render(
1814 "java/synthetic_assertion.jinja",
1815 minijinja::context! {
1816 assertion_kind => "first_chunk_heading",
1817 assertion_type => assertion.assertion_type.as_str(),
1818 pred => pred,
1819 field_name => f,
1820 },
1821 ));
1822 return;
1823 }
1824 "embedding_dimensions" => {
1828 let embed_list = if result_is_simple {
1830 result_var.to_string()
1831 } else {
1832 format!("{result_var}.embeddings()")
1833 };
1834 let expr = format!("({embed_list}.isEmpty() ? 0 : {embed_list}.get(0).size())");
1835 let java_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
1836 out.push_str(&crate::template_env::render(
1837 "java/synthetic_assertion.jinja",
1838 minijinja::context! {
1839 assertion_kind => "embedding_dimensions",
1840 assertion_type => assertion.assertion_type.as_str(),
1841 expr => expr,
1842 java_val => java_val,
1843 field_name => f,
1844 },
1845 ));
1846 return;
1847 }
1848 "embeddings_valid" | "embeddings_finite" | "embeddings_non_zero" | "embeddings_normalized" => {
1849 let embed_list = if result_is_simple {
1851 result_var.to_string()
1852 } else {
1853 format!("{result_var}.embeddings()")
1854 };
1855 let pred = match f.as_str() {
1856 "embeddings_valid" => {
1857 format!("{embed_list}.stream().allMatch(e -> e != null && !e.isEmpty())")
1858 }
1859 "embeddings_finite" => {
1860 format!("{embed_list}.stream().flatMap(java.util.Collection::stream).allMatch(Float::isFinite)")
1861 }
1862 "embeddings_non_zero" => {
1863 format!("{embed_list}.stream().allMatch(e -> e.stream().anyMatch(v -> v != 0.0f))")
1864 }
1865 "embeddings_normalized" => format!(
1866 "{embed_list}.stream().allMatch(e -> {{ double n = e.stream().mapToDouble(v -> v * v).sum(); return Math.abs(n - 1.0) < 1e-3; }})"
1867 ),
1868 _ => unreachable!(),
1869 };
1870 let assertion_kind = format!("embeddings_{}", f.strip_prefix("embeddings_").unwrap_or(f));
1871 out.push_str(&crate::template_env::render(
1872 "java/synthetic_assertion.jinja",
1873 minijinja::context! {
1874 assertion_kind => assertion_kind,
1875 assertion_type => assertion.assertion_type.as_str(),
1876 pred => pred,
1877 field_name => f,
1878 },
1879 ));
1880 return;
1881 }
1882 "keywords" | "keywords_count" => {
1884 out.push_str(&crate::template_env::render(
1885 "java/synthetic_assertion.jinja",
1886 minijinja::context! {
1887 assertion_kind => "keywords",
1888 field_name => f,
1889 },
1890 ));
1891 return;
1892 }
1893 "metadata" => {
1896 match assertion.assertion_type.as_str() {
1897 "not_empty" | "is_empty" => {
1898 out.push_str(&crate::template_env::render(
1899 "java/synthetic_assertion.jinja",
1900 minijinja::context! {
1901 assertion_kind => "metadata",
1902 assertion_type => assertion.assertion_type.as_str(),
1903 result_var => result_var,
1904 },
1905 ));
1906 return;
1907 }
1908 _ => {} }
1910 }
1911 _ => {}
1912 }
1913 }
1914
1915 if let Some(f) = &assertion.field {
1921 if is_streaming && !f.is_empty() && crate::codegen::streaming_assertions::is_streaming_virtual_field(f) {
1922 if let Some(expr) =
1923 crate::codegen::streaming_assertions::StreamingFieldResolver::accessor(f, "java", "chunks")
1924 {
1925 let line = match assertion.assertion_type.as_str() {
1926 "count_min" => {
1927 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1928 format!(" assertTrue({expr}.size() >= {n}, \"expected >= {n} chunks\");\n")
1929 } else {
1930 String::new()
1931 }
1932 }
1933 "count_equals" => {
1934 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1935 format!(" assertEquals({n}, {expr}.size());\n")
1936 } else {
1937 String::new()
1938 }
1939 }
1940 "equals" => {
1941 if let Some(serde_json::Value::String(s)) = &assertion.value {
1942 let escaped = crate::escape::escape_java(s);
1943 format!(" assertEquals(\"{escaped}\", {expr});\n")
1944 } else if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1945 format!(" assertEquals({n}, {expr});\n")
1946 } else {
1947 String::new()
1948 }
1949 }
1950 "not_empty" => format!(" assertFalse({expr}.isEmpty(), \"expected non-empty\");\n"),
1951 "is_empty" => format!(" assertTrue({expr}.isEmpty(), \"expected empty\");\n"),
1952 "is_true" => format!(" assertTrue({expr}, \"expected true\");\n"),
1953 "is_false" => format!(" assertFalse({expr}, \"expected false\");\n"),
1954 "greater_than" => {
1955 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1956 format!(" assertTrue({expr} > {n}, \"expected > {n}\");\n")
1957 } else {
1958 String::new()
1959 }
1960 }
1961 "greater_than_or_equal" => {
1962 if let Some(n) = assertion.value.as_ref().and_then(|v| v.as_u64()) {
1963 format!(" assertTrue({expr} >= {n}, \"expected >= {n}\");\n")
1964 } else {
1965 String::new()
1966 }
1967 }
1968 "contains" => {
1969 if let Some(serde_json::Value::String(s)) = &assertion.value {
1970 let escaped = crate::escape::escape_java(s);
1971 format!(
1972 " assertTrue({expr}.contains(\"{escaped}\"), \"expected to contain: {escaped}\");\n"
1973 )
1974 } else {
1975 String::new()
1976 }
1977 }
1978 _ => format!(
1979 " // streaming field '{f}': assertion type '{}' not rendered\n",
1980 assertion.assertion_type
1981 ),
1982 };
1983 if !line.is_empty() {
1984 out.push_str(&line);
1985 }
1986 }
1987 return;
1988 }
1989 }
1990
1991 if let Some(f) = &assertion.field {
1993 if !f.is_empty() && !field_resolver.is_valid_for_result(f) {
1994 out.push_str(&crate::template_env::render(
1995 "java/synthetic_assertion.jinja",
1996 minijinja::context! {
1997 assertion_kind => "skipped",
1998 field_name => f,
1999 },
2000 ));
2001 return;
2002 }
2003 }
2004
2005 let sealed_display_type: Option<String> = assertion.field.as_deref().and_then(|f| {
2010 let resolved = field_resolver.resolve(f);
2011 assert_enum_types
2012 .get(f)
2013 .or_else(|| assert_enum_types.get(resolved))
2014 .cloned()
2015 });
2016 let is_sealed_display_field = sealed_display_type.is_some();
2017
2018 let field_is_enum = assertion.field.as_deref().is_some_and(|f| {
2025 let resolved = field_resolver.resolve(f);
2026 let in_enum_fields = enum_fields.get(f).is_some() || enum_fields.get(resolved).is_some();
2027 in_enum_fields && !is_sealed_display_field
2028 });
2029
2030 let field_is_array = assertion
2034 .field
2035 .as_deref()
2036 .is_some_and(|f| field_resolver.is_array(field_resolver.resolve(f)));
2037
2038 let field_expr = if result_is_simple {
2039 result_var.to_string()
2040 } else {
2041 match &assertion.field {
2042 Some(f) if !f.is_empty() => {
2043 let accessor = field_resolver.accessor(f, "java", result_var);
2044 let resolved = field_resolver.resolve(f);
2045 if field_resolver.is_optional(resolved) && !field_resolver.has_map_access(f) {
2052 let optional_expr = format!("java.util.Optional.ofNullable({accessor})");
2055 if field_is_enum {
2059 match assertion.assertion_type.as_str() {
2060 "not_empty" | "is_empty" => optional_expr,
2061 _ => {
2062 format!("{optional_expr}.map(v -> v.getValue()).orElse(\"\")")
2066 }
2067 }
2068 } else {
2069 match assertion.assertion_type.as_str() {
2070 "not_empty" | "is_empty" => optional_expr,
2073 "count_min" | "count_equals" => {
2075 format!("{optional_expr}.orElse(java.util.List.of())")
2076 }
2077 "greater_than" | "less_than" | "greater_than_or_equal" | "less_than_or_equal" => {
2084 if field_resolver.is_array(resolved) {
2085 format!("{optional_expr}.orElse(java.util.List.of())")
2086 } else {
2087 format!("{optional_expr}.map(Number::longValue).orElse(0L)")
2088 }
2089 }
2090 "equals" => {
2096 if is_sealed_display_field {
2097 optional_expr
2099 } else if let Some(expected) = &assertion.value {
2100 if expected.is_number() {
2101 format!("{optional_expr}.map(Number::longValue).orElse(0L)")
2102 } else {
2103 format!("{optional_expr}.orElse(\"\")")
2104 }
2105 } else {
2106 format!("{optional_expr}.orElse(\"\")")
2107 }
2108 }
2109 _ if field_resolver.is_array(resolved) => {
2110 format!("{optional_expr}.orElse(java.util.List.of())")
2111 }
2112 _ => format!("{optional_expr}.orElse(\"\")"),
2113 }
2114 }
2115 } else {
2116 accessor
2117 }
2118 }
2119 _ => result_var.to_string(),
2120 }
2121 };
2122
2123 let string_expr = if field_is_enum && !field_expr.contains(".map(v -> v.getValue())") {
2131 format!("{field_expr}.getValue()")
2132 } else if let Some(ref stype) = sealed_display_type {
2133 let inner_expr = if field_expr.contains("Optional.ofNullable") {
2137 format!("{field_expr}.orElse(null)")
2138 } else {
2139 field_expr.clone()
2140 };
2141 format!("{stype}Display.toDisplayString({inner_expr})")
2142 } else {
2143 field_expr.clone()
2144 };
2145
2146 let assertion_type = assertion.assertion_type.as_str();
2148 let java_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
2149 let is_string_val = assertion.value.as_ref().is_some_and(|v| v.is_string());
2150 let is_numeric_val = assertion.value.as_ref().is_some_and(|v| v.is_number());
2151
2152 let values_java: Vec<String> = assertion
2156 .values
2157 .as_ref()
2158 .map(|values| values.iter().map(json_to_java).collect::<Vec<_>>())
2159 .or_else(|| assertion.value.as_ref().map(|v| vec![json_to_java(v)]))
2160 .unwrap_or_default();
2161
2162 let contains_any_expr = if !values_java.is_empty() {
2163 values_java
2164 .iter()
2165 .map(|v| format!("{string_expr}.contains({v})"))
2166 .collect::<Vec<_>>()
2167 .join(" || ")
2168 } else {
2169 String::new()
2170 };
2171
2172 let length_expr = if result_is_bytes {
2173 format!("{field_expr}.length")
2174 } else {
2175 format!("{field_expr}.length()")
2176 };
2177
2178 let n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);
2179
2180 let call_expr = if let Some(method_name) = &assertion.method {
2181 build_java_method_call(result_var, method_name, assertion.args.as_ref(), class_name)
2182 } else {
2183 String::new()
2184 };
2185
2186 let check = assertion.check.as_deref().unwrap_or("is_true");
2187
2188 let java_check_val = assertion.value.as_ref().map(json_to_java).unwrap_or_default();
2189
2190 let check_n = assertion.value.as_ref().and_then(|v| v.as_u64()).unwrap_or(0);
2191
2192 let is_bool_val = assertion.value.as_ref().is_some_and(|v| v.is_boolean());
2193 let bool_is_true = assertion.value.as_ref().is_some_and(|v| v.as_bool() == Some(true));
2194
2195 let method_returns_collection = assertion
2196 .method
2197 .as_ref()
2198 .is_some_and(|m| matches!(m.as_str(), "find_nodes_by_type" | "findNodesByType"));
2199
2200 let rendered = crate::template_env::render(
2201 "java/assertion.jinja",
2202 minijinja::context! {
2203 assertion_type,
2204 java_val,
2205 string_expr,
2206 field_expr,
2207 field_is_enum,
2208 field_is_array,
2209 is_string_val,
2210 is_numeric_val,
2211 values_java => values_java,
2212 contains_any_expr,
2213 length_expr,
2214 n,
2215 call_expr,
2216 check,
2217 java_check_val,
2218 check_n,
2219 is_bool_val,
2220 bool_is_true,
2221 method_returns_collection,
2222 },
2223 );
2224 out.push_str(&rendered);
2225}
2226
2227fn build_java_method_call(
2231 result_var: &str,
2232 method_name: &str,
2233 args: Option<&serde_json::Value>,
2234 class_name: &str,
2235) -> String {
2236 match method_name {
2237 "root_child_count" => format!("{result_var}.rootNode().childCount()"),
2238 "root_node_type" => format!("{result_var}.rootNode().kind()"),
2239 "named_children_count" => format!("{result_var}.rootNode().namedChildCount()"),
2240 "has_error_nodes" => format!("{class_name}.treeHasErrorNodes({result_var})"),
2241 "error_count" | "tree_error_count" => format!("{class_name}.treeErrorCount({result_var})"),
2242 "tree_to_sexp" => format!("{class_name}.treeToSexp({result_var})"),
2243 "contains_node_type" => {
2244 let node_type = args
2245 .and_then(|a| a.get("node_type"))
2246 .and_then(|v| v.as_str())
2247 .unwrap_or("");
2248 format!("{class_name}.treeContainsNodeType({result_var}, \"{node_type}\")")
2249 }
2250 "find_nodes_by_type" => {
2251 let node_type = args
2252 .and_then(|a| a.get("node_type"))
2253 .and_then(|v| v.as_str())
2254 .unwrap_or("");
2255 format!("{class_name}.findNodesByType({result_var}, \"{node_type}\")")
2256 }
2257 "run_query" => {
2258 let query_source = args
2259 .and_then(|a| a.get("query_source"))
2260 .and_then(|v| v.as_str())
2261 .unwrap_or("");
2262 let language = args
2263 .and_then(|a| a.get("language"))
2264 .and_then(|v| v.as_str())
2265 .unwrap_or("");
2266 let escaped_query = escape_java(query_source);
2267 format!("{class_name}.runQuery({result_var}, \"{language}\", \"{escaped_query}\", source)")
2268 }
2269 _ => {
2270 format!("{result_var}.{}()", method_name.to_lower_camel_case())
2271 }
2272 }
2273}
2274
2275fn emit_java_object_array(arr: &serde_json::Value, elem_type: &str) -> String {
2278 if let Some(items) = arr.as_array() {
2279 if items.is_empty() {
2280 return "java.util.List.of()".to_string();
2281 }
2282 let item_strs: Vec<String> = items
2283 .iter()
2284 .map(|item| {
2285 let json_str = serde_json::to_string(item).unwrap_or_default();
2286 let escaped = escape_java(&json_str);
2287 format!("JsonUtil.fromJson(\"{escaped}\", {elem_type}.class)")
2288 })
2289 .collect();
2290 format!("java.util.Arrays.asList({})", item_strs.join(", "))
2291 } else {
2292 "java.util.List.of()".to_string()
2293 }
2294}
2295
2296fn json_to_java(value: &serde_json::Value) -> String {
2298 json_to_java_typed(value, None)
2299}
2300
2301fn emit_java_batch_item_array(arr: &serde_json::Value, elem_type: &str) -> String {
2305 if let Some(items) = arr.as_array() {
2306 let item_strs: Vec<String> = items
2307 .iter()
2308 .filter_map(|item| {
2309 if let Some(obj) = item.as_object() {
2310 match elem_type {
2311 "BatchBytesItem" => {
2312 let content = obj.get("content").and_then(|v| v.as_array());
2313 let mime_type = obj.get("mime_type").and_then(|v| v.as_str()).unwrap_or("text/plain");
2314 let content_code = if let Some(arr) = content {
2315 let bytes: Vec<String> = arr
2316 .iter()
2317 .filter_map(|v| v.as_u64().map(|n| format!("(byte) {}", n)))
2318 .collect();
2319 format!("new byte[] {{{}}}", bytes.join(", "))
2320 } else {
2321 "new byte[] {}".to_string()
2322 };
2323 Some(format!("new {}({}, \"{}\", null)", elem_type, content_code, mime_type))
2324 }
2325 "BatchFileItem" => {
2326 let path = obj.get("path").and_then(|v| v.as_str()).unwrap_or("");
2327 Some(format!(
2328 "new {}(java.nio.file.Paths.get(\"{}\"), null)",
2329 elem_type, path
2330 ))
2331 }
2332 _ => None,
2333 }
2334 } else {
2335 None
2336 }
2337 })
2338 .collect();
2339 format!("java.util.Arrays.asList({})", item_strs.join(", "))
2340 } else {
2341 "java.util.List.of()".to_string()
2342 }
2343}
2344
2345fn json_to_java_typed(value: &serde_json::Value, element_type: Option<&str>) -> String {
2346 match value {
2347 serde_json::Value::String(s) => format!("\"{}\"", escape_java(s)),
2348 serde_json::Value::Bool(b) => b.to_string(),
2349 serde_json::Value::Number(n) => {
2350 if n.is_f64() {
2351 match element_type {
2352 Some("f32" | "float" | "Float") => format!("{}f", n),
2353 _ => format!("{}d", n),
2354 }
2355 } else {
2356 n.to_string()
2357 }
2358 }
2359 serde_json::Value::Null => "null".to_string(),
2360 serde_json::Value::Array(arr) => {
2361 let items: Vec<String> = arr.iter().map(|v| json_to_java_typed(v, element_type)).collect();
2362 format!("java.util.List.of({})", items.join(", "))
2363 }
2364 serde_json::Value::Object(_) => {
2365 let json_str = serde_json::to_string(value).unwrap_or_default();
2366 format!("\"{}\"", escape_java(&json_str))
2367 }
2368 }
2369}
2370
2371fn java_builder_expression(
2382 obj: &serde_json::Map<String, serde_json::Value>,
2383 type_name: &str,
2384 enum_fields: &std::collections::HashSet<String>,
2385 nested_types: &std::collections::HashMap<String, String>,
2386 nested_types_optional: bool,
2387 path_fields: &[String],
2388) -> String {
2389 let mut expr = format!("{}.builder()", type_name);
2390 for (key, val) in obj {
2391 let camel_key = key.to_lower_camel_case();
2393 let method_name = format!("with{}", camel_key.to_upper_camel_case());
2394
2395 let java_val = match val {
2396 serde_json::Value::String(s) => {
2397 if enum_fields.contains(&camel_key) {
2400 let enum_type_name = camel_key.to_upper_camel_case();
2402 let variant_name = s.to_upper_camel_case();
2403 format!("{}.{}", enum_type_name, variant_name)
2404 } else if camel_key == "preset" && type_name == "PreprocessingOptions" {
2405 let variant_name = s.to_upper_camel_case();
2407 format!("PreprocessingPreset.{}", variant_name)
2408 } else if path_fields.contains(key) {
2409 format!("Optional.of(java.nio.file.Path.of(\"{}\"))", escape_java(s))
2411 } else {
2412 format!("\"{}\"", escape_java(s))
2414 }
2415 }
2416 serde_json::Value::Bool(b) => b.to_string(),
2417 serde_json::Value::Null => "null".to_string(),
2418 serde_json::Value::Number(n) => {
2419 let camel_key = key.to_lower_camel_case();
2427 let is_plain_field = matches!(camel_key.as_str(), "listIndentWidth" | "wrapWidth");
2428 let is_primitive_builder = matches!(type_name, "SecurityLimits" | "SecurityLimitsBuilder");
2431
2432 if is_plain_field || is_primitive_builder {
2433 if n.is_f64() {
2435 format!("{}d", n)
2436 } else {
2437 format!("{}L", n)
2438 }
2439 } else {
2440 if n.is_f64() {
2442 format!("Optional.of({}d)", n)
2443 } else {
2444 format!("Optional.of({}L)", n)
2445 }
2446 }
2447 }
2448 serde_json::Value::Array(arr) => {
2449 let items: Vec<String> = arr.iter().map(|v| json_to_java_typed(v, None)).collect();
2450 format!("java.util.List.of({})", items.join(", "))
2451 }
2452 serde_json::Value::Object(nested) => {
2453 let nested_type = nested_types
2455 .get(key.as_str())
2456 .cloned()
2457 .unwrap_or_else(|| format!("{}Options", key.to_upper_camel_case()));
2458 let inner = java_builder_expression(
2459 nested,
2460 &nested_type,
2461 enum_fields,
2462 nested_types,
2463 nested_types_optional,
2464 &[],
2465 );
2466 let is_primitive_builder = matches!(type_name, "SecurityLimits" | "SecurityLimitsBuilder");
2470 if is_primitive_builder || !nested_types_optional {
2471 inner
2472 } else {
2473 format!("Optional.of({inner})")
2474 }
2475 }
2476 };
2477 expr.push_str(&format!(".{}({})", method_name, java_val));
2478 }
2479 expr.push_str(".build()");
2480 expr
2481}
2482
2483#[allow(dead_code)]
2490fn collect_enum_and_nested_types(
2491 obj: &serde_json::Map<String, serde_json::Value>,
2492 enum_fields: &std::collections::HashMap<String, String>,
2493 types_out: &mut std::collections::BTreeSet<String>,
2494) {
2495 for (key, val) in obj {
2496 let camel_key = key.to_lower_camel_case();
2498 if let Some(enum_type) = enum_fields.get(&camel_key) {
2499 types_out.insert(enum_type.clone());
2501 } else if camel_key == "preset" {
2502 types_out.insert("PreprocessingPreset".to_string());
2504 }
2505 if let Some(nested) = val.as_object() {
2507 collect_enum_and_nested_types(nested, enum_fields, types_out);
2508 }
2509 }
2510}
2511
2512fn collect_nested_type_names(
2513 obj: &serde_json::Map<String, serde_json::Value>,
2514 nested_types: &std::collections::HashMap<String, String>,
2515 types_out: &mut std::collections::BTreeSet<String>,
2516) {
2517 for (key, val) in obj {
2518 if let Some(type_name) = nested_types.get(key.as_str()) {
2519 types_out.insert(type_name.clone());
2520 }
2521 if let Some(nested) = val.as_object() {
2522 collect_nested_type_names(nested, nested_types, types_out);
2523 }
2524 }
2525}
2526
2527fn build_java_visitor(
2533 setup_lines: &mut Vec<String>,
2534 visitor_spec: &crate::fixture::VisitorSpec,
2535 class_name: &str,
2536) -> String {
2537 setup_lines.push("class _TestVisitor implements Visitor {".to_string());
2538 for (method_name, action) in &visitor_spec.callbacks {
2539 emit_java_visitor_method(setup_lines, method_name, action, class_name);
2540 }
2541 setup_lines.push("}".to_string());
2542 setup_lines.push("var visitor = new _TestVisitor();".to_string());
2543 "visitor".to_string()
2544}
2545
2546fn emit_java_visitor_method(
2548 setup_lines: &mut Vec<String>,
2549 method_name: &str,
2550 action: &CallbackAction,
2551 _class_name: &str,
2552) {
2553 let camel_method = method_to_camel(method_name);
2554 let params = match method_name {
2555 "visit_link" => "NodeContext ctx, String href, String text, String title",
2556 "visit_image" => "NodeContext ctx, String src, String alt, String title",
2557 "visit_heading" => "NodeContext ctx, int level, String text, String id",
2558 "visit_code_block" => "NodeContext ctx, String lang, String code",
2559 "visit_code_inline"
2560 | "visit_strong"
2561 | "visit_emphasis"
2562 | "visit_strikethrough"
2563 | "visit_underline"
2564 | "visit_subscript"
2565 | "visit_superscript"
2566 | "visit_mark"
2567 | "visit_button"
2568 | "visit_summary"
2569 | "visit_figcaption"
2570 | "visit_definition_term"
2571 | "visit_definition_description" => "NodeContext ctx, String text",
2572 "visit_text" => "NodeContext ctx, String text",
2573 "visit_list_item" => "NodeContext ctx, boolean ordered, String marker, String text",
2574 "visit_blockquote" => "NodeContext ctx, String content, long depth",
2575 "visit_table_row" => "NodeContext ctx, java.util.List<String> cells, boolean isHeader",
2576 "visit_custom_element" => "NodeContext ctx, String tagName, String html",
2577 "visit_form" => "NodeContext ctx, String actionUrl, String method",
2578 "visit_input" => "NodeContext ctx, String inputType, String name, String value",
2579 "visit_audio" | "visit_video" | "visit_iframe" => "NodeContext ctx, String src",
2580 "visit_details" => "NodeContext ctx, boolean isOpen",
2581 "visit_element_end" | "visit_table_end" | "visit_definition_list_end" | "visit_figure_end" => {
2582 "NodeContext ctx, String output"
2583 }
2584 "visit_list_start" => "NodeContext ctx, boolean ordered",
2585 "visit_list_end" => "NodeContext ctx, boolean ordered, String output",
2586 _ => "NodeContext ctx",
2587 };
2588
2589 let (action_type, action_value, format_args) = match action {
2591 CallbackAction::Skip => ("skip", String::new(), Vec::new()),
2592 CallbackAction::Continue => ("continue", String::new(), Vec::new()),
2593 CallbackAction::PreserveHtml => ("preserve_html", String::new(), Vec::new()),
2594 CallbackAction::Custom { output } => ("custom_literal", escape_java(output), Vec::new()),
2595 CallbackAction::CustomTemplate { template, .. } => {
2596 let mut format_str = String::with_capacity(template.len());
2598 let mut format_args: Vec<String> = Vec::new();
2599 let mut chars = template.chars().peekable();
2600 while let Some(ch) = chars.next() {
2601 if ch == '{' {
2602 let mut name = String::new();
2604 let mut closed = false;
2605 for inner in chars.by_ref() {
2606 if inner == '}' {
2607 closed = true;
2608 break;
2609 }
2610 name.push(inner);
2611 }
2612 if closed && !name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_') {
2613 let camel_name = name.as_str().to_lower_camel_case();
2614 format_args.push(camel_name);
2615 format_str.push_str("%s");
2616 } else {
2617 format_str.push('{');
2619 format_str.push_str(&name);
2620 if closed {
2621 format_str.push('}');
2622 }
2623 }
2624 } else {
2625 format_str.push(ch);
2626 }
2627 }
2628 let escaped = escape_java(&format_str);
2629 if format_args.is_empty() {
2630 ("custom_literal", escaped, Vec::new())
2631 } else {
2632 ("custom_formatted", escaped, format_args)
2633 }
2634 }
2635 };
2636
2637 let params = params.to_string();
2638
2639 let rendered = crate::template_env::render(
2640 "java/visitor_method.jinja",
2641 minijinja::context! {
2642 camel_method,
2643 params,
2644 action_type,
2645 action_value,
2646 format_args => format_args,
2647 },
2648 );
2649 setup_lines.push(rendered);
2650}
2651
2652fn method_to_camel(snake: &str) -> String {
2654 snake.to_lower_camel_case()
2655}
2656
2657#[cfg(test)]
2658mod tests {
2659 use crate::config::{CallConfig, E2eConfig, SelectWhen};
2660 use crate::fixture::Fixture;
2661 use std::collections::HashMap;
2662
2663 fn make_fixture_with_input(id: &str, input: serde_json::Value) -> Fixture {
2664 Fixture {
2665 id: id.to_string(),
2666 category: None,
2667 description: "test fixture".to_string(),
2668 tags: vec![],
2669 skip: None,
2670 env: None,
2671 call: None,
2672 input,
2673 mock_response: None,
2674 source: String::new(),
2675 http: None,
2676 assertions: vec![],
2677 visitor: None,
2678 }
2679 }
2680
2681 #[test]
2684 fn test_java_select_when_routes_to_batch_scrape() {
2685 let mut calls = HashMap::new();
2686 calls.insert(
2687 "batch_scrape".to_string(),
2688 CallConfig {
2689 function: "batchScrape".to_string(),
2690 module: "com.example.kreuzcrawl".to_string(),
2691 select_when: Some(SelectWhen {
2692 input_has: Some("batch_urls".to_string()),
2693 ..Default::default()
2694 }),
2695 ..CallConfig::default()
2696 },
2697 );
2698
2699 let e2e_config = E2eConfig {
2700 call: CallConfig {
2701 function: "scrape".to_string(),
2702 module: "com.example.kreuzcrawl".to_string(),
2703 ..CallConfig::default()
2704 },
2705 calls,
2706 ..E2eConfig::default()
2707 };
2708
2709 let fixture = make_fixture_with_input("batch_empty_urls", serde_json::json!({ "batch_urls": [] }));
2711
2712 let resolved_call = e2e_config.resolve_call_for_fixture(
2713 fixture.call.as_deref(),
2714 &fixture.id,
2715 &fixture.resolved_category(),
2716 &fixture.tags,
2717 &fixture.input,
2718 );
2719 assert_eq!(resolved_call.function, "batchScrape");
2720
2721 let fixture_no_batch =
2723 make_fixture_with_input("simple_scrape", serde_json::json!({ "url": "https://example.com" }));
2724 let resolved_default = e2e_config.resolve_call_for_fixture(
2725 fixture_no_batch.call.as_deref(),
2726 &fixture_no_batch.id,
2727 &fixture_no_batch.resolved_category(),
2728 &fixture_no_batch.tags,
2729 &fixture_no_batch.input,
2730 );
2731 assert_eq!(resolved_default.function, "scrape");
2732 }
2733}