Skip to content

Commit 2a06d44

Browse files
author
a-brandt
committed
added graph examples
1 parent f98d86d commit 2a06d44

File tree

8 files changed

+872
-0
lines changed

8 files changed

+872
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2015 ArangoDB GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.arangodb.example;
18+
19+
import org.junit.runner.RunWith;
20+
import org.junit.runners.Suite;
21+
import org.junit.runners.Suite.SuiteClasses;
22+
23+
import com.arangodb.example.document.DocumentExamplesTestSuite;
24+
import com.arangodb.example.graph.GraphExamplesTestSuite;
25+
26+
/**
27+
* Starts all unit tests
28+
*
29+
* @author a-brandt
30+
*
31+
*/
32+
@RunWith(Suite.class)
33+
@SuiteClasses({
34+
35+
DocumentExamplesTestSuite.class,
36+
37+
GraphExamplesTestSuite.class
38+
39+
})
40+
41+
public class ExamplesTestSuite {
42+
43+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (C) 2015 ArangoDB GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.arangodb.example.graph;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.junit.Assert;
23+
24+
import com.arangodb.ArangoConfigure;
25+
import com.arangodb.ArangoDriver;
26+
import com.arangodb.ArangoException;
27+
import com.arangodb.entity.BooleanResultEntity;
28+
import com.arangodb.entity.CollectionEntity;
29+
import com.arangodb.entity.CollectionOptions;
30+
import com.arangodb.entity.CollectionType;
31+
import com.arangodb.entity.EdgeDefinitionEntity;
32+
import com.arangodb.entity.GraphEntity;
33+
34+
public class BaseExample {
35+
36+
protected ArangoConfigure getConfiguration() {
37+
ArangoConfigure configure = new ArangoConfigure();
38+
// configure.setUser("myUser");
39+
// configure.setPassword("password");
40+
// configuration file: src/test/resources/arangodb.properties
41+
configure.init();
42+
43+
return configure;
44+
}
45+
46+
protected ArangoDriver getArangoDriver(ArangoConfigure configuration) {
47+
return new ArangoDriver(configuration);
48+
}
49+
50+
protected void removeTestDatabase(String name) {
51+
ArangoDriver arangoDriver = getArangoDriver(getConfiguration());
52+
try {
53+
arangoDriver.deleteDatabase(name);
54+
} catch (Exception e) {
55+
}
56+
}
57+
58+
protected void createDatabase(ArangoDriver arangoDriver, String name) {
59+
try {
60+
BooleanResultEntity createDatabase = arangoDriver.createDatabase(name);
61+
Assert.assertNotNull(createDatabase);
62+
Assert.assertNotNull(createDatabase.getResult());
63+
Assert.assertTrue(createDatabase.getResult());
64+
} catch (Exception e) {
65+
Assert.fail("Failed to create database " + name + "; " + e.getMessage());
66+
}
67+
68+
arangoDriver.setDefaultDatabase(name);
69+
}
70+
71+
protected void deleteDatabase(ArangoDriver arangoDriver, String name) {
72+
try {
73+
arangoDriver.deleteDatabase(name);
74+
} catch (Exception e) {
75+
}
76+
}
77+
78+
protected void createCollection(ArangoDriver arangoDriver, String name) {
79+
try {
80+
CollectionEntity createCollection = arangoDriver.createCollection(name);
81+
Assert.assertNotNull(createCollection);
82+
Assert.assertNotNull(createCollection.getName());
83+
Assert.assertEquals(name, createCollection.getName());
84+
} catch (ArangoException e) {
85+
Assert.fail("create collection failed. " + e.getMessage());
86+
}
87+
}
88+
89+
protected void printEntity(Object object) {
90+
if (object == null) {
91+
System.out.println("Document not found");
92+
} else {
93+
System.out.println(object);
94+
}
95+
}
96+
97+
protected void printHeadline(String name) {
98+
System.out.println("---------------------------------------------");
99+
System.out.println(name);
100+
System.out.println("---------------------------------------------");
101+
}
102+
103+
public void createGraph(
104+
ArangoDriver arangoDriver,
105+
String grapName,
106+
String nameEdgeCollection,
107+
String nameVertexCollection) throws ArangoException {
108+
109+
//
110+
printHeadline("create edge collection");
111+
//
112+
113+
CollectionEntity createCollection = arangoDriver.createCollection(nameEdgeCollection,
114+
new CollectionOptions().setType(CollectionType.EDGE));
115+
Assert.assertNotNull(createCollection);
116+
Assert.assertNotNull(createCollection.getId());
117+
Assert.assertTrue(createCollection.getId() > 0L);
118+
119+
//
120+
printHeadline("create vertex collection");
121+
//
122+
123+
createCollection = arangoDriver.createCollection(nameVertexCollection,
124+
new CollectionOptions().setType(CollectionType.DOCUMENT));
125+
Assert.assertNotNull(createCollection);
126+
Assert.assertNotNull(createCollection.getId());
127+
Assert.assertTrue(createCollection.getId() > 0L);
128+
129+
//
130+
printHeadline("create edge definition");
131+
//
132+
133+
EdgeDefinitionEntity ed = new EdgeDefinitionEntity();
134+
// add edge collection name
135+
ed.setCollection(nameEdgeCollection);
136+
137+
// add vertex collection names
138+
ed.getFrom().add(nameVertexCollection);
139+
140+
// add vertex collection names
141+
ed.getTo().add(nameVertexCollection);
142+
143+
//
144+
printHeadline("create edge definition list");
145+
//
146+
List<EdgeDefinitionEntity> edgeDefinitions = new ArrayList<EdgeDefinitionEntity>();
147+
edgeDefinitions.add(ed);
148+
149+
//
150+
printHeadline("create graph");
151+
//
152+
GraphEntity createGraph = arangoDriver.createGraph(grapName, edgeDefinitions, null, true);
153+
Assert.assertNotNull(createGraph);
154+
Assert.assertEquals(grapName, createGraph.getName());
155+
}
156+
157+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (C) 2015 ArangoDB GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.arangodb.example.graph;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.junit.Assert;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
26+
import com.arangodb.ArangoDriver;
27+
import com.arangodb.ArangoException;
28+
import com.arangodb.entity.CollectionEntity;
29+
import com.arangodb.entity.CollectionOptions;
30+
import com.arangodb.entity.CollectionType;
31+
import com.arangodb.entity.EdgeDefinitionEntity;
32+
import com.arangodb.entity.GraphEntity;
33+
34+
/**
35+
* AQL example with new cursor implementation
36+
*
37+
* @author tamtam180 - kirscheless at gmail.com
38+
* @author a-brandt
39+
*
40+
*/
41+
public class CreateGraphExample extends BaseExample {
42+
43+
private static final String DATABASE_NAME = "CreateGraphExample";
44+
45+
private static final String GRAPH_NAME = "example_graph1";
46+
private static final String EDGE_COLLECTION_NAME = "edgeColl1";
47+
private static final String VERTEXT_COLLECTION_NAME = "vertexColl1";
48+
49+
public ArangoDriver arangoDriver;
50+
51+
@Before
52+
public void _before() {
53+
removeTestDatabase(DATABASE_NAME);
54+
55+
arangoDriver = getArangoDriver(getConfiguration());
56+
createDatabase(arangoDriver, DATABASE_NAME);
57+
}
58+
59+
@Test
60+
public void createGraph() throws ArangoException {
61+
62+
//
63+
printHeadline("create edge collection");
64+
//
65+
66+
CollectionEntity createCollection = arangoDriver.createCollection(EDGE_COLLECTION_NAME,
67+
new CollectionOptions().setType(CollectionType.EDGE));
68+
Assert.assertNotNull(createCollection);
69+
Assert.assertNotNull(createCollection.getId());
70+
Assert.assertTrue(createCollection.getId() > 0L);
71+
72+
//
73+
printHeadline("create vertex collection");
74+
//
75+
76+
createCollection = arangoDriver.createCollection(VERTEXT_COLLECTION_NAME,
77+
new CollectionOptions().setType(CollectionType.DOCUMENT));
78+
Assert.assertNotNull(createCollection);
79+
Assert.assertNotNull(createCollection.getId());
80+
Assert.assertTrue(createCollection.getId() > 0L);
81+
82+
//
83+
printHeadline("create edge definition");
84+
//
85+
86+
EdgeDefinitionEntity ed = new EdgeDefinitionEntity();
87+
// add edge collection name
88+
ed.setCollection(EDGE_COLLECTION_NAME);
89+
90+
// add vertex collection names
91+
ed.getFrom().add(VERTEXT_COLLECTION_NAME);
92+
93+
// add vertex collection names
94+
ed.getTo().add(VERTEXT_COLLECTION_NAME);
95+
96+
//
97+
printHeadline("create edge definition list");
98+
//
99+
List<EdgeDefinitionEntity> edgeDefinitions = new ArrayList<EdgeDefinitionEntity>();
100+
edgeDefinitions.add(ed);
101+
102+
//
103+
printHeadline("create graph");
104+
//
105+
GraphEntity createGraph = arangoDriver.createGraph(GRAPH_NAME, edgeDefinitions, null, true);
106+
Assert.assertNotNull(createGraph);
107+
Assert.assertEquals(GRAPH_NAME, createGraph.getName());
108+
}
109+
110+
}

0 commit comments

Comments
 (0)