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