gnodet-bot commented on code in PR #28571: URL: https://github.com/apache/camel/pull/28571#discussion_r4389582978
########## components/camel-ai/camel-langchain4j-ingest/src/test/java/org/apache/camel/component/langchain4j/ingest/IngestIdFilterTest.java: ########## @@ -0,0 +1,64 @@ +/* + * 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.component.langchain4j.ingest; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The includeId/excludeId semantics on their own; {@link LangChain4jIngestFilterTest} covers them in the producer. + */ +class IngestIdFilterTest { + + /** An all-blank includeId accepts every id, instead of rejecting every id for lack of a matching pattern. */ + @Test + void noPatternsAcceptEveryId() { + assertThat(IngestIdFilter.of(null, null).accepts("docs/guide.md")).isTrue(); + assertThat(IngestIdFilter.of("", " ").accepts("docs/guide.md")).isTrue(); + assertThat(IngestIdFilter.of(" , ,", ",").accepts("docs/guide.md")).isTrue(); + } + + @Test + void onlyIdsMatchingAnIncludePatternPass() { + // blank entries and the spaces around a pattern are ignored + IngestIdFilter filter = IngestIdFilter.of(" docs/** , ,*.md", null); + + assertThat(filter.accepts("docs/guide.md")).isTrue(); + assertThat(filter.accepts("notes.md")).isTrue(); + assertThat(filter.accepts("notes.txt")).isFalse(); + } + + @Test + void exclusionWinsOverInclusion() { + IngestIdFilter filter = IngestIdFilter.of("docs/**", "**/draft-*"); + + assertThat(filter.accepts("docs/guide.md")).isTrue(); + assertThat(filter.accepts("docs/draft-plan.md")).isFalse(); + // without includeId, every id not excluded passes + assertThat(IngestIdFilter.of(null, "**/draft-*").accepts("notes.txt")).isTrue(); + } + + @Test + void matchingIsCaseSensitiveAndNeedsTheSameLeadingSlash() { + IngestIdFilter filter = IngestIdFilter.of("docs/**", null); + + assertThat(filter.accepts("Docs/guide.md")).isFalse(); + assertThat(filter.accepts("/docs/guide.md")).isFalse(); + assertThat(IngestIdFilter.of("/docs/**", null).accepts("/docs/guide.md")).isTrue(); + } +} Review Comment: 💡 Consider adding a test for `accepts(null)` to pin the null contract: ```java @Test void nullIdIsRejectedWhenPatternsAreConfigured() { assertThat(IngestIdFilter.of("docs/**", null).accepts(null)).isFalse(); assertThat(IngestIdFilter.of(null, "docs/**").accepts(null)).isFalse(); // no patterns → null passes through (no filter configured) assertThat(IngestIdFilter.of(null, null).accepts(null)).isTrue(); } ``` ########## components/camel-ai/camel-langchain4j-ingest/src/main/java/org/apache/camel/component/langchain4j/ingest/IngestIdFilter.java: ########## @@ -0,0 +1,66 @@ +/* + * 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.component.langchain4j.ingest; + +import java.util.Arrays; + +import org.apache.camel.util.AntPathMatcher; + +/** + * The {@code includeId}/{@code excludeId} document id filter the producer applies. Code that sees the document id + * before the producer, such as a route that parses documents first, can apply the same filter, so a filtered document + * is never parsed. + */ +public final class IngestIdFilter { + + private final String[] includeIds; + private final String[] excludeIds; + + private IngestIdFilter(String[] includeIds, String[] excludeIds) { + this.includeIds = includeIds; + this.excludeIds = excludeIds; + } + + /** + * The filter for the given option values: comma-separated Ant-style patterns, matched case-sensitively; null or + * blank means no patterns. + */ + public static IngestIdFilter of(String includeId, String excludeId) { + return new IngestIdFilter(parsePatterns(includeId), parsePatterns(excludeId)); + } + + /** Exclusion wins over inclusion; with includeId set, only matching ids pass. */ + public boolean accepts(String documentId) { + if (AntPathMatcher.INSTANCE.anyMatch(excludeIds, documentId)) { + return false; + } + return includeIds == null || AntPathMatcher.INSTANCE.anyMatch(includeIds, documentId); Review Comment: ⚠️ **NPE when `documentId` is `null`**: `AntPathMatcher.anyMatch` fast-returns `false` when `patterns == null`, but when patterns are set it delegates to `doMatch` which calls `path.startsWith(...)` — NPE on a null path. Now that this is public API explicitly documented for external callers, a null guard is needed: ```suggestion public boolean accepts(String documentId) { if (documentId == null) { return false; } if (AntPathMatcher.INSTANCE.anyMatch(excludeIds, documentId)) { ``` -- 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]
