Skip to content

Commit 6c275bd

Browse files
author
michele
committed
CustomTypeHintTest
1 parent e11dd74 commit 6c275bd

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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.serde;
22+
23+
24+
import com.arangodb.ArangoCollection;
25+
import com.arangodb.ArangoDB;
26+
import com.arangodb.ArangoDatabase;
27+
import com.arangodb.VelocyJack;
28+
import com.arangodb.model.DocumentCreateOptions;
29+
import com.fasterxml.jackson.annotation.JsonProperty;
30+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
35+
import static org.hamcrest.Matchers.is;
36+
import static org.junit.Assert.assertThat;
37+
38+
/**
39+
* @author Michele Rastelli
40+
*/
41+
public class CustomTypeHintTest {
42+
43+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "type")
44+
public interface Animal {
45+
String getName();
46+
}
47+
48+
public static class Gorilla implements Animal {
49+
private String name;
50+
51+
@Override
52+
public String getName() {
53+
return name;
54+
}
55+
56+
public void setName(String name) {
57+
this.name = name;
58+
}
59+
}
60+
61+
public static class Zoo {
62+
63+
@JsonProperty("_key")
64+
private String key;
65+
66+
private Animal animal;
67+
68+
public String getKey() {
69+
return key;
70+
}
71+
72+
public void setKey(String key) {
73+
this.key = key;
74+
}
75+
76+
public Animal getAnimal() {
77+
return animal;
78+
}
79+
80+
public void setAnimal(Animal animal) {
81+
this.animal = animal;
82+
}
83+
}
84+
85+
private static final String COLLECTION_NAME = "collection";
86+
87+
private ArangoDatabase db;
88+
private ArangoCollection collection;
89+
90+
@Before
91+
public void init() {
92+
ArangoDB arangoDB = new ArangoDB.Builder()
93+
.serializer(new VelocyJack())
94+
.build();
95+
96+
String TEST_DB = "custom-serde-test";
97+
db = arangoDB.db(TEST_DB);
98+
if (!db.exists()) {
99+
db.create();
100+
}
101+
102+
collection = db.collection(COLLECTION_NAME);
103+
if (!collection.exists()) {
104+
collection.create();
105+
}
106+
}
107+
108+
@After
109+
public void shutdown() {
110+
if (db.exists())
111+
db.drop();
112+
}
113+
114+
@Test
115+
public void insertDocument() {
116+
Gorilla gorilla = new Gorilla();
117+
gorilla.setName("kingKong");
118+
119+
Zoo doc = new Zoo();
120+
doc.setAnimal(gorilla);
121+
122+
Zoo insertedDoc = collection.insertDocument(
123+
doc,
124+
new DocumentCreateOptions().returnNew(true)
125+
).getNew();
126+
127+
assertThat((insertedDoc.getAnimal().getName()), is("kingKong"));
128+
129+
String key = insertedDoc.getKey();
130+
131+
// in the db a document like this is created:
132+
// {
133+
// "animal": {
134+
// "type": "com.arangodb.serde.CustomTypeHintTest$Gorilla",
135+
// "name": "kingKong"
136+
// }
137+
// }
138+
139+
final Zoo readDoc = collection.getDocument(
140+
key,
141+
Zoo.class,
142+
null);
143+
144+
assertThat((readDoc.getAnimal().getName()), is("kingKong"));
145+
}
146+
}

0 commit comments

Comments
 (0)