Search in sources :

Example 1 with Direction

use of org.springframework.data.domain.Sort.Direction in project irida by phac-nml.

the class CRUDServiceImplTest method testSearch.

@Test
@SuppressWarnings("unchecked")
public void testSearch() {
    int page = 1;
    int size = 1;
    Direction order = Direction.ASC;
    Page<IdentifiableTestEntity> idPage = new PageImpl<>(Lists.newArrayList(new IdentifiableTestEntity(), new IdentifiableTestEntity()));
    when(crudRepository.findAll(any(Specification.class), any(Pageable.class))).thenReturn(idPage);
    Page<IdentifiableTestEntity> search = crudService.search(IdentifiableTestEntitySpecification.search(), page, size, order);
    assertEquals(2, search.getTotalElements());
    ArgumentCaptor<Pageable> pageArgument = ArgumentCaptor.forClass(Pageable.class);
    verify(crudRepository).findAll(any(Specification.class), pageArgument.capture());
    // ensure a created date sort property is set
    Pageable pagable = pageArgument.getValue();
    Order sort = pagable.getSort().iterator().next();
    assertEquals("createdDate", sort.getProperty());
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Order(org.springframework.data.domain.Sort.Order) IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity) Pageable(org.springframework.data.domain.Pageable) IdentifiableTestEntitySpecification(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntitySpecification) Specification(org.springframework.data.jpa.domain.Specification) Direction(org.springframework.data.domain.Sort.Direction) Test(org.junit.Test)

Example 2 with Direction

use of org.springframework.data.domain.Sort.Direction in project irida by phac-nml.

the class CRUDServiceImplTest method testSearchSortEmptyString.

@Test
@SuppressWarnings("unchecked")
public void testSearchSortEmptyString() {
    int page = 1;
    int size = 1;
    Direction order = Direction.ASC;
    Page<IdentifiableTestEntity> idPage = new PageImpl<>(Lists.newArrayList(new IdentifiableTestEntity(), new IdentifiableTestEntity()));
    when(crudRepository.findAll(any(Specification.class), any(Pageable.class))).thenReturn(idPage);
    Page<IdentifiableTestEntity> search = crudService.search(IdentifiableTestEntitySpecification.search(), page, size, order, "");
    assertEquals(2, search.getTotalElements());
    ArgumentCaptor<Pageable> pageArgument = ArgumentCaptor.forClass(Pageable.class);
    verify(crudRepository).findAll(any(Specification.class), pageArgument.capture());
    // ensure a created date sort property is set
    Pageable pagable = pageArgument.getValue();
    Order sort = pagable.getSort().iterator().next();
    assertEquals("createdDate", sort.getProperty());
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Order(org.springframework.data.domain.Sort.Order) IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity) Pageable(org.springframework.data.domain.Pageable) IdentifiableTestEntitySpecification(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntitySpecification) Specification(org.springframework.data.jpa.domain.Specification) Direction(org.springframework.data.domain.Sort.Direction) Test(org.junit.Test)

Example 3 with Direction

use of org.springframework.data.domain.Sort.Direction in project irida by phac-nml.

the class RemoteAPIControllerTest method testGetAjaxList.

@SuppressWarnings("unchecked")
@Test
public void testGetAjaxList() {
    int page = 0;
    int size = 10;
    int draw = 1;
    int sortColumn = 0;
    String direction = "asc";
    String searchValue = "";
    Principal principal = () -> USER_NAME;
    RemoteAPI api1 = new RemoteAPI("api name", "http://somewhere", "an api", "client1", "secret1");
    api1.setId(1L);
    RemoteAPI api2 = new RemoteAPI("api name 2", "http://nowhere", "another api", "client2", "secret2");
    api2.setId(2L);
    Page<RemoteAPI> apiPage = new PageImpl<>(Lists.newArrayList(api1, api2));
    when(remoteAPIService.search(any(Specification.class), eq(page), eq(size), any(Direction.class), any(String.class))).thenReturn(apiPage);
    Map<String, Object> ajaxAPIList = remoteAPIController.getAjaxAPIList(page, size, draw, sortColumn, direction, searchValue, principal, locale);
    verify(remoteAPIService).search(any(Specification.class), eq(page), eq(size), any(Direction.class), any(String.class));
    assertNotNull(ajaxAPIList);
    assertFalse(ajaxAPIList.isEmpty());
    List<List<String>> apiList = (List<List<String>>) ajaxAPIList.get(DataTable.RESPONSE_PARAM_DATA);
    assertEquals(2, apiList.size());
}
Also used : RemoteAPI(ca.corefacility.bioinformatics.irida.model.RemoteAPI) PageImpl(org.springframework.data.domain.PageImpl) Specification(org.springframework.data.jpa.domain.Specification) List(java.util.List) Direction(org.springframework.data.domain.Sort.Direction) Principal(java.security.Principal) Test(org.junit.Test)

Example 4 with Direction

use of org.springframework.data.domain.Sort.Direction in project irida by phac-nml.

the class ProjectSamplesControllerTest method testGetProjectsAvailableToCopySamplesAsUser.

@Test
public void testGetProjectsAvailableToCopySamplesAsUser() {
    String term = "";
    int page = 0;
    int pagesize = 10;
    Direction order = Direction.ASC;
    final Project p = new Project();
    User puser = new User(USER_NAME, null, null, null, null, null);
    puser.setSystemRole(Role.ROLE_USER);
    Page<Project> projects = new PageImpl<>(Lists.newArrayList(TestDataFactory.constructProject(), TestDataFactory.constructProject()));
    when(projectService.read(1L)).thenReturn(p);
    when(projectService.getUnassociatedProjects(p, term, page, pagesize, order, ProjectSamplesController.PROJECT_NAME_PROPERTY)).thenReturn(projects);
    Map<String, Object> projectsAvailableToCopySamples = controller.getProjectsAvailableToCopySamples(1L, term, pagesize, page);
    assertTrue(projectsAvailableToCopySamples.containsKey("total"));
    assertEquals(2L, projectsAvailableToCopySamples.get("total"));
    assertTrue(projectsAvailableToCopySamples.containsKey("projects"));
    verify(projectService).getUnassociatedProjects(p, term, page, pagesize, order, ProjectSamplesController.PROJECT_NAME_PROPERTY);
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) Direction(org.springframework.data.domain.Sort.Direction) Test(org.junit.Test)

Example 5 with Direction

use of org.springframework.data.domain.Sort.Direction in project spring-data-commons by spring-projects.

the class SortHandlerMethodArgumentResolver method legacyFoldExpressions.

/**
 * Folds the given {@link Sort} instance into two expressions. The first being the property list, the second being the
 * direction.
 *
 * @throws IllegalArgumentException if a {@link Sort} with multiple {@link Direction}s has been handed in.
 * @param sort must not be {@literal null}.
 * @return
 */
protected List<String> legacyFoldExpressions(Sort sort) {
    List<String> expressions = new ArrayList<>();
    ExpressionBuilder builder = null;
    for (Order order : sort) {
        Direction direction = order.getDirection();
        if (builder == null) {
            builder = new ExpressionBuilder(direction);
        } else if (!builder.hasSameDirectionAs(order)) {
            throw new IllegalArgumentException(String.format("%s in legacy configuration only supports a single direction to sort by!", getClass().getSimpleName()));
        }
        builder.add(order.getProperty());
    }
    return builder == null ? Collections.emptyList() : builder.dumpExpressionIfPresentInto(expressions);
}
Also used : Order(org.springframework.data.domain.Sort.Order) ArrayList(java.util.ArrayList) Direction(org.springframework.data.domain.Sort.Direction)

Aggregations

Direction (org.springframework.data.domain.Sort.Direction)13 Test (org.junit.Test)8 PageImpl (org.springframework.data.domain.PageImpl)8 Order (org.springframework.data.domain.Sort.Order)8 Specification (org.springframework.data.jpa.domain.Specification)6 IdentifiableTestEntity (ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity)4 IdentifiableTestEntitySpecification (ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntitySpecification)4 Pageable (org.springframework.data.domain.Pageable)4 User (ca.corefacility.bioinformatics.irida.model.user.User)3 Project (ca.corefacility.bioinformatics.irida.model.project.Project)2 ArrayList (java.util.ArrayList)2 RemoteAPI (ca.corefacility.bioinformatics.irida.model.RemoteAPI)1 UserSpecification (ca.corefacility.bioinformatics.irida.repositories.specification.UserSpecification)1 MongoException (com.mongodb.MongoException)1 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)1 DistributionWithAuditInfo (com.synopsys.integration.alert.common.rest.model.DistributionWithAuditInfo)1 Principal (java.security.Principal)1 List (java.util.List)1 PageRequest (org.springframework.data.domain.PageRequest)1 Sort (org.springframework.data.domain.Sort)1