Search in sources :

Example 1 with Order

use of org.springframework.data.domain.Sort.Order 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 Order

use of org.springframework.data.domain.Sort.Order 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 Order

use of org.springframework.data.domain.Sort.Order in project spring-data-keyvalue by spring-projects.

the class KeyValueQuerydslUtils method toOrderSpecifier.

/**
 * Transforms a plain {@link Order} into a QueryDsl specific {@link OrderSpecifier}.
 *
 * @param sort must not be {@literal null}.
 * @param builder must not be {@literal null}.
 * @return empty {@code OrderSpecifier<?>[]} when sort is {@literal null}.
 */
static OrderSpecifier<?>[] toOrderSpecifier(Sort sort, PathBuilder<?> builder) {
    Assert.notNull(sort, "Sort must not be null.");
    Assert.notNull(builder, "Builder must not be null.");
    List<OrderSpecifier<?>> specifiers = null;
    if (sort instanceof QSort) {
        specifiers = ((QSort) sort).getOrderSpecifiers();
    } else {
        specifiers = new ArrayList<>();
        for (Order order : sort) {
            specifiers.add(toOrderSpecifier(order, builder));
        }
    }
    return specifiers.toArray(new OrderSpecifier<?>[specifiers.size()]);
}
Also used : Order(org.springframework.data.domain.Sort.Order) QSort(org.springframework.data.querydsl.QSort) OrderSpecifier(com.querydsl.core.types.OrderSpecifier)

Example 4 with Order

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

the class QSortUnitTests method shouldSupportSortByOperatorExpressions.

// DATACMNS-566
@Test
public void shouldSupportSortByOperatorExpressions() {
    QUser user = QUser.user;
    QSort sort = new QSort(user.dateOfBirth.yearMonth().asc());
    Sort result = sort.and(Sort.by(Direction.ASC, "lastname"));
    assertThat(result).hasSize(2);
    assertThat(result).contains(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, user.dateOfBirth.yearMonth().toString()));
}
Also used : Order(org.springframework.data.domain.Sort.Order) Sort(org.springframework.data.domain.Sort) Test(org.junit.Test)

Example 5 with Order

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

the class QSortUnitTests method shouldCreateSortForDeepNestedPathCorrectly.

// DATACMNS-621
@Test
public void shouldCreateSortForDeepNestedPathCorrectly() {
    QSort sort = new QSort(wrapperForUserWrapper.wrapper.user.firstname.asc());
    assertThat(sort).contains(new Order(Direction.ASC, "wrapper.user.firstname"));
}
Also used : Order(org.springframework.data.domain.Sort.Order) Test(org.junit.Test)

Aggregations

Order (org.springframework.data.domain.Sort.Order)40 Sort (org.springframework.data.domain.Sort)20 PageRequest (org.springframework.data.domain.PageRequest)14 Test (org.junit.Test)12 Direction (org.springframework.data.domain.Sort.Direction)9 PageImpl (org.springframework.data.domain.PageImpl)7 ArrayList (java.util.ArrayList)5 Pageable (org.springframework.data.domain.Pageable)5 IdentifiableTestEntity (ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity)4 IdentifiableTestEntitySpecification (ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntitySpecification)4 Specification (org.springframework.data.jpa.domain.Specification)4 Document (org.bson.Document)2 MongoException (com.mongodb.MongoException)1 OrderSpecifier (com.querydsl.core.types.OrderSpecifier)1 PathBuilderFactory (com.querydsl.core.types.dsl.PathBuilderFactory)1 StringPath (com.querydsl.core.types.dsl.StringPath)1 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)1 Task (eu.bcvsolutions.idm.core.scheduler.api.dto.Task)1 WorkflowHistoricTaskInstanceDto (eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowHistoricTaskInstanceDto)1 Comparator (java.util.Comparator)1