[OLINGO-545] System query options also applies to entity requests
This commit is contained in:
parent
0113414e26
commit
6421f54803
|
@ -39,6 +39,7 @@ import org.apache.olingo.commons.api.http.HttpHeader;
|
||||||
import org.apache.olingo.commons.api.http.HttpStatusCode;
|
import org.apache.olingo.commons.api.http.HttpStatusCode;
|
||||||
import org.apache.olingo.fit.AbstractBaseTestITCase;
|
import org.apache.olingo.fit.AbstractBaseTestITCase;
|
||||||
import org.apache.olingo.fit.tecsvc.TecSvcConst;
|
import org.apache.olingo.fit.tecsvc.TecSvcConst;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ExpandWithSystemQueryOptionsITCase extends AbstractBaseTestITCase {
|
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
|
@Test
|
||||||
public void testURIEscaping() {
|
public void testURIEscaping() {
|
||||||
final Map<QueryOption, Object> options = new HashMap<QueryOption, Object>();
|
final Map<QueryOption, Object> options = new HashMap<QueryOption, Object>();
|
||||||
|
|
|
@ -173,7 +173,12 @@ public class TechnicalEntityProcessor extends TechnicalProcessor
|
||||||
ODataSerializer serializer = odata.createSerializer(format);
|
ODataSerializer serializer = odata.createSerializer(format);
|
||||||
final ExpandOption expand = uriInfo.getExpandOption();
|
final ExpandOption expand = uriInfo.getExpandOption();
|
||||||
final SelectOption select = uriInfo.getSelectOption();
|
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()
|
EntitySerializerOptions.with()
|
||||||
.contextURL(format == ODataFormat.JSON_NO_METADATA ? null :
|
.contextURL(format == ODataFormat.JSON_NO_METADATA ? null :
|
||||||
getContextUrl(edmEntitySet, edmEntityType, true, expand, select))
|
getContextUrl(edmEntitySet, edmEntityType, true, expand, select))
|
||||||
|
|
|
@ -52,24 +52,39 @@ public class ExpandSystemQueryOptionHandler {
|
||||||
|
|
||||||
public void applyExpandQueryOptions(final EntitySet entitySet, final EdmEntitySet edmEntitySet,
|
public void applyExpandQueryOptions(final EntitySet entitySet, final EdmEntitySet edmEntitySet,
|
||||||
final ExpandOption expandOption) throws ODataApplicationException {
|
final ExpandOption expandOption) throws ODataApplicationException {
|
||||||
final EdmEntityType entityType = edmEntitySet.getEntityType();
|
|
||||||
if (expandOption == null) {
|
if (expandOption == null) {
|
||||||
return;
|
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()) {
|
for (ExpandItem item : expandOption.getExpandItems()) {
|
||||||
final List<UriResource> uriResourceParts = item.getResourcePath().getUriResourceParts();
|
final List<UriResource> uriResourceParts = item.getResourcePath().getUriResourceParts();
|
||||||
if (uriResourceParts.size() == 1 && uriResourceParts.get(0) instanceof UriResourceNavigation) {
|
if (uriResourceParts.size() == 1 && uriResourceParts.get(0) instanceof UriResourceNavigation) {
|
||||||
final String navPropertyName = ((UriResourceNavigation) uriResourceParts.get(0)).getProperty().getName();
|
final String navPropertyName = ((UriResourceNavigation) uriResourceParts.get(0)).getProperty().getName();
|
||||||
final EdmEntitySet targetEdmEntitySet = (EdmEntitySet) edmEntitySet.getRelatedBindingTarget(navPropertyName);
|
final EdmEntitySet targetEdmEntitySet = (EdmEntitySet) edmEntitySet.getRelatedBindingTarget(navPropertyName);
|
||||||
|
|
||||||
for (final Entity entity : entitySet.getEntities()) {
|
|
||||||
final Link link = entity.getNavigationLink(navPropertyName);
|
final Link link = entity.getNavigationLink(navPropertyName);
|
||||||
if (link != null && entityType.getNavigationProperty(navPropertyName).isCollection()) {
|
if (link != null && entityType.getNavigationProperty(navPropertyName).isCollection()) {
|
||||||
applyOptionsToEntityCollection(link.getInlineEntitySet(), targetEdmEntitySet, item.getFilterOption(),
|
applyOptionsToEntityCollection(link.getInlineEntitySet(), targetEdmEntitySet, item.getFilterOption(),
|
||||||
item.getOrderByOption(), item.getCountOption(), item.getSkipOption(), item.getTopOption());
|
item.getOrderByOption(), item.getCountOption(), item.getSkipOption(), item.getTopOption());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
throw new ODataApplicationException("Not supported resource part in expand system query option",
|
throw new ODataApplicationException("Not supported resource part in expand system query option",
|
||||||
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
|
HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ROOT);
|
||||||
|
@ -83,6 +98,7 @@ public class ExpandSystemQueryOptionHandler {
|
||||||
|
|
||||||
FilterHandler.applyFilterSystemQuery(filterOption, entitySet, edmEntitySet);
|
FilterHandler.applyFilterSystemQuery(filterOption, entitySet, edmEntitySet);
|
||||||
OrderByHandler.applyOrderByOption(orderByOption, entitySet, edmEntitySet);
|
OrderByHandler.applyOrderByOption(orderByOption, entitySet, edmEntitySet);
|
||||||
|
// TODO Add CountHandler
|
||||||
SkipHandler.applySkipSystemQueryHandler(skipOption, entitySet);
|
SkipHandler.applySkipSystemQueryHandler(skipOption, entitySet);
|
||||||
TopHandler.applyTopSystemQueryOption(topOption, entitySet);
|
TopHandler.applyTopSystemQueryOption(topOption, entitySet);
|
||||||
}
|
}
|
||||||
|
@ -105,7 +121,7 @@ public class ExpandSystemQueryOptionHandler {
|
||||||
return copiedEntitySets.get(entitySet);
|
return copiedEntitySets.get(entitySet);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Entity copyEntityShallowRekursive(final Entity entity) {
|
public Entity copyEntityShallowRekursive(final Entity entity) {
|
||||||
if (!copiedEntities.containsKey(entity)) {
|
if (!copiedEntities.containsKey(entity)) {
|
||||||
final Entity copiedEntity = new EntityImpl();
|
final Entity copiedEntity = new EntityImpl();
|
||||||
copiedEntity.getProperties().addAll(entity.getProperties());
|
copiedEntity.getProperties().addAll(entity.getProperties());
|
||||||
|
|
Loading…
Reference in New Issue