This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch q4f-followups in repository https://gitbox.apache.org/repos/asf/camel.git
commit 819fd289bcd20ec31827818fa7c09c90b75f3b27 Author: Claus Ibsen <[email protected]> AuthorDate: Sat Dec 5 19:09:41 2026 +0100 chore: camel-jbang - catalog doc answers for the Citrus YAML test DSL and the Rest DSL pages A local model asked the catalog tools for the Citrus YAML test DSL and for the Rest DSL section of the rest EIP, found neither, and read on until its tool calls were spent without writing a file (12 of 12 runs of each step). - camel_catalog_doc name=citrus-test describes the Citrus YAML test DSL from the test schema of the Citrus catalog (keys, actions, a sample test), and docPage=<action> gives the keys of one action as YAML. Find, sample, the citrus component answer and camel_catalog_docs point to it. - The Rest DSL pages of the user manual are packaged in camel-jbang-core; docPage of a Rest DSL EIP (or name=rest-dsl) returns their sections. - rest-dsl-binding.adoc: complete the cut-off heading "Binding to POJOs using data formats". Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y4dDi5iQNgtHrDdz2NpwF --- .../modules/ROOT/pages/rest-dsl-binding.adoc | 2 +- dsl/camel-jbang/camel-jbang-core/pom.xml | 12 + .../dsl/jbang/core/commands/ai/AuthoringTools.java | 4 +- .../dsl/jbang/core/commands/ai/CatalogDocs.java | 97 +++- .../dsl/jbang/core/commands/ai/CatalogSamples.java | 8 + .../jbang/core/commands/ai/CitrusTestChecks.java | 11 +- .../dsl/jbang/core/commands/ai/CitrusTestDocs.java | 573 +++++++++++++++++++++ .../jbang/core/commands/ai/citrus-test-sample.yaml | 61 +++ .../jbang/core/commands/ai/CatalogDocsTest.java | 33 +- .../jbang/core/commands/ai/CitrusTestDocsTest.java | 136 +++++ .../jbang/core/commands/mcp/AuthoringTools.java | 6 +- .../dsl/jbang/core/commands/mcp/CatalogTools.java | 12 +- .../core/commands/mcp/McpServerInstructions.java | 5 +- 13 files changed, 938 insertions(+), 22 deletions(-) diff --git a/docs/user-manual/modules/ROOT/pages/rest-dsl-binding.adoc b/docs/user-manual/modules/ROOT/pages/rest-dsl-binding.adoc index ae9814bb2b92..9ca432af5575 100644 --- a/docs/user-manual/modules/ROOT/pages/rest-dsl-binding.adoc +++ b/docs/user-manual/modules/ROOT/pages/rest-dsl-binding.adoc @@ -3,7 +3,7 @@ xref:rest-dsl.adoc[Back to Rest DSL] -== Binding to POJOs using +== Binding to POJOs using data formats The Rest DSL supports automatic binding json/xml contents to/from POJOs using data formats. By default, the binding mode is off, meaning there is no automatic binding happening for diff --git a/dsl/camel-jbang/camel-jbang-core/pom.xml b/dsl/camel-jbang/camel-jbang-core/pom.xml index 0efa9cd0edca..f6d9d4c49bf1 100644 --- a/dsl/camel-jbang/camel-jbang-core/pom.xml +++ b/dsl/camel-jbang/camel-jbang-core/pom.xml @@ -474,6 +474,18 @@ <resource> <directory>src/generated/resources</directory> </resource> + <resource> + <!-- the Rest DSL pages of the user manual, which the catalog does not have: the + sections camel_catalog_doc returns for the EIPs of the Rest DSL --> + <directory>${project.basedir}/../../../docs/user-manual/modules/ROOT/pages</directory> + <targetPath>org/apache/camel/dsl/jbang/core/commands/ai/user-manual</targetPath> + <includes> + <include>rest-dsl.adoc</include> + <include>rest-dsl-binding.adoc</include> + <include>rest-dsl-validation.adoc</include> + <include>rest-dsl-openapi.adoc</include> + </includes> + </resource> </resources> </configuration> </execution> diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringTools.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringTools.java index bf8c875a4694..7cc9c45c967c 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringTools.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringTools.java @@ -116,9 +116,9 @@ public final class AuthoringTools { /** Registers the authoring tools; called once by the {@link ToolRegistry}. */ static void register(Consumer<ToolDescriptor> registry) { registry.accept(tool("camel_catalog_doc", - "Catalog documentation of a component, data format, language, EIP, Kamelet, built-in bean or the Java API: description, options, Maven coordinates, the URI rules of a component; for simple its functions and operators (optionsFilter narrows them). endpoint validates a URI. A full key such as camel.server.port lists its application.properties group.") + "Catalog documentation of a component, data format, language, EIP, Kamelet, built-in bean or the Java API: description, options, Maven coordinates, the URI rules of a component; for simple its functions and operators (optionsFilter narrows them). endpoint validates a URI. A full key such as camel.server.port lists its application.properties group. name=citrus-test is the Citrus YAML test DSL (test/*.citrus.it.yaml), docPage one of its actions; name=rest-dsl the Rest DSL p [...] .param("name", "string", - "Name, e.g. kafka, json (a data format by its YAML name or artifact), simple, timer, choice, split, Exchange", + "Name, e.g. kafka, json (a data format by its YAML name or artifact), simple, timer, choice, split, Exchange, rest-dsl, citrus-test", false) .param("endpoint", "string", "Endpoint URI to check, e.g. kafka:orders?brokers=host:9092", false) .param("kind", "string", diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogDocs.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogDocs.java index e080fdcf5f81..f20412f5f1b3 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogDocs.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogDocs.java @@ -258,6 +258,10 @@ public final class CatalogDocs { String filter = filterTerms(optionsFilter); JsonObject answer = answer(catalog, name, endpoint, kind, filter, includeOptions, includeHeaders, includeDoc, docPage, limit, kameletsVersion); + if (name != null && "citrus".equalsIgnoreCase(name.trim()) && !CitrusTestDocs.NAME.equals(answer.getString("kind"))) { + // the citrus component is for routes; a model that wants to write a Citrus test asks for it too + answer.put("citrusTest", CitrusTestDocs.HINT); + } if (several(filter) && (endpoint == null || endpoint.isBlank()) && (docPage == null || docPage.isBlank()) && !answer.containsKey("error")) { putUnmatchedTerms(catalog, name, kind, filter, includeOptions, limit, kameletsVersion, answer); @@ -373,6 +377,11 @@ public final class CatalogDocs { name = name.substring(0, name.length() - suffixKind.length() - 1); kind = suffixKind; } + if (normalizeName(name).startsWith("restdsl") && (kind == null || "eip".equals(kind))) { + // the Rest DSL pages of the user manual (rest-dsl, rest-dsl-binding, Rest DSL): the rest EIP has them + name = "rest"; + kind = "eip"; + } String page = docPage != null ? docPage.trim().toLowerCase(Locale.ROOT) : null; if ("kamelet".equals(kind) && !KameletDefinitions.catalog(kameletsVersion).containsKey(name) && ("kamelet".equals(name) || page != null)) { @@ -386,6 +395,15 @@ public final class CatalogDocs { if (scope == null) { return error("includeOptions must be common, required, all, true or false, got: " + includeOptions); } + // the Citrus YAML test DSL, which is not in the Camel catalog: name=citrus-test (or citrus test yaml), or an + // action with kind=citrus-test (q4f benchmark, Dec 2026: a model asked for it this way and found nothing) + boolean citrusKind = kind != null && (CitrusTestDocs.NAME.equalsIgnoreCase(kind) || "citrus".equalsIgnoreCase(kind) + || CitrusTestDocs.isCitrusTest(kind)); + if (CitrusTestDocs.isCitrusTest(name) || citrusKind) { + String action = docPage != null && !docPage.isBlank() ? docPage + : citrusKind && !CitrusTestDocs.isCitrusTest(name) && !"citrus".equalsIgnoreCase(name) ? name : null; + return CitrusTestDocs.doc(action, scope == OptionScope.ALL); + } if (kind == null || "component".equals(kind)) { ComponentModel cm = catalog.componentModel(name); @@ -409,6 +427,10 @@ public final class CatalogDocs { } // else a section of the page, by its title (CAMEL-25541) DocSection section = findSection(sections, page); + if (section == null && "citrus".equals(cm.getScheme()) && CitrusTestDocs.isAction(docPage)) { + // an action of a Citrus test (http:sendRequest), which the component page does not have + return CitrusTestDocs.doc(docPage, scope == OptionScope.ALL); + } if (section == null) { JsonObject err = error("No doc page or section '" + page + "' for component " + name); err.put("docPages", new JsonArray(pages)); @@ -844,6 +866,13 @@ public final class CatalogDocs { result.put("closestOptions", closest); } } + if (isRestDsl(model) && docSections(catalog.asciiDoc(model.getName() + "-eip")).isEmpty() + && !restDslSections().isEmpty()) { + result.put("restDslDoc", "camel_catalog_doc name=" + model.getName() + " docPage=<section> returns a" + + " section of the Rest DSL pages of the user manual, such as: " + + String.join(", ", restDslSections().stream().filter(s -> s.level() == 2) + .map(DocSection::title).toList())); + } putElsewhere(catalog, result, filter); if (matchedTerm != null) { result.put("matchedTerm", matchedTerm); @@ -861,14 +890,25 @@ public final class CatalogDocs { private static JsonObject eipSection(CamelCatalog catalog, EipModel em, String page, String lowerFilter) { String adoc = catalog.asciiDoc(em.getName() + "-eip"); List<DocSection> sections = docSections(adoc); + boolean restDsl = sections.isEmpty() && isRestDsl(em); + if (restDsl) { + sections = restDslSections(); + } DocSection section = findSection(sections, page); if (section != null) { JsonObject answer = eipDoc(em, lowerFilter, OptionScope.NONE, section.text()); answer.put("section", section.title()); + if (restDsl) { + answer.put("source", restDslPage(section) + " (the user manual of Camel " + new DefaultCamelCatalog() + .getCatalogVersion() + ")"); + } return answer; } JsonObject err; - if (sections.isEmpty()) { + if (restDsl) { + err = error("No section '" + page + "' in the Rest DSL pages of the user manual"); + err.put("sections", sectionTitles(sections, lowerFilter)); + } else if (sections.isEmpty()) { err = error("The EIP " + em.getName() + " has no doc page with sections in the catalog, so there is no" + " section '" + page + "'. Its options are in camel_catalog_doc name=" + em.getName() + " without docPage"); @@ -887,6 +927,54 @@ public final class CatalogDocs { return err; } + /** The name camel_catalog_doc takes for the Rest DSL pages of the user manual, as the rest EIP with docPage. */ + public static final String REST_DSL_NAME = "rest-dsl"; + + /** The pages of the user manual about the Rest DSL that camel-jbang-core carries, in the order they are read. */ + static final List<String> REST_DSL_PAGES + = List.of("rest-dsl.adoc", "rest-dsl-binding.adoc", "rest-dsl-validation.adoc", "rest-dsl-openapi.adoc"); + + private static volatile Map<DocSection, String> restDslSections; + + /** Whether the EIP is a part of the Rest DSL (rest, restConfiguration, post, param...), which has no EIP page. */ + static boolean isRestDsl(EipModel em) { + return em.getLabel() != null && Arrays.asList(em.getLabel().split(",")).contains("rest"); + } + + /** + * The sections of the Rest DSL pages of the user manual. The rest EIP has no EIP page, and a model that asked for + * the Rest DSL section of it was told so, and asked again with other flags until its tool calls were spent; the + * Rest DSL section of the user manual was what it looked for (contracts-rest-code-first of the local-model + * benchmark, qwen4, Dec 2026: 12 of 12 runs). + */ + static List<DocSection> restDslSections() { + return new ArrayList<>(restDslPages().keySet()); + } + + private static String restDslPage(DocSection section) { + return restDslPages().getOrDefault(section, REST_DSL_PAGES.get(0)); + } + + private static Map<DocSection, String> restDslPages() { + Map<DocSection, String> answer = restDslSections; + if (answer == null) { + answer = new LinkedHashMap<>(); + for (String page : REST_DSL_PAGES) { + try (InputStream is = CatalogDocs.class.getResourceAsStream("user-manual/" + page)) { + if (is != null) { + for (DocSection s : docSections(new String(is.readAllBytes(), StandardCharsets.UTF_8))) { + answer.put(s, page); + } + } + } catch (java.io.IOException e) { + LOG.debug("Cannot read the user manual page {}", page, e); + } + } + restDslSections = answer; + } + return answer; + } + /** * The top-level entries of the YAML DSL whose model is an application model of the catalog (models-app), not an EIP * model: camel_catalog_doc and camel_catalog_find did not find sslContextParameters (CAMEL-25735). @@ -1361,6 +1449,11 @@ public final class CatalogDocs { matches.add(o); } } + if (CitrusTestDocs.findsCitrusTest(term) + && (kind == null || CitrusTestDocs.NAME.equals(kind) || "component".equals(kind))) { + // the Citrus YAML test DSL is not in the Camel catalog; a model looks for it by these words + matches.add(0, CitrusTestDocs.findMatch()); + } String header = null; if (matches.isEmpty() && (kind == null || "component".equals(kind))) { // a message header by its name or constant (CamelAwsS3Key, AWS2S3Constants KEY), which nothing is named @@ -2021,7 +2114,7 @@ public final class CatalogDocs { : sb + " (each returned as text)"; } - private static JsonObject error(String message) { + static JsonObject error(String message) { JsonObject err = new JsonObject(); err.put("error", message); return err; diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogSamples.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogSamples.java index 7878325e7625..27ab53a0a726 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogSamples.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogSamples.java @@ -489,6 +489,11 @@ public final class CatalogSamples { */ public static JsonObject sample(CamelCatalog catalog, String kind, String name, int limit) { String lower = name != null ? name.trim().toLowerCase(Locale.ROOT) : ""; + if (CitrusTestDocs.isCitrusTest(name) || CitrusTestDocs.isCitrusTest(kind) + || CitrusTestDocs.NAME.equalsIgnoreCase(kind)) { + // a Citrus test, which is not a route (q4f benchmark, Dec 2026: camel_catalog_sample name="citrus test") + return CitrusTestDocs.sampleAnswer(name); + } boolean kameletKind = "kamelet".equalsIgnoreCase(kind) || "kamelet file".equalsIgnoreCase(kind); if (kameletKind || lower.contains("kamelet") && !lower.equals("kamelet") && kind == null) { // kamelet-custom, custom-kamelet, kamelet-action: how to write a Kamelet file @@ -504,6 +509,9 @@ public final class CatalogSamples { } } JsonObject answer = doSample(catalog, kind, name, limit); + if (lower.equals("citrus")) { + answer.put("citrusTest", CitrusTestDocs.HINT); + } if (lower.equals("kamelet") && !answer.containsKey("error")) { // the samples show a route that uses a Kamelet; a model asking for kamelet often wants to write one List<Map<String, String>> files = kameletFileSamples(catalog); diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CitrusTestChecks.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CitrusTestChecks.java index fb019f2d1d6e..fba70706f5cb 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CitrusTestChecks.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CitrusTestChecks.java @@ -228,7 +228,7 @@ public final class CitrusTestChecks { */ private static TestSchema load(String version) { try { - return of(version, read(version)); + return of(version, read(version, SCHEMA_FILE)); } catch (Exception e) { String msg = e.getMessage() != null ? e.getMessage().lines().findFirst().orElse("") : ""; return new TestSchema( @@ -238,11 +238,12 @@ public final class CitrusTestChecks { } /** - * The test schema from the classpath (camel-launcher has the test plugin), else from the catalog jar of the version - * in the local Maven repository (where the test plugin put it when it was downloaded), or downloaded. + * A file of the Citrus catalog, such as the test schema, from the classpath (camel-launcher has the test plugin), + * else from the catalog jar of the version in the local Maven repository (where the test plugin put it when it was + * downloaded), or downloaded. */ - private static String read(String version) throws Exception { - String entry = SCHEMA_DIR + version + "/" + SCHEMA_FILE; + static String read(String version, String file) throws Exception { + String entry = SCHEMA_DIR + version + "/" + file; try (InputStream is = CitrusTestChecks.class.getClassLoader().getResourceAsStream(entry)) { if (is != null) { return new String(is.readAllBytes(), StandardCharsets.UTF_8); diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CitrusTestDocs.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CitrusTestDocs.java new file mode 100644 index 000000000000..674788075618 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/CitrusTestDocs.java @@ -0,0 +1,573 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dsl.jbang.core.commands.ai; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import org.apache.camel.util.json.JsonArray; +import org.apache.camel.util.json.JsonObject; +import tools.jackson.databind.JsonNode; +import tools.jackson.databind.ObjectMapper; +import tools.jackson.databind.json.JsonMapper; + +/** + * The Citrus YAML test DSL as the catalog tools answer it: what a test file is, its keys, its actions, a sample test, + * and the keys of one action, all from the test schema of the Citrus catalog (the one {@link CitrusTestChecks} checks a + * test with). A model asked to write a Citrus test searched the Camel catalog for it (camel_catalog_find citrus test + * yaml, camel_catalog_sample citrus test) and found nothing, then read section after section of the citrus component + * page, which is about a component of routes, until it ran out of tool calls without writing the test + * (test-citrus-orders of the local-model benchmark, qwen4, Dec 2026: 12 of 12 runs). + * <p/> + * The schema is the documentation: each action and key has its description there. An action is shown as the YAML it is + * written in, each key with its type and description as a comment, so the answer can be copied. + */ +public final class CitrusTestDocs { + + /** The name the catalog tools give the Citrus YAML test DSL (camel_catalog_doc name=citrus-test). */ + public static final String NAME = "citrus-test"; + + /** What the answers are about, for the hints of other answers. */ + public static final String HINT = "A Citrus test (test/<name>.citrus.it.yaml, run with camel test run) is written in the" + + " Citrus YAML test DSL, not with the citrus component: camel_catalog_doc" + + " name=citrus-test describes it with a sample test, docPage=<action> (http," + + " http:sendRequest, camel:cli:run) gives the keys of an action, and" + + " camel_catalog_sample name=citrus-test returns the sample test"; + + private static final String SAMPLE_RESOURCE = "citrus-test-sample.yaml"; + /** How many actions of a group the overview names; docPage=<group> names them all. */ + private static final int MAX_SUB_ACTIONS = 8; + /** How deep the keys under an action are shown. */ + private static final int MAX_DEPTH = 5; + /** How many lines the YAML of an action may have; more are left out with a note. */ + private static final int MAX_LINES = 80; + /** The method of an HTTP request that stands for the others in the YAML of an action. */ + private static final List<String> PREFERRED = List.of("POST", "GET"); + private static final Set<String> ACTION_REFS = Set.of("#/definitions/TestActions", "#/definitions/TestActionBuilder"); + + private static final String ACTION_CATALOG = "citrus-catalog-aggregate-test-actions.json"; + private static final ObjectMapper JSON = JsonMapper.builderWithJackson2Defaults().build(); + + private static volatile String sample; + private static volatile Set<String> actionNames; + + private CitrusTestDocs() { + } + + /** + * Whether a name or kind of camel_catalog_doc or camel_catalog_sample asks for the Citrus YAML test DSL: + * citrus-test, citrus test yaml, citrus yaml dsl, citrus-yaml; not citrus alone, which is the component. + */ + public static boolean isCitrusTest(String name) { + if (name == null) { + return false; + } + String lower = name.trim().toLowerCase(Locale.ROOT); + if (!lower.startsWith("citrus") || lower.equals("citrus")) { + return false; + } + String rest = lower.substring("citrus".length()).replaceAll("[^a-z]+", " ").strip(); + return !rest.isEmpty() && List.of(rest.split(" ")).stream() + .allMatch(w -> Set.of("test", "tests", "testing", "yaml", "dsl", "it", "case", "testcase", "file", + "action", "actions", "syntax", "sample", "example", "examples").contains(w)); + } + + /** Whether a find term is about Citrus tests: it names citrus, and is more than the component name alone. */ + public static boolean findsCitrusTest(String term) { + if (term == null) { + return false; + } + String lower = term.trim().toLowerCase(Locale.ROOT); + return lower.contains("citrus") && (isCitrusTest(lower) || lower.contains("test") || lower.contains("yaml")); + } + + /** The match camel_catalog_find adds for a term about Citrus tests. */ + public static JsonObject findMatch() { + JsonObject o = new JsonObject(); + o.put("kind", NAME); + o.put("name", NAME); + o.put("title", "Citrus YAML test DSL"); + o.put("description", "How to write a Citrus test (test/<name>.citrus.it.yaml, run with camel test run): its" + + " keys, its actions and a sample test; camel_catalog_doc name=citrus-test"); + return o; + } + + /** + * The Citrus YAML test DSL (no action given), or the keys of one action, such as http, http:sendRequest, + * camel:cli:run or "camel cli verify". + * + * @param action the action, or null for the DSL itself + * @param all whether to show the advanced keys too + */ + public static JsonObject doc(String action, boolean all) { + if (!CitrusTestChecks.hasSchema()) { + JsonObject err = CatalogDocs.error("The test schema of Citrus " + CitrusTestChecks.version() + + " could not be loaded (" + CitrusTestChecks.schemaProblem() + ")"); + err.put("sample", sample()); + return err; + } + if (action == null || action.isBlank() || isCitrusTest(action) || "main".equalsIgnoreCase(action.trim())) { + return overview(); + } + List<String> path = actionPath(action); + if (path == null) { + JsonObject err = CatalogDocs.error("Citrus " + CitrusTestChecks.version() + " has no test action '" + + action.trim() + "'"); + err.put("actions", actionNames()); + err.put("hint", "docPage=<action>, with its group as http:sendRequest or camel:cli:run"); + return err; + } + return action(path, all); + } + + /** Whether the text names an action of a Citrus test (http, http:sendRequest, camel cli run). */ + public static boolean isAction(String text) { + return text != null && !text.isBlank() && CitrusTestChecks.hasSchema() && actionPath(text) != null; + } + + /** The sample test, which the tests check against the test schema. */ + public static String sample() { + String s = sample; + if (s == null) { + try (InputStream is = CitrusTestDocs.class.getResourceAsStream(SAMPLE_RESOURCE)) { + s = is != null ? new String(is.readAllBytes(), StandardCharsets.UTF_8) : ""; + } catch (IOException e) { + s = ""; + } + sample = s; + } + return s; + } + + /** The answer of camel_catalog_sample name=citrus-test. */ + public static JsonObject sampleAnswer(String name) { + JsonObject answer = new JsonObject(); + answer.put("name", name != null ? name.trim() : NAME); + answer.put("kind", NAME); + answer.put("placement", placement()); + JsonObject one = new JsonObject(); + one.put("source", "Camel CLI, checked against the test schema of Citrus " + CitrusTestChecks.version()); + one.put("yaml", sample()); + answer.put("samples", new JsonArray(List.of(one))); + answer.put("count", 1); + answer.put("docHint", "camel_catalog_doc name=citrus-test lists the actions; docPage=<action> gives the keys of" + + " one"); + return answer; + } + + private static String placement() { + return "a file of its own, test/<name>.citrus.it.yaml in the project directory (camel test init <file> makes" + + " one); run it with camel test run test/<name>.citrus.it.yaml from the project directory. The paths in" + + " the test are relative to the test file: ../orders.camel.yaml, ../sample.json"; + } + + private static JsonObject overview() { + JsonNode model = CitrusTestChecks.schema().model(); + JsonObject answer = new JsonObject(); + answer.put("kind", NAME); + answer.put("name", NAME); + answer.put("title", "Citrus YAML test DSL (Citrus " + CitrusTestChecks.version() + ")"); + answer.put("description", "A Citrus test is a YAML file with a name and a list of actions that run in order:" + + " start the integration (camel: cli: run), send a request (http: sendRequest)," + + " check the response (http: receiveResponse), check the log (camel: cli: verify)."); + answer.put("placement", placement()); + JsonObject keys = new JsonObject(); + JsonNode props = model.path("properties"); + for (String key : props.propertyNames()) { + keys.put(key, description(props.get(key))); + } + answer.put("testKeys", keys); + JsonArray actions = new JsonArray(); + JsonNode top = model.at("/definitions/TestActions/properties"); + for (String name : top.propertyNames()) { + JsonObject o = new JsonObject(); + o.put("name", name); + String d = description(top.get(name)); + if (!d.isEmpty()) { + o.put("description", d); + } + List<String> subs = subActions(List.of(name)); + if (subs.size() > MAX_SUB_ACTIONS) { + // playwright, selenium and kubernetes have dozens: docPage=<group> lists them all + o.put("actions", new JsonArray(subs.subList(0, MAX_SUB_ACTIONS))); + o.put("moreActions", subs.size() - MAX_SUB_ACTIONS); + } else if (!subs.isEmpty()) { + o.put("actions", new JsonArray(subs)); + } + actions.add(o); + } + answer.put("actions", actions); + answer.put("sample", sample()); + answer.put("actionHint", "camel_catalog_doc name=citrus-test docPage=<action> gives the keys of an action as" + + " YAML: http:sendRequest, http:receiveResponse, camel:cli:run, camel:cli:verify"); + answer.put("examplesHint", "camel_catalog_examples filter=citrus lists the example projects with a Citrus test" + + " (test/ has it)"); + return answer; + } + + private static JsonObject action(List<String> path, boolean all) { + JsonNode top = CitrusTestChecks.schema().model().at("/definitions/TestActions/properties"); + // the keys of each group on the way (http: client, server), then the action with its keys + List<String> lines = new ArrayList<>(); + List<String> advanced = new ArrayList<>(); + JsonNode node = top.get(path.get(0)); + String indent = " "; + lines.add("- " + path.get(0) + ":" + comment(description(node), null)); + for (int i = 1; i < path.size(); i++) { + Map<String, JsonNode> props = properties(node); + List<String> siblings = subActions(path.subList(0, i)); + for (Map.Entry<String, JsonNode> e : props.entrySet()) { + if (!e.getKey().equals(path.get(i)) && !siblings.contains(e.getKey()) && !isAdvanced(e.getValue()) + && properties(e.getValue()).isEmpty()) { + lines.add(indent + e.getKey() + ": <" + type(e.getValue()) + ">" + + comment(description(e.getValue()), null)); + } + } + node = props.get(path.get(i)); + lines.add(indent + path.get(i) + ":" + comment(description(node), null)); + indent += " "; + } + List<String> subs = subActions(path); + if (subs.isEmpty()) { + shape(node, indent, MAX_DEPTH, lines, advanced, String.join(".", path), all); + } else { + // a group: its own keys, and its actions by name, each asked for on its own + Map<String, JsonNode> props = properties(node); + for (Map.Entry<String, JsonNode> e : props.entrySet()) { + if (!subs.contains(e.getKey()) && (all || !isAdvanced(e.getValue()))) { + lines.add(indent + e.getKey() + ": <" + type(e.getValue()) + ">" + + comment(describe(e.getValue()), null)); + } else if (!subs.contains(e.getKey())) { + advanced.add(String.join(".", path) + "." + e.getKey()); + } + } + for (String sub : subs) { + lines.add(indent + sub + ": {...}" + comment(description(props.get(sub)), null)); + } + } + JsonObject answer = new JsonObject(); + answer.put("kind", NAME); + answer.put("action", String.join(":", path)); + String d = description(node); + if (!d.isEmpty()) { + answer.put("description", d); + } + boolean cut = lines.size() > MAX_LINES; + StringBuilder yaml = new StringBuilder(); + for (String line : cut ? lines.subList(0, MAX_LINES) : lines) { + yaml.append(line).append('\n'); + } + answer.put("yaml", yaml.toString()); + answer.put("note", "An item of actions: (or of finally:). Each key with its type and description; write the" + + " values, not the <type>. Text values in quotes."); + if (cut) { + answer.put("cut", "the YAML is cut at " + MAX_LINES + " lines"); + } + if (!advanced.isEmpty()) { + answer.put("advancedKeys", new JsonArray(advanced)); + answer.put("advancedHint", "includeOptions=all shows the advanced keys too"); + } + if (!subs.isEmpty()) { + answer.put("actions", new JsonArray(subs)); + answer.put("actionHint", "docPage=" + String.join(":", path) + ":" + subs.get(0) + " gives the keys of one"); + } + return answer; + } + + /** The YAML of the keys of a node, each with its type and description, the advanced ones left out unless all. */ + private static void shape( + JsonNode node, String indent, int depth, List<String> lines, List<String> advanced, String path, + boolean all) { + Map<String, JsonNode> props = properties(node); + // the keys with the same schema, such as the methods of an HTTP request, are shown once + Map<String, List<String>> same = new LinkedHashMap<>(); + for (Map.Entry<String, JsonNode> e : props.entrySet()) { + String ref = refOf(e.getValue()); + String key = ref != null && !isActionRef(ref) ? ref : "key:" + e.getKey(); + same.computeIfAbsent(key, k -> new ArrayList<>()).add(e.getKey()); + } + for (List<String> keys : same.values()) { + String key = keys.get(0); + for (String p : PREFERRED) { + if (keys.contains(p)) { + key = p; + break; + } + } + JsonNode value = props.get(key); + if (!all && isAdvanced(value)) { + advanced.add(path + "." + key); + continue; + } + String others = null; + if (keys.size() > 1) { + List<String> rest = new ArrayList<>(keys); + rest.remove(key); + others = "or " + String.join(", ", rest); + } + String comment = comment(describe(value), others); + if (isActions(value)) { + lines.add(indent + key + ": [<test actions, as under actions:>]" + comment); + continue; + } + JsonNode items = items(value); + if (items != null) { + Map<String, JsonNode> itemProps = properties(items); + if (!itemProps.isEmpty() && depth > 0) { + lines.add(indent + key + ":" + comment); + int first = lines.size(); + shape(items, indent + " ", depth - 1, lines, advanced, path + "." + key + "[]", all); + if (lines.size() > first) { + String line = lines.get(first); + lines.set(first, indent + " - " + line.substring(indent.length() + 4)); + } + } else { + lines.add(indent + key + ": [<" + type(items) + ">]" + comment); + } + continue; + } + if (!properties(value).isEmpty() && depth > 0) { + lines.add(indent + key + ":" + comment); + shape(value, indent + " ", depth - 1, lines, advanced, path + "." + key, all); + } else { + lines.add(indent + key + ": <" + type(value) + ">" + comment); + } + } + } + + /** + * The path of an action in the schema, from its name as a model writes it (http:sendRequest, http sendRequest, + * http.sendRequest, camel: cli: run, "camel cli verify"), any case; null when there is no such action. + */ + static List<String> actionPath(String action) { + String[] words = action.trim().split("[\\s:./]+"); + JsonNode node = CitrusTestChecks.schema().model().at("/definitions/TestActions"); + List<String> path = new ArrayList<>(); + for (String w : words) { + if (w.isEmpty() || w.equals("-")) { + continue; + } + String found = null; + for (String key : properties(node).keySet()) { + if (key.equalsIgnoreCase(w)) { + found = key; + break; + } + } + if (found == null) { + return null; + } + path.add(found); + node = properties(node).get(found); + } + return path.isEmpty() ? null : path; + } + + private static JsonArray actionNames() { + JsonArray names = new JsonArray(); + CitrusTestChecks.schema().model().at("/definitions/TestActions/properties").propertyNames().forEach(names::add); + return names; + } + + /** The sub-actions of an action group (http: sendRequest, receiveResponse...; camel: cli...), from the catalog. */ + private static List<String> subActions(List<String> path) { + String prefix = String.join(":", path) + ":"; + List<String> answer = new ArrayList<>(); + for (String name : actionCatalog()) { + if (name.startsWith(prefix) && name.indexOf(':', prefix.length()) < 0) { + answer.add(name.substring(prefix.length())); + } + } + return answer; + } + + /** + * The names of the test actions in the action catalog of Citrus (http, http:sendRequest, camel:cli:run), which says + * which keys of an action are actions of their own; the schema does not. Empty when it cannot be read. + */ + static Set<String> actionCatalog() { + Set<String> names = actionNames; + if (names == null) { + names = new LinkedHashSet<>(); + try { + JsonNode catalog = JSON.readTree(CitrusTestChecks.read(CitrusTestChecks.version(), ACTION_CATALOG)); + catalog.propertyNames().forEach(names::add); + } catch (Exception e) { + // without it, an action is shown with its keys only + } + actionNames = names; + } + return names; + } + + /** The properties of a node, through its references and branches (the methods of sendRequest are in a branch). */ + private static Map<String, JsonNode> properties(JsonNode node) { + Map<String, JsonNode> answer = new LinkedHashMap<>(); + collect(node, answer, 0); + return answer; + } + + private static void collect(JsonNode node, Map<String, JsonNode> answer, int depth) { + node = deref(node); + if (node == null || !node.isObject() || depth > 8) { + return; + } + JsonNode props = node.path("properties"); + for (String key : props.propertyNames()) { + JsonNode v = props.get(key); + JsonNode had = answer.get(key); + if (had == null || !substantive(had) && substantive(v)) { + answer.put(key, v); + } + } + for (String composition : new String[] { "allOf", "anyOf", "oneOf" }) { + for (JsonNode branch : node.path(composition)) { + collect(branch, answer, depth + 1); + } + } + } + + private static boolean substantive(JsonNode node) { + return node != null && node.isObject() && !node.isEmpty(); + } + + /** The node a reference leads to; a reference to the test actions is kept, as they are not shown. */ + private static JsonNode deref(JsonNode node) { + for (int i = 0; i < 20 && node != null && node.has("$ref"); i++) { + String ref = node.get("$ref").asString(""); + if (isActionRef(ref) || !ref.startsWith("#")) { + return node; + } + node = CitrusTestChecks.schema().model().at(ref.substring(1)); + } + return node; + } + + /** The reference of a node, directly or as the first part of an allOf. */ + private static String refOf(JsonNode node) { + if (node == null) { + return null; + } + if (node.has("$ref")) { + return node.get("$ref").asString(""); + } + for (JsonNode part : node.path("allOf")) { + if (part.has("$ref")) { + return part.get("$ref").asString(""); + } + } + return null; + } + + private static boolean isActions(JsonNode node) { + if (isActionRef(refOf(node))) { + return true; + } + JsonNode items = node != null ? deref(node).path("items") : null; + return items != null && !items.isMissingNode() && isActionRef(refOf(items)); + } + + /** Whether a reference is to the test actions (Set.of does not take null). */ + private static boolean isActionRef(String ref) { + return ref != null && ACTION_REFS.contains(ref); + } + + /** The items of a list, or null when the node is not a list. */ + private static JsonNode items(JsonNode node) { + if (node == null) { + return null; + } + for (JsonNode n : List.of(node, deref(node))) { + if (n.has("items")) { + return n.get("items"); + } + for (JsonNode part : n.path("allOf")) { + JsonNode d = deref(part); + if (d.has("items")) { + return d.get("items"); + } + } + } + return null; + } + + /** A value of a node of the schema, from the node, its reference or the parts of an allOf. */ + private static JsonNode value(JsonNode node, String key) { + if (node == null) { + return null; + } + for (JsonNode n : List.of(node, deref(node))) { + if (n.has(key)) { + return n.get(key); + } + for (JsonNode part : n.path("allOf")) { + JsonNode v = part.has(key) ? part.get(key) : deref(part).get(key); + if (v != null) { + return v; + } + } + } + return null; + } + + private static String description(JsonNode node) { + JsonNode d = value(node, "description"); + return d != null ? d.asString("").strip() : ""; + } + + /** The description with the values it takes and its default. */ + private static String describe(JsonNode node) { + StringBuilder sb = new StringBuilder(description(node)); + JsonNode e = value(node, "enum"); + if (e != null && e.isArray()) { + List<String> values = new ArrayList<>(); + e.forEach(v -> values.add(v.asString(""))); + sb.append(sb.isEmpty() ? "" : " ").append("One of ").append(String.join(", ", values)).append('.'); + } + JsonNode def = value(node, "default"); + if (def != null && !def.isNull()) { + sb.append(sb.isEmpty() ? "" : " ").append("Default ").append(def.asString("")).append('.'); + } + return sb.toString(); + } + + private static String type(JsonNode node) { + JsonNode t = value(node, "type"); + return t != null && t.isString() ? t.asString("") : "value"; + } + + private static boolean isAdvanced(JsonNode node) { + JsonNode c = value(node, "$comment"); + return c != null && c.asString("").contains("group:advanced"); + } + + private static String comment(String description, String more) { + String text = description == null ? "" : description; + if (more != null) { + text = text.isEmpty() ? more : text + " (" + more + ")"; + } + return text.isEmpty() ? "" : " # " + text.replace('\n', ' '); + } +} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/org/apache/camel/dsl/jbang/core/commands/ai/citrus-test-sample.yaml b/dsl/camel-jbang/camel-jbang-core/src/main/resources/org/apache/camel/dsl/jbang/core/commands/ai/citrus-test-sample.yaml new file mode 100644 index 000000000000..e2bf7c54e7b0 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/org/apache/camel/dsl/jbang/core/commands/ai/citrus-test-sample.yaml @@ -0,0 +1,61 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# yaml-language-server: $schema=https://citrusframework.org/schema/json/testcase/citrus-testcase.json +name: greetings-test +description: Starts the integration, calls its HTTP API, and checks the answer and the log +variables: + - name: user + value: Ada +actions: + # start the integration from the project directory: the paths are relative to this file in test/ + - camel: + cli: + run: + integration: + name: "greetings" + file: "../greetings.camel.yaml" + systemProperties: + file: "../application.properties" + - camel: + cli: + verify: + integration: "greetings" + logMessage: "(greetings) started" + - http: + client: "http://localhost:8080" + sendRequest: + POST: + path: "/greetings" + contentType: "application/json" + body: + data: '{"name": "${user}"}' + - http: + client: "http://localhost:8080" + receiveResponse: + response: + status: "200" + validate: + - jsonPath: + - expression: "$.greeting" + value: "Hello ${user}" + # a request body from a file: body: {resource: {file: "../order.json"}} + - camel: + cli: + verify: + integration: "greetings" + logMessage: "Greeted ${user}" diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogDocsTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogDocsTest.java index 27dcb9391902..20ce6de6fa08 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogDocsTest.java +++ b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/CatalogDocsTest.java @@ -310,12 +310,37 @@ class CatalogDocsTest { } @Test - void docPageOfAnEipWithoutSectionsSaysSoAndNamesTheComponentPage() throws Exception { - // rest is the REST DSL (an EIP without a doc page of its own) and a component with one - JsonObject missing = catalogDoc(Map.of("name", "rest", "kind", "eip", "docPage", "Rest producer binding")); - assertTrue(missing.getString("error").contains("The EIP rest has no doc page with sections"), missing.toJson()); + void docPageOfAnEipWithoutSectionsSaysSo() throws Exception { + JsonObject missing = catalogDoc(Map.of("name", "otherwise", "kind", "eip", "docPage", "Usage")); + assertTrue(missing.getString("error").contains("The EIP otherwise has no doc page with sections"), + missing.toJson()); assertNull(missing.get("sections")); + } + + @Test + void docPageOfTheRestEipIsASectionOfTheRestDslPages() throws Exception { + // the rest EIP has no EIP page: a model asked for its Rest DSL section 12 times in a row (q4f benchmark) + JsonObject section = catalogDoc(Map.of("name", "rest", "kind", "eip", "docPage", "Rest DSL")); + assertEquals("Rest DSL", section.getString("section"), section.toJson()); + assertTrue(section.getString("source").startsWith("rest-dsl.adoc"), section.toJson()); + assertTrue(section.getString("doc").contains("- rest:"), "the YAML tab of the page"); + + JsonObject binding + = catalogDoc(Map.of("name", "post", "kind", "eip", "docPage", "Binding to POJOs using data formats")); + assertTrue(binding.getString("source").startsWith("rest-dsl-binding.adoc"), binding.toJson()); + + // the page names as a model writes them + JsonObject byPage = catalogDoc(Map.of("name", "rest-dsl", "docPage", "Returning an HTTP status code")); + assertEquals("Returning an HTTP status code", byPage.getString("section"), byPage.toJson()); + + JsonObject missing = catalogDoc(Map.of("name", "rest", "kind", "eip", "docPage", "main")); + assertTrue(missing.getString("error").contains("Rest DSL pages of the user manual"), missing.toJson()); + assertTrue(missing.getCollection("sections").contains("Rest DSL"), missing.toJson()); assertTrue(missing.getString("componentDoc").contains("kind=component"), missing.toJson()); + + JsonObject rest = catalogDoc(Map.of("name", "rest", "kind", "eip")); + assertTrue(rest.getString("restDslDoc").contains("Returning an HTTP status code"), rest.toJson()); + assertNull(catalogDoc(Map.of("name", "split", "kind", "eip")).get("restDslDoc")); } @Test diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/CitrusTestDocsTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/CitrusTestDocsTest.java new file mode 100644 index 000000000000..aa8036dec079 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/ai/CitrusTestDocsTest.java @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dsl.jbang.core.commands.ai; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.camel.catalog.DefaultCamelCatalog; +import org.apache.camel.dsl.yaml.validator.YamlTrees; +import org.apache.camel.util.json.JsonArray; +import org.apache.camel.util.json.JsonObject; +import org.apache.camel.util.json.Jsoner; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class CitrusTestDocsTest { + + private static JsonObject call(String tool, Map<String, String> args) throws Exception { + Object answer = ToolRegistry.execute(tool, new ToolContext(), new HashMap<>(args)); + return answer instanceof JsonObject o ? o : (JsonObject) Jsoner.deserialize(String.valueOf(answer)); + } + + private static List<String> strings(JsonObject o, String key) { + return ((JsonArray) o.get(key)).stream().map(String::valueOf).toList(); + } + + @Test + void theSampleIsAValidTest() { + assertThat(CitrusTestChecks.hasSchema()).as(CitrusTestChecks.schemaProblem()).isTrue(); + String sample = CitrusTestDocs.sample(); + assertThat(sample).contains("sendRequest:", "receiveResponse:", "camel:", "logMessage:"); + assertThat(CitrusTestChecks.validate(sample)).isEmpty(); + assertThat(YamlTrees.newMapper().readTree(sample).path("actions").size()).isGreaterThan(3); + } + + @Test + void namesOfTheDsl() { + assertThat(CitrusTestDocs.isCitrusTest("citrus-test")).isTrue(); + assertThat(CitrusTestDocs.isCitrusTest("citrus test yaml")).isTrue(); + assertThat(CitrusTestDocs.isCitrusTest("Citrus YAML DSL")).isTrue(); + assertThat(CitrusTestDocs.isCitrusTest("citrus")).as("the component").isFalse(); + assertThat(CitrusTestDocs.isCitrusTest("citrus-component")).isFalse(); + assertThat(CitrusTestDocs.isCitrusTest("kafka")).isFalse(); + assertThat(CitrusTestDocs.findsCitrusTest("citrus yaml test actions http")).isTrue(); + assertThat(CitrusTestDocs.findsCitrusTest("citrus")).isFalse(); + } + + @Test + void theDslWithItsActionsAndTheSample() throws Exception { + JsonObject doc = call("camel_catalog_doc", Map.of("name", "citrus-test")); + assertThat(doc.getString("kind")).isEqualTo("citrus-test"); + assertThat(doc.getString("placement")).contains("camel test run", "../"); + assertThat(doc.getString("sample")).isEqualTo(CitrusTestDocs.sample()); + assertThat(((JsonObject) doc.get("testKeys")).keySet()).contains("name", "actions", "variables"); + JsonObject http = ((JsonArray) doc.get("actions")).stream().map(JsonObject.class::cast) + .filter(a -> "http".equals(a.getString("name"))).findFirst().orElseThrow(); + assertThat(strings(http, "actions")).contains("sendRequest", "receiveResponse"); + } + + @Test + void anActionAsYaml() throws Exception { + JsonObject send = call("camel_catalog_doc", Map.of("name", "citrus-test", "docPage", "http:sendRequest")); + assertThat(send.getString("action")).isEqualTo("http:sendRequest"); + String yaml = send.getString("yaml"); + assertThat(yaml).startsWith("- http:").contains(" client: <string>", " sendRequest:", " POST:", + "path: <string>", "data: <string>", "resource:"); + assertThat(yaml).doesNotContain("receiveResponse").as("the other actions of the group"); + assertThat(strings(send, "advancedKeys")).contains("http.sendRequest.POST.accept"); + // the YAML is the shape of a test action, so its keys pass the schema + assertThat(YamlTrees.newMapper().readTree(yaml).isArray()).isTrue(); + + JsonObject all = call("camel_catalog_doc", + Map.of("name", "citrus-test", "docPage", "http sendRequest", "includeOptions", "all")); + assertThat(all.getString("yaml")).contains("accept: <string>"); + assertThat(all.get("advancedKeys")).isNull(); + + JsonObject run = call("camel_catalog_doc", Map.of("name", "citrus-test", "docPage", "camel cli run")); + assertThat(run.getString("action")).isEqualTo("camel:cli:run"); + assertThat(run.getString("yaml")).contains("- camel:", " cli:", " run:", "integration:", "file: <string>"); + + JsonObject group = call("camel_catalog_doc", Map.of("name", "citrus-test", "docPage", "camel")); + assertThat(strings(group, "actions")).contains("cli"); + + JsonObject unknown = call("camel_catalog_doc", Map.of("name", "citrus-test", "docPage", "assert-log")); + assertThat(unknown.getString("error")).contains("no test action 'assert-log'"); + assertThat(strings(unknown, "actions")).contains("http", "camel", "echo"); + } + + @Test + void whereAModelLooksForIt() throws Exception { + // the calls of the q4f benchmark (Dec 2026), which found nothing about writing a Citrus test + JsonObject component = call("camel_catalog_doc", Map.of("name", "citrus", "kind", "component")); + assertThat(component.getString("kind")).isEqualTo("component"); + assertThat(component.getString("citrusTest")).contains("name=citrus-test"); + + JsonObject action = call("camel_catalog_doc", + Map.of("name", "citrus", "kind", "component", "docPage", "http:receiveResponse")); + assertThat(action.getString("action")).isEqualTo("http:receiveResponse"); + JsonObject section = call("camel_catalog_doc", + Map.of("name", "citrus", "kind", "component", "docPage", "Path validation")); + assertThat(section.getString("section")).isEqualTo("Path validation"); + + JsonObject byKind = call("camel_catalog_doc", Map.of("name", "http", "kind", "citrus-test")); + assertThat(byKind.getString("action")).isEqualTo("http"); + + for (String term : List.of("citrus test yaml", "citrus yaml test dsl http send", "citrus test")) { + JsonArray matches = (JsonArray) CatalogDocs.find(new DefaultCamelCatalog(), term, null, 10).get("matches"); + assertThat(((JsonObject) matches.get(0)).getString("name")).as(term).isEqualTo("citrus-test"); + } + JsonArray component2 = (JsonArray) CatalogDocs.find(new DefaultCamelCatalog(), "citrus", null, 10).get("matches"); + assertThat(component2.stream().map(o -> ((JsonObject) o).getString("name"))).doesNotContain("citrus-test"); + + JsonObject sample = call("camel_catalog_sample", Map.of("name", "citrus test")); + assertThat(sample.getString("kind")).isEqualTo("citrus-test"); + assertThat(((JsonObject) ((JsonArray) sample.get("samples")).get(0)).getString("yaml")) + .isEqualTo(CitrusTestDocs.sample()); + JsonObject componentSample = call("camel_catalog_sample", Map.of("name", "citrus")); + assertThat(componentSample.getString("citrusTest")).contains("name=citrus-test"); + } +} diff --git a/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java b/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java index a055443fe8bf..61faf7080797 100644 --- a/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java +++ b/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/AuthoringTools.java @@ -95,11 +95,13 @@ public class AuthoringTools { + "unknown or invalid options, missing path. An EIP alias such as fan-out or dedup finds the EIP. " + "includeHeaders=true adds the message headers of a component, includeDoc=true the AsciiDoc " + "page, docPage one of its sections. A component option carries its application.properties " - + "key (configKey); a full key (camel.server.port, camel.jbang.dependencies) lists its group.") + + "key (configKey); a full key (camel.server.port, camel.jbang.dependencies) lists its group. " + + "name=citrus-test is the Citrus YAML test DSL (test/*.citrus.it.yaml), docPage one of its " + + "actions; name=rest-dsl the Rest DSL pages of the user manual.") @MetaField(prefix = "camel.apache.org/", name = "deterministic", type = MetaField.Type.BOOLEAN, value = "true") public JsonObject camel_catalog_doc( McpConnection connection, - @ToolArg(description = "Name, e.g. kafka, json (a data format by its YAML name or artifact), simple, timer, choice, split, Exchange", + @ToolArg(description = "Name, e.g. kafka, json (a data format by its YAML name or artifact), simple, timer, choice, split, Exchange, rest-dsl, citrus-test", required = false) String name, @ToolArg(description = "Endpoint URI to check, e.g. kafka:orders?brokers=host:9092", required = false) String endpoint, diff --git a/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/CatalogTools.java b/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/CatalogTools.java index a7a5788186cf..30dd4f0a7f22 100644 --- a/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/CatalogTools.java +++ b/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/CatalogTools.java @@ -16,6 +16,7 @@ */ package org.apache.camel.dsl.jbang.core.commands.mcp; +import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -27,6 +28,8 @@ import io.quarkiverse.mcp.server.ToolArg; import io.quarkiverse.mcp.server.ToolCallException; import io.quarkiverse.mcp.server.ToolGuardrails; import org.apache.camel.catalog.CamelCatalog; +import org.apache.camel.dsl.jbang.core.commands.ai.CatalogDocs; +import org.apache.camel.dsl.jbang.core.commands.ai.CitrusTestDocs; /** * MCP tool listing the documentation pages of the Camel catalog. The documentation of the catalog artifacts themselves @@ -60,10 +63,11 @@ public class CatalogTools { try { CamelCatalog cat = catalogService.loadCatalog(null, camelVersion, null); - List<String> names = cat.findDocNames(); - if (names == null) { - names = List.of(); - } + List<String> names = cat.findDocNames() != null ? new ArrayList<>(cat.findDocNames()) : new ArrayList<>(); + // what camel_catalog_doc answers that is not a page of the catalog: the Rest DSL pages of the user manual, + // and the Citrus YAML test DSL (a model asked for filter rest-dsl and citrus, and found neither) + names.add(CatalogDocs.REST_DSL_NAME); + names.add(CitrusTestDocs.NAME); if (filter != null && !filter.isBlank()) { String lowerFilter = filter.toLowerCase(); diff --git a/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/McpServerInstructions.java b/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/McpServerInstructions.java index a2d777aa778d..1b6be8dbc846 100644 --- a/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/McpServerInstructions.java +++ b/dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/McpServerInstructions.java @@ -79,8 +79,9 @@ public class McpServerInstructions implements InitialResponseInfo { - which component for a protocol or product: camel_catalog_find - a validated YAML sample and where it goes: camel_catalog_sample - complete example projects: camel_catalog_examples - - a sample Citrus test (test/<name>.citrus.it.yaml, run with camel test run): the test/ file of an \ - example, camel_catalog_examples with filter citrus + - a Citrus test (test/<name>.citrus.it.yaml, run with camel test run): camel_catalog_doc \ + name=citrus-test (its actions and a sample; docPage=http:sendRequest for one action), and the test/ \ + file of an example, camel_catalog_examples with filter citrus - Maven coordinates (the --dep value) of a Java class: camel_dependency_for_class - validate YAML/Java/XML/properties before writing: camel_validate_source - run an integration: camel_run; its log: camel_get_log; its errors: camel_get_errors
