diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java b/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java index eae9df9..10deeed 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java +++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueOperations.java @@ -36,7 +36,7 @@ public interface KeyValueOperations extends DisposableBean { * Add given object. Object needs to have id property to which a generated value will be assigned. * * @param objectToInsert - * @return + * @return the inserted object. */ T insert(T objectToInsert); @@ -45,8 +45,9 @@ public interface KeyValueOperations extends DisposableBean { * * @param id must not be {@literal null}. * @param objectToInsert must not be {@literal null}. + * @return the inserted object. */ - void insert(Object id, Object objectToInsert); + T insert(Object id, T objectToInsert); /** * Get all elements of given type. Respects {@link KeySpace} if present and therefore returns all elements that can be @@ -121,14 +122,16 @@ public interface KeyValueOperations extends DisposableBean { /** * @param objectToUpdate must not be {@literal null}. + * @return the updated object. */ - void update(Object objectToUpdate); + T update(T objectToUpdate); /** * @param id must not be {@literal null}. * @param objectToUpdate must not be {@literal null}. + * @return the updated object. */ - void update(Object id, Object objectToUpdate); + T update(Object id, T objectToUpdate); /** * Remove all elements of type. Respects {@link KeySpace} if present and therefore removes all elements that can be diff --git a/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java b/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java index aa92b74..5f19a61 100644 --- a/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java +++ b/src/main/java/org/springframework/data/keyvalue/core/KeyValueTemplate.java @@ -137,8 +137,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub entity.getIdProperty(), identifierGenerator); Object id = generatingIdAccessor.getOrGenerateIdentifier(); - insert(id, objectToInsert); - return objectToInsert; + return insert(id, objectToInsert); } /* @@ -146,7 +145,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub * @see org.springframework.data.keyvalue.core.KeyValueOperations#insert(java.lang.Object, java.lang.Object) */ @Override - public void insert(Object id, Object objectToInsert) { + public T insert(Object id, T objectToInsert) { Assert.notNull(id, "Id for object to be inserted must not be null!"); Assert.notNull(objectToInsert, "Object to be inserted must not be null!"); @@ -167,6 +166,8 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub }); potentiallyPublishEvent(KeyValueEvent.afterInsert(id, keyspace, objectToInsert.getClass(), objectToInsert)); + + return objectToInsert; } /* @@ -175,7 +176,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub */ @SuppressWarnings("rawtypes") @Override - public void update(Object objectToUpdate) { + public T update(T objectToUpdate) { KeyValuePersistentEntity entity = getKeyValuePersistentEntity(objectToUpdate); @@ -184,7 +185,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub String.format("Cannot determine id for type %s", ClassUtils.getUserClass(objectToUpdate))); } - update(entity.getIdentifierAccessor(objectToUpdate).getRequiredIdentifier(), objectToUpdate); + return update(entity.getIdentifierAccessor(objectToUpdate).getRequiredIdentifier(), objectToUpdate); } /* @@ -192,7 +193,7 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub * @see org.springframework.data.keyvalue.core.KeyValueOperations#update(java.lang.Object, java.lang.Object) */ @Override - public void update(Object id, Object objectToUpdate) { + public T update(Object id, T objectToUpdate) { Assert.notNull(id, "Id for object to be inserted must not be null!"); Assert.notNull(objectToUpdate, "Object to be updated must not be null!"); @@ -205,6 +206,8 @@ public class KeyValueTemplate implements KeyValueOperations, ApplicationEventPub potentiallyPublishEvent( KeyValueEvent.afterUpdate(id, keyspace, objectToUpdate.getClass(), objectToUpdate, existing)); + + return objectToUpdate; } /* diff --git a/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java b/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java index fc8a961..b78b1e3 100644 --- a/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java +++ b/src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java @@ -98,11 +98,10 @@ public class SimpleKeyValueRepository implements KeyValueRepository implements KeyValueRepository Iterable saveAll(Iterable entities) { - entities.forEach(this::save); + List saved = new ArrayList<>(); - return entities; + for (S entity : entities) { + saved.add(save(entity)); + } + + return saved; } /* diff --git a/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java b/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java index a54acec..00eec41 100644 --- a/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java +++ b/src/test/java/org/springframework/data/keyvalue/core/KeyValueTemplateUnitTests.java @@ -110,6 +110,15 @@ public class KeyValueTemplateUnitTests { verify(adapterMock, times(1)).put("1", FOO_ONE, Foo.class.getName()); } + @Test // DATACMNS-225 + public void insertShouldReturnInsertedObject() { + + ClassWithStringId object = new ClassWithStringId(); + + assertThat(template.insert(object), is(object)); + assertThat(template.insert("1", object), is(object)); + } + @Test // DATACMNS-525 public void insertShouldThrowExceptionWhenObectWithIdAlreadyExists() { @@ -225,6 +234,16 @@ public class KeyValueTemplateUnitTests { verify(adapterMock, times(1)).put("1", FOO_TWO, Foo.class.getName()); } + @Test // DATAKV-225 + public void updateShouldReturnUpdatedObject() { + + ClassWithStringId object = new ClassWithStringId(); + object.id = "foo"; + + assertThat(template.update(object), is(object)); + assertThat(template.update("1", object), is(object)); + } + @Test(expected = IllegalArgumentException.class) // DATACMNS-525 public void updateShouldThrowExceptionWhenGivenNullId() { template.update(null, FOO_ONE);