10000 convert ArangoOperations.replace() · arangodb/spring-data@f95c012 · GitHub
[go: up one dir, main page]

Skip to content

Commit f95c012

Browse files
committed
convert ArangoOperations.replace()
1 parent 93dea3f commit f95c012

File tree

5 files changed

+77
-26
lines changed

5 files changed

+77
-26
lines changed

src/main/java/com/arangodb/springframework/core/ArangoOperations.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ <T> MultiDocumentEntity<DocumentUpdateEntity<T>> updateAll(
256256
* @return information about the documents
257257
* @throws DataAccessException
258258
*/
259-
<T> MultiDocumentEntity<? extends DocumentEntity> replace(
260-
Iterable<T> values,
261-
Class<T> entityClass,
262-
DocumentReplaceOptions options) throws DataAccessException;
259+
<T> MultiDocumentEntity<DocumentUpdateEntity<T>> replaceAll(
260+
Iterable<? extends T> values,
261+
DocumentReplaceOptions options,
262+
Class<T> entityClass) throws DataAccessException;
263263

264264
/**
265265
* Replaces multiple documents in the specified collection with the ones in the values, the replaced documents are
@@ -274,7 +274,7 @@ <T> MultiDocumentEntity<? extends DocumentEntity> replace(
274274
* @return information about the documents
275275
* @throws DataAccessException
276276
*/
277-
<T> MultiDocumentEntity<? extends DocumentEntity> replace(Iterable<T> values, Class<T> entityClass)
277+
<T> MultiDocumentEntity<DocumentUpdateEntity<?>> replaceAll(Iterable<? extends T> values, Class<T> entityClass)
278278
throws DataAccessException;
279279

280280
/**
@@ -290,7 +290,7 @@ <T> MultiDocumentEntity<? extends DocumentEntity> replace(Iterable<T> values, Cl
290290
* @return information about the document
291291
* @throws DataAccessException
292292
*/
293-
<T> DocumentEntity replace(Object id, T value, DocumentReplaceOptions options) throws DataAccessException;
293+
<T> DocumentUpdateEntity<T> replace(Object id, T value, DocumentReplaceOptions options) throws DataAccessException;
294294

295295
/**
296296
* Replaces the document with {@code id} with the one in the body, provided there is such a document and no
@@ -303,7 +303,7 @@ <T> MultiDocumentEntity<? extends DocumentEntity> replace(Iterable<T> values, Cl
303303
* @return information about the document
304304
* @throws DataAccessException
305305
*/
306-
<T> DocumentEntity replace(Object id, T value) throws DataAccessException;
306+
DocumentUpdateEntity<?> replace(Object id, Object value) throws DataAccessException;
307307

308308
/**
309309
* Retrieves the document with the given {@code id} from a collection.
@@ -377,8 +377,6 @@ <T> MultiDocumentEntity<DocumentCreateEntity<T>> insertAll(
377377
* Creates new documents from the given documents, unless there is already a document with the _key given. If no
378378
* _key is given, a new unique _key is generated automatically.
379379
*
380-
* @param <T>
381-
*
382380
* @param values
383381
* A List of documents
384382
* @param entityClass

src/main/java/com/arangodb/springframework/core/template/ArangoTemplate.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,14 +398,17 @@ public DocumentUpdateEntity<?> update(final Object id, final Object value) throw
398398
}
399399

400400
@Override
401-
public <T> MultiDocumentEntity<? extends DocumentEntity> replace(final Iterable<T> values,
402-
final Class<T> entityClass, final DocumentReplaceOptions options) throws DataAccessException {
401+
public <T> MultiDocumentEntity<DocumentUpdateEntity<T>> replaceAll(
402+
final Iterable<? extends T> values,
403+
final DocumentReplaceOptions options,
404+
final Class<T> entityClass
405+
) throws DataAccessException {
403406

404407
potentiallyEmitBeforeSaveEvent(values);
405408

406-
final MultiDocumentEntity<? extends DocumentEntity> result;
409+
MultiDocumentEntity<DocumentUpdateEntity<T>> result;
407410
try {
408-
result = _collection(entityClass).replaceDocuments(values, options);
411+
result = _collection(entityClass).replaceDocuments(values, options, entityClass);
409412
} catch (final ArangoDBException e) {
410413
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
411414
}
@@ -416,17 +419,20 @@ public <T> MultiDocumentEntity<? extends DocumentEntity> replace(final Iterable<
416419
}
417420

418421
@Override
419-
public <T> MultiDocumentEntity<? extends DocumentEntity> replace(final Iterable<T> values,
420-
final Class<T> entityClass) throws DataAccessException {
421-
return replace(values, entityClass, new DocumentReplaceOptions());
422+
@SuppressWarnings({"rawtypes", "unchecked"})
423+
public <T> MultiDocumentEntity<DocumentUpdateEntity<?>> replaceAll(
424+
final Iterable<? extends T> values,
425+
final Class<T> entityClass
426+
) throws DataAccessException {
427+
return replaceAll(values, new DocumentReplaceOptions(), (Class) entityClass);
422428
}
423429

424430
@Override
425-
public DocumentEntity replace(final Object id, final Object value, final DocumentReplaceOptions options)
431+
public <T> DocumentUpdateEntity<T> replace(final Object id, final T value, final DocumentReplaceOptions options)
426432
throws DataAccessException {
427433
potentiallyEmitEvent(new BeforeSaveEvent<>(value));
428434

429-
final DocumentEntity result;
435+
DocumentUpdateEntity<T> result;
430436
try {
431437
result = _collection(value.getClass(), id).replaceDocument(determineDocumentKeyFromId(id), value, options);
432438
} catch (final ArangoDBException e) {
@@ -439,7 +445,7 @@ public DocumentEntity replace(final Object id, final Object value, final Documen
439445
}
440446

441447
@Override
442-
public DocumentEntity replace(final Object id, final Object value) throws DataAccessException {
448+
public DocumentUpdateEntity<?> replace(final Object id, final Object value) throws DataAccessException {
443449
return replace(id, value, new DocumentReplaceOptions());
444450
}
445451

src/test/java/com/arangodb/springframework/core/mapping/event/SaveEventTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void replaceMultiSaveEvent() {
146146
listener.afterSaveEvents.clear();
147147
john.setId("non-existing-id");
148148
bob.setAge(30);
149-
template.replace(customers, Customer.class);
149+
template.replaceAll(customers, Customer.class);
150150

151151
assertThat(listener.beforeSaveEvents.size(), is(2));
152152
for (final BeforeSaveEvent<Customer> event : listener.beforeSaveEvents) {

src/test/java/com/arangodb/springframework/core/template/ArangoTemplateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public void replaceDocuments() {
220220
final Product documentB = template.find(b.getId(), Product.class).get();
221221
documentB.setName("bb");
222222

223-
final MultiDocumentEntity<? extends DocumentEntity> res = template.replace(Arrays.asList(documentA, documentB),
223+
final MultiDocumentEntity<? extends DocumentEntity> res = template.replaceAll(Arrays.asList(documentA, documentB),
224224
Product.class);
225225
assertThat(res, is(notNullValue()));
226226
assertThat(res.getDocuments().size(), is(2));

src/test/java/com/arangodb/springframework/example/polymorphic/template/PolymorphicTemplate.java

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,25 @@
2020

2121
package com.arangodb.springframework.example.polymorphic.template;
2222

23+
import com.arangodb.ArangoCollection;
2324
import com.arangodb.ArangoCursor;
2425
import com.arangodb.entity.DocumentCreateEntity;
2526
import com.arangodb.entity.DocumentDeleteEntity;
2627
import com.arangodb.entity.DocumentUpdateEntity;
2728
import com.arangodb.entity.MultiDocumentEntity;
2829
import com.arangodb.model.DocumentCreateOptions;
2930
import com.arangodb.model.DocumentDeleteOptions;
31+
import com.arangodb.model.DocumentReplaceOptions;
3032
import com.arangodb.model.DocumentUpdateOptions;
3133
import com.arangodb.springframework.AbstractArangoTest;
34+
import com.arangodb.springframework.ArangoTestConfiguration;
3235
import com.arangodb.springframework.example.polymorphic.entity.Animal;
3336
import com.arangodb.springframework.example.polymorphic.entity.Dog;
3437
import com.arangodb.springframework 10000 .example.polymorphic.entity.Eagle;
38+
import org.junit.jupiter.api.BeforeEach;
3539
import org.junit.jupiter.api.Test;
3640

3741
import java.util.List;
38-
import java.util.stream.Collectors;
3942

4043
import static org.hamcrest.MatcherAssert.assertThat;
4144
import static org.hamcrest.Matchers.is;
@@ -46,6 +49,15 @@
4649
*/
4750
public class PolymorphicTemplate extends AbstractArangoTest {
4851

52+
private ArangoCollection col() {
53+
return template.driver().db(ArangoTestConfiguration.DB).collection("animals");
54+
}
55+
56+
@BeforeEach
57+
void init() {
58+
template.collection(Animal.class).truncate();
59+
}
60+
4961
@Test
5062
public void query() {
5163
Dog dog = new Dog();
@@ -78,6 +90,8 @@ public void insertVariance() {
7890

7991
DocumentCreateEntity<Animal> res = template.insert(dog, new DocumentCreateOptions().returnNew(true));
8092
assertThat(res.getNew(), is(dog));
93+
94+
assertThat(col().documentExists(dog.getId()), is(true));
8195
}
8296

8397
@Test
@@ -97,6 +111,9 @@ public void insertAllVariance() {
97111
MultiDocumentEntity<DocumentCreateEntity<Animal>> res =
98112
template.insertAll(dogs, new DocumentCreateOptions().returnNew(true), Animal.class);
99113
assertThat(res.getDocuments().stream().map(DocumentCreateEntity::getNew).toList().containsAll(dogs), is(true));
114+
115+
assertThat(col().documentExists(dog1.getId()), is(true));
116+
assertThat(col().documentExists(dog2.getId()), is(true));
100117
}
101118

102119
@Test
@@ -137,8 +154,8 @@ public void updateVariance() {
137154
dog.setTeeths(11);
138155

139156
template.insert(dog);
140-
DocumentUpdateEntity<Animal> res = template.update(dog.getId(), dog, new DocumentUpdateOptions().returnNew(true));
141-
assertThat(res.getNew(), is(dog));
157+
DocumentUpdateEntity<Animal> res = template.update(dog.getId(), dog, new DocumentUpdateOptions().returnOld(true));
158+
assertThat(res.getOld(), is(dog));
142159
}
143160

144161
@Test
@@ -155,8 +172,38 @@ public void updateAllVariance() {
155172

156173
List<Dog> dogs = List.of(dog1, dog2);
157174
template.insertAll(dogs, Animal.class);
158-
MultiDocumentEntity<DocumentUpdateEntity<Animal>> res = template.updateAll(dogs, new DocumentUpdateOptions().returnNew(true), Animal.class);
159-
assertThat(res.getDocuments().stream().map(DocumentUpdateEntity::getNew).toList().containsAll(dogs), is(true));
175+
MultiDocumentEntity<DocumentUpdateEntity<Animal>> res = template.updateAll(dogs, new DocumentUpdateOptions().returnOld(true), Animal.class);
176+
assertThat(res.getDocuments().stream().map(DocumentUpdateEntity::getOld).toList().containsAll(dogs), is(true));
177+
}
178+
179+
@Test
180+
public void replaceVariance() {
181+
Dog dog = new Dog();
182+
dog.setId("1");
183+
dog.setName("dog");
184+
dog.setTeeths(11);
185+
186+
template.insert(dog);
187+
DocumentUpdateEntity<Animal> res = template.replace(dog.getId(), dog, new DocumentReplaceOptions().returnOld(true));
188+
assertThat(res.getOld(), is(dog));
189+
}
190+
191+
@Test
192+
public void replaceAllVariance() {
193+
Dog dog1 = new Dog();
194+
dog1.setId("1");
195+
dog1.setName("dog1");
196+
dog1.setTeeths(11);
197+
198+
Dog dog2 = new Dog();
199+
dog1.setId("2");
200+
dog1.setName("dog2");
201+
dog1.setTeeths(22);
202+
203+
List<Dog> dogs = List.of(dog1, dog2);
204+
template.insertAll(dogs, Animal.class);
205+
MultiDocumentEntity<DocumentUpdateEntity<Animal>> res = template.replaceAll(dogs, new DocumentReplaceOptions().returnOld(true), Animal.class);
206+
assertThat(res.getDocuments().stream().map(DocumentUpdateEntity::getOld).toList().containsAll(dogs), is(true));
160207
}
161208

162209
}

0 commit comments

Comments
 (0)
0