You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2015/04/04 19:05:13 UTC

[07/50] [abbrv] olingo-odata4 git commit: [OLINGO-545] System query options also applies to entity requests

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


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/6421f548
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/6421f548
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/6421f548

Branch: refs/heads/OLINGO-549-ODataV4-JPA
Commit: 6421f5480398cd53fcd6601c818b3f9e196fec5c
Parents: 0113414
Author: Christian Holzer <c....@sap.com>
Authored: Wed Mar 25 16:45:12 2015 +0100
Committer: Christian Holzer <c....@sap.com>
Committed: Thu Mar 26 15:13:50 2015 +0100

----------------------------------------------------------------------
 .../ExpandWithSystemQueryOptionsITCase.java     | 71 ++++++++++++++++++--
 .../processor/TechnicalEntityProcessor.java     |  9 ++-
 .../ExpandSystemQueryOptionHandler.java         | 36 +++++++---
 3 files changed, 99 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/6421f548/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/ExpandWithSystemQueryOptionsITCase.java
----------------------------------------------------------------------
diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/ExpandWithSystemQueryOptionsITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/ExpandWithSystemQueryOptionsITCase.java
index a6b6eb9..2db9535 100644
--- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/ExpandWithSystemQueryOptionsITCase.java
+++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/ExpandWithSystemQueryOptionsITCase.java
@@ -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 {
@@ -216,20 +217,80 @@ 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>();
-    options.put(QueryOption.FILTER, "PropertyInt16 eq 1" 
-    + " and PropertyComp/PropertyComp/PropertyDuration eq duration'PT1S' and length(PropertyString) gt 4");
+    options.put(QueryOption.FILTER, "PropertyInt16 eq 1"
+        + " and PropertyComp/PropertyComp/PropertyDuration eq duration'PT1S' and length(PropertyString) gt 4");
     final ODataRetrieveResponse<ODataEntitySet> response =
         buildRequest(ES_TWO_KEY_NAV, NAV_PROPERTY_ET_TWO_KEY_NAV_MANY, options);
     final List<ODataEntity> entities = response.getBody().getEntities();
-    
+
     assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
     assertEquals(4, entities.size());
   }
-  
+
   private ODataRetrieveResponse<ODataEntitySet> buildRequest(final String entitySet, final String navigationProperty,
       final Map<QueryOption, Object> expandOptions) {
     return buildRequest(entitySet, navigationProperty, expandOptions, null);

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/6421f548/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalEntityProcessor.java
----------------------------------------------------------------------
diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalEntityProcessor.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalEntityProcessor.java
index ff7bd5f..109a192 100644
--- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalEntityProcessor.java
+++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalEntityProcessor.java
@@ -168,12 +168,17 @@ public class TechnicalEntityProcessor extends TechnicalProcessor
         edmEntitySet.getEntityType();
 
     final Entity entity = readEntity(uriInfo);
-
+    
     final ODataFormat format = ODataFormat.fromContentType(requestedContentType);
     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))

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/6421f548/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/queryoptions/ExpandSystemQueryOptionHandler.java
----------------------------------------------------------------------
diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/queryoptions/ExpandSystemQueryOptionHandler.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/queryoptions/ExpandSystemQueryOptionHandler.java
index 51fe49b..3ae3b3a 100644
--- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/queryoptions/ExpandSystemQueryOptionHandler.java
+++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/queryoptions/ExpandSystemQueryOptionHandler.java
@@ -52,23 +52,38 @@ 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());
-          }
+        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",
@@ -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);
   }
@@ -96,7 +112,7 @@ public class ExpandSystemQueryOptionHandler {
 
       copiedEntitySets.put(entitySet, copiedEntitySet);
       copiedEntitySets.put(copiedEntitySet, copiedEntitySet);
-      
+
       for (Entity entity : entitySet.getEntities()) {
         copiedEntitySet.getEntities().add(copyEntityShallowRekursive(entity));
       }
@@ -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());
@@ -120,7 +136,7 @@ public class ExpandSystemQueryOptionHandler {
       copiedEntity.setSelfLink(entity.getSelfLink());
       copiedEntity.setType(entity.getType());
       copiedEntity.getNavigationBindings().addAll(entity.getNavigationBindings());
-      
+
       copiedEntities.put(entity, copiedEntity);
       copiedEntities.put(copiedEntity, copiedEntity);