@@ -165,4 +165,106 @@ private String getDefaultTemplatePromptText(int keywordCount, String documentCon
165165 return prompt .getContents ();
166166 }
167167
168+ @ Test
169+ void testApplyWithEmptyDocumentsList () {
170+ List <Document > emptyDocuments = List .of ();
171+ KeywordMetadataEnricher keywordMetadataEnricher = new KeywordMetadataEnricher (chatModel , 3 );
172+
173+ keywordMetadataEnricher .apply (emptyDocuments );
174+
175+ verify (chatModel , never ()).call (any (Prompt .class ));
176+ }
177+
178+ @ Test
179+ void testApplyWithSingleDocument () {
180+ List <Document > documents = List .of (new Document ("single content" ));
181+ given (chatModel .call (any (Prompt .class ))).willReturn (new ChatResponse (
182+ List .of (new Generation (new AssistantMessage ("single, keyword, test, document, content" )))));
183+
184+ KeywordMetadataEnricher keywordMetadataEnricher = new KeywordMetadataEnricher (chatModel , 5 );
185+ keywordMetadataEnricher .apply (documents );
186+
187+ verify (chatModel , times (1 )).call (promptCaptor .capture ());
188+ assertThat (documents .get (0 ).getMetadata ()).containsEntry (EXCERPT_KEYWORDS_METADATA_KEY ,
189+ "single, keyword, test, document, content" );
190+ }
191+
192+ @ Test
193+ void testApplyWithDocumentContainingExistingMetadata () {
194+ Document document = new Document ("content with existing metadata" );
195+ document .getMetadata ().put ("existing_key" , "existing_value" );
196+ List <Document > documents = List .of (document );
197+ given (chatModel .call (any (Prompt .class )))
198+ .willReturn (new ChatResponse (List .of (new Generation (new AssistantMessage ("new, keywords" )))));
199+
200+ KeywordMetadataEnricher keywordMetadataEnricher = new KeywordMetadataEnricher (chatModel , 2 );
201+ keywordMetadataEnricher .apply (documents );
202+
203+ assertThat (documents .get (0 ).getMetadata ()).containsEntry ("existing_key" , "existing_value" );
204+ assertThat (documents .get (0 ).getMetadata ()).containsEntry (EXCERPT_KEYWORDS_METADATA_KEY , "new, keywords" );
205+ }
206+
207+ @ Test
208+ void testApplyWithEmptyStringResponse () {
209+ List <Document > documents = List .of (new Document ("content" ));
210+ given (chatModel .call (any (Prompt .class )))
211+ .willReturn (new ChatResponse (List .of (new Generation (new AssistantMessage ("" )))));
212+
213+ KeywordMetadataEnricher keywordMetadataEnricher = new KeywordMetadataEnricher (chatModel , 3 );
214+ keywordMetadataEnricher .apply (documents );
215+
216+ assertThat (documents .get (0 ).getMetadata ()).containsEntry (EXCERPT_KEYWORDS_METADATA_KEY , "" );
217+ }
218+
219+ @ Test
220+ void testApplyWithWhitespaceOnlyResponse () {
221+ List <Document > documents = List .of (new Document ("content" ));
222+ given (chatModel .call (any (Prompt .class )))
223+ .willReturn (new ChatResponse (List .of (new Generation (new AssistantMessage (" \n \t " )))));
224+
225+ KeywordMetadataEnricher keywordMetadataEnricher = new KeywordMetadataEnricher (chatModel , 3 );
226+ keywordMetadataEnricher .apply (documents );
227+
228+ assertThat (documents .get (0 ).getMetadata ()).containsEntry (EXCERPT_KEYWORDS_METADATA_KEY , " \n \t " );
229+ }
230+
231+ @ Test
232+ void testApplyOverwritesExistingKeywords () {
233+ Document document = new Document ("content" );
234+ document .getMetadata ().put (EXCERPT_KEYWORDS_METADATA_KEY , "old, keywords" );
235+ List <Document > documents = List .of (document );
236+ given (chatModel .call (any (Prompt .class )))
237+ .willReturn (new ChatResponse (List .of (new Generation (new AssistantMessage ("new, keywords" )))));
238+
239+ KeywordMetadataEnricher keywordMetadataEnricher = new KeywordMetadataEnricher (chatModel , 2 );
240+ keywordMetadataEnricher .apply (documents );
241+
242+ assertThat (documents .get (0 ).getMetadata ()).containsEntry (EXCERPT_KEYWORDS_METADATA_KEY , "new, keywords" );
243+ }
244+
245+ @ Test
246+ void testBuilderWithBothKeywordCountAndTemplate () {
247+ PromptTemplate customTemplate = new PromptTemplate (CUSTOM_TEMPLATE );
248+
249+ KeywordMetadataEnricher enricher = builder (chatModel ).keywordCount (5 ).keywordsTemplate (customTemplate ).build ();
250+
251+ assertThat (enricher .getKeywordsTemplate ()).isEqualTo (customTemplate );
252+ }
253+
254+ @ Test
255+ void testApplyWithSpecialCharactersInContent () {
256+ List <Document > documents = List .of (new Document ("Content with special chars: @#$%^&*()" ));
257+ given (chatModel .call (any (Prompt .class ))).willReturn (
258+ new ChatResponse (List .of (new Generation (new AssistantMessage ("special, characters, content" )))));
259+
260+ KeywordMetadataEnricher keywordMetadataEnricher = new KeywordMetadataEnricher (chatModel , 3 );
261+ keywordMetadataEnricher .apply (documents );
262+
263+ verify (chatModel , times (1 )).call (promptCaptor .capture ());
264+ assertThat (promptCaptor .getValue ().getUserMessage ().getText ())
265+ .contains ("Content with special chars: @#$%^&*()" );
266+ assertThat (documents .get (0 ).getMetadata ()).containsEntry (EXCERPT_KEYWORDS_METADATA_KEY ,
267+ "special, characters, content" );
268+ }
269+
168270}
0 commit comments