44import static org .junit .jupiter .api .Assertions .assertEquals ;
55import static org .junit .jupiter .api .Assertions .assertTrue ;
66
7- import com .azure .core .credential .AzureKeyCredential ;
87import com .azure .core .http .HttpHeaders ;
98import com .azure .core .http .HttpPipeline ;
109import com .azure .core .http .HttpRequest ;
1110import com .azure .core .http .HttpResponse ;
12- import com .azure .core .http .policy .HttpLogDetailLevel ;
13- import com .azure .core .http .policy .HttpLogOptions ;
11+ import com .azure .core .http .rest .Response ;
1412import com .azure .core .util .Context ;
1513import com .azure .core .util .tracing .Tracer ;
14+ import com .azure .search .documents .SearchAsyncClient ;
15+ import com .azure .search .documents .SearchDocument ;
1616import com .azure .search .documents .indexes .SearchIndexAsyncClient ;
17- import com .azure .search .documents .indexes .SearchIndexClientBuilder ;
17+ import com .azure .search .documents .models .SearchResult ;
18+ import com .azure .search .documents .util .SearchPagedFlux ;
1819import com .microsoft .semantickernel .Kernel ;
1920import com .microsoft .semantickernel .SKBuilders ;
2021import com .microsoft .semantickernel .ai .embeddings .Embedding ;
2122import com .microsoft .semantickernel .ai .embeddings .EmbeddingGeneration ;
2223import com .microsoft .semantickernel .memory .MemoryQueryResult ;
24+ import com .microsoft .semantickernel .memory .MemoryRecord ;
25+ import com .microsoft .semantickernel .memory .MemoryRecordMetadata ;
2326import java .nio .charset .StandardCharsets ;
2427import java .util .ArrayList ;
2528import java .util .Arrays ;
2629import java .util .Base64 ;
2730import java .util .List ;
2831import java .util .function .Function ;
32+ import javax .annotation .Nullable ;
2933import org .junit .jupiter .api .BeforeEach ;
3034import org .junit .jupiter .api .Disabled ;
3135import org .junit .jupiter .api .Test ;
3236import org .mockito .Mockito ;
3337import org .mockito .invocation .InvocationOnMock ;
38+ import reactor .core .publisher .Flux ;
3439import reactor .core .publisher .Mono ;
3540
36- @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
41+ // @Disabled("Pending https://github.com/microsoft/semantic-kernel/issues/1797")
3742class AzureCognitiveSearchMemoryTests {
3843
3944 // com.azure.core.implementation.http.rest.RestProxyBase.RestProxyBase
@@ -85,22 +90,51 @@ public boolean isEnabled() {
8590
8691 @ BeforeEach
8792 void setUp () {
93+ kernel = buildKernel (null );
94+ }
8895
89- String apiKey = "fake-key" ;
90- HttpPipeline pipeline = setUpHttpPipeline ();
96+ private Kernel buildKernel (
97+ @ Nullable Function <SearchDocument , MemoryRecord > memoryRecordMapper ) {
98+ SearchAsyncClient searchAsyncClient = Mockito .mock (SearchAsyncClient .class );
99+ SearchIndexAsyncClient searchIndexAsyncClient = Mockito .mock (SearchIndexAsyncClient .class );
100+ Response response = Mockito .mock (Response .class );
101+ AzureCognitiveSearchMemoryRecord azureCognitiveSearchMemoryRecord =
102+ Mockito .mock (AzureCognitiveSearchMemoryRecord .class );
103+ MemoryRecord memoryRecord = Mockito .mock (MemoryRecord .class );
104+ SearchPagedFlux searchPagedFlux = Mockito .mock (SearchPagedFlux .class );
105+ SearchResult searchResult = Mockito .mock (SearchResult .class );
106+
107+ Mockito .when (searchResult .getDocument (Mockito .any ())).thenReturn (new SearchDocument ());
108+ Mockito .when (searchIndexAsyncClient .getSearchAsyncClient (Mockito .any ()))
109+ .thenReturn (searchAsyncClient );
110+
111+ Mockito .when (searchPagedFlux .filter (Mockito .any ())).thenReturn (Flux .just (searchResult ));
112+
113+ Mockito .when (
114+ searchAsyncClient .getDocumentWithResponse (
115+ Mockito .any (), Mockito .any (), Mockito .any ()))
116+ .thenReturn (Mono .just (response ));
117+
118+ Mockito .when (searchAsyncClient .search (Mockito .any (), Mockito .any ()))
119+ .thenReturn (searchPagedFlux );
120+ Mockito .when (response .getValue ()).thenReturn (azureCognitiveSearchMemoryRecord );
121+ Mockito .when (azureCognitiveSearchMemoryRecord .toMemoryRecord (Mockito .anyBoolean ()))
122+ .thenReturn (memoryRecord );
91123
92- SearchIndexAsyncClient searchIndexAsyncClient =
93- new SearchIndexClientBuilder ()
124+ EmbeddingGeneration <String > embeddingGeneration =
125+ new EmbeddingGeneration <String >() {
126+ /*
127+ new SearchIndexClientBuilder()
94128 .endpoint("https://fake-random-test-host/fake-path")
95129 .credential(new AzureKeyCredential(apiKey))
96130 .pipeline(pipeline)
97131 .httpLogOptions(
98- new HttpLogOptions ()
99- .setLogLevel (HttpLogDetailLevel .BODY_AND_HEADERS ))
132+ new HttpLogOptions()
133+ .setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
100134 .buildAsyncClient();
101135
102- EmbeddingGeneration < String > embeddingGeneration =
103- new EmbeddingGeneration < String >() {
136+ */
137+
104138 List <Embedding > embeddings = new ArrayList <>();
105139
106140 {
@@ -113,12 +147,12 @@ public Mono<List<Embedding>> generateEmbeddingsAsync(List<String> data) {
113147 }
114148 };
115149
116- kernel =
117- SKBuilders . kernel ()
118- . withMemoryStorage (
119- new AzureCognitiveSearchMemoryStore ( searchIndexAsyncClient ))
120- .withDefaultAIService (embeddingGeneration )
121- .build ();
150+ return SKBuilders . kernel ()
151+ . withMemoryStorage (
152+ new AzureCognitiveSearchMemoryStore (
153+ searchIndexAsyncClient , memoryRecordMapper ))
154+ .withDefaultAIService (embeddingGeneration )
155+ .build ();
122156 }
123157
124158 private HttpPipeline setUpHttpPipeline () {
@@ -158,6 +192,7 @@ private Mono<HttpResponse> answerHttpRequest(InvocationOnMock invocation) {
158192 return Mono .just (httpResponse );
159193 }
160194
195+ @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
161196 @ Test
162197 void azureCognitiveSearchMemoryStoreShouldBeProperlyInitialized () {
163198 // Arrange
@@ -172,6 +207,7 @@ void azureCognitiveSearchMemoryStoreShouldBeProperlyInitialized() {
172207 assertTrue (collections .contains ("fake-index" ));
173208 }
174209
210+ @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
175211 @ Test
176212 void azureCognitiveSearchMemoryStoreCanSaveReferenceAsync () {
177213 // Arrange
@@ -205,6 +241,7 @@ void azureCognitiveSearchMemoryStoreCanSaveReferenceAsync() {
205241 assertEquals ("fake-key" , key );
206242 }
207243
244+ @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
208245 @ Test
209246 void azureCognitiveSearchMemoryStoreCanSaveInformationAsync () {
210247 // Arrange
@@ -237,6 +274,7 @@ void azureCognitiveSearchMemoryStoreCanSaveInformationAsync() {
237274 assertEquals ("fake-key" , key );
238275 }
239276
277+ @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
240278 @ Test
241279 void azureCognitiveSearchMemoryStoreGetAsync () {
242280 // Arrange
@@ -267,6 +305,7 @@ void azureCognitiveSearchMemoryStoreGetAsync() {
267305 assertEquals (key , result .getMetadata ().getId ());
268306 }
269307
308+ @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
270309 @ Test
271310 void azureCognitiveSearchMemoryStoreRemoveAsync () {
272311 // Arrange
@@ -298,6 +337,7 @@ void azureCognitiveSearchMemoryStoreRemoveAsync() {
298337 // Assert
299338 }
300339
340+ @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
301341 @ Test
302342 void azureCognitiveSearchMemoryStoreSearchAsync () {
303343 // Arrange
@@ -342,4 +382,54 @@ void azureCognitiveSearchMemoryStoreSearchAsync() {
342382 assertEquals (1 , results .size ());
343383 assertEquals (key , results .get (0 ).getMetadata ().getId ());
344384 }
385+
386+ @ Test
387+ void azureCognitiveSearchMemoryStoreGetAsyncWithCustomMapping () {
388+ // Do not convert to lambda, it will break mockito
389+ Function <SearchDocument , MemoryRecord > transform =
390+ Mockito .spy (
391+ new Function <SearchDocument , MemoryRecord >() {
392+ @ Override
393+ public MemoryRecord apply (SearchDocument searchDocument ) {
394+ return new MemoryRecord (
395+ new MemoryRecordMetadata (
396+ false ,
397+ "an-id" ,
398+ "fake-text" ,
399+ "fake-description" ,
400+ "fake-additionalmetadata" ,
401+ "fake-externalsourcename" ),
402+ new Embedding (Arrays .asList (1.0f , 2.0f , 3.0f )),
403+ "an-id" ,
404+ null );
405+ }
406+ });
407+
408+ kernel = buildKernel (transform );
409+ // Arrange
410+ String key = "fake-key" ;
411+ String base64EncodedKey =
412+ new String (
413+ Base64 .getUrlEncoder ().encode ("fake-key" .getBytes (StandardCharsets .UTF_8 )));
414+
415+ responseBody =
416+ httpRequest ->
417+ "{"
418+ + " \" Id\" : \" "
419+ + base64EncodedKey
420+ + "\" ,"
421+ + " \" Text\" : \" fake-text\" ,"
422+ + " \" Description\" : \" fake-description\" ,"
423+ + " \" AdditionalMetadata\" : \" fake-additionalmetadata\" ,"
424+ + " \" ExternalSourceName\" : \" fake-externalsourcename\" ,"
425+ + " \" Reference\" : false"
426+ + "}" ;
427+
428+ responseStatusCode = httpRequest -> 200 ;
429+
430+ // Act
431+ kernel .getMemory ().searchAsync ("fake-index" , "a-query" , 1 , 0.0f , false ).block ();
432+
433+ Mockito .verify (transform , Mockito .atLeastOnce ()).apply (Mockito .any ());
434+ }
345435}
0 commit comments