Skip to content

Commit 4cd5cdd

Browse files
adityamparikhclaude
andcommitted
test(collection): use MCP service beans instead of raw SolrJ calls
Replace direct SolrClient calls with the actual Spring service beans: - CollectionService.createCollection() to create the test collection - IndexingService.indexJsonDocuments() to index 50 test documents - SearchService.search() for warm-up queries and verification This exercises the real MCP tool code paths (JSON document creation, batch indexing, query building) rather than bypassing them with raw SolrJ. Also adds testSearchVerifiesIndexedDocuments() to confirm SearchService returns correct results with filter queries. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: adityamparikh <aditya.m.parikh@gmail.com>
1 parent 15b86ba commit 4cd5cdd

1 file changed

Lines changed: 47 additions & 20 deletions

File tree

src/test/java/org/apache/solr/mcp/server/collection/CollectionServiceIntegrationTest.java

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818

1919
import static org.junit.jupiter.api.Assertions.*;
2020

21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import java.util.ArrayList;
23+
import java.util.LinkedHashMap;
2124
import java.util.List;
22-
import org.apache.solr.client.solrj.SolrClient;
23-
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
24-
import org.apache.solr.client.solrj.request.SolrQuery;
25-
import org.apache.solr.common.SolrInputDocument;
25+
import java.util.Map;
2626
import org.apache.solr.mcp.server.TestcontainersConfiguration;
27+
import org.apache.solr.mcp.server.indexing.IndexingService;
28+
import org.apache.solr.mcp.server.search.SearchResponse;
29+
import org.apache.solr.mcp.server.search.SearchService;
2730
import org.junit.jupiter.api.BeforeAll;
2831
import org.junit.jupiter.api.Test;
2932
import org.junit.jupiter.api.TestInstance;
@@ -50,32 +53,43 @@ class CollectionServiceIntegrationTest {
5053
private CollectionService collectionService;
5154

5255
@Autowired
53-
private SolrClient solrClient;
56+
private IndexingService indexingService;
57+
58+
@Autowired
59+
private SearchService searchService;
60+
61+
@Autowired
62+
private ObjectMapper objectMapper;
5463

5564
@BeforeAll
5665
void setupCollectionWithData() throws Exception {
57-
// 1. Create collection
58-
CollectionAdminRequest.createCollection(TEST_COLLECTION, "_default", 1, 1).process(solrClient);
66+
// 1. Create collection via CollectionService MCP tool
67+
CollectionCreationResult created = collectionService.createCollection(TEST_COLLECTION, null, null, null);
68+
assertTrue(created.success(), "Collection creation should succeed: " + created.message());
5969
log.debug("Test collection created: {}", TEST_COLLECTION);
6070

61-
// 2. Index documents so metrics have real data
71+
// 2. Index documents via IndexingService MCP tool
72+
List<Map<String, Object>> docs = new ArrayList<>();
6273
for (int i = 0; i < DOC_COUNT; i++) {
63-
SolrInputDocument doc = new SolrInputDocument();
64-
doc.addField("id", "doc-" + i);
65-
doc.addField("title_s", "Document " + i);
66-
doc.addField("category_s", (i % 2 == 0) ? "even" : "odd");
67-
doc.addField("count_i", i);
68-
solrClient.add(TEST_COLLECTION, doc);
74+
Map<String, Object> doc = new LinkedHashMap<>();
75+
doc.put("id", "doc-" + i);
76+
doc.put("title_s", "Document " + i);
77+
doc.put("category_s", (i % 2 == 0) ? "even" : "odd");
78+
doc.put("count_i", i);
79+
docs.add(doc);
6980
}
70-
solrClient.commit(TEST_COLLECTION);
81+
String json = objectMapper.writeValueAsString(docs);
82+
indexingService.indexJsonDocuments(TEST_COLLECTION, json);
83+
log.debug("Indexed {} documents via IndexingService", DOC_COUNT);
7184

72-
// 3. Run several queries to populate query result cache and handler stats
85+
// 3. Run searches via SearchService MCP tool to populate caches and handler
86+
// stats
7387
for (int i = 0; i < 5; i++) {
74-
solrClient.query(TEST_COLLECTION, new SolrQuery("*:*").setRows(10));
75-
solrClient.query(TEST_COLLECTION, new SolrQuery("title_s:Document").setRows(5));
76-
solrClient.query(TEST_COLLECTION, new SolrQuery("*:*").addFilterQuery("category_s:even").setRows(10));
88+
searchService.search(TEST_COLLECTION, "*:*", null, null, null, 0, 10);
89+
searchService.search(TEST_COLLECTION, "title_s:Document", null, null, null, 0, 5);
90+
searchService.search(TEST_COLLECTION, "*:*", List.of("category_s:even"), null, null, 0, 10);
7791
}
78-
log.debug("Indexed {} documents and ran warm-up queries", DOC_COUNT);
92+
log.debug("Ran warm-up queries via SearchService");
7993
}
8094

8195
@Test
@@ -247,4 +261,17 @@ void createCollection_createsAndListable() throws Exception {
247261
|| collections.stream().anyMatch(col -> col.startsWith(name + "_shard"));
248262
assertTrue(exists, "Newly created collection should appear in list (found: " + collections + ")");
249263
}
264+
265+
@Test
266+
void testSearchVerifiesIndexedDocuments() throws Exception {
267+
// Verify the documents we indexed are actually searchable via SearchService
268+
SearchResponse all = searchService.search(TEST_COLLECTION, "*:*", null, null, null, 0, DOC_COUNT);
269+
assertEquals(DOC_COUNT, all.numFound(), "Should find all indexed documents");
270+
assertEquals(DOC_COUNT, all.documents().size(), "Should return all documents in single page");
271+
272+
// Filter search should return only even-category docs
273+
SearchResponse evens = searchService.search(TEST_COLLECTION, "*:*", List.of("category_s:even"), null, null, 0,
274+
DOC_COUNT);
275+
assertEquals(DOC_COUNT / 2, evens.numFound(), "Should find 25 even-category documents");
276+
}
250277
}

0 commit comments

Comments
 (0)