From fa75f4ea06277204cf5b89b154e6f186b8e4bad6 Mon Sep 17 00:00:00 2001 From: rashtao Date: Wed, 28 Aug 2019 12:25:59 +0200 Subject: [PATCH] Bugfix/catch exceptions (#24) * removed empty catch blocks in tests * bugfix collection tests * bugfix ArangoDatabaseTest * bugfix ArangoEdgeCollectionTest * bugfix ArangoVertexCollectionTest --- .../internal/ArangoExecutorAsync.java | 9 +- .../com/arangodb/ArangoCollectionTest.java | 37 +- .../java/com/arangodb/ArangoDatabaseTest.java | 27 +- .../arangodb/ArangoEdgeCollectionTest.java | 681 +++++----- .../java/com/arangodb/ArangoGraphTest.java | 426 ++++--- .../java/com/arangodb/ArangoRouteTest.java | 32 +- .../arangodb/ArangoVertexCollectionTest.java | 644 +++++----- src/test/java/com/arangodb/BaseTest.java | 60 +- .../com/arangodb/example/ExampleBase.java | 50 +- .../graph/AQLActorsAndMoviesExample.java | 1113 ++++++++--------- .../arangodb/example/graph/BaseGraphTest.java | 150 ++- 11 files changed, 1612 insertions(+), 1617 deletions(-) diff --git a/src/main/java/com/arangodb/internal/ArangoExecutorAsync.java b/src/main/java/com/arangodb/internal/ArangoExecutorAsync.java index 655a9f9..65df755 100644 --- a/src/main/java/com/arangodb/internal/ArangoExecutorAsync.java +++ b/src/main/java/com/arangodb/internal/ArangoExecutorAsync.java @@ -27,10 +27,7 @@ import java.io.IOException; import java.lang.reflect.Type; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.function.Function; +import java.util.concurrent.*; /** * @author Mark Vollmary @@ -64,8 +61,8 @@ public CompletableFuture execute( final ResponseDeserializer responseDeserializer, final HostHandle hostHandle) { - return CompletableFuture.supplyAsync(() -> communication.execute(request, hostHandle), outgoingExecutor) - .thenCompose(Function.identity()) + return CompletableFuture.completedFuture(null) + .thenComposeAsync((it) -> communication.execute(request, hostHandle), outgoingExecutor) .thenApplyAsync(responseDeserializer::deserialize); } diff --git a/src/test/java/com/arangodb/ArangoCollectionTest.java b/src/test/java/com/arangodb/ArangoCollectionTest.java index ce0bd57..ec7fd52 100644 --- a/src/test/java/com/arangodb/ArangoCollectionTest.java +++ b/src/test/java/com/arangodb/ArangoCollectionTest.java @@ -29,6 +29,7 @@ import java.util.*; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutionException; import static org.hamcrest.CoreMatchers.notNullValue; @@ -331,7 +332,8 @@ public void updateDocumentIfMatchFail() throws InterruptedException, ExecutionEx final DocumentUpdateOptions options = new DocumentUpdateOptions().ifMatch("no"); db.collection(COLLECTION_NAME).updateDocument(createResult.getKey(), doc, options).get(); fail(); - } catch (final Exception e) { + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } } @@ -508,7 +510,8 @@ public void updateDocumentIgnoreRevsFalse() throws InterruptedException, Executi final DocumentUpdateOptions options = new DocumentUpdateOptions().ignoreRevs(false); db.collection(COLLECTION_NAME).updateDocument(createResult.getKey(), doc, options).get(); fail(); - } catch (final Exception e) { + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } } @@ -581,7 +584,8 @@ public void replaceDocumentIfMatchFail() throws InterruptedException, ExecutionE final DocumentReplaceOptions options = new DocumentReplaceOptions().ifMatch("no"); db.collection(COLLECTION_NAME).replaceDocument(createResult.getKey(), doc, options).get(); fail(); - } catch (final Exception e) { + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } } @@ -598,7 +602,8 @@ public void replaceDocumentIgnoreRevsFalse() throws InterruptedException, Execut final DocumentReplaceOptions options = new DocumentReplaceOptions().ignoreRevs(false); db.collection(COLLECTION_NAME).replaceDocument(createResult.getKey(), doc, options).get(); fail(); - } catch (final Exception e) { + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } } @@ -703,7 +708,8 @@ public void deleteDocumentIfMatchFail() throws InterruptedException, ExecutionEx try { db.collection(COLLECTION_NAME).deleteDocument(createResult.getKey(), null, options).get(); fail(); - } catch (final Exception e) { + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } } @@ -843,9 +849,7 @@ public void createGeo2Index() throws ExecutionException, InterruptedException { } else { assertThat(indexResult.getType(), is(IndexType.geo2)); } - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { + } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } }).get(); @@ -1250,7 +1254,7 @@ public void importDocumentsDuplicateUpdate() throws InterruptedException, Execut } @Test - public void importDocumentsCompleteFail() { + public void importDocumentsCompleteFail() throws InterruptedException { final Collection values = new ArrayList<>(); values.add(new BaseDocument("1")); values.add(new BaseDocument("2")); @@ -1258,8 +1262,9 @@ public void importDocumentsCompleteFail() { try { db.collection(COLLECTION_NAME).importDocuments(values, new DocumentImportOptions().complete(true)).get(); fail(); - } catch (InterruptedException | ExecutionException e) { - assertThat(e.getMessage(), containsString("1210")); + } catch (ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); + assertThat(((ArangoDBException) e.getCause()).getErrorNum(), is(1210)); } } @@ -1434,13 +1439,14 @@ public void importDocumentsJsonDuplicateUpdate() throws InterruptedException, Ex } @Test - public void importDocumentsJsonCompleteFail() { + public void importDocumentsJsonCompleteFail() throws InterruptedException { final String values = "[{\"_key\":\"1\"},{\"_key\":\"2\"},{\"_key\":\"2\"}]"; try { db.collection(COLLECTION_NAME).importDocuments(values, new DocumentImportOptions().complete(true)).get(); fail(); - } catch (InterruptedException | ExecutionException e) { - assertThat(e.getMessage(), containsString("1210")); + } catch (ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); + assertThat(((ArangoDBException) e.getCause()).getErrorNum(), is(1210)); } } @@ -1887,7 +1893,8 @@ public void rename() throws InterruptedException, ExecutionException { try { db.collection(COLLECTION_NAME).getInfo().get(); fail(); - } catch (final Exception e) { + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } } finally { db.collection(COLLECTION_NAME + "1").rename(COLLECTION_NAME).get(); diff --git a/src/test/java/com/arangodb/ArangoDatabaseTest.java b/src/test/java/com/arangodb/ArangoDatabaseTest.java index 9c4682b..4b41d3b 100644 --- a/src/test/java/com/arangodb/ArangoDatabaseTest.java +++ b/src/test/java/com/arangodb/ArangoDatabaseTest.java @@ -35,7 +35,6 @@ import java.io.IOException; import java.util.*; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicInteger; @@ -181,7 +180,8 @@ public void deleteCollection() throws InterruptedException, ExecutionException { try { db.collection(COLLECTION_NAME).getInfo().get(); fail(); - } catch (final Exception e) { + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } } @@ -196,7 +196,8 @@ public void deleteSystemCollection() throws InterruptedException, ExecutionExcep try { db.collection(name).getInfo().get(); fail(); - } catch (final Exception e) { + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } } @@ -210,13 +211,15 @@ public void deleteSystemCollectionFail() throws InterruptedException, ExecutionE try { db.collection(name).drop().get(); fail(); - } catch (final Exception e) { + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } db.collection(name).drop(true).get(); try { db.collection(name).getInfo().get(); fail(); - } catch (final Exception e) { + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } } @@ -563,9 +566,10 @@ public void queryWithTTL() throws InterruptedException, ExecutionException { Thread.sleep(wait * 1000); } } - fail("this should fail"); + fail(); } catch (final ArangoDBException ex) { - assertThat(ex.getMessage(), is("Response: 404, Error: 1600 - cursor not found")); + assertThat(ex.getResponseCode(), is(404)); + assertThat(ex.getErrorNum(), is(1600)); } finally { db.collection(COLLECTION_NAME).drop().get(); } @@ -934,7 +938,8 @@ public void transactionallowImplicit() throws InterruptedException, ExecutionExc options.allowImplicit(false); db.transaction(action, VPackSlice.class, options).get(); fail(); - } catch (final Exception e) { + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } } finally { db.collection("someCollection").drop().get(); @@ -1046,13 +1051,13 @@ public void getDocument() throws InterruptedException, ExecutionException { } @Test - public void shouldIncludeExceptionMessage() { + public void shouldIncludeExceptionMessage() throws InterruptedException { final String exceptionMessage = "My error context"; final String action = "function (params) {" + "throw '" + exceptionMessage + "';" + "}"; try { - db.transaction(action, VPackSlice.class, null).join(); + db.transaction(action, VPackSlice.class, null).get(); fail(); - } catch (final CompletionException e) { + } catch (final ExecutionException e) { assertThat(e.getCause(), instanceOf(ArangoDBException.class)); ArangoDBException cause = ((ArangoDBException) e.getCause()); assertThat(cause.getErrorMessage(), is(exceptionMessage)); diff --git a/src/test/java/com/arangodb/ArangoEdgeCollectionTest.java b/src/test/java/com/arangodb/ArangoEdgeCollectionTest.java index 3e9c31c..cd64b1b 100644 --- a/src/test/java/com/arangodb/ArangoEdgeCollectionTest.java +++ b/src/test/java/com/arangodb/ArangoEdgeCollectionTest.java @@ -20,10 +20,7 @@ package com.arangodb; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; @@ -50,344 +47,348 @@ /** * @author Mark Vollmary - * */ public class ArangoEdgeCollectionTest extends BaseTest { - private static final String GRAPH_NAME = "db_collection_test"; - private static final String EDGE_COLLECTION_NAME = "db_edge_collection_test"; - private static final String VERTEX_COLLECTION_NAME = "db_vertex_collection_test"; - - @BeforeClass - public static void setup() throws InterruptedException, ExecutionException { - try { - db.createCollection(VERTEX_COLLECTION_NAME, null).get(); - } catch (final Exception e) { - } - try { - db.createCollection(EDGE_COLLECTION_NAME, new CollectionCreateOptions().type(CollectionType.EDGES)).get(); - } catch (final Exception e) { - } - final Collection edgeDefinitions = new ArrayList<>(); - edgeDefinitions.add(new EdgeDefinition().collection(EDGE_COLLECTION_NAME).from(VERTEX_COLLECTION_NAME) - .to(VERTEX_COLLECTION_NAME)); - db.createGraph(GRAPH_NAME, edgeDefinitions, null).get(); - } - - @After - public void teardown() throws InterruptedException, ExecutionException { - for (final String collection : new String[] { VERTEX_COLLECTION_NAME, EDGE_COLLECTION_NAME }) { - db.collection(collection).truncate().get(); - } - } - - private BaseEdgeDocument createEdgeValue() throws InterruptedException, ExecutionException { - final VertexEntity v1 = db.graph(GRAPH_NAME).vertexCollection(VERTEX_COLLECTION_NAME) - .insertVertex(new BaseDocument(), null).get(); - final VertexEntity v2 = db.graph(GRAPH_NAME).vertexCollection(VERTEX_COLLECTION_NAME) - .insertVertex(new BaseDocument(), null).get(); - - final BaseEdgeDocument value = new BaseEdgeDocument(); - value.setFrom(v1.getId()); - value.setTo(v2.getId()); - return value; - } - - @Test - public void insertEdge() throws InterruptedException, ExecutionException { - final BaseEdgeDocument value = createEdgeValue(); - final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null).get(); - assertThat(edge, is(notNullValue())); - final BaseEdgeDocument document = db.collection(EDGE_COLLECTION_NAME) - .getDocument(edge.getKey(), BaseEdgeDocument.class, null).get(); - assertThat(document, is(notNullValue())); - assertThat(document.getKey(), is(edge.getKey())); - } - - @Test - public void getEdge() throws InterruptedException, ExecutionException { - final BaseEdgeDocument value = createEdgeValue(); - final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null).get(); - final BaseDocument document = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(edge.getKey(), BaseDocument.class, null).get(); - assertThat(document, is(notNullValue())); - assertThat(document.getKey(), is(edge.getKey())); - } - - @Test - public void getEdgeIfMatch() throws InterruptedException, ExecutionException { - final BaseEdgeDocument value = createEdgeValue(); - final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null).get(); - final DocumentReadOptions options = new DocumentReadOptions().ifMatch(edge.getRev()); - final BaseDocument document = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(edge.getKey(), BaseDocument.class, options).get(); - assertThat(document, is(notNullValue())); - assertThat(document.getKey(), is(edge.getKey())); - } - - @Test - public void getEdgeIfMatchFail() throws InterruptedException, ExecutionException { - final BaseEdgeDocument value = createEdgeValue(); - final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null).get(); - final DocumentReadOptions options = new DocumentReadOptions().ifMatch("no"); - try { - db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(edge.getKey(), BaseEdgeDocument.class, options).get(); - fail(); - } catch (final Exception e) { - } - } - - @Test - public void getEdgeIfNoneMatch() throws InterruptedException, ExecutionException { - final BaseEdgeDocument value = createEdgeValue(); - final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null).get(); - final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch("no"); - final BaseDocument document = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(edge.getKey(), BaseDocument.class, options).get(); - assertThat(document, is(notNullValue())); - assertThat(document.getKey(), is(edge.getKey())); - } - - @Test - public void getEdgeIfNoneMatchFail() throws InterruptedException, ExecutionException { - final BaseEdgeDocument value = createEdgeValue(); - final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null).get(); - final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch(edge.getRev()); - try { - db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(edge.getKey(), BaseEdgeDocument.class, options).get(); - fail(); - } catch (final Exception e) { - } - } - - @Test - public void replaceEdge() throws InterruptedException, ExecutionException { - final BaseEdgeDocument doc = createEdgeValue(); - doc.addAttribute("a", "test"); - final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) - .get(); - doc.getProperties().clear(); - doc.addAttribute("b", "test"); - final EdgeUpdateEntity replaceResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .replaceEdge(createResult.getKey(), doc, null).get(); - assertThat(replaceResult, is(notNullValue())); - assertThat(replaceResult.getId(), is(createResult.getId())); - assertThat(replaceResult.getRev(), is(not(replaceResult.getOldRev()))); - assertThat(replaceResult.getOldRev(), is(createResult.getRev())); - - final BaseEdgeDocument readResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); - assertThat(readResult.getKey(), is(createResult.getKey())); - assertThat(readResult.getRevision(), is(replaceResult.getRev())); - assertThat(readResult.getProperties().keySet(), not(hasItem("a"))); - assertThat(readResult.getAttribute("b"), is(notNullValue())); - assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); - } - - @Test - public void replaceEdgeIfMatch() throws InterruptedException, ExecutionException { - final BaseEdgeDocument doc = createEdgeValue(); - doc.addAttribute("a", "test"); - final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) - .get(); - doc.getProperties().clear(); - doc.addAttribute("b", "test"); - final EdgeReplaceOptions options = new EdgeReplaceOptions().ifMatch(createResult.getRev()); - final EdgeUpdateEntity replaceResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .replaceEdge(createResult.getKey(), doc, options).get(); - assertThat(replaceResult, is(notNullValue())); - assertThat(replaceResult.getId(), is(createResult.getId())); - assertThat(replaceResult.getRev(), is(not(replaceResult.getOldRev()))); - assertThat(replaceResult.getOldRev(), is(createResult.getRev())); - - final BaseEdgeDocument readResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); - assertThat(readResult.getKey(), is(createResult.getKey())); - assertThat(readResult.getRevision(), is(replaceResult.getRev())); - assertThat(readResult.getProperties().keySet(), not(hasItem("a"))); - assertThat(readResult.getAttribute("b"), is(notNullValue())); - assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); - } - - @Test - public void replaceEdgeIfMatchFail() throws InterruptedException, ExecutionException { - final BaseEdgeDocument doc = createEdgeValue(); - doc.addAttribute("a", "test"); - final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) - .get(); - doc.getProperties().clear(); - doc.addAttribute("b", "test"); - try { - final EdgeReplaceOptions options = new EdgeReplaceOptions().ifMatch("no"); - db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).replaceEdge(createResult.getKey(), doc, options) - .get(); - fail(); - } catch (final Exception e) { - } - } - - @Test - public void updateEdge() throws InterruptedException, ExecutionException { - final BaseEdgeDocument doc = createEdgeValue(); - doc.addAttribute("a", "test"); - doc.addAttribute("c", "test"); - final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) - .get(); - doc.updateAttribute("a", "test1"); - doc.addAttribute("b", "test"); - doc.updateAttribute("c", null); - final EdgeUpdateEntity updateResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .updateEdge(createResult.getKey(), doc, null).get(); - assertThat(updateResult, is(notNullValue())); - assertThat(updateResult.getId(), is(createResult.getId())); - assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); - assertThat(updateResult.getOldRev(), is(createResult.getRev())); - - final BaseEdgeDocument readResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); - assertThat(readResult.getKey(), is(createResult.getKey())); - assertThat(readResult.getAttribute("a"), is(notNullValue())); - assertThat(String.valueOf(readResult.getAttribute("a")), is("test1")); - assertThat(readResult.getAttribute("b"), is(notNullValue())); - assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); - assertThat(readResult.getRevision(), is(updateResult.getRev())); - assertThat(readResult.getProperties().keySet(), hasItem("c")); - } - - @Test - public void updateEdgeIfMatch() throws InterruptedException, ExecutionException { - final BaseEdgeDocument doc = createEdgeValue(); - doc.addAttribute("a", "test"); - doc.addAttribute("c", "test"); - final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) - .get(); - doc.updateAttribute("a", "test1"); - doc.addAttribute("b", "test"); - doc.updateAttribute("c", null); - final EdgeUpdateOptions options = new EdgeUpdateOptions().ifMatch(createResult.getRev()); - final EdgeUpdateEntity updateResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .updateEdge(createResult.getKey(), doc, options).get(); - assertThat(updateResult, is(notNullValue())); - assertThat(updateResult.getId(), is(createResult.getId())); - assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); - assertThat(updateResult.getOldRev(), is(createResult.getRev())); - - final BaseEdgeDocument readResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); - assertThat(readResult.getKey(), is(createResult.getKey())); - assertThat(readResult.getAttribute("a"), is(notNullValue())); - assertThat(String.valueOf(readResult.getAttribute("a")), is("test1")); - assertThat(readResult.getAttribute("b"), is(notNullValue())); - assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); - assertThat(readResult.getRevision(), is(updateResult.getRev())); - assertThat(readResult.getProperties().keySet(), hasItem("c")); - } - - @Test - public void updateEdgeIfMatchFail() throws InterruptedException, ExecutionException { - final BaseEdgeDocument doc = createEdgeValue(); - doc.addAttribute("a", "test"); - doc.addAttribute("c", "test"); - final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) - .get(); - doc.updateAttribute("a", "test1"); - doc.addAttribute("b", "test"); - doc.updateAttribute("c", null); - try { - final EdgeUpdateOptions options = new EdgeUpdateOptions().ifMatch("no"); - db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).updateEdge(createResult.getKey(), doc, options) - .get(); - fail(); - } catch (final Exception e) { - } - } - - @Test - public void updateEdgeKeepNullTrue() throws InterruptedException, ExecutionException { - final BaseEdgeDocument doc = createEdgeValue(); - doc.addAttribute("a", "test"); - final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) - .get(); - doc.updateAttribute("a", null); - final EdgeUpdateOptions options = new EdgeUpdateOptions().keepNull(true); - final EdgeUpdateEntity updateResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .updateEdge(createResult.getKey(), doc, options).get(); - assertThat(updateResult, is(notNullValue())); - assertThat(updateResult.getId(), is(createResult.getId())); - assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); - assertThat(updateResult.getOldRev(), is(createResult.getRev())); - - final BaseEdgeDocument readResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); - assertThat(readResult.getKey(), is(createResult.getKey())); - assertThat(readResult.getProperties().keySet().size(), is(1)); - assertThat(readResult.getProperties().keySet(), hasItem("a")); - } - - @Test - public void updateEdgeKeepNullFalse() throws InterruptedException, ExecutionException { - final BaseEdgeDocument doc = createEdgeValue(); - doc.addAttribute("a", "test"); - final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) - .get(); - doc.updateAttribute("a", null); - final EdgeUpdateOptions options = new EdgeUpdateOptions().keepNull(false); - final EdgeUpdateEntity updateResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .updateEdge(createResult.getKey(), doc, options).get(); - assertThat(updateResult, is(notNullValue())); - assertThat(updateResult.getId(), is(createResult.getId())); - assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); - assertThat(updateResult.getOldRev(), is(createResult.getRev())); - - final BaseEdgeDocument readResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); - assertThat(readResult.getKey(), is(createResult.getKey())); - assertThat(readResult.getId(), is(createResult.getId())); - assertThat(readResult.getRevision(), is(notNullValue())); - assertThat(readResult.getProperties().keySet(), not(hasItem("a"))); - } - - @Test - public void deleteEdge() throws InterruptedException, ExecutionException { - final BaseEdgeDocument doc = createEdgeValue(); - final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) - .get(); - db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).deleteEdge(createResult.getKey(), null).get(); - try { - db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); - fail(); - } catch (final Exception e) { - } - } - - @Test - public void deleteEdgeIfMatch() throws InterruptedException, ExecutionException { - final BaseEdgeDocument doc = createEdgeValue(); - final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) - .get(); - final EdgeDeleteOptions options = new EdgeDeleteOptions().ifMatch(createResult.getRev()); - db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).deleteEdge(createResult.getKey(), options).get(); - try { - db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) - .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); - fail(); - } catch (final Exception e) { - } - } - - @Test - public void deleteEdgeIfMatchFail() throws InterruptedException, ExecutionException { - final BaseEdgeDocument doc = createEdgeValue(); - final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) - .get(); - final EdgeDeleteOptions options = new EdgeDeleteOptions().ifMatch("no"); - try { - db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).deleteEdge(createResult.getKey(), options).get(); - fail(); - } catch (final Exception e) { + private static final String GRAPH_NAME = "db_collection_test"; + private static final String EDGE_COLLECTION_NAME = "db_edge_collection_test"; + private static final String VERTEX_COLLECTION_NAME = "db_vertex_collection_test"; + + @BeforeClass + public static void setup() throws InterruptedException, ExecutionException { + if (!db.collection(VERTEX_COLLECTION_NAME).exists().get()) { + db.createCollection(VERTEX_COLLECTION_NAME, null).get(); + } + if (!db.collection(EDGE_COLLECTION_NAME).exists().get()) { + db.createCollection(EDGE_COLLECTION_NAME, new CollectionCreateOptions().type(CollectionType.EDGES)).get(); + } + final Collection edgeDefinitions = new ArrayList<>(); + edgeDefinitions.add(new EdgeDefinition().collection(EDGE_COLLECTION_NAME).from(VERTEX_COLLECTION_NAME) + .to(VERTEX_COLLECTION_NAME)); + db.createGraph(GRAPH_NAME, edgeDefinitions, null).get(); + } + + @After + public void teardown() throws InterruptedException, ExecutionException { + for (final String collection : new String[]{VERTEX_COLLECTION_NAME, EDGE_COLLECTION_NAME}) { + db.collection(collection).truncate().get(); + } + } + + private BaseEdgeDocument createEdgeValue() throws InterruptedException, ExecutionException { + final VertexEntity v1 = db.graph(GRAPH_NAME).vertexCollection(VERTEX_COLLECTION_NAME) + .insertVertex(new BaseDocument(), null).get(); + final VertexEntity v2 = db.graph(GRAPH_NAME).vertexCollection(VERTEX_COLLECTION_NAME) + .insertVertex(new BaseDocument(), null).get(); + + final BaseEdgeDocument value = new BaseEdgeDocument(); + value.setFrom(v1.getId()); + value.setTo(v2.getId()); + return value; + } + + @Test + public void insertEdge() throws InterruptedException, ExecutionException { + final BaseEdgeDocument value = createEdgeValue(); + final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null).get(); + assertThat(edge, is(notNullValue())); + final BaseEdgeDocument document = db.collection(EDGE_COLLECTION_NAME) + .getDocument(edge.getKey(), BaseEdgeDocument.class, null).get(); + assertThat(document, is(notNullValue())); + assertThat(document.getKey(), is(edge.getKey())); + } + + @Test + public void getEdge() throws InterruptedException, ExecutionException { + final BaseEdgeDocument value = createEdgeValue(); + final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null).get(); + final BaseDocument document = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(edge.getKey(), BaseDocument.class, null).get(); + assertThat(document, is(notNullValue())); + assertThat(document.getKey(), is(edge.getKey())); + } + + @Test + public void getEdgeIfMatch() throws InterruptedException, ExecutionException { + final BaseEdgeDocument value = createEdgeValue(); + final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null).get(); + final DocumentReadOptions options = new DocumentReadOptions().ifMatch(edge.getRev()); + final BaseDocument document = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(edge.getKey(), BaseDocument.class, options).get(); + assertThat(document, is(notNullValue())); + assertThat(document.getKey(), is(edge.getKey())); + } + + @Test + public void getEdgeIfMatchFail() throws InterruptedException, ExecutionException { + final BaseEdgeDocument value = createEdgeValue(); + final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null).get(); + final DocumentReadOptions options = new DocumentReadOptions().ifMatch("no"); + try { + db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(edge.getKey(), BaseEdgeDocument.class, options).get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); + } + } + + @Test + public void getEdgeIfNoneMatch() throws InterruptedException, ExecutionException { + final BaseEdgeDocument value = createEdgeValue(); + final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null).get(); + final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch("no"); + final BaseDocument document = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(edge.getKey(), BaseDocument.class, options).get(); + assertThat(document, is(notNullValue())); + assertThat(document.getKey(), is(edge.getKey())); + } + + @Test + public void getEdgeIfNoneMatchFail() throws InterruptedException, ExecutionException { + final BaseEdgeDocument value = createEdgeValue(); + final EdgeEntity edge = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(value, null).get(); + final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch(edge.getRev()); + try { + db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(edge.getKey(), BaseEdgeDocument.class, options).get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); + } + } + + @Test + public void replaceEdge() throws InterruptedException, ExecutionException { + final BaseEdgeDocument doc = createEdgeValue(); + doc.addAttribute("a", "test"); + final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) + .get(); + doc.getProperties().clear(); + doc.addAttribute("b", "test"); + final EdgeUpdateEntity replaceResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .replaceEdge(createResult.getKey(), doc, null).get(); + assertThat(replaceResult, is(notNullValue())); + assertThat(replaceResult.getId(), is(createResult.getId())); + assertThat(replaceResult.getRev(), is(not(replaceResult.getOldRev()))); + assertThat(replaceResult.getOldRev(), is(createResult.getRev())); + + final BaseEdgeDocument readResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); + assertThat(readResult.getKey(), is(createResult.getKey())); + assertThat(readResult.getRevision(), is(replaceResult.getRev())); + assertThat(readResult.getProperties().keySet(), not(hasItem("a"))); + assertThat(readResult.getAttribute("b"), is(notNullValue())); + assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); + } + + @Test + public void replaceEdgeIfMatch() throws InterruptedException, ExecutionException { + final BaseEdgeDocument doc = createEdgeValue(); + doc.addAttribute("a", "test"); + final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) + .get(); + doc.getProperties().clear(); + doc.addAttribute("b", "test"); + final EdgeReplaceOptions options = new EdgeReplaceOptions().ifMatch(createResult.getRev()); + final EdgeUpdateEntity replaceResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .replaceEdge(createResult.getKey(), doc, options).get(); + assertThat(replaceResult, is(notNullValue())); + assertThat(replaceResult.getId(), is(createResult.getId())); + assertThat(replaceResult.getRev(), is(not(replaceResult.getOldRev()))); + assertThat(replaceResult.getOldRev(), is(createResult.getRev())); + + final BaseEdgeDocument readResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); + assertThat(readResult.getKey(), is(createResult.getKey())); + assertThat(readResult.getRevision(), is(replaceResult.getRev())); + assertThat(readResult.getProperties().keySet(), not(hasItem("a"))); + assertThat(readResult.getAttribute("b"), is(notNullValue())); + assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); + } + + @Test + public void replaceEdgeIfMatchFail() throws InterruptedException, ExecutionException { + final BaseEdgeDocument doc = createEdgeValue(); + doc.addAttribute("a", "test"); + final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) + .get(); + doc.getProperties().clear(); + doc.addAttribute("b", "test"); + try { + final EdgeReplaceOptions options = new EdgeReplaceOptions().ifMatch("no"); + db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).replaceEdge(createResult.getKey(), doc, options) + .get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); + } + } + + @Test + public void updateEdge() throws InterruptedException, ExecutionException { + final BaseEdgeDocument doc = createEdgeValue(); + doc.addAttribute("a", "test"); + doc.addAttribute("c", "test"); + final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) + .get(); + doc.updateAttribute("a", "test1"); + doc.addAttribute("b", "test"); + doc.updateAttribute("c", null); + final EdgeUpdateEntity updateResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .updateEdge(createResult.getKey(), doc, null).get(); + assertThat(updateResult, is(notNullValue())); + assertThat(updateResult.getId(), is(createResult.getId())); + assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); + assertThat(updateResult.getOldRev(), is(createResult.getRev())); + + final BaseEdgeDocument readResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); + assertThat(readResult.getKey(), is(createResult.getKey())); + assertThat(readResult.getAttribute("a"), is(notNullValue())); + assertThat(String.valueOf(readResult.getAttribute("a")), is("test1")); + assertThat(readResult.getAttribute("b"), is(notNullValue())); + assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); + assertThat(readResult.getRevision(), is(updateResult.getRev())); + assertThat(readResult.getProperties().keySet(), hasItem("c")); + } + + @Test + public void updateEdgeIfMatch() throws InterruptedException, ExecutionException { + final BaseEdgeDocument doc = createEdgeValue(); + doc.addAttribute("a", "test"); + doc.addAttribute("c", "test"); + final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) + .get(); + doc.updateAttribute("a", "test1"); + doc.addAttribute("b", "test"); + doc.updateAttribute("c", null); + final EdgeUpdateOptions options = new EdgeUpdateOptions().ifMatch(createResult.getRev()); + final EdgeUpdateEntity updateResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .updateEdge(createResult.getKey(), doc, options).get(); + assertThat(updateResult, is(notNullValue())); + assertThat(updateResult.getId(), is(createResult.getId())); + assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); + assertThat(updateResult.getOldRev(), is(createResult.getRev())); + + final BaseEdgeDocument readResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); + assertThat(readResult.getKey(), is(createResult.getKey())); + assertThat(readResult.getAttribute("a"), is(notNullValue())); + assertThat(String.valueOf(readResult.getAttribute("a")), is("test1")); + assertThat(readResult.getAttribute("b"), is(notNullValue())); + assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); + assertThat(readResult.getRevision(), is(updateResult.getRev())); + assertThat(readResult.getProperties().keySet(), hasItem("c")); + } + + @Test + public void updateEdgeIfMatchFail() throws InterruptedException, ExecutionException { + final BaseEdgeDocument doc = createEdgeValue(); + doc.addAttribute("a", "test"); + doc.addAttribute("c", "test"); + final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) + .get(); + doc.updateAttribute("a", "test1"); + doc.addAttribute("b", "test"); + doc.updateAttribute("c", null); + try { + final EdgeUpdateOptions options = new EdgeUpdateOptions().ifMatch("no"); + db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).updateEdge(createResult.getKey(), doc, options) + .get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); + } + } + + @Test + public void updateEdgeKeepNullTrue() throws InterruptedException, ExecutionException { + final BaseEdgeDocument doc = createEdgeValue(); + doc.addAttribute("a", "test"); + final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) + .get(); + doc.updateAttribute("a", null); + final EdgeUpdateOptions options = new EdgeUpdateOptions().keepNull(true); + final EdgeUpdateEntity updateResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .updateEdge(createResult.getKey(), doc, options).get(); + assertThat(updateResult, is(notNullValue())); + assertThat(updateResult.getId(), is(createResult.getId())); + assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); + assertThat(updateResult.getOldRev(), is(createResult.getRev())); + + final BaseEdgeDocument readResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); + assertThat(readResult.getKey(), is(createResult.getKey())); + assertThat(readResult.getProperties().keySet().size(), is(1)); + assertThat(readResult.getProperties().keySet(), hasItem("a")); + } + + @Test + public void updateEdgeKeepNullFalse() throws InterruptedException, ExecutionException { + final BaseEdgeDocument doc = createEdgeValue(); + doc.addAttribute("a", "test"); + final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) + .get(); + doc.updateAttribute("a", null); + final EdgeUpdateOptions options = new EdgeUpdateOptions().keepNull(false); + final EdgeUpdateEntity updateResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .updateEdge(createResult.getKey(), doc, options).get(); + assertThat(updateResult, is(notNullValue())); + assertThat(updateResult.getId(), is(createResult.getId())); + assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); + assertThat(updateResult.getOldRev(), is(createResult.getRev())); + + final BaseEdgeDocument readResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); + assertThat(readResult.getKey(), is(createResult.getKey())); + assertThat(readResult.getId(), is(createResult.getId())); + assertThat(readResult.getRevision(), is(notNullValue())); + assertThat(readResult.getProperties().keySet(), not(hasItem("a"))); + } + + @Test + public void deleteEdge() throws InterruptedException, ExecutionException { + final BaseEdgeDocument doc = createEdgeValue(); + final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) + .get(); + db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).deleteEdge(createResult.getKey(), null).get(); + try { + db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); + } + } + + @Test + public void deleteEdgeIfMatch() throws InterruptedException, ExecutionException { + final BaseEdgeDocument doc = createEdgeValue(); + final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) + .get(); + final EdgeDeleteOptions options = new EdgeDeleteOptions().ifMatch(createResult.getRev()); + db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).deleteEdge(createResult.getKey(), options).get(); + try { + db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME) + .getEdge(createResult.getKey(), BaseEdgeDocument.class, null).get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); + } + } + + @Test + public void deleteEdgeIfMatchFail() throws InterruptedException, ExecutionException { + final BaseEdgeDocument doc = createEdgeValue(); + final EdgeEntity createResult = db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(doc, null) + .get(); + final EdgeDeleteOptions options = new EdgeDeleteOptions().ifMatch("no"); + try { + db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).deleteEdge(createResult.getKey(), options).get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } - } + } } diff --git a/src/test/java/com/arangodb/ArangoGraphTest.java b/src/test/java/com/arangodb/ArangoGraphTest.java index b1745a6..75ede3b 100644 --- a/src/test/java/com/arangodb/ArangoGraphTest.java +++ b/src/test/java/com/arangodb/ArangoGraphTest.java @@ -46,237 +46,233 @@ /** * @author Mark Vollmary - * */ public class ArangoGraphTest extends BaseTest { - private static final String GRAPH_NAME = "db_collection_test"; - private static final String EDGE_COL_1 = "db_edge1_collection_test"; - private static final String EDGE_COL_2 = "db_edge2_collection_test"; - private static final String EDGE_COL_3 = "db_edge3_collection_test"; - private static final String VERTEX_COL_1 = "db_vertex1_collection_test"; - private static final String VERTEX_COL_2 = "db_vertex2_collection_test"; - private static final String VERTEX_COL_3 = "db_vertex3_collection_test"; - private static final String VERTEX_COL_4 = "db_vertex4_collection_test"; - private static final Integer REPLICATION_FACTOR = 2; - private static final Integer NUMBER_OF_SHARDS = 2; + private static final String GRAPH_NAME = "db_collection_test"; + private static final String EDGE_COL_1 = "db_edge1_collection_test"; + private static final String EDGE_COL_2 = "db_edge2_collection_test"; + private static final String EDGE_COL_3 = "db_edge3_collection_test"; + private static final String VERTEX_COL_1 = "db_vertex1_collection_test"; + private static final String VERTEX_COL_2 = "db_vertex2_collection_test"; + private static final String VERTEX_COL_3 = "db_vertex3_collection_test"; + private static final String VERTEX_COL_4 = "db_vertex4_collection_test"; + private static final Integer REPLICATION_FACTOR = 2; + private static final Integer NUMBER_OF_SHARDS = 2; - @BeforeClass - public static void setup() throws InterruptedException, ExecutionException { - try { - db.graph(GRAPH_NAME).drop().get(); - } catch (final Exception e) { - } - final Collection edgeDefinitions = new ArrayList<>(); - edgeDefinitions.add(new EdgeDefinition().collection(EDGE_COL_1).from(VERTEX_COL_1).to(VERTEX_COL_2)); - edgeDefinitions - .add(new EdgeDefinition().collection(EDGE_COL_2).from(VERTEX_COL_2).to(VERTEX_COL_1, VERTEX_COL_3)); - final GraphCreateOptions options = new GraphCreateOptions(); - if (arangoDB.getRole().get() != ServerRole.SINGLE) { - options.replicationFactor(REPLICATION_FACTOR).numberOfShards(NUMBER_OF_SHARDS); - } - db.createGraph(GRAPH_NAME, edgeDefinitions, options).get(); - } + @BeforeClass + public static void setup() throws InterruptedException, ExecutionException { + if (db.graph(GRAPH_NAME).exists().get()) { + db.graph(GRAPH_NAME).drop().get(); + } + final Collection edgeDefinitions = new ArrayList<>(); + edgeDefinitions.add(new EdgeDefinition().collection(EDGE_COL_1).from(VERTEX_COL_1).to(VERTEX_COL_2)); + edgeDefinitions + .add(new EdgeDefinition().collection(EDGE_COL_2).from(VERTEX_COL_2).to(VERTEX_COL_1, VERTEX_COL_3)); + final GraphCreateOptions options = new GraphCreateOptions(); + if (arangoDB.getRole().get() != ServerRole.SINGLE) { + options.replicationFactor(REPLICATION_FACTOR).numberOfShards(NUMBER_OF_SHARDS); + } + db.createGraph(GRAPH_NAME, edgeDefinitions, options).get(); + } - @After - public void teardown() throws InterruptedException, ExecutionException { - for (final String collection : new String[] { EDGE_COL_1, EDGE_COL_2, VERTEX_COL_1, VERTEX_COL_2, VERTEX_COL_3, - VERTEX_COL_4 }) { - final ArangoCollectionAsync c = db.collection(collection); - if (c.exists().get()) { - c.truncate().get(); - } - } - } + @After + public void teardown() throws InterruptedException, ExecutionException { + for (final String collection : new String[]{EDGE_COL_1, EDGE_COL_2, VERTEX_COL_1, VERTEX_COL_2, VERTEX_COL_3, + VERTEX_COL_4}) { + final ArangoCollectionAsync c = db.collection(collection); + if (c.exists().get()) { + c.truncate().get(); + } + } + } - @Test - public void create() throws InterruptedException, ExecutionException { - try { - final GraphEntity result = db.graph(GRAPH_NAME + "_1").create(null).get(); - assertThat(result, is(notNullValue())); - assertThat(result.getName(), is(GRAPH_NAME + "_1")); - } finally { - db.graph(GRAPH_NAME + "_1").drop().get(); - } - } + @Test + public void create() throws InterruptedException, ExecutionException { + try { + final GraphEntity result = db.graph(GRAPH_NAME + "_1").create(null).get(); + assertThat(result, is(notNullValue())); + assertThat(result.getName(), is(GRAPH_NAME + "_1")); + } finally { + db.graph(GRAPH_NAME + "_1").drop().get(); + } + } - @Test - public void getGraphs() throws InterruptedException, ExecutionException { - final Collection graphs = db.getGraphs().get(); - assertThat(graphs, is(notNullValue())); - assertThat(graphs.size(), is(1)); - } + @Test + public void getGraphs() throws InterruptedException, ExecutionException { + final Collection graphs = db.getGraphs().get(); + assertThat(graphs, is(notNullValue())); + assertThat(graphs.size(), is(1)); + } - @Test - public void getInfo() throws InterruptedException, ExecutionException { - final GraphEntity info = db.graph(GRAPH_NAME).getInfo().get(); - assertThat(info, is(notNullValue())); - assertThat(info.getName(), is(GRAPH_NAME)); - assertThat(info.getEdgeDefinitions().size(), is(2)); - final Iterator iterator = info.getEdgeDefinitions().iterator(); - final EdgeDefinition e1 = iterator.next(); - assertThat(e1.getCollection(), is(EDGE_COL_1)); - assertThat(e1.getFrom(), hasItem(VERTEX_COL_1)); - assertThat(e1.getTo(), hasItem(VERTEX_COL_2)); - final EdgeDefinition e2 = iterator.next(); - assertThat(e2.getCollection(), is(EDGE_COL_2)); - assertThat(e2.getFrom(), hasItem(VERTEX_COL_2)); - assertThat(e2.getTo(), hasItems(VERTEX_COL_1, VERTEX_COL_3)); - assertThat(info.getOrphanCollections(), is(empty())); + @Test + public void getInfo() throws InterruptedException, ExecutionException { + final GraphEntity info = db.graph(GRAPH_NAME).getInfo().get(); + assertThat(info, is(notNullValue())); + assertThat(info.getName(), is(GRAPH_NAME)); + assertThat(info.getEdgeDefinitions().size(), is(2)); + final Iterator iterator = info.getEdgeDefinitions().iterator(); + final EdgeDefinition e1 = iterator.next(); + assertThat(e1.getCollection(), is(EDGE_COL_1)); + assertThat(e1.getFrom(), hasItem(VERTEX_COL_1)); + assertThat(e1.getTo(), hasItem(VERTEX_COL_2)); + final EdgeDefinition e2 = iterator.next(); + assertThat(e2.getCollection(), is(EDGE_COL_2)); + assertThat(e2.getFrom(), hasItem(VERTEX_COL_2)); + assertThat(e2.getTo(), hasItems(VERTEX_COL_1, VERTEX_COL_3)); + assertThat(info.getOrphanCollections(), is(empty())); - if (arangoDB.getRole().get() != ServerRole.SINGLE) { - for (final String collection : new String[] { VERTEX_COL_1, VERTEX_COL_2 }) { - final CollectionPropertiesEntity properties = db.collection(collection).getProperties().get(); - assertThat(properties.getReplicationFactor(), is(REPLICATION_FACTOR)); - assertThat(properties.getNumberOfShards(), is(NUMBER_OF_SHARDS)); - } - for (final String collection : new String[] { EDGE_COL_1, EDGE_COL_2 }) { - final CollectionPropertiesEntity properties = db.collection(collection).getProperties().get(); - assertThat(properties.getReplicationFactor(), is(REPLICATION_FACTOR)); - } - } - } + if (arangoDB.getRole().get() != ServerRole.SINGLE) { + for (final String collection : new String[]{VERTEX_COL_1, VERTEX_COL_2}) { + final CollectionPropertiesEntity properties = db.collection(collection).getProperties().get(); + assertThat(properties.getReplicationFactor(), is(REPLICATION_FACTOR)); + assertThat(properties.getNumberOfShards(), is(NUMBER_OF_SHARDS)); + } + for (final String collection : new String[]{EDGE_COL_1, EDGE_COL_2}) { + final CollectionPropertiesEntity properties = db.collection(collection).getProperties().get(); + assertThat(properties.getReplicationFactor(), is(REPLICATION_FACTOR)); + } + } + } - @Test - public void getVertexCollections() throws InterruptedException, ExecutionException { - final Collection vertexCollections = db.graph(GRAPH_NAME).getVertexCollections().get(); - assertThat(vertexCollections, is(notNullValue())); - assertThat(vertexCollections.size(), is(3)); - assertThat(vertexCollections, hasItems(VERTEX_COL_1, VERTEX_COL_2, VERTEX_COL_3)); - } + @Test + public void getVertexCollections() throws InterruptedException, ExecutionException { + final Collection vertexCollections = db.graph(GRAPH_NAME).getVertexCollections().get(); + assertThat(vertexCollections, is(notNullValue())); + assertThat(vertexCollections.size(), is(3)); + assertThat(vertexCollections, hasItems(VERTEX_COL_1, VERTEX_COL_2, VERTEX_COL_3)); + } - @Test - public void addVertexCollection() throws InterruptedException, ExecutionException { - final GraphEntity graph = db.graph(GRAPH_NAME).addVertexCollection(VERTEX_COL_4).get(); - assertThat(graph, is(notNullValue())); - final Collection vertexCollections = db.graph(GRAPH_NAME).getVertexCollections().get(); - assertThat(vertexCollections, hasItems(VERTEX_COL_1, VERTEX_COL_2, VERTEX_COL_3, VERTEX_COL_4)); - setup(); - } + @Test + public void addVertexCollection() throws InterruptedException, ExecutionException { + final GraphEntity graph = db.graph(GRAPH_NAME).addVertexCollection(VERTEX_COL_4).get(); + assertThat(graph, is(notNullValue())); + final Collection vertexCollections = db.graph(GRAPH_NAME).getVertexCollections().get(); + assertThat(vertexCollections, hasItems(VERTEX_COL_1, VERTEX_COL_2, VERTEX_COL_3, VERTEX_COL_4)); + setup(); + } - @Test - public void getEdgeCollections() throws InterruptedException, ExecutionException { - final Collection edgeCollections = db.graph(GRAPH_NAME).getEdgeDefinitions().get(); - assertThat(edgeCollections, is(notNullValue())); - assertThat(edgeCollections.size(), is(2)); - assertThat(edgeCollections, hasItems(EDGE_COL_1, EDGE_COL_2)); - } + @Test + public void getEdgeCollections() throws InterruptedException, ExecutionException { + final Collection edgeCollections = db.graph(GRAPH_NAME).getEdgeDefinitions().get(); + assertThat(edgeCollections, is(notNullValue())); + assertThat(edgeCollections.size(), is(2)); + assertThat(edgeCollections, hasItems(EDGE_COL_1, EDGE_COL_2)); + } - @Test - public void addEdgeDefinition() throws InterruptedException, ExecutionException { - final GraphEntity graph = db.graph(GRAPH_NAME) - .addEdgeDefinition(new EdgeDefinition().collection(EDGE_COL_3).from(VERTEX_COL_1).to(VERTEX_COL_2)) - .get(); - assertThat(graph, is(notNullValue())); - final Collection edgeDefinitions = graph.getEdgeDefinitions(); - assertThat(edgeDefinitions.size(), is(3)); - int count = 0; - for (final EdgeDefinition e : edgeDefinitions) { - if (e.getCollection().equals(EDGE_COL_3)) { - count++; - } - } - assertThat(count, is(1)); - for (final EdgeDefinition e : edgeDefinitions) { - if (e.getCollection().equals(EDGE_COL_3)) { - assertThat(e.getFrom(), hasItem(VERTEX_COL_1)); - assertThat(e.getTo(), hasItem(VERTEX_COL_2)); - } - } - if (arangoDB.getRole().get() != ServerRole.SINGLE) { - final CollectionPropertiesEntity properties = db.collection(EDGE_COL_3).getProperties().get(); - assertThat(properties.getReplicationFactor(), is(REPLICATION_FACTOR)); - assertThat(properties.getNumberOfShards(), is(NUMBER_OF_SHARDS)); - } - setup(); - } + @Test + public void addEdgeDefinition() throws InterruptedException, ExecutionException { + final GraphEntity graph = db.graph(GRAPH_NAME) + .addEdgeDefinition(new EdgeDefinition().collection(EDGE_COL_3).from(VERTEX_COL_1).to(VERTEX_COL_2)) + .get(); + assertThat(graph, is(notNullValue())); + final Collection edgeDefinitions = graph.getEdgeDefinitions(); + assertThat(edgeDefinitions.size(), is(3)); + int count = 0; + for (final EdgeDefinition e : edgeDefinitions) { + if (e.getCollection().equals(EDGE_COL_3)) { + count++; + } + } + assertThat(count, is(1)); + for (final EdgeDefinition e : edgeDefinitions) { + if (e.getCollection().equals(EDGE_COL_3)) { + assertThat(e.getFrom(), hasItem(VERTEX_COL_1)); + assertThat(e.getTo(), hasItem(VERTEX_COL_2)); + } + } + if (arangoDB.getRole().get() != ServerRole.SINGLE) { + final CollectionPropertiesEntity properties = db.collection(EDGE_COL_3).getProperties().get(); + assertThat(properties.getReplicationFactor(), is(REPLICATION_FACTOR)); + assertThat(properties.getNumberOfShards(), is(NUMBER_OF_SHARDS)); + } + setup(); + } - @Test - public void replaceEdgeDefinition() throws InterruptedException, ExecutionException { - final GraphEntity graph = db.graph(GRAPH_NAME) - .replaceEdgeDefinition(new EdgeDefinition().collection(EDGE_COL_1).from(VERTEX_COL_3).to(VERTEX_COL_4)) - .get(); - final Collection edgeDefinitions = graph.getEdgeDefinitions(); - assertThat(edgeDefinitions.size(), is(2)); - int count = 0; - for (final EdgeDefinition e : edgeDefinitions) { - if (e.getCollection().equals(EDGE_COL_1)) { - count++; - } - } - assertThat(count, is(1)); - for (final EdgeDefinition e : edgeDefinitions) { - if (e.getCollection().equals(EDGE_COL_1)) { - assertThat(e.getFrom(), hasItem(VERTEX_COL_3)); - assertThat(e.getTo(), hasItem(VERTEX_COL_4)); - } - } - setup(); - } + @Test + public void replaceEdgeDefinition() throws InterruptedException, ExecutionException { + final GraphEntity graph = db.graph(GRAPH_NAME) + .replaceEdgeDefinition(new EdgeDefinition().collection(EDGE_COL_1).from(VERTEX_COL_3).to(VERTEX_COL_4)) + .get(); + final Collection edgeDefinitions = graph.getEdgeDefinitions(); + assertThat(edgeDefinitions.size(), is(2)); + int count = 0; + for (final EdgeDefinition e : edgeDefinitions) { + if (e.getCollection().equals(EDGE_COL_1)) { + count++; + } + } + assertThat(count, is(1)); + for (final EdgeDefinition e : edgeDefinitions) { + if (e.getCollection().equals(EDGE_COL_1)) { + assertThat(e.getFrom(), hasItem(VERTEX_COL_3)); + assertThat(e.getTo(), hasItem(VERTEX_COL_4)); + } + } + setup(); + } - @Test - public void removeEdgeDefinition() throws InterruptedException, ExecutionException { - final GraphEntity graph = db.graph(GRAPH_NAME).removeEdgeDefinition(EDGE_COL_1).get(); - final Collection edgeDefinitions = graph.getEdgeDefinitions(); - assertThat(edgeDefinitions.size(), is(1)); - assertThat(edgeDefinitions.iterator().next().getCollection(), is(EDGE_COL_2)); - setup(); - } + @Test + public void removeEdgeDefinition() throws InterruptedException, ExecutionException { + final GraphEntity graph = db.graph(GRAPH_NAME).removeEdgeDefinition(EDGE_COL_1).get(); + final Collection edgeDefinitions = graph.getEdgeDefinitions(); + assertThat(edgeDefinitions.size(), is(1)); + assertThat(edgeDefinitions.iterator().next().getCollection(), is(EDGE_COL_2)); + setup(); + } - @Test - public void smartGraph() throws InterruptedException, ExecutionException { - if (arangoDB.getVersion().get().getLicense() == License.ENTERPRISE) { - for (final String collection : new String[] { EDGE_COL_1, EDGE_COL_2, VERTEX_COL_1, VERTEX_COL_2, - VERTEX_COL_3, VERTEX_COL_4 }) { - try { - db.collection(collection).drop().get(); - } catch (final Exception e) { - } - } - final Collection edgeDefinitions = new ArrayList<>(); - edgeDefinitions.add(new EdgeDefinition().collection(EDGE_COL_1).from(VERTEX_COL_1).to(VERTEX_COL_2)); - edgeDefinitions - .add(new EdgeDefinition().collection(EDGE_COL_2).from(VERTEX_COL_2).to(VERTEX_COL_1, VERTEX_COL_3)); - final GraphEntity graph = db.createGraph(GRAPH_NAME + "_smart", edgeDefinitions, - new GraphCreateOptions().isSmart(true).smartGraphAttribute("test").replicationFactor(REPLICATION_FACTOR) - .numberOfShards(NUMBER_OF_SHARDS)) - .get(); - assertThat(graph, is(notNullValue())); - assertThat(graph.getIsSmart(), is(true)); - assertThat(graph.getSmartGraphAttribute(), is("test")); - assertThat(graph.getNumberOfShards(), is(2)); - try { - db.graph(GRAPH_NAME + "_smart").drop().get(); - } catch (final Exception e) { - } - } - } + @Test + public void smartGraph() throws InterruptedException, ExecutionException { + if (arangoDB.getVersion().get().getLicense() == License.ENTERPRISE) { + for (final String collection : new String[]{EDGE_COL_1, EDGE_COL_2, VERTEX_COL_1, VERTEX_COL_2, + VERTEX_COL_3, VERTEX_COL_4}) { + if (db.collection(collection).exists().get()) { + db.collection(collection).drop().get(); + } + } + final Collection edgeDefinitions = new ArrayList<>(); + edgeDefinitions.add(new EdgeDefinition().collection(EDGE_COL_1).from(VERTEX_COL_1).to(VERTEX_COL_2)); + edgeDefinitions + .add(new EdgeDefinition().collection(EDGE_COL_2).from(VERTEX_COL_2).to(VERTEX_COL_1, VERTEX_COL_3)); + final GraphEntity graph = db.createGraph(GRAPH_NAME + "_smart", edgeDefinitions, + new GraphCreateOptions().isSmart(true).smartGraphAttribute("test").replicationFactor(REPLICATION_FACTOR) + .numberOfShards(NUMBER_OF_SHARDS)) + .get(); + assertThat(graph, is(notNullValue())); + assertThat(graph.getIsSmart(), is(true)); + assertThat(graph.getSmartGraphAttribute(), is("test")); + assertThat(graph.getNumberOfShards(), is(2)); + if (db.graph(GRAPH_NAME + "_smart").exists().get()) { + db.graph(GRAPH_NAME + "_smart").drop().get(); + } + } + } - @Test - public void drop() throws InterruptedException, ExecutionException { - final String edgeCollection = "edge_drop"; - final String vertexCollection = "vertex_drop"; - final String graph = GRAPH_NAME + "_drop"; - final GraphEntity result = db.graph(graph).create(Collections - .singleton(new EdgeDefinition().collection(edgeCollection).from(vertexCollection).to(vertexCollection))) - .get(); - assertThat(result, is(notNullValue())); - db.graph(graph).drop().get(); - assertThat(db.collection(edgeCollection).exists().get(), is(true)); - assertThat(db.collection(vertexCollection).exists().get(), is(true)); - } + @Test + public void drop() throws InterruptedException, ExecutionException { + final String edgeCollection = "edge_drop"; + final String vertexCollection = "vertex_drop"; + final String graph = GRAPH_NAME + "_drop"; + final GraphEntity result = db.graph(graph).create(Collections + .singleton(new EdgeDefinition().collection(edgeCollection).from(vertexCollection).to(vertexCollection))) + .get(); + assertThat(result, is(notNullValue())); + db.graph(graph).drop().get(); + assertThat(db.collection(edgeCollection).exists().get(), is(true)); + assertThat(db.collection(vertexCollection).exists().get(), is(true)); + } - @Test - public void dropPlusDropCollections() throws InterruptedException, ExecutionException { - final String edgeCollection = "edge_dropC"; - final String vertexCollection = "vertex_dropC"; - final String graph = GRAPH_NAME + "_dropC"; - final GraphEntity result = db.graph(graph).create(Collections - .singleton(new EdgeDefinition().collection(edgeCollection).from(vertexCollection).to(vertexCollection))) - .get(); - assertThat(result, is(notNullValue())); - db.graph(graph).drop(true).get(); - assertThat(db.collection(edgeCollection).exists().get(), is(false)); - assertThat(db.collection(vertexCollection).exists().get(), is(false)); - } + @Test + public void dropPlusDropCollections() throws InterruptedException, ExecutionException { + final String edgeCollection = "edge_dropC"; + final String vertexCollection = "vertex_dropC"; + final String graph = GRAPH_NAME + "_dropC"; + final GraphEntity result = db.graph(graph).create(Collections + .singleton(new EdgeDefinition().collection(edgeCollection).from(vertexCollection).to(vertexCollection))) + .get(); + assertThat(result, is(notNullValue())); + db.graph(graph).drop(true).get(); + assertThat(db.collection(edgeCollection).exists().get(), is(false)); + assertThat(db.collection(vertexCollection).exists().get(), is(false)); + } } diff --git a/src/test/java/com/arangodb/ArangoRouteTest.java b/src/test/java/com/arangodb/ArangoRouteTest.java index 4aef03f..79e8d01 100644 --- a/src/test/java/com/arangodb/ArangoRouteTest.java +++ b/src/test/java/com/arangodb/ArangoRouteTest.java @@ -35,7 +35,6 @@ /** * @author Mark Vollmary - * */ public class ArangoRouteTest extends BaseTest { @@ -65,21 +64,20 @@ public void withHeader() throws InterruptedException, ExecutionException { } */ - @Test - public void withParentHeader() throws InterruptedException, ExecutionException { - final ArangoCollectionAsync collection = db.collection("route-test-col"); - try { - collection.create().get(); - final BaseDocument doc = new BaseDocument(); - collection.insertDocument(doc).get(); - db.route("/_api/document").withHeader(ArangoRequestParam.IF_NONE_MATCH, doc.getRevision()) - .route(doc.getId()).get().get(); - fail(); - } catch (final ExecutionException e) { - assertThat(e.getCause(), instanceOf(ArangoDBException.class)); - } finally { - collection.drop().get(); - } - } + @Test + public void withParentHeader() throws InterruptedException, ExecutionException { + final ArangoCollectionAsync collection = db.collection("route-test-col"); + try { + collection.create().get(); + final BaseDocument doc = new BaseDocument(); + collection.insertDocument(doc).get(); + db.route("/_api/document").withHeader(ArangoRequestParam.IF_NONE_MATCH, doc.getRevision()) + .route(doc.getId()).get().get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); + } + collection.drop().get(); + } } diff --git a/src/test/java/com/arangodb/ArangoVertexCollectionTest.java b/src/test/java/com/arangodb/ArangoVertexCollectionTest.java index 3ddf568..d09ac31 100644 --- a/src/test/java/com/arangodb/ArangoVertexCollectionTest.java +++ b/src/test/java/com/arangodb/ArangoVertexCollectionTest.java @@ -20,10 +20,7 @@ package com.arangodb; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; @@ -45,330 +42,335 @@ /** * @author Mark Vollmary - * */ public class ArangoVertexCollectionTest extends BaseTest { - private static final String GRAPH_NAME = "db_collection_test"; - private static final String COLLECTION_NAME = "db_vertex_collection_test"; - - @BeforeClass - public static void setup() throws InterruptedException, ExecutionException { - try { - db.createCollection(COLLECTION_NAME, null).get(); - } catch (final Exception e) { - } - final GraphCreateOptions options = new GraphCreateOptions().orphanCollections(COLLECTION_NAME); - db.createGraph(GRAPH_NAME, null, options).get(); - } - - @After - public void teardown() throws InterruptedException, ExecutionException { - db.collection(COLLECTION_NAME).truncate().get(); - } - - @Test - public void dropVertexCollection() throws InterruptedException, ExecutionException { - db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).drop().get(); - final Collection vertexCollections = db.graph(GRAPH_NAME).getVertexCollections().get(); - assertThat(vertexCollections, not(hasItem(COLLECTION_NAME))); - } - - @Test - public void insertVertex() throws InterruptedException, ExecutionException { - final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .insertVertex(new BaseDocument(), null).get(); - assertThat(vertex, is(notNullValue())); - final BaseDocument document = db.collection(COLLECTION_NAME) - .getDocument(vertex.getKey(), BaseDocument.class, null).get(); - assertThat(document, is(notNullValue())); - assertThat(document.getKey(), is(vertex.getKey())); - } - - @Test - public void getVertex() throws InterruptedException, ExecutionException { - final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .insertVertex(new BaseDocument(), null).get(); - final BaseDocument document = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(vertex.getKey(), BaseDocument.class, null).get(); - assertThat(document, is(notNullValue())); - assertThat(document.getKey(), is(vertex.getKey())); - } - - @Test - public void getVertexIfMatch() throws InterruptedException, ExecutionException { - final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .insertVertex(new BaseDocument(), null).get(); - final DocumentReadOptions options = new DocumentReadOptions().ifMatch(vertex.getRev()); - final BaseDocument document = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(vertex.getKey(), BaseDocument.class, options).get(); - assertThat(document, is(notNullValue())); - assertThat(document.getKey(), is(vertex.getKey())); - } - - @Test - public void getVertexIfMatchFail() throws InterruptedException, ExecutionException { - final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .insertVertex(new BaseDocument(), null).get(); - final DocumentReadOptions options = new DocumentReadOptions().ifMatch("no"); - try { - db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(vertex.getKey(), BaseDocument.class, options).get(); - fail(); - } catch (final Exception e) { - } - } - - @Test - public void getVertexIfNoneMatch() throws InterruptedException, ExecutionException { - final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .insertVertex(new BaseDocument(), null).get(); - final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch("no"); - final BaseDocument document = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(vertex.getKey(), BaseDocument.class, options).get(); - assertThat(document, is(notNullValue())); - assertThat(document.getKey(), is(vertex.getKey())); - } - - @Test - public void getVertexIfNoneMatchFail() throws InterruptedException, ExecutionException { - final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .insertVertex(new BaseDocument(), null).get(); - final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch(vertex.getRev()); - try { - db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(vertex.getKey(), BaseDocument.class, options).get(); - fail(); - } catch (final Exception e) { + private static final String GRAPH_NAME = "db_collection_test"; + private static final String COLLECTION_NAME = "db_vertex_collection_test"; + + @BeforeClass + public static void setup() throws InterruptedException, ExecutionException { + if (!db.collection(COLLECTION_NAME).exists().get()) { + db.createCollection(COLLECTION_NAME, null).get(); + } + final GraphCreateOptions options = new GraphCreateOptions().orphanCollections(COLLECTION_NAME); + db.createGraph(GRAPH_NAME, null, options).get(); + } + + @After + public void teardown() throws InterruptedException, ExecutionException { + db.collection(COLLECTION_NAME).truncate().get(); + } + + @Test + public void dropVertexCollection() throws InterruptedException, ExecutionException { + db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).drop().get(); + final Collection vertexCollections = db.graph(GRAPH_NAME).getVertexCollections().get(); + assertThat(vertexCollections, not(hasItem(COLLECTION_NAME))); + } + + @Test + public void insertVertex() throws InterruptedException, ExecutionException { + final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .insertVertex(new BaseDocument(), null).get(); + assertThat(vertex, is(notNullValue())); + final BaseDocument document = db.collection(COLLECTION_NAME) + .getDocument(vertex.getKey(), BaseDocument.class, null).get(); + assertThat(document, is(notNullValue())); + assertThat(document.getKey(), is(vertex.getKey())); + } + + @Test + public void getVertex() throws InterruptedException, ExecutionException { + final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .insertVertex(new BaseDocument(), null).get(); + final BaseDocument document = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(vertex.getKey(), BaseDocument.class, null).get(); + assertThat(document, is(notNullValue())); + assertThat(document.getKey(), is(vertex.getKey())); + } + + @Test + public void getVertexIfMatch() throws InterruptedException, ExecutionException { + final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .insertVertex(new BaseDocument(), null).get(); + final DocumentReadOptions options = new DocumentReadOptions().ifMatch(vertex.getRev()); + final BaseDocument document = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(vertex.getKey(), BaseDocument.class, options).get(); + assertThat(document, is(notNullValue())); + assertThat(document.getKey(), is(vertex.getKey())); + } + + @Test + public void getVertexIfMatchFail() throws InterruptedException, ExecutionException { + final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .insertVertex(new BaseDocument(), null).get(); + final DocumentReadOptions options = new DocumentReadOptions().ifMatch("no"); + try { + db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(vertex.getKey(), BaseDocument.class, options).get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); + } + } + + @Test + public void getVertexIfNoneMatch() throws InterruptedException, ExecutionException { + final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .insertVertex(new BaseDocument(), null).get(); + final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch("no"); + final BaseDocument document = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(vertex.getKey(), BaseDocument.class, options).get(); + assertThat(document, is(notNullValue())); + assertThat(document.getKey(), is(vertex.getKey())); + } + + @Test + public void getVertexIfNoneMatchFail() throws InterruptedException, ExecutionException { + final VertexEntity vertex = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .insertVertex(new BaseDocument(), null).get(); + final DocumentReadOptions options = new DocumentReadOptions().ifNoneMatch(vertex.getRev()); + try { + db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(vertex.getKey(), BaseDocument.class, options).get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } - } - - @Test - public void replaceVertex() throws InterruptedException, ExecutionException { - final BaseDocument doc = new BaseDocument(); - doc.addAttribute("a", "test"); - final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) - .get(); - doc.getProperties().clear(); - doc.addAttribute("b", "test"); - final VertexUpdateEntity replaceResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .replaceVertex(createResult.getKey(), doc, null).get(); - assertThat(replaceResult, is(notNullValue())); - assertThat(replaceResult.getId(), is(createResult.getId())); - assertThat(replaceResult.getRev(), is(not(replaceResult.getOldRev()))); - assertThat(replaceResult.getOldRev(), is(createResult.getRev())); - - final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(createResult.getKey(), BaseDocument.class, null).get(); - assertThat(readResult.getKey(), is(createResult.getKey())); - assertThat(readResult.getRevision(), is(replaceResult.getRev())); - assertThat(readResult.getProperties().keySet(), not(hasItem("a"))); - assertThat(readResult.getAttribute("b"), is(notNullValue())); - assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); - } - - @Test - public void replaceVertexIfMatch() throws InterruptedException, ExecutionException { - final BaseDocument doc = new BaseDocument(); - doc.addAttribute("a", "test"); - final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) - .get(); - doc.getProperties().clear(); - doc.addAttribute("b", "test"); - final VertexReplaceOptions options = new VertexReplaceOptions().ifMatch(createResult.getRev()); - final VertexUpdateEntity replaceResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .replaceVertex(createResult.getKey(), doc, options).get(); - assertThat(replaceResult, is(notNullValue())); - assertThat(replaceResult.getId(), is(createResult.getId())); - assertThat(replaceResult.getRev(), is(not(replaceResult.getOldRev()))); - assertThat(replaceResult.getOldRev(), is(createResult.getRev())); - - final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(createResult.getKey(), BaseDocument.class, null).get(); - assertThat(readResult.getKey(), is(createResult.getKey())); - assertThat(readResult.getRevision(), is(replaceResult.getRev())); - assertThat(readResult.getProperties().keySet(), not(hasItem("a"))); - assertThat(readResult.getAttribute("b"), is(notNullValue())); - assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); - } - - @Test - public void replaceVertexIfMatchFail() throws InterruptedException, ExecutionException { - final BaseDocument doc = new BaseDocument(); - doc.addAttribute("a", "test"); - final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) - .get(); - doc.getProperties().clear(); - doc.addAttribute("b", "test"); - try { - final VertexReplaceOptions options = new VertexReplaceOptions().ifMatch("no"); - db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).replaceVertex(createResult.getKey(), doc, options) - .get(); - fail(); - } catch (final Exception e) { + } + + @Test + public void replaceVertex() throws InterruptedException, ExecutionException { + final BaseDocument doc = new BaseDocument(); + doc.addAttribute("a", "test"); + final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) + .get(); + doc.getProperties().clear(); + doc.addAttribute("b", "test"); + final VertexUpdateEntity replaceResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .replaceVertex(createResult.getKey(), doc, null).get(); + assertThat(replaceResult, is(notNullValue())); + assertThat(replaceResult.getId(), is(createResult.getId())); + assertThat(replaceResult.getRev(), is(not(replaceResult.getOldRev()))); + assertThat(replaceResult.getOldRev(), is(createResult.getRev())); + + final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(createResult.getKey(), BaseDocument.class, null).get(); + assertThat(readResult.getKey(), is(createResult.getKey())); + assertThat(readResult.getRevision(), is(replaceResult.getRev())); + assertThat(readResult.getProperties().keySet(), not(hasItem("a"))); + assertThat(readResult.getAttribute("b"), is(notNullValue())); + assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); + } + + @Test + public void replaceVertexIfMatch() throws InterruptedException, ExecutionException { + final BaseDocument doc = new BaseDocument(); + doc.addAttribute("a", "test"); + final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) + .get(); + doc.getProperties().clear(); + doc.addAttribute("b", "test"); + final VertexReplaceOptions options = new VertexReplaceOptions().ifMatch(createResult.getRev()); + final VertexUpdateEntity replaceResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .replaceVertex(createResult.getKey(), doc, options).get(); + assertThat(replaceResult, is(notNullValue())); + assertThat(replaceResult.getId(), is(createResult.getId())); + assertThat(replaceResult.getRev(), is(not(replaceResult.getOldRev()))); + assertThat(replaceResult.getOldRev(), is(createResult.getRev())); + + final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(createResult.getKey(), BaseDocument.class, null).get(); + assertThat(readResult.getKey(), is(createResult.getKey())); + assertThat(readResult.getRevision(), is(replaceResult.getRev())); + assertThat(readResult.getProperties().keySet(), not(hasItem("a"))); + assertThat(readResult.getAttribute("b"), is(notNullValue())); + assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); + } + + @Test + public void replaceVertexIfMatchFail() throws InterruptedException, ExecutionException { + final BaseDocument doc = new BaseDocument(); + doc.addAttribute("a", "test"); + final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) + .get(); + doc.getProperties().clear(); + doc.addAttribute("b", "test"); + try { + final VertexReplaceOptions options = new VertexReplaceOptions().ifMatch("no"); + db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).replaceVertex(createResult.getKey(), doc, options) + .get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } - } - - @Test - public void updateVertex() throws InterruptedException, ExecutionException { - final BaseDocument doc = new BaseDocument(); - doc.addAttribute("a", "test"); - doc.addAttribute("c", "test"); - final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) - .get(); - doc.updateAttribute("a", "test1"); - doc.addAttribute("b", "test"); - doc.updateAttribute("c", null); - final VertexUpdateEntity updateResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .updateVertex(createResult.getKey(), doc, null).get(); - assertThat(updateResult, is(notNullValue())); - assertThat(updateResult.getId(), is(createResult.getId())); - assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); - assertThat(updateResult.getOldRev(), is(createResult.getRev())); - - final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(createResult.getKey(), BaseDocument.class, null).get(); - assertThat(readResult.getKey(), is(createResult.getKey())); - assertThat(readResult.getAttribute("a"), is(notNullValue())); - assertThat(String.valueOf(readResult.getAttribute("a")), is("test1")); - assertThat(readResult.getAttribute("b"), is(notNullValue())); - assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); - assertThat(readResult.getRevision(), is(updateResult.getRev())); - assertThat(readResult.getProperties().keySet(), hasItem("c")); - } - - @Test - public void updateVertexIfMatch() throws InterruptedException, ExecutionException { - final BaseDocument doc = new BaseDocument(); - doc.addAttribute("a", "test"); - doc.addAttribute("c", "test"); - final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) - .get(); - doc.updateAttribute("a", "test1"); - doc.addAttribute("b", "test"); - doc.updateAttribute("c", null); - final VertexUpdateOptions options = new VertexUpdateOptions().ifMatch(createResult.getRev()); - final VertexUpdateEntity updateResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .updateVertex(createResult.getKey(), doc, options).get(); - assertThat(updateResult, is(notNullValue())); - assertThat(updateResult.getId(), is(createResult.getId())); - assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); - assertThat(updateResult.getOldRev(), is(createResult.getRev())); - - final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(createResult.getKey(), BaseDocument.class, null).get(); - assertThat(readResult.getKey(), is(createResult.getKey())); - assertThat(readResult.getAttribute("a"), is(notNullValue())); - assertThat(String.valueOf(readResult.getAttribute("a")), is("test1")); - assertThat(readResult.getAttribute("b"), is(notNullValue())); - assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); - assertThat(readResult.getRevision(), is(updateResult.getRev())); - assertThat(readResult.getProperties().keySet(), hasItem("c")); - } - - @Test - public void updateVertexIfMatchFail() throws InterruptedException, ExecutionException { - final BaseDocument doc = new BaseDocument(); - doc.addAttribute("a", "test"); - doc.addAttribute("c", "test"); - final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) - .get(); - doc.updateAttribute("a", "test1"); - doc.addAttribute("b", "test"); - doc.updateAttribute("c", null); - try { - final VertexUpdateOptions options = new VertexUpdateOptions().ifMatch("no"); - db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).updateVertex(createResult.getKey(), doc, options) - .get(); - fail(); - } catch (final Exception e) { + } + + @Test + public void updateVertex() throws InterruptedException, ExecutionException { + final BaseDocument doc = new BaseDocument(); + doc.addAttribute("a", "test"); + doc.addAttribute("c", "test"); + final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) + .get(); + doc.updateAttribute("a", "test1"); + doc.addAttribute("b", "test"); + doc.updateAttribute("c", null); + final VertexUpdateEntity updateResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .updateVertex(createResult.getKey(), doc, null).get(); + assertThat(updateResult, is(notNullValue())); + assertThat(updateResult.getId(), is(createResult.getId())); + assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); + assertThat(updateResult.getOldRev(), is(createResult.getRev())); + + final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(createResult.getKey(), BaseDocument.class, null).get(); + assertThat(readResult.getKey(), is(createResult.getKey())); + assertThat(readResult.getAttribute("a"), is(notNullValue())); + assertThat(String.valueOf(readResult.getAttribute("a")), is("test1")); + assertThat(readResult.getAttribute("b"), is(notNullValue())); + assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); + assertThat(readResult.getRevision(), is(updateResult.getRev())); + assertThat(readResult.getProperties().keySet(), hasItem("c")); + } + + @Test + public void updateVertexIfMatch() throws InterruptedException, ExecutionException { + final BaseDocument doc = new BaseDocument(); + doc.addAttribute("a", "test"); + doc.addAttribute("c", "test"); + final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) + .get(); + doc.updateAttribute("a", "test1"); + doc.addAttribute("b", "test"); + doc.updateAttribute("c", null); + final VertexUpdateOptions options = new VertexUpdateOptions().ifMatch(createResult.getRev()); + final VertexUpdateEntity updateResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .updateVertex(createResult.getKey(), doc, options).get(); + assertThat(updateResult, is(notNullValue())); + assertThat(updateResult.getId(), is(createResult.getId())); + assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); + assertThat(updateResult.getOldRev(), is(createResult.getRev())); + + final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(createResult.getKey(), BaseDocument.class, null).get(); + assertThat(readResult.getKey(), is(createResult.getKey())); + assertThat(readResult.getAttribute("a"), is(notNullValue())); + assertThat(String.valueOf(readResult.getAttribute("a")), is("test1")); + assertThat(readResult.getAttribute("b"), is(notNullValue())); + assertThat(String.valueOf(readResult.getAttribute("b")), is("test")); + assertThat(readResult.getRevision(), is(updateResult.getRev())); + assertThat(readResult.getProperties().keySet(), hasItem("c")); + } + + @Test + public void updateVertexIfMatchFail() throws InterruptedException, ExecutionException { + final BaseDocument doc = new BaseDocument(); + doc.addAttribute("a", "test"); + doc.addAttribute("c", "test"); + final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) + .get(); + doc.updateAttribute("a", "test1"); + doc.addAttribute("b", "test"); + doc.updateAttribute("c", null); + try { + final VertexUpdateOptions options = new VertexUpdateOptions().ifMatch("no"); + db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).updateVertex(createResult.getKey(), doc, options) + .get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } - } - - @Test - public void updateVertexKeepNullTrue() throws InterruptedException, ExecutionException { - final BaseDocument doc = new BaseDocument(); - doc.addAttribute("a", "test"); - final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) - .get(); - doc.updateAttribute("a", null); - final VertexUpdateOptions options = new VertexUpdateOptions().keepNull(true); - final VertexUpdateEntity updateResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .updateVertex(createResult.getKey(), doc, options).get(); - assertThat(updateResult, is(notNullValue())); - assertThat(updateResult.getId(), is(createResult.getId())); - assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); - assertThat(updateResult.getOldRev(), is(createResult.getRev())); - - final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(createResult.getKey(), BaseDocument.class, null).get(); - assertThat(readResult.getKey(), is(createResult.getKey())); - assertThat(readResult.getProperties().keySet().size(), is(1)); - assertThat(readResult.getProperties().keySet(), hasItem("a")); - } - - @Test - public void updateVertexKeepNullFalse() throws InterruptedException, ExecutionException { - final BaseDocument doc = new BaseDocument(); - doc.addAttribute("a", "test"); - final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) - .get(); - doc.updateAttribute("a", null); - final VertexUpdateOptions options = new VertexUpdateOptions().keepNull(false); - final VertexUpdateEntity updateResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .updateVertex(createResult.getKey(), doc, options).get(); - assertThat(updateResult, is(notNullValue())); - assertThat(updateResult.getId(), is(createResult.getId())); - assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); - assertThat(updateResult.getOldRev(), is(createResult.getRev())); - - final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(createResult.getKey(), BaseDocument.class, null).get(); - assertThat(readResult.getKey(), is(createResult.getKey())); - assertThat(readResult.getId(), is(createResult.getId())); - assertThat(readResult.getRevision(), is(notNullValue())); - assertThat(readResult.getProperties().keySet(), not(hasItem("a"))); - } - - @Test - public void deleteVertex() throws InterruptedException, ExecutionException { - final BaseDocument doc = new BaseDocument(); - final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) - .get(); - db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).deleteVertex(createResult.getKey(), null).get(); - try { - db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(createResult.getKey(), BaseDocument.class, null).get(); - fail(); - } catch (final Exception e) { + } + + @Test + public void updateVertexKeepNullTrue() throws InterruptedException, ExecutionException { + final BaseDocument doc = new BaseDocument(); + doc.addAttribute("a", "test"); + final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) + .get(); + doc.updateAttribute("a", null); + final VertexUpdateOptions options = new VertexUpdateOptions().keepNull(true); + final VertexUpdateEntity updateResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .updateVertex(createResult.getKey(), doc, options).get(); + assertThat(updateResult, is(notNullValue())); + assertThat(updateResult.getId(), is(createResult.getId())); + assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); + assertThat(updateResult.getOldRev(), is(createResult.getRev())); + + final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(createResult.getKey(), BaseDocument.class, null).get(); + assertThat(readResult.getKey(), is(createResult.getKey())); + assertThat(readResult.getProperties().keySet().size(), is(1)); + assertThat(readResult.getProperties().keySet(), hasItem("a")); + } + + @Test + public void updateVertexKeepNullFalse() throws InterruptedException, ExecutionException { + final BaseDocument doc = new BaseDocument(); + doc.addAttribute("a", "test"); + final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) + .get(); + doc.updateAttribute("a", null); + final VertexUpdateOptions options = new VertexUpdateOptions().keepNull(false); + final VertexUpdateEntity updateResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .updateVertex(createResult.getKey(), doc, options).get(); + assertThat(updateResult, is(notNullValue())); + assertThat(updateResult.getId(), is(createResult.getId())); + assertThat(updateResult.getRev(), is(not(updateResult.getOldRev()))); + assertThat(updateResult.getOldRev(), is(createResult.getRev())); + + final BaseDocument readResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(createResult.getKey(), BaseDocument.class, null).get(); + assertThat(readResult.getKey(), is(createResult.getKey())); + assertThat(readResult.getId(), is(createResult.getId())); + assertThat(readResult.getRevision(), is(notNullValue())); + assertThat(readResult.getProperties().keySet(), not(hasItem("a"))); + } + + @Test + public void deleteVertex() throws InterruptedException, ExecutionException { + final BaseDocument doc = new BaseDocument(); + final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) + .get(); + db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).deleteVertex(createResult.getKey(), null).get(); + try { + db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(createResult.getKey(), BaseDocument.class, null).get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } - } - - @Test - public void deleteVertexIfMatch() throws InterruptedException, ExecutionException { - final BaseDocument doc = new BaseDocument(); - final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) - .get(); - final VertexDeleteOptions options = new VertexDeleteOptions().ifMatch(createResult.getRev()); - db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).deleteVertex(createResult.getKey(), options).get(); - try { - db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) - .getVertex(createResult.getKey(), BaseDocument.class, null).get(); - fail(); - } catch (final Exception e) { + } + + @Test + public void deleteVertexIfMatch() throws InterruptedException, ExecutionException { + final BaseDocument doc = new BaseDocument(); + final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) + .get(); + final VertexDeleteOptions options = new VertexDeleteOptions().ifMatch(createResult.getRev()); + db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).deleteVertex(createResult.getKey(), options).get(); + try { + db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME) + .getVertex(createResult.getKey(), BaseDocument.class, null).get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } - } - - @Test - public void deleteVertexIfMatchFail() throws InterruptedException, ExecutionException { - final BaseDocument doc = new BaseDocument(); - final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) - .get(); - final VertexDeleteOptions options = new VertexDeleteOptions().ifMatch("no"); - try { - db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).deleteVertex(createResult.getKey(), options).get(); - fail(); - } catch (final Exception e) { + } + + @Test + public void deleteVertexIfMatchFail() throws InterruptedException, ExecutionException { + final BaseDocument doc = new BaseDocument(); + final VertexEntity createResult = db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).insertVertex(doc, null) + .get(); + final VertexDeleteOptions options = new VertexDeleteOptions().ifMatch("no"); + try { + db.graph(GRAPH_NAME).vertexCollection(COLLECTION_NAME).deleteVertex(createResult.getKey(), options).get(); + fail(); + } catch (final ExecutionException e) { + assertThat(e.getCause(), instanceOf(ArangoDBException.class)); } - } + } } diff --git a/src/test/java/com/arangodb/BaseTest.java b/src/test/java/com/arangodb/BaseTest.java index 3f39be1..d12dc1d 100644 --- a/src/test/java/com/arangodb/BaseTest.java +++ b/src/test/java/com/arangodb/BaseTest.java @@ -27,41 +27,41 @@ /** * @author Mark Vollmary - * */ public abstract class BaseTest { - protected static final String TEST_DB = "java_driver_test_db"; - protected static ArangoDBAsync arangoDB; - protected static ArangoDatabaseAsync db; + protected static final String TEST_DB = "java_driver_test_db"; + protected static ArangoDBAsync arangoDB; + protected static ArangoDatabaseAsync db; + + @BeforeClass + public static void init() throws InterruptedException, ExecutionException { + if (arangoDB == null) { + arangoDB = new ArangoDBAsync.Builder().build(); + } + + if (arangoDB.db(TEST_DB).exists().get()) { + arangoDB.db(TEST_DB).drop().get(); + } - @BeforeClass - public static void init() throws InterruptedException, ExecutionException { - if (arangoDB == null) { - arangoDB = new ArangoDBAsync.Builder().build(); - } - try { - arangoDB.db(TEST_DB).drop().get(); - } catch (final Exception e) { - } - arangoDB.createDatabase(TEST_DB).get(); - BaseTest.db = arangoDB.db(TEST_DB); - } + arangoDB.createDatabase(TEST_DB).get(); + BaseTest.db = arangoDB.db(TEST_DB); + } - @AfterClass - public static void shutdown() throws InterruptedException, ExecutionException { - arangoDB.db(TEST_DB).drop().get(); - arangoDB.shutdown(); - arangoDB = null; - } + @AfterClass + public static void shutdown() throws InterruptedException, ExecutionException { + arangoDB.db(TEST_DB).drop().get(); + arangoDB.shutdown(); + arangoDB = null; + } - protected boolean requireVersion(final int major, final int minor) throws InterruptedException, ExecutionException { - return requireVersion(arangoDB, major, minor); - } + protected boolean requireVersion(final int major, final int minor) throws InterruptedException, ExecutionException { + return requireVersion(arangoDB, major, minor); + } - protected static boolean requireVersion(final ArangoDBAsync arangoDB, final int major, final int minor) - throws InterruptedException, ExecutionException { - final String[] split = arangoDB.getVersion().get().getVersion().split("\\."); - return Integer.valueOf(split[0]) >= major && Integer.valueOf(split[1]) >= minor; - } + protected static boolean requireVersion(final ArangoDBAsync arangoDB, final int major, final int minor) + throws InterruptedException, ExecutionException { + final String[] split = arangoDB.getVersion().get().getVersion().split("\\."); + return Integer.valueOf(split[0]) >= major && Integer.valueOf(split[1]) >= minor; + } } diff --git a/src/test/java/com/arangodb/example/ExampleBase.java b/src/test/java/com/arangodb/example/ExampleBase.java index 4246924..0c2bf00 100644 --- a/src/test/java/com/arangodb/example/ExampleBase.java +++ b/src/test/java/com/arangodb/example/ExampleBase.java @@ -31,34 +31,32 @@ /** * @author Mark Vollmary - * */ public class ExampleBase { - protected static final String DB_NAME = "json_example_db"; - protected static final String COLLECTION_NAME = "json_example_collection"; - - protected static ArangoDBAsync arangoDB; - protected static ArangoDatabaseAsync db; - protected static ArangoCollectionAsync collection; - - @BeforeClass - public static void setUp() throws InterruptedException, ExecutionException { - arangoDB = new ArangoDBAsync.Builder().build(); - try { - arangoDB.db(DB_NAME).drop().get(); - } catch (final Exception e) { - } - arangoDB.createDatabase(DB_NAME).get(); - db = arangoDB.db(DB_NAME); - db.createCollection(COLLECTION_NAME).get(); - collection = db.collection(COLLECTION_NAME); - } - - @AfterClass - public static void tearDown() throws InterruptedException, ExecutionException { - db.drop().get(); - arangoDB.shutdown(); - } + protected static final String DB_NAME = "json_example_db"; + protected static final String COLLECTION_NAME = "json_example_collection"; + + protected static ArangoDBAsync arangoDB; + protected static ArangoDatabaseAsync db; + protected static ArangoCollectionAsync collection; + + @BeforeClass + public static void setUp() throws InterruptedException, ExecutionException { + arangoDB = new ArangoDBAsync.Builder().build(); + if (arangoDB.db(DB_NAME).exists().get()) { + arangoDB.db(DB_NAME).drop().get(); + } + arangoDB.createDatabase(DB_NAME).get(); + db = arangoDB.db(DB_NAME); + db.createCollection(COLLECTION_NAME).get(); + collection = db.collection(COLLECTION_NAME); + } + + @AfterClass + public static void tearDown() throws InterruptedException, ExecutionException { + db.drop().get(); + arangoDB.shutdown(); + } } diff --git a/src/test/java/com/arangodb/example/graph/AQLActorsAndMoviesExample.java b/src/test/java/com/arangodb/example/graph/AQLActorsAndMoviesExample.java index 62fb768..5bf7bfc 100644 --- a/src/test/java/com/arangodb/example/graph/AQLActorsAndMoviesExample.java +++ b/src/test/java/com/arangodb/example/graph/AQLActorsAndMoviesExample.java @@ -42,570 +42,567 @@ /** * @author Mark Vollmary - * * @see AQL Example Queries on an - * Actors and Movies Database - * + * Actors and Movies Database */ public class AQLActorsAndMoviesExample { - private static final String TEST_DB = "actors_movies_test_db"; - private static ArangoDBAsync arangoDB; - private static ArangoDatabaseAsync db; + private static final String TEST_DB = "actors_movies_test_db"; + private static ArangoDBAsync arangoDB; + private static ArangoDatabaseAsync db; - @BeforeClass - public static void setUp() throws InterruptedException, ExecutionException { - arangoDB = new ArangoDBAsync.Builder().build(); - try { + @BeforeClass + public static void setUp() throws InterruptedException, ExecutionException { + arangoDB = new ArangoDBAsync.Builder().build(); + if (arangoDB.db(TEST_DB).exists().get()) { arangoDB.db(TEST_DB).drop().get(); - } catch (final Exception e) { - } - arangoDB.createDatabase(TEST_DB).get(); - db = arangoDB.db(TEST_DB); - createData(); - } - - @AfterClass - public static void tearDown() throws InterruptedException, ExecutionException { - db.drop().get(); - arangoDB.shutdown(); - } - - /** - * @throws ExecutionException - * @throws InterruptedException - * @see AQL - * Example Queries on an Actors and Movies Database - */ - @Test - public void allActorsActsInMovie1or2() throws InterruptedException, ExecutionException { - final CompletableFuture> f = db.query( - "WITH actors FOR x IN ANY 'movies/TheMatrix' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN x._id", - null, null, String.class); - f.whenComplete((cursor, ex) -> { - assertThat(cursor.asListRemaining(), - hasItems("actors/Keanu", "actors/Hugo", "actors/Emil", "actors/Carrie", "actors/Laurence")); - }).get(); - } - - /** - * @throws ExecutionException - * @throws InterruptedException - * @see AQL - * Example Queries on an Actors and Movies Database - */ - @Test - public void allActorsActsInMovie1or2UnionDistinct() throws InterruptedException, ExecutionException { - final CompletableFuture> f = db.query( - "WITH actors FOR x IN UNION_DISTINCT ((FOR y IN ANY 'movies/TheMatrix' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN y._id), (FOR y IN ANY 'movies/TheDevilsAdvocate' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN y._id)) RETURN x", - null, null, String.class); - f.whenComplete((cursor, ex) -> { - assertThat(cursor.asListRemaining(), hasItems("actors/Emil", "actors/Hugo", "actors/Carrie", - "actors/Laurence", "actors/Keanu", "actors/Al", "actors/Charlize")); - }).get(); - } - - /** - * @throws ExecutionException - * @throws InterruptedException - * @see AQL - * Example Queries on an Actors and Movies Database - */ - @Test - public void allActorsActsInMovie1and2() throws InterruptedException, ExecutionException { - final CompletableFuture> f = db.query( - "WITH actors FOR x IN INTERSECTION ((FOR y IN ANY 'movies/TheMatrix' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN y._id), (FOR y IN ANY 'movies/TheDevilsAdvocate' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN y._id)) RETURN x", - null, null, String.class); - f.whenComplete((cursor, ex) -> { - assertThat(cursor.asListRemaining(), hasItems("actors/Keanu")); - }).get(); - } - - /** - * @throws ExecutionException - * @throws InterruptedException - * @see AQL - * Example Queries on an Actors and Movies Database - */ - @Test - public void allMoviesBetweenActor1andActor2() throws InterruptedException, ExecutionException { - final CompletableFuture> f = db.query( - "WITH movies FOR x IN INTERSECTION ((FOR y IN ANY 'actors/Hugo' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN y._id), (FOR y IN ANY 'actors/Keanu' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN y._id)) RETURN x", - null, null, String.class); - f.whenComplete((cursor, ex) -> { - assertThat(cursor.asListRemaining(), - hasItems("movies/TheMatrixRevolutions", "movies/TheMatrixReloaded", "movies/TheMatrix")); - }).get(); - } - - /** - * @throws ExecutionException - * @throws InterruptedException - * @see AQL - * Example Queries on an Actors and Movies Database - */ - @Test - public void allActorsWhoActedIn3orMoreMovies() throws InterruptedException, ExecutionException { - final CompletableFuture> f = db.query( - "FOR x IN actsIn COLLECT actor = x._from WITH COUNT INTO counter FILTER counter >= 3 RETURN {actor: actor, movies: counter}", - null, null, Actor.class); - f.whenComplete((cursor, ex) -> { - assertThat(cursor.asListRemaining(), - hasItems(new Actor("actors/Carrie", 3), new Actor("actors/CubaG", 4), new Actor("actors/Hugo", 3), - new Actor("actors/Keanu", 4), new Actor("actors/Laurence", 3), new Actor("actors/MegR", 5), - new Actor("actors/TomC", 3), new Actor("actors/TomH", 3))); - }).get(); - } - - /** - * @throws ExecutionException - * @throws InterruptedException - * @see AQL - * Example Queries on an Actors and Movies Database - */ - @Test - public void allMoviesWhereExactly6ActorsActedIn() throws InterruptedException, ExecutionException { - final CompletableFuture> f = db.query( - "FOR x IN actsIn COLLECT movie = x._to WITH COUNT INTO counter FILTER counter == 6 RETURN movie", null, - null, String.class); - f.whenComplete((cursor, ex) -> { - assertThat(cursor.asListRemaining(), - hasItems("movies/SleeplessInSeattle", "movies/TopGun", "movies/YouveGotMail")); - }).get(); - } - - /** - * @throws ExecutionException - * @throws InterruptedException - * @see AQL - * Example Queries on an Actors and Movies Database - */ - @Test - public void theNumberOfActorsByMovie() throws InterruptedException, ExecutionException { - final CompletableFuture> f = db.query( - "FOR x IN actsIn COLLECT movie = x._to WITH COUNT INTO counter RETURN {movie: movie, actors: counter}", - null, null, Movie.class); - f.whenComplete((cursor, ex) -> { - assertThat(cursor.asListRemaining(), - hasItems(new Movie("movies/AFewGoodMen", 11), new Movie("movies/AsGoodAsItGets", 4), - new Movie("movies/JerryMaguire", 9), new Movie("movies/JoeVersustheVolcano", 3), - new Movie("movies/SleeplessInSeattle", 6), new Movie("movies/SnowFallingonCedars", 4), - new Movie("movies/StandByMe", 7), new Movie("movies/TheDevilsAdvocate", 3), - new Movie("movies/TheMatrix", 5), new Movie("movies/TheMatrixReloaded", 4), - new Movie("movies/TheMatrixRevolutions", 4), new Movie("movies/TopGun", 6), - new Movie("movies/WhatDreamsMayCome", 5), new Movie("movies/WhenHarryMetSally", 4), - new Movie("movies/YouveGotMail", 6))); - }).get(); - } - - /** - * @throws ExecutionException - * @throws InterruptedException - * @see AQL - * Example Queries on an Actors and Movies Database - */ - @Test - public void theNumberOfMoviesByActor() throws InterruptedException, ExecutionException { - final CompletableFuture> f = db.query( - "FOR x IN actsIn COLLECT actor = x._from WITH COUNT INTO counter RETURN {actor: actor, movies: counter}", - null, null, Actor.class); - f.whenComplete((cursor, ex) -> { - assertThat(cursor.asListRemaining(), - hasItems(new Actor("actors/Al", 1), new Actor("actors/AnnabellaS", 1), new Actor("actors/AnthonyE", 1), - new Actor("actors/BillPull", 1), new Actor("actors/BillyC", 1), new Actor("actors/BonnieH", 1), - new Actor("actors/BrunoK", 1), new Actor("actors/Carrie", 3), new Actor("actors/CarrieF", 1), - new Actor("actors/Charlize", 1), new Actor("actors/ChristopherG", 1), new Actor("actors/CoreyF", 1), - new Actor("actors/CubaG", 4), new Actor("actors/DaveC", 1), new Actor("actors/DemiM", 1), - new Actor("actors/Emil", 1), new Actor("actors/EthanH", 1), new Actor("actors/GregK", 2), - new Actor("actors/HelenH", 1), new Actor("actors/Hugo", 3), new Actor("actors/JackN", 2), - new Actor("actors/JamesC", 1), new Actor("actors/JamesM", 1), new Actor("actors/JayM", 1), - new Actor("actors/JerryO", 2), new Actor("actors/JohnC", 1), new Actor("actors/JonathanL", 1), - new Actor("actors/JTW", 1), new Actor("actors/Keanu", 4), new Actor("actors/KellyM", 1), - new Actor("actors/KellyP", 1), new Actor("actors/KevinB", 1), new Actor("actors/KevinP", 1), - new Actor("actors/KieferS", 2), new Actor("actors/Laurence", 3), new Actor("actors/MarshallB", 1), - new Actor("actors/MaxS", 2), new Actor("actors/MegR", 5), new Actor("actors/Nathan", 1), - new Actor("actors/NoahW", 1), new Actor("actors/ParkerP", 1), new Actor("actors/ReginaK", 1), - new Actor("actors/ReneeZ", 1), new Actor("actors/RickY", 1), new Actor("actors/RitaW", 1), - new Actor("actors/RiverP", 1), new Actor("actors/Robin", 1), new Actor("actors/RosieO", 1), - new Actor("actors/SteveZ", 1), new Actor("actors/TomC", 3), new Actor("actors/TomH", 3), - new Actor("actors/TomS", 1), new Actor("actors/ValK", 1), new Actor("actors/VictorG", 1), - new Actor("actors/WernerH", 1), new Actor("actors/WilW", 1))); - }).get(); - } - - /** - * @throws ExecutionException - * @throws InterruptedException - * @see AQL - * Example Queries on an Actors and Movies Database - */ - @Test - public void theNumberOfMoviesActedInBetween2005and2010byActor() throws InterruptedException, ExecutionException { - final CompletableFuture> f = db.query( - "FOR x IN actsIn FILTER x.year >= 1990 && x.year <= 1995 COLLECT actor = x._from WITH COUNT INTO counter RETURN {actor: actor, movies: counter}", - null, null, Actor.class); - f.whenComplete((cursor, ex) -> { - assertThat(cursor.asListRemaining(), - hasItems(new Actor("actors/BillPull", 1), new Actor("actors/ChristopherG", 1), - new Actor("actors/CubaG", 1), new Actor("actors/DemiM", 1), new Actor("actors/JackN", 1), - new Actor("actors/JamesM", 1), new Actor("actors/JTW", 1), new Actor("actors/KevinB", 1), - new Actor("actors/KieferS", 1), new Actor("actors/MegR", 2), new Actor("actors/Nathan", 1), - new Actor("actors/NoahW", 1), new Actor("actors/RitaW", 1), new Actor("actors/RosieO", 1), - new Actor("actors/TomC", 1), new Actor("actors/TomH", 2), new Actor("actors/VictorG", 1))); - }).get(); - } - - public static class Actor { - private String actor; - private Integer movies; - - public Actor() { - super(); - } - - public Actor(final String actor, final Integer movies) { - super(); - this.actor = actor; - this.movies = movies; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((actor == null) ? 0 : actor.hashCode()); - result = prime * result + ((movies == null) ? 0 : movies.hashCode()); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final Actor other = (Actor) obj; - if (actor == null) { - if (other.actor != null) { - return false; - } - } else if (!actor.equals(other.actor)) { - return false; - } - if (movies == null) { - if (other.movies != null) { - return false; - } - } else if (!movies.equals(other.movies)) { - return false; - } - return true; } - - } - - public static class Movie { - private String movie; - private Integer actors; - - public Movie() { - super(); - } - - public Movie(final String movie, final Integer actors) { - super(); - this.movie = movie; - this.actors = actors; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((actors == null) ? 0 : actors.hashCode()); - result = prime * result + ((movie == null) ? 0 : movie.hashCode()); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final Movie other = (Movie) obj; - if (actors == null) { - if (other.actors != null) { - return false; - } - } else if (!actors.equals(other.actors)) { - return false; - } - if (movie == null) { - if (other.movie != null) { - return false; - } - } else if (!movie.equals(other.movie)) { - return false; - } - return true; - } - - } - - private static DocumentCreateEntity saveMovie( - final ArangoCollectionAsync movies, - final String key, - final String title, - final int released, - final String tagline) throws InterruptedException, ExecutionException { - final BaseDocument value = new BaseDocument(); - value.setKey(key); - value.addAttribute("title", title); - value.addAttribute("released", released); - value.addAttribute("tagline", tagline); - return movies.insertDocument(value).get(); - } - - private static DocumentCreateEntity saveActor( - final ArangoCollectionAsync actors, - final String key, - final String name, - final int born) throws InterruptedException, ExecutionException { - final BaseDocument value = new BaseDocument(); - value.setKey(key); - value.addAttribute("name", name); - value.addAttribute("born", born); - return actors.insertDocument(value).get(); - } - - private static DocumentCreateEntity saveActsIn( - final ArangoCollectionAsync actsIn, - final String actor, - final String movie, - final String[] roles, - final int year) throws InterruptedException, ExecutionException { - final BaseEdgeDocument value = new BaseEdgeDocument(); - value.setFrom(actor); - value.setTo(movie); - value.addAttribute("roles", roles); - value.addAttribute("year", year); - return actsIn.insertDocument(value).get(); - } - - private static void createData() throws InterruptedException, ExecutionException { - db.createCollection("actors").get(); - final ArangoCollectionAsync actors = db.collection("actors"); - db.createCollection("movies").get(); - final ArangoCollectionAsync movies = db.collection("movies"); - db.createCollection("actsIn", new CollectionCreateOptions().type(CollectionType.EDGES)).get(); - final ArangoCollectionAsync actsIn = db.collection("actsIn"); - - final String theMatrix = saveMovie(movies, "TheMatrix", "The Matrix", 1999, "Welcome to the Real World") - .getId(); - final String keanu = saveActor(actors, "Keanu", "Keanu Reeves", 1964).getId(); - final String carrie = saveActor(actors, "Carrie", "Carrie-Anne Moss", 1967).getId(); - final String laurence = saveActor(actors, "Laurence", "Laurence Fishburne", 1961).getId(); - final String hugo = saveActor(actors, "Hugo", "Hugo Weaving", 1960).getId(); - final String emil = saveActor(actors, "Emil", "Emil Eifrem", 1978).getId(); - - saveActsIn(actsIn, keanu, theMatrix, new String[] { "Neo" }, 1999); - saveActsIn(actsIn, carrie, theMatrix, new String[] { "Trinity" }, 1999); - saveActsIn(actsIn, laurence, theMatrix, new String[] { "Morpheus" }, 1999); - saveActsIn(actsIn, hugo, theMatrix, new String[] { "Agent Smith" }, 1999); - saveActsIn(actsIn, emil, theMatrix, new String[] { "Emil" }, 1999); - - final String theMatrixReloaded = saveMovie(movies, "TheMatrixReloaded", "The Matrix Reloaded", 2003, - "Free your mind").getId(); - saveActsIn(actsIn, keanu, theMatrixReloaded, new String[] { "Neo" }, 2003); - saveActsIn(actsIn, carrie, theMatrixReloaded, new String[] { "Trinity" }, 2003); - saveActsIn(actsIn, laurence, theMatrixReloaded, new String[] { "Morpheus" }, 2003); - saveActsIn(actsIn, hugo, theMatrixReloaded, new String[] { "Agent Smith" }, 2003); - - final String theMatrixRevolutions = saveMovie(movies, "TheMatrixRevolutions", "The Matrix Revolutions", 2003, - "Everything that has a beginning has an end").getId(); - saveActsIn(actsIn, keanu, theMatrixRevolutions, new String[] { "Neo" }, 2003); - saveActsIn(actsIn, carrie, theMatrixRevolutions, new String[] { "Trinity" }, 2003); - saveActsIn(actsIn, laurence, theMatrixRevolutions, new String[] { "Morpheus" }, 2003); - saveActsIn(actsIn, hugo, theMatrixRevolutions, new String[] { "Agent Smith" }, 2003); - - final String theDevilsAdvocate = saveMovie(movies, "TheDevilsAdvocate", "The Devil's Advocate", 1997, - "Evil has its winning ways").getId(); - final String charlize = saveActor(actors, "Charlize", "Charlize Theron", 1975).getId(); - final String al = saveActor(actors, "Al", "Al Pacino", 1940).getId(); - saveActsIn(actsIn, keanu, theDevilsAdvocate, new String[] { "Kevin Lomax" }, 1997); - saveActsIn(actsIn, charlize, theDevilsAdvocate, new String[] { "Mary Ann Lomax" }, 1997); - saveActsIn(actsIn, al, theDevilsAdvocate, new String[] { "John Milton" }, 1997); - - final String AFewGoodMen = saveMovie(movies, "AFewGoodMen", "A Few Good Men", 1992, - "In the heart of the nation's capital, in a courthouse of the U.S. government, one man will stop at nothing to keep his honor, and one will stop at nothing to find the truth.") - .getId(); - final String tomC = saveActor(actors, "TomC", "Tom Cruise", 1962).getId(); - final String jackN = saveActor(actors, "JackN", "Jack Nicholson", 1937).getId(); - final String demiM = saveActor(actors, "DemiM", "Demi Moore", 1962).getId(); - final String kevinB = saveActor(actors, "KevinB", "Kevin Bacon", 1958).getId(); - final String kieferS = saveActor(actors, "KieferS", "Kiefer Sutherland", 1966).getId(); - final String noahW = saveActor(actors, "NoahW", "Noah Wyle", 1971).getId(); - final String cubaG = saveActor(actors, "CubaG", "Cuba Gooding Jr.", 1968).getId(); - final String kevinP = saveActor(actors, "KevinP", "Kevin Pollak", 1957).getId(); - final String jTW = saveActor(actors, "JTW", "J.T. Walsh", 1943).getId(); - final String jamesM = saveActor(actors, "JamesM", "James Marshall", 1967).getId(); - final String christopherG = saveActor(actors, "ChristopherG", "Christopher Guest", 1948).getId(); - saveActsIn(actsIn, tomC, AFewGoodMen, new String[] { "Lt. Daniel Kaffee" }, 1992); - saveActsIn(actsIn, jackN, AFewGoodMen, new String[] { "Col. Nathan R. Jessup" }, 1992); - saveActsIn(actsIn, demiM, AFewGoodMen, new String[] { "Lt. Cdr. JoAnne Galloway" }, 1992); - saveActsIn(actsIn, kevinB, AFewGoodMen, new String[] { "Capt. Jack Ross" }, 1992); - saveActsIn(actsIn, kieferS, AFewGoodMen, new String[] { "Lt. Jonathan Kendrick" }, 1992); - saveActsIn(actsIn, noahW, AFewGoodMen, new String[] { "Cpl. Jeffrey Barnes" }, 1992); - saveActsIn(actsIn, cubaG, AFewGoodMen, new String[] { "Cpl. Carl Hammaker" }, 1992); - saveActsIn(actsIn, kevinP, AFewGoodMen, new String[] { "Lt. Sam Weinberg" }, 1992); - saveActsIn(actsIn, jTW, AFewGoodMen, new String[] { "Lt. Col. Matthew Andrew Markinson" }, 1992); - saveActsIn(actsIn, jamesM, AFewGoodMen, new String[] { "Pfc. Louden Downey" }, 1992); - saveActsIn(actsIn, christopherG, AFewGoodMen, new String[] { "Dr. Stone" }, 1992); - - final String topGun = saveMovie(movies, "TopGun", "Top Gun", 1986, "I feel the need, the need for speed.") - .getId(); - final String kellyM = saveActor(actors, "KellyM", "Kelly McGillis", 1957).getId(); - final String valK = saveActor(actors, "ValK", "Val Kilmer", 1959).getId(); - final String anthonyE = saveActor(actors, "AnthonyE", "Anthony Edwards", 1962).getId(); - final String tomS = saveActor(actors, "TomS", "Tom Skerritt", 1933).getId(); - final String megR = saveActor(actors, "MegR", "Meg Ryan", 1961).getId(); - saveActsIn(actsIn, tomC, topGun, new String[] { "Maverick" }, 1986); - saveActsIn(actsIn, kellyM, topGun, new String[] { "Charlie" }, 1986); - saveActsIn(actsIn, valK, topGun, new String[] { "Iceman" }, 1986); - saveActsIn(actsIn, anthonyE, topGun, new String[] { "Goose" }, 1986); - saveActsIn(actsIn, tomS, topGun, new String[] { "Viper" }, 1986); - saveActsIn(actsIn, megR, topGun, new String[] { "Carole" }, 1986); - - final String jerryMaguire = saveMovie(movies, "JerryMaguire", "Jerry Maguire", 2000, - "The rest of his life begins now.").getId(); - final String reneeZ = saveActor(actors, "ReneeZ", "Renee Zellweger", 1969).getId(); - final String kellyP = saveActor(actors, "KellyP", "Kelly Preston", 1962).getId(); - final String jerryO = saveActor(actors, "JerryO", "Jerry O'Connell", 1974).getId(); - final String jayM = saveActor(actors, "JayM", "Jay Mohr", 1970).getId(); - final String bonnieH = saveActor(actors, "BonnieH", "Bonnie Hunt", 1961).getId(); - final String reginaK = saveActor(actors, "ReginaK", "Regina King", 1971).getId(); - final String jonathanL = saveActor(actors, "JonathanL", "Jonathan Lipnicki", 1996).getId(); - saveActsIn(actsIn, tomC, jerryMaguire, new String[] { "Jerry Maguire" }, 2000); - saveActsIn(actsIn, cubaG, jerryMaguire, new String[] { "Rod Tidwell" }, 2000); - saveActsIn(actsIn, reneeZ, jerryMaguire, new String[] { "Dorothy Boyd" }, 2000); - saveActsIn(actsIn, kellyP, jerryMaguire, new String[] { "Avery Bishop" }, 2000); - saveActsIn(actsIn, jerryO, jerryMaguire, new String[] { "Frank Cushman" }, 2000); - saveActsIn(actsIn, jayM, jerryMaguire, new String[] { "Bob Sugar" }, 2000); - saveActsIn(actsIn, bonnieH, jerryMaguire, new String[] { "Laurel Boyd" }, 2000); - saveActsIn(actsIn, reginaK, jerryMaguire, new String[] { "Marcee Tidwell" }, 2000); - saveActsIn(actsIn, jonathanL, jerryMaguire, new String[] { "Ray Boyd" }, 2000); - - final String standByMe = saveMovie(movies, "StandByMe", "Stand By Me", 1986, - "For some, it's the last real taste of innocence, and the first real taste of life. But for everyone, it's the time that memories are made of.") - .getId(); - final String riverP = saveActor(actors, "RiverP", "River Phoenix", 1970).getId(); - final String coreyF = saveActor(actors, "CoreyF", "Corey Feldman", 1971).getId(); - final String wilW = saveActor(actors, "WilW", "Wil Wheaton", 1972).getId(); - final String johnC = saveActor(actors, "JohnC", "John Cusack", 1966).getId(); - final String marshallB = saveActor(actors, "MarshallB", "Marshall Bell", 1942).getId(); - saveActsIn(actsIn, wilW, standByMe, new String[] { "Gordie Lachance" }, 1986); - saveActsIn(actsIn, riverP, standByMe, new String[] { "Chris Chambers" }, 1986); - saveActsIn(actsIn, jerryO, standByMe, new String[] { "Vern Tessio" }, 1986); - saveActsIn(actsIn, coreyF, standByMe, new String[] { "Teddy Duchamp" }, 1986); - saveActsIn(actsIn, johnC, standByMe, new String[] { "Denny Lachance" }, 1986); - saveActsIn(actsIn, kieferS, standByMe, new String[] { "Ace Merrill" }, 1986); - saveActsIn(actsIn, marshallB, standByMe, new String[] { "Mr. Lachance" }, 1986); - - final String asGoodAsItGets = saveMovie(movies, "AsGoodAsItGets", "As Good as It Gets", 1997, - "A comedy from the heart that goes for the throat.").getId(); - final String helenH = saveActor(actors, "HelenH", "Helen Hunt", 1963).getId(); - final String gregK = saveActor(actors, "GregK", "Greg Kinnear", 1963).getId(); - saveActsIn(actsIn, jackN, asGoodAsItGets, new String[] { "Melvin Udall" }, 1997); - saveActsIn(actsIn, helenH, asGoodAsItGets, new String[] { "Carol Connelly" }, 1997); - saveActsIn(actsIn, gregK, asGoodAsItGets, new String[] { "Simon Bishop" }, 1997); - saveActsIn(actsIn, cubaG, asGoodAsItGets, new String[] { "Frank Sachs" }, 1997); - - final String whatDreamsMayCome = saveMovie(movies, "WhatDreamsMayCome", "What Dreams May Come", 1998, - "After life there is more. The end is just the beginning.").getId(); - final String annabellaS = saveActor(actors, "AnnabellaS", "Annabella Sciorra", 1960).getId(); - final String maxS = saveActor(actors, "MaxS", "Max von Sydow", 1929).getId(); - final String wernerH = saveActor(actors, "WernerH", "Werner Herzog", 1942).getId(); - final String robin = saveActor(actors, "Robin", "Robin Williams", 1951).getId(); - saveActsIn(actsIn, robin, whatDreamsMayCome, new String[] { "Chris Nielsen" }, 1998); - saveActsIn(actsIn, cubaG, whatDreamsMayCome, new String[] { "Albert Lewis" }, 1998); - saveActsIn(actsIn, annabellaS, whatDreamsMayCome, new String[] { "Annie Collins-Nielsen" }, 1998); - saveActsIn(actsIn, maxS, whatDreamsMayCome, new String[] { "The Tracker" }, 1998); - saveActsIn(actsIn, wernerH, whatDreamsMayCome, new String[] { "The Face" }, 1998); - - final String snowFallingonCedars = saveMovie(movies, "SnowFallingonCedars", "Snow Falling on Cedars", 1999, - "First loves last. Forever.").getId(); - final String ethanH = saveActor(actors, "EthanH", "Ethan Hawke", 1970).getId(); - final String rickY = saveActor(actors, "RickY", "Rick Yune", 1971).getId(); - final String jamesC = saveActor(actors, "JamesC", "James Cromwell", 1940).getId(); - saveActsIn(actsIn, ethanH, snowFallingonCedars, new String[] { "Ishmael Chambers" }, 1999); - saveActsIn(actsIn, rickY, snowFallingonCedars, new String[] { "Kazuo Miyamoto" }, 1999); - saveActsIn(actsIn, maxS, snowFallingonCedars, new String[] { "Nels Gudmundsson" }, 1999); - saveActsIn(actsIn, jamesC, snowFallingonCedars, new String[] { "Judge Fielding" }, 1999); - - final String youveGotMail = saveMovie(movies, "YouveGotMail", "You've Got Mail", 1998, - "At odds in life... in love on-line.").getId(); - final String parkerP = saveActor(actors, "ParkerP", "Parker Posey", 1968).getId(); - final String daveC = saveActor(actors, "DaveC", "Dave Chappelle", 1973).getId(); - final String steveZ = saveActor(actors, "SteveZ", "Steve Zahn", 1967).getId(); - final String tomH = saveActor(actors, "TomH", "Tom Hanks", 1956).getId(); - saveActsIn(actsIn, tomH, youveGotMail, new String[] { "Joe Fox" }, 1998); - saveActsIn(actsIn, megR, youveGotMail, new String[] { "Kathleen Kelly" }, 1998); - saveActsIn(actsIn, gregK, youveGotMail, new String[] { "Frank Navasky" }, 1998); - saveActsIn(actsIn, parkerP, youveGotMail, new String[] { "Patricia Eden" }, 1998); - saveActsIn(actsIn, daveC, youveGotMail, new String[] { "Kevin Jackson" }, 1998); - saveActsIn(actsIn, steveZ, youveGotMail, new String[] { "George Pappas" }, 1998); - - final String sleeplessInSeattle = saveMovie(movies, "SleeplessInSeattle", "Sleepless in Seattle", 1993, - "What if someone you never met, someone you never saw, someone you never knew was the only someone for you?") - .getId(); - final String ritaW = saveActor(actors, "RitaW", "Rita Wilson", 1956).getId(); - final String billPull = saveActor(actors, "BillPull", "Bill Pullman", 1953).getId(); - final String victorG = saveActor(actors, "VictorG", "Victor Garber", 1949).getId(); - final String rosieO = saveActor(actors, "RosieO", "Rosie O'Donnell", 1962).getId(); - saveActsIn(actsIn, tomH, sleeplessInSeattle, new String[] { "Sam Baldwin" }, 1993); - saveActsIn(actsIn, megR, sleeplessInSeattle, new String[] { "Annie Reed" }, 1993); - saveActsIn(actsIn, ritaW, sleeplessInSeattle, new String[] { "Suzy" }, 1993); - saveActsIn(actsIn, billPull, sleeplessInSeattle, new String[] { "Walter" }, 1993); - saveActsIn(actsIn, victorG, sleeplessInSeattle, new String[] { "Greg" }, 1993); - saveActsIn(actsIn, rosieO, sleeplessInSeattle, new String[] { "Becky" }, 1993); - - final String joeVersustheVolcano = saveMovie(movies, "JoeVersustheVolcano", "Joe Versus the Volcano", 1990, - "A story of love, lava and burning desire.").getId(); - final String nathan = saveActor(actors, "Nathan", "Nathan Lane", 1956).getId(); - saveActsIn(actsIn, tomH, joeVersustheVolcano, new String[] { "Joe Banks" }, 1990); - saveActsIn(actsIn, megR, joeVersustheVolcano, - new String[] { "DeDe', 'Angelica Graynamore', 'Patricia Graynamore" }, 1990); - saveActsIn(actsIn, nathan, joeVersustheVolcano, new String[] { "Baw" }, 1990); - - final String whenHarryMetSally = saveMovie(movies, "WhenHarryMetSally", "When Harry Met Sally", 1998, - "At odds in life... in love on-line.").getId(); - final String billyC = saveActor(actors, "BillyC", "Billy Crystal", 1948).getId(); - final String carrieF = saveActor(actors, "CarrieF", "Carrie Fisher", 1956).getId(); - final String brunoK = saveActor(actors, "BrunoK", "Bruno Kirby", 1949).getId(); - saveActsIn(actsIn, billyC, whenHarryMetSally, new String[] { "Harry Burns" }, 1998); - saveActsIn(actsIn, megR, whenHarryMetSally, new String[] { "Sally Albright" }, 1998); - saveActsIn(actsIn, carrieF, whenHarryMetSally, new String[] { "Marie" }, 1998); - saveActsIn(actsIn, brunoK, whenHarryMetSally, new String[] { "Jess" }, 1998); - } + arangoDB.createDatabase(TEST_DB).get(); + db = arangoDB.db(TEST_DB); + createData(); + } + + @AfterClass + public static void tearDown() throws InterruptedException, ExecutionException { + db.drop().get(); + arangoDB.shutdown(); + } + + /** + * @throws ExecutionException + * @throws InterruptedException + * @see AQL + * Example Queries on an Actors and Movies Database + */ + @Test + public void allActorsActsInMovie1or2() throws InterruptedException, ExecutionException { + final CompletableFuture> f = db.query( + "WITH actors FOR x IN ANY 'movies/TheMatrix' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN x._id", + null, null, String.class); + f.whenComplete((cursor, ex) -> { + assertThat(cursor.asListRemaining(), + hasItems("actors/Keanu", "actors/Hugo", "actors/Emil", "actors/Carrie", "actors/Laurence")); + }).get(); + } + + /** + * @throws ExecutionException + * @throws InterruptedException + * @see AQL + * Example Queries on an Actors and Movies Database + */ + @Test + public void allActorsActsInMovie1or2UnionDistinct() throws InterruptedException, ExecutionException { + final CompletableFuture> f = db.query( + "WITH actors FOR x IN UNION_DISTINCT ((FOR y IN ANY 'movies/TheMatrix' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN y._id), (FOR y IN ANY 'movies/TheDevilsAdvocate' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN y._id)) RETURN x", + null, null, String.class); + f.whenComplete((cursor, ex) -> { + assertThat(cursor.asListRemaining(), hasItems("actors/Emil", "actors/Hugo", "actors/Carrie", + "actors/Laurence", "actors/Keanu", "actors/Al", "actors/Charlize")); + }).get(); + } + + /** + * @throws ExecutionException + * @throws InterruptedException + * @see AQL + * Example Queries on an Actors and Movies Database + */ + @Test + public void allActorsActsInMovie1and2() throws InterruptedException, ExecutionException { + final CompletableFuture> f = db.query( + "WITH actors FOR x IN INTERSECTION ((FOR y IN ANY 'movies/TheMatrix' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN y._id), (FOR y IN ANY 'movies/TheDevilsAdvocate' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN y._id)) RETURN x", + null, null, String.class); + f.whenComplete((cursor, ex) -> { + assertThat(cursor.asListRemaining(), hasItems("actors/Keanu")); + }).get(); + } + + /** + * @throws ExecutionException + * @throws InterruptedException + * @see AQL + * Example Queries on an Actors and Movies Database + */ + @Test + public void allMoviesBetweenActor1andActor2() throws InterruptedException, ExecutionException { + final CompletableFuture> f = db.query( + "WITH movies FOR x IN INTERSECTION ((FOR y IN ANY 'actors/Hugo' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN y._id), (FOR y IN ANY 'actors/Keanu' actsIn OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN y._id)) RETURN x", + null, null, String.class); + f.whenComplete((cursor, ex) -> { + assertThat(cursor.asListRemaining(), + hasItems("movies/TheMatrixRevolutions", "movies/TheMatrixReloaded", "movies/TheMatrix")); + }).get(); + } + + /** + * @throws ExecutionException + * @throws InterruptedException + * @see AQL + * Example Queries on an Actors and Movies Database + */ + @Test + public void allActorsWhoActedIn3orMoreMovies() throws InterruptedException, ExecutionException { + final CompletableFuture> f = db.query( + "FOR x IN actsIn COLLECT actor = x._from WITH COUNT INTO counter FILTER counter >= 3 RETURN {actor: actor, movies: counter}", + null, null, Actor.class); + f.whenComplete((cursor, ex) -> { + assertThat(cursor.asListRemaining(), + hasItems(new Actor("actors/Carrie", 3), new Actor("actors/CubaG", 4), new Actor("actors/Hugo", 3), + new Actor("actors/Keanu", 4), new Actor("actors/Laurence", 3), new Actor("actors/MegR", 5), + new Actor("actors/TomC", 3), new Actor("actors/TomH", 3))); + }).get(); + } + + /** + * @throws ExecutionException + * @throws InterruptedException + * @see AQL + * Example Queries on an Actors and Movies Database + */ + @Test + public void allMoviesWhereExactly6ActorsActedIn() throws InterruptedException, ExecutionException { + final CompletableFuture> f = db.query( + "FOR x IN actsIn COLLECT movie = x._to WITH COUNT INTO counter FILTER counter == 6 RETURN movie", null, + null, String.class); + f.whenComplete((cursor, ex) -> { + assertThat(cursor.asListRemaining(), + hasItems("movies/SleeplessInSeattle", "movies/TopGun", "movies/YouveGotMail")); + }).get(); + } + + /** + * @throws ExecutionException + * @throws InterruptedException + * @see AQL + * Example Queries on an Actors and Movies Database + */ + @Test + public void theNumberOfActorsByMovie() throws InterruptedException, ExecutionException { + final CompletableFuture> f = db.query( + "FOR x IN actsIn COLLECT movie = x._to WITH COUNT INTO counter RETURN {movie: movie, actors: counter}", + null, null, Movie.class); + f.whenComplete((cursor, ex) -> { + assertThat(cursor.asListRemaining(), + hasItems(new Movie("movies/AFewGoodMen", 11), new Movie("movies/AsGoodAsItGets", 4), + new Movie("movies/JerryMaguire", 9), new Movie("movies/JoeVersustheVolcano", 3), + new Movie("movies/SleeplessInSeattle", 6), new Movie("movies/SnowFallingonCedars", 4), + new Movie("movies/StandByMe", 7), new Movie("movies/TheDevilsAdvocate", 3), + new Movie("movies/TheMatrix", 5), new Movie("movies/TheMatrixReloaded", 4), + new Movie("movies/TheMatrixRevolutions", 4), new Movie("movies/TopGun", 6), + new Movie("movies/WhatDreamsMayCome", 5), new Movie("movies/WhenHarryMetSally", 4), + new Movie("movies/YouveGotMail", 6))); + }).get(); + } + + /** + * @throws ExecutionException + * @throws InterruptedException + * @see AQL + * Example Queries on an Actors and Movies Database + */ + @Test + public void theNumberOfMoviesByActor() throws InterruptedException, ExecutionException { + final CompletableFuture> f = db.query( + "FOR x IN actsIn COLLECT actor = x._from WITH COUNT INTO counter RETURN {actor: actor, movies: counter}", + null, null, Actor.class); + f.whenComplete((cursor, ex) -> { + assertThat(cursor.asListRemaining(), + hasItems(new Actor("actors/Al", 1), new Actor("actors/AnnabellaS", 1), new Actor("actors/AnthonyE", 1), + new Actor("actors/BillPull", 1), new Actor("actors/BillyC", 1), new Actor("actors/BonnieH", 1), + new Actor("actors/BrunoK", 1), new Actor("actors/Carrie", 3), new Actor("actors/CarrieF", 1), + new Actor("actors/Charlize", 1), new Actor("actors/ChristopherG", 1), new Actor("actors/CoreyF", 1), + new Actor("actors/CubaG", 4), new Actor("actors/DaveC", 1), new Actor("actors/DemiM", 1), + new Actor("actors/Emil", 1), new Actor("actors/EthanH", 1), new Actor("actors/GregK", 2), + new Actor("actors/HelenH", 1), new Actor("actors/Hugo", 3), new Actor("actors/JackN", 2), + new Actor("actors/JamesC", 1), new Actor("actors/JamesM", 1), new Actor("actors/JayM", 1), + new Actor("actors/JerryO", 2), new Actor("actors/JohnC", 1), new Actor("actors/JonathanL", 1), + new Actor("actors/JTW", 1), new Actor("actors/Keanu", 4), new Actor("actors/KellyM", 1), + new Actor("actors/KellyP", 1), new Actor("actors/KevinB", 1), new Actor("actors/KevinP", 1), + new Actor("actors/KieferS", 2), new Actor("actors/Laurence", 3), new Actor("actors/MarshallB", 1), + new Actor("actors/MaxS", 2), new Actor("actors/MegR", 5), new Actor("actors/Nathan", 1), + new Actor("actors/NoahW", 1), new Actor("actors/ParkerP", 1), new Actor("actors/ReginaK", 1), + new Actor("actors/ReneeZ", 1), new Actor("actors/RickY", 1), new Actor("actors/RitaW", 1), + new Actor("actors/RiverP", 1), new Actor("actors/Robin", 1), new Actor("actors/RosieO", 1), + new Actor("actors/SteveZ", 1), new Actor("actors/TomC", 3), new Actor("actors/TomH", 3), + new Actor("actors/TomS", 1), new Actor("actors/ValK", 1), new Actor("actors/VictorG", 1), + new Actor("actors/WernerH", 1), new Actor("actors/WilW", 1))); + }).get(); + } + + /** + * @throws ExecutionException + * @throws InterruptedException + * @see AQL + * Example Queries on an Actors and Movies Database + */ + @Test + public void theNumberOfMoviesActedInBetween2005and2010byActor() throws InterruptedException, ExecutionException { + final CompletableFuture> f = db.query( + "FOR x IN actsIn FILTER x.year >= 1990 && x.year <= 1995 COLLECT actor = x._from WITH COUNT INTO counter RETURN {actor: actor, movies: counter}", + null, null, Actor.class); + f.whenComplete((cursor, ex) -> { + assertThat(cursor.asListRemaining(), + hasItems(new Actor("actors/BillPull", 1), new Actor("actors/ChristopherG", 1), + new Actor("actors/CubaG", 1), new Actor("actors/DemiM", 1), new Actor("actors/JackN", 1), + new Actor("actors/JamesM", 1), new Actor("actors/JTW", 1), new Actor("actors/KevinB", 1), + new Actor("actors/KieferS", 1), new Actor("actors/MegR", 2), new Actor("actors/Nathan", 1), + new Actor("actors/NoahW", 1), new Actor("actors/RitaW", 1), new Actor("actors/RosieO", 1), + new Actor("actors/TomC", 1), new Actor("actors/TomH", 2), new Actor("actors/VictorG", 1))); + }).get(); + } + + public static class Actor { + private String actor; + private Integer movies; + + public Actor() { + super(); + } + + public Actor(final String actor, final Integer movies) { + super(); + this.actor = actor; + this.movies = movies; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((actor == null) ? 0 : actor.hashCode()); + result = prime * result + ((movies == null) ? 0 : movies.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Actor other = (Actor) obj; + if (actor == null) { + if (other.actor != null) { + return false; + } + } else if (!actor.equals(other.actor)) { + return false; + } + if (movies == null) { + if (other.movies != null) { + return false; + } + } else if (!movies.equals(other.movies)) { + return false; + } + return true; + } + + } + + public static class Movie { + private String movie; + private Integer actors; + + public Movie() { + super(); + } + + public Movie(final String movie, final Integer actors) { + super(); + this.movie = movie; + this.actors = actors; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((actors == null) ? 0 : actors.hashCode()); + result = prime * result + ((movie == null) ? 0 : movie.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Movie other = (Movie) obj; + if (actors == null) { + if (other.actors != null) { + return false; + } + } else if (!actors.equals(other.actors)) { + return false; + } + if (movie == null) { + if (other.movie != null) { + return false; + } + } else if (!movie.equals(other.movie)) { + return false; + } + return true; + } + + } + + private static DocumentCreateEntity saveMovie( + final ArangoCollectionAsync movies, + final String key, + final String title, + final int released, + final String tagline) throws InterruptedException, ExecutionException { + final BaseDocument value = new BaseDocument(); + value.setKey(key); + value.addAttribute("title", title); + value.addAttribute("released", released); + value.addAttribute("tagline", tagline); + return movies.insertDocument(value).get(); + } + + private static DocumentCreateEntity saveActor( + final ArangoCollectionAsync actors, + final String key, + final String name, + final int born) throws InterruptedException, ExecutionException { + final BaseDocument value = new BaseDocument(); + value.setKey(key); + value.addAttribute("name", name); + value.addAttribute("born", born); + return actors.insertDocument(value).get(); + } + + private static DocumentCreateEntity saveActsIn( + final ArangoCollectionAsync actsIn, + final String actor, + final String movie, + final String[] roles, + final int year) throws InterruptedException, ExecutionException { + final BaseEdgeDocument value = new BaseEdgeDocument(); + value.setFrom(actor); + value.setTo(movie); + value.addAttribute("roles", roles); + value.addAttribute("year", year); + return actsIn.insertDocument(value).get(); + } + + private static void createData() throws InterruptedException, ExecutionException { + db.createCollection("actors").get(); + final ArangoCollectionAsync actors = db.collection("actors"); + db.createCollection("movies").get(); + final ArangoCollectionAsync movies = db.collection("movies"); + db.createCollection("actsIn", new CollectionCreateOptions().type(CollectionType.EDGES)).get(); + final ArangoCollectionAsync actsIn = db.collection("actsIn"); + + final String theMatrix = saveMovie(movies, "TheMatrix", "The Matrix", 1999, "Welcome to the Real World") + .getId(); + final String keanu = saveActor(actors, "Keanu", "Keanu Reeves", 1964).getId(); + final String carrie = saveActor(actors, "Carrie", "Carrie-Anne Moss", 1967).getId(); + final String laurence = saveActor(actors, "Laurence", "Laurence Fishburne", 1961).getId(); + final String hugo = saveActor(actors, "Hugo", "Hugo Weaving", 1960).getId(); + final String emil = saveActor(actors, "Emil", "Emil Eifrem", 1978).getId(); + + saveActsIn(actsIn, keanu, theMatrix, new String[]{"Neo"}, 1999); + saveActsIn(actsIn, carrie, theMatrix, new String[]{"Trinity"}, 1999); + saveActsIn(actsIn, laurence, theMatrix, new String[]{"Morpheus"}, 1999); + saveActsIn(actsIn, hugo, theMatrix, new String[]{"Agent Smith"}, 1999); + saveActsIn(actsIn, emil, theMatrix, new String[]{"Emil"}, 1999); + + final String theMatrixReloaded = saveMovie(movies, "TheMatrixReloaded", "The Matrix Reloaded", 2003, + "Free your mind").getId(); + saveActsIn(actsIn, keanu, theMatrixReloaded, new String[]{"Neo"}, 2003); + saveActsIn(actsIn, carrie, theMatrixReloaded, new String[]{"Trinity"}, 2003); + saveActsIn(actsIn, laurence, theMatrixReloaded, new String[]{"Morpheus"}, 2003); + saveActsIn(actsIn, hugo, theMatrixReloaded, new String[]{"Agent Smith"}, 2003); + + final String theMatrixRevolutions = saveMovie(movies, "TheMatrixRevolutions", "The Matrix Revolutions", 2003, + "Everything that has a beginning has an end").getId(); + saveActsIn(actsIn, keanu, theMatrixRevolutions, new String[]{"Neo"}, 2003); + saveActsIn(actsIn, carrie, theMatrixRevolutions, new String[]{"Trinity"}, 2003); + saveActsIn(actsIn, laurence, theMatrixRevolutions, new String[]{"Morpheus"}, 2003); + saveActsIn(actsIn, hugo, theMatrixRevolutions, new String[]{"Agent Smith"}, 2003); + + final String theDevilsAdvocate = saveMovie(movies, "TheDevilsAdvocate", "The Devil's Advocate", 1997, + "Evil has its winning ways").getId(); + final String charlize = saveActor(actors, "Charlize", "Charlize Theron", 1975).getId(); + final String al = saveActor(actors, "Al", "Al Pacino", 1940).getId(); + saveActsIn(actsIn, keanu, theDevilsAdvocate, new String[]{"Kevin Lomax"}, 1997); + saveActsIn(actsIn, charlize, theDevilsAdvocate, new String[]{"Mary Ann Lomax"}, 1997); + saveActsIn(actsIn, al, theDevilsAdvocate, new String[]{"John Milton"}, 1997); + + final String AFewGoodMen = saveMovie(movies, "AFewGoodMen", "A Few Good Men", 1992, + "In the heart of the nation's capital, in a courthouse of the U.S. government, one man will stop at nothing to keep his honor, and one will stop at nothing to find the truth.") + .getId(); + final String tomC = saveActor(actors, "TomC", "Tom Cruise", 1962).getId(); + final String jackN = saveActor(actors, "JackN", "Jack Nicholson", 1937).getId(); + final String demiM = saveActor(actors, "DemiM", "Demi Moore", 1962).getId(); + final String kevinB = saveActor(actors, "KevinB", "Kevin Bacon", 1958).getId(); + final String kieferS = saveActor(actors, "KieferS", "Kiefer Sutherland", 1966).getId(); + final String noahW = saveActor(actors, "NoahW", "Noah Wyle", 1971).getId(); + final String cubaG = saveActor(actors, "CubaG", "Cuba Gooding Jr.", 1968).getId(); + final String kevinP = saveActor(actors, "KevinP", "Kevin Pollak", 1957).getId(); + final String jTW = saveActor(actors, "JTW", "J.T. Walsh", 1943).getId(); + final String jamesM = saveActor(actors, "JamesM", "James Marshall", 1967).getId(); + final String christopherG = saveActor(actors, "ChristopherG", "Christopher Guest", 1948).getId(); + saveActsIn(actsIn, tomC, AFewGoodMen, new String[]{"Lt. Daniel Kaffee"}, 1992); + saveActsIn(actsIn, jackN, AFewGoodMen, new String[]{"Col. Nathan R. Jessup"}, 1992); + saveActsIn(actsIn, demiM, AFewGoodMen, new String[]{"Lt. Cdr. JoAnne Galloway"}, 1992); + saveActsIn(actsIn, kevinB, AFewGoodMen, new String[]{"Capt. Jack Ross"}, 1992); + saveActsIn(actsIn, kieferS, AFewGoodMen, new String[]{"Lt. Jonathan Kendrick"}, 1992); + saveActsIn(actsIn, noahW, AFewGoodMen, new String[]{"Cpl. Jeffrey Barnes"}, 1992); + saveActsIn(actsIn, cubaG, AFewGoodMen, new String[]{"Cpl. Carl Hammaker"}, 1992); + saveActsIn(actsIn, kevinP, AFewGoodMen, new String[]{"Lt. Sam Weinberg"}, 1992); + saveActsIn(actsIn, jTW, AFewGoodMen, new String[]{"Lt. Col. Matthew Andrew Markinson"}, 1992); + saveActsIn(actsIn, jamesM, AFewGoodMen, new String[]{"Pfc. Louden Downey"}, 1992); + saveActsIn(actsIn, christopherG, AFewGoodMen, new String[]{"Dr. Stone"}, 1992); + + final String topGun = saveMovie(movies, "TopGun", "Top Gun", 1986, "I feel the need, the need for speed.") + .getId(); + final String kellyM = saveActor(actors, "KellyM", "Kelly McGillis", 1957).getId(); + final String valK = saveActor(actors, "ValK", "Val Kilmer", 1959).getId(); + final String anthonyE = saveActor(actors, "AnthonyE", "Anthony Edwards", 1962).getId(); + final String tomS = saveActor(actors, "TomS", "Tom Skerritt", 1933).getId(); + final String megR = saveActor(actors, "MegR", "Meg Ryan", 1961).getId(); + saveActsIn(actsIn, tomC, topGun, new String[]{"Maverick"}, 1986); + saveActsIn(actsIn, kellyM, topGun, new String[]{"Charlie"}, 1986); + saveActsIn(actsIn, valK, topGun, new String[]{"Iceman"}, 1986); + saveActsIn(actsIn, anthonyE, topGun, new String[]{"Goose"}, 1986); + saveActsIn(actsIn, tomS, topGun, new String[]{"Viper"}, 1986); + saveActsIn(actsIn, megR, topGun, new String[]{"Carole"}, 1986); + + final String jerryMaguire = saveMovie(movies, "JerryMaguire", "Jerry Maguire", 2000, + "The rest of his life begins now.").getId(); + final String reneeZ = saveActor(actors, "ReneeZ", "Renee Zellweger", 1969).getId(); + final String kellyP = saveActor(actors, "KellyP", "Kelly Preston", 1962).getId(); + final String jerryO = saveActor(actors, "JerryO", "Jerry O'Connell", 1974).getId(); + final String jayM = saveActor(actors, "JayM", "Jay Mohr", 1970).getId(); + final String bonnieH = saveActor(actors, "BonnieH", "Bonnie Hunt", 1961).getId(); + final String reginaK = saveActor(actors, "ReginaK", "Regina King", 1971).getId(); + final String jonathanL = saveActor(actors, "JonathanL", "Jonathan Lipnicki", 1996).getId(); + saveActsIn(actsIn, tomC, jerryMaguire, new String[]{"Jerry Maguire"}, 2000); + saveActsIn(actsIn, cubaG, jerryMaguire, new String[]{"Rod Tidwell"}, 2000); + saveActsIn(actsIn, reneeZ, jerryMaguire, new String[]{"Dorothy Boyd"}, 2000); + saveActsIn(actsIn, kellyP, jerryMaguire, new String[]{"Avery Bishop"}, 2000); + saveActsIn(actsIn, jerryO, jerryMaguire, new String[]{"Frank Cushman"}, 2000); + saveActsIn(actsIn, jayM, jerryMaguire, new String[]{"Bob Sugar"}, 2000); + saveActsIn(actsIn, bonnieH, jerryMaguire, new String[]{"Laurel Boyd"}, 2000); + saveActsIn(actsIn, reginaK, jerryMaguire, new String[]{"Marcee Tidwell"}, 2000); + saveActsIn(actsIn, jonathanL, jerryMaguire, new String[]{"Ray Boyd"}, 2000); + + final String standByMe = saveMovie(movies, "StandByMe", "Stand By Me", 1986, + "For some, it's the last real taste of innocence, and the first real taste of life. But for everyone, it's the time that memories are made of.") + .getId(); + final String riverP = saveActor(actors, "RiverP", "River Phoenix", 1970).getId(); + final String coreyF = saveActor(actors, "CoreyF", "Corey Feldman", 1971).getId(); + final String wilW = saveActor(actors, "WilW", "Wil Wheaton", 1972).getId(); + final String johnC = saveActor(actors, "JohnC", "John Cusack", 1966).getId(); + final String marshallB = saveActor(actors, "MarshallB", "Marshall Bell", 1942).getId(); + saveActsIn(actsIn, wilW, standByMe, new String[]{"Gordie Lachance"}, 1986); + saveActsIn(actsIn, riverP, standByMe, new String[]{"Chris Chambers"}, 1986); + saveActsIn(actsIn, jerryO, standByMe, new String[]{"Vern Tessio"}, 1986); + saveActsIn(actsIn, coreyF, standByMe, new String[]{"Teddy Duchamp"}, 1986); + saveActsIn(actsIn, johnC, standByMe, new String[]{"Denny Lachance"}, 1986); + saveActsIn(actsIn, kieferS, standByMe, new String[]{"Ace Merrill"}, 1986); + saveActsIn(actsIn, marshallB, standByMe, new String[]{"Mr. Lachance"}, 1986); + + final String asGoodAsItGets = saveMovie(movies, "AsGoodAsItGets", "As Good as It Gets", 1997, + "A comedy from the heart that goes for the throat.").getId(); + final String helenH = saveActor(actors, "HelenH", "Helen Hunt", 1963).getId(); + final String gregK = saveActor(actors, "GregK", "Greg Kinnear", 1963).getId(); + saveActsIn(actsIn, jackN, asGoodAsItGets, new String[]{"Melvin Udall"}, 1997); + saveActsIn(actsIn, helenH, asGoodAsItGets, new String[]{"Carol Connelly"}, 1997); + saveActsIn(actsIn, gregK, asGoodAsItGets, new String[]{"Simon Bishop"}, 1997); + saveActsIn(actsIn, cubaG, asGoodAsItGets, new String[]{"Frank Sachs"}, 1997); + + final String whatDreamsMayCome = saveMovie(movies, "WhatDreamsMayCome", "What Dreams May Come", 1998, + "After life there is more. The end is just the beginning.").getId(); + final String annabellaS = saveActor(actors, "AnnabellaS", "Annabella Sciorra", 1960).getId(); + final String maxS = saveActor(actors, "MaxS", "Max von Sydow", 1929).getId(); + final String wernerH = saveActor(actors, "WernerH", "Werner Herzog", 1942).getId(); + final String robin = saveActor(actors, "Robin", "Robin Williams", 1951).getId(); + saveActsIn(actsIn, robin, whatDreamsMayCome, new String[]{"Chris Nielsen"}, 1998); + saveActsIn(actsIn, cubaG, whatDreamsMayCome, new String[]{"Albert Lewis"}, 1998); + saveActsIn(actsIn, annabellaS, whatDreamsMayCome, new String[]{"Annie Collins-Nielsen"}, 1998); + saveActsIn(actsIn, maxS, whatDreamsMayCome, new String[]{"The Tracker"}, 1998); + saveActsIn(actsIn, wernerH, whatDreamsMayCome, new String[]{"The Face"}, 1998); + + final String snowFallingonCedars = saveMovie(movies, "SnowFallingonCedars", "Snow Falling on Cedars", 1999, + "First loves last. Forever.").getId(); + final String ethanH = saveActor(actors, "EthanH", "Ethan Hawke", 1970).getId(); + final String rickY = saveActor(actors, "RickY", "Rick Yune", 1971).getId(); + final String jamesC = saveActor(actors, "JamesC", "James Cromwell", 1940).getId(); + saveActsIn(actsIn, ethanH, snowFallingonCedars, new String[]{"Ishmael Chambers"}, 1999); + saveActsIn(actsIn, rickY, snowFallingonCedars, new String[]{"Kazuo Miyamoto"}, 1999); + saveActsIn(actsIn, maxS, snowFallingonCedars, new String[]{"Nels Gudmundsson"}, 1999); + saveActsIn(actsIn, jamesC, snowFallingonCedars, new String[]{"Judge Fielding"}, 1999); + + final String youveGotMail = saveMovie(movies, "YouveGotMail", "You've Got Mail", 1998, + "At odds in life... in love on-line.").getId(); + final String parkerP = saveActor(actors, "ParkerP", "Parker Posey", 1968).getId(); + final String daveC = saveActor(actors, "DaveC", "Dave Chappelle", 1973).getId(); + final String steveZ = saveActor(actors, "SteveZ", "Steve Zahn", 1967).getId(); + final String tomH = saveActor(actors, "TomH", "Tom Hanks", 1956).getId(); + saveActsIn(actsIn, tomH, youveGotMail, new String[]{"Joe Fox"}, 1998); + saveActsIn(actsIn, megR, youveGotMail, new String[]{"Kathleen Kelly"}, 1998); + saveActsIn(actsIn, gregK, youveGotMail, new String[]{"Frank Navasky"}, 1998); + saveActsIn(actsIn, parkerP, youveGotMail, new String[]{"Patricia Eden"}, 1998); + saveActsIn(actsIn, daveC, youveGotMail, new String[]{"Kevin Jackson"}, 1998); + saveActsIn(actsIn, steveZ, youveGotMail, new String[]{"George Pappas"}, 1998); + + final String sleeplessInSeattle = saveMovie(movies, "SleeplessInSeattle", "Sleepless in Seattle", 1993, + "What if someone you never met, someone you never saw, someone you never knew was the only someone for you?") + .getId(); + final String ritaW = saveActor(actors, "RitaW", "Rita Wilson", 1956).getId(); + final String billPull = saveActor(actors, "BillPull", "Bill Pullman", 1953).getId(); + final String victorG = saveActor(actors, "VictorG", "Victor Garber", 1949).getId(); + final String rosieO = saveActor(actors, "RosieO", "Rosie O'Donnell", 1962).getId(); + saveActsIn(actsIn, tomH, sleeplessInSeattle, new String[]{"Sam Baldwin"}, 1993); + saveActsIn(actsIn, megR, sleeplessInSeattle, new String[]{"Annie Reed"}, 1993); + saveActsIn(actsIn, ritaW, sleeplessInSeattle, new String[]{"Suzy"}, 1993); + saveActsIn(actsIn, billPull, sleeplessInSeattle, new String[]{"Walter"}, 1993); + saveActsIn(actsIn, victorG, sleeplessInSeattle, new String[]{"Greg"}, 1993); + saveActsIn(actsIn, rosieO, sleeplessInSeattle, new String[]{"Becky"}, 1993); + + final String joeVersustheVolcano = saveMovie(movies, "JoeVersustheVolcano", "Joe Versus the Volcano", 1990, + "A story of love, lava and burning desire.").getId(); + final String nathan = saveActor(actors, "Nathan", "Nathan Lane", 1956).getId(); + saveActsIn(actsIn, tomH, joeVersustheVolcano, new String[]{"Joe Banks"}, 1990); + saveActsIn(actsIn, megR, joeVersustheVolcano, + new String[]{"DeDe', 'Angelica Graynamore', 'Patricia Graynamore"}, 1990); + saveActsIn(actsIn, nathan, joeVersustheVolcano, new String[]{"Baw"}, 1990); + + final String whenHarryMetSally = saveMovie(movies, "WhenHarryMetSally", "When Harry Met Sally", 1998, + "At odds in life... in love on-line.").getId(); + final String billyC = saveActor(actors, "BillyC", "Billy Crystal", 1948).getId(); + final String carrieF = saveActor(actors, "CarrieF", "Carrie Fisher", 1956).getId(); + final String brunoK = saveActor(actors, "BrunoK", "Bruno Kirby", 1949).getId(); + saveActsIn(actsIn, billyC, whenHarryMetSally, new String[]{"Harry Burns"}, 1998); + saveActsIn(actsIn, megR, whenHarryMetSally, new String[]{"Sally Albright"}, 1998); + saveActsIn(actsIn, carrieF, whenHarryMetSally, new String[]{"Marie"}, 1998); + saveActsIn(actsIn, brunoK, whenHarryMetSally, new String[]{"Jess"}, 1998); + } } diff --git a/src/test/java/com/arangodb/example/graph/BaseGraphTest.java b/src/test/java/com/arangodb/example/graph/BaseGraphTest.java index 988a9d7..ea4e985 100644 --- a/src/test/java/com/arangodb/example/graph/BaseGraphTest.java +++ b/src/test/java/com/arangodb/example/graph/BaseGraphTest.java @@ -35,86 +35,80 @@ /** * @author Mark Vollmary - * */ public abstract class BaseGraphTest { - protected static final String TEST_DB = "java_driver_graph_test_db"; - protected static ArangoDBAsync arangoDB; - protected static ArangoDatabaseAsync db; - protected static final String GRAPH_NAME = "traversalGraph"; - protected static final String EDGE_COLLECTION_NAME = "edges"; - protected static final String VERTEXT_COLLECTION_NAME = "circles"; - - @BeforeClass - public static void init() throws InterruptedException, ExecutionException { - if (arangoDB == null) { - arangoDB = new ArangoDBAsync.Builder().build(); - } - try { - arangoDB.db(TEST_DB).drop().get(); - } catch (final Exception e) { - } - arangoDB.createDatabase(TEST_DB).get(); - BaseGraphTest.db = arangoDB.db(TEST_DB); - - final Collection edgeDefinitions = new ArrayList<>(); - final EdgeDefinition edgeDefinition = new EdgeDefinition().collection(EDGE_COLLECTION_NAME) - .from(VERTEXT_COLLECTION_NAME).to(VERTEXT_COLLECTION_NAME); - edgeDefinitions.add(edgeDefinition); - try { - db.createGraph(GRAPH_NAME, edgeDefinitions, null).get(); - addExampleElements(); - } catch (final Exception ex) { - - } - } - - @AfterClass - public static void shutdown() throws InterruptedException, ExecutionException { - arangoDB.db(TEST_DB).drop().get(); - arangoDB.shutdown(); - arangoDB = null; - } - - private static void addExampleElements() throws InterruptedException, ExecutionException { - - // Add circle circles - final VertexEntity vA = createVertex(new Circle("A", "1")); - final VertexEntity vB = createVertex(new Circle("B", "2")); - final VertexEntity vC = createVertex(new Circle("C", "3")); - final VertexEntity vD = createVertex(new Circle("D", "4")); - final VertexEntity vE = createVertex(new Circle("E", "5")); - final VertexEntity vF = createVertex(new Circle("F", "6")); - final VertexEntity vG = createVertex(new Circle("G", "7")); - final VertexEntity vH = createVertex(new Circle("H", "8")); - final VertexEntity vI = createVertex(new Circle("I", "9")); - final VertexEntity vJ = createVertex(new Circle("J", "10")); - final VertexEntity vK = createVertex(new Circle("K", "11")); - - // Add relevant edges - left branch: - saveEdge(new CircleEdge(vA.getId(), vB.getId(), false, true, "left_bar")); - saveEdge(new CircleEdge(vB.getId(), vC.getId(), false, true, "left_blarg")); - saveEdge(new CircleEdge(vC.getId(), vD.getId(), false, true, "left_blorg")); - saveEdge(new CircleEdge(vB.getId(), vE.getId(), false, true, "left_blub")); - saveEdge(new CircleEdge(vE.getId(), vF.getId(), false, true, "left_schubi")); - - // Add relevant edges - right branch: - saveEdge(new CircleEdge(vA.getId(), vG.getId(), false, true, "right_foo")); - saveEdge(new CircleEdge(vG.getId(), vH.getId(), false, true, "right_blob")); - saveEdge(new CircleEdge(vH.getId(), vI.getId(), false, true, "right_blub")); - saveEdge(new CircleEdge(vG.getId(), vJ.getId(), false, true, "right_zip")); - saveEdge(new CircleEdge(vJ.getId(), vK.getId(), false, true, "right_zup")); - } - - private static EdgeEntity saveEdge(final CircleEdge edge) - throws InterruptedException, ExecutionException { - return db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(edge).get(); - } - - private static VertexEntity createVertex(final Circle vertex) - throws InterruptedException, ExecutionException { - return db.graph(GRAPH_NAME).vertexCollection(VERTEXT_COLLECTION_NAME).insertVertex(vertex).get(); - } + protected static final String TEST_DB = "java_driver_graph_test_db"; + protected static ArangoDBAsync arangoDB; + protected static ArangoDatabaseAsync db; + protected static final String GRAPH_NAME = "traversalGraph"; + protected static final String EDGE_COLLECTION_NAME = "edges"; + protected static final String VERTEXT_COLLECTION_NAME = "circles"; + + @BeforeClass + public static void init() throws InterruptedException, ExecutionException { + if (arangoDB == null) { + arangoDB = new ArangoDBAsync.Builder().build(); + } + if (arangoDB.db(TEST_DB).exists().get()) { + arangoDB.db(TEST_DB).drop().get(); + } + arangoDB.createDatabase(TEST_DB).get(); + BaseGraphTest.db = arangoDB.db(TEST_DB); + + final Collection edgeDefinitions = new ArrayList<>(); + final EdgeDefinition edgeDefinition = new EdgeDefinition().collection(EDGE_COLLECTION_NAME) + .from(VERTEXT_COLLECTION_NAME).to(VERTEXT_COLLECTION_NAME); + edgeDefinitions.add(edgeDefinition); + db.createGraph(GRAPH_NAME, edgeDefinitions, null).get(); + addExampleElements(); + } + + @AfterClass + public static void shutdown() throws InterruptedException, ExecutionException { + arangoDB.db(TEST_DB).drop().get(); + arangoDB.shutdown(); + arangoDB = null; + } + + private static void addExampleElements() throws InterruptedException, ExecutionException { + + // Add circle circles + final VertexEntity vA = createVertex(new Circle("A", "1")); + final VertexEntity vB = createVertex(new Circle("B", "2")); + final VertexEntity vC = createVertex(new Circle("C", "3")); + final VertexEntity vD = createVertex(new Circle("D", "4")); + final VertexEntity vE = createVertex(new Circle("E", "5")); + final VertexEntity vF = createVertex(new Circle("F", "6")); + final VertexEntity vG = createVertex(new Circle("G", "7")); + final VertexEntity vH = createVertex(new Circle("H", "8")); + final VertexEntity vI = createVertex(new Circle("I", "9")); + final VertexEntity vJ = createVertex(new Circle("J", "10")); + final VertexEntity vK = createVertex(new Circle("K", "11")); + + // Add relevant edges - left branch: + saveEdge(new CircleEdge(vA.getId(), vB.getId(), false, true, "left_bar")); + saveEdge(new CircleEdge(vB.getId(), vC.getId(), false, true, "left_blarg")); + saveEdge(new CircleEdge(vC.getId(), vD.getId(), false, true, "left_blorg")); + saveEdge(new CircleEdge(vB.getId(), vE.getId(), false, true, "left_blub")); + saveEdge(new CircleEdge(vE.getId(), vF.getId(), false, true, "left_schubi")); + + // Add relevant edges - right branch: + saveEdge(new CircleEdge(vA.getId(), vG.getId(), false, true, "right_foo")); + saveEdge(new CircleEdge(vG.getId(), vH.getId(), false, true, "right_blob")); + saveEdge(new CircleEdge(vH.getId(), vI.getId(), false, true, "right_blub")); + saveEdge(new CircleEdge(vG.getId(), vJ.getId(), false, true, "right_zip")); + saveEdge(new CircleEdge(vJ.getId(), vK.getId(), false, true, "right_zup")); + } + + private static EdgeEntity saveEdge(final CircleEdge edge) + throws InterruptedException, ExecutionException { + return db.graph(GRAPH_NAME).edgeCollection(EDGE_COLLECTION_NAME).insertEdge(edge).get(); + } + + private static VertexEntity createVertex(final Circle vertex) + throws InterruptedException, ExecutionException { + return db.graph(GRAPH_NAME).vertexCollection(VERTEXT_COLLECTION_NAME).insertVertex(vertex).get(); + } }