SortUtility { public static <T extends Enum<T> & FieldNameProvider> List<Order> parse(final Class<T> enumType, final String sortString) throws SortParameterSyntaxErrorException { final List<Order> parsedSortings = new ArrayList<>(); if (sortString != null) { final StringTokenizer tupleTokenizer = new StringTokenizer(sortString, DELIMITER_SORT_TUPLE); while (tupleTokenizer.hasMoreTokens()) { final String sortTuple = tupleTokenizer.nextToken().trim(); final StringTokenizer fieldDirectionTokenizer = new StringTokenizer(sortTuple, DELIMITER_FIELD_DIRECTION); if (fieldDirectionTokenizer.countTokens() == 2) { final String fieldName = fieldDirectionTokenizer.nextToken().trim().toUpperCase(); final String sortDirectionStr = fieldDirectionTokenizer.nextToken().trim(); final T identifier = getAttributeIdentifierByName(enumType, fieldName); final Direction sortDirection = getDirection(sortDirectionStr); parsedSortings.add(new Order(sortDirection, identifier.getFieldName())); } else { throw new SortParameterSyntaxErrorException(); } } } return parsedSortings; } private  SortUtility(); static List<Order> parse(final Class<T> enumType,
            final String sortString); static final String DELIMITER_FIELD_DIRECTION; }
@Test @Description("Ascending sorting based on name.") public void parseSortParam1() { final List<Order> parse = SortUtility.parse(TargetFields.class, SORT_PARAM_1); assertThat(1).as("Count of parsing parameter").isEqualTo(parse.size()); }
@Test @Description("Ascending sorting based on name and descending sorting based on description.") public void parseSortParam2() { final List<Order> parse = SortUtility.parse(TargetFields.class, SORT_PARAM_2); assertThat(2).as("Count of parsing parameter").isEqualTo(parse.size()); }
@Test @Description("Sorting with wrong syntax leads to SortParameterSyntaxErrorException.") public void parseWrongSyntaxParam() { try { SortUtility.parse(TargetFields.class, SYNTAX_FAILURE_SORT_PARAM); fail("SortParameterSyntaxErrorException expected because of wrong syntax"); } catch (final SortParameterSyntaxErrorException e) { } }
@Test @Description("Sorting based on name with case sensitive is possible.") public void parsingIsNotCaseSensitive() { SortUtility.parse(TargetFields.class, CASE_INSENSITIVE_DIRECTION_PARAM); SortUtility.parse(TargetFields.class, CASE_INSENSITIVE_DIRECTION_PARAM_1); }
@Test @Description("Sorting with unknown direction order leads to SortParameterUnsupportedDirectionException.") public void parseWrongDirectionParam() { try { SortUtility.parse(TargetFields.class, WRONG_DIRECTION_PARAM); fail("SortParameterUnsupportedDirectionException expected because of unknown direction order"); } catch (final SortParameterUnsupportedDirectionException e) { } }
@Test @Description("Sorting with unknown field leads to SortParameterUnsupportedFieldException.") public void parseWrongFieldParam() { try { SortUtility.parse(TargetFields.class, WRONG_FIELD_PARAM); fail("SortParameterUnsupportedFieldException expected because of unknown field"); } catch (final SortParameterUnsupportedFieldException e) { } }