Skip to content

Commit a032439

Browse files
author
Mark
committed
added examples
1 parent f9be048 commit a032439

File tree

2 files changed

+228
-0
lines changed

2 files changed

+228
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 java.util.Optional;
28+
29+
import org.junit.AfterClass;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
import com.arangodb.ArangoCollection;
34+
import com.arangodb.ArangoDB;
35+
import com.arangodb.ArangoDBException;
36+
import com.arangodb.ArangoDatabase;
37+
import com.arangodb.entity.BaseDocument;
38+
import com.arangodb.entity.DocumentCreateEntity;
39+
import com.arangodb.velocypack.VPackBuilder;
40+
import com.arangodb.velocypack.VPackSlice;
41+
import com.arangodb.velocypack.ValueType;
42+
import com.arangodb.velocypack.exception.VPackBuilderException;
43+
import com.arangodb.velocypack.exception.VPackException;
44+
45+
/**
46+
* @author Mark - mark at arangodb.com
47+
*
48+
*/
49+
public class DocumentExample {
50+
51+
private static final String DB_NAME = "json_document_example_db";
52+
private static final String COLLECTION_NAME = "json_document_example_collection";
53+
54+
private static ArangoDB arangoDB;
55+
private static ArangoDatabase db;
56+
private static ArangoCollection collection;
57+
58+
@BeforeClass
59+
public static void setUp() {
60+
arangoDB = new ArangoDB.Builder().build();
61+
try {
62+
arangoDB.db(DB_NAME).drop();
63+
} catch (final ArangoDBException e) {
64+
}
65+
arangoDB.createDatabase(DB_NAME);
66+
db = arangoDB.db(DB_NAME);
67+
db.createCollection(COLLECTION_NAME);
68+
collection = db.collection(COLLECTION_NAME);
69+
}
70+
71+
@AfterClass
72+
public static void tearDown() {
73+
db.drop();
74+
arangoDB.shutdown();
75+
}
76+
77+
@Test
78+
public void insertVPackGetVPack() throws VPackException {
79+
final VPackBuilder builder = new VPackBuilder();
80+
builder.add(ValueType.OBJECT).add("test", 123).close();
81+
final DocumentCreateEntity<VPackSlice> doc = collection.insertDocument(builder.slice());
82+
83+
final Optional<VPackSlice> vpack = collection.getDocument(doc.getKey(), VPackSlice.class);
84+
assertThat(vpack.isPresent(), is(true));
85+
final VPackSlice test = vpack.get().get("test");
86+
assertThat(test.isInt(), is(true));
87+
assertThat(test.getAsInt(), is(123));
88+
}
89+
90+
@Test
91+
public void insertJsonGetVPack() throws VPackException {
92+
final DocumentCreateEntity<String> doc = collection.insertDocument("{\"test\":123}");
93+
94+
final Optional<VPackSlice> vpack = collection.getDocument(doc.getKey(), VPackSlice.class);
95+
assertThat(vpack.isPresent(), is(true));
96+
final VPackSlice test = vpack.get().get("test");
97+
assertThat(test.isInt(), is(true));
98+
assertThat(test.getAsInt(), is(123));
99+
}
100+
101+
@Test
102+
public void insertVPackGetBaseDocument() throws VPackBuilderException {
103+
final VPackBuilder builder = new VPackBuilder();
104+
builder.add(ValueType.OBJECT).add("test", 123).close();
105+
final DocumentCreateEntity<VPackSlice> doc = collection.insertDocument(builder.slice());
106+
107+
final Optional<BaseDocument> document = collection.getDocument(doc.getKey(), BaseDocument.class);
108+
assertThat(document.isPresent(), is(true));
109+
assertThat(document.get().getAttribute("test"), is(notNullValue()));
110+
assertThat((long) document.get().getAttribute("test"), is(123L));
111+
}
112+
113+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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.velocypack;
22+
23+
import static org.hamcrest.Matchers.is;
24+
import static org.junit.Assert.assertThat;
25+
26+
import java.util.Iterator;
27+
import java.util.Map.Entry;
28+
29+
import org.junit.Test;
30+
31+
import com.arangodb.velocypack.VPackBuilder;
32+
import com.arangodb.velocypack.VPackSlice;
33+
import com.arangodb.velocypack.ValueType;
34+
import com.arangodb.velocypack.exception.VPackException;
35+
36+
/**
37+
* @author Mark - mark at arangodb.com
38+
*
39+
*/
40+
public class VPackExample {
41+
42+
@Test
43+
public void buildObject() throws VPackException {
44+
final VPackBuilder builder = new VPackBuilder();
45+
builder.add(ValueType.OBJECT);// object start
46+
builder.add("foo", 1); // add field "foo" with value 1
47+
builder.add("bar", 2); // add field "bar" with value 2
48+
builder.close();// object end
49+
50+
final VPackSlice slice = builder.slice(); // create slice
51+
assertThat(slice.isObject(), is(true));
52+
assertThat(slice.size(), is(2)); // number of fields
53+
54+
final VPackSlice foo = slice.get("foo"); // get field "foo"
55+
assertThat(foo.isInteger(), is(true));
56+
assertThat(foo.getAsInt(), is(1));
57+
58+
final VPackSlice bar = slice.get("bar"); // get field "bar"
59+
assertThat(bar.isInteger(), is(true));
60+
assertThat(bar.getAsInt(), is(2));
61+
62+
// iterate over the fields
63+
for (final Iterator<Entry<String, VPackSlice>> iterator = slice.objectIterator(); iterator.hasNext();) {
64+
final Entry<String, VPackSlice> field = iterator.next();
65+
assertThat(field.getValue().isInteger(), is(true));
66+
}
67+
}
68+
69+
@Test
70+
public void buildArray() throws VPackException {
71+
final VPackBuilder builder = new VPackBuilder();
72+
builder.add(ValueType.ARRAY); // array start
73+
builder.add(1);// add value 1
74+
builder.add(2);// add value 2
75+
builder.add(3);// add value 3
76+
builder.close(); // array end
77+
78+
final VPackSlice slice = builder.slice();// create slice
79+
assertThat(slice.isArray(), is(true));
80+
assertThat(slice.size(), is(3));// number of values
81+
82+
// iterate over values
83+
for (int i = 0; i < slice.size(); i++) {
84+
final VPackSlice value = slice.get(i);
85+
assertThat(value.isInteger(), is(true));
86+
assertThat(value.getAsInt(), is(i + 1));
87+
}
88+
89+
// iterate over values with Iterator
90+
for (final Iterator<VPackSlice> iterator = slice.arrayIterator(); iterator.hasNext();) {
91+
final VPackSlice value = iterator.next();
92+
assertThat(value.isInteger(), is(true));
93+
}
94+
}
95+
96+
@Test
97+
public void buildObjectInObject() throws VPackException {
98+
final VPackBuilder builder = new VPackBuilder();
99+
builder.add(ValueType.OBJECT);// object start
100+
builder.add("foo", ValueType.OBJECT); // add object in field "foo"
101+
builder.add("bar", 2); // add field "bar" with value 2 to object "foo"
102+
builder.close();// object "foo" end
103+
builder.close();// object end
104+
105+
final VPackSlice slice = builder.slice(); // create slice
106+
assertThat(slice.isObject(), is(true));
107+
108+
final VPackSlice foo = slice.get("foo");
109+
assertThat(foo.isObject(), is(true));
110+
111+
final VPackSlice bar = foo.get("bar"); // get field "bar" from "foo"
112+
assertThat(bar.isInteger(), is(true));
113+
}
114+
115+
}

0 commit comments

Comments
 (0)