|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Universal Permissive License v 1.0 as shown at |
| 5 | + * https://oss.oracle.com/licenses/upl. |
| 6 | + */ |
| 7 | +package com.oracle.coherence.rag.api; |
| 8 | + |
| 9 | +import com.oracle.bedrock.options.Timeout; |
| 10 | +import com.oracle.bedrock.testsupport.deferred.Eventually; |
| 11 | + |
| 12 | +import io.helidon.microprofile.testing.junit5.HelidonTest; |
| 13 | + |
| 14 | +import jakarta.inject.Inject; |
| 15 | +import jakarta.ws.rs.client.Entity; |
| 16 | +import jakarta.ws.rs.client.WebTarget; |
| 17 | +import jakarta.ws.rs.core.MediaType; |
| 18 | +import jakarta.ws.rs.core.Response; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeAll; |
| 24 | +import org.junit.jupiter.api.MethodOrderer; |
| 25 | +import org.junit.jupiter.api.Order; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.TestInstance; |
| 28 | +import org.junit.jupiter.api.TestMethodOrder; |
| 29 | + |
| 30 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | +import static org.hamcrest.Matchers.anyOf; |
| 32 | +import static org.hamcrest.Matchers.containsString; |
| 33 | +import static org.hamcrest.Matchers.is; |
| 34 | + |
| 35 | +@SuppressWarnings({"NewClassNamingConvention", "CdiInjectionPointsInspection"}) |
| 36 | +@HelidonTest |
| 37 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 38 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 39 | +class KbIT |
| 40 | + { |
| 41 | + |
| 42 | + @Inject |
| 43 | + WebTarget target; |
| 44 | + |
| 45 | + private final String store = "it-store"; |
| 46 | + |
| 47 | + private String docUri; |
| 48 | + |
| 49 | + @Inject |
| 50 | + Kb kb; |
| 51 | + |
| 52 | + @BeforeAll |
| 53 | + static void setUp() |
| 54 | + { |
| 55 | + System.setProperty("coherence.cacheconfig", "coherence-rag-cache-config.xml"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + @Order(1) |
| 60 | + void setUpStoreAndIngest() throws Exception |
| 61 | + { |
| 62 | + // configure store with local ONNX embedding model |
| 63 | + String cfg = "{\"embeddingModel\":\"-/all-MiniLM-L6-v2\",\"normalizeEmbeddings\":true,\"chunkSize\":512,\"chunkOverlap\":64}"; |
| 64 | + try (Response putCfg = target.path("api/kb/config/" + store) |
| 65 | + .request() |
| 66 | + .put(Entity.entity(cfg, MediaType.APPLICATION_JSON_TYPE))) |
| 67 | + { |
| 68 | + assertThat(putCfg.getStatus(), is(204)); |
| 69 | + } |
| 70 | + |
| 71 | + // compute absolute file URI to test PDF |
| 72 | + File pdf = new File("src/test/resources/database-release-notes.pdf"); |
| 73 | + assertThat("Test PDF must exist", pdf.exists(), is(true)); |
| 74 | + docUri = pdf.toURI().toString(); |
| 75 | + |
| 76 | + // import document via docs endpoint |
| 77 | + String importBody = "[\"" + docUri + "\"]"; |
| 78 | + try (Response imp = target.path("api/kb/" + store + "/docs") |
| 79 | + .request() |
| 80 | + .post(Entity.entity(importBody, MediaType.APPLICATION_JSON_TYPE))) |
| 81 | + { |
| 82 | + assertThat(imp.getStatus(), is(204)); |
| 83 | + } |
| 84 | + |
| 85 | + // wait until chunking and embedding complete |
| 86 | + Eventually.assertDeferred(() -> isDocIngested(docUri, 70), is(true), Timeout.after(5, TimeUnit.MINUTES)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + @Order(2) |
| 91 | + void testStoreList() |
| 92 | + { |
| 93 | + Response list = target.path("api/kb").request().get(); |
| 94 | + assertThat(list.getStatus(), is(200)); |
| 95 | + assertThat(list.readEntity(String.class), containsString(store)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + @Order(3) |
| 100 | + void testGetStoreConfig() |
| 101 | + { |
| 102 | + Response getCfg = target.path("api/kb/config/" + store).request(MediaType.APPLICATION_JSON_TYPE).get(); |
| 103 | + assertThat(getCfg.getStatus(), is(200)); |
| 104 | + assertThat(getCfg.readEntity(String.class), containsString("all-MiniLM-L6-v2")); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @Order(4) |
| 109 | + void testGetChunks() |
| 110 | + { |
| 111 | + Response chunks = target.path("api/kb/" + store + "/chunks") |
| 112 | + .queryParam("docId", docUri) |
| 113 | + .request(MediaType.APPLICATION_JSON_TYPE) |
| 114 | + .get(); |
| 115 | + assertThat(chunks.getStatus(), is(200)); |
| 116 | + assertThat(chunks.readEntity(String.class), containsString("chunks")); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + @Order(5) |
| 121 | + void testCreateAndRemoveIndex() |
| 122 | + { |
| 123 | + String hnsw = "{\"type\":\"HNSW\",\"parameters\":{\"m\":16,\"efConstruction\":200,\"efSearch\":64}}"; |
| 124 | + try (Response createIndex = target.path("api/kb/" + store + "/index") |
| 125 | + .request() |
| 126 | + .post(Entity.entity(hnsw, MediaType.APPLICATION_JSON_TYPE))) |
| 127 | + { |
| 128 | + assertThat(createIndex.getStatus(), is(202)); |
| 129 | + } |
| 130 | + |
| 131 | + try (Response delIdx = target.path("api/kb/" + store + "/index").request().delete()) |
| 132 | + { |
| 133 | + assertThat(delIdx.getStatus(), anyOf(is(202), is(304))); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + @Order(6) |
| 139 | + void testVectorOnlySearch() |
| 140 | + { |
| 141 | + String body = "{\"query\":\"release notes\",\"maxResults\":5,\"minScore\":0.0}"; |
| 142 | + try (Response res = target.path("api/kb/search") |
| 143 | + .request(MediaType.APPLICATION_JSON_TYPE) |
| 144 | + .post(Entity.entity(body, MediaType.APPLICATION_JSON_TYPE))) |
| 145 | + { |
| 146 | + assertThat(res.getStatus(), is(200)); |
| 147 | + assertThat(res.readEntity(String.class), containsString("results")); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + @Order(7) |
| 153 | + void testHybridSearch() |
| 154 | + { |
| 155 | + String body = "{\"query\":\"release notes\",\"maxResults\":5,\"minScore\":0.0,\"fullTextWeight\":0.5}"; |
| 156 | + try (Response res = target.path("api/kb/search") |
| 157 | + .request(MediaType.APPLICATION_JSON_TYPE) |
| 158 | + .post(Entity.entity(body, MediaType.APPLICATION_JSON_TYPE))) |
| 159 | + { |
| 160 | + assertThat(res.getStatus(), is(200)); |
| 161 | + assertThat(res.readEntity(String.class), containsString("results")); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // ---- helpers --------------------------------------------------------- |
| 166 | + |
| 167 | + private boolean isDocIngested(String docId, int cExpectedChunks) |
| 168 | + { |
| 169 | + var mapChunks = kb.store(store).getChunks(docId); |
| 170 | + return mapChunks != null && mapChunks.size() >= cExpectedChunks; |
| 171 | + } |
| 172 | + } |
| 173 | + |
0 commit comments