Skip to content

Commit b17d280

Browse files
committed
Merge pull request #5 from shipunyc/master
Adds tests on QueryIterable's fetchNextBlock and reset; updates version ...
2 parents dd0fb95 + 0bd97d5 commit b17d280

File tree

5 files changed

+46
-25
lines changed

5 files changed

+46
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ To get the binaries of this library as distributed by Microsoft, ready for use w
1414
<dependency>
1515
<groupId>com.microsoft.azure</groupId>
1616
<artifactId>azure-documentdb</artifactId>
17-
<version>0.9.0</version>
17+
<version>0.9.1</version>
1818
</dependency>
1919

2020
###Option 2: Source Via Git

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.microsoft.azure</groupId>
55
<artifactId>azure-documentdb</artifactId>
6-
<version>0.9.0</version>
6+
<version>0.9.1</version>
77
<name>${project.groupId}:${project.artifactId}</name>
88
<description>Java SDK for Microsoft Azure DocumentDB</description>
99
<url>http://azure.microsoft.com/en-us/services/documentdb/</url>

src/com/microsoft/azure/documentdb/HttpConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public static class Versions {
153153

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

156-
public static String USER_AGENT = "documentdb-java-sdk-0.9.0";
156+
public static String USER_AGENT = "documentdb-java-sdk-0.9.1";
157157
}
158158

159159
public static class StatusCodes {

src/com/microsoft/azure/documentdb/QueryIterable.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.Map;
77
import java.util.NoSuchElementException;
88

9-
import org.apache.commons.lang3.StringUtils;
109

1110
/**
1211
*
@@ -21,6 +20,7 @@ public class QueryIterable<T extends Resource> implements Iterable<T> {
2120
private DocumentServiceRequest request = null;
2221
private ReadType readType;
2322
private Class<T> classT;
23+
private String initialContinuation = null;
2424
private String continuation = null;
2525
private boolean hasStarted = false;
2626
private List<T> items = new ArrayList<T>();
@@ -38,6 +38,14 @@ public class QueryIterable<T extends Resource> implements Iterable<T> {
3838
this.request = request;
3939
this.readType = readType;
4040
this.classT = classT;
41+
42+
if (this.request != null && this.request.getHeaders() != null) {
43+
String continuationToken = this.request.getHeaders().get(HttpConstants.HttpHeaders.CONTINUATION);
44+
if (!QueryIterable.isNullEmptyOrFalse(continuationToken)) {
45+
this.initialContinuation = continuationToken;
46+
}
47+
}
48+
4149
this.reset();
4250
}
4351

@@ -141,16 +149,10 @@ public List<T> toList() {
141149
*/
142150
public void reset() {
143151
this.hasStarted = false;
144-
this.continuation = null;
152+
this.continuation = this.initialContinuation;
145153
this.items = new ArrayList<T>();
146154
this.currentIndex = 0;
147155
this.hasNext = true;
148-
if (this.request != null && this.request.getHeaders() != null) {
149-
String continuationToken = this.request.getHeaders().get(HttpConstants.HttpHeaders.CONTINUATION);
150-
if (!StringUtils.isBlank(continuationToken)) {
151-
this.continuation = continuationToken;
152-
}
153-
}
154156
}
155157

156158
/**
@@ -164,17 +166,17 @@ public List<T> fetchNextBlock()
164166
DocumentServiceResponse response = null;
165167
List<T> fetchedItems = null;
166168

167-
while (!this.isNullEmptyOrFalse(this.continuation) || !this.hasStarted) {
168-
if (!this.isNullEmptyOrFalse(this.continuation)) {
169-
request.getHeaders().put(HttpConstants.HttpHeaders.CONTINUATION, this.continuation);
169+
while (!QueryIterable.isNullEmptyOrFalse(this.continuation) || !this.hasStarted) {
170+
if (!QueryIterable.isNullEmptyOrFalse(this.continuation)) {
171+
this.request.getHeaders().put(HttpConstants.HttpHeaders.CONTINUATION, this.continuation);
170172
} else {
171-
request.getHeaders().remove(HttpConstants.HttpHeaders.CONTINUATION);
173+
this.request.getHeaders().remove(HttpConstants.HttpHeaders.CONTINUATION);
172174
}
173175

174176
if (this.readType == ReadType.Feed) {
175-
response = this.client.doReadFeed(request);
177+
response = this.client.doReadFeed(this.request);
176178
} else {
177-
response = this.client.doQuery(request);
179+
response = this.client.doQuery(this.request);
178180
}
179181

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

201-
private boolean isNullEmptyOrFalse(String s) {
203+
private static boolean isNullEmptyOrFalse(String s) {
202204
return s == null || s.isEmpty() || s == "false" || s == "False";
203205
}
204206
}

src/com/microsoft/azure/documentdb/test/GatewayTests.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.junit.Test;
1919
import org.junit.rules.TestRule;
2020
import org.junit.runner.Description;
21+
import org.junit.runners.model.Statement;
2122

2223
import com.microsoft.azure.documentdb.AccessCondition;
2324
import com.microsoft.azure.documentdb.AccessConditionType;
@@ -38,6 +39,7 @@
3839
import com.microsoft.azure.documentdb.MediaReadMode;
3940
import com.microsoft.azure.documentdb.Permission;
4041
import com.microsoft.azure.documentdb.PermissionMode;
42+
import com.microsoft.azure.documentdb.QueryIterable;
4143
import com.microsoft.azure.documentdb.RequestOptions;
4244
import com.microsoft.azure.documentdb.ResourceResponse;
4345
import com.microsoft.azure.documentdb.StoredProcedure;
@@ -337,9 +339,12 @@ public void testCollectionCrud() throws DocumentClientException {
337339

338340
@Test
339341
public void testQueryIterableCrud() throws DocumentClientException {
340-
DocumentClient client = new DocumentClient(HOST, MASTER_KEY, ConnectionPolicy.GetDefault(), ConsistencyLevel.Session);
341-
List<Document> documents = client.readDocuments(this.collectionForTest.getSelfLink(), null).getQueryIterable().toList();
342-
int beforeCreateDocumentsCount = documents.size();
342+
DocumentClient client = new DocumentClient(HOST,
343+
MASTER_KEY,
344+
ConnectionPolicy.GetDefault(),
345+
ConsistencyLevel.Session);
346+
List<Document> documents = client.readDocuments(this.collectionForTest.getSelfLink(),
347+
null).getQueryIterable().toList();
343348
final int numOfDocuments = 10;
344349

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

351-
int noOfDocumentsPerPage = numOfDocuments / 5;
356+
int numOfDocumentsPerPage = numOfDocuments / 5;
352357
FeedOptions fo = new FeedOptions();
353-
fo.setPageSize(noOfDocumentsPerPage);
358+
fo.setPageSize(numOfDocumentsPerPage);
354359
FeedResponse<Document> feedResponse;
355360
int i = 0;
356361
String continuationToken = null;
@@ -365,7 +370,7 @@ public void testQueryIterableCrud() throws DocumentClientException {
365370
for (Document document : feedResponse.getQueryIterable()) {
366371
i++;
367372
currentPage.add(document.getId());
368-
if (i == noOfDocumentsPerPage) {
373+
if (i == numOfDocumentsPerPage) {
369374
break;
370375
}
371376
}
@@ -385,7 +390,21 @@ public void testQueryIterableCrud() throws DocumentClientException {
385390
} while (continuationToken != null);
386391

387392
documents = client.readDocuments(this.collectionForTest.getSelfLink(), null).getQueryIterable().toList();
388-
Assert.assertEquals(beforeCreateDocumentsCount + numOfDocuments, documents.size());
393+
Assert.assertEquals(numOfDocuments, documents.size());
394+
395+
// Test fetch next block.
396+
fo = new FeedOptions();
397+
fo.setPageSize(6);
398+
399+
QueryIterable<Document> queryItr =
400+
client.readDocuments(this.collectionForTest.getSelfLink(), fo).getQueryIterable();
401+
Assert.assertEquals(6, queryItr.fetchNextBlock().size());
402+
Assert.assertEquals(4, queryItr.fetchNextBlock().size());
403+
404+
// Reset the query iterable.
405+
queryItr.reset();
406+
Assert.assertEquals(6, queryItr.fetchNextBlock().size());
407+
Assert.assertEquals(4, queryItr.fetchNextBlock().size());
389408
}
390409

391410
@Test

0 commit comments

Comments
 (0)