|
| 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