use of org.springframework.data.domain.Sort.Order in project spring-data-keyvalue by spring-projects.
the class SpelSortAccessor method resolve.
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.core.SortAccessor#resolve(org.springframework.data.keyvalue.core.query.KeyValueQuery)
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Comparator<?> resolve(KeyValueQuery<?> query) {
if (query.getSort().isUnsorted()) {
return null;
}
Optional<Comparator<?>> comparator = Optional.empty();
for (Order order : query.getSort()) {
SpelPropertyComparator<Object> spelSort = new SpelPropertyComparator<>(order.getProperty(), parser);
if (Direction.DESC.equals(order.getDirection())) {
spelSort.desc();
if (!NullHandling.NATIVE.equals(order.getNullHandling())) {
spelSort = NullHandling.NULLS_FIRST.equals(order.getNullHandling()) ? spelSort.nullsFirst() : spelSort.nullsLast();
}
}
if (!comparator.isPresent()) {
comparator = Optional.of(spelSort);
} else {
SpelPropertyComparator<Object> spelSortToUse = spelSort;
comparator = comparator.map(it -> it.thenComparing(spelSortToUse));
}
}
return comparator.orElseThrow(() -> new IllegalStateException("No sort definitions have been added to this CompoundComparator to compare"));
}
use of org.springframework.data.domain.Sort.Order in project irida by phac-nml.
the class CRUDServiceImplTest method testSearchSortSetProperty.
@Test
@SuppressWarnings("unchecked")
public void testSearchSortSetProperty() {
int page = 1;
int size = 1;
String property = "nonNull";
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, property);
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(property, sort.getProperty());
}
use of org.springframework.data.domain.Sort.Order in project irida by phac-nml.
the class CRUDServiceImplTest method testSearchSortEmptyArray.
@Test
@SuppressWarnings("unchecked")
public void testSearchSortEmptyArray() {
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, new String[0]);
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());
}
Aggregations