SortParserImpl implements SortParser { protected Optional<SortField> getSortFieldOptional(String sortString) { List<String> list = StringUtil.split(sortString, ':'); if (list.isEmpty()) { return Optional.empty(); } if (list.size() > 2) { throw new InvalidSortException( "Unable to parse sort string: " + sortString); } String fieldName = list.get(0); final boolean ascending; if (list.size() > 1) { ascending = isAscending(list.get(1)); } else { ascending = _ASC_DEFAULT; } if (_entityModel == null) { return Optional.of(new SortField(fieldName, ascending)); } Optional<EntityField> entityFieldOptional = getEntityFieldOptional( _entityModel.getEntityFieldsMap(), fieldName); return entityFieldOptional.map( entityField -> Optional.of(new SortField(entityField, ascending)) ).orElseThrow( () -> new InvalidSortException( "Unable to sort by property: " + fieldName) ); } SortParserImpl(EntityModel entityModel); @Override List<SortField> parse(String sortString);  }
@Test public void testGetSortFieldOptionalInvalidSyntax() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _sortParserImpl.getSortFieldOptional( "fieldExternal:desc:another") ).isInstanceOf( InvalidSortException.class ); exception.hasMessageStartingWith("Unable to parse sort string"); }
@Test public void testGetSortFieldOptionalNull() { Optional<SortField> sortFieldOptional = _sortParserImpl.getSortFieldOptional(null); Assert.assertTrue(!sortFieldOptional.isPresent()); }