|
| 1 | +/* |
| 2 | + * Copyright 2023-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.vectorstore.gemfire.autoconfigure; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.databind.JsonNode; |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import com.github.dockerjava.api.model.ExposedPort; |
| 25 | +import com.github.dockerjava.api.model.PortBinding; |
| 26 | +import com.github.dockerjava.api.model.Ports; |
| 27 | +import com.vmware.gemfire.testcontainers.GemFireCluster; |
| 28 | +import io.micrometer.observation.tck.TestObservationRegistry; |
| 29 | +import org.junit.jupiter.api.AfterAll; |
| 30 | +import org.junit.jupiter.api.Assertions; |
| 31 | +import org.junit.jupiter.api.BeforeAll; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | + |
| 34 | +import org.springframework.ai.embedding.EmbeddingModel; |
| 35 | +import org.springframework.ai.transformers.TransformersEmbeddingModel; |
| 36 | +import org.springframework.ai.vectorstore.gemfire.GemFireVectorStore; |
| 37 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 38 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 39 | +import org.springframework.context.annotation.Bean; |
| 40 | +import org.springframework.context.annotation.Configuration; |
| 41 | + |
| 42 | +import static org.assertj.core.api.Assertions.assertThat; |
| 43 | + |
| 44 | +/** |
| 45 | + * @author Geet Rawat |
| 46 | + * @author Christian Tzolov |
| 47 | + * @author Thomas Vitale |
| 48 | + */ |
| 49 | +class GemFireVectorStoreAutoConfigurationAuthenticationIT { |
| 50 | + |
| 51 | + private static final String INDEX_NAME = "spring-ai-index"; |
| 52 | + |
| 53 | + private static final int BEAM_WIDTH = 50; |
| 54 | + |
| 55 | + private static final int MAX_CONNECTIONS = 8; |
| 56 | + |
| 57 | + private static final String SIMILARITY_FUNCTION = "DOT_PRODUCT"; |
| 58 | + |
| 59 | + private static final String[] FIELDS = { "someField1", "someField2" }; |
| 60 | + |
| 61 | + private static final int BUCKET_COUNT = 2; |
| 62 | + |
| 63 | + private static final int HTTP_SERVICE_PORT = 9090; |
| 64 | + |
| 65 | + private static final int LOCATOR_COUNT = 1; |
| 66 | + |
| 67 | + private static final int SERVER_COUNT = 1; |
| 68 | + |
| 69 | + private static GemFireCluster gemFireCluster; |
| 70 | + |
| 71 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 72 | + .withConfiguration(AutoConfigurations.of(GemFireVectorStoreAutoConfiguration.class)) |
| 73 | + .withUserConfiguration(Config.class) |
| 74 | + .withPropertyValues("spring.ai.vectorstore.gemfire.index-name=" + INDEX_NAME) |
| 75 | + .withPropertyValues("spring.ai.vectorstore.gemfire.beam-width=" + BEAM_WIDTH) |
| 76 | + .withPropertyValues("spring.ai.vectorstore.gemfire.max-connections=" + MAX_CONNECTIONS) |
| 77 | + .withPropertyValues("spring.ai.vectorstore.gemfire.vector-similarity-function=" + SIMILARITY_FUNCTION) |
| 78 | + .withPropertyValues("spring.ai.vectorstore.gemfire.buckets=" + BUCKET_COUNT) |
| 79 | + .withPropertyValues("spring.ai.vectorstore.gemfire.fields=someField1,someField2") |
| 80 | + .withPropertyValues("spring.ai.vectorstore.gemfire.host=localhost") |
| 81 | + .withPropertyValues("spring.ai.vectorstore.gemfire.port=" + HTTP_SERVICE_PORT) |
| 82 | + .withPropertyValues("spring.ai.vectorstore.gemfire.initialize-schema=true") |
| 83 | + .withPropertyValues("spring.ai.vectorstore.gemfire.username=clusterManage,dataRead") |
| 84 | + .withPropertyValues("spring.ai.vectorstore.gemfire.password=clusterManage,dataRead") |
| 85 | + .withPropertyValues("spring.ai.vectorstore.gemfire.token=0123456789012345678901234567890"); |
| 86 | + |
| 87 | + @AfterAll |
| 88 | + public static void stopGemFireCluster() { |
| 89 | + gemFireCluster.close(); |
| 90 | + } |
| 91 | + |
| 92 | + @BeforeAll |
| 93 | + public static void startGemFireCluster() { |
| 94 | + Ports.Binding hostPort = Ports.Binding.bindPort(HTTP_SERVICE_PORT); |
| 95 | + ExposedPort exposedPort = new ExposedPort(HTTP_SERVICE_PORT); |
| 96 | + PortBinding mappedPort = new PortBinding(hostPort, exposedPort); |
| 97 | + gemFireCluster = new GemFireCluster("gemfire/gemfire-all:10.1-jdk17", LOCATOR_COUNT, SERVER_COUNT); |
| 98 | + gemFireCluster.withConfiguration(GemFireCluster.SERVER_GLOB, |
| 99 | + container -> container.withExposedPorts(HTTP_SERVICE_PORT) |
| 100 | + .withCreateContainerCmdModifier(cmd -> cmd.getHostConfig().withPortBindings(mappedPort))); |
| 101 | + gemFireCluster.withGemFireProperty(GemFireCluster.SERVER_GLOB, "http-service-port", |
| 102 | + Integer.toString(HTTP_SERVICE_PORT)); |
| 103 | + gemFireCluster.withGemFireProperty(GemFireCluster.ALL_GLOB, "security-manager", |
| 104 | + "org.apache.geode.examples.SimpleSecurityManager"); |
| 105 | + |
| 106 | + gemFireCluster.withGemFireProperty(GemFireCluster.ALL_GLOB, "security-username", "clusterManage"); |
| 107 | + gemFireCluster.withGemFireProperty(GemFireCluster.ALL_GLOB, "security-password", "clusterManage"); |
| 108 | + gemFireCluster.acceptLicense().start(); |
| 109 | + |
| 110 | + System.setProperty("spring.data.gemfire.pool.locators", |
| 111 | + String.format("localhost[%d]", gemFireCluster.getLocatorPort())); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void ensureGemFireVectorStoreCustomConfiguration() { |
| 116 | + this.contextRunner.run(context -> { |
| 117 | + GemFireVectorStore store = context.getBean(GemFireVectorStore.class); |
| 118 | + |
| 119 | + Assertions.assertNotNull(store); |
| 120 | + assertThat(store.getIndexName()).isEqualTo(INDEX_NAME); |
| 121 | + assertThat(store.getBeamWidth()).isEqualTo(BEAM_WIDTH); |
| 122 | + assertThat(store.getMaxConnections()).isEqualTo(MAX_CONNECTIONS); |
| 123 | + assertThat(store.getVectorSimilarityFunction()).isEqualTo(SIMILARITY_FUNCTION); |
| 124 | + assertThat(store.getFields()).isEqualTo(FIELDS); |
| 125 | + |
| 126 | + String indexJson = store.getIndex(); |
| 127 | + Map<String, Object> index = parseIndex(indexJson); |
| 128 | + assertThat(index.get("name")).isEqualTo(INDEX_NAME); |
| 129 | + assertThat(index.get("beam-width")).isEqualTo(BEAM_WIDTH); |
| 130 | + assertThat(index.get("max-connections")).isEqualTo(MAX_CONNECTIONS); |
| 131 | + assertThat(index.get("vector-similarity-function")).isEqualTo(SIMILARITY_FUNCTION); |
| 132 | + assertThat(index.get("buckets")).isEqualTo(BUCKET_COUNT); |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + private Map<String, Object> parseIndex(String json) { |
| 137 | + try { |
| 138 | + JsonNode rootNode = new ObjectMapper().readTree(json); |
| 139 | + Map<String, Object> indexDetails = new HashMap<>(); |
| 140 | + if (rootNode.isObject()) { |
| 141 | + if (rootNode.has("name")) { |
| 142 | + indexDetails.put("name", rootNode.get("name").asText()); |
| 143 | + } |
| 144 | + if (rootNode.has("beam-width")) { |
| 145 | + indexDetails.put("beam-width", rootNode.get("beam-width").asInt()); |
| 146 | + } |
| 147 | + if (rootNode.has("max-connections")) { |
| 148 | + indexDetails.put("max-connections", rootNode.get("max-connections").asInt()); |
| 149 | + } |
| 150 | + if (rootNode.has("vector-similarity-function")) { |
| 151 | + indexDetails.put("vector-similarity-function", rootNode.get("vector-similarity-function").asText()); |
| 152 | + } |
| 153 | + if (rootNode.has("buckets")) { |
| 154 | + indexDetails.put("buckets", rootNode.get("buckets").asInt()); |
| 155 | + } |
| 156 | + if (rootNode.has("number-of-embeddings")) { |
| 157 | + indexDetails.put("number-of-embeddings", rootNode.get("number-of-embeddings").asInt()); |
| 158 | + } |
| 159 | + } |
| 160 | + return indexDetails; |
| 161 | + } |
| 162 | + catch (Exception e) { |
| 163 | + return new HashMap<>(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Configuration(proxyBeanMethods = false) |
| 168 | + static class Config { |
| 169 | + |
| 170 | + @Bean |
| 171 | + public TestObservationRegistry observationRegistry() { |
| 172 | + return TestObservationRegistry.create(); |
| 173 | + } |
| 174 | + |
| 175 | + @Bean |
| 176 | + public EmbeddingModel embeddingModel() { |
| 177 | + return new TransformersEmbeddingModel(); |
| 178 | + } |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments