SortParser { public static List<RequestSort> parseSort(Map<String, Object> rawSort) { List<RequestSort> unorderedSort = findSortFields(rawSort); List<RequestSort> orderedSort = applyOrder(unorderedSort, rawSort); LOG.info("Parsed sorts: {}", orderedSort); return orderedSort; } private  SortParser(); static List<RequestSort> parseSort(Map<String, Object> rawSort);  }
@Test public void shouldProduceEmptySort() throws Exception { Map<String, Object> rawSort = readFiltersFromResources("EmptySort.json"); List<RequestSort> requestSorts = SortParser.parseSort(rawSort); Assertions.assertThat(requestSorts) .hasSize(0); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionForIncorrectRootElement() throws Exception { Map<String, Object> rawSort = readFiltersFromResources("IncorrectSortRoot.json"); SortParser.parseSort(rawSort); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionForMultipleSortFieldsWithIncorrectOrderOperator() throws Exception { Map<String, Object> rawSort = readFiltersFromResources("MultipleSortWithIncorrectOrderOperator.json"); SortParser.parseSort(rawSort); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionForMultipleSortFieldsWithoutOrderOperator() throws Exception { Map<String, Object> rawSort = readFiltersFromResources("MultipleSortWithoutOrderOperator.json"); SortParser.parseSort(rawSort); }
@Test public void testSingleFieldWithOperator() throws Exception { Map<String, Object> rawSort = readFiltersFromResources("SingleSortRequest.json"); List<RequestSort> requestSorts = SortParser.parseSort(rawSort); Assertions.assertThat(requestSorts) .hasSize(1) .contains(RequestSort.of("name", SortType.ASC)); }
@Test public void testMultipleFieldsSort() throws Exception { Map<String, Object> rawSort = readFiltersFromResources("MultipleSortRequest.json"); List<RequestSort> requestSorts = SortParser.parseSort(rawSort); Assertions.assertThat(requestSorts) .hasSize(2) .containsSequence(RequestSort.of("name", SortType.ASC), RequestSort.of("last", SortType.DESC)); }
@Test public void testMultipleFieldsSortWithCustomOrder() throws Exception { Map<String, Object> rawSort = readFiltersFromResources("MultipleSortWithCustomOrderRequest.json"); List<RequestSort> requestSorts = SortParser.parseSort(rawSort); Assertions.assertThat(requestSorts) .hasSize(2) .containsSequence(RequestSort.of("last", SortType.DESC), RequestSort.of("name", SortType.ASC)); }