DATAJDBC-378 - Proper handling of null and empty collections in JdbcAggregateTemplate.

Original Pull Request: #155
This commit is contained in:
Jens Schauder
2019-05-25 10:25:07 +03:00
committed by Christoph Strobl
parent d701b575e6
commit 619396357a
3 changed files with 47 additions and 1 deletions

View File

@@ -152,6 +152,9 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
*/
@Override
public long count(Class<?> domainType) {
Assert.notNull(domainType, "Domain type must not be null");
return accessStrategy.count(domainType);
}
@@ -162,6 +165,9 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override
public <T> T findById(Object id, Class<T> domainType) {
Assert.notNull(id, "Id must not be null");
Assert.notNull(domainType, "Domain type must not be null");
T entity = accessStrategy.findById(id, domainType);
if (entity != null) {
publishAfterLoad(id, entity);
@@ -175,6 +181,10 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
*/
@Override
public <T> boolean existsById(Object id, Class<T> domainType) {
Assert.notNull(id, "Id must not be null");
Assert.notNull(domainType, "Domain type must not be null");
return accessStrategy.existsById(id, domainType);
}
@@ -185,6 +195,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override
public <T> Iterable<T> findAll(Class<T> domainType) {
Assert.notNull(domainType, "Domain type must not be null");
Iterable<T> all = accessStrategy.findAll(domainType);
publishAfterLoad(all);
return all;
@@ -197,6 +209,9 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
Assert.notNull(ids, "Ids must not be null");
Assert.notNull(domainType, "Domain type must not be null");
Iterable<T> allById = accessStrategy.findAllById(ids, domainType);
publishAfterLoad(allById);
return allById;
@@ -209,6 +224,9 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override
public <S> void delete(S aggregateRoot, Class<S> domainType) {
Assert.notNull(aggregateRoot, "Aggregate root must not be null");
Assert.notNull(domainType, "Domain type must not be null");
IdentifierAccessor identifierAccessor = context.getRequiredPersistentEntity(domainType)
.getIdentifierAccessor(aggregateRoot);
@@ -221,6 +239,10 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
*/
@Override
public <S> void deleteById(Object id, Class<S> domainType) {
Assert.notNull(id, "Id must not be null");
Assert.notNull(domainType, "Domain type must not be null");
deleteTree(id, null, domainType);
}
@@ -231,6 +253,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
@Override
public void deleteAll(Class<?> domainType) {
Assert.notNull(domainType, "Domain type must not be null");
AggregateChange<?> change = createDeletingChange(domainType);
change.executeWith(interpreter, context, converter);
}

View File

@@ -18,6 +18,7 @@ package org.springframework.data.jdbc.core.convert;
import java.sql.JDBCType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -241,6 +242,10 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
@SuppressWarnings("unchecked")
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
if (!ids.iterator().hasNext()) {
return Collections.emptyList();
}
RelationalPersistentProperty idProperty = getRequiredPersistentEntity(domainType).getRequiredIdProperty();
MapSqlParameterSource parameterSource = new MapSqlParameterSource();

View File

@@ -35,7 +35,6 @@ import org.junit.Assume;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
@@ -597,6 +596,24 @@ public class JdbcAggregateTemplateIntegrationTests {
});
}
@Test // DATAJDBC-378
public void findAllByIdMustNotAcceptNullArgumentForType() {
assertThatThrownBy(() -> template.findAllById(singleton(23L), null)).isInstanceOf(IllegalArgumentException.class);
}
@Test // DATAJDBC-378
public void findAllByIdMustNotAcceptNullArgumentForIds() {
assertThatThrownBy(() -> template.findAllById(null, LegoSet.class)).isInstanceOf(IllegalArgumentException.class);
}
@Test // DATAJDBC-378
public void findAllByIdWithEmpthListMustReturnEmptyResult() {
assertThat(template.findAllById(emptyList(), LegoSet.class)).isEmpty();
}
private static NoIdMapChain4 createNoIdMapTree() {
NoIdMapChain4 chain4 = new NoIdMapChain4();