Skip to content

Commit

Permalink
Merge pull request #5 from shipunyc/master
Browse files Browse the repository at this point in the history
Adds tests on QueryIterable's fetchNextBlock and reset; updates version ...
  • Loading branch information
shipunyc committed Dec 19, 2014
2 parents dd0fb95 + 0bd97d5 commit b17d280
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To get the binaries of this library as distributed by Microsoft, ready for use w
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>0.9.0</version>
<version>0.9.1</version>
</dependency>

###Option 2: Source Via Git
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>0.9.0</version>
<version>0.9.1</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Java SDK for Microsoft Azure DocumentDB</description>
<url>http://azure.microsoft.com/en-us/services/documentdb/</url>
Expand Down
2 changes: 1 addition & 1 deletion src/com/microsoft/azure/documentdb/HttpConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static class Versions {

public static String CURRENT_VERSION = "2014-08-21";

public static String USER_AGENT = "documentdb-java-sdk-0.9.0";
public static String USER_AGENT = "documentdb-java-sdk-0.9.1";
}

public static class StatusCodes {
Expand Down
32 changes: 17 additions & 15 deletions src/com/microsoft/azure/documentdb/QueryIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Map;
import java.util.NoSuchElementException;

import org.apache.commons.lang3.StringUtils;

/**
*
Expand All @@ -21,6 +20,7 @@ public class QueryIterable<T extends Resource> implements Iterable<T> {
private DocumentServiceRequest request = null;
private ReadType readType;
private Class<T> classT;
private String initialContinuation = null;
private String continuation = null;
private boolean hasStarted = false;
private List<T> items = new ArrayList<T>();
Expand All @@ -38,6 +38,14 @@ public class QueryIterable<T extends Resource> implements Iterable<T> {
this.request = request;
this.readType = readType;
this.classT = classT;

if (this.request != null && this.request.getHeaders() != null) {
String continuationToken = this.request.getHeaders().get(HttpConstants.HttpHeaders.CONTINUATION);
if (!QueryIterable.isNullEmptyOrFalse(continuationToken)) {
this.initialContinuation = continuationToken;
}
}

this.reset();
}

Expand Down Expand Up @@ -141,16 +149,10 @@ public List<T> toList() {
*/
public void reset() {
this.hasStarted = false;
this.continuation = null;
this.continuation = this.initialContinuation;
this.items = new ArrayList<T>();
this.currentIndex = 0;
this.hasNext = true;
if (this.request != null && this.request.getHeaders() != null) {
String continuationToken = this.request.getHeaders().get(HttpConstants.HttpHeaders.CONTINUATION);
if (!StringUtils.isBlank(continuationToken)) {
this.continuation = continuationToken;
}
}
}

/**
Expand All @@ -164,17 +166,17 @@ public List<T> fetchNextBlock()
DocumentServiceResponse response = null;
List<T> fetchedItems = null;

while (!this.isNullEmptyOrFalse(this.continuation) || !this.hasStarted) {
if (!this.isNullEmptyOrFalse(this.continuation)) {
request.getHeaders().put(HttpConstants.HttpHeaders.CONTINUATION, this.continuation);
while (!QueryIterable.isNullEmptyOrFalse(this.continuation) || !this.hasStarted) {
if (!QueryIterable.isNullEmptyOrFalse(this.continuation)) {
this.request.getHeaders().put(HttpConstants.HttpHeaders.CONTINUATION, this.continuation);
} else {
request.getHeaders().remove(HttpConstants.HttpHeaders.CONTINUATION);
this.request.getHeaders().remove(HttpConstants.HttpHeaders.CONTINUATION);
}

if (this.readType == ReadType.Feed) {
response = this.client.doReadFeed(request);
response = this.client.doReadFeed(this.request);
} else {
response = this.client.doQuery(request);
response = this.client.doQuery(this.request);
}

// A retriable exception may happen. "this.hasStarted" and "this.continuation" must not be set
Expand All @@ -198,7 +200,7 @@ public List<T> fetchNextBlock()
return fetchedItems;
}

private boolean isNullEmptyOrFalse(String s) {
private static boolean isNullEmptyOrFalse(String s) {
return s == null || s.isEmpty() || s == "false" || s == "False";
}
}
33 changes: 26 additions & 7 deletions src/com/microsoft/azure/documentdb/test/GatewayTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import com.microsoft.azure.documentdb.AccessCondition;
import com.microsoft.azure.documentdb.AccessConditionType;
Expand All @@ -38,6 +39,7 @@
import com.microsoft.azure.documentdb.MediaReadMode;
import com.microsoft.azure.documentdb.Permission;
import com.microsoft.azure.documentdb.PermissionMode;
import com.microsoft.azure.documentdb.QueryIterable;
import com.microsoft.azure.documentdb.RequestOptions;
import com.microsoft.azure.documentdb.ResourceResponse;
import com.microsoft.azure.documentdb.StoredProcedure;
Expand Down Expand Up @@ -337,9 +339,12 @@ public void testCollectionCrud() throws DocumentClientException {

@Test
public void testQueryIterableCrud() throws DocumentClientException {
DocumentClient client = new DocumentClient(HOST, MASTER_KEY, ConnectionPolicy.GetDefault(), ConsistencyLevel.Session);
List<Document> documents = client.readDocuments(this.collectionForTest.getSelfLink(), null).getQueryIterable().toList();
int beforeCreateDocumentsCount = documents.size();
DocumentClient client = new DocumentClient(HOST,
MASTER_KEY,
ConnectionPolicy.GetDefault(),
ConsistencyLevel.Session);
List<Document> documents = client.readDocuments(this.collectionForTest.getSelfLink(),
null).getQueryIterable().toList();
final int numOfDocuments = 10;

// Create 10 documents.
Expand All @@ -348,9 +353,9 @@ public void testQueryIterableCrud() throws DocumentClientException {
client.createDocument(this.collectionForTest.getSelfLink(), documentDefinition, null, false);
}

int noOfDocumentsPerPage = numOfDocuments / 5;
int numOfDocumentsPerPage = numOfDocuments / 5;
FeedOptions fo = new FeedOptions();
fo.setPageSize(noOfDocumentsPerPage);
fo.setPageSize(numOfDocumentsPerPage);
FeedResponse<Document> feedResponse;
int i = 0;
String continuationToken = null;
Expand All @@ -365,7 +370,7 @@ public void testQueryIterableCrud() throws DocumentClientException {
for (Document document : feedResponse.getQueryIterable()) {
i++;
currentPage.add(document.getId());
if (i == noOfDocumentsPerPage) {
if (i == numOfDocumentsPerPage) {
break;
}
}
Expand All @@ -385,7 +390,21 @@ public void testQueryIterableCrud() throws DocumentClientException {
} while (continuationToken != null);

documents = client.readDocuments(this.collectionForTest.getSelfLink(), null).getQueryIterable().toList();
Assert.assertEquals(beforeCreateDocumentsCount + numOfDocuments, documents.size());
Assert.assertEquals(numOfDocuments, documents.size());

// Test fetch next block.
fo = new FeedOptions();
fo.setPageSize(6);

QueryIterable<Document> queryItr =
client.readDocuments(this.collectionForTest.getSelfLink(), fo).getQueryIterable();
Assert.assertEquals(6, queryItr.fetchNextBlock().size());
Assert.assertEquals(4, queryItr.fetchNextBlock().size());

// Reset the query iterable.
queryItr.reset();
Assert.assertEquals(6, queryItr.fetchNextBlock().size());
Assert.assertEquals(4, queryItr.fetchNextBlock().size());
}

@Test
Expand Down

0 comments on commit b17d280

Please sign in to comment.