|
5 | 5 |
|
6 | 6 | package org.opensearch.ml.action.memorycontainer.memory;
|
7 | 7 |
|
| 8 | +import static org.junit.Assert.assertEquals; |
| 9 | +import static org.junit.Assert.assertTrue; |
8 | 10 | import static org.mockito.ArgumentMatchers.any;
|
9 | 11 | import static org.mockito.Mockito.doAnswer;
|
10 | 12 | import static org.mockito.Mockito.mock;
|
|
20 | 22 |
|
21 | 23 | import org.junit.Before;
|
22 | 24 | import org.junit.Test;
|
| 25 | +import org.mockito.ArgumentCaptor; |
23 | 26 | import org.mockito.Mock;
|
24 | 27 | import org.mockito.MockitoAnnotations;
|
25 | 28 | import org.opensearch.action.bulk.BulkItemResponse;
|
@@ -253,7 +256,73 @@ public void testExecuteMemoryOperations_NoneDecision() {
|
253 | 256 |
|
254 | 257 | memoryOperationsService.executeMemoryOperations(decisions, indexName, sessionId, user, input, storageConfig, operationsListener);
|
255 | 258 |
|
256 |
| - verify(operationsListener).onResponse(any(List.class)); |
| 259 | + // Verify that NONE events result in an empty response list (no operations to execute) |
| 260 | + ArgumentCaptor<List<MemoryResult>> resultsCaptor = ArgumentCaptor.forClass(List.class); |
| 261 | + verify(operationsListener).onResponse(resultsCaptor.capture()); |
| 262 | + List<MemoryResult> results = resultsCaptor.getValue(); |
| 263 | + assertTrue(results.isEmpty()); |
| 264 | + } |
| 265 | + |
| 266 | + @Test |
| 267 | + public void testExecuteMemoryOperations_MixedDecisionsExcludesNone() { |
| 268 | + // Create mixed decisions: ADD, UPDATE, DELETE, and NONE |
| 269 | + MemoryDecision addDecision = mock(MemoryDecision.class); |
| 270 | + when(addDecision.getEvent()).thenReturn(MemoryEvent.ADD); |
| 271 | + when(addDecision.getText()).thenReturn("New fact"); |
| 272 | + |
| 273 | + MemoryDecision updateDecision = mock(MemoryDecision.class); |
| 274 | + when(updateDecision.getEvent()).thenReturn(MemoryEvent.UPDATE); |
| 275 | + when(updateDecision.getId()).thenReturn("memory-2"); |
| 276 | + when(updateDecision.getText()).thenReturn("Updated fact"); |
| 277 | + when(updateDecision.getOldMemory()).thenReturn("Old fact"); |
| 278 | + |
| 279 | + MemoryDecision deleteDecision = mock(MemoryDecision.class); |
| 280 | + when(deleteDecision.getEvent()).thenReturn(MemoryEvent.DELETE); |
| 281 | + when(deleteDecision.getId()).thenReturn("memory-3"); |
| 282 | + when(deleteDecision.getText()).thenReturn("Deleted fact"); |
| 283 | + |
| 284 | + MemoryDecision noneDecision = mock(MemoryDecision.class); |
| 285 | + when(noneDecision.getEvent()).thenReturn(MemoryEvent.NONE); |
| 286 | + when(noneDecision.getId()).thenReturn("memory-4"); |
| 287 | + when(noneDecision.getText()).thenReturn("Unchanged fact"); |
| 288 | + |
| 289 | + List<MemoryDecision> decisions = Arrays.asList(addDecision, updateDecision, deleteDecision, noneDecision); |
| 290 | + String indexName = "memory-index"; |
| 291 | + String sessionId = "session-123"; |
| 292 | + User user = null; |
| 293 | + MLAddMemoriesInput input = mock(MLAddMemoriesInput.class); |
| 294 | + when(input.getAgentId()).thenReturn("agent-123"); |
| 295 | + when(input.getTags()).thenReturn(new HashMap<>()); |
| 296 | + MemoryStorageConfig storageConfig = mock(MemoryStorageConfig.class); |
| 297 | + |
| 298 | + BulkResponse bulkResponse = mock(BulkResponse.class); |
| 299 | + when(bulkResponse.hasFailures()).thenReturn(false); |
| 300 | + BulkItemResponse addItem = mock(BulkItemResponse.class); |
| 301 | + when(addItem.getOpType()).thenReturn(org.opensearch.action.DocWriteRequest.OpType.INDEX); |
| 302 | + when(addItem.isFailed()).thenReturn(false); |
| 303 | + when(addItem.getId()).thenReturn("new-memory-id"); |
| 304 | + when(bulkResponse.getItems()).thenReturn(new BulkItemResponse[] { addItem }); |
| 305 | + |
| 306 | + doAnswer(invocation -> { |
| 307 | + ActionListener<BulkResponse> listener = invocation.getArgument(1); |
| 308 | + listener.onResponse(bulkResponse); |
| 309 | + return null; |
| 310 | + }).when(client).bulk(any(), any()); |
| 311 | + |
| 312 | + memoryOperationsService.executeMemoryOperations(decisions, indexName, sessionId, user, input, storageConfig, operationsListener); |
| 313 | + |
| 314 | + // Verify that only ADD, UPDATE, DELETE are included in results (not NONE) |
| 315 | + ArgumentCaptor<List<MemoryResult>> resultsCaptor = ArgumentCaptor.forClass(List.class); |
| 316 | + verify(operationsListener).onResponse(resultsCaptor.capture()); |
| 317 | + List<MemoryResult> results = resultsCaptor.getValue(); |
| 318 | + |
| 319 | + // Should have 3 results (ADD, UPDATE, DELETE) but not NONE |
| 320 | + assertEquals(3, results.size()); |
| 321 | + |
| 322 | + // Verify the events in the results |
| 323 | + assertEquals(MemoryEvent.ADD, results.get(0).getEvent()); |
| 324 | + assertEquals(MemoryEvent.UPDATE, results.get(1).getEvent()); |
| 325 | + assertEquals(MemoryEvent.DELETE, results.get(2).getEvent()); |
257 | 326 | }
|
258 | 327 |
|
259 | 328 | @Test
|
|
0 commit comments