SortParserImpl implements SortParser { protected Optional<EntityField> getEntityFieldOptional( Map<String, EntityField> entityFieldsMap, String fieldName) { if (fieldName.contains(StringPool.FORWARD_SLASH)) { List<String> list = StringUtil.split(fieldName, '/'); String complexTypeName = list.get(0); EntityField entityField = entityFieldsMap.get(complexTypeName); if ((entityField == null) || !Objects.equals( EntityField.Type.COMPLEX, entityField.getType())) { throw new InvalidSortException( "Unable to sort because \"" + fieldName + "\" is not a complex property"); } ComplexEntityField complexEntityField = (ComplexEntityField)entityField; return getEntityFieldOptional( complexEntityField.getEntityFieldsMap(), fieldName.substring(complexTypeName.length() + 1)); } return Optional.ofNullable(entityFieldsMap.get(fieldName)); } SortParserImpl(EntityModel entityModel); @Override List<SortField> parse(String sortString);  }
@Test public void testGetEntityFieldOptional() { Optional<EntityField> entityFieldOptional = _sortParserImpl.getEntityFieldOptional( _entityModel.getEntityFieldsMap(), "fieldExternal"); Assert.assertTrue(entityFieldOptional.isPresent()); EntityField entityField = entityFieldOptional.get(); Assert.assertEquals("fieldExternal", entityField.getName()); }
@Test public void testGetEntityFieldOptionalWithComplexType() { Optional<EntityField> entityFieldOptional = _sortParserImpl.getEntityFieldOptional( _entityModel.getEntityFieldsMap(), "complexFieldExternal/fieldInsideComplexFieldExternal"); Assert.assertTrue(entityFieldOptional.isPresent()); EntityField entityField = entityFieldOptional.get(); Assert.assertEquals( "fieldInsideComplexFieldExternal", entityField.getName()); }
@Test public void testGetEntityFieldOptionalWithInvalidType() { AbstractThrowableAssert exception = Assertions.assertThatThrownBy( () -> _sortParserImpl.getEntityFieldOptional( _entityModel.getEntityFieldsMap(), "fieldExternal2/invalidType") ).isInstanceOf( InvalidSortException.class ); exception.hasMessageEndingWith("is not a complex property"); }