PageableSortParameterExtractor extends AbstractSortParameterExtractor { @Override public Sort extract(Invocation invocation) { if (invocation == null) { throw new InvalidArgumentException("Invocation cannot be null"); } final Object value = invocation.getArguments()[index]; if (value == null) { throw new InvalidArgumentException("Page value should not be empty"); } if (value instanceof Pageable) { final Pageable pageable = (Pageable) value; final org.springframework.data.domain.Sort sort = pageable.getSort(); return getSort(sort); } throw new InvalidArgumentException("No valid value was passed to deduce the sort description from"); } PageableSortParameterExtractor(int index); @Override Sort extract(Invocation invocation);  }
@Test(expectedExceptions = InvalidArgumentException.class) public void testNullInvocation() { final PageableSortParameterExtractor extractor = new PageableSortParameterExtractor(0); extractor.extract(null); }
@Test(expectedExceptions = InvalidArgumentException.class) public void testPassingNullValue() { final PageableSortParameterExtractor extractor = new PageableSortParameterExtractor(0); extractor.extract(new ImmutableInvocation(null, new Object[]{null})); }
@Test(expectedExceptions = InvalidArgumentException.class) public void testPassingWrongType() { final PageableSortParameterExtractor extractor = new PageableSortParameterExtractor(0); extractor.extract(new ImmutableInvocation(null, new Object[]{new Object()})); }
@Test public void testPassingPageableWithNullSort() { final PageableSortParameterExtractor extractor = new PageableSortParameterExtractor(0); final Sort extracted = extractor.extract(new ImmutableInvocation(null, new Object[]{PageRequest.of(0, 1)})); assertThat(extracted instanceof ImmutableSort, is(true)); }
@Test public void testPassingPageableWithNullProperSort() { final PageableSortParameterExtractor extractor = new PageableSortParameterExtractor(0); final Sort extracted = extractor.extract(new ImmutableInvocation(null, new Object[]{ PageRequest.of(0, 1, org.springframework.data.domain.Sort.Direction.DESC, "a", "b")})); assertThat(extracted, is(notNullValue())); assertThat(extracted.getOrders(), hasSize(2)); assertThat(extracted.getOrders().get(0).getProperty(), is("a")); assertThat(extracted.getOrders().get(0).getDirection(), is(SortDirection.DESCENDING)); assertThat(extracted.getOrders().get(0).getNullHandling(), is(NullHandling.DEFAULT)); assertThat(extracted.getOrders().get(1).getProperty(), is("b")); assertThat(extracted.getOrders().get(1).getDirection(), is(SortDirection.DESCENDING)); assertThat(extracted.getOrders().get(1).getNullHandling(), is(NullHandling.DEFAULT)); }