Skip to content

Commit

Permalink
PoC for paginated results
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Oct 17, 2024
1 parent f353917 commit edf3389
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
20 changes: 20 additions & 0 deletions server/src/main/java/access/api/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.domain.*;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -141,6 +142,25 @@ public ResponseEntity<List<User>> search(@RequestParam(value = "query") String q
return ResponseEntity.ok(users);
}

@GetMapping("search-paginated")
public ResponseEntity<Page<User>> searchPaginated(@RequestParam(value = "query") String query,
@RequestParam(value = "pageNumber", required = false, defaultValue = "0") int pageNumber,
@RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
@RequestParam(value = "sort", required = false, defaultValue = "id") String sort,
@RequestParam(value = "sortDirection", required = false, defaultValue = "ASC") String sortDirection,
@Parameter(hidden = true) User user) {
LOG.debug(String.format("/search-paginated for user %s", user.getEduPersonPrincipalName()));
UserPermissions.assertSuperUser(user);
if (query.equals("owl")) {
List<User> content = userRepository.findAll();
PageRequest pageRequest = PageRequest.of(0, content.size(), Sort.by(Sort.Direction.ASC.name(), "id"));
return ResponseEntity.ok(new PageImpl<>(content, pageRequest, content.size()));
}
Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.fromString(sortDirection), sort));
Page<User> users = userRepository.searchByPage(query.replaceAll("@", " ") + "*", pageable);
return ResponseEntity.ok(users);
}

@GetMapping("search-by-application")
public ResponseEntity<List<UserRoles>> searchByApplication(@RequestParam(value = "query") String query,
@Parameter(hidden = true) User user) {
Expand Down
7 changes: 7 additions & 0 deletions server/src/main/java/access/repository/UserRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package access.repository;

import access.model.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -30,6 +32,11 @@ public interface UserRepository extends JpaRepository<User, Long> {
nativeQuery = true)
List<User> search(String keyWord, int limit);

@Query(value = "SELECT * FROM users WHERE MATCH (given_name, family_name, email) against (?1 IN BOOLEAN MODE)",
countQuery = "SELECT count(*) FROM users WHERE MATCH (given_name, family_name, email) against (?1 IN BOOLEAN MODE)",
nativeQuery = true)
Page<User> searchByPage(String keyWord, Pageable pageable );

@Query(value = "SELECT u.id, u.email, u.name, u.schac_home_organization, u.created_at, u.last_activity, " +
"ur.authority, r.name AS role_name, r.id AS role_id, ur.end_date " +
"FROM users u " +
Expand Down
1 change: 1 addition & 0 deletions server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ spring:
properties:
hibernate:
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
format_sql: false
open-in-view: false
show-sql: false
datasource:
Expand Down
4 changes: 3 additions & 1 deletion server/src/test/java/access/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@
"manage.url: http://localhost:8081",
"myconext.uri: http://localhost:8081/myconext/api/invite/provision-eduid",
"manage.enabled: true",
"feature.limit-institution-admin-role-visibility=true"
"feature.limit-institution-admin-role-visibility=true",
"spring.jpa.properties.hibernate.format_sql=true",
"spring.jpa.show-sql=true"
})
@SuppressWarnings("unchecked")
public abstract class AbstractTest {
Expand Down
38 changes: 38 additions & 0 deletions server/src/test/java/access/api/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.restassured.common.mapper.TypeRef;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;
Expand Down Expand Up @@ -294,6 +295,43 @@ void search() throws Exception {
assertEquals(1, users.size());
}

@Test
void searchPaginated() throws Exception {
AccessCookieFilter accessCookieFilter = openIDConnectFlow("/api/v1/users/login", SUPER_SUB);

Map<String, Object> users = given()
.when()
.filter(accessCookieFilter.cookieFilter())
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.queryParam("query", "exam")
.queryParam("pageNumber", 0)
.queryParam("pageSize", 3)
.queryParam("sort", "given_name")
.queryParam("sortDirection", Sort.Direction.ASC.name())
.get("/api/v1/users/search-paginated")
.as(new TypeRef<>() {
});
assertEquals(3, ((List<?>) users.get("content")).size());
assertEquals(6, users.get("totalElements"));
}

@Test
void searchPaginatedAll() throws Exception {
AccessCookieFilter accessCookieFilter = openIDConnectFlow("/api/v1/users/login", SUPER_SUB);

Map<String, Object> users = given()
.when()
.filter(accessCookieFilter.cookieFilter())
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.queryParam("query", "owl")
.get("/api/v1/users/search-paginated")
.as(new TypeRef<>() {
});
assertEquals(6, ((List<?>) users.get("content")).size());
}

@Test
void searchByApplication() throws Exception {
//Institution admin is enriched with Manage information
Expand Down
21 changes: 21 additions & 0 deletions server/src/test/java/access/repository/UserRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import access.AbstractTest;
import access.model.User;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -34,6 +39,22 @@ void search() {
assertEquals(3, users.size());
}

@Test
void searchByPage() {
PageRequest pageRequest = PageRequest.of(1, 3, Sort.by(Sort.Direction.DESC, "given_name"));
Page<User> users = userRepository.searchByPage("exam*", pageRequest);
assertEquals(6L, users.getTotalElements());
assertEquals(3, users.getContent().size());
}

@Test
void searchByPageSingleResult() {
PageRequest pageRequest = PageRequest.of(0, 3, Sort.by(Sort.Direction.DESC, "given_name"));
Page<User> users = userRepository.searchByPage("mary*", pageRequest);
assertEquals(1L, users.getTotalElements());
assertEquals(1, users.getContent().size());
}

@Test
void searchByApplication() {
List<String> manageIdentifiers = IntStream.range(1, 7).boxed().map(String::valueOf).collect(Collectors.toList());
Expand Down

0 comments on commit edf3389

Please sign in to comment.