[OLINGO-545] System query options also applies to entity requests

This commit is contained in:
Christian Holzer 2015-03-25 16:45:12 +01:00
parent 0113414e26
commit 6421f54803
3 changed files with 99 additions and 17 deletions

View File

@ -39,6 +39,7 @@ import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.fit.AbstractBaseTestITCase;
import org.apache.olingo.fit.tecsvc.TecSvcConst;
import org.junit.Ignore;
import org.junit.Test;
public class ExpandWithSystemQueryOptionsITCase extends AbstractBaseTestITCase {
@ -217,6 +218,66 @@ public class ExpandWithSystemQueryOptionsITCase extends AbstractBaseTestITCase {
}
}
@Test
@Ignore("Server do not support navigation property count annotations")
public void testCount() {
final ODataClient client = getClient();
final Map<QueryOption, Object> options = new HashMap<QueryOption, Object>();
options.put(QueryOption.SELECT, "PropertyInt16");
options.put(QueryOption.COUNT, true);
final URI uri =
client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_TWO_KEY_NAV).expandWithOptions(
NAV_PROPERTY_ET_TWO_KEY_NAV_MANY, options).addQueryOption(QueryOption.SELECT,
"PropertyInt16,PropertyString").build();
final ODataRetrieveResponse<ODataEntitySet> response =
client.getRetrieveRequestFactory().getEntitySetRequest(uri).execute();
final List<ODataEntity> entities = response.getBody().getEntities();
assertEquals(4, entities.size());
for (final ODataEntity entity : entities) {
final Object propInt16 = entity.getProperty(PROPERTY_INT16).getPrimitiveValue().toValue();
final Object propString = entity.getProperty(PROPERTY_STRING).getPrimitiveValue().toValue();
final ODataEntitySet entitySet =
entity.getNavigationLink(NAV_PROPERTY_ET_TWO_KEY_NAV_MANY).asInlineEntitySet().getEntitySet();
if (propInt16.equals(1) && propString.equals("1")) {
assertEquals(Integer.valueOf(2), entitySet.getCount());
} else if (propInt16.equals(1) && propString.equals("2")) {
assertEquals(Integer.valueOf(2), entitySet.getCount());
} else if (propInt16.equals(2) && propString.equals("1")) {
assertEquals(Integer.valueOf(2), entitySet.getCount());
} else if (propInt16.equals(3) && propString.equals("1")) {
assertEquals(Integer.valueOf(0), entitySet.getCount());
} else {
fail();
}
}
}
@Test
public void testSingleEntiyWithExpand() {
/* A single entity request will be dispatched to a different processor method than entity set request */
final ODataClient client = getClient();
final Map<QueryOption, Object> options = new HashMap<QueryOption, Object>();
options.put(QueryOption.FILTER, "PropertyInt16 lt 2");
final Map<String, Object> keys = new HashMap<String, Object>();
keys.put("PropertyInt16", 1);
keys.put("PropertyString", "1");
final URI uri = client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_TWO_KEY_NAV).appendKeySegment(keys)
.expandWithOptions(NAV_PROPERTY_ET_KEY_NAV_MANY, options).build();
final ODataRetrieveResponse<ODataEntity> response =
client.getRetrieveRequestFactory().getEntityRequest(uri).execute();
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
final ODataEntitySet entitySet =
response.getBody().getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_MANY).asInlineEntitySet().getEntitySet();
assertEquals(1, entitySet.getEntities().size());
assertEquals(1, entitySet.getEntities().get(0).getProperty(PROPERTY_INT16).getPrimitiveValue().toValue());
}
@Test
public void testURIEscaping() {
final Map<QueryOption, Object> options = new HashMap<QueryOption, Object>();

View File

@ -173,7 +173,12 @@ public class TechnicalEntityProcessor extends TechnicalProcessor
ODataSerializer serializer = odata.createSerializer(format);
final ExpandOption expand = uriInfo.getExpandOption();
final SelectOption select = uriInfo.getSelectOption();
response.setContent(serializer.entity(edmEntitySet.getEntityType(), entity,
final ExpandSystemQueryOptionHandler expandHandler = new ExpandSystemQueryOptionHandler();
final Entity entitySerialization = expandHandler.copyEntityShallowRekursive(entity);
expandHandler.applyExpandQueryOptions(entitySerialization, edmEntitySet, expand);
response.setContent(serializer.entity(edmEntitySet.getEntityType(), entitySerialization,
EntitySerializerOptions.with()
.contextURL(format == ODataFormat.JSON_NO_METADATA ? null :
getContextUrl(edmEntitySet, edmEntityType, true, expand, select))

View File

@ -52,24 +52,39 @@ public class ExpandSystemQueryOptionHandler {
public void applyExpandQueryOptions(final EntitySet entitySet, final EdmEntitySet edmEntitySet,
final ExpandOption expandOption) throws ODataApplicationException {
final EdmEntityType entityType = edmEntitySet.getEntityType();
if (expandOption == null) {
return;
}
for (final Entity entity : entitySet.getEntities()) {
applyExpandOptionToEntity(entity, edmEntitySet, expandOption);
}
}
public void applyExpandQueryOptions(Entity entity, EdmEntitySet edmEntitySet, ExpandOption expandOption)
throws ODataApplicationException {
if (expandOption == null) {
return;
}
applyExpandOptionToEntity(entity, edmEntitySet, expandOption);
}
private void applyExpandOptionToEntity(final Entity entity, final EdmEntitySet edmEntitySet,
final ExpandOption expandOption) throws ODataApplicationException {
final EdmEntityType entityType = edmEntitySet.getEntityType();
for (ExpandItem item : expandOption.getExpandItems()) {
final List<UriResource> uriResourceParts = item.getResourcePath().getUriResourceParts();
if (uriResourceParts.size() == 1 && uriResourceParts.get(0) instanceof UriResourceNavigation) {
final String navPropertyName = ((UriResourceNavigation) uriResourceParts.get(0)).getProperty().getName();
final EdmEntitySet targetEdmEntitySet = (EdmEntitySet) edmEntitySet.getRelatedBindingTarget(navPropertyName);
for (final Entity entity : entitySet.getEntities()) {
final Link link = entity.getNavigationLink(navPropertyName);
if (link != null && entityType.getNavigationProperty(navPropertyName).isCollection()) {
applyOptionsToEntityCollection(link.getInlineEntitySet(), targetEdmEntitySet, item.getFilterOption(),
item.getOrderByOption(), item.getCountOption(), item.getSkipOption(), item.getTopOption());
}
}
} else {
throw new ODataApplicationException("Not supported resource part in expand system query option",
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
@ -83,6 +98,7 @@ public class ExpandSystemQueryOptionHandler {
FilterHandler.applyFilterSystemQuery(filterOption, entitySet, edmEntitySet);
OrderByHandler.applyOrderByOption(orderByOption, entitySet, edmEntitySet);
// TODO Add CountHandler
SkipHandler.applySkipSystemQueryHandler(skipOption, entitySet);
TopHandler.applyTopSystemQueryOption(topOption, entitySet);
}
@ -105,7 +121,7 @@ public class ExpandSystemQueryOptionHandler {
return copiedEntitySets.get(entitySet);
}
private Entity copyEntityShallowRekursive(final Entity entity) {
public Entity copyEntityShallowRekursive(final Entity entity) {
if (!copiedEntities.containsKey(entity)) {
final Entity copiedEntity = new EntityImpl();
copiedEntity.getProperties().addAll(entity.getProperties());