DefaultOrderByFieldResolver implements OrderByFieldResolver { @Override public List<OrderBy> getOrdersBy(final String orderByParam, final Object resource) { List<OrderBy> orders = new ArrayList<>(); String orderByParameter = orderByParam; if (orderByParameter != null && !orderByParameter.isEmpty()) { Map<String, String> resourceReplacementMap = replacementMap.get(resource.getClass().getName()); orderByParameter = orderByParameter.replaceAll("\\s", ""); if (!pattern.matcher(orderByParameter).matches()) { throw new InvalidOrderByException(orderByParameter); } String[] ordersBy = orderByParameter.split(","); for (String order: ordersBy) { String[] orderByParts = order.split("-"); String fieldName = getOrderByFieldName(orderByParts, resourceReplacementMap); OrderByType type = OrderByType.resolve(orderByParts[1]); OrderBy orderBy = new OrderBy(fieldName, type); orders.add(orderBy); } } return orders; }  @Override List<OrderBy> getOrdersBy(final String orderByParam, final Object resource); void setPattern(final Pattern pattern); void setReplacementMap(final Map<String, Map<String, String>> replacementMap);  }
@Test public void testGetOrdersBy() throws Exception { String orderByParam = "name-desc,identifier-asc,citizenCountryId-desc"; Object resource = new PersonResource(); OrderBy orderBy1 = new OrderBy("name", OrderByType.DESC); OrderBy orderBy2 = new OrderBy("identifier", OrderByType.ASC); OrderBy orderBy3 = new OrderBy("citizenCountry", OrderByType.DESC); List<OrderBy> expected = Arrays.asList(orderBy1, orderBy2, orderBy3); Map<String, String> personMap = new HashMap<>(); personMap.put("name", "name"); personMap.put("identifier", "identifier"); personMap.put("citizenCountryId", "citizenCountry"); when(replacementMap.get(anyString())).thenReturn(personMap); List<OrderBy> actual = unit.getOrdersBy(orderByParam, resource); verify(replacementMap).get("org.lnu.is.resource.person.PersonResource"); assertEquals(expected, actual); }
@Test public void testGetOrdersByWithoutMap() throws Exception { String orderByParam = "name-desc,identifier-asc"; Object resource = new PersonResource(); OrderBy orderBy1 = new OrderBy("name", OrderByType.DESC); OrderBy orderBy2 = new OrderBy("identifier", OrderByType.ASC); List<OrderBy> expected = Arrays.asList(orderBy1, orderBy2); Map<String, String> personMap = new HashMap<>(); personMap.put("name", "name"); personMap.put("identifier", "identifier"); when(replacementMap.get(anyString())).thenReturn(personMap); List<OrderBy> actual = unit.getOrdersBy(orderByParam, resource); verify(replacementMap).get("org.lnu.is.resource.person.PersonResource"); assertEquals(expected, actual); }
@Test public void testGetOrdersByWithoutResourceMapAndWithFieldThatContainsID() throws Exception { String orderByParam = "name-desc,identifier-asc,citizenCountryId-desc"; Object resource = new PersonResource(); OrderBy orderBy1 = new OrderBy("name", OrderByType.DESC); OrderBy orderBy2 = new OrderBy("identifier", OrderByType.ASC); OrderBy orderBy3 = new OrderBy("citizenCountry", OrderByType.DESC); List<OrderBy> expected = Arrays.asList(orderBy1, orderBy2, orderBy3); exception.expect(IllegalArgumentException.class); exception.expectMessage("Fields that contains 'Id' are not supported for ordering.Please contact backend-dev team to fix this issue."); List<OrderBy> actual = unit.getOrdersBy(orderByParam, resource); verify(replacementMap).get("org.lnu.is.resource.person.PersonResource"); assertEquals(expected, actual); }
@Test public void testGetOrdersByWithUnknowIdResolver() throws Exception { String orderByParam = "name-desc,identifier-asc,citizenCountryId-desc"; Object resource = new PersonResource(); OrderBy orderBy1 = new OrderBy("name", OrderByType.DESC); OrderBy orderBy2 = new OrderBy("identifier", OrderByType.ASC); OrderBy orderBy3 = new OrderBy("citizenCountry", OrderByType.DESC); List<OrderBy> expected = Arrays.asList(orderBy1, orderBy2, orderBy3); Map<String, String> personMap = new HashMap<>(); personMap.put("name", "name"); personMap.put("identifier", "identifier"); when(replacementMap.get(anyString())).thenReturn(personMap); exception.expect(IllegalArgumentException.class); exception.expectMessage("Fields that contains 'Id' are not supported for ordering.Please contact backend-dev team to fix this issue."); List<OrderBy> actual = unit.getOrdersBy(orderByParam, resource); verify(replacementMap).get("org.lnu.is.resource.person.PersonResource"); assertEquals(expected, actual); }