Skip to content

Commit 67e2ad0

Browse files
author
a-brandt
committed
added examples for graphs
1 parent cc96ee2 commit 67e2ad0

8 files changed

+553
-6
lines changed

src/test/java/com/arangodb/example/graph/CreateGraphExample.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@
3232
import com.arangodb.entity.GraphEntity;
3333

3434
/**
35-
* AQL example with new cursor implementation
35+
* Create graph example
3636
*
37-
* @author tamtam180 - kirscheless at gmail.com
3837
* @author a-brandt
3938
*
4039
*/

src/test/java/com/arangodb/example/graph/CreateVerticesAndEdgesExample.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
import com.arangodb.entity.marker.VertexEntity;
2727

2828
/**
29-
* AQL example with new cursor implementation
29+
* AQL example creating vertices and edges
3030
*
31-
* @author tamtam180 - kirscheless at gmail.com
3231
* @author a-brandt
3332
*
3433
*/
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.arangodb.example.graph;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import com.arangodb.ArangoDriver;
8+
import com.arangodb.ArangoException;
9+
import com.arangodb.ErrorNums;
10+
import com.arangodb.entity.DeletedEntity;
11+
import com.arangodb.entity.EdgeEntity;
12+
import com.arangodb.entity.marker.VertexEntity;
13+
14+
/**
15+
* Delete vertices and edges example
16+
*
17+
* @author a-brandt
18+
*
19+
*/
20+
public class DeleteVerticesAndEdgesExample extends BaseExample {
21+
22+
private static final String DATABASE_NAME = "DeleteVerticesAndEdgesExample";
23+
24+
private static final String GRAPH_NAME = "example_graph1";
25+
private static final String EDGE_COLLECTION_NAME = "edgeColl1";
26+
private static final String VERTEXT_COLLECTION_NAME = "vertexColl1";
27+
28+
public ArangoDriver arangoDriver;
29+
30+
@Before
31+
public void _before() throws ArangoException {
32+
removeTestDatabase(DATABASE_NAME);
33+
34+
arangoDriver = getArangoDriver(getConfiguration());
35+
createDatabase(arangoDriver, DATABASE_NAME);
36+
createGraph(arangoDriver, GRAPH_NAME, EDGE_COLLECTION_NAME, VERTEXT_COLLECTION_NAME);
37+
}
38+
39+
@Test
40+
public void deleteVertex() throws ArangoException {
41+
42+
//
43+
printHeadline("delete vertex");
44+
//
45+
46+
VertexEntity<Person> v1 = createVertex(new Person("A", Person.MALE));
47+
48+
DeletedEntity deletedEntity = arangoDriver.graphDeleteVertex(GRAPH_NAME, VERTEXT_COLLECTION_NAME,
49+
v1.getDocumentKey());
50+
Assert.assertNotNull(deletedEntity);
51+
Assert.assertTrue(deletedEntity.getDeleted());
52+
53+
try {
54+
arangoDriver.graphGetVertex(GRAPH_NAME, VERTEXT_COLLECTION_NAME, v1.getDocumentKey(), Person.class);
55+
Assert.fail("graphGetVertex should fail");
56+
} catch (ArangoException ex) {
57+
Assert.assertEquals(ErrorNums.ERROR_ARANGO_DOCUMENT_NOT_FOUND, ex.getErrorNumber());
58+
}
59+
}
60+
61+
@Test
62+
public void getEdge() throws ArangoException {
63+
64+
//
65+
printHeadline("delete edge");
66+
//
67+
68+
VertexEntity<Person> v1 = createVertex(new Person("A", Person.MALE));
69+
VertexEntity<Person> v2 = createVertex(new Person("B", Person.FEMALE));
70+
EdgeEntity<Knows> e1 = createEdge(v1, v2, new Knows(1984));
71+
72+
DeletedEntity deletedEntity = arangoDriver.graphDeleteEdge(GRAPH_NAME, EDGE_COLLECTION_NAME,
73+
e1.getDocumentKey());
74+
Assert.assertNotNull(deletedEntity);
75+
Assert.assertTrue(deletedEntity.getDeleted());
76+
77+
try {
78+
arangoDriver.graphGetEdge(GRAPH_NAME, EDGE_COLLECTION_NAME, e1.getDocumentKey(), Knows.class);
79+
Assert.fail("graphGetEdge should fail");
80+
} catch (ArangoException ex) {
81+
Assert.assertEquals(ErrorNums.ERROR_ARANGO_DOCUMENT_NOT_FOUND, ex.getErrorNumber());
82+
}
83+
}
84+
85+
private VertexEntity<Person> createVertex(Person person) throws ArangoException {
86+
VertexEntity<Person> v = arangoDriver.graphCreateVertex(GRAPH_NAME, VERTEXT_COLLECTION_NAME, person, true);
87+
Assert.assertNotNull(v);
88+
return v;
89+
}
90+
91+
private EdgeEntity<Knows> createEdge(VertexEntity<Person> personFrom, VertexEntity<Person> personTo, Knows knows)
92+
throws ArangoException {
93+
EdgeEntity<Knows> e = arangoDriver.graphCreateEdge(GRAPH_NAME, EDGE_COLLECTION_NAME,
94+
personFrom.getDocumentHandle(), personTo.getDocumentHandle(), knows, false);
95+
Assert.assertNotNull(e);
96+
return e;
97+
}
98+
99+
}

0 commit comments

Comments
 (0)