From e41b81ea09c34353414d43280e535242133c4793 Mon Sep 17 00:00:00 2001 From: Archana Rai Date: Tue, 7 Feb 2017 11:28:41 +0530 Subject: [PATCH] [OLINGO-1077] EntityIterator count support Signed-off-by: Christian Amend --- .../fit/tecsvc/http/BasicStreamITCase.java | 84 +++++++++++++++++++ .../commons/api/data/EntityIterator.java | 19 +++-- .../processor/TechnicalEntityProcessor.java | 5 ++ 3 files changed, 103 insertions(+), 5 deletions(-) diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicStreamITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicStreamITCase.java index ce7753680..42ddb93ee 100644 --- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicStreamITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicStreamITCase.java @@ -20,6 +20,7 @@ package org.apache.olingo.fit.tecsvc.http; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import java.net.HttpURLConnection; import java.net.URL; @@ -154,6 +155,89 @@ public class BasicStreamITCase extends AbstractBaseTestITCase { assertTrue(content.contains("ESStreamServerSidePaging?$format=json&%24skiptoken=2%2A10")); } + + @Test + public void streamCountXml() throws Exception { + URL url = new URL(SERVICE_URI + "ESStreamServerSidePaging?$count=true&$format=xml"); + + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod(HttpMethod.GET.name()); + connection.connect(); + + assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); + assertEquals(ContentType.APPLICATION_XML, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE))); + + final String content = IOUtils.toString(connection.getInputStream()); + assertTrue(content.contains("")); + assertTrue(content.contains("ESStreamServerSidePaging(1)")); + assertTrue(content.contains("504")); + assertTrue(content.contains("1")); + assertTrue(content.contains("readLink")); + } + + + @Test + public void streamCountJson() throws Exception { + URL url = new URL(SERVICE_URI + "ESStreamServerSidePaging?$count=true&$format=json"); + + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod(HttpMethod.GET.name()); + connection.connect(); + + assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); + assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE))); + + final String content = IOUtils.toString(connection.getInputStream()); + + assertTrue(content.contains("{\"PropertyInt16\":2,"+ + "\"PropertyStream@odata.mediaEtag\":\"eTag\",\"PropertyStream@odata.mediaContentType\":\"image/jpeg\"}")); + assertTrue(content.contains("\"@odata.nextLink\"")); + assertTrue(content.contains("ESStreamServerSidePaging?$count=true&$format=json&%24skiptoken=1%2A10")); + assertTrue(content.contains("\"@odata.count\":504")); + } + + @Test + public void streamCountFalsetXml() throws Exception { + URL url = new URL(SERVICE_URI + "ESStreamServerSidePaging?$count=false&$format=xml"); + + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod(HttpMethod.GET.name()); + connection.connect(); + + assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); + assertEquals(ContentType.APPLICATION_XML, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE))); + + final String content = IOUtils.toString(connection.getInputStream()); + assertTrue(content.contains("")); + assertTrue(content.contains("ESStreamServerSidePaging(1)")); + assertTrue(content.contains("1")); + assertTrue(content.contains("readLink")); + assertFalse(content.contains("504")); + } + + + @Test + public void streamCountFalseJson() throws Exception { + URL url = new URL(SERVICE_URI + "ESStreamServerSidePaging?$count=false&$format=json"); + + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod(HttpMethod.GET.name()); + connection.connect(); + + assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); + assertEquals(ContentType.JSON, ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE))); + + final String content = IOUtils.toString(connection.getInputStream()); + + assertTrue(content.contains("{\"PropertyInt16\":2,"+ + "\"PropertyStream@odata.mediaEtag\":\"eTag\",\"PropertyStream@odata.mediaContentType\":\"image/jpeg\"}")); + assertTrue(content.contains("\"@odata.nextLink\"")); + assertTrue(content.contains("ESStreamServerSidePaging?$count=false&$format=json&%24skiptoken=1%2A10")); + assertFalse(content.contains("\"@odata.count\":504")); + } + @Override protected ODataClient getClient() { return null; diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/data/EntityIterator.java b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/data/EntityIterator.java index 145b2ac37..809a40ee5 100644 --- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/data/EntityIterator.java +++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/data/EntityIterator.java @@ -30,6 +30,8 @@ import java.util.List; public abstract class EntityIterator extends AbstractEntityCollection implements Iterator { private URI next; + + private Integer count; /** * {@inheritDoc} */ @@ -72,18 +74,16 @@ public abstract class EntityIterator extends AbstractEntityCollection implements } /** - * {@inheritDoc} - *

- * ATTENTION: getCount is not supported by default. + * Gets count + * */ public Integer getCount() { - throw new ODataNotSupportedException("Entity Iterator does not support getCount()"); + return count; } /** * Gets next link. * - * @param next next link. */ public URI getNext() { return next; @@ -106,4 +106,13 @@ public abstract class EntityIterator extends AbstractEntityCollection implements public void setNext(final URI next) { this.next = next; } + + /** + * Sets count. + * + * @param count count value. + */ + public void setCount(final Integer count) { + this.count = count; + } } 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 e21b6465e..d7830be96 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 @@ -633,6 +633,7 @@ public class TechnicalEntityProcessor extends TechnicalProcessor EntityIterator streamCollection = new EntityIterator() { Iterator entityIterator = entityCollection.iterator(); private URI next = entityCollection.getNext(); + private Integer count = entityCollection.getCount(); @Override public List getOperations() { return entityCollection.getOperations(); @@ -642,6 +643,10 @@ public class TechnicalEntityProcessor extends TechnicalProcessor return next; } + public Integer getCount() { + return count; + } + @Override public boolean hasNext() { return entityIterator.hasNext();