Skip to content

Commit 1203e3a

Browse files
committed
typed ArangoCollection#insertDocument
1 parent 0e45426 commit 1203e3a

File tree

6 files changed

+262
-17
lines changed

6 files changed

+262
-17
lines changed

src/main/java/com/arangodb/ArangoCollection.java

+14
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ public interface ArangoCollection extends ArangoSerdeAccessor {
7676
*/
7777
<T> DocumentCreateEntity<T> insertDocument(T value, DocumentCreateOptions options) throws ArangoDBException;
7878

79+
/**
80+
* Creates a new document from the given document, unless there is already a document with the _key given. If no
81+
* _key is given, a new unique _key is generated automatically.
82+
*
83+
* @param value A representation of a single document (POJO, {@link com.arangodb.util.RawJson} or {@link com.arangodb.util.RawBytes})
84+
* @param options Additional options, can be null
85+
* @param type Deserialization target type for the returned documents.
86+
* @return information about the document
87+
* @throws ArangoDBException
88+
* @see <a href="https://www.arangodb.com/docs/stable/http/document-working-with-documents.html#create-document">API
89+
* Documentation</a>
90+
*/
91+
<T> DocumentCreateEntity<T> insertDocument(T value, DocumentCreateOptions options, Class<T> type) throws ArangoDBException;
92+
7993
/**
8094
* Creates new documents from the given documents, unless there is already a document with the _key given. If no
8195
* _key is given, a new unique _key is generated automatically.

src/main/java/com/arangodb/async/ArangoCollectionAsync.java

+13
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ public interface ArangoCollectionAsync extends ArangoSerdeAccessor {
7575
*/
7676
<T> CompletableFuture<DocumentCreateEntity<T>> insertDocument(final T value, final DocumentCreateOptions options);
7777

78+
/**
79+
* Creates a new document from the given document, unless there is already a document with the _key given. If no
80+
* _key is given, a new unique _key is generated automatically.
81+
*
82+
* @param value A representation of a single document (POJO, {@link com.arangodb.util.RawJson} or {@link com.arangodb.util.RawBytes})
83+
* @param options Additional options, can be null
84+
* @param type Deserialization target type for the returned documents.
85+
* @return information about the document
86+
* @see <a href="https://www.arangodb.com/docs/stable/http/document-working-with-documents.html#create-document">API
87+
* Documentation</a>
88+
*/
89+
<T> CompletableFuture<DocumentCreateEntity<T>> insertDocument(final T value, final DocumentCreateOptions options, Class<T> type);
90+
7891
/**
7992
* Creates new documents from the given documents, unless there is already a document with the _key given. If no
8093
* _key is given, a new unique _key is generated automatically.

src/main/java/com/arangodb/async/internal/ArangoCollectionAsyncImpl.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,17 @@ public <T> CompletableFuture<DocumentCreateEntity<T>> insertDocument(final T val
5353
}
5454

5555
@Override
56+
@SuppressWarnings("unchecked")
5657
public <T> CompletableFuture<DocumentCreateEntity<T>> insertDocument(
5758
final T value,
5859
final DocumentCreateOptions options) {
60+
return insertDocument(value, options, (Class<T>) value.getClass());
61+
}
62+
63+
@Override
64+
public <T> CompletableFuture<DocumentCreateEntity<T>> insertDocument(T value, DocumentCreateOptions options, Class<T> type) {
5965
return executor.execute(insertDocumentRequest(value, options),
60-
constructParametricType(DocumentCreateEntity.class, value.getClass()));
66+
constructParametricType(DocumentCreateEntity.class, type));
6167
}
6268

6369
@Override

src/main/java/com/arangodb/internal/ArangoCollectionImpl.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,16 @@ public <T> DocumentCreateEntity<T> insertDocument(final T value) throws ArangoDB
5252
}
5353

5454
@Override
55+
@SuppressWarnings("unchecked")
5556
public <T> DocumentCreateEntity<T> insertDocument(final T value, final DocumentCreateOptions options)
5657
throws ArangoDBException {
58+
return insertDocument(value, options, (Class<T>) value.getClass());
59+
}
60+
61+
@Override
62+
public <T> DocumentCreateEntity<T> insertDocument(final T value, final DocumentCreateOptions options, final Class<T> type) throws ArangoDBException {
5763
return executor.execute(insertDocumentRequest(value, options),
58-
constructParametricType(DocumentCreateEntity.class, value.getClass()));
64+
constructParametricType(DocumentCreateEntity.class, type));
5965
}
6066

6167
@Override

src/test/java/com/arangodb/ArangoCollectionTest.java

+109-15
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,15 @@
2020

2121
package com.arangodb;
2222

23-
import com.arangodb.entity.BaseDocument;
24-
import com.arangodb.entity.BaseEdgeDocument;
25-
import com.arangodb.entity.CollectionEntity;
26-
import com.arangodb.entity.CollectionPropertiesEntity;
27-
import com.arangodb.entity.CollectionRevisionEntity;
28-
import com.arangodb.entity.DocumentCreateEntity;
29-
import com.arangodb.entity.DocumentDeleteEntity;
30-
import com.arangodb.entity.DocumentEntity;
31-
import com.arangodb.entity.DocumentImportEntity;
32-
import com.arangodb.entity.DocumentUpdateEntity;
33-
import com.arangodb.entity.IndexEntity;
34-
import com.arangodb.entity.IndexType;
35-
import com.arangodb.entity.MultiDocumentEntity;
36-
import com.arangodb.entity.Permissions;
37-
import com.arangodb.entity.ShardEntity;
23+
import com.arangodb.entity.*;
3824
import com.arangodb.model.*;
3925
import com.arangodb.model.DocumentImportOptions.OnDuplicate;
26+
import com.arangodb.serde.JacksonSerde;
4027
import com.arangodb.util.MapBuilder;
4128
import com.arangodb.util.RawBytes;
4229
import com.arangodb.util.RawJson;
4330
import com.fasterxml.jackson.annotation.JsonInclude;
31+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
4432
import com.fasterxml.jackson.core.JsonProcessingException;
4533
import com.fasterxml.jackson.databind.ObjectMapper;
4634
import org.junit.jupiter.api.BeforeAll;
@@ -90,6 +78,78 @@ static void init() {
9078
initEdgeCollections(EDGE_COLLECTION_NAME);
9179
}
9280

81+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "type")
82+
public interface Animal {
83+
String getKey();
84+
85+
String getName();
86+
}
87+
88+
public static class Dog implements Animal {
89+
90+
@Key
91+
private String key;
92+
private String name;
93+
94+
public Dog() {
95+
}
96+
97+
public Dog(String key, String name) {
98+
this.key = key;
99+
this.name = name;
100+
}
101+
102+
@Override
103+
public String getKey() {
104+
return key;
105+
}
106+
107+
public void setKey(String key) {
108+
this.key = key;
109+
}
110+
111+
@Override
112+
public String getName() {
113+
return name;
114+
}
115+
116+
public void setName(String name) {
117+
this.name = name;
118+
}
119+
}
120+
121+
public static class Cat implements Animal {
122+
@Key
123+
private String key;
124+
private String name;
125+
126+
public Cat() {
127+
}
128+
129+
public Cat(String key, String name) {
130+
this.key = key;
131+
this.name = name;
132+
}
133+
134+
@Override
135+
public String getKey() {
136+
return key;
137+
}
138+
139+
public void setKey(String key) {
140+
this.key = key;
141+
}
142+
143+
@Override
144+
public String getName() {
145+
return name;
146+
}
147+
148+
public void setName(String name) {
149+
this.name = name;
150+
}
151+
}
152+
93153
@ParameterizedTest(name = "{index}")
94154
@MethodSource("cols")
95155
void insertDocument(ArangoCollection collection) {
@@ -160,6 +220,40 @@ void insertDocumentReturnNew(ArangoCollection collection) {
160220
assertThat(doc.getNew()).isNotNull();
161221
}
162222

223+
@ParameterizedTest(name = "{index}")
224+
@MethodSource("cols")
225+
void insertDocumentWithTypeOverwriteModeReplace(ArangoCollection collection) {
226+
assumeTrue(isAtLeastVersion(3, 7));
227+
assumeTrue(collection.getSerde().getUserSerde() instanceof JacksonSerde, "polymorphic deserialization support required");
228+
229+
String key = UUID.randomUUID().toString();
230+
Dog dog = new Dog(key, "Teddy");
231+
Cat cat = new Cat(key, "Luna");
232+
233+
final DocumentCreateOptions options = new DocumentCreateOptions()
234+
.returnNew(true)
235+
.returnOld(true)
236+
.overwriteMode(OverwriteMode.replace);
237+
collection.insertDocument(dog, options);
238+
final DocumentCreateEntity<Animal> doc = collection.insertDocument(cat, options, Animal.class);
239+
assertThat(doc).isNotNull();
240+
assertThat(doc.getId()).isNotNull();
241+
assertThat(doc.getKey()).isNotNull().isEqualTo(key);
242+
assertThat(doc.getRev()).isNotNull();
243+
244+
assertThat(doc.getOld())
245+
.isNotNull()
246+
.isInstanceOf(Dog.class);
247+
assertThat(doc.getOld().getKey()).isEqualTo(key);
248+
assertThat(doc.getOld().getName()).isEqualTo("Teddy");
249+
250+
assertThat(doc.getNew())
251+
.isNotNull()
252+
.isInstanceOf(Cat.class);
253+
assertThat(doc.getNew().getKey()).isEqualTo(key);
254+
assertThat(doc.getNew().getName()).isEqualTo("Luna");
255+
}
256+
163257
@ParameterizedTest(name = "{index}")
164258
@MethodSource("cols")
165259
void insertDocumentOverwriteModeIgnore(ArangoCollection collection) {

src/test/java/com/arangodb/async/ArangoCollectionTest.java

+112
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@
2020

2121
package com.arangodb.async;
2222

23+
import com.arangodb.ArangoCollection;
2324
import com.arangodb.ArangoDBException;
2425
import com.arangodb.entity.*;
2526
import com.arangodb.model.*;
2627
import com.arangodb.model.DocumentImportOptions.OnDuplicate;
28+
import com.arangodb.serde.JacksonSerde;
2729
import com.arangodb.util.RawJson;
30+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
2831
import org.junit.jupiter.api.AfterEach;
2932
import org.junit.jupiter.api.BeforeAll;
3033
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.params.ParameterizedTest;
35+
import org.junit.jupiter.params.provider.MethodSource;
3136

3237
import java.util.*;
3338
import java.util.concurrent.CompletableFuture;
@@ -48,6 +53,78 @@ class ArangoCollectionTest extends BaseTest {
4853

4954
private static final String COLLECTION_NAME = "db_collection_test";
5055

56+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "type")
57+
public interface Animal {
58+
String getKey();
59+
60+
String getName();
61+
}
62+
63+
public static class Dog implements Animal {
64+
65+
@Key
66+
private String key;
67+
private String name;
68+
69+
public Dog() {
70+
}
71+
72+
public Dog(String key, String name) {
73+
this.key = key;
74+
this.name = name;
75+
}
76+
77+
@Override
78+
public String getKey() {
79+
return key;
80+
}
81+
82+
public void setKey(String key) {
83+
this.key = key;
84+
}
85+
86+
@Override
87+
public String getName() {
88+
return name;
89+
}
90+
91+
public void setName(String name) {
92+
this.name = name;
93+
}
94+
}
95+
96+
public static class Cat implements Animal {
97+
@Key
98+
private String key;
99+
private String name;
100+
101+
public Cat() {
102+
}
103+
104+
public Cat(String key, String name) {
105+
this.key = key;
106+
this.name = name;
107+
}
108+
109+
@Override
110+
public String getKey() {
111+
return key;
112+
}
113+
114+
public void setKey(String key) {
115+
this.key = key;
116+
}
117+
118+
@Override
119+
public String getName() {
120+
return name;
121+
}
122+
123+
public void setName(String name) {
124+
this.name = name;
125+
}
126+
}
127+
51128
ArangoCollectionTest() throws ExecutionException, InterruptedException {
52129
ArangoCollectionAsync collection = db.collection(COLLECTION_NAME);
53130
if (!collection.exists().get()) {
@@ -103,6 +180,41 @@ void insertDocumentReturnNew() throws InterruptedException, ExecutionException {
103180
.get();
104181
}
105182

183+
184+
@Test
185+
void insertDocumentWithTypeOverwriteModeReplace() throws InterruptedException, ExecutionException {
186+
assumeTrue(isAtLeastVersion(3, 7));
187+
assumeTrue(db.getSerde().getUserSerde() instanceof JacksonSerde, "polymorphic deserialization support required");
188+
ArangoCollectionAsync collection = db.collection(COLLECTION_NAME);
189+
String key = UUID.randomUUID().toString();
190+
Dog dog = new Dog(key, "Teddy");
191+
Cat cat = new Cat(key, "Luna");
192+
193+
final DocumentCreateOptions options = new DocumentCreateOptions()
194+
.returnNew(true)
195+
.returnOld(true)
196+
.overwriteMode(OverwriteMode.replace);
197+
collection.insertDocument(dog, options).get();
198+
final DocumentCreateEntity<Animal> doc = collection.insertDocument(cat, options, Animal.class).get();
199+
assertThat(doc).isNotNull();
200+
assertThat(doc.getId()).isNotNull();
201+
assertThat(doc.getKey()).isNotNull().isEqualTo(key);
202+
assertThat(doc.getRev()).isNotNull();
203+
204+
assertThat(doc.getOld())
205+
.isNotNull()
206+
.isInstanceOf(Dog.class);
207+
assertThat(doc.getOld().getKey()).isEqualTo(key);
208+
assertThat(doc.getOld().getName()).isEqualTo("Teddy");
209+
210+
assertThat(doc.getNew())
211+
.isNotNull()
212+
.isInstanceOf(Cat.class);
213+
assertThat(doc.getNew().getKey()).isEqualTo(key);
214+
assertThat(doc.getNew().getName()).isEqualTo("Luna");
215+
}
216+
217+
106218
@Test
107219
void insertDocumentWaitForSync() throws InterruptedException, ExecutionException {
108220
final DocumentCreateOptions options = new DocumentCreateOptions().waitForSync(true);

0 commit comments

Comments
 (0)