Skip to content

Commit 1d8330f

Browse files
author
a-brandt
committed
added more tests
1 parent 5e0f5e0 commit 1d8330f

13 files changed

+1092
-75
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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.document;
18+
19+
import java.util.HashMap;
20+
import java.util.Iterator;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import org.junit.Assert;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
import com.arangodb.ArangoDriver;
29+
import com.arangodb.ArangoException;
30+
import com.arangodb.CursorResult;
31+
import com.arangodb.util.AqlQueryOptions;
32+
33+
public class AqlQueryWithSpecialReturnTypesExample extends BaseExample {
34+
35+
private static final String DATABASE_NAME = "SimplePersonAqlQueryWithLimitExample";
36+
37+
private static final String COLLECTION_NAME = "SimplePersonAqlQueryWithLimitExample";
38+
39+
public ArangoDriver arangoDriver;
40+
41+
@Before
42+
public void _before() {
43+
removeTestDatabase(DATABASE_NAME);
44+
45+
arangoDriver = getArangoDriver(getConfiguration());
46+
createDatabase(arangoDriver, DATABASE_NAME);
47+
createCollection(arangoDriver, COLLECTION_NAME);
48+
}
49+
50+
@Test
51+
public void simplePersonAqlWithLimitQuery() {
52+
//
53+
// You can find the ArangoDB Web interface here:
54+
// http://127.0.0.1:8529/
55+
//
56+
// change the log level to "debug" in /src/test/resource/logback.xml to
57+
// see the HTTP communication
58+
59+
try {
60+
//
61+
printHeadline("create example documents");
62+
//
63+
createExamples();
64+
65+
//
66+
printHeadline("build query");
67+
//
68+
69+
// bind @gender to WOMAN
70+
HashMap<String, Object> bindVars = new HashMap<String, Object>();
71+
bindVars.put("gender", FEMALE);
72+
73+
// query (count = true, batchSize = 5)
74+
AqlQueryOptions aqlQueryOptions = new AqlQueryOptions().setCount(true).setBatchSize(5);
75+
76+
//
77+
printHeadline("get query results in a map");
78+
//
79+
80+
String queryString = "FOR t IN " + COLLECTION_NAME
81+
+ " FILTER t.age >= 20 && t.age < 30 && t.gender == @gender RETURN t";
82+
System.out.println(queryString);
83+
84+
@SuppressWarnings("rawtypes")
85+
CursorResult<Map> cursor = arangoDriver.executeAqlQuery(queryString, bindVars, aqlQueryOptions, Map.class);
86+
Assert.assertNotNull(cursor);
87+
88+
@SuppressWarnings("rawtypes")
89+
Iterator<Map> iterator = cursor.iterator();
90+
while (iterator.hasNext()) {
91+
Map<?, ?> map = iterator.next();
92+
93+
Assert.assertNotNull(map);
94+
Assert.assertNotNull(map.get("name"));
95+
Assert.assertNotNull(map.get("gender"));
96+
Assert.assertNotNull(map.get("age"));
97+
98+
System.out.printf("%15s (%5s): %s%n", map.get("name"), map.get("gender"), map.get("age").toString());
99+
}
100+
101+
//
102+
printHeadline("get query results in a list");
103+
//
104+
105+
queryString = "FOR t IN " + COLLECTION_NAME
106+
+ " FILTER t.age >= 20 && t.age < 30 && t.gender == @gender RETURN [t.name, t.gender, t.age]";
107+
System.out.println(queryString);
108+
109+
@SuppressWarnings("rawtypes")
110+
CursorResult<List> cursor2 = arangoDriver.executeAqlQuery(queryString, bindVars, aqlQueryOptions,
111+
List.class);
112+
Assert.assertNotNull(cursor2);
113+
114+
@SuppressWarnings("rawtypes")
115+
Iterator<List> iterator2 = cursor2.iterator();
116+
while (iterator2.hasNext()) {
117+
List<?> list = iterator2.next();
118+
119+
Assert.assertNotNull(list);
120+
Assert.assertNotNull(list.get(0));
121+
Assert.assertNotNull(list.get(1));
122+
Assert.assertNotNull(list.get(2));
123+
124+
System.out.printf("%15s (%5s): %s%n", list.get(0), list.get(1), list.get(2).toString());
125+
}
126+
127+
} catch (ArangoException e) {
128+
Assert.fail("Example failed. " + e.getMessage());
129+
}
130+
131+
}
132+
133+
private void createExamples() throws ArangoException {
134+
// create some persons
135+
for (int i = 0; i < 100; i++) {
136+
SimplePerson person = new SimplePerson();
137+
person.setName("TestUser" + i);
138+
person.setGender((i % 2) == 0 ? MALE : FEMALE);
139+
person.setAge((int) (Math.random() * 100) + 10);
140+
141+
arangoDriver.createDocument(COLLECTION_NAME, person, true, null);
142+
}
143+
144+
}
145+
146+
}

β€Žsrc/test/java/com/arangodb/example/document/BaseExample.java

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,97 @@
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+
117
package com.arangodb.example.document;
218

19+
import org.junit.Assert;
20+
321
import com.arangodb.ArangoConfigure;
422
import com.arangodb.ArangoDriver;
523
import com.arangodb.ArangoException;
24+
import com.arangodb.entity.BooleanResultEntity;
625
import com.arangodb.entity.CollectionEntity;
726

827
public class BaseExample {
928

10-
protected static ArangoDriver createDatabase(String name) {
29+
protected static final String FEMALE = "female";
30+
31+
protected static final String MALE = "male";
1132

33+
protected ArangoConfigure getConfiguration() {
1234
ArangoConfigure configure = new ArangoConfigure();
1335
// configure.setUser("myUser");
1436
// configure.setPassword("password");
1537
// configuration file: src/test/resources/arangodb.properties
1638
configure.init();
1739

18-
ArangoDriver arangoDriver = new ArangoDriver(configure);
40+
return configure;
41+
}
42+
43+
protected ArangoDriver getArangoDriver(ArangoConfigure configuration) {
44+
return new ArangoDriver(configuration);
45+
}
1946

20-
deleteDatabase(arangoDriver, name);
47+
protected void removeTestDatabase(String name) {
48+
ArangoDriver arangoDriver = getArangoDriver(getConfiguration());
49+
try {
50+
arangoDriver.deleteDatabase(name);
51+
} catch (Exception e) {
52+
}
53+
}
2154

55+
protected void createDatabase(ArangoDriver arangoDriver, String name) {
2256
try {
23-
arangoDriver.createDatabase(name);
24-
System.out.println("Database created: " + name);
57+
BooleanResultEntity createDatabase = arangoDriver.createDatabase(name);
58+
Assert.assertNotNull(createDatabase);
59+
Assert.assertNotNull(createDatabase.getResult());
60+
Assert.assertTrue(createDatabase.getResult());
2561
} catch (Exception e) {
26-
System.out.println("Failed to create database " + name + "; " + e.getMessage());
62+
Assert.fail("Failed to create database " + name + "; " + e.getMessage());
2763
}
2864

2965
arangoDriver.setDefaultDatabase(name);
30-
31-
return arangoDriver;
3266
}
3367

34-
protected static void deleteDatabase(ArangoDriver arangoDriver, String name) {
68+
protected void deleteDatabase(ArangoDriver arangoDriver, String name) {
3569
try {
3670
arangoDriver.deleteDatabase(name);
3771
} catch (Exception e) {
3872
}
3973
}
4074

41-
protected static void createCollection(ArangoDriver arangoDriver, String name) {
75+
protected void createCollection(ArangoDriver arangoDriver, String name) {
4276
try {
4377
CollectionEntity createCollection = arangoDriver.createCollection(name);
44-
System.out.println(
45-
"created collection '" + createCollection.getName() + "' with id '" + createCollection.getId() + "'");
78+
Assert.assertNotNull(createCollection);
79+
Assert.assertNotNull(createCollection.getName());
80+
Assert.assertEquals(name, createCollection.getName());
4681
} catch (ArangoException e) {
47-
System.err.println(e.getMessage());
82+
Assert.fail("create collection failed. " + e.getMessage());
4883
}
4984
}
5085

51-
protected static void printEntity(Object object) {
86+
protected void printEntity(Object object) {
5287
if (object == null) {
5388
System.out.println("Document not found");
5489
} else {
5590
System.out.println(object);
5691
}
5792
}
5893

59-
protected static void printHeadline(String name) {
94+
protected void printHeadline(String name) {
6095
System.out.println("---------------------------------------------");
6196
System.out.println(name);
6297
System.out.println("---------------------------------------------");

0 commit comments

Comments
 (0)