|
| 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.opensearch.autoconfigure; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.opensearch.testcontainers.OpensearchContainer; |
| 26 | +import org.testcontainers.junit.jupiter.Container; |
| 27 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 28 | +import org.testcontainers.utility.DockerImageName; |
| 29 | + |
| 30 | +import org.springframework.ai.document.Document; |
| 31 | +import org.springframework.ai.embedding.EmbeddingModel; |
| 32 | +import org.springframework.ai.retry.autoconfigure.SpringAiRetryAutoConfiguration; |
| 33 | +import org.springframework.ai.transformers.TransformersEmbeddingModel; |
| 34 | +import org.springframework.ai.vectorstore.opensearch.OpenSearchVectorStore; |
| 35 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 36 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 37 | +import org.springframework.context.annotation.Bean; |
| 38 | +import org.springframework.context.annotation.Configuration; |
| 39 | +import org.springframework.core.io.DefaultResourceLoader; |
| 40 | + |
| 41 | +import static org.assertj.core.api.Assertions.assertThat; |
| 42 | + |
| 43 | +@Testcontainers |
| 44 | +class OpenSearchVectorStoreNonAwsFallbackIT { |
| 45 | + |
| 46 | + @Container |
| 47 | + private static final OpensearchContainer<?> opensearchContainer = new OpensearchContainer<>( |
| 48 | + DockerImageName.parse("opensearchproject/opensearch:2.13.0")); |
| 49 | + |
| 50 | + private static final String DOCUMENT_INDEX = "nonaws-spring-ai-document-index"; |
| 51 | + |
| 52 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 53 | + .withConfiguration(AutoConfigurations.of(OpenSearchVectorStoreAutoConfiguration.class, |
| 54 | + SpringAiRetryAutoConfiguration.class)) |
| 55 | + .withUserConfiguration(Config.class) |
| 56 | + .withPropertyValues("spring.ai.vectorstore.opensearch.aws.enabled=false", |
| 57 | + "spring.ai.vectorstore.opensearch.uris=" + opensearchContainer.getHttpHostAddress(), |
| 58 | + "spring.ai.vectorstore.opensearch.indexName=" + DOCUMENT_INDEX, |
| 59 | + "spring.ai.vectorstore.opensearch.mappingJson={\"properties\":{\"embedding\":{\"type\":\"knn_vector\",\"dimension\":384}}}"); |
| 60 | + |
| 61 | + private List<Document> documents = List.of( |
| 62 | + new Document("1", getText("classpath:/test/data/spring.ai.txt"), Map.of("meta1", "meta1")), |
| 63 | + new Document("2", getText("classpath:/test/data/time.shelter.txt"), Map.of()), |
| 64 | + new Document("3", getText("classpath:/test/data/great.depression.txt"), Map.of("meta2", "meta2"))); |
| 65 | + |
| 66 | + @Test |
| 67 | + void nonAwsFallbackConfigurationWorks() { |
| 68 | + this.contextRunner.run(context -> { |
| 69 | + // AWS-specific bean should NOT be present |
| 70 | + assertThat(context.containsBeanDefinition("awsOpenSearchConnectionDetails")).isFalse(); |
| 71 | + // Standard OpenSearch bean should be present |
| 72 | + assertThat(context.getBeansOfType(OpenSearchConnectionDetails.class)).isNotEmpty(); |
| 73 | + // OpenSearchVectorStore should still be present |
| 74 | + assertThat(context.getBeansOfType(OpenSearchVectorStore.class)).isNotEmpty(); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + private String getText(String uri) { |
| 79 | + var resource = new DefaultResourceLoader().getResource(uri); |
| 80 | + try { |
| 81 | + return resource.getContentAsString(StandardCharsets.UTF_8); |
| 82 | + } |
| 83 | + catch (IOException e) { |
| 84 | + throw new RuntimeException(e); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Configuration(proxyBeanMethods = false) |
| 89 | + static class Config { |
| 90 | + |
| 91 | + @Bean |
| 92 | + public EmbeddingModel embeddingModel() { |
| 93 | + return new TransformersEmbeddingModel(); |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments