|
| 1 | +/* |
| 2 | + * DISCLAIMER |
| 3 | + * |
| 4 | + * Copyright 2016 ArangoDB GmbH, Cologne, Germany |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | + */ |
| 20 | + |
| 21 | + |
| 22 | +package com.arangodb; |
| 23 | + |
| 24 | +import com.arangodb.jackson.dataformat.velocypack.VPackMapper; |
| 25 | +import com.arangodb.velocypack.VPackSlice; |
| 26 | +import com.arangodb.velocystream.Request; |
| 27 | +import com.arangodb.velocystream.RequestType; |
| 28 | +import com.arangodb.velocystream.Response; |
| 29 | +import com.fasterxml.jackson.databind.JsonNode; |
| 30 | +import com.fasterxml.jackson.databind.type.TypeFactory; |
| 31 | +import org.hamcrest.Matchers; |
| 32 | +import org.junit.BeforeClass; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.junit.runners.Parameterized; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.HashMap; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Map; |
| 42 | +import java.util.stream.Collectors; |
| 43 | +import java.util.stream.IntStream; |
| 44 | + |
| 45 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 46 | +import static org.junit.Assume.assumeTrue; |
| 47 | + |
| 48 | +/** |
| 49 | + * @author Michele Rastelli |
| 50 | + */ |
| 51 | +@RunWith(Parameterized.class) |
| 52 | +public class CursorApiTest extends BaseTest { |
| 53 | + |
| 54 | + public CursorApiTest(final ArangoDB arangoDB) { |
| 55 | + super(arangoDB); |
| 56 | + } |
| 57 | + |
| 58 | + private final VPackMapper mapper = new VPackMapper(); |
| 59 | + |
| 60 | + @BeforeClass |
| 61 | + public static void init() { |
| 62 | + BaseTest.initDB(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Consume an AQL cursor calling POST /_api/cursor/<cursor-id> |
| 67 | + */ |
| 68 | + @Test |
| 69 | + public void consumeCursorWithPOST() throws IOException { |
| 70 | + assumeTrue(isAtLeastVersion(3, 7, 11)); |
| 71 | + String cursorId = createCursor("FOR i IN 1..1000 RETURN i", IntStream.rangeClosed(1, 100).boxed().collect(Collectors.toList())); |
| 72 | + boolean hasMore = true; |
| 73 | + int i = 101; |
| 74 | + while (hasMore) { |
| 75 | + System.out.println(i); |
| 76 | + hasMore = nextBatch(cursorId, IntStream.rangeClosed(i, i + 99).boxed().collect(Collectors.toList())); |
| 77 | + i = i + 100; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private String createCursor(String query, List<Integer> expected) throws IOException { |
| 82 | + Request createRequest = new Request( |
| 83 | + db.name(), |
| 84 | + RequestType.POST, |
| 85 | + "/_api/cursor" |
| 86 | + ); |
| 87 | + Map<String, Object> requestBody = new HashMap<>(); |
| 88 | + requestBody.put("query", query); |
| 89 | + requestBody.put("batchSize", 100); |
| 90 | + createRequest.setBody(new VPackSlice(mapper.writeValueAsBytes(requestBody))); |
| 91 | + Response response = arangoDB.execute(createRequest); |
| 92 | + JsonNode responseNode = mapper.readTree(response.getBody().toByteArray()); |
| 93 | + JsonNode resultNode = responseNode.withArray("result"); |
| 94 | + List<Integer> result = mapper.readerFor(TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Integer.class)) |
| 95 | + .readValue(resultNode); |
| 96 | + assertThat(result, Matchers.is(expected)); |
| 97 | + return responseNode.path("id").textValue(); |
| 98 | + } |
| 99 | + |
| 100 | + private boolean nextBatch(String id, List<Integer> expected) throws IOException { |
| 101 | + Request createRequest = new Request( |
| 102 | + db.name(), |
| 103 | + RequestType.POST, |
| 104 | + "/_api/cursor/" + id |
| 105 | + ); |
| 106 | + Response response = arangoDB.execute(createRequest); |
| 107 | + JsonNode responseNode = mapper.readTree(response.getBody().toByteArray()); |
| 108 | + JsonNode resultNode = responseNode.withArray("result"); |
| 109 | + List<Integer> result = mapper.readerFor(TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, Integer.class)) |
| 110 | + .readValue(resultNode); |
| 111 | + assertThat(result, Matchers.is(expected)); |
| 112 | + return responseNode.path("hasMore").booleanValue(); |
| 113 | + } |
| 114 | +} |
0 commit comments