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" )
3741class AzureCognitiveSearchMemoryTests {
3842
3943 // com.azure.core.implementation.http.rest.RestProxyBase.RestProxyBase
@@ -85,22 +89,51 @@ public boolean isEnabled() {
8589
8690 @ BeforeEach
8791 void setUp () {
92+ kernel = buildKernel (null );
93+ }
8894
89- String apiKey = "fake-key" ;
90- HttpPipeline pipeline = setUpHttpPipeline ();
95+ private Kernel buildKernel (
96+ @ Nullable Function <SearchDocument , MemoryRecord > memoryRecordMapper ) {
97+ SearchAsyncClient searchAsyncClient = Mockito .mock (SearchAsyncClient .class );
98+ SearchIndexAsyncClient searchIndexAsyncClient = Mockito .mock (SearchIndexAsyncClient .class );
99+ Response response = Mockito .mock (Response .class );
100+ AzureCognitiveSearchMemoryRecord azureCognitiveSearchMemoryRecord =
101+ Mockito .mock (AzureCognitiveSearchMemoryRecord .class );
102+ MemoryRecord memoryRecord = Mockito .mock (MemoryRecord .class );
103+ SearchPagedFlux searchPagedFlux = Mockito .mock (SearchPagedFlux .class );
104+ SearchResult searchResult = Mockito .mock (SearchResult .class );
105+
106+ Mockito .when (searchResult .getDocument (Mockito .any ())).thenReturn (new SearchDocument ());
107+ Mockito .when (searchIndexAsyncClient .getSearchAsyncClient (Mockito .any ()))
108+ .thenReturn (searchAsyncClient );
109+
110+ Mockito .when (searchPagedFlux .filter (Mockito .any ())).thenReturn (Flux .just (searchResult ));
111+
112+ Mockito .when (
113+ searchAsyncClient .getDocumentWithResponse (
114+ Mockito .any (), Mockito .any (), Mockito .any ()))
115+ .thenReturn (Mono .just (response ));
116+
117+ Mockito .when (searchAsyncClient .search (Mockito .any (), Mockito .any ()))
118+ .thenReturn (searchPagedFlux );
119+ Mockito .when (response .getValue ()).thenReturn (azureCognitiveSearchMemoryRecord );
120+ Mockito .when (azureCognitiveSearchMemoryRecord .toMemoryRecord (Mockito .anyBoolean ()))
121+ .thenReturn (memoryRecord );
91122
92- SearchIndexAsyncClient searchIndexAsyncClient =
93- new SearchIndexClientBuilder ()
123+ EmbeddingGeneration <String > embeddingGeneration =
124+ new EmbeddingGeneration <String >() {
125+ /*
126+ new SearchIndexClientBuilder()
94127 .endpoint("https://fake-random-test-host/fake-path")
95128 .credential(new AzureKeyCredential(apiKey))
96129 .pipeline(pipeline)
97130 .httpLogOptions(
98- new HttpLogOptions ()
99- .setLogLevel (HttpLogDetailLevel .BODY_AND_HEADERS ))
131+ new HttpLogOptions()
132+ .setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
100133 .buildAsyncClient();
101134
102- EmbeddingGeneration < String > embeddingGeneration =
103- new EmbeddingGeneration < String >() {
135+ */
136+
104137 List <Embedding > embeddings = new ArrayList <>();
105138
106139 {
@@ -113,12 +146,12 @@ public Mono<List<Embedding>> generateEmbeddingsAsync(List<String> data) {
113146 }
114147 };
115148
116- kernel =
117- SKBuilders . kernel ()
118- . withMemoryStorage (
119- new AzureCognitiveSearchMemoryStore ( searchIndexAsyncClient ))
120- .withDefaultAIService (embeddingGeneration )
121- .build ();
149+ return SKBuilders . kernel ()
150+ . withMemoryStorage (
151+ new AzureCognitiveSearchMemoryStore (
152+ searchIndexAsyncClient , memoryRecordMapper ))
153+ .withDefaultAIService (embeddingGeneration )
154+ .build ();
122155 }
123156
124157 private HttpPipeline setUpHttpPipeline () {
@@ -158,6 +191,7 @@ private Mono<HttpResponse> answerHttpRequest(InvocationOnMock invocation) {
158191 return Mono .just (httpResponse );
159192 }
160193
194+ @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
161195 @ Test
162196 void azureCognitiveSearchMemoryStoreShouldBeProperlyInitialized () {
163197 // Arrange
@@ -172,6 +206,7 @@ void azureCognitiveSearchMemoryStoreShouldBeProperlyInitialized() {
172206 assertTrue (collections .contains ("fake-index" ));
173207 }
174208
209+ @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
175210 @ Test
176211 void azureCognitiveSearchMemoryStoreCanSaveReferenceAsync () {
177212 // Arrange
@@ -205,6 +240,7 @@ void azureCognitiveSearchMemoryStoreCanSaveReferenceAsync() {
205240 assertEquals ("fake-key" , key );
206241 }
207242
243+ @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
208244 @ Test
209245 void azureCognitiveSearchMemoryStoreCanSaveInformationAsync () {
210246 // Arrange
@@ -237,6 +273,7 @@ void azureCognitiveSearchMemoryStoreCanSaveInformationAsync() {
237273 assertEquals ("fake-key" , key );
238274 }
239275
276+ @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
240277 @ Test
241278 void azureCognitiveSearchMemoryStoreGetAsync () {
242279 // Arrange
@@ -267,6 +304,7 @@ void azureCognitiveSearchMemoryStoreGetAsync() {
267304 assertEquals (key , result .getMetadata ().getId ());
268305 }
269306
307+ @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
270308 @ Test
271309 void azureCognitiveSearchMemoryStoreRemoveAsync () {
272310 // Arrange
@@ -298,6 +336,7 @@ void azureCognitiveSearchMemoryStoreRemoveAsync() {
298336 // Assert
299337 }
300338
339+ @ Disabled ("Pending https://github.com/microsoft/semantic-kernel/issues/1797" )
301340 @ Test
302341 void azureCognitiveSearchMemoryStoreSearchAsync () {
303342 // Arrange
@@ -342,4 +381,54 @@ void azureCognitiveSearchMemoryStoreSearchAsync() {
342381 assertEquals (1 , results .size ());
343382 assertEquals (key , results .get (0 ).getMetadata ().getId ());
344383 }
384+
385+ @ Test
386+ void azureCognitiveSearchMemoryStoreGetAsyncWithCustomMapping () {
387+ // Do not convert to lambda, it will break mockito
388+ Function <SearchDocument , MemoryRecord > transform =
389+ Mockito .spy (
390+ new Function <SearchDocument , MemoryRecord >() {
391+ @ Override
392+ public MemoryRecord apply (SearchDocument searchDocument ) {
393+ return new MemoryRecord (
394+ new MemoryRecordMetadata (
395+ false ,
396+ "an-id" ,
397+ "fake-text" ,
398+ "fake-description" ,
399+ "fake-additionalmetadata" ,
400+ "fake-externalsourcename" ),
401+ new Embedding (Arrays .asList (1.0f , 2.0f , 3.0f )),
402+ "an-id" ,
403+ null );
404+ }
405+ });
406+
407+ kernel = buildKernel (transform );
408+ // Arrange
409+ String key = "fake-key" ;
410+ String base64EncodedKey =
411+ new String (
412+ Base64 .getUrlEncoder ().encode ("fake-key" .getBytes (StandardCharsets .UTF_8 )));
413+
414+ responseBody =
415+ httpRequest ->
416+ "{"
417+ + " \" Id\" : \" "
418+ + base64EncodedKey
419+ + "\" ,"
420+ + " \" Text\" : \" fake-text\" ,"
421+ + " \" Description\" : \" fake-description\" ,"
422+ + " \" AdditionalMetadata\" : \" fake-additionalmetadata\" ,"
423+ + " \" ExternalSourceName\" : \" fake-externalsourcename\" ,"
424+ + " \" Reference\" : false"
425+ + "}" ;
426+
427+ responseStatusCode = httpRequest -> 200 ;
428+
429+ // Act
430+ kernel .getMemory ().searchAsync ("fake-index" , "a-query" , 1 , 0.0f , false ).block ();
431+
432+ Mockito .verify (transform , Mockito .atLeastOnce ()).apply (Mockito .any ());
433+ }
345434}
0 commit comments