gnodet-bot commented on code in PR #26605: URL: https://github.com/apache/camel/pull/26605#discussion_r4050693356
########## dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/ExampleToolsTest.java: ########## @@ -0,0 +1,70 @@ +/* + * 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.mcp; + +import java.util.List; + +import org.apache.camel.dsl.jbang.core.common.ExampleHelper; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class ExampleToolsTest { + + @Test + void listsTheWholeLadderInReadingOrderWithoutArguments() { + ExampleTools.ExampleListResult all = new ExampleTools().camel_catalog_examples(null, null, null); + + assertThat(all.count()).isEqualTo(all.total()); + assertThat(all.groups()).isNotEmpty(); + List<String> levels = all.groups().stream().map(ExampleTools.GroupInfo::level).toList(); + assertThat(levels).isSubsetOf(ExampleHelper.getGroupOrder()); + assertThat(levels).isSortedAccordingTo((a, b) -> Integer.compare( + ExampleHelper.getGroupOrder().indexOf(a), ExampleHelper.getGroupOrder().indexOf(b))); + assertThat(levels.get(0)).isEqualTo("quick-start"); + ExampleTools.GroupInfo first = all.groups().get(0); + assertThat(first.title()).isEqualTo(ExampleHelper.getGroupTitle("quick-start")); + assertThat(first.intro()).isNotBlank(); + + // the examples follow the groups in order + List<String> exampleLevels = all.examples().stream().map(ExampleTools.ExampleInfo::level).distinct().toList(); + assertThat(exampleLevels).isEqualTo(levels); + assertThat(all.examples().stream().map(ExampleTools.ExampleInfo::name)).contains("quick-start/timer-log"); + } + + @Test + void filtersOneGroupAndReturnsWhatTheExamplesTeach() { + ExampleTools.ExampleListResult run = new ExampleTools().camel_catalog_examples(null, "run", null); + + assertThat(run.groups()).hasSize(1); + assertThat(run.groups().get(0).level()).isEqualTo("run"); + assertThat(run.groups().get(0).count()).isEqualTo(run.total()); + assertThat(run.examples()).allMatch(e -> "run".equals(e.level())); + assertThat(run.examples()).isSortedAccordingTo((a, b) -> Integer.compare(a.order(), b.order())); Review Comment: ⚠️ **NPE risk in sort comparator:** `Integer.compare(a.order(), b.order())` auto-unboxes both `Integer` arguments. `ExampleInfo.order()` is declared as `Integer` and `toExampleInfo` maps `getOrder() == Integer.MAX_VALUE → null`, so any example in the `run` group that carries no `order` metadata will produce a `null` here and throw a `NullPointerException` at test time. The test happens to pass today because every current `run` example has an explicit `order` field. Adding a new example without one silently breaks the test suite. Use a null-safe comparator: ```suggestion assertThat(run.examples()).isSortedAccordingTo( Comparator.comparingInt(e -> e.order() != null ? e.order() : Integer.MAX_VALUE)); ``` ########## dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/ExampleToolsTest.java: ########## @@ -0,0 +1,70 @@ +/* + * 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.mcp; + +import java.util.List; + +import org.apache.camel.dsl.jbang.core.common.ExampleHelper; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class ExampleToolsTest { + + @Test + void listsTheWholeLadderInReadingOrderWithoutArguments() { + ExampleTools.ExampleListResult all = new ExampleTools().camel_catalog_examples(null, null, null); + + assertThat(all.count()).isEqualTo(all.total()); Review Comment: ⚠️ **Fragile implicit assumption:** `all.count() == all.total()` holds only while the total number of examples stays below the default limit of 50. The PR body itself mentions ~40 examples today, so this passes now — but when the catalog grows past 50 entries with no other code change, this assertion will fail and the breakage will be confusing (the code will be correct, only the test assumption will be wrong). Make the intent explicit: ```suggestion assertThat(all.count()).isEqualTo(all.total()); // true while total examples < default limit (50) ``` Or, better, pass an explicit limit larger than the catalog and assert equality against that: ```java ExampleTools.ExampleListResult all = new ExampleTools().camel_catalog_examples(null, null, 1000); assertThat(all.count()).isEqualTo(all.total()); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
