QueryUtil { public static void setQueryParams(Query query, QueryParams queryParams) { if (query == null) { throw new IllegalArgumentException("Cannot set parameters for a null query"); } if (queryParams != null) { if (queryParams.isPagingSet()) { long page = queryParams.getPage(); long pageSize = queryParams.getPageSize(); long fromIncl = page * pageSize - pageSize; long toExcl = page * pageSize; query.setRange(fromIncl, toExcl); } if (queryParams.isOrderSet()) { String order = StringUtils.join(queryParams.getOrderList(), ", "); query.setOrdering(order); } } } private  QueryUtil(); static void setQueryParams(Query query, QueryParams queryParams); static void useFilters(Query query, Filters filters); static void useFilter(Query query, String[] properties, Object[] values, Map<String, String> fieldTypeMap); static void useFilter(Query query, String[] properties, Object[] values, Map<String, String> fieldTypeMap,
                                 InstanceSecurityRestriction restriction); static void useFilter(Query query, List<Property> properties); static void useFilter(Query query, List<Property> properties,
                                 InstanceSecurityRestriction restriction); static void useFilterFromPattern(Query query, String pattern, List<Property> properties); static String asMatchesPattern(String string); static String asEqualsPattern(String string); static void setCountResult(Query query);  }
@Test public void shouldSetQueryParams() { when(queryParams.isOrderSet()).thenReturn(true); when(queryParams.isPagingSet()).thenReturn(true); when(queryParams.getOrderList()).thenReturn(singletonList(new Order("field", "ascending"))); when(queryParams.getPage()).thenReturn(2); when(queryParams.getPageSize()).thenReturn(10); QueryUtil.setQueryParams(query, queryParams); verify(query).setRange(10, 20); verify(query).setOrdering("field ascending"); }
@Test public void shouldSetMultipleOrders() { QueryParams queryParams = new QueryParams(null, null, asList(new Order("field1", Order.Direction.DESC), new Order("field2", Order.Direction.ASC), new Order("field3", Order.Direction.ASC))); QueryUtil.setQueryParams(query, queryParams); verify(query).setOrdering("field1 descending, field2 ascending, field3 ascending"); }