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+ package  org .springframework .ai .testcontainers .service .connection .mongo ;
17+ 
18+ import  org .junit .jupiter .api .Test ;
19+ import  org .springframework .ai .autoconfigure .vectorstore .mongo .MongoDBAtlasVectorStoreAutoConfiguration ;
20+ import  org .springframework .ai .document .Document ;
21+ import  org .springframework .ai .embedding .EmbeddingModel ;
22+ import  org .springframework .ai .openai .OpenAiEmbeddingModel ;
23+ import  org .springframework .ai .openai .api .OpenAiApi ;
24+ import  org .springframework .ai .vectorstore .SearchRequest ;
25+ import  org .springframework .ai .vectorstore .VectorStore ;
26+ import  org .springframework .beans .factory .annotation .Autowired ;
27+ import  org .springframework .boot .autoconfigure .ImportAutoConfiguration ;
28+ import  org .springframework .boot .autoconfigure .data .mongo .MongoDataAutoConfiguration ;
29+ import  org .springframework .boot .autoconfigure .mongo .MongoAutoConfiguration ;
30+ import  org .springframework .boot .testcontainers .service .connection .ServiceConnection ;
31+ import  org .springframework .context .annotation .Bean ;
32+ import  org .springframework .context .annotation .Configuration ;
33+ import  org .springframework .test .context .TestPropertySource ;
34+ import  org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
35+ import  org .testcontainers .junit .jupiter .Container ;
36+ import  org .testcontainers .junit .jupiter .Testcontainers ;
37+ import  org .testcontainers .mongodb .MongoDBAtlasLocalContainer ;
38+ 
39+ import  java .util .Collections ;
40+ import  java .util .List ;
41+ import  java .util .stream .Collectors ;
42+ 
43+ import  static  org .assertj .core .api .Assertions .assertThat ;
44+ 
45+ @ SpringJUnitConfig 
46+ @ Testcontainers 
47+ @ TestPropertySource (properties  = { "spring.data.mongodb.database=simpleaidb" ,
48+ 		"spring.ai.vectorstore.mongodb.initialize-schema=true" ,
49+ 		"spring.ai.vectorstore.mongodb.collection-name=test_collection" ,
50+ 		"spring.ai.vectorstore.mongodb.index-name=text_index"  })
51+ class  MongoDbAtlasLocalContainerConnectionDetailsFactoryTest  {
52+ 
53+ 	@ Container 
54+ 	@ ServiceConnection 
55+ 	private  static  MongoDBAtlasLocalContainer  container  = new  MongoDBAtlasLocalContainer (
56+ 			"mongodb/mongodb-atlas-local:7.0.9" );
57+ 
58+ 	@ Autowired 
59+ 	private  VectorStore  vectorStore ;
60+ 
61+ 	@ Test 
62+ 	public  void  addAndSearch () throws  InterruptedException  {
63+ 		List <Document > documents  = List .of (
64+ 				new  Document (
65+ 						"Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!" ,
66+ 						Collections .singletonMap ("meta1" , "meta1" )),
67+ 				new  Document ("Hello World Hello World Hello World Hello World Hello World Hello World Hello World" ),
68+ 				new  Document (
69+ 						"Great Depression Great Depression Great Depression Great Depression Great Depression Great Depression" ,
70+ 						Collections .singletonMap ("meta2" , "meta2" )));
71+ 
72+ 		vectorStore .add (documents );
73+ 		Thread .sleep (5000 ); // Await a second for the document to be indexed 
74+ 
75+ 		List <Document > results  = vectorStore .similaritySearch (SearchRequest .query ("Great" ).withTopK (1 ));
76+ 
77+ 		assertThat (results ).hasSize (1 );
78+ 		Document  resultDoc  = results .get (0 );
79+ 		assertThat (resultDoc .getId ()).isEqualTo (documents .get (2 ).getId ());
80+ 		assertThat (resultDoc .getContent ()).isEqualTo (
81+ 				"Great Depression Great Depression Great Depression Great Depression Great Depression Great Depression" );
82+ 		assertThat (resultDoc .getMetadata ()).containsEntry ("meta2" , "meta2" );
83+ 
84+ 		// Remove all documents from the store 
85+ 		vectorStore .delete (documents .stream ().map (Document ::getId ).collect (Collectors .toList ()));
86+ 
87+ 		List <Document > results2  = vectorStore .similaritySearch (SearchRequest .query ("Great" ).withTopK (1 ));
88+ 		assertThat (results2 ).isEmpty ();
89+ 	}
90+ 
91+ 	@ Configuration (proxyBeanMethods  = false )
92+ 	@ ImportAutoConfiguration ({ MongoAutoConfiguration .class , MongoDataAutoConfiguration .class ,
93+ 			MongoDBAtlasVectorStoreAutoConfiguration .class  })
94+ 	static  class  Config  {
95+ 
96+ 		@ Bean 
97+ 		public  EmbeddingModel  embeddingModel () {
98+ 			return  new  OpenAiEmbeddingModel (new  OpenAiApi (System .getenv ("OPENAI_API_KEY" )));
99+ 		}
100+ 
101+ 	}
102+ 
103+ }
0 commit comments