org.apache.olingo.server.api.uri.queryoption.CountOption Java Examples

The following examples show how to use org.apache.olingo.server.api.uri.queryoption.CountOption. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TechnicalEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private SerializerResult serializeDeltaPayloads(final ODataRequest request, final Delta delta, 
    final EdmEntitySet edmEntitySet, final EdmEntityType edmEntityType,
    final ContentType requestedFormat, final ExpandOption expand, final SelectOption select,
    final CountOption countOption, String id) throws ODataLibraryException {

  return odata.createEdmDeltaSerializer(requestedFormat, request.getHeaders(HttpHeader.ODATA_VERSION))
      .entityCollection(serviceMetadata,
      edmEntityType, delta,
      EntityCollectionSerializerOptions.with()
      .contextURL(isODataMetadataNone(requestedFormat) ? null :
        getContextUrl(request.getRawODataPath(), edmEntitySet, edmEntityType, false, expand, select, false))
    .count(countOption)
    .expand(expand).select(select)
    .id(id)
    .build());
 
}
 
Example #2
Source File: TechnicalEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private SerializerResult serializeEntityCollection(final ODataRequest request, final EntityCollection
    entityCollection, final EdmEntitySet edmEntitySet, final EdmEntityType edmEntityType,
    final ContentType requestedFormat, final ExpandOption expand, final SelectOption select,
    final CountOption countOption, String id, final boolean isContNav) throws ODataLibraryException {

  return odata.createSerializer(requestedFormat, request.getHeaders(HttpHeader.ODATA_VERSION))
      .entityCollection(
      serviceMetadata,
      edmEntityType,
      entityCollection,
      EntityCollectionSerializerOptions.with()
          .contextURL(isODataMetadataNone(requestedFormat) ? null :
              getContextUrl(request.getRawODataPath(), edmEntitySet, edmEntityType, false, expand, select, isContNav))
          .count(countOption)
          .expand(expand).select(select)
          .id(id)
          .build());
}
 
Example #3
Source File: ExpandSystemQueryOptionHandler.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void applyOptionsToEntityCollection(final EntityCollection entitySet,
    final EdmBindingTarget edmBindingTarget,
    final FilterOption filterOption, final OrderByOption orderByOption, final CountOption countOption,
    final SkipOption skipOption, final TopOption topOption, final ExpandOption expandOption,
    final UriInfoResource uriInfo, final Edm edm)
    throws ODataApplicationException {

  FilterHandler.applyFilterSystemQuery(filterOption, entitySet, uriInfo, edm);
  OrderByHandler.applyOrderByOption(orderByOption, entitySet, uriInfo, edm);
  CountHandler.applyCountSystemQueryOption(countOption, entitySet);
  SkipHandler.applySkipSystemQueryHandler(skipOption, entitySet);
  TopHandler.applyTopSystemQueryOption(topOption, entitySet);

  // Apply nested expand system query options to remaining entities
  if (expandOption != null) {
    for (final Entity entity : entitySet.getEntities()) {
      applyExpandOptionToEntity(entity, edmBindingTarget, expandOption, uriInfo, edm);
    }
  }
}
 
Example #4
Source File: TechnicalEntityProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private SerializerResult serializeReferenceCollection(final EntityCollection entityCollection,
    final EdmEntitySet edmEntitySet, final ContentType requestedFormat, final CountOption countOption)
    throws ODataLibraryException {

  return odata.createSerializer(requestedFormat)
      .referenceCollection(serviceMetadata, edmEntitySet, entityCollection,
          ReferenceCollectionSerializerOptions.with()
              .contextURL(ContextURL.with().asCollection().suffix(Suffix.REFERENCE).build())
              .count(countOption).build());
}
 
Example #5
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private List<Entity> applyCountQueryOption(EntityCollection entityCollection, List<Entity> modifiedEntityList, 
    CountOption countOption) {

  // handle $count: always return the original number of entities, without considering $top and $skip
  if (countOption != null) {
    boolean isCount = countOption.getValue();
    if (isCount) {
      entityCollection.setCount(modifiedEntityList.size());
    }
  }
  
  return modifiedEntityList;
}
 
Example #6
Source File: JsonDeltaSerializerWithNavigationsTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeltaCount() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  CountOption countOption = Mockito.mock(CountOption.class);
  Mockito.when(countOption.getValue()).thenReturn(true);
  Delta delta = new Delta();
  delta.setCount(1);
  List<DeltaLink> addedLinks = new ArrayList<DeltaLink>();
  DeltaLink link1 = new DeltaLink();
  link1.setRelationship("NavPropertyETAllPrimOne");
  link1.setSource(new URI("ESDelta(100)"));
  link1.setTarget(new URI("ESAllPrim(0)"));
  addedLinks.add(link1 );
  delta.getAddedLinks().addAll(addedLinks );
  delta.setDeltaLink(new URI("23042017"));
   InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .count(countOption)
      .build()).getContent();
     String jsonString = IOUtils.toString(stream);
     final String expectedResult = "{"
         + "\"@context\":\"$metadata#ESDelta/$delta\","
         + "\"@count\":\"1\","
         + "\"value\":[{"
         + "\"@context\":\"#ESDelta/$link\",\"source\":\"ESDelta(100)\","
         + "\"relationship\":\"NavPropertyETAllPrimOne\","
         + "\"target\":\"ESAllPrim(0)\"}],"          
         + "\"@deltaLink\":\"23042017\""
         + "}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example #7
Source File: ODataXmlSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected void writeExpandedNavigationProperty(final ServiceMetadata metadata,
    final EdmNavigationProperty property, final Link navigationLink,
    final ExpandOption innerExpand, final Integer toDepth, 
    final SelectOption innerSelect, final CountOption coutOption, 
    final boolean writeNavigationCount, final boolean writeOnlyRef,final String xml10InvalidCharReplacement,
    final Set<String> ancestors, String name,
    final XMLStreamWriter writer) throws XMLStreamException, SerializerException {
  if (property.isCollection()) {
    if (navigationLink != null && navigationLink.getInlineEntitySet() != null) {
      writer.writeStartElement(ATOM, Constants.ATOM_ELEM_FEED, NS_ATOM);
      if (writeNavigationCount) {
        writeCount(navigationLink.getInlineEntitySet(), writer);
      } else {
        if (coutOption != null && coutOption.getValue()) {
          writeCount(navigationLink.getInlineEntitySet(), writer);
        }
        writeEntitySet(metadata, property.getType(), navigationLink.getInlineEntitySet(), innerExpand, toDepth,
            innerSelect, xml10InvalidCharReplacement, writer, writeOnlyRef, name, ancestors);
      }
      writer.writeEndElement();
    }
  } else {
    if (navigationLink != null && navigationLink.getInlineEntity() != null) {
      writeEntity(metadata, property.getType(), navigationLink.getInlineEntity(), null,
          innerExpand, toDepth, innerSelect, xml10InvalidCharReplacement, writer, 
          false, writeOnlyRef, name, ancestors);
    }
  }
}
 
Example #8
Source File: JsonDeltaSerializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeltaCount() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  CountOption countOption = Mockito.mock(CountOption.class);
  Mockito.when(countOption.getValue()).thenReturn(true);
  Delta delta = new Delta();
  delta.setCount(1);
  List<DeltaLink> addedLinks = new ArrayList<DeltaLink>();
  DeltaLink link1 = new DeltaLink();
  link1.setRelationship("NavPropertyETAllPrimOne");
  link1.setSource(new URI("ESDelta(100)"));
  link1.setTarget(new URI("ESAllPrim(0)"));
  addedLinks.add(link1 );
  delta.getAddedLinks().addAll(addedLinks );
  delta.setDeltaLink(new URI("23042017"));
   InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .count(countOption)
      .build()).getContent();
     String jsonString = IOUtils.toString(stream);
     final String expectedResult = "{"
         + "\"@odata.context\":\"$metadata#ESDelta/$delta\","
         + "\"@odata.count\":\"1\","
         + "\"value\":[{"
         + "\"@odata.context\":\"#ESDelta/$link\",\"source\":\"ESDelta(100)\","
         + "\"relationship\":\"NavPropertyETAllPrimOne\","
         + "\"target\":\"ESAllPrim(0)\"}],"          
         + "\"@odata.deltaLink\":\"23042017\""
         + "}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example #9
Source File: ODataXmlSerializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void entitySetCompAllPrim() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompAllPrim");
  EntityCollection entitySet = data.readAll(edmEntitySet);
  entitySet.setCount(entitySet.getEntities().size());
  entitySet.setNext(URI.create("/next"));
  CountOption countOption = Mockito.mock(CountOption.class);
  Mockito.when(countOption.getValue()).thenReturn(true);
  InputStream result = serializer.entityCollection(metadata, edmEntitySet.getEntityType(), entitySet,
      EntityCollectionSerializerOptions.with()
          .contextURL(ContextURL.with().serviceRoot(new URI("http://host:port"))
              .entitySet(edmEntitySet).build())
          .id("http://host/svc/ESCompAllPrim")
          .count(countOption)
          .build()).getContent();
  final String resultString = IOUtils.toString(result);
  String prefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
      + "<a:feed xmlns:a=\"http://www.w3.org/2005/Atom\" "
      + "xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\" "
      + "xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" "
      + "m:context=\"http://host:port$metadata#ESCompAllPrim\" "
      + "m:metadata-etag=\"metadataETag\">"
      + "<a:id>http://host/svc/ESCompAllPrim</a:id>"
      + "<m:count>4</m:count>"
      + "<a:link rel=\"next\" href=\"/next\"></a:link>"
      + "<a:entry m:etag=\"W/&quot;32767&quot;\">"
      + "<a:id>ESCompAllPrim(32767)</a:id><a:title></a:title><a:summary></a:summary>";
  Assert.assertTrue(resultString.startsWith(prefix));
}
 
Example #10
Source File: JsonDeltaSerializerWithNavigations.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected void writeExpandedNavigationProperty(
    final ServiceMetadata metadata, final EdmNavigationProperty property,
    final Link navigationLink, final ExpandOption innerExpand,
    final SelectOption innerSelect, final CountOption innerCount,
    final boolean writeOnlyCount, final boolean writeOnlyRef, final String name,
    final JsonGenerator json, boolean isFullRepresentation) throws IOException, SerializerException {

  if (property.isCollection()) {
    if (navigationLink == null || navigationLink.getInlineEntitySet() == null) {
      json.writeFieldName(property.getName());
      json.writeStartArray();
      json.writeEndArray();
    } else if (navigationLink != null && navigationLink.getInlineEntitySet() != null) {
      if (isFullRepresentation) {
        json.writeFieldName(property.getName());
      } else {
        json.writeFieldName(property.getName() + Constants.AT + Constants.DELTAVALUE);
      }
      writeEntitySet(metadata, property.getType(), navigationLink.getInlineEntitySet(), innerExpand,
          innerSelect, writeOnlyRef, name, json, isFullRepresentation);
    }

  } else {
    if (isFullRepresentation) {
      json.writeFieldName(property.getName());
    } else {
      json.writeFieldName(property.getName()+ Constants.AT + Constants.DELTAVALUE);
    }
    if (navigationLink == null || navigationLink.getInlineEntity() == null) {
      json.writeNull();
    } else if (navigationLink != null && navigationLink.getInlineEntity() != null) {
      if (navigationLink.getInlineEntity() instanceof DeletedEntity) {
        writeDeletedEntity(navigationLink.getInlineEntity(), json);
      } else {
        writeAddedUpdatedEntity(metadata, property.getType(), navigationLink.getInlineEntity(),
            innerExpand, innerSelect, null, writeOnlyRef, name, json, isFullRepresentation);
      }
    }
  }
}
 
Example #11
Source File: DebugTabUri.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void appendCommonJsonObjects(JsonGenerator gen,
    final CountOption countOption, final SkipOption skipOption, final TopOption topOption,
    final FilterOption filterOption, final OrderByOption orderByOption,
    final SelectOption selectOption, final ExpandOption expandOption, final SearchOption searchOption,
    final ApplyOption applyOption)
    throws IOException {
  if (countOption != null) {
    gen.writeBooleanField("isCount", countOption.getValue());
  }

  if (skipOption != null) {
    gen.writeNumberField("skip", skipOption.getValue());
  }

  if (topOption != null) {
    gen.writeNumberField("top", topOption.getValue());
  }

  if (filterOption != null) {
    gen.writeFieldName("filter");
    appendExpressionJson(gen, filterOption.getExpression());
  }

  if (orderByOption != null && !orderByOption.getOrders().isEmpty()) {
    gen.writeFieldName("orderby");
    gen.writeStartObject();
    gen.writeStringField("nodeType", "orderCollection");
    gen.writeFieldName("orders");
    appendOrderByItemsJson(gen, orderByOption.getOrders());
    gen.writeEndObject();
  }

  if (selectOption != null && !selectOption.getSelectItems().isEmpty()) {
    gen.writeFieldName("select");
    appendSelectedPropertiesJson(gen, selectOption.getSelectItems());
  }

  if (expandOption != null && !expandOption.getExpandItems().isEmpty()) {
    gen.writeFieldName("expand");
    appendExpandedPropertiesJson(gen, expandOption.getExpandItems());
  }

  if (searchOption != null) {
    gen.writeFieldName("search");
    appendSearchJson(gen, searchOption.getSearchExpression());
  }

  if (applyOption != null) {
    gen.writeFieldName("apply");
    appendApplyItemsJson(gen, applyOption.getApplyItems());
  }
}
 
Example #12
Source File: ProductsEntityCollectionProcessor.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Override
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
    throws ODataApplicationException, ODataLibraryException {
    // 1st we have retrieve the requested EntitySet from the uriInfo object (representation of the parsed service URI)
    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet)resourcePaths.get(0); // in our example, the first segment is the EntitySet
    EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

    // 2nd: fetch the data from backend for this requested EntitySetName
    // it has to be delivered as EntitySet object
    EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet);

    // 3rd: apply System Query Options
    // modify the result set according to the query options, specified by the end user
    List<Entity> entityList = entityCollection.getEntities();
    EntityCollection returnEntityCollection = new EntityCollection();

    // handle $count: always return the original number of entities, without considering $top and $skip
    CountOption countOption = uriInfo.getCountOption();
    if (countOption != null) {
        boolean isCount = countOption.getValue();
        if (isCount) {
            returnEntityCollection.setCount(entityList.size());
        }
    }

    applyQueryOptions(uriInfo, entityList, returnEntityCollection);

    // 3rd: create a serializer based on the requested format (json)
    ODataSerializer serializer = odata.createSerializer(responseFormat);

    // 4th: Now serialize the content: transform from the EntitySet object to InputStream
    EdmEntityType edmEntityType = edmEntitySet.getEntityType();
    ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

    final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
    EntityCollectionSerializerOptions opts =
        EntityCollectionSerializerOptions
            .with().id(id)
            .count(countOption)
            .contextURL(contextUrl)
            .build();

    SerializerResult serializerResult = serializer.entityCollection(serviceMetadata,
                                                                    edmEntityType,
                                                                    returnEntityCollection,
                                                                    opts);
    InputStream serializedContent = serializerResult.getContent();

    // Finally: configure the response object: set the body, headers and status code
    response.setContent(serializedContent);
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example #13
Source File: ExpandItemImpl.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public CountOption getCountOption() {
  return inlineCountOption;
}
 
Example #14
Source File: UriInfoImpl.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public CountOption getCountOption() {
  return (CountOption) systemQueryOptions.get(SystemQueryOptionKind.COUNT);
}
 
Example #15
Source File: ODataJsonSerializer.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
protected void writeExpandedNavigationProperty(
    final ServiceMetadata metadata, final EdmNavigationProperty property,
    final Link navigationLink, final ExpandOption innerExpand,
    Integer toDepth, final SelectOption innerSelect, final CountOption innerCount,
    final boolean writeOnlyCount, final boolean writeOnlyRef, final Set<String> ancestors,
    String name, final JsonGenerator json) throws IOException, SerializerException, DecoderException {

  if (property.isCollection()) {
    if (writeOnlyCount) {
      if (navigationLink == null || navigationLink.getInlineEntitySet() == null) {
        writeInlineCount(property.getName(), 0, json);
      } else {
        writeInlineCount(property.getName(), navigationLink.getInlineEntitySet().getCount(), json);
      }
    } else {
      if (navigationLink == null || navigationLink.getInlineEntitySet() == null) {
        if (innerCount != null && innerCount.getValue()) {
          writeInlineCount(property.getName(), 0, json);
        }
        json.writeFieldName(property.getName());
        json.writeStartArray();
        json.writeEndArray();
      } else {
        if (innerCount != null && innerCount.getValue()) {
          writeInlineCount(property.getName(), navigationLink.getInlineEntitySet().getCount(), json);
        }
        json.writeFieldName(property.getName());
        writeEntitySet(metadata, property.getType(), navigationLink.getInlineEntitySet(), innerExpand, toDepth,
            innerSelect, writeOnlyRef, ancestors, name, json);
      }
    }
  } else {
    json.writeFieldName(property.getName());
    if (navigationLink == null || navigationLink.getInlineEntity() == null) {
      json.writeNull();
    } else {
      writeEntity(metadata, property.getType(), navigationLink.getInlineEntity(), null,
          innerExpand, toDepth, innerSelect, writeOnlyRef, ancestors, name, json);
    }
  }
}
 
Example #16
Source File: RequestURLHierarchyVisitor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(CountOption info) {
}
 
Example #17
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo,
    ContentType responseFormat) throws ODataApplicationException, SerializerException {

  // 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
  List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
  // in our example, the first segment is the EntitySet
  UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
  EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

  // 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
  EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet);

  // 3rd: apply System Query Options
  // modify the result set according to the query options, specified by the end user
  List<Entity> entityList = entityCollection.getEntities();
  EntityCollection returnEntityCollection = new EntityCollection();

  // handle $count: always return the original number of entities, without considering $top and $skip
  CountOption countOption = uriInfo.getCountOption();
  if (countOption != null) {
    boolean isCount = countOption.getValue();
    if (isCount) {
      returnEntityCollection.setCount(entityList.size());
    }
  }
  
  // handle $skip
  SkipOption skipOption = uriInfo.getSkipOption();
  if (skipOption != null) {
    int skipNumber = skipOption.getValue();
    if (skipNumber >= 0) {
      if(skipNumber <= entityList.size()) {
        entityList = entityList.subList(skipNumber, entityList.size());
      } else {
        // The client skipped all entities
        entityList.clear();
      }
    } else {
      throw new ODataApplicationException("Invalid value for $skip", HttpStatusCode.BAD_REQUEST.getStatusCode(),
          Locale.ROOT);
    }
  }
  
  // handle $top
  TopOption topOption = uriInfo.getTopOption();
  if (topOption != null) {
    int topNumber = topOption.getValue();
    if (topNumber >= 0) {
      if(topNumber <= entityList.size()) {
        entityList = entityList.subList(0, topNumber);
      }  // else the client has requested more entities than available => return what we have
    } else {
      throw new ODataApplicationException("Invalid value for $top", HttpStatusCode.BAD_REQUEST.getStatusCode(),
          Locale.ROOT);
    }
  }

  // after applying the system query options, create the EntityCollection based on the reduced list
  for (Entity entity : entityList) {
    returnEntityCollection.getEntities().add(entity);
  }

  // 4th: create a serializer based on the requested format (json)
  ODataSerializer serializer = odata.createSerializer(responseFormat);

  // and serialize the content: transform from the EntitySet object to InputStream
  EdmEntityType edmEntityType = edmEntitySet.getEntityType();
  ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

  final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
  EntityCollectionSerializerOptions opts =
      EntityCollectionSerializerOptions.with().contextURL(contextUrl).id(id).count(countOption).build();
  SerializerResult serializerResult =
      serializer.entityCollection(serviceMetadata, edmEntityType, returnEntityCollection, opts);
  InputStream serializedContent = serializerResult.getContent();

  // 5th: configure the response object: set the body, headers and status code
  response.setContent(serializedContent);
  response.setStatusCode(HttpStatusCode.OK.getStatusCode());
  response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example #18
Source File: EntityCollectionSerializerOptions.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
/** Gets the $count system query option. */
public CountOption getCount() {
  return count;
}
 
Example #19
Source File: ReferenceCollectionSerializerOptions.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
/** Gets the $count system query option. */
public CountOption getCount() {
  return count;
}
 
Example #20
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void entityCollectionStreamed() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EntityIterator entityIterator = new EntityIterator() {
    EntityCollection entityCollection = data.readAll(edmEntitySet);
    Iterator<Entity> innerIterator = entityCollection.iterator();
    
    @Override
    public List<Operation> getOperations() {
      return entityCollection.getOperations();
    } 
    
    @Override
    public boolean hasNext() {
      return innerIterator.hasNext();
    }
    @Override
    public Entity next() {
      return innerIterator.next();
    }
  };
  CountOption countOption = Mockito.mock(CountOption.class);
  Mockito.when(countOption.getValue()).thenReturn(true);

  ODataContent result = serializer.entityCollectionStreamed(
      metadata, edmEntitySet.getEntityType(), entityIterator,
      EntityCollectionSerializerOptions.with()
          .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
          .build()).getODataContent();
  ByteArrayOutputStream bout = new ByteArrayOutputStream();
  result.write(bout);
  final String resultString = new String(bout.toByteArray(), "UTF-8");

  Assert.assertThat(resultString, CoreMatchers.startsWith("{"
      + "\"@context\":\"$metadata#ESAllPrim\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":[{\"PropertyInt16\":32767,\"PropertyString\""));
  Assert.assertThat(resultString, CoreMatchers.endsWith(
      "\"PropertyTimeOfDay\":\"00:01:01\"}]}"));

  int count = 0;
  int index = -1;
  while ((index = resultString.indexOf("PropertyInt16\":", ++index)) > 0) {
    count++;
  }
  Assert.assertEquals(4, count);
}
 
Example #21
Source File: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void entityCollectionStreamed() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EntityIterator entityIterator = new EntityIterator() {
    EntityCollection entityCollection = data.readAll(edmEntitySet);
    Iterator<Entity> innerIterator = entityCollection.iterator();
    
    @Override
    public List<Operation> getOperations() {
      return entityCollection.getOperations();
    } 
    
    @Override
    public boolean hasNext() {
      return innerIterator.hasNext();
    }
    @Override
    public Entity next() {
      return innerIterator.next();
    }
  };
  CountOption countOption = Mockito.mock(CountOption.class);
  Mockito.when(countOption.getValue()).thenReturn(true);

  ODataContent result = serializer.entityCollectionStreamed(
      metadata, edmEntitySet.getEntityType(), entityIterator,
      EntityCollectionSerializerOptions.with()
          .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
          .build()).getODataContent();
  ByteArrayOutputStream bout = new ByteArrayOutputStream();
  result.write(bout);
  final String resultString = new String(bout.toByteArray(), "UTF-8");

  Assert.assertThat(resultString, CoreMatchers.startsWith("{"
      + "\"@odata.context\":\"$metadata#ESAllPrim\","
      + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":[{\"PropertyInt16\":32767,\"PropertyString\""));
  Assert.assertThat(resultString, CoreMatchers.endsWith(
      "\"PropertyTimeOfDay\":\"00:01:01\"}]}"));

  int count = 0;
  int index = -1;
  while ((index = resultString.indexOf("PropertyInt16\":", ++index)) > 0) {
    count++;
  }
  Assert.assertEquals(4, count);
}
 
Example #22
Source File: CountHandler.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public static void applyCountSystemQueryOption(final CountOption countOption, final EntityCollection entitySet) {
  if (countOption != null && countOption.getValue()) {
    entitySet.setCount(entitySet.getEntities().size());
  }
}
 
Example #23
Source File: UriInfoCrossjoin.java    From olingo-odata4 with Apache License 2.0 2 votes vote down vote up
/**
 * @return Object containing information of the $count option
 */
CountOption getCountOption();
 
Example #24
Source File: UriInfoAll.java    From olingo-odata4 with Apache License 2.0 2 votes vote down vote up
/**
 * @return Object containing information of the $count option
 */
CountOption getCountOption();
 
Example #25
Source File: UriInfoResource.java    From olingo-odata4 with Apache License 2.0 2 votes vote down vote up
/**
 * @return Object containing information of the $count option
 */
CountOption getCountOption();
 
Example #26
Source File: QueryHandler.java    From micro-integrator with Apache License 2.0 2 votes vote down vote up
/**
 * This method applies count query option to the given entity collection.
 *
 * @param countOption Count option
 * @param entitySet   Entity collection
 */
public static void applyCountSystemQueryOption(final CountOption countOption, final EntityCollection entitySet) {
    if (countOption.getValue()) {
        entitySet.setCount(entitySet.getEntities().size());
    }
}
 
Example #27
Source File: RequestURLVisitor.java    From olingo-odata4 with Apache License 2.0 votes vote down vote up
void visit(CountOption info);