Skip to content

Commit 812a57c

Browse files
author
Mark
committed
examples
1 parent d000685 commit 812a57c

File tree

7 files changed

+312
-139
lines changed

7 files changed

+312
-139
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.example;
22+
23+
import org.junit.AfterClass;
24+
import org.junit.BeforeClass;
25+
26+
import com.arangodb.ArangoCollection;
27+
import com.arangodb.ArangoDB;
28+
import com.arangodb.ArangoDBException;
29+
import com.arangodb.ArangoDatabase;
30+
31+
/**
32+
* @author Mark - mark at arangodb.com
33+
*
34+
*/
35+
public class ExampleBase {
36+
37+
protected static final String DB_NAME = "json_example_db";
38+
protected static final String COLLECTION_NAME = "json_example_collection";
39+
40+
protected static ArangoDB arangoDB;
41+
protected static ArangoDatabase db;
42+
protected static ArangoCollection collection;
43+
44+
@BeforeClass
45+
public static void setUp() {
46+
arangoDB = new ArangoDB.Builder().build();
47+
try {
48+
arangoDB.db(DB_NAME).drop();
49+
} catch (final ArangoDBException e) {
50+
}
51+
arangoDB.createDatabase(DB_NAME);
52+
db = arangoDB.db(DB_NAME);
53+
db.createCollection(COLLECTION_NAME);
54+
collection = db.collection(COLLECTION_NAME);
55+
}
56+
57+
@AfterClass
58+
public static void tearDown() {
59+
db.drop();
60+
arangoDB.shutdown();
61+
}
62+
63+
}

src/test/java/com/arangodb/example/document/AqlQueryWithSpecialReturnTypesExample.java

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@
2828
import java.util.List;
2929
import java.util.Map;
3030

31-
import org.junit.AfterClass;
3231
import org.junit.BeforeClass;
3332
import org.junit.Test;
3433

3534
import com.arangodb.ArangoCursor;
36-
import com.arangodb.ArangoDB;
37-
import com.arangodb.ArangoDBException;
38-
import com.arangodb.ArangoDatabase;
3935
import com.arangodb.entity.BaseDocument;
36+
import com.arangodb.example.ExampleBase;
4037
import com.arangodb.util.MapBuilder;
4138
import com.arangodb.velocypack.VPackSlice;
4239
import com.arangodb.velocypack.exception.VPackException;
@@ -45,33 +42,13 @@
4542
* @author Mark - mark at arangodb.com
4643
*
4744
*/
48-
public class AqlQueryWithSpecialReturnTypesExample {
49-
50-
private static final String DB_NAME = "document_example_db";
51-
private static final String COLLECTION_NAME = "document_example_collection";
52-
53-
private static ArangoDB arangoDB;
54-
private static ArangoDatabase db;
45+
public class AqlQueryWithSpecialReturnTypesExample extends ExampleBase {
5546

5647
@BeforeClass
57-
public static void setUp() {
58-
arangoDB = new ArangoDB.Builder().build();
59-
try {
60-
arangoDB.db(DB_NAME).drop();
61-
} catch (final ArangoDBException e) {
62-
}
63-
arangoDB.createDatabase(DB_NAME);
64-
db = arangoDB.db(DB_NAME);
65-
db.createCollection(COLLECTION_NAME);
48+
public static void before() {
6649
createExamples();
6750
}
6851

69-
@AfterClass
70-
public static void tearDown() {
71-
db.drop();
72-
arangoDB.shutdown();
73-
}
74-
7552
public enum Gender {
7653
MALE, FEMALE
7754
}

src/test/java/com/arangodb/example/document/DocumentExample.java

Lines changed: 0 additions & 113 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.example.document;
22+
23+
import static org.hamcrest.Matchers.is;
24+
import static org.junit.Assert.assertThat;
25+
26+
import java.util.Optional;
27+
28+
import org.json.simple.parser.ParseException;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
32+
import com.arangodb.entity.BaseDocument;
33+
import com.arangodb.entity.DocumentCreateEntity;
34+
import com.arangodb.example.ExampleBase;
35+
import com.arangodb.velocypack.VPackSlice;
36+
37+
/**
38+
* @author Mark - mark at arangodb.com
39+
*
40+
*/
41+
public class GetDocumentExample extends ExampleBase {
42+
43+
private static String key = null;
44+
45+
@BeforeClass
46+
public static void before() {
47+
final BaseDocument value = new BaseDocument();
48+
value.addAttribute("foo", "bar");
49+
final DocumentCreateEntity<BaseDocument> doc = collection.insertDocument(value);
50+
key = doc.getKey();
51+
}
52+
53+
@Test
54+
public void getAsBean() {
55+
final Optional<TestEntity> doc = collection.getDocument(key, TestEntity.class);
56+
assertThat(doc.isPresent(), is(true));
57+
assertThat(doc.get().getFoo(), is("bar"));
58+
}
59+
60+
@Test
61+
public void getAsBaseDocument() {
62+
final Optional<BaseDocument> doc = collection.getDocument(key, BaseDocument.class);
63+
assertThat(doc.isPresent(), is(true));
64+
assertThat(doc.get().getAttribute("foo"), is("bar"));
65+
}
66+
67+
@Test
68+
public void getAsVPack() {
69+
final Optional<VPackSlice> doc = collection.getDocument(key, VPackSlice.class);
70+
assertThat(doc.isPresent(), is(true));
71+
assertThat(doc.get().get("foo").isString(), is(true));
72+
assertThat(doc.get().get("foo").getAsString(), is("bar"));
73+
}
74+
75+
@Test
76+
public void getAsJson() throws ParseException {
77+
final Optional<String> doc = collection.getDocument(key, String.class);
78+
assertThat(doc.isPresent(), is(true));
79+
assertThat(doc.get().contains("foo"), is(true));
80+
assertThat(doc.get().contains("bar"), is(true));
81+
}
82+
83+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.example.document;
22+
23+
import static org.hamcrest.Matchers.is;
24+
import static org.hamcrest.Matchers.notNullValue;
25+
import static org.junit.Assert.assertThat;
26+
27+
import org.junit.Test;
28+
29+
import com.arangodb.entity.BaseDocument;
30+
import com.arangodb.entity.DocumentCreateEntity;
31+
import com.arangodb.example.ExampleBase;
32+
import com.arangodb.velocypack.VPackBuilder;
33+
import com.arangodb.velocypack.VPackSlice;
34+
import com.arangodb.velocypack.ValueType;
35+
36+
/**
37+
* @author Mark - mark at arangodb.com
38+
*
39+
*/
40+
public class InsertDocumentExample extends ExampleBase {
41+
42+
@Test
43+
public void insertBean() {
44+
final DocumentCreateEntity<TestEntity> doc = collection.insertDocument(new TestEntity("bar"));
45+
assertThat(doc.getKey(), is(notNullValue()));
46+
}
47+
48+
@Test
49+
public void insertBaseDocument() {
50+
final BaseDocument value = new BaseDocument();
51+
value.addAttribute("foo", "bar");
52+
final DocumentCreateEntity<BaseDocument> doc = collection.insertDocument(value);
53+
assertThat(doc.getKey(), is(notNullValue()));
54+
}
55+
56+
@Test
57+
public void insertVPack() {
58+
final VPackBuilder builder = new VPackBuilder();
59+
builder.add(ValueType.OBJECT).add("foo", "bar").close();
60+
final DocumentCreateEntity<VPackSlice> doc = collection.insertDocument(builder.slice());
61+
assertThat(doc.getKey(), is(notNullValue()));
62+
}
63+
64+
@Test
65+
public void insertJson() {
66+
final DocumentCreateEntity<String> doc = collection.insertDocument("{\"foo\":\"bar\"}");
67+
assertThat(doc.getKey(), is(notNullValue()));
68+
}
69+
70+
}

0 commit comments

Comments
 (0)