1+ package io .sentrius .sso .core .services .agents ;
2+
3+ import static org .junit .jupiter .api .Assertions .*;
4+ import static org .mockito .ArgumentMatchers .*;
5+ import static org .mockito .Mockito .*;
6+
7+ import java .time .Instant ;
8+ import java .time .LocalDateTime ;
9+ import java .util .Arrays ;
10+ import java .util .List ;
11+ import java .util .UUID ;
12+
13+ import org .junit .jupiter .api .BeforeEach ;
14+ import org .junit .jupiter .api .Test ;
15+ import org .mockito .Mock ;
16+ import org .mockito .MockitoAnnotations ;
17+ import org .springframework .data .domain .Page ;
18+ import org .springframework .data .domain .PageImpl ;
19+ import org .springframework .data .domain .PageRequest ;
20+ import org .springframework .data .domain .Pageable ;
21+
22+ import io .sentrius .sso .core .dto .AgentCommunicationDTO ;
23+ import io .sentrius .sso .core .model .chat .AgentCommunication ;
24+ import io .sentrius .sso .core .repository .AgentCommunicationRepository ;
25+
26+ class AgentMemorySearchServiceTest {
27+
28+ @ Mock
29+ private AgentCommunicationRepository agentCommunicationRepository ;
30+
31+ private AgentMemorySearchService agentMemorySearchService ;
32+
33+ @ BeforeEach
34+ void setUp () {
35+ MockitoAnnotations .openMocks (this );
36+ agentMemorySearchService = new AgentMemorySearchService (agentCommunicationRepository );
37+ }
38+
39+ @ Test
40+ void searchByContent_ShouldReturnResults () {
41+ // Given
42+ String searchTerm = "test content" ;
43+ int page = 0 ;
44+ int size = 20 ;
45+
46+ AgentCommunication mockComm = createMockAgentCommunication ();
47+ Page <AgentCommunication > mockPage = new PageImpl <>(Arrays .asList (mockComm ));
48+
49+ when (agentCommunicationRepository .findByPayloadContainingIgnoreCase (
50+ eq (searchTerm ), any (Pageable .class ))).thenReturn (mockPage );
51+
52+ // When
53+ Page <AgentCommunicationDTO > result = agentMemorySearchService .searchByContent (searchTerm , page , size );
54+
55+ // Then
56+ assertNotNull (result );
57+ assertEquals (1 , result .getContent ().size ());
58+ assertEquals (mockComm .getSourceAgent (), result .getContent ().get (0 ).getSourceAgent ());
59+ verify (agentCommunicationRepository ).findByPayloadContainingIgnoreCase (eq (searchTerm ), any (Pageable .class ));
60+ }
61+
62+ @ Test
63+ void searchByAgent_ShouldReturnResults () {
64+ // Given
65+ String agentName = "test-agent" ;
66+ int page = 0 ;
67+ int size = 20 ;
68+
69+ AgentCommunication mockComm = createMockAgentCommunication ();
70+ Page <AgentCommunication > mockPage = new PageImpl <>(Arrays .asList (mockComm ));
71+
72+ when (agentCommunicationRepository .findBySourceAgentContainingIgnoreCaseOrTargetAgentContainingIgnoreCase (
73+ eq (agentName ), eq (agentName ), any (Pageable .class ))).thenReturn (mockPage );
74+
75+ // When
76+ Page <AgentCommunicationDTO > result = agentMemorySearchService .searchByAgent (agentName , page , size );
77+
78+ // Then
79+ assertNotNull (result );
80+ assertEquals (1 , result .getContent ().size ());
81+ assertEquals (mockComm .getTargetAgent (), result .getContent ().get (0 ).getTargetAgent ());
82+ verify (agentCommunicationRepository ).findBySourceAgentContainingIgnoreCaseOrTargetAgentContainingIgnoreCase (
83+ eq (agentName ), eq (agentName ), any (Pageable .class ));
84+ }
85+
86+ @ Test
87+ void searchByAgentAndContent_ShouldReturnResults () {
88+ // Given
89+ String agentName = "test-agent" ;
90+ String searchTerm = "test content" ;
91+ int page = 0 ;
92+ int size = 20 ;
93+
94+ AgentCommunication mockComm = createMockAgentCommunication ();
95+ Page <AgentCommunication > mockPage = new PageImpl <>(Arrays .asList (mockComm ));
96+
97+ when (agentCommunicationRepository .findBySourceAgentAndPayloadContainingIgnoreCase (
98+ eq (agentName ), eq (searchTerm ), any (Pageable .class ))).thenReturn (mockPage );
99+
100+ // When
101+ Page <AgentCommunicationDTO > result = agentMemorySearchService .searchByAgentAndContent (agentName , searchTerm , page , size );
102+
103+ // Then
104+ assertNotNull (result );
105+ assertEquals (1 , result .getContent ().size ());
106+ verify (agentCommunicationRepository ).findBySourceAgentAndPayloadContainingIgnoreCase (
107+ eq (agentName ), eq (searchTerm ), any (Pageable .class ));
108+ }
109+
110+ @ Test
111+ void getAllMemories_ShouldReturnAllResults () {
112+ // Given
113+ int page = 0 ;
114+ int size = 20 ;
115+
116+ AgentCommunication mockComm = createMockAgentCommunication ();
117+ Page <AgentCommunication > mockPage = new PageImpl <>(Arrays .asList (mockComm ));
118+
119+ when (agentCommunicationRepository .findAll (any (Pageable .class ))).thenReturn (mockPage );
120+
121+ // When
122+ Page <AgentCommunicationDTO > result = agentMemorySearchService .getAllMemories (page , size );
123+
124+ // Then
125+ assertNotNull (result );
126+ assertEquals (1 , result .getContent ().size ());
127+ verify (agentCommunicationRepository ).findAll (any (Pageable .class ));
128+ }
129+
130+ private AgentCommunication createMockAgentCommunication () {
131+ return AgentCommunication .builder ()
132+ .id (1L )
133+ .sourceAgent ("test-source-agent" )
134+ .targetAgent ("test-target-agent" )
135+ .messageType ("chat_request" )
136+ .communicationId (UUID .randomUUID ())
137+ .payload ("test payload content" )
138+ .createdAt (Instant .now ())
139+ .linkedRequests (Arrays .asList ()) // Empty list for simplicity
140+ .build ();
141+ }
142+ }
0 commit comments