|
| 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.util; |
| 22 | + |
| 23 | +import static org.hamcrest.Matchers.is; |
| 24 | +import static org.junit.Assert.assertThat; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Collection; |
| 28 | + |
| 29 | +import org.junit.BeforeClass; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import com.arangodb.ArangoDB; |
| 33 | +import com.arangodb.entity.BaseDocument; |
| 34 | +import com.arangodb.velocypack.Type; |
| 35 | +import com.arangodb.velocypack.VPackBuilder; |
| 36 | +import com.arangodb.velocypack.VPackSlice; |
| 37 | +import com.arangodb.velocypack.ValueType; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author Mark - mark at arangodb.com |
| 41 | + * |
| 42 | + */ |
| 43 | +public class ArangoUtilTest { |
| 44 | + |
| 45 | + private static ArangoUtil util; |
| 46 | + |
| 47 | + @BeforeClass |
| 48 | + public static void setup() { |
| 49 | + final ArangoDB arangoDB = new ArangoDB.Builder().build(); |
| 50 | + util = arangoDB.util(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void deseriarlize() { |
| 55 | + final VPackBuilder builder = new VPackBuilder().add(ValueType.OBJECT).add("foo", "bar").close(); |
| 56 | + final BaseDocument doc = util.deserialize(builder.slice(), BaseDocument.class); |
| 57 | + assertThat(doc.getAttribute("foo").toString(), is("bar")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void serialize() { |
| 62 | + final BaseDocument entity = new BaseDocument(); |
| 63 | + entity.addAttribute("foo", "bar"); |
| 64 | + final VPackSlice vpack = util.serialize(entity); |
| 65 | + assertThat(vpack.get("foo").isString(), is(true)); |
| 66 | + assertThat(vpack.get("foo").getAsString(), is("bar")); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void serializeNullValues() { |
| 71 | + final BaseDocument entity = new BaseDocument(); |
| 72 | + entity.addAttribute("foo", null); |
| 73 | + final VPackSlice vpack = util.serialize(entity, true); |
| 74 | + assertThat(vpack.get("foo").isNull(), is(true)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void serializeType() { |
| 79 | + final Collection<BaseDocument> list = new ArrayList<BaseDocument>(); |
| 80 | + list.add(new BaseDocument()); |
| 81 | + list.add(new BaseDocument()); |
| 82 | + |
| 83 | + final VPackSlice vpack = util.serialize(list, new Type<Collection<BaseDocument>>() { |
| 84 | + }.getType()); |
| 85 | + assertThat(vpack.isArray(), is(true)); |
| 86 | + assertThat(vpack.getLength(), is(list.size())); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments