|
| 1 | +package io.sentrius.sso.controllers.api.documents; |
| 2 | + |
| 3 | +import io.sentrius.sso.core.config.SystemOptions; |
| 4 | +import io.sentrius.sso.core.dto.documents.DocumentSearchDTO; |
| 5 | +import io.sentrius.sso.core.model.documents.Document; |
| 6 | +import io.sentrius.sso.core.model.users.User; |
| 7 | +import io.sentrius.sso.core.services.ErrorOutputService; |
| 8 | +import io.sentrius.sso.core.services.UserService; |
| 9 | +import io.sentrius.sso.core.services.documents.DocumentService; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 14 | + |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.*; |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | + |
| 22 | +/** |
| 23 | + * Integration test to verify document search returns empty results when no matches found. |
| 24 | + */ |
| 25 | +@SpringBootTest |
| 26 | +class DocumentSearchIntegrationTest { |
| 27 | + |
| 28 | + @MockBean |
| 29 | + private DocumentService documentService; |
| 30 | + |
| 31 | + @MockBean |
| 32 | + private UserService userService; |
| 33 | + |
| 34 | + @MockBean |
| 35 | + private SystemOptions systemOptions; |
| 36 | + |
| 37 | + @MockBean |
| 38 | + private ErrorOutputService errorOutputService; |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private DocumentController documentController; |
| 42 | + |
| 43 | + @Test |
| 44 | + void testSearchDocuments_NoMatchesReturnsEmptyList() { |
| 45 | + // Setup: Mock user service |
| 46 | + User mockUser = new User(); |
| 47 | + mockUser.setUserId("test-user"); |
| 48 | + when(userService.getOperatingUser(any(), any(), any())).thenReturn(mockUser); |
| 49 | + |
| 50 | + // Setup: Search for non-existent content |
| 51 | + DocumentSearchDTO searchDTO = DocumentSearchDTO.builder() |
| 52 | + .query("XYZNONEXISTENTQUERY123456") |
| 53 | + .build(); |
| 54 | + |
| 55 | + // Setup: Mock service to return empty list (simulating no matches) |
| 56 | + when(documentService.searchDocuments(any(DocumentSearchDTO.class))) |
| 57 | + .thenReturn(Collections.emptyList()); |
| 58 | + |
| 59 | + // Execute search |
| 60 | + var response = documentController.searchDocuments(searchDTO, null, null); |
| 61 | + |
| 62 | + // Verify: Should return empty list, not all documents |
| 63 | + assertNotNull(response); |
| 64 | + assertNotNull(response.getBody()); |
| 65 | + assertTrue(response.getBody().isEmpty(), |
| 66 | + "Search with no matches should return empty list, not all documents"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testSearchDocuments_WithFiltersButNoMatches() { |
| 71 | + // Setup: Mock user service |
| 72 | + User mockUser = new User(); |
| 73 | + mockUser.setUserId("test-user"); |
| 74 | + when(userService.getOperatingUser(any(), any(), any())).thenReturn(mockUser); |
| 75 | + |
| 76 | + // Setup: Search with filters that don't match any documents |
| 77 | + DocumentSearchDTO searchDTO = DocumentSearchDTO.builder() |
| 78 | + .query("test query") |
| 79 | + .documentType("NONEXISTENT_TYPE") |
| 80 | + .markings("NONEXISTENT_MARKINGS") |
| 81 | + .build(); |
| 82 | + |
| 83 | + // Setup: Mock service to return empty list |
| 84 | + when(documentService.searchDocuments(any(DocumentSearchDTO.class))) |
| 85 | + .thenReturn(Collections.emptyList()); |
| 86 | + |
| 87 | + // Execute search |
| 88 | + var response = documentController.searchDocuments(searchDTO, null, null); |
| 89 | + |
| 90 | + // Verify: Should return empty list |
| 91 | + assertNotNull(response); |
| 92 | + assertNotNull(response.getBody()); |
| 93 | + assertTrue(response.getBody().isEmpty(), |
| 94 | + "Search with non-matching filters should return empty list"); |
| 95 | + } |
| 96 | +} |
0 commit comments