From 2b57ce428e60430eea3c8ee76bbd813d9696816a Mon Sep 17 00:00:00 2001 From: Kim Kern Date: Wed, 18 Dec 2024 09:12:35 +0100 Subject: [PATCH] Limit combobox court results to 200 (#2433) RISDEV-5901 Doc offices complained that 10 is not enough to find matching courts. --- .../ris/caselaw/adapter/CourtController.java | 6 ++- .../database/jpa/DatabaseCourtRepository.java | 4 +- .../jpa/PostgresCourtRepositoryImpl.java | 8 ++-- .../ris/caselaw/domain/CourtService.java | 6 +-- .../caselaw/domain/court/CourtRepository.java | 4 +- .../caselaw/adapter/CourtControllerTest.java | 38 +++++++++++++++++-- .../ris/caselaw/adapter/CourtServiceTest.java | 18 ++++----- .../DocumentationUnitIntegrationTest.java | 2 +- frontend/src/services/comboboxItemService.ts | 2 +- 9 files changed, 61 insertions(+), 27 deletions(-) diff --git a/backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/CourtController.java b/backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/CourtController.java index c7f2fe9ce7..0cfcd0ddb4 100644 --- a/backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/CourtController.java +++ b/backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/CourtController.java @@ -30,7 +30,9 @@ public CourtController(CourtService service) { */ @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) @PreAuthorize("isAuthenticated()") - public List getCourts(@RequestParam(value = "q", required = false) String searchStr) { - return service.getCourts(searchStr); + public List getCourts( + @RequestParam(value = "q", required = false) String searchStr, + @RequestParam(value = "sz", required = false, defaultValue = "200") Integer size) { + return service.getCourts(searchStr, size); } } diff --git a/backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/database/jpa/DatabaseCourtRepository.java b/backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/database/jpa/DatabaseCourtRepository.java index 049f826e63..33d598fbcd 100644 --- a/backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/database/jpa/DatabaseCourtRepository.java +++ b/backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/database/jpa/DatabaseCourtRepository.java @@ -61,7 +61,7 @@ label LIKE UPPER('%' || :searchStr || '%') ORDER BY weight, label - LIMIT 10 + LIMIT :size """) - List findBySearchStr(@Param("searchStr") String searchStr); + List findBySearchStr(@Param("searchStr") String searchStr, @Param("size") Integer size); } diff --git a/backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/database/jpa/PostgresCourtRepositoryImpl.java b/backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/database/jpa/PostgresCourtRepositoryImpl.java index bc7eb97a46..a7e78357ab 100644 --- a/backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/database/jpa/PostgresCourtRepositoryImpl.java +++ b/backend/src/main/java/de/bund/digitalservice/ris/caselaw/adapter/database/jpa/PostgresCourtRepositoryImpl.java @@ -18,8 +18,8 @@ public PostgresCourtRepositoryImpl(DatabaseCourtRepository repository) { } @Override - public List findBySearchStr(String searchString) { - return repository.findBySearchStr(searchString).stream() + public List findBySearchStr(String searchString, Integer size) { + return repository.findBySearchStr(searchString, size).stream() .map(CourtTransformer::transformToDomain) .toList(); } @@ -48,8 +48,8 @@ public Optional findUniqueBySearchString(String searchString) { } @Override - public List findAllByOrderByTypeAscLocationAsc() { - return repository.findByOrderByTypeAscLocationAsc(Limit.of(10)).stream() + public List findAllByOrderByTypeAscLocationAsc(Integer size) { + return repository.findByOrderByTypeAscLocationAsc(Limit.of(size)).stream() .map(CourtTransformer::transformToDomain) .toList(); } diff --git a/backend/src/main/java/de/bund/digitalservice/ris/caselaw/domain/CourtService.java b/backend/src/main/java/de/bund/digitalservice/ris/caselaw/domain/CourtService.java index de64ddeb0a..dc22d4da83 100644 --- a/backend/src/main/java/de/bund/digitalservice/ris/caselaw/domain/CourtService.java +++ b/backend/src/main/java/de/bund/digitalservice/ris/caselaw/domain/CourtService.java @@ -15,11 +15,11 @@ public CourtService(CourtRepository courtRepository) { this.courtRepository = courtRepository; } - public List getCourts(String searchStr) { + public List getCourts(String searchStr, Integer size) { if (searchStr != null && !searchStr.trim().isBlank()) { - return courtRepository.findBySearchStr(searchStr.trim()); + return courtRepository.findBySearchStr(searchStr.trim(), size); } - return courtRepository.findAllByOrderByTypeAscLocationAsc(); + return courtRepository.findAllByOrderByTypeAscLocationAsc(size); } } diff --git a/backend/src/main/java/de/bund/digitalservice/ris/caselaw/domain/court/CourtRepository.java b/backend/src/main/java/de/bund/digitalservice/ris/caselaw/domain/court/CourtRepository.java index ee98e9ba7d..e40fbc2fcf 100644 --- a/backend/src/main/java/de/bund/digitalservice/ris/caselaw/domain/court/CourtRepository.java +++ b/backend/src/main/java/de/bund/digitalservice/ris/caselaw/domain/court/CourtRepository.java @@ -6,11 +6,11 @@ @NoRepositoryBean public interface CourtRepository { - List findBySearchStr(String searchString); + List findBySearchStr(String searchString, Integer size); Optional findByTypeAndLocation(String type, String location); Optional findUniqueBySearchString(String searchString); - List findAllByOrderByTypeAscLocationAsc(); + List findAllByOrderByTypeAscLocationAsc(Integer size); } diff --git a/backend/src/test/java/de/bund/digitalservice/ris/caselaw/adapter/CourtControllerTest.java b/backend/src/test/java/de/bund/digitalservice/ris/caselaw/adapter/CourtControllerTest.java index 7b200fef57..a605acdf3e 100644 --- a/backend/src/test/java/de/bund/digitalservice/ris/caselaw/adapter/CourtControllerTest.java +++ b/backend/src/test/java/de/bund/digitalservice/ris/caselaw/adapter/CourtControllerTest.java @@ -1,5 +1,7 @@ package de.bund.digitalservice.ris.caselaw.adapter; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -28,8 +30,8 @@ class CourtControllerTest { @MockBean private ClientRegistrationRepository clientRegistrationRepository; @Test - void testGetCourts() { - when(service.getCourts(null)).thenReturn(new ArrayList<>()); + void testGetCourtsWithoutSize() { + when(service.getCourts(anyString(), anyInt())).thenReturn(new ArrayList<>()); risWebTestClient .withDefaultLogin() @@ -39,6 +41,36 @@ void testGetCourts() { .expectStatus() .isOk(); - verify(service, times(1)).getCourts(null); + verify(service, times(1)).getCourts(null, 200); + } + + @Test + void testGetCourtsWithSize() { + when(service.getCourts(anyString(), anyInt())).thenReturn(new ArrayList<>()); + + risWebTestClient + .withDefaultLogin() + .get() + .uri("/api/v1/caselaw/courts?sz=100") + .exchange() + .expectStatus() + .isOk(); + + verify(service, times(1)).getCourts(null, 100); + } + + @Test + void testGetCourtsWithQuery() { + when(service.getCourts(anyString(), anyInt())).thenReturn(new ArrayList<>()); + + risWebTestClient + .withDefaultLogin() + .get() + .uri("/api/v1/caselaw/courts?q=test") + .exchange() + .expectStatus() + .isOk(); + + verify(service, times(1)).getCourts("test", 200); } } diff --git a/backend/src/test/java/de/bund/digitalservice/ris/caselaw/adapter/CourtServiceTest.java b/backend/src/test/java/de/bund/digitalservice/ris/caselaw/adapter/CourtServiceTest.java index 07c60aed19..91828a3d8b 100644 --- a/backend/src/test/java/de/bund/digitalservice/ris/caselaw/adapter/CourtServiceTest.java +++ b/backend/src/test/java/de/bund/digitalservice/ris/caselaw/adapter/CourtServiceTest.java @@ -33,13 +33,13 @@ void testGetTwoDifferentCourts() { Court courtB = Court.builder().type("XYZ").location("Hamburg").build(); List returnedCourts = List.of(courtA, courtB); - when(courtRepository.findAllByOrderByTypeAscLocationAsc()).thenReturn(returnedCourts); + when(courtRepository.findAllByOrderByTypeAscLocationAsc(100)).thenReturn(returnedCourts); - List resultCourts = service.getCourts(null); + List resultCourts = service.getCourts(null, 100); Assertions.assertEquals(returnedCourts, resultCourts); - verify(courtRepository).findAllByOrderByTypeAscLocationAsc(); + verify(courtRepository).findAllByOrderByTypeAscLocationAsc(100); } @Test @@ -51,13 +51,13 @@ void testGetCourtsWithSearchString() { String trimmedSearchString = searchString.trim(); List returnedCourts = List.of(courtA, courtB); - when(courtRepository.findBySearchStr(trimmedSearchString)).thenReturn(returnedCourts); + when(courtRepository.findBySearchStr(trimmedSearchString, 100)).thenReturn(returnedCourts); - List resultCourts = service.getCourts(searchString); + List resultCourts = service.getCourts(searchString, 100); Assertions.assertEquals(returnedCourts, resultCourts); - verify(courtRepository).findBySearchStr(trimmedSearchString); + verify(courtRepository).findBySearchStr(trimmedSearchString, 100); } @Test @@ -66,12 +66,12 @@ void testGetCourtsWithBlankSearchString() { Court courtB = Court.builder().type("XYZ").location("Hamburg").build(); List returnedCourts = List.of(courtA, courtB); - when(courtRepository.findAllByOrderByTypeAscLocationAsc()).thenReturn(returnedCourts); + when(courtRepository.findAllByOrderByTypeAscLocationAsc(100)).thenReturn(returnedCourts); - List resultCourts = service.getCourts(""); + List resultCourts = service.getCourts("", 100); Assertions.assertEquals(returnedCourts, resultCourts); - verify(courtRepository).findAllByOrderByTypeAscLocationAsc(); + verify(courtRepository).findAllByOrderByTypeAscLocationAsc(100); } } diff --git a/backend/src/test/java/de/bund/digitalservice/ris/caselaw/integration/tests/DocumentationUnitIntegrationTest.java b/backend/src/test/java/de/bund/digitalservice/ris/caselaw/integration/tests/DocumentationUnitIntegrationTest.java index 5a17a7a69c..323396292f 100644 --- a/backend/src/test/java/de/bund/digitalservice/ris/caselaw/integration/tests/DocumentationUnitIntegrationTest.java +++ b/backend/src/test/java/de/bund/digitalservice/ris/caselaw/integration/tests/DocumentationUnitIntegrationTest.java @@ -216,7 +216,7 @@ void testParametrizedDocumentationUnitCreation() { when(documentNumberPatternConfig.getDocumentNumberPatterns()) .thenReturn(Map.of("DS", "XXRE0******YY")); - var court = databaseCourtRepository.findBySearchStr("AG Aachen").get(0); + var court = databaseCourtRepository.findBySearchStr("AG Aachen", 100).getFirst(); risWebTestClient .withDefaultLogin() diff --git a/frontend/src/services/comboboxItemService.ts b/frontend/src/services/comboboxItemService.ts index e0784a3d0c..cd3f18fb37 100644 --- a/frontend/src/services/comboboxItemService.ts +++ b/frontend/src/services/comboboxItemService.ts @@ -143,7 +143,7 @@ type ComboboxItemService = { const service: ComboboxItemService = { getCourts: (filter: Ref) => - fetchFromEndpoint(Endpoint.courts, filter), + fetchFromEndpoint(Endpoint.courts, filter, 200), getDocumentTypes: (filter: Ref) => fetchFromEndpoint(Endpoint.documentTypes, filter), getDependentLiteratureDocumentTypes: (filter: Ref) =>